1 // Formatting library for C++ - 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 #ifndef FMT_GTEST_EXTRA_H_
9 #define FMT_GTEST_EXTRA_H_
16 #ifndef FMT_USE_FILE_DESCRIPTORS
17 # define FMT_USE_FILE_DESCRIPTORS 0
20 #if FMT_USE_FILE_DESCRIPTORS
21 # include "fmt/posix.h"
24 #define FMT_TEST_THROW_(statement, expected_exception, expected_message, fail) \
25 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
26 if (::testing::AssertionResult gtest_ar = ::testing::AssertionSuccess()) { \
27 std::string gtest_expected_message = expected_message; \
28 bool gtest_caught_expected = false; \
30 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
31 } catch (expected_exception const& e) { \
32 if (gtest_expected_message != e.what()) { \
33 gtest_ar << #statement \
34 " throws an exception with a different message.\n" \
35 << "Expected: " << gtest_expected_message << "\n" \
36 << " Actual: " << e.what(); \
37 goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
39 gtest_caught_expected = true; \
41 gtest_ar << "Expected: " #statement \
42 " throws an exception of type " #expected_exception \
43 ".\n Actual: it throws a different type."; \
44 goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
46 if (!gtest_caught_expected) { \
47 gtest_ar << "Expected: " #statement \
48 " throws an exception of type " #expected_exception \
49 ".\n Actual: it throws nothing."; \
50 goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
53 GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__) \
54 : fail(gtest_ar.failure_message())
56 // Tests that the statement throws the expected exception and the exception's
57 // what() method returns expected message.
58 #define EXPECT_THROW_MSG(statement, expected_exception, expected_message) \
59 FMT_TEST_THROW_(statement, expected_exception, expected_message, \
60 GTEST_NONFATAL_FAILURE_)
62 std::string
format_system_error(int error_code
, fmt::string_view message
);
64 #define EXPECT_SYSTEM_ERROR(statement, error_code, message) \
65 EXPECT_THROW_MSG(statement, fmt::system_error, \
66 format_system_error(error_code, message))
68 #if FMT_USE_FILE_DESCRIPTORS
70 // Captures file output by redirecting it to a pipe.
71 // The output it can handle is limited by the pipe capacity.
72 class OutputRedirect
{
75 fmt::file original_
; // Original file passed to redirector.
76 fmt::file read_end_
; // Read end of the pipe where the output is redirected.
78 GTEST_DISALLOW_COPY_AND_ASSIGN_(OutputRedirect
);
84 explicit OutputRedirect(FILE* file
);
85 ~OutputRedirect() FMT_NOEXCEPT
;
87 // Restores the original file, reads output from the pipe into a string
89 std::string
restore_and_read();
92 # define FMT_TEST_WRITE_(statement, expected_output, file, fail) \
93 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
94 if (::testing::AssertionResult gtest_ar = ::testing::AssertionSuccess()) { \
95 std::string gtest_expected_output = expected_output; \
96 OutputRedirect gtest_redir(file); \
97 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
98 std::string gtest_output = gtest_redir.restore_and_read(); \
99 if (gtest_output != gtest_expected_output) { \
100 gtest_ar << #statement " produces different output.\n" \
101 << "Expected: " << gtest_expected_output << "\n" \
102 << " Actual: " << gtest_output; \
103 goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
106 GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__) \
107 : fail(gtest_ar.failure_message())
109 // Tests that the statement writes the expected output to file.
110 # define EXPECT_WRITE(file, statement, expected_output) \
111 FMT_TEST_WRITE_(statement, expected_output, file, GTEST_NONFATAL_FAILURE_)
115 // Suppresses Windows assertions on invalid file descriptors, making
116 // POSIX functions return proper error codes instead of crashing on Windows.
117 class SuppressAssert
{
119 _invalid_parameter_handler original_handler_
;
120 int original_report_mode_
;
122 static void handle_invalid_parameter(const wchar_t*, const wchar_t*,
123 const wchar_t*, unsigned, uintptr_t) {}
128 _set_invalid_parameter_handler(handle_invalid_parameter
)),
129 original_report_mode_(_CrtSetReportMode(_CRT_ASSERT
, 0)) {}
131 _set_invalid_parameter_handler(original_handler_
);
132 _CrtSetReportMode(_CRT_ASSERT
, original_report_mode_
);
136 # define SUPPRESS_ASSERT(statement) \
142 # define SUPPRESS_ASSERT(statement) statement
145 # define EXPECT_SYSTEM_ERROR_NOASSERT(statement, error_code, message) \
146 EXPECT_SYSTEM_ERROR(SUPPRESS_ASSERT(statement), error_code, message)
148 // Attempts to read count characters from a file.
149 std::string
read(fmt::file
& f
, std::size_t count
);
151 # define EXPECT_READ(file, expected_content) \
152 EXPECT_EQ(expected_content, read(file, std::strlen(expected_content)))
154 #endif // FMT_USE_FILE_DESCRIPTORS
156 template <typename Mock
> struct ScopedMock
: testing::StrictMock
<Mock
> {
157 ScopedMock() { Mock::instance
= this; }
158 ~ScopedMock() { Mock::instance
= FMT_NULL
; }
161 #endif // FMT_GTEST_EXTRA_H_