summaryrefslogtreecommitdiff
path: root/chromium/net/third_party/quiche/overrides/quiche_platform_impl/quiche_test_helpers_impl.h
blob: 0433ee6547ccc9b4e867219c8875b0fe6238ebd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_THIRD_PARTY_QUICHE_OVERRIDES_QUICHE_PLATFORM_IMPL_QUICHE_TEST_HELPERS_IMPL_H_
#define NET_THIRD_PARTY_QUICHE_OVERRIDES_QUICHE_PLATFORM_IMPL_QUICHE_TEST_HELPERS_IMPL_H_

#include <iosfwd>
#include <sstream>

#include "net/test/gtest_util.h"
#include "testing/gmock/include/gmock/gmock-matchers.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

#define EXPECT_QUICHE_BUG_IMPL EXPECT_DFATAL

// Defines VERIFY_* macros, analogous to gUnit's EXPECT_* and ASSERT_* macros,
// but these return an appropriate AssertionResult if the condition is not
// satisfied. This enables one to create a function for verifying expectations
// that are needed by multiple callers or that rely on arguments not accessible
// to the main test method. Using VERIFY_SUCCESS allows one to annotate the
// a failing AssertionResult with more context.

namespace http2::test {

template <typename T>
class VerifyThatHelper {
 public:
  VerifyThatHelper(const T& value, ::testing::Matcher<T> matcher) {
    matches_ = matcher.Matches(value);
    if (!matches_) {
      printed_value_ = ::testing::PrintToString(value);

      std::ostringstream os;
      matcher.DescribeTo(&os);
      matcher_description_ = os.str();
    }
  }

  VerifyThatHelper(const VerifyThatHelper&) = delete;
  VerifyThatHelper& operator=(const VerifyThatHelper&) = delete;

  explicit operator bool() const { return matches_; }

  const std::string& printed_value() const { return printed_value_; }
  const std::string& matcher_description() const {
    return matcher_description_;
  }

 private:
  bool matches_;
  std::string printed_value_;
  std::string matcher_description_;
};

// Constructs a failure message for Boolean assertions such as VERIFY_TRUE.
std::string GetBoolAssertionFailureMessage(
    const ::testing::AssertionResult& assertion_result,
    const char* expression_text,
    const char* actual_predicate_value,
    const char* expected_predicate_value);

namespace {
// Define HasSubstr() for Http2StringPiece arguments.
// This shadows ::testing::HasSubstr(), which only works on argument types
// that can be implicitly converted to a std::string.
inline ::testing::PolymorphicMatcher<net::test::StringPieceHasSubstrMatcher>
HasSubstr(const std::string& substring) {
  return ::testing::MakePolymorphicMatcher(
      net::test::StringPieceHasSubstrMatcher(substring));
}
}  // namespace

}  // namespace http2::test

// Macro for adding verification location to output stream or AssertionResult.
// Starts with a new-line because of the way that gUnit displays failures for
//    EXPECT_TRUE(CallToFunctionThatFailsToVerify()).
#define VERIFY_FAILED_LOCATION_                   \
  "\n"                                            \
      << "(VERIFY failed in " << __func__ << "\n" \
      << "               at " __FILE__ " : " << __LINE__ << ")\n"

// Implements Boolean test verifications VERIFY_TRUE and VERIFY_FALSE.
// text is a textual representation of expression as it was passed into
// VERIFY_TRUE or VERIFY_FALSE.
// clang-format off
#define VERIFY_TEST_BOOLEAN_(condition, text, actual, expected)        \
  if (const ::testing::AssertionResult __assertion_result =            \
          ::testing::AssertionResult((condition) ? expected : actual)) \
    ;                                                                  \
  else                                                                 \
    return ::testing::AssertionFailure()                               \
         << VERIFY_FAILED_LOCATION_                                    \
         << ::http2::test::GetBoolAssertionFailureMessage(             \
                __assertion_result, text, #actual, #expected)
// clang-format on

// Boolean assertions. condition can be either a Boolean expression or an
// expression convertible to a boolean (such as a ::gtl::labs::optional).
#define VERIFY_TRUE(condition) \
  VERIFY_TEST_BOOLEAN_(condition, #condition, false, true)

#define VERIFY_FALSE(condition) \
  VERIFY_TEST_BOOLEAN_(condition, #condition, true, false)

// Convenient helper macro for writing methods that return an AssertionFailure
// that includes the tested condition in the message (in the manner of
// ASSERT_THAT and EXPECT_THAT).
//
// This macro avoids the do {} while(false) trick and putting braces around
// the if so you can do things like:
// VERIFY_THAT(foo, Lt(4)) << "foo too big in iteration " << i;
// (This parallels the implementation of CHECK().)
//
// We use an if statement with an empty true branch so that this doesn't eat
// a neighboring else when used in an unbraced if statement like:
// if (condition)
//   VERIFY_THAT(foo, Eq(bar));
// else
//   FAIL();
#define VERIFY_THAT(value, matcher)                                         \
  if (const auto& _verify_that_helper =                                     \
          ::http2::test::VerifyThatHelper<decltype(value)>(value, matcher)) \
    ;                                                                       \
  else                                                                      \
    return ::testing::AssertionFailure()                                    \
           << "Failed to verify that '" #value "' ("                        \
           << _verify_that_helper.printed_value() << ") "                   \
           << _verify_that_helper.matcher_description()                     \
           << " (on " __FILE__ ":" << __LINE__ << "). "

// Useful variants of VERIFY_THAT, similar to the corresponding EXPECT_X or
// ASSERT_X defined by gUnit.
#define VERIFY_EQ(val1, val2) VERIFY_THAT(val1, ::testing::Eq(val2))
#define VERIFY_NE(val1, val2) VERIFY_THAT(val1, ::testing::Ne(val2))
#define VERIFY_GT(val1, val2) VERIFY_THAT(val1, ::testing::Gt(val2))
#define VERIFY_LT(val1, val2) VERIFY_THAT(val1, ::testing::Lt(val2))
#define VERIFY_GE(val1, val2) VERIFY_THAT(val1, ::testing::Ge(val2))
#define VERIFY_LE(val1, val2) VERIFY_THAT(val1, ::testing::Le(val2))

// Convenience macro matching EXPECT_OK
#define VERIFY_OK(statement) VERIFY_EQ(::util::Status::OK, (statement))

// This version verifies that an expression of type AssertionResult is
// AssertionSuccess. If instead the value is an AssertionFailure, it appends
// info about the current code location to the failure's message and returns
// the failure to the caller of the current method. It permits the code site
// to append further messages to the failure message. For example:
//    VERIFY_SUCCESS(SomeCall()) << "Some more context about SomeCall";
// clang-format off
#define VERIFY_SUCCESS(expr)                                  \
  if (::testing::AssertionResult __assertion_result = (expr)) \
    ;                                                         \
  else                                                        \
    return __assertion_result << VERIFY_FAILED_LOCATION_
// clang-format on

#define VERIFY_AND_RETURN_SUCCESS(expression) \
  {                                           \
    VERIFY_SUCCESS(expression);               \
    return ::testing::AssertionSuccess();     \
  }

#endif  // NET_THIRD_PARTY_QUICHE_OVERRIDES_QUICHE_PLATFORM_IMPL_QUICHE_TEST_HELPERS_IMPL_H_