Message.tests.cpp 7.8 KB

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