CppUTest
UtestMacros.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_UTestMacros_h
29 #define D_UTestMacros_h
30 
41 #define TEST_GROUP_BASE(testGroup, baseclass) \
42  extern int externTestGroup##testGroup; \
43  int externTestGroup##testGroup = 0; \
44  struct TEST_GROUP_##CppUTestGroup##testGroup : public baseclass
45 
46 #define TEST_BASE(testBaseClass) \
47  struct testBaseClass : public Utest
48 
49 #define TEST_GROUP(testGroup) \
50  TEST_GROUP_BASE(testGroup, Utest)
51 
52 #define TEST_SETUP() \
53  virtual void setup()
54 
55 #define TEST_TEARDOWN() \
56  virtual void teardown()
57 
58 #define TEST(testGroup, testName) \
59  /* External declarations for strict compilers */ \
60  class TEST_##testGroup##_##testName##_TestShell; \
61  extern TEST_##testGroup##_##testName##_TestShell TEST_##testGroup##_##testName##_TestShell_instance; \
62  \
63  class TEST_##testGroup##_##testName##_Test : public TEST_GROUP_##CppUTestGroup##testGroup \
64 { public: TEST_##testGroup##_##testName##_Test () : TEST_GROUP_##CppUTestGroup##testGroup () {} \
65  void testBody(); }; \
66  class TEST_##testGroup##_##testName##_TestShell : public UtestShell { \
67  virtual Utest* createTest() { return new TEST_##testGroup##_##testName##_Test; } \
68  } TEST_##testGroup##_##testName##_TestShell_instance; \
69  static TestInstaller TEST_##testGroup##_##testName##_Installer(TEST_##testGroup##_##testName##_TestShell_instance, #testGroup, #testName, __FILE__,__LINE__); \
70  void TEST_##testGroup##_##testName##_Test::testBody()
71 
72 #define IGNORE_TEST(testGroup, testName)\
73  /* External declarations for strict compilers */ \
74  class IGNORE##testGroup##_##testName##_TestShell; \
75  extern IGNORE##testGroup##_##testName##_TestShell IGNORE##testGroup##_##testName##_TestShell_instance; \
76  \
77  class IGNORE##testGroup##_##testName##_Test : public TEST_GROUP_##CppUTestGroup##testGroup \
78 { public: IGNORE##testGroup##_##testName##_Test () : TEST_GROUP_##CppUTestGroup##testGroup () {} \
79  public: void testBodyThatNeverRuns (); }; \
80  class IGNORE##testGroup##_##testName##_TestShell : public IgnoredUtestShell { \
81  virtual Utest* createTest() { return new IGNORE##testGroup##_##testName##_Test; } \
82  } IGNORE##testGroup##_##testName##_TestShell_instance; \
83  static TestInstaller TEST_##testGroup##testName##_Installer(IGNORE##testGroup##_##testName##_TestShell_instance, #testGroup, #testName, __FILE__,__LINE__); \
84  void IGNORE##testGroup##_##testName##_Test::testBodyThatNeverRuns ()
85 
86 #define IMPORT_TEST_GROUP(testGroup) \
87  extern int externTestGroup##testGroup;\
88  extern int* p##testGroup; \
89  int* p##testGroup = &externTestGroup##testGroup
90 
91 // Different checking macros
92 
93 #define CHECK(condition)\
94  CHECK_LOCATION_TRUE(condition, "CHECK", #condition, __FILE__, __LINE__)
95 
96 #define CHECK_TEXT(condition, text) \
97  CHECK_LOCATION_TEXT(condition, "CHECK", #condition, text, __FILE__, __LINE__)
98 
99 #define CHECK_TRUE(condition)\
100  CHECK_LOCATION_TRUE(condition, "CHECK_TRUE", #condition, __FILE__, __LINE__)
101 
102 #define CHECK_FALSE(condition)\
103  CHECK_LOCATION_FALSE(condition, "CHECK_FALSE", #condition, __FILE__, __LINE__)
104 
105 #define CHECK_LOCATION_TEXT(condition, checkString, conditionString, text, file, line) \
106  { UtestShell::getCurrent()->assertTrueText((condition) != 0, checkString, conditionString, text, file, line); }
107 
108 #define CHECK_LOCATION_TRUE(condition, checkString, conditionString, file, line)\
109  { UtestShell::getCurrent()->assertTrue((condition) != 0, checkString, conditionString, file, line); }
110 
111 #define CHECK_LOCATION_FALSE(condition, checkString, conditionString, file, line)\
112  { UtestShell::getCurrent()->assertTrue((condition) == 0, checkString, conditionString, file, line); }
113 
114 //This check needs the operator!=(), and a StringFrom(YourType) function
115 #define CHECK_EQUAL(expected,actual)\
116  CHECK_EQUAL_LOCATION(expected, actual, __FILE__, __LINE__)
117 
118 #define CHECK_EQUAL_LOCATION(expected,actual, file, line)\
119  if ((expected) != (actual))\
120  {\
121  { \
122  UtestShell::getTestResult()->countCheck();\
123  CheckEqualFailure _f(UtestShell::getCurrent(), file, line, StringFrom(expected), StringFrom(actual)); \
124  UtestShell::getTestResult()->addFailure(_f);\
125  } \
126  UtestShell::getCurrent()->exitCurrentTest(); \
127  }\
128  else\
129  UtestShell::getTestResult()->countCheck();
130 
131 //This check checks for char* string equality using strcmp.
132 //This makes up for the fact that CHECK_EQUAL only compares the pointers to char*'s
133 #define STRCMP_EQUAL(expected,actual)\
134  STRCMP_EQUAL_LOCATION(expected, actual, __FILE__, __LINE__)
135 
136 #define STRCMP_EQUAL_LOCATION(expected,actual, file, line)\
137  { UtestShell::getCurrent()->assertCstrEqual(expected, actual, file, line); }
138 
139 #define STRCMP_NOCASE_EQUAL(expected,actual)\
140  STRCMP_NOCASE_EQUAL_LOCATION(expected, actual, __FILE__, __LINE__)
141 
142 #define STRCMP_NOCASE_EQUAL_LOCATION(expected,actual, file, line)\
143  { UtestShell::getCurrent()->assertCstrNoCaseEqual(expected, actual, file, line); }
144 
145 #define STRCMP_CONTAINS(expected,actual)\
146  STRCMP_CONTAINS_LOCATION(expected, actual, __FILE__, __LINE__)
147 
148 #define STRCMP_CONTAINS_LOCATION(expected,actual, file, line)\
149  { UtestShell::getCurrent()->assertCstrContains(expected, actual, file, line); }
150 
151 #define STRCMP_NOCASE_CONTAINS(expected,actual)\
152  STRCMP_NOCASE_CONTAINS_LOCATION(expected, actual, __FILE__, __LINE__)
153 
154 #define STRCMP_NOCASE_CONTAINS_LOCATION(expected,actual, file, line)\
155  { UtestShell::getCurrent()->assertCstrNoCaseContains(expected, actual, file, line); }
156 
157 //Check two long integers for equality
158 #define LONGS_EQUAL(expected,actual)\
159  LONGS_EQUAL_LOCATION(expected,actual,__FILE__, __LINE__)
160 
161 #define LONGS_EQUAL_LOCATION(expected,actual,file,line)\
162  { UtestShell::getCurrent()->assertLongsEqual((long)expected, (long)actual, file, line); }
163 
164 #define BYTES_EQUAL(expected, actual)\
165  LONGS_EQUAL((expected) & 0xff,(actual) & 0xff)
166 
167 #define POINTERS_EQUAL(expected, actual)\
168  POINTERS_EQUAL_LOCATION((expected),(actual), __FILE__, __LINE__)
169 
170 #define POINTERS_EQUAL_LOCATION(expected,actual,file,line)\
171  { UtestShell::getCurrent()->assertPointersEqual((void *)expected, (void *)actual, file, line); }
172 
173 //Check two doubles for equality within a tolerance threshold
174 #define DOUBLES_EQUAL(expected,actual,threshold)\
175  DOUBLES_EQUAL_LOCATION(expected,actual,threshold,__FILE__,__LINE__)
176 
177 #define DOUBLES_EQUAL_LOCATION(expected,actual,threshold,file,line)\
178  { UtestShell::getCurrent()->assertDoublesEqual(expected, actual, threshold, file, line); }
179 
180 //Fail if you get to this macro
181 //The macro FAIL may already be taken, so allow FAIL_TEST too
182 #ifndef FAIL
183 #define FAIL(text)\
184  FAIL_LOCATION(text, __FILE__,__LINE__)
185 
186 #define FAIL_LOCATION(text, file, line)\
187  { UtestShell::getCurrent()->fail(text, file, line); }
188 #endif
189 
190 #define FAIL_TEST(text)\
191  FAIL_TEST_LOCATION(text, __FILE__,__LINE__)
192 
193 #define FAIL_TEST_LOCATION(text, file,line)\
194  { UtestShell::getCurrent()->fail(text, file, line); }
195 
196 #define UT_PRINT_LOCATION(text, file, line) \
197  { UtestShell::getCurrent()->print(text, file, line); }
198 
199 #define UT_PRINT(text) \
200  UT_PRINT_LOCATION(text, __FILE__, __LINE__)
201 
202 #if CPPUTEST_USE_STD_CPP_LIB
203 #define CHECK_THROWS(expected, expression) \
204  { \
205  SimpleString msg("expected to throw "#expected "\nbut threw nothing"); \
206  bool caught_expected = false; \
207  try { \
208  (expression); \
209  } catch(const expected &) { \
210  caught_expected = true; \
211  } catch(...) { \
212  msg = "expected to throw " #expected "\nbut threw a different type"; \
213  } \
214  if (!caught_expected) { \
215  UtestShell::getCurrent()->fail(msg.asCharString(), __FILE__, __LINE__); \
216  } \
217  }
218 #endif /* CPPUTEST_USE_STD_CPP_LIB */
219 
220 #define UT_CRASH() { UtestShell::crash(); }
221 #define RUN_ALL_TESTS(ac, av) CommandLineTestRunner::RunAllTests(ac, av)
222 
223 #endif /*D_UTestMacros_h*/