1 // Copyright 2021 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef NET_THIRD_PARTY_QUICHE_OVERRIDES_QUICHE_PLATFORM_IMPL_QUICHE_TEST_HELPERS_IMPL_H_
6 #define NET_THIRD_PARTY_QUICHE_OVERRIDES_QUICHE_PLATFORM_IMPL_QUICHE_TEST_HELPERS_IMPL_H_
7
8 #include <iosfwd>
9 #include <sstream>
10
11 #include "net/test/gtest_util.h"
12 #include "testing/gmock/include/gmock/gmock-matchers.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 #define EXPECT_QUICHE_BUG_IMPL EXPECT_DFATAL
17
18 // Defines VERIFY_* macros, analogous to gUnit's EXPECT_* and ASSERT_* macros,
19 // but these return an appropriate AssertionResult if the condition is not
20 // satisfied. This enables one to create a function for verifying expectations
21 // that are needed by multiple callers or that rely on arguments not accessible
22 // to the main test method. Using VERIFY_SUCCESS allows one to annotate the
23 // a failing AssertionResult with more context.
24
25 namespace http2::test {
26
27 template <typename T>
28 class VerifyThatHelper {
29 public:
VerifyThatHelper(const T & value,::testing::Matcher<T> matcher)30 VerifyThatHelper(const T& value, ::testing::Matcher<T> matcher) {
31 matches_ = matcher.Matches(value);
32 if (!matches_) {
33 printed_value_ = ::testing::PrintToString(value);
34
35 std::ostringstream os;
36 matcher.DescribeTo(&os);
37 matcher_description_ = os.str();
38 }
39 }
40
41 VerifyThatHelper(const VerifyThatHelper&) = delete;
42 VerifyThatHelper& operator=(const VerifyThatHelper&) = delete;
43
44 explicit operator bool() const { return matches_; }
45
printed_value()46 const std::string& printed_value() const { return printed_value_; }
matcher_description()47 const std::string& matcher_description() const {
48 return matcher_description_;
49 }
50
51 private:
52 bool matches_;
53 std::string printed_value_;
54 std::string matcher_description_;
55 };
56
57 // Constructs a failure message for Boolean assertions such as VERIFY_TRUE.
58 std::string GetBoolAssertionFailureMessage(
59 const ::testing::AssertionResult& assertion_result,
60 const char* expression_text,
61 const char* actual_predicate_value,
62 const char* expected_predicate_value);
63
64 namespace {
65 // Define HasSubstr() for Http2StringPiece arguments.
66 // This shadows ::testing::HasSubstr(), which only works on argument types
67 // that can be implicitly converted to a std::string.
68 inline ::testing::PolymorphicMatcher<net::test::StringPieceHasSubstrMatcher>
HasSubstr(const std::string & substring)69 HasSubstr(const std::string& substring) {
70 return ::testing::MakePolymorphicMatcher(
71 net::test::StringPieceHasSubstrMatcher(substring));
72 }
73 } // namespace
74
75 } // namespace http2::test
76
77 // Macro for adding verification location to output stream or AssertionResult.
78 // Starts with a new-line because of the way that gUnit displays failures for
79 // EXPECT_TRUE(CallToFunctionThatFailsToVerify()).
80 #define VERIFY_FAILED_LOCATION_ \
81 "\n" \
82 << "(VERIFY failed in " << __func__ << "\n" \
83 << " at " __FILE__ " : " << __LINE__ << ")\n"
84
85 // Implements Boolean test verifications VERIFY_TRUE and VERIFY_FALSE.
86 // text is a textual representation of expression as it was passed into
87 // VERIFY_TRUE or VERIFY_FALSE.
88 // clang-format off
89 #define VERIFY_TEST_BOOLEAN_(condition, text, actual, expected) \
90 if (const ::testing::AssertionResult __assertion_result = \
91 ::testing::AssertionResult((condition) ? expected : actual)) \
92 ; \
93 else \
94 return ::testing::AssertionFailure() \
95 << VERIFY_FAILED_LOCATION_ \
96 << ::http2::test::GetBoolAssertionFailureMessage( \
97 __assertion_result, text, #actual, #expected)
98 // clang-format on
99
100 // Boolean assertions. condition can be either a Boolean expression or an
101 // expression convertible to a boolean (such as a ::gtl::labs::optional).
102 #define VERIFY_TRUE(condition) \
103 VERIFY_TEST_BOOLEAN_(condition, #condition, false, true)
104
105 #define VERIFY_FALSE(condition) \
106 VERIFY_TEST_BOOLEAN_(condition, #condition, true, false)
107
108 // Convenient helper macro for writing methods that return an AssertionFailure
109 // that includes the tested condition in the message (in the manner of
110 // ASSERT_THAT and EXPECT_THAT).
111 //
112 // This macro avoids the do {} while(false) trick and putting braces around
113 // the if so you can do things like:
114 // VERIFY_THAT(foo, Lt(4)) << "foo too big in iteration " << i;
115 // (This parallels the implementation of CHECK().)
116 //
117 // We use an if statement with an empty true branch so that this doesn't eat
118 // a neighboring else when used in an unbraced if statement like:
119 // if (condition)
120 // VERIFY_THAT(foo, Eq(bar));
121 // else
122 // FAIL();
123 #define VERIFY_THAT(value, matcher) \
124 if (const auto& _verify_that_helper = \
125 ::http2::test::VerifyThatHelper<decltype(value)>(value, matcher)) \
126 ; \
127 else \
128 return ::testing::AssertionFailure() \
129 << "Failed to verify that '" #value "' (" \
130 << _verify_that_helper.printed_value() << ") " \
131 << _verify_that_helper.matcher_description() \
132 << " (on " __FILE__ ":" << __LINE__ << "). "
133
134 // Useful variants of VERIFY_THAT, similar to the corresponding EXPECT_X or
135 // ASSERT_X defined by gUnit.
136 #define VERIFY_EQ(val1, val2) VERIFY_THAT(val1, ::testing::Eq(val2))
137 #define VERIFY_NE(val1, val2) VERIFY_THAT(val1, ::testing::Ne(val2))
138 #define VERIFY_GT(val1, val2) VERIFY_THAT(val1, ::testing::Gt(val2))
139 #define VERIFY_LT(val1, val2) VERIFY_THAT(val1, ::testing::Lt(val2))
140 #define VERIFY_GE(val1, val2) VERIFY_THAT(val1, ::testing::Ge(val2))
141 #define VERIFY_LE(val1, val2) VERIFY_THAT(val1, ::testing::Le(val2))
142
143 // Convenience macro matching EXPECT_OK
144 #define VERIFY_OK(statement) VERIFY_EQ(::util::Status::OK, (statement))
145
146 // This version verifies that an expression of type AssertionResult is
147 // AssertionSuccess. If instead the value is an AssertionFailure, it appends
148 // info about the current code location to the failure's message and returns
149 // the failure to the caller of the current method. It permits the code site
150 // to append further messages to the failure message. For example:
151 // VERIFY_SUCCESS(SomeCall()) << "Some more context about SomeCall";
152 // clang-format off
153 #define VERIFY_SUCCESS(expr) \
154 if (::testing::AssertionResult __assertion_result = (expr)) \
155 ; \
156 else \
157 return __assertion_result << VERIFY_FAILED_LOCATION_
158 // clang-format on
159
160 #define VERIFY_AND_RETURN_SUCCESS(expression) \
161 { \
162 VERIFY_SUCCESS(expression); \
163 return ::testing::AssertionSuccess(); \
164 }
165
166 #endif // NET_THIRD_PARTY_QUICHE_OVERRIDES_QUICHE_PLATFORM_IMPL_QUICHE_TEST_HELPERS_IMPL_H_
167