CppUTest
Utest.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 // This file contains the Test class along with the macros which make effective
29 // in the harness.
30 
31 #ifndef D_UTest_h
32 #define D_UTest_h
33 
34 #include "SimpleString.h"
35 
36 class TestResult;
37 class TestPlugin;
38 class TestFailure;
39 class TestFilter;
40 
41 extern bool doubles_equal(double d1, double d2, double threshold);
42 
44 
45 class UtestShell;
46 
47 class Utest
48 {
49 public:
50  Utest();
51  virtual ~Utest();
52  virtual void run();
53 
54  virtual void setup();
55  virtual void teardown();
56  virtual void testBody();
57 };
58 
60 
62 {
63 public:
64  UtestShell(const char* groupName, const char* testName, const char* fileName,
65  int lineNumber);
66  virtual ~UtestShell();
67 
68  virtual void runOneTestWithPlugins(TestPlugin* plugin, TestResult& result);
69  virtual SimpleString getFormattedName() const;
70 
71  virtual UtestShell* addTest(UtestShell* test);
72  virtual UtestShell *getNext() const;
73  virtual bool isNull() const;
74  virtual int countTests();
75 
76  bool shouldRun(const TestFilter& groupFilter, const TestFilter& nameFilter) const;
77  const SimpleString getName() const;
78  const SimpleString getGroup() const;
79  const SimpleString getFile() const;
80  int getLineNumber() const;
81  virtual const char *getProgressIndicator() const;
82 
83  static TestResult *getTestResult();
84  static UtestShell *getCurrent();
85 
86  virtual void assertTrue(bool condition, const char *checkString, const char *conditionString, const char *fileName, int lineNumber);
87  virtual void assertTrueText(bool condition, const char *checkString, const char *conditionString, const char* text, const char *fileName, int lineNumber);
88  virtual void assertCstrEqual(const char *expected, const char *actual, const char *fileName, int lineNumber);
89  virtual void assertCstrNoCaseEqual(const char *expected, const char *actual, const char *fileName, int lineNumber);
90  virtual void assertCstrContains(const char *expected, const char *actual, const char *fileName, int lineNumber);
91  virtual void assertCstrNoCaseContains(const char *expected, const char *actual, const char *fileName, int lineNumber);
92  virtual void assertLongsEqual(long expected, long actual, const char *fileName, int lineNumber);
93  virtual void assertPointersEqual(const void *expected, const void *actual, const char *fileName, int lineNumber);
94  virtual void assertDoublesEqual(double expected, double actual, double threshold, const char *fileName, int lineNumber);
95  virtual void fail(const char *text, const char *fileName, int lineNumber);
96 
97  virtual void print(const char *text, const char *fileName, int lineNumber);
98  virtual void print(const SimpleString & text, const char *fileName, int lineNumber);
99 
100  void setFileName(const char *fileName);
101  void setLineNumber(int lineNumber);
102  void setGroupName(const char *groupName);
103  void setTestName(const char *testName);
104 
105  virtual void exitCurrentTest();
106  virtual void exitCurrentTestWithoutException();
107 
108  static void crash();
109  static void setCrashMethod(void (*crashme)());
110  static void resetCrashMethod();
111 
112  virtual bool isRunInSeperateProcess() const;
113  virtual void setRunInSeperateProcess();
114 
115  virtual Utest* createTest();
116  virtual void destroyTest(Utest* test);
117 
118  virtual void runOneTest(TestPlugin *plugin, TestResult & result);
119 protected:
120  UtestShell();
121  UtestShell(const char *groupName, const char *testName, const char *fileName, int lineNumber, UtestShell *nextTest);
122 
123  virtual SimpleString getMacroName() const;
124 private:
125  const char *group_;
126  const char *name_;
127  const char *file_;
128  int lineNumber_;
129  UtestShell *next_;
130  bool isRunAsSeperateProcess_;
131 
132  void setTestResult(TestResult* result);
133  void setCurrentTest(UtestShell* test);
134 
135  static UtestShell* currentTest_;
136  static TestResult* testResult_;
137 
138  void failWith(const TestFailure& failure);
139 };
140 
142 
144 {
145 public:
146  explicit NullTestShell();
147  explicit NullTestShell(const char* fileName, int lineNumber);
148  virtual ~NullTestShell();
149 
150  void testBody();
151 
152  static NullTestShell& instance();
153 
154  virtual int countTests();
155  virtual UtestShell*getNext() const;
156  virtual bool isNull() const;
157 private:
158 
160  NullTestShell& operator=(const NullTestShell&);
161 
162 };
163 
164 
166 
168 
169 class ExecFunctionTest : public Utest
170 {
171 public:
173  void testBody();
174  virtual void setup();
175  virtual void teardown();
176 private:
177  ExecFunctionTestShell* shell_;
178 };
179 
181 
183 {
184 public:
185  void (*setup_)();
186  void (*teardown_)();
187  void (*testFunction_)();
188 
189  ExecFunctionTestShell(void(*set)() = 0, void(*tear)() = 0) :
190  UtestShell("Generic", "Generic", "Generic", 1), setup_(set), teardown_(
191  tear), testFunction_(0)
192  {
193  }
194  Utest* createTest() { return new ExecFunctionTest(this); }
195  virtual ~ExecFunctionTestShell();
196 };
197 
199 
201 {
202 public:
203  int dummy_;
204 };
205 
207 
209 {
210 public:
212  virtual ~IgnoredUtestShell();
213  explicit IgnoredUtestShell(const char* groupName, const char* testName,
214  const char* fileName, int lineNumber);
215  virtual const char* getProgressIndicator() const;
216  protected: virtual SimpleString getMacroName() const;
217  virtual void runOneTestWithPlugins(TestPlugin* plugin, TestResult& result);
218 
219 private:
220 
222  IgnoredUtestShell& operator=(const IgnoredUtestShell&);
223 
224 };
225 
227 
229 {
230 public:
231  explicit TestInstaller(UtestShell& shell, const char* groupName, const char* testName,
232  const char* fileName, int lineNumber);
233  virtual ~TestInstaller();
234 
235  void unDo();
236 
237 private:
238 
240  TestInstaller& operator=(const TestInstaller&);
241 
242 };
243 
244 #endif
Definition: Utest.h:182
Definition: TestResult.h:41
Definition: Utest.h:169
Definition: Utest.h:208
Definition: TestPlugin.h:34
Definition: Utest.h:143
Definition: Utest.h:200
Definition: Utest.h:47
Definition: SimpleString.h:46
Definition: TestFailure.h:44
Definition: Utest.h:61
Definition: TestFilter.h:33
Definition: Utest.h:228