Upstream tarball 10152
[amule.git] / unittests / muleunit / test.h
blob4ca56292ed3849afbe4d9058037841ac1c2efbef
1 //
2 // MuleUnit: A minimalistic C++ Unit testing framework based on EasyUnit.
3 //
4 // Copyright (c) 2005-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2004-2008 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.
11 //
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.
16 //
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
20 //
22 #ifndef TEST_H
23 #define TEST_H
25 #include <exception>
27 #include <wx/string.h>
28 #include <list>
32 /**
33 * MuleUnit namespace.
34 * This is the namespace containing all muleunit classes.
36 namespace muleunit
39 class TestCase;
40 class BTList;
43 /** Returns the size of a static array. */
44 template <typename T, size_t N>
45 inline size_t ArraySize(T(&)[N])
47 return N;
51 /** Printf for wide-char strings. */
52 inline void Printf(const wxChar *pszFormat, ...)
54 va_list argptr;
55 va_start(argptr, pszFormat);
57 wxPuts(wxString::FormatV(pszFormat, argptr).c_str());
59 va_end(argptr);
63 /** This exception is raised if an ASSERT fails. */
64 struct CTestFailureException : public std::exception
66 /** Constructor, takes a snapshot of the current context, and adds the given information. */
67 CTestFailureException(const wxString& msg, const wxChar* file, long lineNumber);
69 ~CTestFailureException() throw();
71 /** Prints the context backtrace for the location where the exception was thrown. */
72 void PrintBT() const;
74 virtual const char* what () const throw ();
75 private:
76 //! Pointer to struct containing a snapshot of the contexts
77 //! taken at the time the exception was created.
78 struct BTList* m_bt;
80 //! The message passed in the constructor.
81 std::string m_message;
84 /** This exception is raised if an wxASSERT fails. */
85 struct CAssertFailureException : public CTestFailureException
87 public:
88 CAssertFailureException(const wxString& msg, const wxChar* file, long lineNumber)
89 : CTestFailureException(msg, file, lineNumber)
95 /**
96 * This class is used to produce informative backtraces
98 * This is done by specifying a "context" for a given scope, using
99 * the CONTEXT macro, at which point a description is added to the
100 * current list of contexts. At destruction, when the scope is exited,
101 * the context is removed from the queue.
103 * The resulting "backtrace" can then be printed by calling the
104 * PrintBT() function of an CTestFailureException.
106 class CContext
108 public:
109 /** Adds a context with the specified information and description. */
110 CContext(const wxChar* file, int line, const wxString& desc);
112 /** Removes the context addded by the constructor. */
113 ~CContext();
117 //! Used to join the CContext instance name with the line-number.
118 //! This is done to prevent shadowing.
119 #define DO_CONTEXT(x, y, z) x y##z
121 //! Specifies the context of the current scope.
122 #define CONTEXT(x) CContext wxCONCAT(context,__LINE__)(wxT(__FILE__), __LINE__, x)
125 /**
126 * This class disables assertions while it is in scope.
128 class CAssertOff
130 public:
131 CAssertOff();
132 ~CAssertOff();
137 * Test class containing all macros to do unit testing.
138 * A test object represents a test that will be executed. Once it has been
139 * executed, it reports all failures in the testPartResult linked list.
141 * A failure occurs when a test fails (condition is false).
143 class Test
145 public:
147 * Main Test constructor. Used to create a test that will register itself
148 * with TestRegistry and with its test case.
149 * @param testCaseName Name of the test case this test belongs to
150 * @param testName Name of this test
152 Test(const wxString& testCaseName, const wxString& testName);
155 * Main Test desctructor
156 * Delete the testPartResult linked list. This is why the user should
157 * only use the macro provided by muleunit to report a test result.
159 virtual ~Test();
162 * Fixtures that will be called after run().
164 virtual void tearDown();
167 * Fixtures that will be called before run().
169 virtual void setUp();
172 * Test code should be in this method.
173 * run() will be called by the Test's TestCase, hence subclasses of Test
174 * should override this method.
176 virtual void run();
179 * Get the name of the TestCase this test belongs to. The name of the
180 * TestCase is the first parameter of the test declaration. For example,
181 * if a test is declared as TEST(TESTCASE1, TEST1), this method will return
182 * "TESTCASE1".
184 * @return The TestCase name of this test
186 const wxString& getTestCaseName() const;
189 * Get the name of this test. The name of the test is the second
190 * parameter of the test declaration. For example,
191 * if a test is declared as TEST(TESTCASE1, TEST1), this method will return
192 * "TEST1".
194 * @return The name of this test.
196 const wxString& getTestName() const;
198 template <typename A, typename B>
199 static void DoAssertEquals(const wxString& file, unsigned line, const A& a, const B& b)
201 if (!(a == b)) {
202 wxString message = wxT("Expected '") + StringFrom(a) +
203 wxT("' but got '") + StringFrom(b) + wxT("'");
205 throw CTestFailureException(message, file, line);
209 protected:
210 wxString m_testCaseName;
211 wxString m_testName;
212 TestCase* m_testCase;
217 * Helperfunction that converts basic types to strings.
219 template <typename TYPE>
220 wxString StringFrom(const TYPE& value)
222 return wxString() << value;
225 inline wxString StringFrom(unsigned long long value)
227 return wxString::Format(wxT("%") wxLongLongFmtSpec wxT("u"), value);
230 inline wxString StringFrom(signed long long value)
232 return wxString::Format(wxT("%") wxLongLongFmtSpec wxT("i"), value);
236 #define THROW_TEST_FAILURE(message) \
237 throw CTestFailureException(message, wxT(__FILE__), __LINE__)
241 * Asserts that a condition is true.
242 * If the condition is not true, a failure is generated.
243 * @param condition Condition to fullfill for the assertion to pass
244 * @param message Message that will be displayed if this assertion fails
246 #define ASSERT_TRUE_M(condition, message) \
248 if (!(condition)) { \
249 THROW_TEST_FAILURE(message); \
255 * Same as ASSERT_TRUE, but without an explicit message.
257 #define ASSERT_TRUE(condition) \
258 ASSERT_TRUE_M(condition, wxString(wxT("Not true: ")) + wxT(#condition));
262 * Same as ASSERT_TRUE, but without an explicit message and condition must be false.
264 #define ASSERT_FALSE(condition) \
265 ASSERT_TRUE_M(!(condition), wxString(wxT("Not false: ")) + wxT(#condition));
269 * Asserts that the two parameters are equals. Operator == must be defined.
270 * If the two parameters are not equals, a failure is generated.
271 * @param expected Expected value
272 * @param actual Actual value to be compared
273 * @param message Message that will be displayed if this assertion fails
275 #define ASSERT_EQUALS_M(expected,actual,message)\
277 if (!(expected == actual)) { \
278 THROW_TEST_FAILURE(message); \
284 * Same as ASSERT_EQUALS_M, but without an explicit message.
286 #define ASSERT_EQUALS(expected, actual) \
287 Test::DoAssertEquals(wxT(__FILE__), __LINE__, expected, actual)
291 * Make a test fails with the given message.
292 * @param text Failure message
294 #define FAIL_M(text) \
295 THROW_TEST_FAILURE(text)
298 * Same as FAIL_M, but without an explicit message.
300 #define FAIL() FAIL_M(wxT("Test failed."))
304 * Requires that an exception of a certain type is raised.
306 #define ASSERT_RAISES_M(type, call, message) \
307 try { \
308 { call; }\
309 THROW_TEST_FAILURE(message); \
310 } catch (const type&) { \
311 } catch (const std::exception& e) { \
312 THROW_TEST_FAILURE(wxString::FromAscii(e.what())); \
318 * Same as ASSERT_RAISES, but without an explicit message.
320 #define ASSERT_RAISES(type, call) \
321 ASSERT_RAISES_M(type, (call), wxT("Exception of type ") wxT(#type) wxT(" not raised."))
326 * Define a test in a TestCase using test fixtures.
327 * User should put his test code between brackets after using this macro.
329 * This macro should only be used if test fixtures were declared earlier in
330 * this order: DECLARE, SETUP, TEARDOWN.
331 * @param testCaseName TestCase name where the test belongs to. Should be
332 * the same name of DECLARE, SETUP and TEARDOWN.
333 * @param testName Unique test name.
334 * @param testDisplayName This will be displayed when running the test.
336 #define TEST_M(testCaseName, testName, testDisplayName) \
337 class testCaseName##testName##Test : public testCaseName##Declare##Test \
339 public: \
340 testCaseName##testName##Test() \
341 : testCaseName##Declare##Test(wxT(#testCaseName), testDisplayName) \
345 void run(); \
346 } testCaseName##testName##Instance; \
348 void testCaseName##testName##Test::run()
351 * Define a test in a TestCase using test fixtures.
352 * User should put his test code between brackets after using this macro.
354 * This macro should only be used if test fixtures were declared earlier in
355 * this order: DECLARE, SETUP, TEARDOWN.
356 * @param testCaseName TestCase name where the test belongs to. Should be
357 * the same name of DECLARE, SETUP and TEARDOWN.
358 * @param testName Unique test name.
360 #define TEST(testCaseName, testName) TEST_M(testCaseName, testName, wxT(#testName))
363 * Location to declare variables and objets.
364 * This is where user should declare members accessible by TESTF,
365 * SETUP and TEARDOWN.
367 * User should not use brackets after using this macro. User should
368 * not initialize any members here.
370 * @param testCaseName TestCase name of the fixtures
371 * @see END_DECLARE for more information.
373 #define DECLARE(testCaseName)\
374 class testCaseName##Declare##Test : public Test \
376 public: \
377 testCaseName##Declare##Test(const wxString& testCaseName, const wxString& testName) \
378 : Test (testCaseName, testName) {} \
379 virtual void run() = 0; \
382 * Ending macro used after DECLARE.
384 * User should use this macro after declaring members with
385 * DECLARE macro.
387 #define END_DECLARE };
391 * Macro for creating a fixture with no setup/teardown or member variables.
393 #define DECLARE_SIMPLE(testCaseName) \
394 DECLARE(testCaseName) \
395 END_DECLARE;
397 } // MuleUnit ns
398 #endif // TEST_H