Message.tests.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // Copyright Catch2 Authors
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // https://www.boost.org/LICENSE_1_0.txt)
  5. // SPDX-License-Identifier: BSL-1.0
  6. #include <catch2/catch_test_macros.hpp>
  7. #include <iostream>
  8. TEST_CASE( "INFO and WARN do not abort tests", "[messages][.]" ) {
  9. INFO( "this is a " << "message" ); // This should output the message if a failure occurs
  10. WARN( "this is a " << "warning" ); // This should always output the message but then continue
  11. }
  12. TEST_CASE( "#1455 - INFO and WARN can start with a linebreak", "[messages][.]" ) {
  13. // Previously these would be hidden from the console reporter output,
  14. // because it would fail at properly reflowing the text
  15. INFO( "\nThis info message starts with a linebreak" );
  16. WARN( "\nThis warning message starts with a linebreak" );
  17. }
  18. TEST_CASE( "SUCCEED counts as a test pass", "[messages]" ) {
  19. SUCCEED( "this is a " << "success" );
  20. }
  21. TEST_CASE( "INFO gets logged on failure", "[failing][messages][.]" ) {
  22. INFO( "this message should be logged" );
  23. INFO( "so should this" );
  24. int a = 2;
  25. REQUIRE( a == 1 );
  26. }
  27. TEST_CASE( "INFO gets logged on failure, even if captured before successful assertions", "[failing][messages][.]" ) {
  28. INFO( "this message may be logged later" );
  29. int a = 2;
  30. CHECK( a == 2 );
  31. INFO( "this message should be logged" );
  32. CHECK( a == 1 );
  33. INFO( "and this, but later" );
  34. CHECK( a == 0 );
  35. INFO( "but not this" );
  36. CHECK( a == 2 );
  37. }
  38. TEST_CASE( "FAIL aborts the test", "[failing][messages][.]" ) {
  39. FAIL( "This is a " << "failure" ); // This should output the message and abort
  40. WARN( "We should never see this");
  41. }
  42. TEST_CASE( "FAIL_CHECK does not abort the test", "[failing][messages][.]" ) {
  43. FAIL_CHECK( "This is a " << "failure" ); // This should output the message then continue
  44. WARN( "This message appears in the output");
  45. }
  46. TEST_CASE( "FAIL does not require an argument", "[failing][messages][.]" ) {
  47. FAIL();
  48. }
  49. TEST_CASE( "SUCCEED does not require an argument", "[messages][.]" ) {
  50. SUCCEED();
  51. }
  52. TEST_CASE( "Output from all sections is reported", "[failing][messages][.]" ) {
  53. SECTION( "one" ) {
  54. FAIL( "Message from section one" );
  55. }
  56. SECTION( "two" ) {
  57. FAIL( "Message from section two" );
  58. }
  59. }
  60. TEST_CASE( "Standard output from all sections is reported", "[messages][.]" ) {
  61. SECTION( "one" ) {
  62. std::cout << "Message from section one" << std::endl;
  63. }
  64. SECTION( "two" ) {
  65. std::cout << "Message from section two" << std::endl;
  66. }
  67. }
  68. TEST_CASE( "Standard error is reported and redirected", "[messages][.][approvals]" ) {
  69. SECTION( "std::cerr" ) {
  70. std::cerr << "Write to std::cerr" << std::endl;
  71. }
  72. SECTION( "std::clog" ) {
  73. std::clog << "Write to std::clog" << std::endl;
  74. }
  75. SECTION( "Interleaved writes to cerr and clog" ) {
  76. std::cerr << "Inter";
  77. std::clog << "leaved";
  78. std::cerr << ' ';
  79. std::clog << "writes";
  80. std::cerr << " to error";
  81. std::clog << " streams" << std::endl;
  82. }
  83. }
  84. TEST_CASE( "INFO is reset for each loop", "[messages][failing][.]" ) {
  85. for( int i=0; i<100; i++ )
  86. {
  87. INFO( "current counter " << i );
  88. CAPTURE( i );
  89. REQUIRE( i < 10 );
  90. }
  91. }
  92. TEST_CASE( "The NO_FAIL macro reports a failure but does not fail the test", "[messages]" ) {
  93. CHECK_NOFAIL( 1 == 2 );
  94. }
  95. TEST_CASE( "just info", "[info][isolated info][messages]" ) {
  96. INFO( "this should never be seen" );
  97. }
  98. TEST_CASE( "just failure", "[fail][isolated info][.][messages]" ) {
  99. FAIL( "Previous info should not be seen" );
  100. }
  101. TEST_CASE( "sends information to INFO", "[.][failing]" ) {
  102. INFO( "hi" );
  103. int i = 7;
  104. CAPTURE( i );
  105. REQUIRE( false );
  106. }
  107. TEST_CASE( "Pointers can be converted to strings", "[messages][.][approvals]" ) {
  108. int p;
  109. WARN( "actual address of p: " << &p );
  110. WARN( "toString(p): " << ::Catch::Detail::stringify( &p ) );
  111. }
  112. template <typename T>
  113. static void unscoped_info( T msg ) {
  114. UNSCOPED_INFO( msg );
  115. }
  116. TEST_CASE( "just unscoped info", "[unscoped][info]" ) {
  117. unscoped_info( "this should NOT be seen" );
  118. unscoped_info( "this also should NOT be seen" );
  119. }
  120. TEST_CASE( "just failure after unscoped info", "[failing][.][unscoped][info]" ) {
  121. FAIL( "previous unscoped info SHOULD not be seen" );
  122. }
  123. TEST_CASE( "print unscoped info if passing unscoped info is printed", "[unscoped][info]" ) {
  124. unscoped_info( "this MAY be seen IF info is printed for passing assertions" );
  125. REQUIRE( true );
  126. }
  127. TEST_CASE( "prints unscoped info on failure", "[failing][.][unscoped][info]" ) {
  128. unscoped_info( "this SHOULD be seen" );
  129. unscoped_info( "this SHOULD also be seen" );
  130. REQUIRE( false );
  131. unscoped_info( "but this should NOT be seen" );
  132. }
  133. TEST_CASE( "not prints unscoped info from previous failures", "[failing][.][unscoped][info]" ) {
  134. unscoped_info( "this MAY be seen only for the FIRST assertion IF info is printed for passing assertions" );
  135. REQUIRE( true );
  136. unscoped_info( "this MAY be seen only for the SECOND assertion IF info is printed for passing assertions" );
  137. REQUIRE( true );
  138. unscoped_info( "this SHOULD be seen" );
  139. REQUIRE( false );
  140. }
  141. TEST_CASE( "prints unscoped info only for the first assertion", "[failing][.][unscoped][info]" ) {
  142. unscoped_info( "this SHOULD be seen only ONCE" );
  143. CHECK( false );
  144. CHECK( true );
  145. unscoped_info( "this MAY also be seen only ONCE IF info is printed for passing assertions" );
  146. CHECK( true );
  147. CHECK( true );
  148. }
  149. TEST_CASE( "stacks unscoped info in loops", "[failing][.][unscoped][info]" ) {
  150. UNSCOPED_INFO("Count 1 to 3...");
  151. for (int i = 1; i <= 3; i++) {
  152. unscoped_info(i);
  153. }
  154. CHECK( false );
  155. UNSCOPED_INFO("Count 4 to 6...");
  156. for (int i = 4; i <= 6; i++) {
  157. unscoped_info(i);
  158. }
  159. CHECK( false );
  160. }
  161. TEST_CASE( "mix info, unscoped info and warning", "[unscoped][info]" ) {
  162. INFO("info");
  163. unscoped_info("unscoped info");
  164. WARN("and warn may mix");
  165. WARN("they are not cleared after warnings");
  166. }
  167. TEST_CASE( "CAPTURE can deal with complex expressions", "[messages][capture]" ) {
  168. int a = 1;
  169. int b = 2;
  170. int c = 3;
  171. CAPTURE( a, b, c, a + b, a+b, c > b, a == 1 );
  172. SUCCEED();
  173. }
  174. #ifdef __clang__
  175. #pragma clang diagnostic push
  176. #pragma clang diagnostic ignored "-Wunused-value" // In (1, 2), the "1" is unused ...
  177. #endif
  178. #ifdef __GNUC__
  179. #pragma GCC diagnostic push
  180. #pragma GCC diagnostic ignored "-Wunused-value" // All the comma operators are side-effect free
  181. #endif
  182. #ifdef _MSC_VER
  183. #pragma warning(push)
  184. #pragma warning(disable:4709) // comma in indexing operator
  185. #endif
  186. template <typename T1, typename T2>
  187. struct helper_1436 {
  188. helper_1436(T1 t1_, T2 t2_):
  189. t1{ t1_ },
  190. t2{ t2_ }
  191. {}
  192. T1 t1;
  193. T2 t2;
  194. };
  195. template <typename T1, typename T2>
  196. std::ostream& operator<<(std::ostream& out, helper_1436<T1, T2> const& helper) {
  197. out << "{ " << helper.t1 << ", " << helper.t2 << " }";
  198. return out;
  199. }
  200. // Clang and gcc have different names for this warning, and clang also
  201. // warns about an unused value. This warning must be disabled for C++20.
  202. #if defined(__GNUG__) && !defined(__clang__)
  203. #pragma GCC diagnostic push
  204. #pragma GCC diagnostic ignored "-Wpragmas"
  205. #pragma GCC diagnostic ignored "-Wcomma-subscript"
  206. #elif defined(__clang__)
  207. #pragma clang diagnostic push
  208. #pragma clang diagnostic ignored "-Wunknown-pragmas"
  209. #pragma clang diagnostic ignored "-Wunknown-warning-option"
  210. #pragma clang diagnostic ignored "-Wdeprecated-comma-subscript"
  211. #pragma clang diagnostic ignored "-Wunused-value"
  212. #endif
  213. TEST_CASE("CAPTURE can deal with complex expressions involving commas", "[messages][capture]") {
  214. CAPTURE(std::vector<int>{1, 2, 3}[0, 1, 2],
  215. std::vector<int>{1, 2, 3}[(0, 1)],
  216. std::vector<int>{1, 2, 3}[0]);
  217. CAPTURE((helper_1436<int, int>{12, -12}),
  218. (helper_1436<int, int>(-12, 12)));
  219. CAPTURE( (1, 2), (2, 3) );
  220. SUCCEED();
  221. }
  222. #ifdef __GNUG__
  223. #pragma GCC diagnostic pop
  224. #endif
  225. TEST_CASE("CAPTURE parses string and character constants", "[messages][capture]") {
  226. CAPTURE(("comma, in string", "escaped, \", "), "single quote in string,',", "some escapes, \\,\\\\");
  227. CAPTURE("some, ), unmatched, } prenheses {[<");
  228. CAPTURE('"', '\'', ',', '}', ')', '(', '{');
  229. SUCCEED();
  230. }
  231. #ifdef __clang__
  232. #pragma clang diagnostic pop
  233. #endif
  234. #ifdef __GNUC__
  235. #pragma GCC diagnostic pop
  236. #endif
  237. #ifdef _MSC_VER
  238. #pragma warning(pop)
  239. #endif