debian: fix build-deps for focal
[amule.git] / unittests / muleunit / test.h
blob0d499f8302dd5d2b8b186ed6d0a4e65d8c431b57
1 //
2 // MuleUnit: A minimalistic C++ Unit testing framework based on EasyUnit.
3 //
4 // Copyright (c) 2005-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2004-2011 Barthelemy Dagenais ( barthelemy@prologique.com )
6 //
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Lesser General Public
9 // License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // Lesser General Public License for more details.
17 // You should have received a copy of the GNU Lesser General Public
18 // License along with this library; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #ifndef TEST_H
23 #define TEST_H
25 #include <exception>
27 #include <wx/string.h>
28 #include <list>
29 #include <string>
31 #if wxCHECK_VERSION(2, 9, 0)
32 #include <wx/wxcrt.h>
33 #endif
35 /**
36 * MuleUnit namespace.
37 * This is the namespace containing all muleunit classes.
39 namespace muleunit
42 class TestCase;
43 class BTList;
46 /** Returns the size of a static array. */
47 template <typename T, size_t N>
48 inline size_t ArraySize(T(&)[N])
50 return N;
54 /** Print wide-char strings. */
55 inline void Print(const wxString& str)
57 wxPuts(str.c_str());
61 /** This exception is raised if an ASSERT fails. */
62 struct CTestFailureException : public std::exception
64 /** Constructor, takes a snapshot of the current context, and adds the given information. */
65 CTestFailureException(const wxString& msg, const wxChar* file, long lineNumber);
67 ~CTestFailureException() throw();
69 /** Prints the context backtrace for the location where the exception was thrown. */
70 void PrintBT() const;
72 virtual const char* what () const throw ();
73 private:
74 //! Pointer to struct containing a snapshot of the contexts
75 //! taken at the time the exception was created.
76 struct BTList* m_bt;
78 //! The message passed in the constructor.
79 std::string m_message;
82 /** This exception is raised if an wxASSERT fails. */
83 struct CAssertFailureException : public CTestFailureException
85 public:
86 CAssertFailureException(const wxString& msg, const wxChar* file, long lineNumber)
87 : CTestFailureException(msg, file, lineNumber)
93 /**
94 * This class is used to produce informative backtraces
96 * This is done by specifying a "context" for a given scope, using
97 * the CONTEXT macro, at which point a description is added to the
98 * current list of contexts. At destruction, when the scope is exited,
99 * the context is removed from the queue.
101 * The resulting "backtrace" can then be printed by calling the
102 * PrintBT() function of an CTestFailureException.
104 class CContext
106 public:
107 /** Adds a context with the specified information and description. */
108 CContext(const wxChar* file, int line, const wxString& desc);
110 /** Removes the context addded by the constructor. */
111 ~CContext();
115 //! Used to join the CContext instance name with the line-number.
116 //! This is done to prevent shadowing.
117 #define DO_CONTEXT(x, y, z) x y##z
119 //! Specifies the context of the current scope.
120 #define CONTEXT(x) CContext wxCONCAT(context,__LINE__)(wxT(__FILE__), __LINE__, x)
124 * This class disables assertions while it is in scope.
126 class CAssertOff
128 public:
129 CAssertOff();
130 ~CAssertOff();
135 * Helperfunction that converts basic types to strings.
137 template <typename TYPE>
138 wxString StringFrom(const TYPE& value)
140 return wxString() << value;
143 inline wxString StringFrom(unsigned long long value)
145 return wxString::Format(wxT("%") wxLongLongFmtSpec wxT("u"), value);
148 inline wxString StringFrom(signed long long value)
150 return wxString::Format(wxT("%") wxLongLongFmtSpec wxT("i"), value);
155 * Test class containing all macros to do unit testing.
156 * A test object represents a test that will be executed. Once it has been
157 * executed, it reports all failures in the testPartResult linked list.
159 * A failure occurs when a test fails (condition is false).
161 class Test
163 public:
165 * Main Test constructor. Used to create a test that will register itself
166 * with TestRegistry and with its test case.
167 * @param testCaseName Name of the test case this test belongs to
168 * @param testName Name of this test
170 Test(const wxString& testCaseName, const wxString& testName);
173 * Main Test desctructor
174 * Delete the testPartResult linked list. This is why the user should
175 * only use the macro provided by muleunit to report a test result.
177 virtual ~Test();
180 * Fixtures that will be called after run().
182 virtual void tearDown();
185 * Fixtures that will be called before run().
187 virtual void setUp();
190 * Test code should be in this method.
191 * run() will be called by the Test's TestCase, hence subclasses of Test
192 * should override this method.
194 virtual void run();
197 * Get the name of the TestCase this test belongs to. The name of the
198 * TestCase is the first parameter of the test declaration. For example,
199 * if a test is declared as TEST(TESTCASE1, TEST1), this method will return
200 * "TESTCASE1".
202 * @return The TestCase name of this test
204 const wxString& getTestCaseName() const;
207 * Get the name of this test. The name of the test is the second
208 * parameter of the test declaration. For example,
209 * if a test is declared as TEST(TESTCASE1, TEST1), this method will return
210 * "TEST1".
212 * @return The name of this test.
214 const wxString& getTestName() const;
216 template <typename A, typename B>
217 static void DoAssertEquals(const wxString& file, unsigned line, const A& a, const B& b)
219 if (!(a == b)) {
220 wxString message = wxT("Expected '") + StringFrom(a) +
221 wxT("' but got '") + StringFrom(b) + wxT("'");
222 #if wxCHECK_VERSION(3, 1, 0)
223 throw CTestFailureException(message, file.c_str(), line);
224 #else
225 throw CTestFailureException(message, file, line);
226 #endif
230 protected:
231 wxString m_testCaseName;
232 wxString m_testName;
233 TestCase* m_testCase;
237 #define THROW_TEST_FAILURE(message) \
238 throw CTestFailureException(message, wxT(__FILE__), __LINE__)
242 * Asserts that a condition is true.
243 * If the condition is not true, a failure is generated.
244 * @param condition Condition to fullfill for the assertion to pass
245 * @param message Message that will be displayed if this assertion fails
247 #define ASSERT_TRUE_M(condition, message) \
249 if (!(condition)) { \
250 THROW_TEST_FAILURE(message); \
256 * Same as ASSERT_TRUE, but without an explicit message.
258 #define ASSERT_TRUE(condition) \
259 ASSERT_TRUE_M(condition, wxString(wxT("Not true: ")) + wxT(#condition));
263 * Same as ASSERT_TRUE, but without an explicit message and condition must be false.
265 #define ASSERT_FALSE(condition) \
266 ASSERT_TRUE_M(!(condition), wxString(wxT("Not false: ")) + wxT(#condition));
270 * Asserts that the two parameters are equals. Operator == must be defined.
271 * If the two parameters are not equals, a failure is generated.
272 * @param expected Expected value
273 * @param actual Actual value to be compared
274 * @param message Message that will be displayed if this assertion fails
276 #define ASSERT_EQUALS_M(expected,actual,message)\
278 if (!(expected == actual)) { \
279 THROW_TEST_FAILURE(message); \
285 * Same as ASSERT_EQUALS_M, but without an explicit message.
287 #define ASSERT_EQUALS(expected, actual) \
288 Test::DoAssertEquals(wxT(__FILE__), __LINE__, expected, actual)
292 * Make a test fails with the given message.
293 * @param text Failure message
295 #define FAIL_M(text) \
296 THROW_TEST_FAILURE(text)
299 * Same as FAIL_M, but without an explicit message.
301 #define FAIL() FAIL_M(wxT("Test failed."))
305 * Requires that an exception of a certain type is raised.
307 #define ASSERT_RAISES_M(type, call, message) \
308 try { \
309 { call; }\
310 THROW_TEST_FAILURE(message); \
311 } catch (const type&) { \
312 } catch (const std::exception& e) { \
313 THROW_TEST_FAILURE(wxString::FromAscii(e.what())); \
319 * Same as ASSERT_RAISES, but without an explicit message.
321 #define ASSERT_RAISES(type, call) \
322 ASSERT_RAISES_M(type, (call), wxT("Exception of type ") wxT(#type) wxT(" not raised."))
327 * Define a test in a TestCase using test fixtures.
328 * User should put his test code between brackets after using this macro.
330 * This macro should only be used if test fixtures were declared earlier in
331 * this order: DECLARE, SETUP, TEARDOWN.
332 * @param testCaseName TestCase name where the test belongs to. Should be
333 * the same name of DECLARE, SETUP and TEARDOWN.
334 * @param testName Unique test name.
335 * @param testDisplayName This will be displayed when running the test.
337 #define TEST_M(testCaseName, testName, testDisplayName) \
338 class testCaseName##testName##Test : public testCaseName##Declare##Test \
340 public: \
341 testCaseName##testName##Test() \
342 : testCaseName##Declare##Test(wxT(#testCaseName), testDisplayName) \
346 void run(); \
347 } testCaseName##testName##Instance; \
349 void testCaseName##testName##Test::run()
352 * Define a test in a TestCase using test fixtures.
353 * User should put his test code between brackets after using this macro.
355 * This macro should only be used if test fixtures were declared earlier in
356 * this order: DECLARE, SETUP, TEARDOWN.
357 * @param testCaseName TestCase name where the test belongs to. Should be
358 * the same name of DECLARE, SETUP and TEARDOWN.
359 * @param testName Unique test name.
361 #define TEST(testCaseName, testName) TEST_M(testCaseName, testName, wxT(#testName))
364 * Location to declare variables and objets.
365 * This is where user should declare members accessible by TESTF,
366 * SETUP and TEARDOWN.
368 * User should not use brackets after using this macro. User should
369 * not initialize any members here.
371 * @param testCaseName TestCase name of the fixtures
372 * @see END_DECLARE for more information.
374 #define DECLARE(testCaseName)\
375 class testCaseName##Declare##Test : public Test \
377 public: \
378 testCaseName##Declare##Test(const wxString& testCaseName, const wxString& testName) \
379 : Test (testCaseName, testName) {} \
380 virtual void run() = 0; \
383 * Ending macro used after DECLARE.
385 * User should use this macro after declaring members with
386 * DECLARE macro.
388 #define END_DECLARE };
392 * Macro for creating a fixture with no setup/teardown or member variables.
394 #define DECLARE_SIMPLE(testCaseName) \
395 DECLARE(testCaseName) \
396 END_DECLARE;
398 } // MuleUnit ns
399 #endif // TEST_H