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