Debian Codegraph @

SDL_audiolib/3rdparty/fmt/test/gtest-extra.h

raw symbols SCIP
1 // Formatting library for C++ - custom Google Test assertions
2 //
3 // Copyright (c) 2012 - present, Victor Zverovich
4 // All rights reserved.
5 //
6 // For the license information refer to format.h.
7
8 #ifndef FMT_GTEST_EXTRA_H_
9 #define FMT_GTEST_EXTRA_H_
10
11 #include <stdlib.h> // _invalid_parameter_handler
12
13 #include <string>
14
15 #ifdef FMT_MODULE_TEST
16 import fmt;
17 #else
18 # include "fmt/os.h"
19 #endif // FMG_MODULE_TEST
20
21 #include "gmock/gmock.h"
22
23 #define FMT_TEST_THROW_(statement, expected_exception, expected_message, fail) \
24 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
25 if (::testing::AssertionResult gtest_ar = ::testing::AssertionSuccess()) { \
26 std::string gtest_expected_message = expected_message; \
27 bool gtest_caught_expected = false; \
28 try { \
29 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
30 } catch (expected_exception const& e) { \
31 if (gtest_expected_message != e.what()) { \
32 gtest_ar << #statement \
33 " throws an exception with a different message.\n" \
34 << "Expected: " << gtest_expected_message << "\n" \
35 << " Actual: " << e.what(); \
36 goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
37 } \
38 gtest_caught_expected = true; \
39 } catch (...) { \
40 gtest_ar << "Expected: " #statement \
41 " throws an exception of type " #expected_exception \
42 ".\n Actual: it throws a different type."; \
43 goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
44 } \
45 if (!gtest_caught_expected) { \
46 gtest_ar << "Expected: " #statement \
47 " throws an exception of type " #expected_exception \
48 ".\n Actual: it throws nothing."; \
49 goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
50 } \
51 } else \
52 GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__) \
53 : fail(gtest_ar.failure_message())
54
55 // Tests that the statement throws the expected exception and the exception's
56 // what() method returns expected message.
57 #define EXPECT_THROW_MSG(statement, expected_exception, expected_message) \
58 FMT_TEST_THROW_(statement, expected_exception, expected_message, \
59 GTEST_NONFATAL_FAILURE_)
60
61 inline std::string system_error_message(int error_code,
62 const std::string& message) {
63 auto ec = std::error_code(error_code, std::generic_category());
64 return std::system_error(ec, message).what();
65 }
66
67 #define EXPECT_SYSTEM_ERROR(statement, error_code, message) \
68 EXPECT_THROW_MSG(statement, std::system_error, \
69 system_error_message(error_code, message))
70
71 #if FMT_USE_FCNTL
72
73 // Captures file output by redirecting it to a pipe.
74 // The output it can handle is limited by the pipe capacity.
75 class output_redirect {
76 private:
77 FILE* file_;
78 fmt::file original_; // Original file passed to redirector.
79 fmt::file read_end_; // Read end of the pipe where the output is redirected.
80
81 void flush();
82 void restore();
83
84 public:
85 explicit output_redirect(FILE* file);
86 ~output_redirect() FMT_NOEXCEPT;
87
88 output_redirect(const output_redirect&) = delete;
89 void operator=(const output_redirect&) = delete;
90
91 // Restores the original file, reads output from the pipe into a string
92 // and returns it.
93 std::string restore_and_read();
94 };
95
96 # define FMT_TEST_WRITE_(statement, expected_output, file, fail) \
97 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
98 if (::testing::AssertionResult gtest_ar = ::testing::AssertionSuccess()) { \
99 std::string gtest_expected_output = expected_output; \
100 output_redirect gtest_redir(file); \
101 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
102 std::string gtest_output = gtest_redir.restore_and_read(); \
103 if (gtest_output != gtest_expected_output) { \
104 gtest_ar << #statement " produces different output.\n" \
105 << "Expected: " << gtest_expected_output << "\n" \
106 << " Actual: " << gtest_output; \
107 goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
108 } \
109 } else \
110 GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__) \
111 : fail(gtest_ar.failure_message())
112
113 // Tests that the statement writes the expected output to file.
114 # define EXPECT_WRITE(file, statement, expected_output) \
115 FMT_TEST_WRITE_(statement, expected_output, file, GTEST_NONFATAL_FAILURE_)
116
117 # ifdef _MSC_VER
118
119 // Suppresses Windows assertions on invalid file descriptors, making
120 // POSIX functions return proper error codes instead of crashing on Windows.
121 class suppress_assert {
122 private:
123 _invalid_parameter_handler original_handler_;
124 int original_report_mode_;
125
126 static void handle_invalid_parameter(const wchar_t*, const wchar_t*,
127 const wchar_t*, unsigned, uintptr_t) {}
128
129 public:
130 suppress_assert()
131 : original_handler_(
132 _set_invalid_parameter_handler(handle_invalid_parameter)),
133 original_report_mode_(_CrtSetReportMode(_CRT_ASSERT, 0)) {}
134 ~suppress_assert() {
135 _set_invalid_parameter_handler(original_handler_);
136 _CrtSetReportMode(_CRT_ASSERT, original_report_mode_);
137 (void)original_report_mode_;
138 }
139 };
140
141 # define SUPPRESS_ASSERT(statement) \
142 { \
143 suppress_assert sa; \
144 statement; \
145 }
146 # else
147 # define SUPPRESS_ASSERT(statement) statement
148 # endif // _MSC_VER
149
150 # define EXPECT_SYSTEM_ERROR_NOASSERT(statement, error_code, message) \
151 EXPECT_SYSTEM_ERROR(SUPPRESS_ASSERT(statement), error_code, message)
152
153 // Attempts to read count characters from a file.
154 std::string read(fmt::file& f, size_t count);
155
156 # define EXPECT_READ(file, expected_content) \
157 EXPECT_EQ(expected_content, \
158 read(file, fmt::string_view(expected_content).size()))
159
160 #else
161 # define EXPECT_WRITE(file, statement, expected_output) \
162 do { \
163 (void)(file); \
164 (void)(statement); \
165 (void)(expected_output); \
166 SUCCEED(); \
167 } while (false)
168 #endif // FMT_USE_FCNTL
169
170 #endif // FMT_GTEST_EXTRA_H_