NetBurner 3.1
TestTestingFixture.h
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 #ifndef D_TestTestingFixture_H
29 #define D_TestTestingFixture_H
30 
31 #include "TestRegistry.h"
32 #include "TestOutput.h"
33 
34 class TestTestingFixture
35 {
36 public:
37 
38  TestTestingFixture()
39  {
40  output_ = new StringBufferTestOutput();
41  result_ = new TestResult(*output_);
42  genTest_ = new ExecFunctionTestShell();
43  registry_ = new TestRegistry();
44 
45  registry_->setCurrentRegistry(registry_);
46  registry_->addTest(genTest_);
47 
48  lineOfCodeExecutedAfterCheck = false;
49  }
50 
51  virtual ~TestTestingFixture()
52  {
53  registry_->setCurrentRegistry(0);
54  delete registry_;
55  delete result_;
56  delete output_;
57  delete genTest_;
58  }
59 
60  void addTest(UtestShell * test)
61  {
62  registry_->addTest(test);
63  }
64 
65  void setTestFunction(void(*testFunction)())
66  {
67  genTest_->testFunction_ = testFunction;
68  }
69 
70  void setSetup(void(*setupFunction)())
71  {
72  genTest_->setup_ = setupFunction;
73  }
74 
75  void setTeardown(void(*teardownFunction)())
76  {
77  genTest_->teardown_ = teardownFunction;
78  }
79 
80  void runTestWithMethod(void(*method)())
81  {
82  setTestFunction(method);
83  runAllTests();
84  }
85 
86  void runAllTests()
87  {
88  registry_->runAllTests(*result_);
89  }
90 
91  int getFailureCount()
92  {
93  return result_->getFailureCount();
94  }
95 
96  int getCheckCount()
97  {
98  return result_->getCheckCount();
99  }
100 
101  int getIgnoreCount()
102  {
103  return result_->getIgnoredCount();
104  }
105 
106  bool hasTestFailed()
107  {
108  return genTest_->hasFailed();
109  }
110 
111  void assertPrintContains(const SimpleString& contains)
112  {
113  assertPrintContains(getOutput(), contains);
114  }
115 
116  const SimpleString& getOutput()
117  {
118  return output_->getOutput();
119  }
120 
121  static void assertPrintContains(const SimpleString& output, const SimpleString& contains)
122  {
123  STRCMP_CONTAINS(contains.asCharString(), output.asCharString());
124  }
125 
126  int getRunCount()
127  {
128  return result_->getRunCount();
129  }
130 
131  void checkTestFailsWithProperTestLocation(const char* text, const char* file, int line);
132 
133  static void lineExecutedAfterCheck();
134  static bool lineOfCodeExecutedAfterCheck;
135 
136  TestRegistry* registry_;
137  ExecFunctionTestShell* genTest_;
138  StringBufferTestOutput* output_;
139  TestResult * result_;
140 };
141 
142 class SetBooleanOnDestructorCall
143 {
144  bool& booleanToSet_;
145 public:
146  SetBooleanOnDestructorCall(bool& booleanToSet) : booleanToSet_(booleanToSet)
147  {
148  }
149 
150  virtual ~SetBooleanOnDestructorCall()
151  {
152  booleanToSet_ = true;
153  }
154 };
155 
156 #endif