]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/fmt/test/gtest-extra.h
update download target update for octopus release
[ceph.git] / ceph / src / seastar / 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 } \
32 catch (expected_exception const& e) { \
33 if (gtest_expected_message != e.what()) { \
34 gtest_ar \
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__); \
39 } \
40 gtest_caught_expected = true; \
41 } \
42 catch (...) { \
43 gtest_ar << \
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__); \
47 } \
48 if (!gtest_caught_expected) { \
49 gtest_ar << \
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__); \
53 } \
54 } else \
55 GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__): \
56 fail(gtest_ar.failure_message())
57
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_)
63
64 std::string format_system_error(int error_code, fmt::string_view message);
65
66 #define EXPECT_SYSTEM_ERROR(statement, error_code, message) \
67 EXPECT_THROW_MSG(statement, fmt::system_error, \
68 format_system_error(error_code, message))
69
70 #if FMT_USE_FILE_DESCRIPTORS
71
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 {
75 private:
76 FILE *file_;
77 fmt::file original_; // Original file passed to redirector.
78 fmt::file read_end_; // Read end of the pipe where the output is redirected.
79
80 GTEST_DISALLOW_COPY_AND_ASSIGN_(OutputRedirect);
81
82 void flush();
83 void restore();
84
85 public:
86 explicit OutputRedirect(FILE *file);
87 ~OutputRedirect() FMT_NOEXCEPT;
88
89 // Restores the original file, reads output from the pipe into a string
90 // and returns it.
91 std::string restore_and_read();
92 };
93
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) { \
102 gtest_ar \
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__); \
107 } \
108 } else \
109 GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__): \
110 fail(gtest_ar.failure_message())
111
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_)
115
116 #ifdef _MSC_VER
117
118 // Suppresses Windows assertions on invalid file descriptors, making
119 // POSIX functions return proper error codes instead of crashing on Windows.
120 class SuppressAssert {
121 private:
122 _invalid_parameter_handler original_handler_;
123 int original_report_mode_;
124
125 static void handle_invalid_parameter(const wchar_t *,
126 const wchar_t *, const wchar_t *, unsigned , uintptr_t) {}
127
128 public:
129 SuppressAssert()
130 : original_handler_(_set_invalid_parameter_handler(handle_invalid_parameter)),
131 original_report_mode_(_CrtSetReportMode(_CRT_ASSERT, 0)) {
132 }
133 ~SuppressAssert() {
134 _set_invalid_parameter_handler(original_handler_);
135 _CrtSetReportMode(_CRT_ASSERT, original_report_mode_);
136 }
137 };
138
139 # define SUPPRESS_ASSERT(statement) { SuppressAssert sa; statement; }
140 #else
141 # define SUPPRESS_ASSERT(statement) statement
142 #endif // _MSC_VER
143
144 #define EXPECT_SYSTEM_ERROR_NOASSERT(statement, error_code, message) \
145 EXPECT_SYSTEM_ERROR(SUPPRESS_ASSERT(statement), error_code, message)
146
147 // Attempts to read count characters from a file.
148 std::string read(fmt::file &f, std::size_t count);
149
150 #define EXPECT_READ(file, expected_content) \
151 EXPECT_EQ(expected_content, read(file, std::strlen(expected_content)))
152
153 #endif // FMT_USE_FILE_DESCRIPTORS
154
155 template <typename Mock>
156 struct ScopedMock : testing::StrictMock<Mock> {
157 ScopedMock() { Mock::instance = this; }
158 ~ScopedMock() { Mock::instance = FMT_NULL; }
159 };
160
161 #endif // FMT_GTEST_EXTRA_H_