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
9 #include "catch.hpp"
10 #include <iostream>
11
12 TEST_CASE( "INFO and WARN do not abort tests", "[messages][.]" ) {
13 INFO( "this is a " << "message" ); // This should output the message if a failure occurs
14 WARN( "this is a " << "warning" ); // This should always output the message but then continue
15 }
16
17 TEST_CASE( "SUCCEED counts as a test pass", "[messages]" ) {
18 SUCCEED( "this is a " << "success" );
19 }
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
28 TEST_CASE( "INFO gets logged on failure, even if captured before successful assertions", "[failing][messages][.]" ) {
29 INFO( "this message may be logged later" );
30 int a = 2;
31 CHECK( a == 2 );
32
33 INFO( "this message should be logged" );
34
35 CHECK( a == 1 );
36
37 INFO( "and this, but later" );
38
39 CHECK( a == 0 );
40
41 INFO( "but not this" );
42
43 CHECK( a == 2 );
44 }
45
46 TEST_CASE( "FAIL aborts the test", "[failing][messages][.]" ) {
47 FAIL( "This is a " << "failure" ); // This should output the message and abort
48 WARN( "We should never see this");
49 }
50
51 TEST_CASE( "FAIL_CHECK does not abort the test", "[failing][messages][.]" ) {
52 FAIL_CHECK( "This is a " << "failure" ); // This should output the message then continue
53 WARN( "This message appears in the output");
54 }
55
56 TEST_CASE( "FAIL does not require an argument", "[failing][messages][.]" ) {
57 FAIL();
58 }
59
60 TEST_CASE( "SUCCEED does not require an argument", "[messages][.]" ) {
61 SUCCEED();
62 }
63
64 TEST_CASE( "Output from all sections is reported", "[failing][messages][.]" ) {
65 SECTION( "one" ) {
66 FAIL( "Message from section one" );
67 }
68
69 SECTION( "two" ) {
70 FAIL( "Message from section two" );
71 }
72 }
73
74 TEST_CASE( "Standard output from all sections is reported", "[messages][.]" ) {
75 SECTION( "one" ) {
76 std::cout << "Message from section one" << std::endl;
77 }
78
79 SECTION( "two" ) {
80 std::cout << "Message from section two" << std::endl;
81 }
82 }
83
84 TEST_CASE( "Standard error is reported and redirected", "[messages][.][approvals]" ) {
85 SECTION( "std::cerr" ) {
86 std::cerr << "Write to std::cerr" << std::endl;
87 }
88 SECTION( "std::clog" ) {
89 std::clog << "Write to std::clog" << std::endl;
90 }
91 SECTION( "Interleaved writes to cerr and clog" ) {
92 std::cerr << "Inter";
93 std::clog << "leaved";
94 std::cerr << ' ';
95 std::clog << "writes";
96 std::cerr << " to error";
97 std::clog << " streams" << std::endl;
98 }
99 }
100
101 TEST_CASE( "INFO is reset for each loop", "[messages][failing][.]" ) {
102 for( int i=0; i<100; i++ )
103 {
104 INFO( "current counter " << i );
105 CAPTURE( i );
106 REQUIRE( i < 10 );
107 }
108 }
109
110 TEST_CASE( "The NO_FAIL macro reports a failure but does not fail the test", "[messages]" ) {
111 CHECK_NOFAIL( 1 == 2 );
112 }
113
114 TEST_CASE( "just info", "[info][isolated info][messages]" ) {
115 INFO( "this should never be seen" );
116 }
117 TEST_CASE( "just failure", "[fail][isolated info][.][messages]" ) {
118 FAIL( "Previous info should not be seen" );
119 }
120
121
122 TEST_CASE( "sends information to INFO", "[.][failing]" ) {
123 INFO( "hi" );
124 int i = 7;
125 CAPTURE( i );
126 REQUIRE( false );
127 }
128
129 TEST_CASE( "Pointers can be converted to strings", "[messages][.][approvals]" ) {
130 int p;
131 WARN( "actual address of p: " << &p );
132 WARN( "toString(p): " << ::Catch::Detail::stringify( &p ) );
133 }
134
135 template <typename T>
unscoped_info(T msg)136 static void unscoped_info( T msg ) {
137 UNSCOPED_INFO( msg );
138 }
139
140 TEST_CASE( "just unscoped info", "[unscoped][info]" ) {
141 unscoped_info( "this should NOT be seen" );
142 unscoped_info( "this also should NOT be seen" );
143 }
144
145 TEST_CASE( "just failure after unscoped info", "[failing][.][unscoped][info]" ) {
146 FAIL( "previous unscoped info SHOULD not be seen" );
147 }
148
149 TEST_CASE( "print unscoped info if passing unscoped info is printed", "[unscoped][info]" ) {
150 unscoped_info( "this MAY be seen IF info is printed for passing assertions" );
151 REQUIRE( true );
152 }
153
154 TEST_CASE( "prints unscoped info on failure", "[failing][.][unscoped][info]" ) {
155 unscoped_info( "this SHOULD be seen" );
156 unscoped_info( "this SHOULD also be seen" );
157 REQUIRE( false );
158 unscoped_info( "but this should NOT be seen" );
159 }
160
161 TEST_CASE( "not prints unscoped info from previous failures", "[failing][.][unscoped][info]" ) {
162 unscoped_info( "this MAY be seen only for the FIRST assertion IF info is printed for passing assertions" );
163 REQUIRE( true );
164 unscoped_info( "this MAY be seen only for the SECOND assertion IF info is printed for passing assertions" );
165 REQUIRE( true );
166 unscoped_info( "this SHOULD be seen" );
167 REQUIRE( false );
168 }
169
170 TEST_CASE( "prints unscoped info only for the first assertion", "[failing][.][unscoped][info]" ) {
171 unscoped_info( "this SHOULD be seen only ONCE" );
172 CHECK( false );
173 CHECK( true );
174 unscoped_info( "this MAY also be seen only ONCE IF info is printed for passing assertions" );
175 CHECK( true );
176 CHECK( true );
177 }
178
179 TEST_CASE( "stacks unscoped info in loops", "[failing][.][unscoped][info]" ) {
180 UNSCOPED_INFO("Count 1 to 3...");
181 for (int i = 1; i <= 3; i++) {
182 unscoped_info(i);
183 }
184 CHECK( false );
185
186 UNSCOPED_INFO("Count 4 to 6...");
187 for (int i = 4; i <= 6; i++) {
188 unscoped_info(i);
189 }
190 CHECK( false );
191 }
192
193 TEST_CASE( "mix info, unscoped info and warning", "[unscoped][info]" ) {
194 INFO("info");
195 unscoped_info("unscoped info");
196 WARN("and warn may mix");
197 WARN("they are not cleared after warnings");
198 }
199
200 TEST_CASE( "CAPTURE can deal with complex expressions", "[messages][capture]" ) {
201 int a = 1;
202 int b = 2;
203 int c = 3;
204 CAPTURE( a, b, c, a + b, a+b, c > b, a == 1 );
205 SUCCEED();
206 }
207
208 #ifdef __clang__
209 #pragma clang diagnostic push
210 #pragma clang diagnostic ignored "-Wunused-value" // In (1, 2), the "1" is unused ...
211 #endif
212 #ifdef __GNUC__
213 #pragma GCC diagnostic push
214 #pragma GCC diagnostic ignored "-Wunused-value" // All the comma operators are side-effect free
215 #endif
216 #ifdef _MSC_VER
217 #pragma warning(push)
218 #pragma warning(disable:4709) // comma in indexing operator
219 #endif
220
221 template <typename T1, typename T2>
222 struct helper_1436 {
helper_1436helper_1436223 helper_1436(T1 t1, T2 t2):
224 t1{ t1 },
225 t2{ t2 }
226 {}
227 T1 t1;
228 T2 t2;
229 };
230
231 template <typename T1, typename T2>
operator <<(std::ostream & out,helper_1436<T1,T2> const & helper)232 std::ostream& operator<<(std::ostream& out, helper_1436<T1, T2> const& helper) {
233 out << "{ " << helper.t1 << ", " << helper.t2 << " }";
234 return out;
235 }
236
237 TEST_CASE("CAPTURE can deal with complex expressions involving commas", "[messages][capture]") {
238 CAPTURE(std::vector<int>{1, 2, 3}[0, 1, 2],
239 std::vector<int>{1, 2, 3}[(0, 1)],
240 std::vector<int>{1, 2, 3}[0]);
241 CAPTURE((helper_1436<int, int>{12, -12}),
242 (helper_1436<int, int>(-12, 12)));
243 CAPTURE( (1, 2), (2, 3) );
244 SUCCEED();
245 }
246
247 #ifdef __clang__
248 #pragma clang diagnostic pop
249 #endif
250 #ifdef __GNUC__
251 #pragma GCC diagnostic pop
252 #endif
253 #ifdef _MSC_VER
254 #pragma warning(pop)
255 #endif
256