3 // $Id: cpptest-assert.h,v 1.3 2005/06/08 08:08:06 nilu Exp $
5 // CppTest - A C++ Unit Testing Framework
6 // Copyright (c) 2003 Niklas Lundell
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2 of the License, or (at your option) any later version.
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the
22 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 // Boston, MA 02111-1307, USA.
29 #ifndef CPPTEST_ASSERT_H
30 #define CPPTEST_ASSERT_H
32 /// Unconditional failure.
34 /// Used in conjunction with Test::Suite.
36 /// \param msg Provided message.
40 /// void MySuite::test()
46 /// // handling valid cases ...
49 /// TEST_FAIL("This should not happen")
54 /// For a description of all asserts, see \ref asserts.
56 #define TEST_FAIL(msg) \
58 assertment(::Test::Source(__FILE__, __LINE__, (msg) != 0 ? #msg : "")); \
59 if (!continue_after_failure()) return; \
62 /// Verify an expression and issues an assertment if it fails.
64 /// Used in conjunction with Test::Suite.
66 /// \param expr Expression to test.
68 /// \see TEST_ASSERT_MSG(expr, msg)
72 /// void MySuite::test()
78 /// TEST_ASSERT(i == 13)
82 /// For a description of all asserts, see \ref asserts.
84 #define TEST_ASSERT(expr) \
88 assertment(::Test::Source(__FILE__, __LINE__, #expr)); \
89 if (!continue_after_failure()) return; \
93 /// Verify an expression and issues an assertment if it fails.
95 /// Used in conjunction with Test::Suite.
97 /// \param expr Expression to test.
98 /// \param msg User message.
100 /// \see TEST_ASSERT(expr)
102 /// For a description of all asserts, see \ref asserts.
104 #define TEST_ASSERT_MSG(expr, msg) \
108 assertment(::Test::Source(__FILE__, __LINE__, msg)); \
109 if (!continue_after_failure()) return; \
113 /// Verify that two expressions are equal up to a constant, issues an
114 /// assertment if it fails.
116 /// Used in conjunction with Test::Suite.
118 /// \param a First expression to test.
119 /// \param b Second expression to test.
120 /// \param delta Constant.
122 /// \see TEST_ASSERT_DELTA_MSG(a, b, delta, msg)
124 /// For a description of all asserts, see \ref asserts.
126 #define TEST_ASSERT_DELTA(a, b, delta) \
128 if (((b) < (a) - (delta)) || ((b) > (a) + (delta))) \
130 assertment(::Test::Source(__FILE__, __LINE__, \
131 "delta(" #a ", " #b ", " #delta ")" )); \
132 if (!continue_after_failure()) return; \
136 /// Verify that two expressions are equal up to a constant, issues an
137 /// assertment if it fails.
139 /// Used in conjunction with Test::Suite.
141 /// \param a First expression to test.
142 /// \param b Second expression to test.
143 /// \param delta Constant.
144 /// \param msg User message.
146 /// \see TEST_ASSERT_DELTA(a, b, delta)
148 /// For a description of all asserts, see \ref asserts.
150 #define TEST_ASSERT_DELTA_MSG(a, b, delta, msg) \
152 if (((b) < (a) - (delta)) || ((b) > (a) + (delta))) \
154 assertment(::Test::Source(__FILE__, __LINE__, msg)); \
155 if (!continue_after_failure()) return; \
159 /// Verify an expression and expects an exception in return.
160 /// An assertment is issued if the exception is not thrown.
162 /// Used in conjunction with Test::Suite.
164 /// \param expr Expression to test.
165 /// \param x Expected exception.
167 /// \see TEST_THROWS_MSG(expr, x, msg)
169 /// For a description of all asserts, see \ref asserts.
171 #define TEST_THROWS(expr, x) \
173 bool __expected = false; \
175 catch (x) { __expected = true; } \
179 assertment(::Test::Source(__FILE__, __LINE__, #expr)); \
180 if (!continue_after_failure()) return; \
184 /// Verify an expression and expects an exception in return.
185 /// An assertment is issued if the exception is not thrown.
187 /// Used in conjunction with Test::Suite.
189 /// \param expr Expression to test.
190 /// \param x Expected exception.
191 /// \param msg User message.
193 /// \see TEST_THROWS(expr, x)
195 /// For a description of all asserts, see \ref asserts.
197 #define TEST_THROWS_MSG(expr, x, msg) \
199 bool __expected = false; \
201 catch (x) { __expected = true; } \
205 assertment(::Test::Source(__FILE__, __LINE__, msg)); \
206 if (!continue_after_failure()) return; \
210 /// Verify an expression and expects any exception in return.
211 /// An assertment is issued if no exception is thrown.
213 /// Used in conjunction with Test::Suite.
215 /// \param expr Expression to test.
217 /// \see TEST_THROWS_ANYTHING_MSG(expr, msg)
219 /// For a description of all asserts, see \ref asserts.
221 #define TEST_THROWS_ANYTHING(expr) \
223 bool __expected = false; \
225 catch (...) { __expected = true; } \
228 assertment(::Test::Source(__FILE__, __LINE__, #expr)); \
229 if (!continue_after_failure()) return; \
233 /// Verify an expression and expects any exception in return.
234 /// An assertment is issued if no exception is thrown.
236 /// Used in conjunction with Test::Suite.
238 /// \param expr Expression to test.
239 /// \param msg User message.
241 /// \see TEST_THROWS_ANYTHING(expr)
243 /// For a description of all asserts, see \ref asserts.
245 #define TEST_THROWS_ANYTHING_MSG(expr, msg) \
247 bool __expected = false; \
249 catch (...) { __expected = true; } \
252 assertment(::Test::Source(__FILE__, __LINE__, msg)); \
253 if (!continue_after_failure()) return; \
257 /// Verify an expression and expects no exception in return.
258 /// An assertment is issued if any exception is thrown.
260 /// Used in conjunction with Test::Suite.
262 /// \param expr Expression to test.
264 /// \see TEST_THROWS_NOTHING_MSG(expr, msg)
266 /// For a description of all asserts, see \ref asserts.
268 #define TEST_THROWS_NOTHING(expr) \
270 bool __expected = true; \
272 catch (...) { __expected = false; } \
275 assertment(::Test::Source(__FILE__, __LINE__, #expr)); \
276 if (!continue_after_failure()) return; \
280 /// Verify an expression and expects no exception in return.
281 /// An assertment is issued if any exception is thrown.
283 /// Used in conjunction with Test::Suite.
285 /// \param expr Expression to test.
286 /// \param msg User message.
288 /// \see TEST_THROWS_NOTHING(expr)
290 /// For a description of all asserts, see \ref asserts.
292 #define TEST_THROWS_NOTHING_MSG(expr, msg) \
294 bool __expected = true; \
296 catch (...) { __expected = false; } \
299 assertment(::Test::Source(__FILE__, __LINE__, msg)); \
300 if (!continue_after_failure()) return; \
304 /// \page asserts Available asserts
306 /// Normally, assert macros issues an assert if the given expression, if any,
307 /// fails (as defined by the macro). Assertments include information about the
308 /// current test suite, test function, source file, source file line, and a
309 /// message. The message is normally the offending expression, however, for
310 /// macros ending in _MSG it is possibly to supply a user defined message.
312 /// <b>General asserts</b>
315 /// <b>Comparision asserts</b>
316 /// - TEST_ASSERT(expr)
317 /// - TEST_ASSERT_MSG(expr, msg)
318 /// - TEST_ASSERT_DELTA(a, b, delta)
319 /// - TEST_ASSERT_DELTA_MSG(a, b, delta, msg)
321 /// <b>Exception asserts</b>
322 /// - TEST_THROWS(expr, x)
323 /// - TEST_THROWS_MSG(expr, x, msg)
324 /// - TEST_THROWS_ANYTHING(expr)
325 /// - TEST_THROWS_ANYTHING_MSG(expr, msg)
326 /// - TEST_THROWS_NOTHING(expr)
327 /// - TEST_THROWS_NOTHING_MSG(expr, msg)
330 #endif // #ifndef CPPTEST_ASSERT_H