Fixes to the unittests
[amule.git] / unittests / muleunit / test.h
blobac90825c1f70d22d48d4e7cefa11145a78d355c1
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>
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 /** Print wide-char strings. */
52 inline void Print(const wxString& str)
54 wxPuts(str.c_str());
58 /** This exception is raised if an ASSERT fails. */
59 struct CTestFailureException : public std::exception
61 /** Constructor, takes a snapshot of the current context, and adds the given information. */
62 CTestFailureException(const wxString& msg, const wxChar* file, long lineNumber);
64 ~CTestFailureException() throw();
66 /** Prints the context backtrace for the location where the exception was thrown. */
67 void PrintBT() const;
69 virtual const char* what () const throw ();
70 private:
71 //! Pointer to struct containing a snapshot of the contexts
72 //! taken at the time the exception was created.
73 struct BTList* m_bt;
75 //! The message passed in the constructor.
76 std::string m_message;
79 /** This exception is raised if an wxASSERT fails. */
80 struct CAssertFailureException : public CTestFailureException
82 public:
83 CAssertFailureException(const wxString& msg, const wxChar* file, long lineNumber)
84 : CTestFailureException(msg, file, lineNumber)
90 /**
91 * This class is used to produce informative backtraces
93 * This is done by specifying a "context" for a given scope, using
94 * the CONTEXT macro, at which point a description is added to the
95 * current list of contexts. At destruction, when the scope is exited,
96 * the context is removed from the queue.
98 * The resulting "backtrace" can then be printed by calling the
99 * PrintBT() function of an CTestFailureException.
101 class CContext
103 public:
104 /** Adds a context with the specified information and description. */
105 CContext(const wxChar* file, int line, const wxString& desc);
107 /** Removes the context addded by the constructor. */
108 ~CContext();
112 //! Used to join the CContext instance name with the line-number.
113 //! This is done to prevent shadowing.
114 #define DO_CONTEXT(x, y, z) x y##z
116 //! Specifies the context of the current scope.
117 #define CONTEXT(x) CContext wxCONCAT(context,__LINE__)(wxT(__FILE__), __LINE__, x)
121 * This class disables assertions while it is in scope.
123 class CAssertOff
125 public:
126 CAssertOff();
127 ~CAssertOff();
132 * Helperfunction that converts basic types to strings.
134 template <typename TYPE>
135 wxString StringFrom(const TYPE& value)
137 return wxString() << value;
140 inline wxString StringFrom(unsigned long long value)
142 return wxString::Format(wxT("%") wxLongLongFmtSpec wxT("u"), value);
145 inline wxString StringFrom(signed long long value)
147 return wxString::Format(wxT("%") wxLongLongFmtSpec wxT("i"), value);
152 * Test class containing all macros to do unit testing.
153 * A test object represents a test that will be executed. Once it has been
154 * executed, it reports all failures in the testPartResult linked list.
156 * A failure occurs when a test fails (condition is false).
158 class Test
160 public:
162 * Main Test constructor. Used to create a test that will register itself
163 * with TestRegistry and with its test case.
164 * @param testCaseName Name of the test case this test belongs to
165 * @param testName Name of this test
167 Test(const wxString& testCaseName, const wxString& testName);
170 * Main Test desctructor
171 * Delete the testPartResult linked list. This is why the user should
172 * only use the macro provided by muleunit to report a test result.
174 virtual ~Test();
177 * Fixtures that will be called after run().
179 virtual void tearDown();
182 * Fixtures that will be called before run().
184 virtual void setUp();
187 * Test code should be in this method.
188 * run() will be called by the Test's TestCase, hence subclasses of Test
189 * should override this method.
191 virtual void run();
194 * Get the name of the TestCase this test belongs to. The name of the
195 * TestCase is the first parameter of the test declaration. For example,
196 * if a test is declared as TEST(TESTCASE1, TEST1), this method will return
197 * "TESTCASE1".
199 * @return The TestCase name of this test
201 const wxString& getTestCaseName() const;
204 * Get the name of this test. The name of the test is the second
205 * parameter of the test declaration. For example,
206 * if a test is declared as TEST(TESTCASE1, TEST1), this method will return
207 * "TEST1".
209 * @return The name of this test.
211 const wxString& getTestName() const;
213 template <typename A, typename B>
214 static void DoAssertEquals(const wxString& file, unsigned line, const A& a, const B& b)
216 if (!(a == b)) {
217 wxString message = wxT("Expected '") + StringFrom(a) +
218 wxT("' but got '") + StringFrom(b) + wxT("'");
220 throw CTestFailureException(message, file, line);
224 protected:
225 wxString m_testCaseName;
226 wxString m_testName;
227 TestCase* m_testCase;
231 #define THROW_TEST_FAILURE(message) \
232 throw CTestFailureException(message, wxT(__FILE__), __LINE__)
236 * Asserts that a condition is true.
237 * If the condition is not true, a failure is generated.
238 * @param condition Condition to fullfill for the assertion to pass
239 * @param message Message that will be displayed if this assertion fails
241 #define ASSERT_TRUE_M(condition, message) \
243 if (!(condition)) { \
244 THROW_TEST_FAILURE(message); \
250 * Same as ASSERT_TRUE, but without an explicit message.
252 #define ASSERT_TRUE(condition) \
253 ASSERT_TRUE_M(condition, wxString(wxT("Not true: ")) + wxT(#condition));
257 * Same as ASSERT_TRUE, but without an explicit message and condition must be false.
259 #define ASSERT_FALSE(condition) \
260 ASSERT_TRUE_M(!(condition), wxString(wxT("Not false: ")) + wxT(#condition));
264 * Asserts that the two parameters are equals. Operator == must be defined.
265 * If the two parameters are not equals, a failure is generated.
266 * @param expected Expected value
267 * @param actual Actual value to be compared
268 * @param message Message that will be displayed if this assertion fails
270 #define ASSERT_EQUALS_M(expected,actual,message)\
272 if (!(expected == actual)) { \
273 THROW_TEST_FAILURE(message); \
279 * Same as ASSERT_EQUALS_M, but without an explicit message.
281 #define ASSERT_EQUALS(expected, actual) \
282 Test::DoAssertEquals(wxT(__FILE__), __LINE__, expected, actual)
286 * Make a test fails with the given message.
287 * @param text Failure message
289 #define FAIL_M(text) \
290 THROW_TEST_FAILURE(text)
293 * Same as FAIL_M, but without an explicit message.
295 #define FAIL() FAIL_M(wxT("Test failed."))
299 * Requires that an exception of a certain type is raised.
301 #define ASSERT_RAISES_M(type, call, message) \
302 try { \
303 { call; }\
304 THROW_TEST_FAILURE(message); \
305 } catch (const type&) { \
306 } catch (const std::exception& e) { \
307 THROW_TEST_FAILURE(wxString::FromAscii(e.what())); \
313 * Same as ASSERT_RAISES, but without an explicit message.
315 #define ASSERT_RAISES(type, call) \
316 ASSERT_RAISES_M(type, (call), wxT("Exception of type ") wxT(#type) wxT(" not raised."))
321 * Define a test in a TestCase using test fixtures.
322 * User should put his test code between brackets after using this macro.
324 * This macro should only be used if test fixtures were declared earlier in
325 * this order: DECLARE, SETUP, TEARDOWN.
326 * @param testCaseName TestCase name where the test belongs to. Should be
327 * the same name of DECLARE, SETUP and TEARDOWN.
328 * @param testName Unique test name.
329 * @param testDisplayName This will be displayed when running the test.
331 #define TEST_M(testCaseName, testName, testDisplayName) \
332 class testCaseName##testName##Test : public testCaseName##Declare##Test \
334 public: \
335 testCaseName##testName##Test() \
336 : testCaseName##Declare##Test(wxT(#testCaseName), testDisplayName) \
340 void run(); \
341 } testCaseName##testName##Instance; \
343 void testCaseName##testName##Test::run()
346 * Define a test in a TestCase using test fixtures.
347 * User should put his test code between brackets after using this macro.
349 * This macro should only be used if test fixtures were declared earlier in
350 * this order: DECLARE, SETUP, TEARDOWN.
351 * @param testCaseName TestCase name where the test belongs to. Should be
352 * the same name of DECLARE, SETUP and TEARDOWN.
353 * @param testName Unique test name.
355 #define TEST(testCaseName, testName) TEST_M(testCaseName, testName, wxT(#testName))
358 * Location to declare variables and objets.
359 * This is where user should declare members accessible by TESTF,
360 * SETUP and TEARDOWN.
362 * User should not use brackets after using this macro. User should
363 * not initialize any members here.
365 * @param testCaseName TestCase name of the fixtures
366 * @see END_DECLARE for more information.
368 #define DECLARE(testCaseName)\
369 class testCaseName##Declare##Test : public Test \
371 public: \
372 testCaseName##Declare##Test(const wxString& testCaseName, const wxString& testName) \
373 : Test (testCaseName, testName) {} \
374 virtual void run() = 0; \
377 * Ending macro used after DECLARE.
379 * User should use this macro after declaring members with
380 * DECLARE macro.
382 #define END_DECLARE };
386 * Macro for creating a fixture with no setup/teardown or member variables.
388 #define DECLARE_SIMPLE(testCaseName) \
389 DECLARE(testCaseName) \
390 END_DECLARE;
392 } // MuleUnit ns
393 #endif // TEST_H