CPUnit 0.95 (beta)
The REAL C++ port of JUnit.
|
00001 /* 00002 Copyright (c) 2011 Daniel Bakkelund. 00003 All rights reserved. 00004 00005 Redistribution and use in source and binary forms, with or without 00006 modification, are permitted provided that the following conditions 00007 are met: 00008 1. Redistributions of source code must retain the above copyright 00009 notice, this list of conditions and the following disclaimer. 00010 2. Redistributions in binary form must reproduce the above copyright 00011 notice, this list of conditions and the following disclaimer in the 00012 documentation and/or other materials provided with the distribution. 00013 3. Neither the name of the copyright holders nor the names of its 00014 contributors may be used to endorse or promote products derived from 00015 this software without specific prior written permission. 00016 00017 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00018 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00021 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00022 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00023 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00024 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00025 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00026 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 00027 THE POSSIBILITY OF SUCH DAMAGE. 00028 */ 00029 00030 00031 00032 #include "cpunit_AssertionException.hpp" 00033 #include "cpunit_Callable.hpp" 00034 #include "cpunit_TestStore.hpp" 00035 #include "cpunit_TestExecutionFacade.hpp" 00036 #include "cpunit_TestRunner.hpp" 00037 #include "cpunit_RunAllTestRunner.hpp" 00038 #include "cpunit_BasicTestRunner.hpp" 00039 #include "cpunit_ExecutionReport.hpp" 00040 #include "cpunit_TimeFormat.hpp" 00041 #include "cpunit_TimeGuardRunner.hpp" 00042 #include "cpunit_trace.hpp" 00043 00044 #include <exception> 00045 #include <iostream> 00046 #include <iomanip> 00047 #include <vector> 00048 #include <algorithm> 00049 #include <string> 00050 00051 cpunit::TestExecutionFacade::TestExecutionFacade() { 00052 CPUNIT_DTRACE("TestExecutionFacade::TestExecutionFacade()"); 00053 } 00054 00055 cpunit::TestExecutionFacade::~TestExecutionFacade() { 00056 CPUNIT_DTRACE("TestExecutionFacade::~TestExecutionFacade()"); 00057 } 00058 00059 std::vector<cpunit::ExecutionReport> 00060 cpunit::TestExecutionFacade::execute(const std::vector<std::string> &patterns, const double max_time, const bool verbose, const bool robust) { 00061 CPUNIT_ITRACE("TestExecutionFacade::execute Running subtree matching '"<<patterns<<"' in "<<(robust ? "" : "non-")<<"robust mode."); 00062 std::vector<TestUnit> tests; 00063 for (std::size_t i=0; i<patterns.size(); ++i) { 00064 std::vector<TestUnit> part = TestStore::get_instance().get_test_units(patterns[i]); 00065 tests.insert(tests.end(), part.begin(), part.end()); 00066 } 00067 std::auto_ptr<TestRunner> tr = get_test_runner(robust, max_time); 00068 return execute(tests, verbose, *tr); 00069 } 00070 00071 std::vector<cpunit::ExecutionReport> 00072 cpunit::TestExecutionFacade::execute(std::vector<TestUnit> &tests, const bool verbose, const TestRunner &runner) { 00073 00074 // Make sure a newline is allways sent to std::cout at the end. 00075 struct NewlineAppender { 00076 ~NewlineAppender() { 00077 std::cout<<std::endl; 00078 } 00079 } nla; 00080 00081 std::vector<ExecutionReport> result; 00082 for (std::size_t i=0; i<tests.size(); i++) { 00083 if (verbose) { 00084 const RegInfo &ri = tests[i].get_test()->get_reg_info(); 00085 std::cout<<"Running "<<ri.get_path()<<"::"<<ri.get_name()<<' '<<std::flush; 00086 } 00087 00088 ExecutionReport res = runner.run(tests[i]); 00089 result.push_back(res); 00090 00091 if (verbose) { 00092 std::cout<<TimeFormat(res.get_time_spent())<<"s "; 00093 } 00094 std::cout<<report_progress(res.get_execution_result())<<std::flush; 00095 if (verbose) { 00096 std::cout<<std::endl; 00097 } 00098 } 00099 return result; 00100 } 00101 00102 char 00103 cpunit::TestExecutionFacade::report_progress(const ExecutionReport::ExecutionResult r) const { 00104 switch (r) { 00105 case ExecutionReport::OK: 00106 return '.'; 00107 case ExecutionReport::FAILURE: 00108 return 'F'; 00109 case ExecutionReport::ERROR: 00110 return 'E'; 00111 default: 00112 // todo: change exception to something proper. 00113 throw "Unknown execution result."; 00114 } 00115 } 00116 00117 std::auto_ptr<cpunit::TestRunner> 00118 cpunit::TestExecutionFacade::get_test_runner(const bool robust, const double max_time) const { 00119 std::auto_ptr<TestRunner> leaf(new BasicTestRunner); 00120 00121 if (robust) { 00122 CPUNIT_ITRACE("TestExecutionFacade::get_test_runner - Returning robust TestRunner"); 00123 00124 // For handling of extra, custom exceptions, insert your handler here, 00125 // and remember to modify the next decorator insertion... 00126 // std::auto_ptr<TestRunnerDecorator> d1(new MyCustomHandler); 00127 // d1->set_inner(leaf.release()); 00128 00129 // Add a layer of exception handling over the executing test runner 00130 std::auto_ptr<TestRunnerDecorator> d2(new RunAllTestRunner); 00131 d2->set_inner(leaf.release()); 00132 00133 // Add a layer of time taking 00134 std::auto_ptr<TestRunnerDecorator> d3(new TimeGuardRunner(max_time)); 00135 d3->set_inner(d2.release()); 00136 00137 // Add a new layer of exception handling in case the max-time is exceeded 00138 std::auto_ptr<TestRunnerDecorator> d4(new RunAllTestRunner); 00139 d4->set_inner(d3.release()); 00140 00141 return std::auto_ptr<TestRunner>(d4.release()); 00142 } else { 00143 CPUNIT_ITRACE("TestExecutionFacade::get_test_runner - Returning BasicTestRunner"); 00144 00145 // Add a layer of time taking over the executing test runner 00146 std::auto_ptr<TestRunnerDecorator> d1(new TimeGuardRunner(max_time)); 00147 d1->set_inner(leaf.release()); 00148 00149 return std::auto_ptr<TestRunner>(d1.release()); 00150 } 00151 }