]> git.proxmox.com Git - ceph.git/blob - ceph/src/fmt/test/gtest-extra.h
import 15.2.0 Octopus source
[ceph.git] / ceph / src / fmt / test / gtest-extra.h
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 <string>
12 #include "gmock.h"
13
14 #include "fmt/core.h"
15
16 #ifndef FMT_USE_FILE_DESCRIPTORS
17 # define FMT_USE_FILE_DESCRIPTORS 0
18 #endif
19
20 #if FMT_USE_FILE_DESCRIPTORS
21 # include "fmt/posix.h"
22 #endif
23
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; \
29 try { \
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__); \
38 } \
39 gtest_caught_expected = true; \
40 } catch (...) { \
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__); \
45 } \
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__); \
51 } \
52 } else \
53 GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__) \
54 : fail(gtest_ar.failure_message())
55
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_)
61
62 std::string format_system_error(int error_code, fmt::string_view message);
63
64 #define EXPECT_SYSTEM_ERROR(statement, error_code, message) \
65 EXPECT_THROW_MSG(statement, fmt::system_error, \
66 format_system_error(error_code, message))
67
68 #if FMT_USE_FILE_DESCRIPTORS
69
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 {
73 private:
74 FILE* file_;
75 fmt::file original_; // Original file passed to redirector.
76 fmt::file read_end_; // Read end of the pipe where the output is redirected.
77
78 GTEST_DISALLOW_COPY_AND_ASSIGN_(OutputRedirect);
79
80 void flush();
81 void restore();
82
83 public:
84 explicit OutputRedirect(FILE* file);
85 ~OutputRedirect() FMT_NOEXCEPT;
86
87 // Restores the original file, reads output from the pipe into a string
88 // and returns it.
89 std::string restore_and_read();
90 };
91
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__); \
104 } \
105 } else \
106 GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__) \
107 : fail(gtest_ar.failure_message())
108
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_)
112
113 # ifdef _MSC_VER
114
115 // Suppresses Windows assertions on invalid file descriptors, making
116 // POSIX functions return proper error codes instead of crashing on Windows.
117 class SuppressAssert {
118 private:
119 _invalid_parameter_handler original_handler_;
120 int original_report_mode_;
121
122 static void handle_invalid_parameter(const wchar_t*, const wchar_t*,
123 const wchar_t*, unsigned, uintptr_t) {}
124
125 public:
126 SuppressAssert()
127 : original_handler_(
128 _set_invalid_parameter_handler(handle_invalid_parameter)),
129 original_report_mode_(_CrtSetReportMode(_CRT_ASSERT, 0)) {}
130 ~SuppressAssert() {
131 _set_invalid_parameter_handler(original_handler_);
132 _CrtSetReportMode(_CRT_ASSERT, original_report_mode_);
133 }
134 };
135
136 # define SUPPRESS_ASSERT(statement) \
137 { \
138 SuppressAssert sa; \
139 statement; \
140 }
141 # else
142 # define SUPPRESS_ASSERT(statement) statement
143 # endif // _MSC_VER
144
145 # define EXPECT_SYSTEM_ERROR_NOASSERT(statement, error_code, message) \
146 EXPECT_SYSTEM_ERROR(SUPPRESS_ASSERT(statement), error_code, message)
147
148 // Attempts to read count characters from a file.
149 std::string read(fmt::file& f, std::size_t count);
150
151 # define EXPECT_READ(file, expected_content) \
152 EXPECT_EQ(expected_content, read(file, std::strlen(expected_content)))
153
154 #endif // FMT_USE_FILE_DESCRIPTORS
155
156 template <typename Mock> struct ScopedMock : testing::StrictMock<Mock> {
157 ScopedMock() { Mock::instance = this; }
158 ~ScopedMock() { Mock::instance = FMT_NULL; }
159 };
160
161 #endif // FMT_GTEST_EXTRA_H_