cppunit.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/cppunit.h
  3. // Purpose: wrapper header for CppUnit headers
  4. // Author: Vadim Zeitlin
  5. // Created: 15.02.04
  6. // Copyright: (c) 2004 Vadim Zeitlin
  7. // Licence: wxWindows Licence
  8. /////////////////////////////////////////////////////////////////////////////
  9. #ifndef _WX_CPPUNIT_H_
  10. #define _WX_CPPUNIT_H_
  11. ///////////////////////////////////////////////////////////////////////////////
  12. // using CPPUNIT_TEST() macro results in this warning, disable it as there is
  13. // no other way to get rid of it and it's not very useful anyhow
  14. #ifdef __VISUALC__
  15. // typedef-name 'foo' used as synonym for class-name 'bar'
  16. #pragma warning(disable:4097)
  17. // unreachable code: we don't care about warnings in CppUnit headers
  18. #pragma warning(disable:4702)
  19. // 'id': identifier was truncated to 'num' characters in the debug info
  20. #pragma warning(disable:4786)
  21. #endif // __VISUALC__
  22. #ifdef __BORLANDC__
  23. #pragma warn -8022
  24. #endif
  25. #ifndef CPPUNIT_STD_NEED_ALLOCATOR
  26. #define CPPUNIT_STD_NEED_ALLOCATOR 0
  27. #endif
  28. ///////////////////////////////////////////////////////////////////////////////
  29. // Set the default format for the errors, which can be used by an IDE to jump
  30. // to the error location. This default gets overridden by the cppunit headers
  31. // for some compilers (e.g. VC++).
  32. #ifndef CPPUNIT_COMPILER_LOCATION_FORMAT
  33. #define CPPUNIT_COMPILER_LOCATION_FORMAT "%p:%l:"
  34. #endif
  35. ///////////////////////////////////////////////////////////////////////////////
  36. // Include all needed cppunit headers.
  37. //
  38. #include "wx/beforestd.h"
  39. #ifdef __VISUALC__
  40. #pragma warning(push)
  41. // with cppunit 1.12 we get many bogus warnings 4701 (local variable may be
  42. // used without having been initialized) in TestAssert.h
  43. #pragma warning(disable:4701)
  44. // and also 4100 (unreferenced formal parameter) in extensions/
  45. // ExceptionTestCaseDecorator.h
  46. #pragma warning(disable:4100)
  47. #endif
  48. #include <cppunit/extensions/TestFactoryRegistry.h>
  49. #include <cppunit/ui/text/TestRunner.h>
  50. #include <cppunit/TestCase.h>
  51. #include <cppunit/extensions/HelperMacros.h>
  52. #include <cppunit/CompilerOutputter.h>
  53. #ifdef __VISUALC__
  54. #pragma warning(pop)
  55. #endif
  56. #include "wx/afterstd.h"
  57. #include "wx/string.h"
  58. ///////////////////////////////////////////////////////////////////////////////
  59. // Set of helpful test macros.
  60. //
  61. // Base macro for wrapping CPPUNIT_TEST macros and so making them conditional
  62. // tests, meaning that the test only get registered and thus run when a given
  63. // runtime condition is true.
  64. // In case the condition is evaluated as false a skip message is logged
  65. // (the message will only be shown in verbose mode).
  66. #define WXTEST_ANY_WITH_CONDITION(suiteName, Condition, testMethod, anyTest) \
  67. if (Condition) \
  68. { anyTest; } \
  69. else \
  70. wxLogInfo(wxString::Format(wxT("skipping: %s.%s\n reason: %s equals false\n"), \
  71. wxString(suiteName, wxConvUTF8).c_str(), \
  72. wxString(#testMethod, wxConvUTF8).c_str(), \
  73. wxString(#Condition, wxConvUTF8).c_str()))
  74. // Conditional CPPUNIT_TEST macro.
  75. #define WXTEST_WITH_CONDITION(suiteName, Condition, testMethod) \
  76. WXTEST_ANY_WITH_CONDITION(suiteName, Condition, testMethod, CPPUNIT_TEST(testMethod))
  77. // Conditional CPPUNIT_TEST_FAIL macro.
  78. #define WXTEST_FAIL_WITH_CONDITION(suiteName, Condition, testMethod) \
  79. WXTEST_ANY_WITH_CONDITION(suiteName, Condition, testMethod, CPPUNIT_TEST_FAIL(testMethod))
  80. CPPUNIT_NS_BEGIN
  81. // provide an overload of cppunit assertEquals(T, T) which can be used to
  82. // compare wxStrings directly with C strings
  83. inline void
  84. assertEquals(const char *expected,
  85. const char *actual,
  86. CppUnit::SourceLine sourceLine,
  87. const std::string& message)
  88. {
  89. assertEquals(wxString(expected), wxString(actual), sourceLine, message);
  90. }
  91. inline void
  92. assertEquals(const char *expected,
  93. const wxString& actual,
  94. CppUnit::SourceLine sourceLine,
  95. const std::string& message)
  96. {
  97. assertEquals(wxString(expected), actual, sourceLine, message);
  98. }
  99. inline void
  100. assertEquals(const wxString& expected,
  101. const char *actual,
  102. CppUnit::SourceLine sourceLine,
  103. const std::string& message)
  104. {
  105. assertEquals(expected, wxString(actual), sourceLine, message);
  106. }
  107. inline void
  108. assertEquals(const wchar_t *expected,
  109. const wxString& actual,
  110. CppUnit::SourceLine sourceLine,
  111. const std::string& message)
  112. {
  113. assertEquals(wxString(expected), actual, sourceLine, message);
  114. }
  115. inline void
  116. assertEquals(const wxString& expected,
  117. const wchar_t *actual,
  118. CppUnit::SourceLine sourceLine,
  119. const std::string& message)
  120. {
  121. assertEquals(expected, wxString(actual), sourceLine, message);
  122. }
  123. CPPUNIT_NS_END
  124. // define an assertEquals() overload for the given types, this is a helper and
  125. // shouldn't be used directly because of VC6 complications, see below
  126. #define WX_CPPUNIT_ASSERT_EQUALS(T1, T2) \
  127. inline void \
  128. assertEquals(T1 expected, \
  129. T2 actual, \
  130. CppUnit::SourceLine sourceLine, \
  131. const std::string& message) \
  132. { \
  133. if ( !assertion_traits<T1>::equal(expected,actual) ) \
  134. { \
  135. Asserter::failNotEqual( assertion_traits<T1>::toString(expected), \
  136. assertion_traits<T2>::toString(actual), \
  137. sourceLine, \
  138. message ); \
  139. } \
  140. }
  141. // this macro allows us to specify (usually literal) ints as expected values
  142. // for functions returning integral types different from "int"
  143. //
  144. // FIXME-VC6: due to incorrect resolution of overloaded/template functions in
  145. // this compiler (it basically doesn't use the template version at
  146. // all if any overloaded function matches partially even if none of
  147. // them matches fully) we also need to provide extra overloads
  148. #ifdef __VISUALC6__
  149. #define WX_CPPUNIT_ALLOW_EQUALS_TO_INT(T) \
  150. CPPUNIT_NS_BEGIN \
  151. WX_CPPUNIT_ASSERT_EQUALS(int, T) \
  152. WX_CPPUNIT_ASSERT_EQUALS(T, int) \
  153. WX_CPPUNIT_ASSERT_EQUALS(T, T) \
  154. CPPUNIT_NS_END
  155. CPPUNIT_NS_BEGIN
  156. WX_CPPUNIT_ASSERT_EQUALS(int, int)
  157. CPPUNIT_NS_END
  158. #else // !VC6
  159. #define WX_CPPUNIT_ALLOW_EQUALS_TO_INT(T) \
  160. CPPUNIT_NS_BEGIN \
  161. WX_CPPUNIT_ASSERT_EQUALS(int, T) \
  162. WX_CPPUNIT_ASSERT_EQUALS(T, int) \
  163. CPPUNIT_NS_END
  164. #endif // VC6/!VC6
  165. WX_CPPUNIT_ALLOW_EQUALS_TO_INT(long)
  166. WX_CPPUNIT_ALLOW_EQUALS_TO_INT(short)
  167. WX_CPPUNIT_ALLOW_EQUALS_TO_INT(unsigned)
  168. WX_CPPUNIT_ALLOW_EQUALS_TO_INT(unsigned long)
  169. #if defined( __VMS ) && defined( __ia64 )
  170. WX_CPPUNIT_ALLOW_EQUALS_TO_INT(std::basic_streambuf<char>::pos_type);
  171. #endif
  172. #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
  173. WX_CPPUNIT_ALLOW_EQUALS_TO_INT(wxLongLong_t)
  174. WX_CPPUNIT_ALLOW_EQUALS_TO_INT(unsigned wxLongLong_t)
  175. #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
  176. // Use this macro to compare a wxArrayString with the pipe-separated elements
  177. // of the given string
  178. //
  179. // NB: it's a macro and not a function to have the correct line numbers in the
  180. // test failure messages
  181. #define WX_ASSERT_STRARRAY_EQUAL(s, a) \
  182. { \
  183. wxArrayString expected(wxSplit(s, '|', '\0')); \
  184. \
  185. CPPUNIT_ASSERT_EQUAL( expected.size(), a.size() ); \
  186. \
  187. for ( size_t n = 0; n < a.size(); n++ ) \
  188. { \
  189. CPPUNIT_ASSERT_EQUAL( expected[n], a[n] ); \
  190. } \
  191. }
  192. // Use this macro to assert with the given formatted message (it should contain
  193. // the format string and arguments in a separate pair of parentheses)
  194. #define WX_ASSERT_MESSAGE(msg, cond) \
  195. CPPUNIT_ASSERT_MESSAGE(std::string(wxString::Format msg .mb_str()), (cond))
  196. #define WX_ASSERT_EQUAL_MESSAGE(msg, expected, actual) \
  197. CPPUNIT_ASSERT_EQUAL_MESSAGE(std::string(wxString::Format msg .mb_str()), \
  198. (expected), (actual))
  199. ///////////////////////////////////////////////////////////////////////////////
  200. // define stream inserter for wxString if it's not defined in the main library,
  201. // we need it to output the test failures involving wxString
  202. #if !wxUSE_STD_IOSTREAM
  203. #include "wx/string.h"
  204. #include <iostream>
  205. inline std::ostream& operator<<(std::ostream& o, const wxString& s)
  206. {
  207. #if wxUSE_UNICODE
  208. return o << (const char *)wxSafeConvertWX2MB(s.wc_str());
  209. #else
  210. return o << s.c_str();
  211. #endif
  212. }
  213. #endif // !wxUSE_STD_IOSTREAM
  214. // VC6 doesn't provide overloads for operator<<(__int64) in its stream classes
  215. // so do it ourselves
  216. #if defined(__VISUALC6__) && defined(wxLongLong_t)
  217. #include "wx/longlong.h"
  218. inline std::ostream& operator<<(std::ostream& ostr, wxLongLong_t ll)
  219. {
  220. ostr << wxLongLong(ll).ToString();
  221. return ostr;
  222. }
  223. inline std::ostream& operator<<(std::ostream& ostr, unsigned wxLongLong_t llu)
  224. {
  225. ostr << wxULongLong(llu).ToString();
  226. return ostr;
  227. }
  228. #endif // VC6 && wxLongLong_t
  229. ///////////////////////////////////////////////////////////////////////////////
  230. // Some more compiler warning tweaking and auto linking.
  231. //
  232. #ifdef __BORLANDC__
  233. #pragma warn .8022
  234. #endif
  235. #ifdef _MSC_VER
  236. #pragma warning(default:4702)
  237. #endif // _MSC_VER
  238. // for VC++ automatically link in cppunit library
  239. #ifdef __VISUALC__
  240. #ifdef NDEBUG
  241. #pragma comment(lib, "cppunit.lib")
  242. #else // Debug
  243. #pragma comment(lib, "cppunitd.lib")
  244. #endif // Release/Debug
  245. #endif
  246. #endif // _WX_CPPUNIT_H_