1
0

TestToolsLib.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * TestToolsLib.cpp
  3. * ----------------
  4. * Purpose: Unit test framework for libopenmpt.
  5. * Notes : Currently somewhat unreadable :/
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #include "stdafx.h"
  10. #include "TestToolsLib.h"
  11. #ifdef ENABLE_TESTS
  12. #ifndef MODPLUG_TRACKER
  13. #include <exception>
  14. #include <iostream>
  15. #include <cstdlib>
  16. OPENMPT_NAMESPACE_BEGIN
  17. namespace Test {
  18. void mpt_test_reporter::case_run(const mpt::source_location& loc)
  19. {
  20. #if !MPT_OS_DJGPP
  21. std::cout << "TEST..: " << MPT_AFORMAT("{}({}):")(loc.file_name() ? loc.file_name() : "", loc.line()) << ": " << std::endl;
  22. #else
  23. MPT_UNUSED(loc);
  24. #endif
  25. }
  26. void mpt_test_reporter::case_run(const mpt::source_location& loc, const char* text_e)
  27. {
  28. #if !MPT_OS_DJGPP
  29. std::cout << "TEST..: " << MPT_AFORMAT("{}({}): {}")(loc.file_name() ? loc.file_name() : "", loc.line(), text_e) << ": " << std::endl;
  30. #else
  31. MPT_UNUSED(loc);
  32. MPT_UNUSED(text_e);
  33. #endif
  34. }
  35. void mpt_test_reporter::case_run(const mpt::source_location& loc, const char* text_ex, const char* text_e)
  36. {
  37. if(text_ex)
  38. {
  39. #if !MPT_OS_DJGPP
  40. std::cout << "TEST..: " << MPT_AFORMAT("{}({}): {} throws {}")(loc.file_name() ? loc.file_name() : "", loc.line(), text_e, text_ex) << ": " << std::endl;
  41. #else
  42. MPT_UNUSED(loc);
  43. MPT_UNUSED(text_ex);
  44. MPT_UNUSED(text_e);
  45. #endif
  46. } else
  47. {
  48. #if !MPT_OS_DJGPP
  49. std::cout << "TEST..: " << MPT_AFORMAT("{}({}): {} throws")(loc.file_name() ? loc.file_name() : "", loc.line(), text_e) << ": " << std::endl;
  50. #else
  51. MPT_UNUSED(loc);
  52. MPT_UNUSED(text_ex);
  53. MPT_UNUSED(text_e);
  54. #endif
  55. }
  56. }
  57. void mpt_test_reporter::case_run(const mpt::source_location& loc, const char* text_a, const char* text_cmp, const char* text_b)
  58. {
  59. #if !MPT_OS_DJGPP
  60. std::cout << "TEST..: " << MPT_AFORMAT("{}({}): {} {} {}")(loc.file_name() ? loc.file_name() : "", loc.line(), text_a, text_cmp, text_b) << ": " << std::endl;
  61. #else
  62. MPT_UNUSED(loc);
  63. MPT_UNUSED(text_a);
  64. MPT_UNUSED(text_cmp);
  65. MPT_UNUSED(text_b);
  66. #endif
  67. }
  68. void mpt_test_reporter::case_result(const mpt::source_location& loc, const mpt::test::result& result)
  69. {
  70. MPT_UNUSED(loc);
  71. if(std::holds_alternative<mpt::test::result_success>(result.info))
  72. {
  73. #if !MPT_OS_DJGPP
  74. std::cout << "RESULT: PASS" << std::endl;
  75. #endif
  76. } else if(std::holds_alternative<mpt::test::result_failure>(result.info))
  77. {
  78. fail_count++;
  79. std::cout << "RESULT: FAIL" << std::endl;
  80. std::cout.flush();
  81. std::cerr << "FAIL: " << "FAILURE: " << std::get<mpt::test::result_failure>(result.info).text << std::endl;
  82. std::cerr.flush();
  83. } else if(std::holds_alternative<mpt::test::result_unexpected_exception>(result.info))
  84. {
  85. fail_count++;
  86. std::cout << "RESULT: FAIL" << std::endl;
  87. std::cout.flush();
  88. std::cerr << "FAIL: " << "UNEXPECTED EXCEPTION: " << std::get<mpt::test::result_unexpected_exception>(result.info).text << std::endl;
  89. std::cerr.flush();
  90. } else
  91. {
  92. fail_count++;
  93. std::cout << "RESULT: FAIL" << std::endl;
  94. std::cout.flush();
  95. std::cerr << "FAIL: " << "UNKOWN" << std::endl;
  96. std::cerr.flush();
  97. }
  98. }
  99. int fail_count = 0;
  100. static std::string remove_newlines(std::string str)
  101. {
  102. return mpt::replace(mpt::replace(str, std::string("\n"), std::string(" ")), std::string("\r"), std::string(" "));
  103. }
  104. Testcase::Testcase(Fatality fatality, Verbosity verbosity, const char * const desc, const mpt::source_location &loc)
  105. : fatality(fatality)
  106. , verbosity(verbosity)
  107. , desc(desc)
  108. , loc(loc)
  109. {
  110. return;
  111. }
  112. std::string Testcase::AsString() const
  113. {
  114. return MPT_AFORMAT("{}({}): {}")(loc.file_name() ? loc.file_name() : "", loc.line(), remove_newlines(desc));
  115. }
  116. void Testcase::ShowStart() const
  117. {
  118. switch(verbosity)
  119. {
  120. case VerbosityQuiet:
  121. break;
  122. case VerbosityNormal:
  123. #if !MPT_OS_DJGPP
  124. std::cout << "TEST..: " << AsString() << ": " << std::endl;
  125. #endif
  126. break;
  127. case VerbosityVerbose:
  128. #if !MPT_OS_DJGPP
  129. std::cout << "TEST..: " << AsString() << ": " << std::endl;
  130. #endif
  131. break;
  132. }
  133. }
  134. void Testcase::ShowProgress(const char * text) const
  135. {
  136. switch(verbosity)
  137. {
  138. case VerbosityQuiet:
  139. break;
  140. case VerbosityNormal:
  141. break;
  142. case VerbosityVerbose:
  143. #if !MPT_OS_DJGPP
  144. std::cout << "TEST..: " << AsString() << ": " << text << std::endl;
  145. #else
  146. MPT_UNUSED_VARIABLE(text);
  147. #endif
  148. break;
  149. }
  150. }
  151. void Testcase::ShowPass() const
  152. {
  153. switch(verbosity)
  154. {
  155. case VerbosityQuiet:
  156. break;
  157. case VerbosityNormal:
  158. #if !MPT_OS_DJGPP
  159. std::cout << "RESULT: PASS" << std::endl;
  160. #endif
  161. break;
  162. case VerbosityVerbose:
  163. #if !MPT_OS_DJGPP
  164. std::cout << "PASS..: " << AsString() << std::endl;
  165. #endif
  166. break;
  167. }
  168. }
  169. void Testcase::ShowFail(bool exception, const char * const text) const
  170. {
  171. switch(verbosity)
  172. {
  173. case VerbosityQuiet:
  174. break;
  175. case VerbosityNormal:
  176. std::cout << "RESULT: FAIL" << std::endl;
  177. break;
  178. case VerbosityVerbose:
  179. std::cout << "FAIL..: " << AsString() << std::endl;
  180. break;
  181. }
  182. std::cout.flush();
  183. if(!exception)
  184. {
  185. if(!text || (text && std::string(text).empty()))
  186. {
  187. std::cerr << "FAIL: " << AsString() << std::endl;
  188. } else
  189. {
  190. std::cerr << "FAIL: " << AsString() << " : " << text << std::endl;
  191. }
  192. } else
  193. {
  194. if(!text || (text && std::string(text).empty()))
  195. {
  196. std::cerr << "FAIL: " << AsString() << " EXCEPTION!" << std::endl;
  197. } else
  198. {
  199. std::cerr << "FAIL: " << AsString() << " EXCEPTION: " << text << std::endl;
  200. }
  201. }
  202. std::cerr.flush();
  203. }
  204. void Testcase::ReportPassed()
  205. {
  206. ShowPass();
  207. }
  208. void Testcase::ReportFailed()
  209. {
  210. fail_count++;
  211. ReportException();
  212. }
  213. void Testcase::ReportException()
  214. {
  215. try
  216. {
  217. throw; // get the exception
  218. } catch(TestFailed & e)
  219. {
  220. ShowFail(false, e.values.c_str());
  221. if(fatality == FatalityStop)
  222. {
  223. throw; // rethrow
  224. }
  225. } catch(std::exception & e)
  226. {
  227. ShowFail(true, e.what());
  228. throw; // rethrow
  229. } catch(...)
  230. {
  231. ShowFail(true);
  232. throw; // rethrow
  233. }
  234. }
  235. } // namespace Test
  236. #if defined(MPT_ASSERT_HANDLER_NEEDED)
  237. MPT_NOINLINE void AssertHandler(const mpt::source_location &loc, const char *expr, const char *msg)
  238. {
  239. Test::fail_count++;
  240. if(msg)
  241. {
  242. mpt::log::GlobalLogger().SendLogMessage(loc, LogError, "ASSERT",
  243. U_("ASSERTION FAILED: ") + mpt::ToUnicode(mpt::Charset::ASCII, msg) + U_(" (") + mpt::ToUnicode(mpt::Charset::ASCII, expr) + U_(")")
  244. );
  245. } else
  246. {
  247. mpt::log::GlobalLogger().SendLogMessage(loc, LogError, "ASSERT",
  248. U_("ASSERTION FAILED: ") + mpt::ToUnicode(mpt::Charset::ASCII, expr)
  249. );
  250. }
  251. #if defined(MPT_BUILD_FATAL_ASSERTS)
  252. std::abort();
  253. #endif // MPT_BUILD_FATAL_ASSERTS
  254. }
  255. #endif // MPT_ASSERT_HANDLER_NEEDED
  256. OPENMPT_NAMESPACE_END
  257. #endif // !MODPLUG_TRACKER
  258. #endif // ENABLE_TESTS