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); \
32 catch (expected_exception const& e) { \
33 if (gtest_expected_message != e.what()) { \
35 << #statement " throws an exception with a different message.\n" \
36 << "Expected: " << gtest_expected_message << "\n" \
37 << " Actual: " << e.what(); \
38 goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
40 gtest_caught_expected = true; \
44 "Expected: " #statement " throws an exception of type " \
45 #expected_exception ".\n Actual: it throws a different type."; \
46 goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
48 if (!gtest_caught_expected) { \
50 "Expected: " #statement " throws an exception of type " \
51 #expected_exception ".\n Actual: it throws nothing."; \
52 goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
55 GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__): \
56 fail(gtest_ar.failure_message())
58 // Tests that the statement throws the expected exception and the exception's
59 // what() method returns expected message.
60 #define EXPECT_THROW_MSG(statement, expected_exception, expected_message) \
61 FMT_TEST_THROW_(statement, expected_exception, \
62 expected_message, GTEST_NONFATAL_FAILURE_)
64 std::string
format_system_error(int error_code
, fmt::string_view message
);
66 #define EXPECT_SYSTEM_ERROR(statement, error_code, message) \
67 EXPECT_THROW_MSG(statement, fmt::system_error, \
68 format_system_error(error_code, message))
70 #if FMT_USE_FILE_DESCRIPTORS
72 // Captures file output by redirecting it to a pipe.
73 // The output it can handle is limited by the pipe capacity.
74 class OutputRedirect
{
77 fmt::file original_
; // Original file passed to redirector.
78 fmt::file read_end_
; // Read end of the pipe where the output is redirected.
80 GTEST_DISALLOW_COPY_AND_ASSIGN_(OutputRedirect
);
86 explicit OutputRedirect(FILE *file
);
87 ~OutputRedirect() FMT_NOEXCEPT
;
89 // Restores the original file, reads output from the pipe into a string
91 std::string
restore_and_read();
94 #define FMT_TEST_WRITE_(statement, expected_output, file, fail) \
95 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
96 if (::testing::AssertionResult gtest_ar = ::testing::AssertionSuccess()) { \
97 std::string gtest_expected_output = expected_output; \
98 OutputRedirect gtest_redir(file); \
99 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
100 std::string gtest_output = gtest_redir.restore_and_read(); \
101 if (gtest_output != gtest_expected_output) { \
103 << #statement " produces different output.\n" \
104 << "Expected: " << gtest_expected_output << "\n" \
105 << " Actual: " << gtest_output; \
106 goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
109 GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__): \
110 fail(gtest_ar.failure_message())
112 // Tests that the statement writes the expected output to file.
113 #define EXPECT_WRITE(file, statement, expected_output) \
114 FMT_TEST_WRITE_(statement, expected_output, file, GTEST_NONFATAL_FAILURE_)
118 // Suppresses Windows assertions on invalid file descriptors, making
119 // POSIX functions return proper error codes instead of crashing on Windows.
120 class SuppressAssert
{
122 _invalid_parameter_handler original_handler_
;
123 int original_report_mode_
;
125 static void handle_invalid_parameter(const wchar_t *,
126 const wchar_t *, const wchar_t *, unsigned , uintptr_t) {}
130 : original_handler_(_set_invalid_parameter_handler(handle_invalid_parameter
)),
131 original_report_mode_(_CrtSetReportMode(_CRT_ASSERT
, 0)) {
134 _set_invalid_parameter_handler(original_handler_
);
135 _CrtSetReportMode(_CRT_ASSERT
, original_report_mode_
);
139 # define SUPPRESS_ASSERT(statement) { SuppressAssert sa; statement; }
141 # define SUPPRESS_ASSERT(statement) statement
144 #define EXPECT_SYSTEM_ERROR_NOASSERT(statement, error_code, message) \
145 EXPECT_SYSTEM_ERROR(SUPPRESS_ASSERT(statement), error_code, message)
147 // Attempts to read count characters from a file.
148 std::string
read(fmt::file
&f
, std::size_t count
);
150 #define EXPECT_READ(file, expected_content) \
151 EXPECT_EQ(expected_content, read(file, std::strlen(expected_content)))
153 #endif // FMT_USE_FILE_DESCRIPTORS
155 template <typename Mock
>
156 struct ScopedMock
: testing::StrictMock
<Mock
> {
157 ScopedMock() { Mock::instance
= this; }
158 ~ScopedMock() { Mock::instance
= FMT_NULL
; }
161 #endif // FMT_GTEST_EXTRA_H_