summaryrefslogtreecommitdiffstats
path: root/tests/TestHelpers.h
blob: 665d4bf521b7d7cbe6ceb6c0a9bbdafa77f29001 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Helper macros for writing exception-based tests

/*
The tests are supposed to be contained in small static functions that get called from a main function provided by this framework:
static void test1()
{
	// Perform the test
}

...

IMPLEMENT_TEST_MAIN("TestName",
	test1();
	...
)
*/





/** The exception that is thrown if a test fails.
It doesn't inherit from any type so that it is not possible to catch it by a mistake,
it needs to be caught explicitly (used in the TEST_THROWS).
It bears a single message that is to be displayed to stderr. */
class TestException
{
public:
	TestException(const AString & aFileName, int aLineNumber, const AString & aFunctionName, const AString & aMessage):
		mFileName(aFileName),
		mLineNumber(aLineNumber),
		mFunctionName(aFunctionName),
		mMessage(aMessage)
	{
	}

	AString mFileName;
	int mLineNumber;
	AString mFunctionName;
	AString mMessage;
};





/** Checks that the two values are equal; if not, throws a TestException. */
#define TEST_EQUAL(VAL1, VAL2) \
	do { \
		if (VAL1 != VAL2) \
		{ \
			throw TestException( \
				__FILE__, __LINE__, __FUNCTION__, \
				fmt::format(FMT_STRING("Equality test failed: {} != {}"), #VAL1, #VAL2) \
			); \
		} \
	} while (false)





/** Checks that the two values are equal; if not, throws a TestException, includes the specified message. */
#define TEST_EQUAL_MSG(VAL1, VAL2, MSG) \
	do { \
		if (VAL1 != VAL2) \
		{ \
			throw TestException( \
				__FILE__, __LINE__, __FUNCTION__, \
				fmt::format(FMT_STRING("Equality test failed: {} != {} ({})"), #VAL1, #VAL2, MSG) \
			); \
		} \
	} while (false)





/** Checks that the two values are not equal; if they are, throws a TestException. */
#define TEST_NOTEQUAL(VAL1, VAL2) \
	do { \
		if (VAL1 == VAL2) \
		{ \
			throw TestException( \
				__FILE__, __LINE__, __FUNCTION__, \
				fmt::format(FMT_STRING("Inequality test failed: {} == {}"), #VAL1, #VAL2) \
			); \
		} \
	} while (false)





/** Checks that the statement evaluates to true. */
#define TEST_TRUE(X) TEST_EQUAL(X, true)





/** Checks that the statement evaluates to false. */
#define TEST_FALSE(X) TEST_EQUAL(X, false)





/** Checks that the statement returns a value greater than or equal to the specified value. */
#define TEST_GREATER_THAN_OR_EQUAL(Stmt, Val) \
	do { \
		if (Stmt < Val) \
		{ \
			throw TestException(__FILE__, __LINE__, __FUNCTION__, fmt::format(FMT_STRING("Comparison failed: {} < {}"), #Stmt, #Val)); \
		} \
	} while (false)





/** Checks that the statement returns a value less than or equal to the specified value. */
#define TEST_LESS_THAN_OR_EQUAL(Stmt, Val) \
	do { \
		if (Stmt > Val) \
		{ \
			throw TestException(__FILE__, __LINE__, __FUNCTION__, fmt::format(FMT_STRING("Comparison failed: {} > {}"), #Stmt, #Val)); \
		} \
	} while (false)





/** Checks that the statement throws an exception of the specified class. */
#define TEST_THROWS(Stmt, ExcClass) \
	do { \
		try \
		{ \
			Stmt; \
			throw TestException( \
				__FILE__, __LINE__, __FUNCTION__, \
				fmt::format(FMT_STRING("Failed to throw an exception of type {}"), #ExcClass) \
			); \
		} \
		catch (const ExcClass &) \
		{ \
			/* This is the expected case. */ \
		} \
		catch (const std::exception & exc) \
		{ \
			throw TestException( \
				__FILE__, __LINE__, __FUNCTION__, \
				fmt::format( \
					FMT_STRING("An unexpected std::exception descendant was thrown, was expecting type {}. Message is: {}"), \
					#ExcClass, exc.what() \
				) \
			); \
		} \
		catch (...)\
		{ \
			throw TestException( \
				__FILE__, __LINE__, __FUNCTION__, \
				fmt::format(FMT_STRING("An unexpected unknown exception object was thrown, was expecting type {}"), #ExcClass) \
			); \
		} \
	} while (false)





/** Checks that the statement throws an exception of any type. */
#define TEST_THROWS_ANY(Stmt) \
	do { \
		try \
		{ \
			Stmt; \
			throw TestException( \
				__FILE__, __LINE__, __FUNCTION__, \
				"Failed to throw an exception of any type" \
			); \
		} \
		catch (const TestException & exc) \
		{ \
			throw exc; \
		} \
		catch (...)\
		{ \
			/* This is the expected case */ \
		} \
	} while (false)





/** Fails the test unconditionally, with the specified message. */
#define TEST_FAIL(MSG) \
	throw TestException(__FILE__, __LINE__, __FUNCTION__, MSG)





/** Checks that the statement causes an ASSERT trigger. */
#ifdef NDEBUG
	#define TEST_ASSERTS(Stmt) LOG("Skipped, cannot test in Release mode: TEST_ASSERT(%s); (%s:%d)", #Stmt, __FILE__, __LINE__)
#else
	#define TEST_ASSERTS(Stmt) TEST_THROWS(Stmt, cAssertFailure)
#endif  // else NDEBUG





/** Used to implement the main() function for tests. */
#define IMPLEMENT_TEST_MAIN(TestName, TestCode) \
int main() \
{ \
	LOG("Test started: %s", TestName); \
	\
	try \
	{ \
		TestCode; \
	} \
	catch (const TestException & exc) \
	{ \
		LOGERROR("Test has failed at file %s, line %d, function %s: %s", \
			exc.mFileName.c_str(), \
			exc.mLineNumber, \
			exc.mFunctionName.c_str(), \
			exc.mMessage.c_str() \
		); \
		return 1; \
	} \
	catch (const std::exception & exc) \
	{ \
		LOGERROR("Test has failed, an exception was thrown: %s", exc.what()); \
		return 1; \
	} \
	catch (const cAssertFailure & exc) \
	{ \
		LOGERROR("Test has failed, an unexpected ASSERT was triggered at %s:%d: %s", \
			exc.fileName().c_str(), exc.lineNumber(), exc.expression().c_str() \
		); \
		return 1; \
	} \
	catch (...) \
	{ \
		LOGERROR("Test has failed, an unhandled exception was thrown."); \
		return 1; \
	} \
	\
	LOG("Test finished"); \
	return 0; \
}