28 #ifndef D_UTestMacros_h
29 #define D_UTestMacros_h
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
46 #define TEST_BASE(testBaseClass) \
47 struct testBaseClass : public Utest
49 #define TEST_GROUP(testGroup) \
50 TEST_GROUP_BASE(testGroup, Utest)
52 #define TEST_SETUP() \
55 #define TEST_TEARDOWN() \
56 virtual void teardown()
58 #define TEST(testGroup, testName) \
60 class TEST_##testGroup##_##testName##_TestShell; \
61 extern TEST_##testGroup##_##testName##_TestShell TEST_##testGroup##_##testName##_TestShell_instance; \
63 class TEST_##testGroup##_##testName##_Test : public TEST_GROUP_##CppUTestGroup##testGroup \
64 { public: TEST_##testGroup##_##testName##_Test () : TEST_GROUP_##CppUTestGroup##testGroup () {} \
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()
72 #define IGNORE_TEST(testGroup, testName)\
74 class IGNORE##testGroup##_##testName##_TestShell; \
75 extern IGNORE##testGroup##_##testName##_TestShell IGNORE##testGroup##_##testName##_TestShell_instance; \
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 ()
86 #define IMPORT_TEST_GROUP(testGroup) \
87 extern int externTestGroup##testGroup;\
88 extern int* p##testGroup; \
89 int* p##testGroup = &externTestGroup##testGroup
93 #define CHECK(condition)\
94 CHECK_LOCATION_TRUE(condition, "CHECK", #condition, __FILE__, __LINE__)
96 #define CHECK_TEXT(condition, text) \
97 CHECK_LOCATION_TEXT(condition, "CHECK", #condition, text, __FILE__, __LINE__)
99 #define CHECK_TRUE(condition)\
100 CHECK_LOCATION_TRUE(condition, "CHECK_TRUE", #condition, __FILE__, __LINE__)
102 #define CHECK_FALSE(condition)\
103 CHECK_LOCATION_FALSE(condition, "CHECK_FALSE", #condition, __FILE__, __LINE__)
105 #define CHECK_LOCATION_TEXT(condition, checkString, conditionString, text, file, line) \
106 { UtestShell::getCurrent()->assertTrueText((condition) != 0, checkString, conditionString, text, file, line); }
108 #define CHECK_LOCATION_TRUE(condition, checkString, conditionString, file, line)\
109 { UtestShell::getCurrent()->assertTrue((condition) != 0, checkString, conditionString, file, line); }
111 #define CHECK_LOCATION_FALSE(condition, checkString, conditionString, file, line)\
112 { UtestShell::getCurrent()->assertTrue((condition) == 0, checkString, conditionString, file, line); }
115 #define CHECK_EQUAL(expected,actual)\
116 CHECK_EQUAL_LOCATION(expected, actual, __FILE__, __LINE__)
118 #define CHECK_EQUAL_LOCATION(expected,actual, file, line)\
119 if ((expected) != (actual))\
122 UtestShell::getTestResult()->countCheck();\
123 CheckEqualFailure _f(UtestShell::getCurrent(), file, line, StringFrom(expected), StringFrom(actual)); \
124 UtestShell::getTestResult()->addFailure(_f);\
126 UtestShell::getCurrent()->exitCurrentTest(); \
129 UtestShell::getTestResult()->countCheck();
133 #define STRCMP_EQUAL(expected,actual)\
134 STRCMP_EQUAL_LOCATION(expected, actual, __FILE__, __LINE__)
136 #define STRCMP_EQUAL_LOCATION(expected,actual, file, line)\
137 { UtestShell::getCurrent()->assertCstrEqual(expected, actual, file, line); }
139 #define STRCMP_NOCASE_EQUAL(expected,actual)\
140 STRCMP_NOCASE_EQUAL_LOCATION(expected, actual, __FILE__, __LINE__)
142 #define STRCMP_NOCASE_EQUAL_LOCATION(expected,actual, file, line)\
143 { UtestShell::getCurrent()->assertCstrNoCaseEqual(expected, actual, file, line); }
145 #define STRCMP_CONTAINS(expected,actual)\
146 STRCMP_CONTAINS_LOCATION(expected, actual, __FILE__, __LINE__)
148 #define STRCMP_CONTAINS_LOCATION(expected,actual, file, line)\
149 { UtestShell::getCurrent()->assertCstrContains(expected, actual, file, line); }
151 #define STRCMP_NOCASE_CONTAINS(expected,actual)\
152 STRCMP_NOCASE_CONTAINS_LOCATION(expected, actual, __FILE__, __LINE__)
154 #define STRCMP_NOCASE_CONTAINS_LOCATION(expected,actual, file, line)\
155 { UtestShell::getCurrent()->assertCstrNoCaseContains(expected, actual, file, line); }
158 #define LONGS_EQUAL(expected,actual)\
159 LONGS_EQUAL_LOCATION(expected,actual,__FILE__, __LINE__)
161 #define LONGS_EQUAL_LOCATION(expected,actual,file,line)\
162 { UtestShell::getCurrent()->assertLongsEqual((long)expected, (long)actual, file, line); }
164 #define BYTES_EQUAL(expected, actual)\
165 LONGS_EQUAL((expected) & 0xff,(actual) & 0xff)
167 #define POINTERS_EQUAL(expected, actual)\
168 POINTERS_EQUAL_LOCATION((expected),(actual), __FILE__, __LINE__)
170 #define POINTERS_EQUAL_LOCATION(expected,actual,file,line)\
171 { UtestShell::getCurrent()->assertPointersEqual((void *)expected, (void *)actual, file, line); }
174 #define DOUBLES_EQUAL(expected,actual,threshold)\
175 DOUBLES_EQUAL_LOCATION(expected,actual,threshold,__FILE__,__LINE__)
177 #define DOUBLES_EQUAL_LOCATION(expected,actual,threshold,file,line)\
178 { UtestShell::getCurrent()->assertDoublesEqual(expected, actual, threshold, file, line); }
184 FAIL_LOCATION(text, __FILE__,__LINE__)
186 #define FAIL_LOCATION(text, file, line)\
187 { UtestShell::getCurrent()->fail(text, file, line); }
190 #define FAIL_TEST(text)\
191 FAIL_TEST_LOCATION(text, __FILE__,__LINE__)
193 #define FAIL_TEST_LOCATION(text, file,line)\
194 { UtestShell::getCurrent()->fail(text, file, line); }
196 #define UT_PRINT_LOCATION(text, file, line) \
197 { UtestShell::getCurrent()->print(text, file, line); }
199 #define UT_PRINT(text) \
200 UT_PRINT_LOCATION(text, __FILE__, __LINE__)
202 #if CPPUTEST_USE_STD_CPP_LIB
203 #define CHECK_THROWS(expected, expression) \
205 SimpleString msg("expected to throw "#expected "\nbut threw nothing"); \
206 bool caught_expected = false; \
209 } catch(const expected &) { \
210 caught_expected = true; \
212 msg = "expected to throw " #expected "\nbut threw a different type"; \
214 if (!caught_expected) { \
215 UtestShell::getCurrent()->fail(msg.asCharString(), __FILE__, __LINE__); \
220 #define UT_CRASH() { UtestShell::crash(); }
221 #define RUN_ALL_TESTS(ac, av) CommandLineTestRunner::RunAllTests(ac, av)