1 // Formatting library for C++ - tests of custom Google Test assertions
3 // Copyright (c) 2012 - present, Victor Zverovich
4 // All rights reserved.
6 // For the license information refer to format.h.
8 #include "gtest-extra.h"
10 #include <gtest/gtest-spi.h>
19 // Tests that assertion macros evaluate their arguments exactly once.
21 class single_evaluation_test
: public ::testing::Test
{
23 single_evaluation_test() {
29 static const char* const s_
;
30 static const char* p_
;
37 const char* const single_evaluation_test::s_
= "01234";
38 const char* single_evaluation_test::p_
;
39 int single_evaluation_test::a_
;
40 int single_evaluation_test::b_
;
44 FMT_NORETURN
void throw_exception() { throw std::runtime_error("test"); }
46 FMT_NORETURN
void throw_system_error() {
47 throw fmt::system_error(EDOM
, "test");
50 // Tests that when EXPECT_THROW_MSG fails, it evaluates its message argument
52 TEST_F(single_evaluation_test
, failed_expect_throw_msg
) {
53 EXPECT_NONFATAL_FAILURE(
54 EXPECT_THROW_MSG(throw_exception(), std::exception
, p_
++), "01234");
55 EXPECT_EQ(s_
+ 1, p_
);
58 // Tests that when EXPECT_SYSTEM_ERROR fails, it evaluates its message argument
60 TEST_F(single_evaluation_test
, failed_expect_system_error
) {
61 EXPECT_NONFATAL_FAILURE(EXPECT_SYSTEM_ERROR(throw_system_error(), EDOM
, p_
++),
63 EXPECT_EQ(s_
+ 1, p_
);
66 // Tests that assertion arguments are evaluated exactly once.
67 TEST_F(single_evaluation_test
, exception_tests
) {
68 // successful EXPECT_THROW_MSG
74 std::exception
, (b_
++, "test"));
78 // failed EXPECT_THROW_MSG, throws different type
79 EXPECT_NONFATAL_FAILURE(EXPECT_THROW_MSG(
84 std::logic_error
, (b_
++, "test")),
85 "throws a different type");
89 // failed EXPECT_THROW_MSG, throws an exception with different message
90 EXPECT_NONFATAL_FAILURE(EXPECT_THROW_MSG(
95 std::exception
, (b_
++, "other")),
96 "throws an exception with a different message");
100 // failed EXPECT_THROW_MSG, throws nothing
101 EXPECT_NONFATAL_FAILURE(
102 EXPECT_THROW_MSG(a_
++, std::exception
, (b_
++, "test")), "throws nothing");
107 TEST_F(single_evaluation_test
, system_error_tests
) {
108 // successful EXPECT_SYSTEM_ERROR
112 throw_system_error();
114 EDOM
, (b_
++, "test"));
118 // failed EXPECT_SYSTEM_ERROR, throws different type
119 EXPECT_NONFATAL_FAILURE(EXPECT_SYSTEM_ERROR(
124 EDOM
, (b_
++, "test")),
125 "throws a different type");
129 // failed EXPECT_SYSTEM_ERROR, throws an exception with different message
130 EXPECT_NONFATAL_FAILURE(EXPECT_SYSTEM_ERROR(
133 throw_system_error();
135 EDOM
, (b_
++, "other")),
136 "throws an exception with a different message");
140 // failed EXPECT_SYSTEM_ERROR, throws nothing
141 EXPECT_NONFATAL_FAILURE(EXPECT_SYSTEM_ERROR(a_
++, EDOM
, (b_
++, "test")),
148 // Tests that when EXPECT_WRITE fails, it evaluates its message argument
150 TEST_F(single_evaluation_test
, failed_expect_write
) {
151 EXPECT_NONFATAL_FAILURE(EXPECT_WRITE(stdout
, std::printf("test"), p_
++),
153 EXPECT_EQ(s_
+ 1, p_
);
156 // Tests that assertion arguments are evaluated exactly once.
157 TEST_F(single_evaluation_test
, write_tests
) {
158 // successful EXPECT_WRITE
169 // failed EXPECT_WRITE
170 EXPECT_NONFATAL_FAILURE(EXPECT_WRITE(
182 // Tests EXPECT_WRITE.
183 TEST(gtest_extra_test
, expect_write
) {
184 EXPECT_WRITE(stdout
, do_nothing(), "");
185 EXPECT_WRITE(stdout
, std::printf("test"), "test");
186 EXPECT_WRITE(stderr
, std::fprintf(stderr
, "test"), "test");
187 EXPECT_NONFATAL_FAILURE(EXPECT_WRITE(stdout
, std::printf("that"), "this"),
192 TEST(gtest_extra_test
, expect_write_streaming
) {
193 EXPECT_WRITE(stdout
, std::printf("test"), "test") << "unexpected failure";
194 EXPECT_NONFATAL_FAILURE(EXPECT_WRITE(stdout
, std::printf("test"), "other")
195 << "expected failure",
198 #endif // FMT_USE_FCNTL
200 // Tests that the compiler will not complain about unreachable code in the
201 // EXPECT_THROW_MSG macro.
202 TEST(gtest_extra_test
, expect_throw_no_unreachable_code_warning
) {
205 using std::runtime_error
;
206 EXPECT_THROW_MSG(throw runtime_error(""), runtime_error
, "");
207 EXPECT_NONFATAL_FAILURE(EXPECT_THROW_MSG(n
++, runtime_error
, ""), "");
208 EXPECT_NONFATAL_FAILURE(EXPECT_THROW_MSG(throw 1, runtime_error
, ""), "");
209 EXPECT_NONFATAL_FAILURE(
210 EXPECT_THROW_MSG(throw runtime_error("a"), runtime_error
, "b"), "");
213 // Tests that the compiler will not complain about unreachable code in the
214 // EXPECT_SYSTEM_ERROR macro.
215 TEST(gtest_extra_test
, expect_system_error_no_unreachable_code_warning
) {
218 EXPECT_SYSTEM_ERROR(throw fmt::system_error(EDOM
, "test"), EDOM
, "test");
219 EXPECT_NONFATAL_FAILURE(EXPECT_SYSTEM_ERROR(n
++, EDOM
, ""), "");
220 EXPECT_NONFATAL_FAILURE(EXPECT_SYSTEM_ERROR(throw 1, EDOM
, ""), "");
221 EXPECT_NONFATAL_FAILURE(
222 EXPECT_SYSTEM_ERROR(throw fmt::system_error(EDOM
, "aaa"), EDOM
, "bbb"),
226 TEST(gtest_extra_test
, expect_throw_behaves_like_single_statement
) {
227 if (::testing::internal::AlwaysFalse())
228 EXPECT_THROW_MSG(do_nothing(), std::exception
, "");
230 if (::testing::internal::AlwaysTrue())
231 EXPECT_THROW_MSG(throw_exception(), std::exception
, "test");
236 TEST(gtest_extra_test
, expect_system_error_behaves_like_single_statement
) {
237 if (::testing::internal::AlwaysFalse())
238 EXPECT_SYSTEM_ERROR(do_nothing(), EDOM
, "");
240 if (::testing::internal::AlwaysTrue())
241 EXPECT_SYSTEM_ERROR(throw_system_error(), EDOM
, "test");
246 TEST(gtest_extra_test
, expect_write_behaves_like_single_statement
) {
247 if (::testing::internal::AlwaysFalse())
248 EXPECT_WRITE(stdout
, std::printf("x"), "x");
250 if (::testing::internal::AlwaysTrue())
251 EXPECT_WRITE(stdout
, std::printf("x"), "x");
256 // Tests EXPECT_THROW_MSG.
257 TEST(gtest_extra_test
, expect_throw_msg
) {
258 EXPECT_THROW_MSG(throw_exception(), std::exception
, "test");
259 EXPECT_NONFATAL_FAILURE(
260 EXPECT_THROW_MSG(throw_exception(), std::logic_error
, "test"),
261 "Expected: throw_exception() throws an exception of "
262 "type std::logic_error.\n Actual: it throws a different type.");
263 EXPECT_NONFATAL_FAILURE(
264 EXPECT_THROW_MSG(do_nothing(), std::exception
, "test"),
265 "Expected: do_nothing() throws an exception of type std::exception.\n"
266 " Actual: it throws nothing.");
267 EXPECT_NONFATAL_FAILURE(
268 EXPECT_THROW_MSG(throw_exception(), std::exception
, "other"),
269 "throw_exception() throws an exception with a different message.\n"
274 // Tests EXPECT_SYSTEM_ERROR.
275 TEST(gtest_extra_test
, expect_system_error
) {
276 EXPECT_SYSTEM_ERROR(throw_system_error(), EDOM
, "test");
277 EXPECT_NONFATAL_FAILURE(
278 EXPECT_SYSTEM_ERROR(throw_exception(), EDOM
, "test"),
279 "Expected: throw_exception() throws an exception of "
280 "type std::system_error.\n Actual: it throws a different type.");
281 EXPECT_NONFATAL_FAILURE(
282 EXPECT_SYSTEM_ERROR(do_nothing(), EDOM
, "test"),
283 "Expected: do_nothing() throws an exception of type std::system_error.\n"
284 " Actual: it throws nothing.");
285 EXPECT_NONFATAL_FAILURE(
286 EXPECT_SYSTEM_ERROR(throw_system_error(), EDOM
, "other"),
288 "throw_system_error() throws an exception with a different message.\n"
291 system_error_message(EDOM
, "other"),
292 system_error_message(EDOM
, "test")));
295 TEST(gtest_extra_test
, expect_throw_msg_streaming
) {
296 EXPECT_THROW_MSG(throw_exception(), std::exception
, "test")
297 << "unexpected failure";
298 EXPECT_NONFATAL_FAILURE(
299 EXPECT_THROW_MSG(throw_exception(), std::exception
, "other")
300 << "expected failure",
304 TEST(gtest_extra_test
, expect_system_error_streaming
) {
305 EXPECT_SYSTEM_ERROR(throw_system_error(), EDOM
, "test")
306 << "unexpected failure";
307 EXPECT_NONFATAL_FAILURE(
308 EXPECT_SYSTEM_ERROR(throw_system_error(), EDOM
, "other")
309 << "expected failure",
315 using fmt::buffered_file
;
318 TEST(output_redirect_test
, scoped_redirect
) {
319 auto pipe
= fmt::pipe();
321 buffered_file
file(pipe
.write_end
.fdopen("w"));
322 std::fprintf(file
.get(), "[[[");
324 output_redirect
redir(file
.get());
325 std::fprintf(file
.get(), "censored");
327 std::fprintf(file
.get(), "]]]");
329 EXPECT_READ(pipe
.read_end
, "[[[]]]");
332 // Test that output_redirect handles errors in flush correctly.
333 TEST(output_redirect_test
, flush_error_in_ctor
) {
334 auto pipe
= fmt::pipe();
335 int write_fd
= pipe
.write_end
.descriptor();
336 file write_copy
= pipe
.write_end
.dup(write_fd
);
337 buffered_file f
= pipe
.write_end
.fdopen("w");
338 // Put a character in a file buffer.
339 EXPECT_EQ('x', fputc('x', f
.get()));
340 FMT_POSIX(close(write_fd
));
341 std::unique_ptr
<output_redirect
> redir
{nullptr};
342 EXPECT_SYSTEM_ERROR_NOASSERT(redir
.reset(new output_redirect(f
.get())), EBADF
,
343 "cannot flush stream");
344 redir
.reset(nullptr);
345 write_copy
.dup2(write_fd
); // "undo" close or dtor will fail
348 TEST(output_redirect_test
, dup_error_in_ctor
) {
349 buffered_file f
= open_buffered_file();
350 int fd
= (f
.descriptor
)();
351 file copy
= file::dup(fd
);
352 FMT_POSIX(close(fd
));
353 std::unique_ptr
<output_redirect
> redir
{nullptr};
354 EXPECT_SYSTEM_ERROR_NOASSERT(
355 redir
.reset(new output_redirect(f
.get(), false)), EBADF
,
356 fmt::format("cannot duplicate file descriptor {}", fd
));
357 copy
.dup2(fd
); // "undo" close or dtor will fail
360 TEST(output_redirect_test
, restore_and_read
) {
361 auto pipe
= fmt::pipe();
362 buffered_file
file(pipe
.write_end
.fdopen("w"));
363 std::fprintf(file
.get(), "[[[");
364 output_redirect
redir(file
.get());
365 std::fprintf(file
.get(), "censored");
366 EXPECT_EQ("censored", redir
.restore_and_read());
367 EXPECT_EQ("", redir
.restore_and_read());
368 std::fprintf(file
.get(), "]]]");
369 file
= buffered_file();
370 EXPECT_READ(pipe
.read_end
, "[[[]]]");
373 // Test that OutputRedirect handles errors in flush correctly.
374 TEST(output_redirect_test
, flush_error_in_restore_and_read
) {
375 auto pipe
= fmt::pipe();
376 int write_fd
= pipe
.write_end
.descriptor();
377 file write_copy
= pipe
.write_end
.dup(write_fd
);
378 buffered_file f
= pipe
.write_end
.fdopen("w");
379 output_redirect
redir(f
.get());
380 // Put a character in a file buffer.
381 EXPECT_EQ('x', fputc('x', f
.get()));
382 FMT_POSIX(close(write_fd
));
383 EXPECT_SYSTEM_ERROR_NOASSERT(redir
.restore_and_read(), EBADF
,
384 "cannot flush stream");
385 write_copy
.dup2(write_fd
); // "undo" close or dtor will fail
388 TEST(output_redirect_test
, error_in_dtor
) {
389 auto pipe
= fmt::pipe();
390 int write_fd
= pipe
.write_end
.descriptor();
391 file write_copy
= pipe
.write_end
.dup(write_fd
);
392 buffered_file f
= pipe
.write_end
.fdopen("w");
393 std::unique_ptr
<output_redirect
> redir(new output_redirect(f
.get()));
394 // Put a character in a file buffer.
395 EXPECT_EQ('x', fputc('x', f
.get()));
399 // The close function must be called inside EXPECT_WRITE,
400 // otherwise the system may recycle closed file descriptor when
401 // redirecting the output in EXPECT_STDERR and the second close
402 // will break output redirection.
403 FMT_POSIX(close(write_fd
));
404 SUPPRESS_ASSERT(redir
.reset(nullptr));
406 system_error_message(EBADF
, "cannot flush stream"));
407 write_copy
.dup2(write_fd
); // "undo" close or dtor of buffered_file will fail
410 #endif // FMT_USE_FCNTL