1 /*
2 * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the <organization> nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "CppUTest/TestHarness.h"
29 #include "CppUTest/TestTestingFixture.h"
30
31 bool TestTestingFixture::lineOfCodeExecutedAfterCheck = false;
32
TestTestingFixture()33 TestTestingFixture::TestTestingFixture()
34 {
35 output_ = new StringBufferTestOutput();
36 result_ = new TestResult(*output_);
37 genTest_ = new ExecFunctionTestShell();
38 registry_ = new TestRegistry();
39 ownsExecFunction_ = false;
40
41 registry_->setCurrentRegistry(registry_);
42 registry_->addTest(genTest_);
43
44 lineOfCodeExecutedAfterCheck = false;
45 }
46
flushOutputAndResetResult()47 void TestTestingFixture::flushOutputAndResetResult()
48 {
49 output_->flush();
50 delete result_;
51 result_ = new TestResult(*output_);
52 }
53
~TestTestingFixture()54 TestTestingFixture::~TestTestingFixture()
55 {
56 registry_->setCurrentRegistry(NULLPTR);
57 clearExecFunction();
58 delete registry_;
59 delete result_;
60 delete output_;
61 delete genTest_;
62 }
63
clearExecFunction()64 void TestTestingFixture::clearExecFunction()
65 {
66 if (genTest_->testFunction_ && ownsExecFunction_)
67 delete genTest_->testFunction_;
68 }
69
addTest(UtestShell * test)70 void TestTestingFixture::addTest(UtestShell * test)
71 {
72 registry_->addTest(test);
73 }
74
setTestFunction(void (* testFunction)())75 void TestTestingFixture::setTestFunction(void(*testFunction)())
76 {
77 clearExecFunction();
78
79 genTest_->testFunction_ = new ExecFunctionWithoutParameters(testFunction);
80 ownsExecFunction_ = true;
81 }
82
setTestFunction(ExecFunction * testFunction)83 void TestTestingFixture::setTestFunction(ExecFunction* testFunction)
84 {
85 clearExecFunction();
86
87 genTest_->testFunction_ = testFunction;
88
89 ownsExecFunction_ = false;
90 }
91
setSetup(void (* setupFunction)())92 void TestTestingFixture::setSetup(void(*setupFunction)())
93 {
94 genTest_->setup_ = setupFunction;
95 }
96
setTeardown(void (* teardownFunction)())97 void TestTestingFixture::setTeardown(void(*teardownFunction)())
98 {
99 genTest_->teardown_ = teardownFunction;
100 }
101
installPlugin(TestPlugin * plugin)102 void TestTestingFixture::installPlugin(TestPlugin* plugin)
103 {
104 registry_->installPlugin(plugin);
105 }
106
setRunTestsInSeperateProcess()107 void TestTestingFixture::setRunTestsInSeperateProcess()
108 {
109 registry_->setRunTestsInSeperateProcess();
110 }
111
setOutputVerbose()112 void TestTestingFixture::setOutputVerbose()
113 {
114 output_->verbose(TestOutput::level_verbose);
115 }
116
runTestWithMethod(void (* method)())117 void TestTestingFixture::runTestWithMethod(void(*method)())
118 {
119 setTestFunction(method);
120 runAllTests();
121 }
122
runAllTests()123 void TestTestingFixture::runAllTests()
124 {
125 registry_->runAllTests(*result_);
126 }
127
getFailureCount()128 size_t TestTestingFixture::getFailureCount()
129 {
130 return result_->getFailureCount();
131 }
132
getCheckCount()133 size_t TestTestingFixture::getCheckCount()
134 {
135 return result_->getCheckCount();
136 }
137
getTestCount()138 size_t TestTestingFixture::getTestCount()
139 {
140 return result_->getTestCount();
141 }
142
getIgnoreCount()143 size_t TestTestingFixture::getIgnoreCount()
144 {
145 return result_->getIgnoredCount();
146 }
147
getRegistry()148 TestRegistry* TestTestingFixture::getRegistry()
149 {
150 return registry_;
151 }
152
hasTestFailed()153 bool TestTestingFixture::hasTestFailed()
154 {
155 return genTest_->hasFailed();
156 }
157
assertPrintContains(const SimpleString & contains)158 void TestTestingFixture::assertPrintContains(const SimpleString& contains)
159 {
160 STRCMP_CONTAINS(contains.asCharString(), getOutput().asCharString());
161 }
162
assertPrintContainsNot(const SimpleString & contains)163 void TestTestingFixture::assertPrintContainsNot(const SimpleString& contains)
164 {
165 CHECK(! getOutput().contains(contains));
166 }
167
168
getOutput()169 const SimpleString& TestTestingFixture::getOutput()
170 {
171 return output_->getOutput();
172 }
173
getRunCount()174 size_t TestTestingFixture::getRunCount()
175 {
176 return result_->getRunCount();
177 }
178
lineExecutedAfterCheck()179 void TestTestingFixture::lineExecutedAfterCheck()
180 {
181 lineOfCodeExecutedAfterCheck = true;
182 }
183
checkTestFailsWithProperTestLocation(const char * text,const char * file,size_t line)184 void TestTestingFixture::checkTestFailsWithProperTestLocation(const char* text, const char* file, size_t line)
185 {
186 if (getFailureCount() != 1)
187 FAIL_LOCATION(StringFromFormat("Expected one test failure, but got %d amount of test failures", (int) getFailureCount()).asCharString(), file, line);
188
189 STRCMP_CONTAINS_LOCATION(text, output_->getOutput().asCharString(), "", file, line);
190
191 if (lineOfCodeExecutedAfterCheck)
192 FAIL_LOCATION("The test should jump/throw on failure and not execute the next line. However, the next line was executed.", file, line);
193 }
194
195