| 1 |
// Copyright 2007, Google Inc. |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// Redistribution and use in source and binary forms, with or without |
| 5 |
// modification, are permitted provided that the following conditions are |
| 6 |
// met: |
| 7 |
// |
| 8 |
// * Redistributions of source code must retain the above copyright |
| 9 |
// notice, this list of conditions and the following disclaimer. |
| 10 |
// * Redistributions in binary form must reproduce the above |
| 11 |
// copyright notice, this list of conditions and the following disclaimer |
| 12 |
// in the documentation and/or other materials provided with the |
| 13 |
// distribution. |
| 14 |
// * Neither the name of Google Inc. nor the names of its |
| 15 |
// contributors may be used to endorse or promote products derived from |
| 16 |
// this software without specific prior written permission. |
| 17 |
// |
| 18 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
|
| 30 |
// Google Mock - a framework for writing C++ mock classes. |
| 31 |
// |
| 32 |
// The MATCHER* family of macros can be used in a namespace scope to |
| 33 |
// define custom matchers easily. |
| 34 |
// |
| 35 |
// Basic Usage |
| 36 |
// =========== |
| 37 |
// |
| 38 |
// The syntax |
| 39 |
// |
| 40 |
// MATCHER(name, description_string) { statements; } |
| 41 |
// |
| 42 |
// defines a matcher with the given name that executes the statements, |
| 43 |
// which must return a bool to indicate if the match succeeds. Inside |
| 44 |
// the statements, you can refer to the value being matched by 'arg', |
| 45 |
// and refer to its type by 'arg_type'. |
| 46 |
// |
| 47 |
// The description string documents what the matcher does, and is used |
| 48 |
// to generate the failure message when the match fails. Since a |
| 49 |
// MATCHER() is usually defined in a header file shared by multiple |
| 50 |
// C++ source files, we require the description to be a C-string |
| 51 |
// literal to avoid possible side effects. It can be empty, in which |
| 52 |
// case we'll use the sequence of words in the matcher name as the |
| 53 |
// description. |
| 54 |
// |
| 55 |
// For example: |
| 56 |
// |
| 57 |
// MATCHER(IsEven, "") { return (arg % 2) == 0; } |
| 58 |
// |
| 59 |
// allows you to write |
| 60 |
// |
| 61 |
// // Expects mock_foo.Bar(n) to be called where n is even. |
| 62 |
// EXPECT_CALL(mock_foo, Bar(IsEven())); |
| 63 |
// |
| 64 |
// or, |
| 65 |
// |
| 66 |
// // Verifies that the value of some_expression is even. |
| 67 |
// EXPECT_THAT(some_expression, IsEven()); |
| 68 |
// |
| 69 |
// If the above assertion fails, it will print something like: |
| 70 |
// |
| 71 |
// Value of: some_expression |
| 72 |
// Expected: is even |
| 73 |
// Actual: 7 |
| 74 |
// |
| 75 |
// where the description "is even" is automatically calculated from the |
| 76 |
// matcher name IsEven. |
| 77 |
// |
| 78 |
// Argument Type |
| 79 |
// ============= |
| 80 |
// |
| 81 |
// Note that the type of the value being matched (arg_type) is |
| 82 |
// determined by the context in which you use the matcher and is |
| 83 |
// supplied to you by the compiler, so you don't need to worry about |
| 84 |
// declaring it (nor can you). This allows the matcher to be |
| 85 |
// polymorphic. For example, IsEven() can be used to match any type |
| 86 |
// where the value of "(arg % 2) == 0" can be implicitly converted to |
| 87 |
// a bool. In the "Bar(IsEven())" example above, if method Bar() |
| 88 |
// takes an int, 'arg_type' will be int; if it takes an unsigned long, |
| 89 |
// 'arg_type' will be unsigned long; and so on. |
| 90 |
// |
| 91 |
// Parameterizing Matchers |
| 92 |
// ======================= |
| 93 |
// |
| 94 |
// Sometimes you'll want to parameterize the matcher. For that you |
| 95 |
// can use another macro: |
| 96 |
// |
| 97 |
// MATCHER_P(name, param_name, description_string) { statements; } |
| 98 |
// |
| 99 |
// For example: |
| 100 |
// |
| 101 |
// MATCHER_P(HasAbsoluteValue, value, "") { return abs(arg) == value; } |
| 102 |
// |
| 103 |
// will allow you to write: |
| 104 |
// |
| 105 |
// EXPECT_THAT(Blah("a"), HasAbsoluteValue(n)); |
| 106 |
// |
| 107 |
// which may lead to this message (assuming n is 10): |
| 108 |
// |
| 109 |
// Value of: Blah("a") |
| 110 |
// Expected: has absolute value 10 |
| 111 |
// Actual: -9 |
| 112 |
// |
| 113 |
// Note that both the matcher description and its parameter are |
| 114 |
// printed, making the message human-friendly. |
| 115 |
// |
| 116 |
// In the matcher definition body, you can write 'foo_type' to |
| 117 |
// reference the type of a parameter named 'foo'. For example, in the |
| 118 |
// body of MATCHER_P(HasAbsoluteValue, value) above, you can write |
| 119 |
// 'value_type' to refer to the type of 'value'. |
| 120 |
// |
| 121 |
// We also provide MATCHER_P2, MATCHER_P3, ..., up to MATCHER_P$n to |
| 122 |
// support multi-parameter matchers. |
| 123 |
// |
| 124 |
// Describing Parameterized Matchers |
| 125 |
// ================================= |
| 126 |
// |
| 127 |
// The last argument to MATCHER*() is a string-typed expression. The |
| 128 |
// expression can reference all of the matcher's parameters and a |
| 129 |
// special bool-typed variable named 'negation'. When 'negation' is |
| 130 |
// false, the expression should evaluate to the matcher's description; |
| 131 |
// otherwise it should evaluate to the description of the negation of |
| 132 |
// the matcher. For example, |
| 133 |
// |
| 134 |
// using testing::PrintToString; |
| 135 |
// |
| 136 |
// MATCHER_P2(InClosedRange, low, hi, |
| 137 |
// std::string(negation ? "is not" : "is") + " in range [" + |
| 138 |
// PrintToString(low) + ", " + PrintToString(hi) + "]") { |
| 139 |
// return low <= arg && arg <= hi; |
| 140 |
// } |
| 141 |
// ... |
| 142 |
// EXPECT_THAT(3, InClosedRange(4, 6)); |
| 143 |
// EXPECT_THAT(3, Not(InClosedRange(2, 4))); |
| 144 |
// |
| 145 |
// would generate two failures that contain the text: |
| 146 |
// |
| 147 |
// Expected: is in range [4, 6] |
| 148 |
// ... |
| 149 |
// Expected: is not in range [2, 4] |
| 150 |
// |
| 151 |
// If you specify "" as the description, the failure message will |
| 152 |
// contain the sequence of words in the matcher name followed by the |
| 153 |
// parameter values printed as a tuple. For example, |
| 154 |
// |
| 155 |
// MATCHER_P2(InClosedRange, low, hi, "") { ... } |
| 156 |
// ... |
| 157 |
// EXPECT_THAT(3, InClosedRange(4, 6)); |
| 158 |
// EXPECT_THAT(3, Not(InClosedRange(2, 4))); |
| 159 |
// |
| 160 |
// would generate two failures that contain the text: |
| 161 |
// |
| 162 |
// Expected: in closed range (4, 6) |
| 163 |
// ... |
| 164 |
// Expected: not (in closed range (2, 4)) |
| 165 |
// |
| 166 |
// Types of Matcher Parameters |
| 167 |
// =========================== |
| 168 |
// |
| 169 |
// For the purpose of typing, you can view |
| 170 |
// |
| 171 |
// MATCHER_Pk(Foo, p1, ..., pk, description_string) { ... } |
| 172 |
// |
| 173 |
// as shorthand for |
| 174 |
// |
| 175 |
// template <typename p1_type, ..., typename pk_type> |
| 176 |
// FooMatcherPk<p1_type, ..., pk_type> |
| 177 |
// Foo(p1_type p1, ..., pk_type pk) { ... } |
| 178 |
// |
| 179 |
// When you write Foo(v1, ..., vk), the compiler infers the types of |
| 180 |
// the parameters v1, ..., and vk for you. If you are not happy with |
| 181 |
// the result of the type inference, you can specify the types by |
| 182 |
// explicitly instantiating the template, as in Foo<long, bool>(5, |
| 183 |
// false). As said earlier, you don't get to (or need to) specify |
| 184 |
// 'arg_type' as that's determined by the context in which the matcher |
| 185 |
// is used. You can assign the result of expression Foo(p1, ..., pk) |
| 186 |
// to a variable of type FooMatcherPk<p1_type, ..., pk_type>. This |
| 187 |
// can be useful when composing matchers. |
| 188 |
// |
| 189 |
// While you can instantiate a matcher template with reference types, |
| 190 |
// passing the parameters by pointer usually makes your code more |
| 191 |
// readable. If, however, you still want to pass a parameter by |
| 192 |
// reference, be aware that in the failure message generated by the |
| 193 |
// matcher you will see the value of the referenced object but not its |
| 194 |
// address. |
| 195 |
// |
| 196 |
// Explaining Match Results |
| 197 |
// ======================== |
| 198 |
// |
| 199 |
// Sometimes the matcher description alone isn't enough to explain why |
| 200 |
// the match has failed or succeeded. For example, when expecting a |
| 201 |
// long string, it can be very helpful to also print the diff between |
| 202 |
// the expected string and the actual one. To achieve that, you can |
| 203 |
// optionally stream additional information to a special variable |
| 204 |
// named result_listener, whose type is a pointer to class |
| 205 |
// MatchResultListener: |
| 206 |
// |
| 207 |
// MATCHER_P(EqualsLongString, str, "") { |
| 208 |
// if (arg == str) return true; |
| 209 |
// |
| 210 |
// *result_listener << "the difference: " |
| 211 |
/// << DiffStrings(str, arg); |
| 212 |
// return false; |
| 213 |
// } |
| 214 |
// |
| 215 |
// Overloading Matchers |
| 216 |
// ==================== |
| 217 |
// |
| 218 |
// You can overload matchers with different numbers of parameters: |
| 219 |
// |
| 220 |
// MATCHER_P(Blah, a, description_string1) { ... } |
| 221 |
// MATCHER_P2(Blah, a, b, description_string2) { ... } |
| 222 |
// |
| 223 |
// Caveats |
| 224 |
// ======= |
| 225 |
// |
| 226 |
// When defining a new matcher, you should also consider implementing |
| 227 |
// MatcherInterface or using MakePolymorphicMatcher(). These |
| 228 |
// approaches require more work than the MATCHER* macros, but also |
| 229 |
// give you more control on the types of the value being matched and |
| 230 |
// the matcher parameters, which may leads to better compiler error |
| 231 |
// messages when the matcher is used wrong. They also allow |
| 232 |
// overloading matchers based on parameter types (as opposed to just |
| 233 |
// based on the number of parameters). |
| 234 |
// |
| 235 |
// MATCHER*() can only be used in a namespace scope as templates cannot be |
| 236 |
// declared inside of a local class. |
| 237 |
// |
| 238 |
// More Information |
| 239 |
// ================ |
| 240 |
// |
| 241 |
// To learn more about using these macros, please search for 'MATCHER' |
| 242 |
// on |
| 243 |
// https://github.com/google/googletest/blob/master/docs/gmock_cook_book.md |
| 244 |
// |
| 245 |
// This file also implements some commonly used argument matchers. More |
| 246 |
// matchers can be defined by the user implementing the |
| 247 |
// MatcherInterface<T> interface if necessary. |
| 248 |
// |
| 249 |
// See googletest/include/gtest/gtest-matchers.h for the definition of class |
| 250 |
// Matcher, class MatcherInterface, and others. |
| 251 |
|
| 252 |
// IWYU pragma: private, include "gmock/gmock.h" |
| 253 |
// IWYU pragma: friend gmock/.* |
| 254 |
|
| 255 |
#ifndef GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_ |
| 256 |
#define GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_ |
| 257 |
|
| 258 |
#include <algorithm> |
| 259 |
#include <cmath> |
| 260 |
#include <initializer_list> |
| 261 |
#include <iterator> |
| 262 |
#include <limits> |
| 263 |
#include <memory> |
| 264 |
#include <ostream> // NOLINT |
| 265 |
#include <sstream> |
| 266 |
#include <string> |
| 267 |
#include <type_traits> |
| 268 |
#include <utility> |
| 269 |
#include <vector> |
| 270 |
|
| 271 |
#include "gmock/internal/gmock-internal-utils.h" |
| 272 |
#include "gmock/internal/gmock-port.h" |
| 273 |
#include "gmock/internal/gmock-pp.h" |
| 274 |
#include "gtest/gtest.h" |
| 275 |
|
| 276 |
// MSVC warning C5046 is new as of VS2017 version 15.8. |
| 277 |
#if defined(_MSC_VER) && _MSC_VER >= 1915 |
| 278 |
#define GMOCK_MAYBE_5046_ 5046 |
| 279 |
#else |
| 280 |
#define GMOCK_MAYBE_5046_ |
| 281 |
#endif |
| 282 |
|
| 283 |
GTEST_DISABLE_MSC_WARNINGS_PUSH_( |
| 284 |
4251 GMOCK_MAYBE_5046_ /* class A needs to have dll-interface to be used by |
| 285 |
clients of class B */ |
| 286 |
/* Symbol involving type with internal linkage not defined */) |
| 287 |
|
| 288 |
namespace testing { |
| 289 |
|
| 290 |
// To implement a matcher Foo for type T, define: |
| 291 |
// 1. a class FooMatcherImpl that implements the |
| 292 |
// MatcherInterface<T> interface, and |
| 293 |
// 2. a factory function that creates a Matcher<T> object from a |
| 294 |
// FooMatcherImpl*. |
| 295 |
// |
| 296 |
// The two-level delegation design makes it possible to allow a user |
| 297 |
// to write "v" instead of "Eq(v)" where a Matcher is expected, which |
| 298 |
// is impossible if we pass matchers by pointers. It also eases |
| 299 |
// ownership management as Matcher objects can now be copied like |
| 300 |
// plain values. |
| 301 |
|
| 302 |
// A match result listener that stores the explanation in a string. |
| 303 |
class StringMatchResultListener : public MatchResultListener { |
| 304 |
public: |
| 305 |
StringMatchResultListener() : MatchResultListener(&ss_) {} |
| 306 |
|
| 307 |
// Returns the explanation accumulated so far. |
| 308 |
std::string str() const { return ss_.str(); } |
| 309 |
|
| 310 |
// Clears the explanation accumulated so far. |
| 311 |
void Clear() { ss_.str(""); } |
| 312 |
|
| 313 |
private: |
| 314 |
::std::stringstream ss_; |
| 315 |
|
| 316 |
GTEST_DISALLOW_COPY_AND_ASSIGN_(StringMatchResultListener); |
| 317 |
}; |
| 318 |
|
| 319 |
// Anything inside the 'internal' namespace IS INTERNAL IMPLEMENTATION |
| 320 |
// and MUST NOT BE USED IN USER CODE!!! |
| 321 |
namespace internal { |
| 322 |
|
| 323 |
// The MatcherCastImpl class template is a helper for implementing |
| 324 |
// MatcherCast(). We need this helper in order to partially |
| 325 |
// specialize the implementation of MatcherCast() (C++ allows |
| 326 |
// class/struct templates to be partially specialized, but not |
| 327 |
// function templates.). |
| 328 |
|
| 329 |
// This general version is used when MatcherCast()'s argument is a |
| 330 |
// polymorphic matcher (i.e. something that can be converted to a |
| 331 |
// Matcher but is not one yet; for example, Eq(value)) or a value (for |
| 332 |
// example, "hello"). |
| 333 |
template <typename T, typename M> |
| 334 |
class MatcherCastImpl { |
| 335 |
public: |
| 336 |
static Matcher<T> Cast(const M& polymorphic_matcher_or_value) { |
| 337 |
// M can be a polymorphic matcher, in which case we want to use |
| 338 |
// its conversion operator to create Matcher<T>. Or it can be a value |
| 339 |
// that should be passed to the Matcher<T>'s constructor. |
| 340 |
// |
| 341 |
// We can't call Matcher<T>(polymorphic_matcher_or_value) when M is a |
| 342 |
// polymorphic matcher because it'll be ambiguous if T has an implicit |
| 343 |
// constructor from M (this usually happens when T has an implicit |
| 344 |
// constructor from any type). |
| 345 |
// |
| 346 |
// It won't work to unconditionally implicit_cast |
| 347 |
// polymorphic_matcher_or_value to Matcher<T> because it won't trigger |
| 348 |
// a user-defined conversion from M to T if one exists (assuming M is |
| 349 |
// a value). |
| 350 |
return CastImpl(polymorphic_matcher_or_value, |
| 351 |
std::is_convertible<M, Matcher<T>>{}, |
| 352 |
std::is_convertible<M, T>{}); |
| 353 |
} |
| 354 |
|
| 355 |
private: |
| 356 |
template <bool Ignore> |
| 357 |
static Matcher<T> CastImpl(const M& polymorphic_matcher_or_value, |
| 358 |
std::true_type /* convertible_to_matcher */, |
| 359 |
std::integral_constant<bool, Ignore>) { |
| 360 |
// M is implicitly convertible to Matcher<T>, which means that either |
| 361 |
// M is a polymorphic matcher or Matcher<T> has an implicit constructor |
| 362 |
// from M. In both cases using the implicit conversion will produce a |
| 363 |
// matcher. |
| 364 |
// |
| 365 |
// Even if T has an implicit constructor from M, it won't be called because |
| 366 |
// creating Matcher<T> would require a chain of two user-defined conversions |
| 367 |
// (first to create T from M and then to create Matcher<T> from T). |
| 368 |
return polymorphic_matcher_or_value; |
| 369 |
} |
| 370 |
|
| 371 |
// M can't be implicitly converted to Matcher<T>, so M isn't a polymorphic |
| 372 |
// matcher. It's a value of a type implicitly convertible to T. Use direct |
| 373 |
// initialization to create a matcher. |
| 374 |
static Matcher<T> CastImpl(const M& value, |
| 375 |
std::false_type /* convertible_to_matcher */, |
| 376 |
std::true_type /* convertible_to_T */) { |
| 377 |
return Matcher<T>(ImplicitCast_<T>(value)); |
| 378 |
} |
| 379 |
|
| 380 |
// M can't be implicitly converted to either Matcher<T> or T. Attempt to use |
| 381 |
// polymorphic matcher Eq(value) in this case. |
| 382 |
// |
| 383 |
// Note that we first attempt to perform an implicit cast on the value and |
| 384 |
// only fall back to the polymorphic Eq() matcher afterwards because the |
| 385 |
// latter calls bool operator==(const Lhs& lhs, const Rhs& rhs) in the end |
| 386 |
// which might be undefined even when Rhs is implicitly convertible to Lhs |
| 387 |
// (e.g. std::pair<const int, int> vs. std::pair<int, int>). |
| 388 |
// |
| 389 |
// We don't define this method inline as we need the declaration of Eq(). |
| 390 |
static Matcher<T> CastImpl(const M& value, |
| 391 |
std::false_type /* convertible_to_matcher */, |
| 392 |
std::false_type /* convertible_to_T */); |
| 393 |
}; |
| 394 |
|
| 395 |
// This more specialized version is used when MatcherCast()'s argument |
| 396 |
// is already a Matcher. This only compiles when type T can be |
| 397 |
// statically converted to type U. |
| 398 |
template <typename T, typename U> |
| 399 |
class MatcherCastImpl<T, Matcher<U>> { |
| 400 |
public: |
| 401 |
static Matcher<T> Cast(const Matcher<U>& source_matcher) { |
| 402 |
return Matcher<T>(new Impl(source_matcher)); |
| 403 |
} |
| 404 |
|
| 405 |
private: |
| 406 |
class Impl : public MatcherInterface<T> { |
| 407 |
public: |
| 408 |
explicit Impl(const Matcher<U>& source_matcher) |
| 409 |
: source_matcher_(source_matcher) {} |
| 410 |
|
| 411 |
// We delegate the matching logic to the source matcher. |
| 412 |
bool MatchAndExplain(T x, MatchResultListener* listener) const override { |
| 413 |
using FromType = typename std::remove_cv<typename std::remove_pointer< |
| 414 |
typename std::remove_reference<T>::type>::type>::type; |
| 415 |
using ToType = typename std::remove_cv<typename std::remove_pointer< |
| 416 |
typename std::remove_reference<U>::type>::type>::type; |
| 417 |
// Do not allow implicitly converting base*/& to derived*/&. |
| 418 |
static_assert( |
| 419 |
// Do not trigger if only one of them is a pointer. That implies a |
| 420 |
// regular conversion and not a down_cast. |
| 421 |
(std::is_pointer<typename std::remove_reference<T>::type>::value != |
| 422 |
std::is_pointer<typename std::remove_reference<U>::type>::value) || |
| 423 |
std::is_same<FromType, ToType>::value || |
| 424 |
!std::is_base_of<FromType, ToType>::value, |
| 425 |
"Can't implicitly convert from <base> to <derived>"); |
| 426 |
|
| 427 |
// Do the cast to `U` explicitly if necessary. |
| 428 |
// Otherwise, let implicit conversions do the trick. |
| 429 |
using CastType = |
| 430 |
typename std::conditional<std::is_convertible<T&, const U&>::value, |
| 431 |
T&, U>::type; |
| 432 |
|
| 433 |
return source_matcher_.MatchAndExplain(static_cast<CastType>(x), |
| 434 |
listener); |
| 435 |
} |
| 436 |
|
| 437 |
void DescribeTo(::std::ostream* os) const override { |
| 438 |
source_matcher_.DescribeTo(os); |
| 439 |
} |
| 440 |
|
| 441 |
void DescribeNegationTo(::std::ostream* os) const override { |
| 442 |
source_matcher_.DescribeNegationTo(os); |
| 443 |
} |
| 444 |
|
| 445 |
private: |
| 446 |
const Matcher<U> source_matcher_; |
| 447 |
}; |
| 448 |
}; |
| 449 |
|
| 450 |
// This even more specialized version is used for efficiently casting |
| 451 |
// a matcher to its own type. |
| 452 |
template <typename T> |
| 453 |
class MatcherCastImpl<T, Matcher<T>> { |
| 454 |
public: |
| 455 |
static Matcher<T> Cast(const Matcher<T>& matcher) { return matcher; } |
| 456 |
}; |
| 457 |
|
| 458 |
// Template specialization for parameterless Matcher. |
| 459 |
template <typename Derived> |
| 460 |
class MatcherBaseImpl { |
| 461 |
public: |
| 462 |
MatcherBaseImpl() = default; |
| 463 |
|
| 464 |
template <typename T> |
| 465 |
operator ::testing::Matcher<T>() const { // NOLINT(runtime/explicit) |
| 466 |
return ::testing::Matcher<T>(new |
| 467 |
typename Derived::template gmock_Impl<T>()); |
| 468 |
} |
| 469 |
}; |
| 470 |
|
| 471 |
// Template specialization for Matcher with parameters. |
| 472 |
template <template <typename...> class Derived, typename... Ts> |
| 473 |
class MatcherBaseImpl<Derived<Ts...>> { |
| 474 |
public: |
| 475 |
// Mark the constructor explicit for single argument T to avoid implicit |
| 476 |
// conversions. |
| 477 |
template <typename E = std::enable_if<sizeof...(Ts) == 1>, |
| 478 |
typename E::type* = nullptr> |
| 479 |
explicit MatcherBaseImpl(Ts... params) |
| 480 |
: params_(std::forward<Ts>(params)...) {} |
| 481 |
template <typename E = std::enable_if<sizeof...(Ts) != 1>, |
| 482 |
typename = typename E::type> |
| 483 |
MatcherBaseImpl(Ts... params) // NOLINT |
| 484 |
: params_(std::forward<Ts>(params)...) {} |
| 485 |
|
| 486 |
template <typename F> |
| 487 |
operator ::testing::Matcher<F>() const { // NOLINT(runtime/explicit) |
| 488 |
return Apply<F>(MakeIndexSequence<sizeof...(Ts)>{}); |
| 489 |
} |
| 490 |
|
| 491 |
private: |
| 492 |
template <typename F, std::size_t... tuple_ids> |
| 493 |
::testing::Matcher<F> Apply(IndexSequence<tuple_ids...>) const { |
| 494 |
return ::testing::Matcher<F>( |
| 495 |
new typename Derived<Ts...>::template gmock_Impl<F>( |
| 496 |
std::get<tuple_ids>(params_)...)); |
| 497 |
} |
| 498 |
|
| 499 |
const std::tuple<Ts...> params_; |
| 500 |
}; |
| 501 |
|
| 502 |
} // namespace internal |
| 503 |
|
| 504 |
// In order to be safe and clear, casting between different matcher |
| 505 |
// types is done explicitly via MatcherCast<T>(m), which takes a |
| 506 |
// matcher m and returns a Matcher<T>. It compiles only when T can be |
| 507 |
// statically converted to the argument type of m. |
| 508 |
template <typename T, typename M> |
| 509 |
inline Matcher<T> MatcherCast(const M& matcher) { |
| 510 |
return internal::MatcherCastImpl<T, M>::Cast(matcher); |
| 511 |
} |
| 512 |
|
| 513 |
// This overload handles polymorphic matchers and values only since |
| 514 |
// monomorphic matchers are handled by the next one. |
| 515 |
template <typename T, typename M> |
| 516 |
inline Matcher<T> SafeMatcherCast(const M& polymorphic_matcher_or_value) { |
| 517 |
return MatcherCast<T>(polymorphic_matcher_or_value); |
| 518 |
} |
| 519 |
|
| 520 |
// This overload handles monomorphic matchers. |
| 521 |
// |
| 522 |
// In general, if type T can be implicitly converted to type U, we can |
| 523 |
// safely convert a Matcher<U> to a Matcher<T> (i.e. Matcher is |
| 524 |
// contravariant): just keep a copy of the original Matcher<U>, convert the |
| 525 |
// argument from type T to U, and then pass it to the underlying Matcher<U>. |
| 526 |
// The only exception is when U is a reference and T is not, as the |
| 527 |
// underlying Matcher<U> may be interested in the argument's address, which |
| 528 |
// is not preserved in the conversion from T to U. |
| 529 |
template <typename T, typename U> |
| 530 |
inline Matcher<T> SafeMatcherCast(const Matcher<U>& matcher) { |
| 531 |
// Enforce that T can be implicitly converted to U. |
| 532 |
static_assert(std::is_convertible<const T&, const U&>::value, |
| 533 |
"T must be implicitly convertible to U"); |
| 534 |
// Enforce that we are not converting a non-reference type T to a reference |
| 535 |
// type U. |
| 536 |
GTEST_COMPILE_ASSERT_( |
| 537 |
std::is_reference<T>::value || !std::is_reference<U>::value, |
| 538 |
cannot_convert_non_reference_arg_to_reference); |
| 539 |
// In case both T and U are arithmetic types, enforce that the |
| 540 |
// conversion is not lossy. |
| 541 |
typedef GTEST_REMOVE_REFERENCE_AND_CONST_(T) RawT; |
| 542 |
typedef GTEST_REMOVE_REFERENCE_AND_CONST_(U) RawU; |
| 543 |
constexpr bool kTIsOther = GMOCK_KIND_OF_(RawT) == internal::kOther; |
| 544 |
constexpr bool kUIsOther = GMOCK_KIND_OF_(RawU) == internal::kOther; |
| 545 |
GTEST_COMPILE_ASSERT_( |
| 546 |
kTIsOther || kUIsOther || |
| 547 |
(internal::LosslessArithmeticConvertible<RawT, RawU>::value), |
| 548 |
conversion_of_arithmetic_types_must_be_lossless); |
| 549 |
return MatcherCast<T>(matcher); |
| 550 |
} |
| 551 |
|
| 552 |
// A<T>() returns a matcher that matches any value of type T. |
| 553 |
template <typename T> |
| 554 |
Matcher<T> A(); |
| 555 |
|
| 556 |
// Anything inside the 'internal' namespace IS INTERNAL IMPLEMENTATION |
| 557 |
// and MUST NOT BE USED IN USER CODE!!! |
| 558 |
namespace internal { |
| 559 |
|
| 560 |
// If the explanation is not empty, prints it to the ostream. |
| 561 |
inline void PrintIfNotEmpty(const std::string& explanation, |
| 562 |
::std::ostream* os) { |
| 563 |
if (explanation != "" && os != nullptr) { |
| 564 |
*os << ", " << explanation; |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
// Returns true if the given type name is easy to read by a human. |
| 569 |
// This is used to decide whether printing the type of a value might |
| 570 |
// be helpful. |
| 571 |
inline bool IsReadableTypeName(const std::string& type_name) { |
| 572 |
// We consider a type name readable if it's short or doesn't contain |
| 573 |
// a template or function type. |
| 574 |
return (type_name.length() <= 20 || |
| 575 |
type_name.find_first_of("<(") == std::string::npos); |
| 576 |
} |
| 577 |
|
| 578 |
// Matches the value against the given matcher, prints the value and explains |
| 579 |
// the match result to the listener. Returns the match result. |
| 580 |
// 'listener' must not be NULL. |
| 581 |
// Value cannot be passed by const reference, because some matchers take a |
| 582 |
// non-const argument. |
| 583 |
template <typename Value, typename T> |
| 584 |
bool MatchPrintAndExplain(Value& value, const Matcher<T>& matcher, |
| 585 |
MatchResultListener* listener) { |
| 586 |
if (!listener->IsInterested()) { |
| 587 |
// If the listener is not interested, we do not need to construct the |
| 588 |
// inner explanation. |
| 589 |
return matcher.Matches(value); |
| 590 |
} |
| 591 |
|
| 592 |
StringMatchResultListener inner_listener; |
| 593 |
const bool match = matcher.MatchAndExplain(value, &inner_listener); |
| 594 |
|
| 595 |
UniversalPrint(value, listener->stream()); |
| 596 |
#if GTEST_HAS_RTTI |
| 597 |
const std::string& type_name = GetTypeName<Value>(); |
| 598 |
if (IsReadableTypeName(type_name)) |
| 599 |
*listener->stream() << " (of type " << type_name << ")"; |
| 600 |
#endif |
| 601 |
PrintIfNotEmpty(inner_listener.str(), listener->stream()); |
| 602 |
|
| 603 |
return match; |
| 604 |
} |
| 605 |
|
| 606 |
// An internal helper class for doing compile-time loop on a tuple's |
| 607 |
// fields. |
| 608 |
template <size_t N> |
| 609 |
class TuplePrefix { |
| 610 |
public: |
| 611 |
// TuplePrefix<N>::Matches(matcher_tuple, value_tuple) returns true |
| 612 |
// if and only if the first N fields of matcher_tuple matches |
| 613 |
// the first N fields of value_tuple, respectively. |
| 614 |
template <typename MatcherTuple, typename ValueTuple> |
| 615 |
static bool Matches(const MatcherTuple& matcher_tuple, |
| 616 |
const ValueTuple& value_tuple) { |
| 617 |
return TuplePrefix<N - 1>::Matches(matcher_tuple, value_tuple) && |
| 618 |
std::get<N - 1>(matcher_tuple).Matches(std::get<N - 1>(value_tuple)); |
| 619 |
} |
| 620 |
|
| 621 |
// TuplePrefix<N>::ExplainMatchFailuresTo(matchers, values, os) |
| 622 |
// describes failures in matching the first N fields of matchers |
| 623 |
// against the first N fields of values. If there is no failure, |
| 624 |
// nothing will be streamed to os. |
| 625 |
template <typename MatcherTuple, typename ValueTuple> |
| 626 |
static void ExplainMatchFailuresTo(const MatcherTuple& matchers, |
| 627 |
const ValueTuple& values, |
| 628 |
::std::ostream* os) { |
| 629 |
// First, describes failures in the first N - 1 fields. |
| 630 |
TuplePrefix<N - 1>::ExplainMatchFailuresTo(matchers, values, os); |
| 631 |
|
| 632 |
// Then describes the failure (if any) in the (N - 1)-th (0-based) |
| 633 |
// field. |
| 634 |
typename std::tuple_element<N - 1, MatcherTuple>::type matcher = |
| 635 |
std::get<N - 1>(matchers); |
| 636 |
typedef typename std::tuple_element<N - 1, ValueTuple>::type Value; |
| 637 |
const Value& value = std::get<N - 1>(values); |
| 638 |
StringMatchResultListener listener; |
| 639 |
if (!matcher.MatchAndExplain(value, &listener)) { |
| 640 |
*os << " Expected arg #" << N - 1 << ": "; |
| 641 |
std::get<N - 1>(matchers).DescribeTo(os); |
| 642 |
*os << "\n Actual: "; |
| 643 |
// We remove the reference in type Value to prevent the |
| 644 |
// universal printer from printing the address of value, which |
| 645 |
// isn't interesting to the user most of the time. The |
| 646 |
// matcher's MatchAndExplain() method handles the case when |
| 647 |
// the address is interesting. |
| 648 |
internal::UniversalPrint(value, os); |
| 649 |
PrintIfNotEmpty(listener.str(), os); |
| 650 |
*os << "\n"; |
| 651 |
} |
| 652 |
} |
| 653 |
}; |
| 654 |
|
| 655 |
// The base case. |
| 656 |
template <> |
| 657 |
class TuplePrefix<0> { |
| 658 |
public: |
| 659 |
template <typename MatcherTuple, typename ValueTuple> |
| 660 |
static bool Matches(const MatcherTuple& /* matcher_tuple */, |
| 661 |
const ValueTuple& /* value_tuple */) { |
| 662 |
return true; |
| 663 |
} |
| 664 |
|
| 665 |
template <typename MatcherTuple, typename ValueTuple> |
| 666 |
static void ExplainMatchFailuresTo(const MatcherTuple& /* matchers */, |
| 667 |
const ValueTuple& /* values */, |
| 668 |
::std::ostream* /* os */) {} |
| 669 |
}; |
| 670 |
|
| 671 |
// TupleMatches(matcher_tuple, value_tuple) returns true if and only if |
| 672 |
// all matchers in matcher_tuple match the corresponding fields in |
| 673 |
// value_tuple. It is a compiler error if matcher_tuple and |
| 674 |
// value_tuple have different number of fields or incompatible field |
| 675 |
// types. |
| 676 |
template <typename MatcherTuple, typename ValueTuple> |
| 677 |
bool TupleMatches(const MatcherTuple& matcher_tuple, |
| 678 |
const ValueTuple& value_tuple) { |
| 679 |
// Makes sure that matcher_tuple and value_tuple have the same |
| 680 |
// number of fields. |
| 681 |
GTEST_COMPILE_ASSERT_(std::tuple_size<MatcherTuple>::value == |
| 682 |
std::tuple_size<ValueTuple>::value, |
| 683 |
matcher_and_value_have_different_numbers_of_fields); |
| 684 |
return TuplePrefix<std::tuple_size<ValueTuple>::value>::Matches(matcher_tuple, |
| 685 |
value_tuple); |
| 686 |
} |
| 687 |
|
| 688 |
// Describes failures in matching matchers against values. If there |
| 689 |
// is no failure, nothing will be streamed to os. |
| 690 |
template <typename MatcherTuple, typename ValueTuple> |
| 691 |
void ExplainMatchFailureTupleTo(const MatcherTuple& matchers, |
| 692 |
const ValueTuple& values, ::std::ostream* os) { |
| 693 |
TuplePrefix<std::tuple_size<MatcherTuple>::value>::ExplainMatchFailuresTo( |
| 694 |
matchers, values, os); |
| 695 |
} |
| 696 |
|
| 697 |
// TransformTupleValues and its helper. |
| 698 |
// |
| 699 |
// TransformTupleValuesHelper hides the internal machinery that |
| 700 |
// TransformTupleValues uses to implement a tuple traversal. |
| 701 |
template <typename Tuple, typename Func, typename OutIter> |
| 702 |
class TransformTupleValuesHelper { |
| 703 |
private: |
| 704 |
typedef ::std::tuple_size<Tuple> TupleSize; |
| 705 |
|
| 706 |
public: |
| 707 |
// For each member of tuple 't', taken in order, evaluates '*out++ = f(t)'. |
| 708 |
// Returns the final value of 'out' in case the caller needs it. |
| 709 |
static OutIter Run(Func f, const Tuple& t, OutIter out) { |
| 710 |
return IterateOverTuple<Tuple, TupleSize::value>()(f, t, out); |
| 711 |
} |
| 712 |
|
| 713 |
private: |
| 714 |
template <typename Tup, size_t kRemainingSize> |
| 715 |
struct IterateOverTuple { |
| 716 |
OutIter operator()(Func f, const Tup& t, OutIter out) const { |
| 717 |
*out++ = f(::std::get<TupleSize::value - kRemainingSize>(t)); |
| 718 |
return IterateOverTuple<Tup, kRemainingSize - 1>()(f, t, out); |
| 719 |
} |
| 720 |
}; |
| 721 |
template <typename Tup> |
| 722 |
struct IterateOverTuple<Tup, 0> { |
| 723 |
OutIter operator()(Func /* f */, const Tup& /* t */, OutIter out) const { |
| 724 |
return out; |
| 725 |
} |
| 726 |
}; |
| 727 |
}; |
| 728 |
|
| 729 |
// Successively invokes 'f(element)' on each element of the tuple 't', |
| 730 |
// appending each result to the 'out' iterator. Returns the final value |
| 731 |
// of 'out'. |
| 732 |
template <typename Tuple, typename Func, typename OutIter> |
| 733 |
OutIter TransformTupleValues(Func f, const Tuple& t, OutIter out) { |
| 734 |
return TransformTupleValuesHelper<Tuple, Func, OutIter>::Run(f, t, out); |
| 735 |
} |
| 736 |
|
| 737 |
// Implements _, a matcher that matches any value of any |
| 738 |
// type. This is a polymorphic matcher, so we need a template type |
| 739 |
// conversion operator to make it appearing as a Matcher<T> for any |
| 740 |
// type T. |
| 741 |
class AnythingMatcher { |
| 742 |
public: |
| 743 |
using is_gtest_matcher = void; |
| 744 |
|
| 745 |
template <typename T> |
| 746 |
bool MatchAndExplain(const T& /* x */, std::ostream* /* listener */) const { |
| 747 |
return true; |
| 748 |
} |
| 749 |
void DescribeTo(std::ostream* os) const { *os << "is anything"; } |
| 750 |
void DescribeNegationTo(::std::ostream* os) const { |
| 751 |
// This is mostly for completeness' sake, as it's not very useful |
| 752 |
// to write Not(A<bool>()). However we cannot completely rule out |
| 753 |
// such a possibility, and it doesn't hurt to be prepared. |
| 754 |
*os << "never matches"; |
| 755 |
} |
| 756 |
}; |
| 757 |
|
| 758 |
// Implements the polymorphic IsNull() matcher, which matches any raw or smart |
| 759 |
// pointer that is NULL. |
| 760 |
class IsNullMatcher { |
| 761 |
public: |
| 762 |
template <typename Pointer> |
| 763 |
bool MatchAndExplain(const Pointer& p, |
| 764 |
MatchResultListener* /* listener */) const { |
| 765 |
return p == nullptr; |
| 766 |
} |
| 767 |
|
| 768 |
void DescribeTo(::std::ostream* os) const { *os << "is NULL"; } |
| 769 |
void DescribeNegationTo(::std::ostream* os) const { *os << "isn't NULL"; } |
| 770 |
}; |
| 771 |
|
| 772 |
// Implements the polymorphic NotNull() matcher, which matches any raw or smart |
| 773 |
// pointer that is not NULL. |
| 774 |
class NotNullMatcher { |
| 775 |
public: |
| 776 |
template <typename Pointer> |
| 777 |
bool MatchAndExplain(const Pointer& p, |
| 778 |
MatchResultListener* /* listener */) const { |
| 779 |
return p != nullptr; |
| 780 |
} |
| 781 |
|
| 782 |
void DescribeTo(::std::ostream* os) const { *os << "isn't NULL"; } |
| 783 |
void DescribeNegationTo(::std::ostream* os) const { *os << "is NULL"; } |
| 784 |
}; |
| 785 |
|
| 786 |
// Ref(variable) matches any argument that is a reference to |
| 787 |
// 'variable'. This matcher is polymorphic as it can match any |
| 788 |
// super type of the type of 'variable'. |
| 789 |
// |
| 790 |
// The RefMatcher template class implements Ref(variable). It can |
| 791 |
// only be instantiated with a reference type. This prevents a user |
| 792 |
// from mistakenly using Ref(x) to match a non-reference function |
| 793 |
// argument. For example, the following will righteously cause a |
| 794 |
// compiler error: |
| 795 |
// |
| 796 |
// int n; |
| 797 |
// Matcher<int> m1 = Ref(n); // This won't compile. |
| 798 |
// Matcher<int&> m2 = Ref(n); // This will compile. |
| 799 |
template <typename T> |
| 800 |
class RefMatcher; |
| 801 |
|
| 802 |
template <typename T> |
| 803 |
class RefMatcher<T&> { |
| 804 |
// Google Mock is a generic framework and thus needs to support |
| 805 |
// mocking any function types, including those that take non-const |
| 806 |
// reference arguments. Therefore the template parameter T (and |
| 807 |
// Super below) can be instantiated to either a const type or a |
| 808 |
// non-const type. |
| 809 |
public: |
| 810 |
// RefMatcher() takes a T& instead of const T&, as we want the |
| 811 |
// compiler to catch using Ref(const_value) as a matcher for a |
| 812 |
// non-const reference. |
| 813 |
explicit RefMatcher(T& x) : object_(x) {} // NOLINT |
| 814 |
|
| 815 |
template <typename Super> |
| 816 |
operator Matcher<Super&>() const { |
| 817 |
// By passing object_ (type T&) to Impl(), which expects a Super&, |
| 818 |
// we make sure that Super is a super type of T. In particular, |
| 819 |
// this catches using Ref(const_value) as a matcher for a |
| 820 |
// non-const reference, as you cannot implicitly convert a const |
| 821 |
// reference to a non-const reference. |
| 822 |
return MakeMatcher(new Impl<Super>(object_)); |
| 823 |
} |
| 824 |
|
| 825 |
private: |
| 826 |
template <typename Super> |
| 827 |
class Impl : public MatcherInterface<Super&> { |
| 828 |
public: |
| 829 |
explicit Impl(Super& x) : object_(x) {} // NOLINT |
| 830 |
|
| 831 |
// MatchAndExplain() takes a Super& (as opposed to const Super&) |
| 832 |
// in order to match the interface MatcherInterface<Super&>. |
| 833 |
bool MatchAndExplain(Super& x, |
| 834 |
MatchResultListener* listener) const override { |
| 835 |
*listener << "which is located @" << static_cast<const void*>(&x); |
| 836 |
return &x == &object_; |
| 837 |
} |
| 838 |
|
| 839 |
void DescribeTo(::std::ostream* os) const override { |
| 840 |
*os << "references the variable "; |
| 841 |
UniversalPrinter<Super&>::Print(object_, os); |
| 842 |
} |
| 843 |
|
| 844 |
void DescribeNegationTo(::std::ostream* os) const override { |
| 845 |
*os << "does not reference the variable "; |
| 846 |
UniversalPrinter<Super&>::Print(object_, os); |
| 847 |
} |
| 848 |
|
| 849 |
private: |
| 850 |
const Super& object_; |
| 851 |
}; |
| 852 |
|
| 853 |
T& object_; |
| 854 |
}; |
| 855 |
|
| 856 |
// Polymorphic helper functions for narrow and wide string matchers. |
| 857 |
inline bool CaseInsensitiveCStringEquals(const char* lhs, const char* rhs) { |
| 858 |
return String::CaseInsensitiveCStringEquals(lhs, rhs); |
| 859 |
} |
| 860 |
|
| 861 |
inline bool CaseInsensitiveCStringEquals(const wchar_t* lhs, |
| 862 |
const wchar_t* rhs) { |
| 863 |
return String::CaseInsensitiveWideCStringEquals(lhs, rhs); |
| 864 |
} |
| 865 |
|
| 866 |
// String comparison for narrow or wide strings that can have embedded NUL |
| 867 |
// characters. |
| 868 |
template <typename StringType> |
| 869 |
bool CaseInsensitiveStringEquals(const StringType& s1, const StringType& s2) { |
| 870 |
// Are the heads equal? |
| 871 |
if (!CaseInsensitiveCStringEquals(s1.c_str(), s2.c_str())) { |
| 872 |
return false; |
| 873 |
} |
| 874 |
|
| 875 |
// Skip the equal heads. |
| 876 |
const typename StringType::value_type nul = 0; |
| 877 |
const size_t i1 = s1.find(nul), i2 = s2.find(nul); |
| 878 |
|
| 879 |
// Are we at the end of either s1 or s2? |
| 880 |
if (i1 == StringType::npos || i2 == StringType::npos) { |
| 881 |
return i1 == i2; |
| 882 |
} |
| 883 |
|
| 884 |
// Are the tails equal? |
| 885 |
return CaseInsensitiveStringEquals(s1.substr(i1 + 1), s2.substr(i2 + 1)); |
| 886 |
} |
| 887 |
|
| 888 |
// String matchers. |
| 889 |
|
| 890 |
// Implements equality-based string matchers like StrEq, StrCaseNe, and etc. |
| 891 |
template <typename StringType> |
| 892 |
class StrEqualityMatcher { |
| 893 |
public: |
| 894 |
StrEqualityMatcher(StringType str, bool expect_eq, bool case_sensitive) |
| 895 |
: string_(std::move(str)), |
| 896 |
expect_eq_(expect_eq), |
| 897 |
case_sensitive_(case_sensitive) {} |
| 898 |
|
| 899 |
#if GTEST_INTERNAL_HAS_STRING_VIEW |
| 900 |
bool MatchAndExplain(const internal::StringView& s, |
| 901 |
MatchResultListener* listener) const { |
| 902 |
// This should fail to compile if StringView is used with wide |
| 903 |
// strings. |
| 904 |
const StringType& str = std::string(s); |
| 905 |
return MatchAndExplain(str, listener); |
| 906 |
} |
| 907 |
#endif // GTEST_INTERNAL_HAS_STRING_VIEW |
| 908 |
|
| 909 |
// Accepts pointer types, particularly: |
| 910 |
// const char* |
| 911 |
// char* |
| 912 |
// const wchar_t* |
| 913 |
// wchar_t* |
| 914 |
template <typename CharType> |
| 915 |
bool MatchAndExplain(CharType* s, MatchResultListener* listener) const { |
| 916 |
if (s == nullptr) { |
| 917 |
return !expect_eq_; |
| 918 |
} |
| 919 |
return MatchAndExplain(StringType(s), listener); |
| 920 |
} |
| 921 |
|
| 922 |
// Matches anything that can convert to StringType. |
| 923 |
// |
| 924 |
// This is a template, not just a plain function with const StringType&, |
| 925 |
// because StringView has some interfering non-explicit constructors. |
| 926 |
template <typename MatcheeStringType> |
| 927 |
bool MatchAndExplain(const MatcheeStringType& s, |
| 928 |
MatchResultListener* /* listener */) const { |
| 929 |
const StringType s2(s); |
| 930 |
const bool eq = case_sensitive_ ? s2 == string_ |
| 931 |
: CaseInsensitiveStringEquals(s2, string_); |
| 932 |
return expect_eq_ == eq; |
| 933 |
} |
| 934 |
|
| 935 |
void DescribeTo(::std::ostream* os) const { |
| 936 |
DescribeToHelper(expect_eq_, os); |
| 937 |
} |
| 938 |
|
| 939 |
void DescribeNegationTo(::std::ostream* os) const { |
| 940 |
DescribeToHelper(!expect_eq_, os); |
| 941 |
} |
| 942 |
|
| 943 |
private: |
| 944 |
void DescribeToHelper(bool expect_eq, ::std::ostream* os) const { |
| 945 |
*os << (expect_eq ? "is " : "isn't "); |
| 946 |
*os << "equal to "; |
| 947 |
if (!case_sensitive_) { |
| 948 |
*os << "(ignoring case) "; |
| 949 |
} |
| 950 |
UniversalPrint(string_, os); |
| 951 |
} |
| 952 |
|
| 953 |
const StringType string_; |
| 954 |
const bool expect_eq_; |
| 955 |
const bool case_sensitive_; |
| 956 |
}; |
| 957 |
|
| 958 |
// Implements the polymorphic HasSubstr(substring) matcher, which |
| 959 |
// can be used as a Matcher<T> as long as T can be converted to a |
| 960 |
// string. |
| 961 |
template <typename StringType> |
| 962 |
class HasSubstrMatcher { |
| 963 |
public: |
| 964 |
explicit HasSubstrMatcher(const StringType& substring) |
| 965 |
: substring_(substring) {} |
| 966 |
|
| 967 |
#if GTEST_INTERNAL_HAS_STRING_VIEW |
| 968 |
bool MatchAndExplain(const internal::StringView& s, |
| 969 |
MatchResultListener* listener) const { |
| 970 |
// This should fail to compile if StringView is used with wide |
| 971 |
// strings. |
| 972 |
const StringType& str = std::string(s); |
| 973 |
return MatchAndExplain(str, listener); |
| 974 |
} |
| 975 |
#endif // GTEST_INTERNAL_HAS_STRING_VIEW |
| 976 |
|
| 977 |
// Accepts pointer types, particularly: |
| 978 |
// const char* |
| 979 |
// char* |
| 980 |
// const wchar_t* |
| 981 |
// wchar_t* |
| 982 |
template <typename CharType> |
| 983 |
bool MatchAndExplain(CharType* s, MatchResultListener* listener) const { |
| 984 |
return s != nullptr && MatchAndExplain(StringType(s), listener); |
| 985 |
} |
| 986 |
|
| 987 |
// Matches anything that can convert to StringType. |
| 988 |
// |
| 989 |
// This is a template, not just a plain function with const StringType&, |
| 990 |
// because StringView has some interfering non-explicit constructors. |
| 991 |
template <typename MatcheeStringType> |
| 992 |
bool MatchAndExplain(const MatcheeStringType& s, |
| 993 |
MatchResultListener* /* listener */) const { |
| 994 |
return StringType(s).find(substring_) != StringType::npos; |
| 995 |
} |
| 996 |
|
| 997 |
// Describes what this matcher matches. |
| 998 |
void DescribeTo(::std::ostream* os) const { |
| 999 |
*os << "has substring "; |
| 1000 |
UniversalPrint(substring_, os); |
| 1001 |
} |
| 1002 |
|
| 1003 |
void DescribeNegationTo(::std::ostream* os) const { |
| 1004 |
*os << "has no substring "; |
| 1005 |
UniversalPrint(substring_, os); |
| 1006 |
} |
| 1007 |
|
| 1008 |
private: |
| 1009 |
const StringType substring_; |
| 1010 |
}; |
| 1011 |
|
| 1012 |
// Implements the polymorphic StartsWith(substring) matcher, which |
| 1013 |
// can be used as a Matcher<T> as long as T can be converted to a |
| 1014 |
// string. |
| 1015 |
template <typename StringType> |
| 1016 |
class StartsWithMatcher { |
| 1017 |
public: |
| 1018 |
explicit StartsWithMatcher(const StringType& prefix) : prefix_(prefix) {} |
| 1019 |
|
| 1020 |
#if GTEST_INTERNAL_HAS_STRING_VIEW |
| 1021 |
bool MatchAndExplain(const internal::StringView& s, |
| 1022 |
MatchResultListener* listener) const { |
| 1023 |
// This should fail to compile if StringView is used with wide |
| 1024 |
// strings. |
| 1025 |
const StringType& str = std::string(s); |
| 1026 |
return MatchAndExplain(str, listener); |
| 1027 |
} |
| 1028 |
#endif // GTEST_INTERNAL_HAS_STRING_VIEW |
| 1029 |
|
| 1030 |
// Accepts pointer types, particularly: |
| 1031 |
// const char* |
| 1032 |
// char* |
| 1033 |
// const wchar_t* |
| 1034 |
// wchar_t* |
| 1035 |
template <typename CharType> |
| 1036 |
bool MatchAndExplain(CharType* s, MatchResultListener* listener) const { |
| 1037 |
return s != nullptr && MatchAndExplain(StringType(s), listener); |
| 1038 |
} |
| 1039 |
|
| 1040 |
// Matches anything that can convert to StringType. |
| 1041 |
// |
| 1042 |
// This is a template, not just a plain function with const StringType&, |
| 1043 |
// because StringView has some interfering non-explicit constructors. |
| 1044 |
template <typename MatcheeStringType> |
| 1045 |
bool MatchAndExplain(const MatcheeStringType& s, |
| 1046 |
MatchResultListener* /* listener */) const { |
| 1047 |
const StringType& s2(s); |
| 1048 |
return s2.length() >= prefix_.length() && |
| 1049 |
s2.substr(0, prefix_.length()) == prefix_; |
| 1050 |
} |
| 1051 |
|
| 1052 |
void DescribeTo(::std::ostream* os) const { |
| 1053 |
*os << "starts with "; |
| 1054 |
UniversalPrint(prefix_, os); |
| 1055 |
} |
| 1056 |
|
| 1057 |
void DescribeNegationTo(::std::ostream* os) const { |
| 1058 |
*os << "doesn't start with "; |
| 1059 |
UniversalPrint(prefix_, os); |
| 1060 |
} |
| 1061 |
|
| 1062 |
private: |
| 1063 |
const StringType prefix_; |
| 1064 |
}; |
| 1065 |
|
| 1066 |
// Implements the polymorphic EndsWith(substring) matcher, which |
| 1067 |
// can be used as a Matcher<T> as long as T can be converted to a |
| 1068 |
// string. |
| 1069 |
template <typename StringType> |
| 1070 |
class EndsWithMatcher { |
| 1071 |
public: |
| 1072 |
explicit EndsWithMatcher(const StringType& suffix) : suffix_(suffix) {} |
| 1073 |
|
| 1074 |
#if GTEST_INTERNAL_HAS_STRING_VIEW |
| 1075 |
bool MatchAndExplain(const internal::StringView& s, |
| 1076 |
MatchResultListener* listener) const { |
| 1077 |
// This should fail to compile if StringView is used with wide |
| 1078 |
// strings. |
| 1079 |
const StringType& str = std::string(s); |
| 1080 |
return MatchAndExplain(str, listener); |
| 1081 |
} |
| 1082 |
#endif // GTEST_INTERNAL_HAS_STRING_VIEW |
| 1083 |
|
| 1084 |
// Accepts pointer types, particularly: |
| 1085 |
// const char* |
| 1086 |
// char* |
| 1087 |
// const wchar_t* |
| 1088 |
// wchar_t* |
| 1089 |
template <typename CharType> |
| 1090 |
bool MatchAndExplain(CharType* s, MatchResultListener* listener) const { |
| 1091 |
return s != nullptr && MatchAndExplain(StringType(s), listener); |
| 1092 |
} |
| 1093 |
|
| 1094 |
// Matches anything that can convert to StringType. |
| 1095 |
// |
| 1096 |
// This is a template, not just a plain function with const StringType&, |
| 1097 |
// because StringView has some interfering non-explicit constructors. |
| 1098 |
template <typename MatcheeStringType> |
| 1099 |
bool MatchAndExplain(const MatcheeStringType& s, |
| 1100 |
MatchResultListener* /* listener */) const { |
| 1101 |
const StringType& s2(s); |
| 1102 |
return s2.length() >= suffix_.length() && |
| 1103 |
s2.substr(s2.length() - suffix_.length()) == suffix_; |
| 1104 |
} |
| 1105 |
|
| 1106 |
void DescribeTo(::std::ostream* os) const { |
| 1107 |
*os << "ends with "; |
| 1108 |
UniversalPrint(suffix_, os); |
| 1109 |
} |
| 1110 |
|
| 1111 |
void DescribeNegationTo(::std::ostream* os) const { |
| 1112 |
*os << "doesn't end with "; |
| 1113 |
UniversalPrint(suffix_, os); |
| 1114 |
} |
| 1115 |
|
| 1116 |
private: |
| 1117 |
const StringType suffix_; |
| 1118 |
}; |
| 1119 |
|
| 1120 |
// Implements the polymorphic WhenBase64Unescaped(matcher) matcher, which can be |
| 1121 |
// used as a Matcher<T> as long as T can be converted to a string. |
| 1122 |
class WhenBase64UnescapedMatcher { |
| 1123 |
public: |
| 1124 |
using is_gtest_matcher = void; |
| 1125 |
|
| 1126 |
explicit WhenBase64UnescapedMatcher( |
| 1127 |
const Matcher<const std::string&>& internal_matcher) |
| 1128 |
: internal_matcher_(internal_matcher) {} |
| 1129 |
|
| 1130 |
// Matches anything that can convert to std::string. |
| 1131 |
template <typename MatcheeStringType> |
| 1132 |
bool MatchAndExplain(const MatcheeStringType& s, |
| 1133 |
MatchResultListener* listener) const { |
| 1134 |
const std::string s2(s); // NOLINT (needed for working with string_view). |
| 1135 |
std::string unescaped; |
| 1136 |
if (!internal::Base64Unescape(s2, &unescaped)) { |
| 1137 |
if (listener != nullptr) { |
| 1138 |
*listener << "is not a valid base64 escaped string"; |
| 1139 |
} |
| 1140 |
return false; |
| 1141 |
} |
| 1142 |
return MatchPrintAndExplain(unescaped, internal_matcher_, listener); |
| 1143 |
} |
| 1144 |
|
| 1145 |
void DescribeTo(::std::ostream* os) const { |
| 1146 |
*os << "matches after Base64Unescape "; |
| 1147 |
internal_matcher_.DescribeTo(os); |
| 1148 |
} |
| 1149 |
|
| 1150 |
void DescribeNegationTo(::std::ostream* os) const { |
| 1151 |
*os << "does not match after Base64Unescape "; |
| 1152 |
internal_matcher_.DescribeTo(os); |
| 1153 |
} |
| 1154 |
|
| 1155 |
private: |
| 1156 |
const Matcher<const std::string&> internal_matcher_; |
| 1157 |
}; |
| 1158 |
|
| 1159 |
// Implements a matcher that compares the two fields of a 2-tuple |
| 1160 |
// using one of the ==, <=, <, etc, operators. The two fields being |
| 1161 |
// compared don't have to have the same type. |
| 1162 |
// |
| 1163 |
// The matcher defined here is polymorphic (for example, Eq() can be |
| 1164 |
// used to match a std::tuple<int, short>, a std::tuple<const long&, double>, |
| 1165 |
// etc). Therefore we use a template type conversion operator in the |
| 1166 |
// implementation. |
| 1167 |
template <typename D, typename Op> |
| 1168 |
class PairMatchBase { |
| 1169 |
public: |
| 1170 |
template <typename T1, typename T2> |
| 1171 |
operator Matcher<::std::tuple<T1, T2>>() const { |
| 1172 |
return Matcher<::std::tuple<T1, T2>>(new Impl<const ::std::tuple<T1, T2>&>); |
| 1173 |
} |
| 1174 |
template <typename T1, typename T2> |
| 1175 |
operator Matcher<const ::std::tuple<T1, T2>&>() const { |
| 1176 |
return MakeMatcher(new Impl<const ::std::tuple<T1, T2>&>); |
| 1177 |
} |
| 1178 |
|
| 1179 |
private: |
| 1180 |
static ::std::ostream& GetDesc(::std::ostream& os) { // NOLINT |
| 1181 |
return os << D::Desc(); |
| 1182 |
} |
| 1183 |
|
| 1184 |
template <typename Tuple> |
| 1185 |
class Impl : public MatcherInterface<Tuple> { |
| 1186 |
public: |
| 1187 |
bool MatchAndExplain(Tuple args, |
| 1188 |
MatchResultListener* /* listener */) const override { |
| 1189 |
return Op()(::std::get<0>(args), ::std::get<1>(args)); |
| 1190 |
} |
| 1191 |
void DescribeTo(::std::ostream* os) const override { |
| 1192 |
*os << "are " << GetDesc; |
| 1193 |
} |
| 1194 |
void DescribeNegationTo(::std::ostream* os) const override { |
| 1195 |
*os << "aren't " << GetDesc; |
| 1196 |
} |
| 1197 |
}; |
| 1198 |
}; |
| 1199 |
|
| 1200 |
class Eq2Matcher : public PairMatchBase<Eq2Matcher, AnyEq> { |
| 1201 |
public: |
| 1202 |
static const char* Desc() { return "an equal pair"; } |
| 1203 |
}; |
| 1204 |
class Ne2Matcher : public PairMatchBase<Ne2Matcher, AnyNe> { |
| 1205 |
public: |
| 1206 |
static const char* Desc() { return "an unequal pair"; } |
| 1207 |
}; |
| 1208 |
class Lt2Matcher : public PairMatchBase<Lt2Matcher, AnyLt> { |
| 1209 |
public: |
| 1210 |
static const char* Desc() { return "a pair where the first < the second"; } |
| 1211 |
}; |
| 1212 |
class Gt2Matcher : public PairMatchBase<Gt2Matcher, AnyGt> { |
| 1213 |
public: |
| 1214 |
static const char* Desc() { return "a pair where the first > the second"; } |
| 1215 |
}; |
| 1216 |
class Le2Matcher : public PairMatchBase<Le2Matcher, AnyLe> { |
| 1217 |
public: |
| 1218 |
static const char* Desc() { return "a pair where the first <= the second"; } |
| 1219 |
}; |
| 1220 |
class Ge2Matcher : public PairMatchBase<Ge2Matcher, AnyGe> { |
| 1221 |
public: |
| 1222 |
static const char* Desc() { return "a pair where the first >= the second"; } |
| 1223 |
}; |
| 1224 |
|
| 1225 |
// Implements the Not(...) matcher for a particular argument type T. |
| 1226 |
// We do not nest it inside the NotMatcher class template, as that |
| 1227 |
// will prevent different instantiations of NotMatcher from sharing |
| 1228 |
// the same NotMatcherImpl<T> class. |
| 1229 |
template <typename T> |
| 1230 |
class NotMatcherImpl : public MatcherInterface<const T&> { |
| 1231 |
public: |
| 1232 |
explicit NotMatcherImpl(const Matcher<T>& matcher) : matcher_(matcher) {} |
| 1233 |
|
| 1234 |
bool MatchAndExplain(const T& x, |
| 1235 |
MatchResultListener* listener) const override { |
| 1236 |
return !matcher_.MatchAndExplain(x, listener); |
| 1237 |
} |
| 1238 |
|
| 1239 |
void DescribeTo(::std::ostream* os) const override { |
| 1240 |
matcher_.DescribeNegationTo(os); |
| 1241 |
} |
| 1242 |
|
| 1243 |
void DescribeNegationTo(::std::ostream* os) const override { |
| 1244 |
matcher_.DescribeTo(os); |
| 1245 |
} |
| 1246 |
|
| 1247 |
private: |
| 1248 |
const Matcher<T> matcher_; |
| 1249 |
}; |
| 1250 |
|
| 1251 |
// Implements the Not(m) matcher, which matches a value that doesn't |
| 1252 |
// match matcher m. |
| 1253 |
template <typename InnerMatcher> |
| 1254 |
class NotMatcher { |
| 1255 |
public: |
| 1256 |
explicit NotMatcher(InnerMatcher matcher) : matcher_(matcher) {} |
| 1257 |
|
| 1258 |
// This template type conversion operator allows Not(m) to be used |
| 1259 |
// to match any type m can match. |
| 1260 |
template <typename T> |
| 1261 |
operator Matcher<T>() const { |
| 1262 |
return Matcher<T>(new NotMatcherImpl<T>(SafeMatcherCast<T>(matcher_))); |
| 1263 |
} |
| 1264 |
|
| 1265 |
private: |
| 1266 |
InnerMatcher matcher_; |
| 1267 |
}; |
| 1268 |
|
| 1269 |
// Implements the AllOf(m1, m2) matcher for a particular argument type |
| 1270 |
// T. We do not nest it inside the BothOfMatcher class template, as |
| 1271 |
// that will prevent different instantiations of BothOfMatcher from |
| 1272 |
// sharing the same BothOfMatcherImpl<T> class. |
| 1273 |
template <typename T> |
| 1274 |
class AllOfMatcherImpl : public MatcherInterface<const T&> { |
| 1275 |
public: |
| 1276 |
explicit AllOfMatcherImpl(std::vector<Matcher<T>> matchers) |
| 1277 |
: matchers_(std::move(matchers)) {} |
| 1278 |
|
| 1279 |
void DescribeTo(::std::ostream* os) const override { |
| 1280 |
*os << "("; |
| 1281 |
for (size_t i = 0; i < matchers_.size(); ++i) { |
| 1282 |
if (i != 0) *os << ") and ("; |
| 1283 |
matchers_[i].DescribeTo(os); |
| 1284 |
} |
| 1285 |
*os << ")"; |
| 1286 |
} |
| 1287 |
|
| 1288 |
void DescribeNegationTo(::std::ostream* os) const override { |
| 1289 |
*os << "("; |
| 1290 |
for (size_t i = 0; i < matchers_.size(); ++i) { |
| 1291 |
if (i != 0) *os << ") or ("; |
| 1292 |
matchers_[i].DescribeNegationTo(os); |
| 1293 |
} |
| 1294 |
*os << ")"; |
| 1295 |
} |
| 1296 |
|
| 1297 |
bool MatchAndExplain(const T& x, |
| 1298 |
MatchResultListener* listener) const override { |
| 1299 |
// If either matcher1_ or matcher2_ doesn't match x, we only need |
| 1300 |
// to explain why one of them fails. |
| 1301 |
std::string all_match_result; |
| 1302 |
|
| 1303 |
for (size_t i = 0; i < matchers_.size(); ++i) { |
| 1304 |
StringMatchResultListener slistener; |
| 1305 |
if (matchers_[i].MatchAndExplain(x, &slistener)) { |
| 1306 |
if (all_match_result.empty()) { |
| 1307 |
all_match_result = slistener.str(); |
| 1308 |
} else { |
| 1309 |
std::string result = slistener.str(); |
| 1310 |
if (!result.empty()) { |
| 1311 |
all_match_result += ", and "; |
| 1312 |
all_match_result += result; |
| 1313 |
} |
| 1314 |
} |
| 1315 |
} else { |
| 1316 |
*listener << slistener.str(); |
| 1317 |
return false; |
| 1318 |
} |
| 1319 |
} |
| 1320 |
|
| 1321 |
// Otherwise we need to explain why *both* of them match. |
| 1322 |
*listener << all_match_result; |
| 1323 |
return true; |
| 1324 |
} |
| 1325 |
|
| 1326 |
private: |
| 1327 |
const std::vector<Matcher<T>> matchers_; |
| 1328 |
}; |
| 1329 |
|
| 1330 |
// VariadicMatcher is used for the variadic implementation of |
| 1331 |
// AllOf(m_1, m_2, ...) and AnyOf(m_1, m_2, ...). |
| 1332 |
// CombiningMatcher<T> is used to recursively combine the provided matchers |
| 1333 |
// (of type Args...). |
| 1334 |
template <template <typename T> class CombiningMatcher, typename... Args> |
| 1335 |
class VariadicMatcher { |
| 1336 |
public: |
| 1337 |
VariadicMatcher(const Args&... matchers) // NOLINT |
| 1338 |
: matchers_(matchers...) { |
| 1339 |
static_assert(sizeof...(Args) > 0, "Must have at least one matcher."); |
| 1340 |
} |
| 1341 |
|
| 1342 |
VariadicMatcher(const VariadicMatcher&) = default; |
| 1343 |
VariadicMatcher& operator=(const VariadicMatcher&) = delete; |
| 1344 |
|
| 1345 |
// This template type conversion operator allows an |
| 1346 |
// VariadicMatcher<Matcher1, Matcher2...> object to match any type that |
| 1347 |
// all of the provided matchers (Matcher1, Matcher2, ...) can match. |
| 1348 |
template <typename T> |
| 1349 |
operator Matcher<T>() const { |
| 1350 |
std::vector<Matcher<T>> values; |
| 1351 |
CreateVariadicMatcher<T>(&values, std::integral_constant<size_t, 0>()); |
| 1352 |
return Matcher<T>(new CombiningMatcher<T>(std::move(values))); |
| 1353 |
} |
| 1354 |
|
| 1355 |
private: |
| 1356 |
template <typename T, size_t I> |
| 1357 |
void CreateVariadicMatcher(std::vector<Matcher<T>>* values, |
| 1358 |
std::integral_constant<size_t, I>) const { |
| 1359 |
values->push_back(SafeMatcherCast<T>(std::get<I>(matchers_))); |
| 1360 |
CreateVariadicMatcher<T>(values, std::integral_constant<size_t, I + 1>()); |
| 1361 |
} |
| 1362 |
|
| 1363 |
template <typename T> |
| 1364 |
void CreateVariadicMatcher( |
| 1365 |
std::vector<Matcher<T>>*, |
| 1366 |
std::integral_constant<size_t, sizeof...(Args)>) const {} |
| 1367 |
|
| 1368 |
std::tuple<Args...> matchers_; |
| 1369 |
}; |
| 1370 |
|
| 1371 |
template <typename... Args> |
| 1372 |
using AllOfMatcher = VariadicMatcher<AllOfMatcherImpl, Args...>; |
| 1373 |
|
| 1374 |
// Implements the AnyOf(m1, m2) matcher for a particular argument type |
| 1375 |
// T. We do not nest it inside the AnyOfMatcher class template, as |
| 1376 |
// that will prevent different instantiations of AnyOfMatcher from |
| 1377 |
// sharing the same EitherOfMatcherImpl<T> class. |
| 1378 |
template <typename T> |
| 1379 |
class AnyOfMatcherImpl : public MatcherInterface<const T&> { |
| 1380 |
public: |
| 1381 |
explicit AnyOfMatcherImpl(std::vector<Matcher<T>> matchers) |
| 1382 |
: matchers_(std::move(matchers)) {} |
| 1383 |
|
| 1384 |
void DescribeTo(::std::ostream* os) const override { |
| 1385 |
*os << "("; |
| 1386 |
for (size_t i = 0; i < matchers_.size(); ++i) { |
| 1387 |
if (i != 0) *os << ") or ("; |
| 1388 |
matchers_[i].DescribeTo(os); |
| 1389 |
} |
| 1390 |
*os << ")"; |
| 1391 |
} |
| 1392 |
|
| 1393 |
void DescribeNegationTo(::std::ostream* os) const override { |
| 1394 |
*os << "("; |
| 1395 |
for (size_t i = 0; i < matchers_.size(); ++i) { |
| 1396 |
if (i != 0) *os << ") and ("; |
| 1397 |
matchers_[i].DescribeNegationTo(os); |
| 1398 |
} |
| 1399 |
*os << ")"; |
| 1400 |
} |
| 1401 |
|
| 1402 |
bool MatchAndExplain(const T& x, |
| 1403 |
MatchResultListener* listener) const override { |
| 1404 |
std::string no_match_result; |
| 1405 |
|
| 1406 |
// If either matcher1_ or matcher2_ matches x, we just need to |
| 1407 |
// explain why *one* of them matches. |
| 1408 |
for (size_t i = 0; i < matchers_.size(); ++i) { |
| 1409 |
StringMatchResultListener slistener; |
| 1410 |
if (matchers_[i].MatchAndExplain(x, &slistener)) { |
| 1411 |
*listener << slistener.str(); |
| 1412 |
return true; |
| 1413 |
} else { |
| 1414 |
if (no_match_result.empty()) { |
| 1415 |
no_match_result = slistener.str(); |
| 1416 |
} else { |
| 1417 |
std::string result = slistener.str(); |
| 1418 |
if (!result.empty()) { |
| 1419 |
no_match_result += ", and "; |
| 1420 |
no_match_result += result; |
| 1421 |
} |
| 1422 |
} |
| 1423 |
} |
| 1424 |
} |
| 1425 |
|
| 1426 |
// Otherwise we need to explain why *both* of them fail. |
| 1427 |
*listener << no_match_result; |
| 1428 |
return false; |
| 1429 |
} |
| 1430 |
|
| 1431 |
private: |
| 1432 |
const std::vector<Matcher<T>> matchers_; |
| 1433 |
}; |
| 1434 |
|
| 1435 |
// AnyOfMatcher is used for the variadic implementation of AnyOf(m_1, m_2, ...). |
| 1436 |
template <typename... Args> |
| 1437 |
using AnyOfMatcher = VariadicMatcher<AnyOfMatcherImpl, Args...>; |
| 1438 |
|
| 1439 |
// ConditionalMatcher is the implementation of Conditional(cond, m1, m2) |
| 1440 |
template <typename MatcherTrue, typename MatcherFalse> |
| 1441 |
class ConditionalMatcher { |
| 1442 |
public: |
| 1443 |
ConditionalMatcher(bool condition, MatcherTrue matcher_true, |
| 1444 |
MatcherFalse matcher_false) |
| 1445 |
: condition_(condition), |
| 1446 |
matcher_true_(std::move(matcher_true)), |
| 1447 |
matcher_false_(std::move(matcher_false)) {} |
| 1448 |
|
| 1449 |
template <typename T> |
| 1450 |
operator Matcher<T>() const { // NOLINT(runtime/explicit) |
| 1451 |
return condition_ ? SafeMatcherCast<T>(matcher_true_) |
| 1452 |
: SafeMatcherCast<T>(matcher_false_); |
| 1453 |
} |
| 1454 |
|
| 1455 |
private: |
| 1456 |
bool condition_; |
| 1457 |
MatcherTrue matcher_true_; |
| 1458 |
MatcherFalse matcher_false_; |
| 1459 |
}; |
| 1460 |
|
| 1461 |
// Wrapper for implementation of Any/AllOfArray(). |
| 1462 |
template <template <class> class MatcherImpl, typename T> |
| 1463 |
class SomeOfArrayMatcher { |
| 1464 |
public: |
| 1465 |
// Constructs the matcher from a sequence of element values or |
| 1466 |
// element matchers. |
| 1467 |
template <typename Iter> |
| 1468 |
SomeOfArrayMatcher(Iter first, Iter last) : matchers_(first, last) {} |
| 1469 |
|
| 1470 |
template <typename U> |
| 1471 |
operator Matcher<U>() const { // NOLINT |
| 1472 |
using RawU = typename std::decay<U>::type; |
| 1473 |
std::vector<Matcher<RawU>> matchers; |
| 1474 |
for (const auto& matcher : matchers_) { |
| 1475 |
matchers.push_back(MatcherCast<RawU>(matcher)); |
| 1476 |
} |
| 1477 |
return Matcher<U>(new MatcherImpl<RawU>(std::move(matchers))); |
| 1478 |
} |
| 1479 |
|
| 1480 |
private: |
| 1481 |
const ::std::vector<T> matchers_; |
| 1482 |
}; |
| 1483 |
|
| 1484 |
template <typename T> |
| 1485 |
using AllOfArrayMatcher = SomeOfArrayMatcher<AllOfMatcherImpl, T>; |
| 1486 |
|
| 1487 |
template <typename T> |
| 1488 |
using AnyOfArrayMatcher = SomeOfArrayMatcher<AnyOfMatcherImpl, T>; |
| 1489 |
|
| 1490 |
// Used for implementing Truly(pred), which turns a predicate into a |
| 1491 |
// matcher. |
| 1492 |
template <typename Predicate> |
| 1493 |
class TrulyMatcher { |
| 1494 |
public: |
| 1495 |
explicit TrulyMatcher(Predicate pred) : predicate_(pred) {} |
| 1496 |
|
| 1497 |
// This method template allows Truly(pred) to be used as a matcher |
| 1498 |
// for type T where T is the argument type of predicate 'pred'. The |
| 1499 |
// argument is passed by reference as the predicate may be |
| 1500 |
// interested in the address of the argument. |
| 1501 |
template <typename T> |
| 1502 |
bool MatchAndExplain(T& x, // NOLINT |
| 1503 |
MatchResultListener* listener) const { |
| 1504 |
// Without the if-statement, MSVC sometimes warns about converting |
| 1505 |
// a value to bool (warning 4800). |
| 1506 |
// |
| 1507 |
// We cannot write 'return !!predicate_(x);' as that doesn't work |
| 1508 |
// when predicate_(x) returns a class convertible to bool but |
| 1509 |
// having no operator!(). |
| 1510 |
if (predicate_(x)) return true; |
| 1511 |
*listener << "didn't satisfy the given predicate"; |
| 1512 |
return false; |
| 1513 |
} |
| 1514 |
|
| 1515 |
void DescribeTo(::std::ostream* os) const { |
| 1516 |
*os << "satisfies the given predicate"; |
| 1517 |
} |
| 1518 |
|
| 1519 |
void DescribeNegationTo(::std::ostream* os) const { |
| 1520 |
*os << "doesn't satisfy the given predicate"; |
| 1521 |
} |
| 1522 |
|
| 1523 |
private: |
| 1524 |
Predicate predicate_; |
| 1525 |
}; |
| 1526 |
|
| 1527 |
// Used for implementing Matches(matcher), which turns a matcher into |
| 1528 |
// a predicate. |
| 1529 |
template <typename M> |
| 1530 |
class MatcherAsPredicate { |
| 1531 |
public: |
| 1532 |
explicit MatcherAsPredicate(M matcher) : matcher_(matcher) {} |
| 1533 |
|
| 1534 |
// This template operator() allows Matches(m) to be used as a |
| 1535 |
// predicate on type T where m is a matcher on type T. |
| 1536 |
// |
| 1537 |
// The argument x is passed by reference instead of by value, as |
| 1538 |
// some matcher may be interested in its address (e.g. as in |
| 1539 |
// Matches(Ref(n))(x)). |
| 1540 |
template <typename T> |
| 1541 |
bool operator()(const T& x) const { |
| 1542 |
// We let matcher_ commit to a particular type here instead of |
| 1543 |
// when the MatcherAsPredicate object was constructed. This |
| 1544 |
// allows us to write Matches(m) where m is a polymorphic matcher |
| 1545 |
// (e.g. Eq(5)). |
| 1546 |
// |
| 1547 |
// If we write Matcher<T>(matcher_).Matches(x) here, it won't |
| 1548 |
// compile when matcher_ has type Matcher<const T&>; if we write |
| 1549 |
// Matcher<const T&>(matcher_).Matches(x) here, it won't compile |
| 1550 |
// when matcher_ has type Matcher<T>; if we just write |
| 1551 |
// matcher_.Matches(x), it won't compile when matcher_ is |
| 1552 |
// polymorphic, e.g. Eq(5). |
| 1553 |
// |
| 1554 |
// MatcherCast<const T&>() is necessary for making the code work |
| 1555 |
// in all of the above situations. |
| 1556 |
return MatcherCast<const T&>(matcher_).Matches(x); |
| 1557 |
} |
| 1558 |
|
| 1559 |
private: |
| 1560 |
M matcher_; |
| 1561 |
}; |
| 1562 |
|
| 1563 |
// For implementing ASSERT_THAT() and EXPECT_THAT(). The template |
| 1564 |
// argument M must be a type that can be converted to a matcher. |
| 1565 |
template <typename M> |
| 1566 |
class PredicateFormatterFromMatcher { |
| 1567 |
public: |
| 1568 |
explicit PredicateFormatterFromMatcher(M m) : matcher_(std::move(m)) {} |
| 1569 |
|
| 1570 |
// This template () operator allows a PredicateFormatterFromMatcher |
| 1571 |
// object to act as a predicate-formatter suitable for using with |
| 1572 |
// Google Test's EXPECT_PRED_FORMAT1() macro. |
| 1573 |
template <typename T> |
| 1574 |
AssertionResult operator()(const char* value_text, const T& x) const { |
| 1575 |
// We convert matcher_ to a Matcher<const T&> *now* instead of |
| 1576 |
// when the PredicateFormatterFromMatcher object was constructed, |
| 1577 |
// as matcher_ may be polymorphic (e.g. NotNull()) and we won't |
| 1578 |
// know which type to instantiate it to until we actually see the |
| 1579 |
// type of x here. |
| 1580 |
// |
| 1581 |
// We write SafeMatcherCast<const T&>(matcher_) instead of |
| 1582 |
// Matcher<const T&>(matcher_), as the latter won't compile when |
| 1583 |
// matcher_ has type Matcher<T> (e.g. An<int>()). |
| 1584 |
// We don't write MatcherCast<const T&> either, as that allows |
| 1585 |
// potentially unsafe downcasting of the matcher argument. |
| 1586 |
const Matcher<const T&> matcher = SafeMatcherCast<const T&>(matcher_); |
| 1587 |
|
| 1588 |
// The expected path here is that the matcher should match (i.e. that most |
| 1589 |
// tests pass) so optimize for this case. |
| 1590 |
if (matcher.Matches(x)) { |
| 1591 |
return AssertionSuccess(); |
| 1592 |
} |
| 1593 |
|
| 1594 |
::std::stringstream ss; |
| 1595 |
ss << "Value of: " << value_text << "\n" |
| 1596 |
<< "Expected: "; |
| 1597 |
matcher.DescribeTo(&ss); |
| 1598 |
|
| 1599 |
// Rerun the matcher to "PrintAndExplain" the failure. |
| 1600 |
StringMatchResultListener listener; |
| 1601 |
if (MatchPrintAndExplain(x, matcher, &listener)) { |
| 1602 |
ss << "\n The matcher failed on the initial attempt; but passed when " |
| 1603 |
"rerun to generate the explanation."; |
| 1604 |
} |
| 1605 |
ss << "\n Actual: " << listener.str(); |
| 1606 |
return AssertionFailure() << ss.str(); |
| 1607 |
} |
| 1608 |
|
| 1609 |
private: |
| 1610 |
const M matcher_; |
| 1611 |
}; |
| 1612 |
|
| 1613 |
// A helper function for converting a matcher to a predicate-formatter |
| 1614 |
// without the user needing to explicitly write the type. This is |
| 1615 |
// used for implementing ASSERT_THAT() and EXPECT_THAT(). |
| 1616 |
// Implementation detail: 'matcher' is received by-value to force decaying. |
| 1617 |
template <typename M> |
| 1618 |
inline PredicateFormatterFromMatcher<M> MakePredicateFormatterFromMatcher( |
| 1619 |
M matcher) { |
| 1620 |
return PredicateFormatterFromMatcher<M>(std::move(matcher)); |
| 1621 |
} |
| 1622 |
|
| 1623 |
// Implements the polymorphic IsNan() matcher, which matches any floating type |
| 1624 |
// value that is Nan. |
| 1625 |
class IsNanMatcher { |
| 1626 |
public: |
| 1627 |
template <typename FloatType> |
| 1628 |
bool MatchAndExplain(const FloatType& f, |
| 1629 |
MatchResultListener* /* listener */) const { |
| 1630 |
return (::std::isnan)(f); |
| 1631 |
} |
| 1632 |
|
| 1633 |
void DescribeTo(::std::ostream* os) const { *os << "is NaN"; } |
| 1634 |
void DescribeNegationTo(::std::ostream* os) const { *os << "isn't NaN"; } |
| 1635 |
}; |
| 1636 |
|
| 1637 |
// Implements the polymorphic floating point equality matcher, which matches |
| 1638 |
// two float values using ULP-based approximation or, optionally, a |
| 1639 |
// user-specified epsilon. The template is meant to be instantiated with |
| 1640 |
// FloatType being either float or double. |
| 1641 |
template <typename FloatType> |
| 1642 |
class FloatingEqMatcher { |
| 1643 |
public: |
| 1644 |
// Constructor for FloatingEqMatcher. |
| 1645 |
// The matcher's input will be compared with expected. The matcher treats two |
| 1646 |
// NANs as equal if nan_eq_nan is true. Otherwise, under IEEE standards, |
| 1647 |
// equality comparisons between NANs will always return false. We specify a |
| 1648 |
// negative max_abs_error_ term to indicate that ULP-based approximation will |
| 1649 |
// be used for comparison. |
| 1650 |
FloatingEqMatcher(FloatType expected, bool nan_eq_nan) |
| 1651 |
: expected_(expected), nan_eq_nan_(nan_eq_nan), max_abs_error_(-1) {} |
| 1652 |
|
| 1653 |
// Constructor that supports a user-specified max_abs_error that will be used |
| 1654 |
// for comparison instead of ULP-based approximation. The max absolute |
| 1655 |
// should be non-negative. |
| 1656 |
FloatingEqMatcher(FloatType expected, bool nan_eq_nan, |
| 1657 |
FloatType max_abs_error) |
| 1658 |
: expected_(expected), |
| 1659 |
nan_eq_nan_(nan_eq_nan), |
| 1660 |
max_abs_error_(max_abs_error) { |
| 1661 |
GTEST_CHECK_(max_abs_error >= 0) |
| 1662 |
<< ", where max_abs_error is" << max_abs_error; |
| 1663 |
} |
| 1664 |
|
| 1665 |
// Implements floating point equality matcher as a Matcher<T>. |
| 1666 |
template <typename T> |
| 1667 |
class Impl : public MatcherInterface<T> { |
| 1668 |
public: |
| 1669 |
Impl(FloatType expected, bool nan_eq_nan, FloatType max_abs_error) |
| 1670 |
: expected_(expected), |
| 1671 |
nan_eq_nan_(nan_eq_nan), |
| 1672 |
max_abs_error_(max_abs_error) {} |
| 1673 |
|
| 1674 |
bool MatchAndExplain(T value, |
| 1675 |
MatchResultListener* listener) const override { |
| 1676 |
const FloatingPoint<FloatType> actual(value), expected(expected_); |
| 1677 |
|
| 1678 |
// Compares NaNs first, if nan_eq_nan_ is true. |
| 1679 |
if (actual.is_nan() || expected.is_nan()) { |
| 1680 |
if (actual.is_nan() && expected.is_nan()) { |
| 1681 |
return nan_eq_nan_; |
| 1682 |
} |
| 1683 |
// One is nan; the other is not nan. |
| 1684 |
return false; |
| 1685 |
} |
| 1686 |
if (HasMaxAbsError()) { |
| 1687 |
// We perform an equality check so that inf will match inf, regardless |
| 1688 |
// of error bounds. If the result of value - expected_ would result in |
| 1689 |
// overflow or if either value is inf, the default result is infinity, |
| 1690 |
// which should only match if max_abs_error_ is also infinity. |
| 1691 |
if (value == expected_) { |
| 1692 |
return true; |
| 1693 |
} |
| 1694 |
|
| 1695 |
const FloatType diff = value - expected_; |
| 1696 |
if (::std::fabs(diff) <= max_abs_error_) { |
| 1697 |
return true; |
| 1698 |
} |
| 1699 |
|
| 1700 |
if (listener->IsInterested()) { |
| 1701 |
*listener << "which is " << diff << " from " << expected_; |
| 1702 |
} |
| 1703 |
return false; |
| 1704 |
} else { |
| 1705 |
return actual.AlmostEquals(expected); |
| 1706 |
} |
| 1707 |
} |
| 1708 |
|
| 1709 |
void DescribeTo(::std::ostream* os) const override { |
| 1710 |
// os->precision() returns the previously set precision, which we |
| 1711 |
// store to restore the ostream to its original configuration |
| 1712 |
// after outputting. |
| 1713 |
const ::std::streamsize old_precision = |
| 1714 |
os->precision(::std::numeric_limits<FloatType>::digits10 + 2); |
| 1715 |
if (FloatingPoint<FloatType>(expected_).is_nan()) { |
| 1716 |
if (nan_eq_nan_) { |
| 1717 |
*os << "is NaN"; |
| 1718 |
} else { |
| 1719 |
*os << "never matches"; |
| 1720 |
} |
| 1721 |
} else { |
| 1722 |
*os << "is approximately " << expected_; |
| 1723 |
if (HasMaxAbsError()) { |
| 1724 |
*os << " (absolute error <= " << max_abs_error_ << ")"; |
| 1725 |
} |
| 1726 |
} |
| 1727 |
os->precision(old_precision); |
| 1728 |
} |
| 1729 |
|
| 1730 |
void DescribeNegationTo(::std::ostream* os) const override { |
| 1731 |
// As before, get original precision. |
| 1732 |
const ::std::streamsize old_precision = |
| 1733 |
os->precision(::std::numeric_limits<FloatType>::digits10 + 2); |
| 1734 |
if (FloatingPoint<FloatType>(expected_).is_nan()) { |
| 1735 |
if (nan_eq_nan_) { |
| 1736 |
*os << "isn't NaN"; |
| 1737 |
} else { |
| 1738 |
*os << "is anything"; |
| 1739 |
} |
| 1740 |
} else { |
| 1741 |
*os << "isn't approximately " << expected_; |
| 1742 |
if (HasMaxAbsError()) { |
| 1743 |
*os << " (absolute error > " << max_abs_error_ << ")"; |
| 1744 |
} |
| 1745 |
} |
| 1746 |
// Restore original precision. |
| 1747 |
os->precision(old_precision); |
| 1748 |
} |
| 1749 |
|
| 1750 |
private: |
| 1751 |
bool HasMaxAbsError() const { return max_abs_error_ >= 0; } |
| 1752 |
|
| 1753 |
const FloatType expected_; |
| 1754 |
const bool nan_eq_nan_; |
| 1755 |
// max_abs_error will be used for value comparison when >= 0. |
| 1756 |
const FloatType max_abs_error_; |
| 1757 |
}; |
| 1758 |
|
| 1759 |
// The following 3 type conversion operators allow FloatEq(expected) and |
| 1760 |
// NanSensitiveFloatEq(expected) to be used as a Matcher<float>, a |
| 1761 |
// Matcher<const float&>, or a Matcher<float&>, but nothing else. |
| 1762 |
operator Matcher<FloatType>() const { |
| 1763 |
return MakeMatcher( |
| 1764 |
new Impl<FloatType>(expected_, nan_eq_nan_, max_abs_error_)); |
| 1765 |
} |
| 1766 |
|
| 1767 |
operator Matcher<const FloatType&>() const { |
| 1768 |
return MakeMatcher( |
| 1769 |
new Impl<const FloatType&>(expected_, nan_eq_nan_, max_abs_error_)); |
| 1770 |
} |
| 1771 |
|
| 1772 |
operator Matcher<FloatType&>() const { |
| 1773 |
return MakeMatcher( |
| 1774 |
new Impl<FloatType&>(expected_, nan_eq_nan_, max_abs_error_)); |
| 1775 |
} |
| 1776 |
|
| 1777 |
private: |
| 1778 |
const FloatType expected_; |
| 1779 |
const bool nan_eq_nan_; |
| 1780 |
// max_abs_error will be used for value comparison when >= 0. |
| 1781 |
const FloatType max_abs_error_; |
| 1782 |
}; |
| 1783 |
|
| 1784 |
// A 2-tuple ("binary") wrapper around FloatingEqMatcher: |
| 1785 |
// FloatingEq2Matcher() matches (x, y) by matching FloatingEqMatcher(x, false) |
| 1786 |
// against y, and FloatingEq2Matcher(e) matches FloatingEqMatcher(x, false, e) |
| 1787 |
// against y. The former implements "Eq", the latter "Near". At present, there |
| 1788 |
// is no version that compares NaNs as equal. |
| 1789 |
template <typename FloatType> |
| 1790 |
class FloatingEq2Matcher { |
| 1791 |
public: |
| 1792 |
FloatingEq2Matcher() { Init(-1, false); } |
| 1793 |
|
| 1794 |
explicit FloatingEq2Matcher(bool nan_eq_nan) { Init(-1, nan_eq_nan); } |
| 1795 |
|
| 1796 |
explicit FloatingEq2Matcher(FloatType max_abs_error) { |
| 1797 |
Init(max_abs_error, false); |
| 1798 |
} |
| 1799 |
|
| 1800 |
FloatingEq2Matcher(FloatType max_abs_error, bool nan_eq_nan) { |
| 1801 |
Init(max_abs_error, nan_eq_nan); |
| 1802 |
} |
| 1803 |
|
| 1804 |
template <typename T1, typename T2> |
| 1805 |
operator Matcher<::std::tuple<T1, T2>>() const { |
| 1806 |
return MakeMatcher( |
| 1807 |
new Impl<::std::tuple<T1, T2>>(max_abs_error_, nan_eq_nan_)); |
| 1808 |
} |
| 1809 |
template <typename T1, typename T2> |
| 1810 |
operator Matcher<const ::std::tuple<T1, T2>&>() const { |
| 1811 |
return MakeMatcher( |
| 1812 |
new Impl<const ::std::tuple<T1, T2>&>(max_abs_error_, nan_eq_nan_)); |
| 1813 |
} |
| 1814 |
|
| 1815 |
private: |
| 1816 |
static ::std::ostream& GetDesc(::std::ostream& os) { // NOLINT |
| 1817 |
return os << "an almost-equal pair"; |
| 1818 |
} |
| 1819 |
|
| 1820 |
template <typename Tuple> |
| 1821 |
class Impl : public MatcherInterface<Tuple> { |
| 1822 |
public: |
| 1823 |
Impl(FloatType max_abs_error, bool nan_eq_nan) |
| 1824 |
: max_abs_error_(max_abs_error), nan_eq_nan_(nan_eq_nan) {} |
| 1825 |
|
| 1826 |
bool MatchAndExplain(Tuple args, |
| 1827 |
MatchResultListener* listener) const override { |
| 1828 |
if (max_abs_error_ == -1) { |
| 1829 |
FloatingEqMatcher<FloatType> fm(::std::get<0>(args), nan_eq_nan_); |
| 1830 |
return static_cast<Matcher<FloatType>>(fm).MatchAndExplain( |
| 1831 |
::std::get<1>(args), listener); |
| 1832 |
} else { |
| 1833 |
FloatingEqMatcher<FloatType> fm(::std::get<0>(args), nan_eq_nan_, |
| 1834 |
max_abs_error_); |
| 1835 |
return static_cast<Matcher<FloatType>>(fm).MatchAndExplain( |
| 1836 |
::std::get<1>(args), listener); |
| 1837 |
} |
| 1838 |
} |
| 1839 |
void DescribeTo(::std::ostream* os) const override { |
| 1840 |
*os << "are " << GetDesc; |
| 1841 |
} |
| 1842 |
void DescribeNegationTo(::std::ostream* os) const override { |
| 1843 |
*os << "aren't " << GetDesc; |
| 1844 |
} |
| 1845 |
|
| 1846 |
private: |
| 1847 |
FloatType max_abs_error_; |
| 1848 |
const bool nan_eq_nan_; |
| 1849 |
}; |
| 1850 |
|
| 1851 |
void Init(FloatType max_abs_error_val, bool nan_eq_nan_val) { |
| 1852 |
max_abs_error_ = max_abs_error_val; |
| 1853 |
nan_eq_nan_ = nan_eq_nan_val; |
| 1854 |
} |
| 1855 |
FloatType max_abs_error_; |
| 1856 |
bool nan_eq_nan_; |
| 1857 |
}; |
| 1858 |
|
| 1859 |
// Implements the Pointee(m) matcher for matching a pointer whose |
| 1860 |
// pointee matches matcher m. The pointer can be either raw or smart. |
| 1861 |
template <typename InnerMatcher> |
| 1862 |
class PointeeMatcher { |
| 1863 |
public: |
| 1864 |
explicit PointeeMatcher(const InnerMatcher& matcher) : matcher_(matcher) {} |
| 1865 |
|
| 1866 |
// This type conversion operator template allows Pointee(m) to be |
| 1867 |
// used as a matcher for any pointer type whose pointee type is |
| 1868 |
// compatible with the inner matcher, where type Pointer can be |
| 1869 |
// either a raw pointer or a smart pointer. |
| 1870 |
// |
| 1871 |
// The reason we do this instead of relying on |
| 1872 |
// MakePolymorphicMatcher() is that the latter is not flexible |
| 1873 |
// enough for implementing the DescribeTo() method of Pointee(). |
| 1874 |
template <typename Pointer> |
| 1875 |
operator Matcher<Pointer>() const { |
| 1876 |
return Matcher<Pointer>(new Impl<const Pointer&>(matcher_)); |
| 1877 |
} |
| 1878 |
|
| 1879 |
private: |
| 1880 |
// The monomorphic implementation that works for a particular pointer type. |
| 1881 |
template <typename Pointer> |
| 1882 |
class Impl : public MatcherInterface<Pointer> { |
| 1883 |
public: |
| 1884 |
using Pointee = |
| 1885 |
typename std::pointer_traits<GTEST_REMOVE_REFERENCE_AND_CONST_( |
| 1886 |
Pointer)>::element_type; |
| 1887 |
|
| 1888 |
explicit Impl(const InnerMatcher& matcher) |
| 1889 |
: matcher_(MatcherCast<const Pointee&>(matcher)) {} |
| 1890 |
|
| 1891 |
void DescribeTo(::std::ostream* os) const override { |
| 1892 |
*os << "points to a value that "; |
| 1893 |
matcher_.DescribeTo(os); |
| 1894 |
} |
| 1895 |
|
| 1896 |
void DescribeNegationTo(::std::ostream* os) const override { |
| 1897 |
*os << "does not point to a value that "; |
| 1898 |
matcher_.DescribeTo(os); |
| 1899 |
} |
| 1900 |
|
| 1901 |
bool MatchAndExplain(Pointer pointer, |
| 1902 |
MatchResultListener* listener) const override { |
| 1903 |
if (GetRawPointer(pointer) == nullptr) return false; |
| 1904 |
|
| 1905 |
*listener << "which points to "; |
| 1906 |
return MatchPrintAndExplain(*pointer, matcher_, listener); |
| 1907 |
} |
| 1908 |
|
| 1909 |
private: |
| 1910 |
const Matcher<const Pointee&> matcher_; |
| 1911 |
}; |
| 1912 |
|
| 1913 |
const InnerMatcher matcher_; |
| 1914 |
}; |
| 1915 |
|
| 1916 |
// Implements the Pointer(m) matcher |
| 1917 |
// Implements the Pointer(m) matcher for matching a pointer that matches matcher |
| 1918 |
// m. The pointer can be either raw or smart, and will match `m` against the |
| 1919 |
// raw pointer. |
| 1920 |
template <typename InnerMatcher> |
| 1921 |
class PointerMatcher { |
| 1922 |
public: |
| 1923 |
explicit PointerMatcher(const InnerMatcher& matcher) : matcher_(matcher) {} |
| 1924 |
|
| 1925 |
// This type conversion operator template allows Pointer(m) to be |
| 1926 |
// used as a matcher for any pointer type whose pointer type is |
| 1927 |
// compatible with the inner matcher, where type PointerType can be |
| 1928 |
// either a raw pointer or a smart pointer. |
| 1929 |
// |
| 1930 |
// The reason we do this instead of relying on |
| 1931 |
// MakePolymorphicMatcher() is that the latter is not flexible |
| 1932 |
// enough for implementing the DescribeTo() method of Pointer(). |
| 1933 |
template <typename PointerType> |
| 1934 |
operator Matcher<PointerType>() const { // NOLINT |
| 1935 |
return Matcher<PointerType>(new Impl<const PointerType&>(matcher_)); |
| 1936 |
} |
| 1937 |
|
| 1938 |
private: |
| 1939 |
// The monomorphic implementation that works for a particular pointer type. |
| 1940 |
template <typename PointerType> |
| 1941 |
class Impl : public MatcherInterface<PointerType> { |
| 1942 |
public: |
| 1943 |
using Pointer = |
| 1944 |
const typename std::pointer_traits<GTEST_REMOVE_REFERENCE_AND_CONST_( |
| 1945 |
PointerType)>::element_type*; |
| 1946 |
|
| 1947 |
explicit Impl(const InnerMatcher& matcher) |
| 1948 |
: matcher_(MatcherCast<Pointer>(matcher)) {} |
| 1949 |
|
| 1950 |
void DescribeTo(::std::ostream* os) const override { |
| 1951 |
*os << "is a pointer that "; |
| 1952 |
matcher_.DescribeTo(os); |
| 1953 |
} |
| 1954 |
|
| 1955 |
void DescribeNegationTo(::std::ostream* os) const override { |
| 1956 |
*os << "is not a pointer that "; |
| 1957 |
matcher_.DescribeTo(os); |
| 1958 |
} |
| 1959 |
|
| 1960 |
bool MatchAndExplain(PointerType pointer, |
| 1961 |
MatchResultListener* listener) const override { |
| 1962 |
*listener << "which is a pointer that "; |
| 1963 |
Pointer p = GetRawPointer(pointer); |
| 1964 |
return MatchPrintAndExplain(p, matcher_, listener); |
| 1965 |
} |
| 1966 |
|
| 1967 |
private: |
| 1968 |
Matcher<Pointer> matcher_; |
| 1969 |
}; |
| 1970 |
|
| 1971 |
const InnerMatcher matcher_; |
| 1972 |
}; |
| 1973 |
|
| 1974 |
#if GTEST_HAS_RTTI |
| 1975 |
// Implements the WhenDynamicCastTo<T>(m) matcher that matches a pointer or |
| 1976 |
// reference that matches inner_matcher when dynamic_cast<T> is applied. |
| 1977 |
// The result of dynamic_cast<To> is forwarded to the inner matcher. |
| 1978 |
// If To is a pointer and the cast fails, the inner matcher will receive NULL. |
| 1979 |
// If To is a reference and the cast fails, this matcher returns false |
| 1980 |
// immediately. |
| 1981 |
template <typename To> |
| 1982 |
class WhenDynamicCastToMatcherBase { |
| 1983 |
public: |
| 1984 |
explicit WhenDynamicCastToMatcherBase(const Matcher<To>& matcher) |
| 1985 |
: matcher_(matcher) {} |
| 1986 |
|
| 1987 |
void DescribeTo(::std::ostream* os) const { |
| 1988 |
GetCastTypeDescription(os); |
| 1989 |
matcher_.DescribeTo(os); |
| 1990 |
} |
| 1991 |
|
| 1992 |
void DescribeNegationTo(::std::ostream* os) const { |
| 1993 |
GetCastTypeDescription(os); |
| 1994 |
matcher_.DescribeNegationTo(os); |
| 1995 |
} |
| 1996 |
|
| 1997 |
protected: |
| 1998 |
const Matcher<To> matcher_; |
| 1999 |
|
| 2000 |
static std::string GetToName() { return GetTypeName<To>(); } |
| 2001 |
|
| 2002 |
private: |
| 2003 |
static void GetCastTypeDescription(::std::ostream* os) { |
| 2004 |
*os << "when dynamic_cast to " << GetToName() << ", "; |
| 2005 |
} |
| 2006 |
}; |
| 2007 |
|
| 2008 |
// Primary template. |
| 2009 |
// To is a pointer. Cast and forward the result. |
| 2010 |
template <typename To> |
| 2011 |
class WhenDynamicCastToMatcher : public WhenDynamicCastToMatcherBase<To> { |
| 2012 |
public: |
| 2013 |
explicit WhenDynamicCastToMatcher(const Matcher<To>& matcher) |
| 2014 |
: WhenDynamicCastToMatcherBase<To>(matcher) {} |
| 2015 |
|
| 2016 |
template <typename From> |
| 2017 |
bool MatchAndExplain(From from, MatchResultListener* listener) const { |
| 2018 |
To to = dynamic_cast<To>(from); |
| 2019 |
return MatchPrintAndExplain(to, this->matcher_, listener); |
| 2020 |
} |
| 2021 |
}; |
| 2022 |
|
| 2023 |
// Specialize for references. |
| 2024 |
// In this case we return false if the dynamic_cast fails. |
| 2025 |
template <typename To> |
| 2026 |
class WhenDynamicCastToMatcher<To&> : public WhenDynamicCastToMatcherBase<To&> { |
| 2027 |
public: |
| 2028 |
explicit WhenDynamicCastToMatcher(const Matcher<To&>& matcher) |
| 2029 |
: WhenDynamicCastToMatcherBase<To&>(matcher) {} |
| 2030 |
|
| 2031 |
template <typename From> |
| 2032 |
bool MatchAndExplain(From& from, MatchResultListener* listener) const { |
| 2033 |
// We don't want an std::bad_cast here, so do the cast with pointers. |
| 2034 |
To* to = dynamic_cast<To*>(&from); |
| 2035 |
if (to == nullptr) { |
| 2036 |
*listener << "which cannot be dynamic_cast to " << this->GetToName(); |
| 2037 |
return false; |
| 2038 |
} |
| 2039 |
return MatchPrintAndExplain(*to, this->matcher_, listener); |
| 2040 |
} |
| 2041 |
}; |
| 2042 |
#endif // GTEST_HAS_RTTI |
| 2043 |
|
| 2044 |
// Implements the Field() matcher for matching a field (i.e. member |
| 2045 |
// variable) of an object. |
| 2046 |
template <typename Class, typename FieldType> |
| 2047 |
class FieldMatcher { |
| 2048 |
public: |
| 2049 |
FieldMatcher(FieldType Class::*field, |
| 2050 |
const Matcher<const FieldType&>& matcher) |
| 2051 |
: field_(field), matcher_(matcher), whose_field_("whose given field ") {} |
| 2052 |
|
| 2053 |
FieldMatcher(const std::string& field_name, FieldType Class::*field, |
| 2054 |
const Matcher<const FieldType&>& matcher) |
| 2055 |
: field_(field), |
| 2056 |
matcher_(matcher), |
| 2057 |
whose_field_("whose field `" + field_name + "` ") {} |
| 2058 |
|
| 2059 |
void DescribeTo(::std::ostream* os) const { |
| 2060 |
*os << "is an object " << whose_field_; |
| 2061 |
matcher_.DescribeTo(os); |
| 2062 |
} |
| 2063 |
|
| 2064 |
void DescribeNegationTo(::std::ostream* os) const { |
| 2065 |
*os << "is an object " << whose_field_; |
| 2066 |
matcher_.DescribeNegationTo(os); |
| 2067 |
} |
| 2068 |
|
| 2069 |
template <typename T> |
| 2070 |
bool MatchAndExplain(const T& value, MatchResultListener* listener) const { |
| 2071 |
// FIXME: The dispatch on std::is_pointer was introduced as a workaround for |
| 2072 |
// a compiler bug, and can now be removed. |
| 2073 |
return MatchAndExplainImpl( |
| 2074 |
typename std::is_pointer<typename std::remove_const<T>::type>::type(), |
| 2075 |
value, listener); |
| 2076 |
} |
| 2077 |
|
| 2078 |
private: |
| 2079 |
bool MatchAndExplainImpl(std::false_type /* is_not_pointer */, |
| 2080 |
const Class& obj, |
| 2081 |
MatchResultListener* listener) const { |
| 2082 |
*listener << whose_field_ << "is "; |
| 2083 |
return MatchPrintAndExplain(obj.*field_, matcher_, listener); |
| 2084 |
} |
| 2085 |
|
| 2086 |
bool MatchAndExplainImpl(std::true_type /* is_pointer */, const Class* p, |
| 2087 |
MatchResultListener* listener) const { |
| 2088 |
if (p == nullptr) return false; |
| 2089 |
|
| 2090 |
*listener << "which points to an object "; |
| 2091 |
// Since *p has a field, it must be a class/struct/union type and |
| 2092 |
// thus cannot be a pointer. Therefore we pass false_type() as |
| 2093 |
// the first argument. |
| 2094 |
return MatchAndExplainImpl(std::false_type(), *p, listener); |
| 2095 |
} |
| 2096 |
|
| 2097 |
const FieldType Class::*field_; |
| 2098 |
const Matcher<const FieldType&> matcher_; |
| 2099 |
|
| 2100 |
// Contains either "whose given field " if the name of the field is unknown |
| 2101 |
// or "whose field `name_of_field` " if the name is known. |
| 2102 |
const std::string whose_field_; |
| 2103 |
}; |
| 2104 |
|
| 2105 |
// Implements the Property() matcher for matching a property |
| 2106 |
// (i.e. return value of a getter method) of an object. |
| 2107 |
// |
| 2108 |
// Property is a const-qualified member function of Class returning |
| 2109 |
// PropertyType. |
| 2110 |
template <typename Class, typename PropertyType, typename Property> |
| 2111 |
class PropertyMatcher { |
| 2112 |
public: |
| 2113 |
typedef const PropertyType& RefToConstProperty; |
| 2114 |
|
| 2115 |
PropertyMatcher(Property property, const Matcher<RefToConstProperty>& matcher) |
| 2116 |
: property_(property), |
| 2117 |
matcher_(matcher), |
| 2118 |
whose_property_("whose given property ") {} |
| 2119 |
|
| 2120 |
PropertyMatcher(const std::string& property_name, Property property, |
| 2121 |
const Matcher<RefToConstProperty>& matcher) |
| 2122 |
: property_(property), |
| 2123 |
matcher_(matcher), |
| 2124 |
whose_property_("whose property `" + property_name + "` ") {} |
| 2125 |
|
| 2126 |
void DescribeTo(::std::ostream* os) const { |
| 2127 |
*os << "is an object " << whose_property_; |
| 2128 |
matcher_.DescribeTo(os); |
| 2129 |
} |
| 2130 |
|
| 2131 |
void DescribeNegationTo(::std::ostream* os) const { |
| 2132 |
*os << "is an object " << whose_property_; |
| 2133 |
matcher_.DescribeNegationTo(os); |
| 2134 |
} |
| 2135 |
|
| 2136 |
template <typename T> |
| 2137 |
bool MatchAndExplain(const T& value, MatchResultListener* listener) const { |
| 2138 |
return MatchAndExplainImpl( |
| 2139 |
typename std::is_pointer<typename std::remove_const<T>::type>::type(), |
| 2140 |
value, listener); |
| 2141 |
} |
| 2142 |
|
| 2143 |
private: |
| 2144 |
bool MatchAndExplainImpl(std::false_type /* is_not_pointer */, |
| 2145 |
const Class& obj, |
| 2146 |
MatchResultListener* listener) const { |
| 2147 |
*listener << whose_property_ << "is "; |
| 2148 |
// Cannot pass the return value (for example, int) to MatchPrintAndExplain, |
| 2149 |
// which takes a non-const reference as argument. |
| 2150 |
RefToConstProperty result = (obj.*property_)(); |
| 2151 |
return MatchPrintAndExplain(result, matcher_, listener); |
| 2152 |
} |
| 2153 |
|
| 2154 |
bool MatchAndExplainImpl(std::true_type /* is_pointer */, const Class* p, |
| 2155 |
MatchResultListener* listener) const { |
| 2156 |
if (p == nullptr) return false; |
| 2157 |
|
| 2158 |
*listener << "which points to an object "; |
| 2159 |
// Since *p has a property method, it must be a class/struct/union |
| 2160 |
// type and thus cannot be a pointer. Therefore we pass |
| 2161 |
// false_type() as the first argument. |
| 2162 |
return MatchAndExplainImpl(std::false_type(), *p, listener); |
| 2163 |
} |
| 2164 |
|
| 2165 |
Property property_; |
| 2166 |
const Matcher<RefToConstProperty> matcher_; |
| 2167 |
|
| 2168 |
// Contains either "whose given property " if the name of the property is |
| 2169 |
// unknown or "whose property `name_of_property` " if the name is known. |
| 2170 |
const std::string whose_property_; |
| 2171 |
}; |
| 2172 |
|
| 2173 |
// Type traits specifying various features of different functors for ResultOf. |
| 2174 |
// The default template specifies features for functor objects. |
| 2175 |
template <typename Functor> |
| 2176 |
struct CallableTraits { |
| 2177 |
typedef Functor StorageType; |
| 2178 |
|
| 2179 |
static void CheckIsValid(Functor /* functor */) {} |
| 2180 |
|
| 2181 |
template <typename T> |
| 2182 |
static auto Invoke(Functor f, const T& arg) -> decltype(f(arg)) { |
| 2183 |
return f(arg); |
| 2184 |
} |
| 2185 |
}; |
| 2186 |
|
| 2187 |
// Specialization for function pointers. |
| 2188 |
template <typename ArgType, typename ResType> |
| 2189 |
struct CallableTraits<ResType (*)(ArgType)> { |
| 2190 |
typedef ResType ResultType; |
| 2191 |
typedef ResType (*StorageType)(ArgType); |
| 2192 |
|
| 2193 |
static void CheckIsValid(ResType (*f)(ArgType)) { |
| 2194 |
GTEST_CHECK_(f != nullptr) |
| 2195 |
<< "NULL function pointer is passed into ResultOf()."; |
| 2196 |
} |
| 2197 |
template <typename T> |
| 2198 |
static ResType Invoke(ResType (*f)(ArgType), T arg) { |
| 2199 |
return (*f)(arg); |
| 2200 |
} |
| 2201 |
}; |
| 2202 |
|
| 2203 |
// Implements the ResultOf() matcher for matching a return value of a |
| 2204 |
// unary function of an object. |
| 2205 |
template <typename Callable, typename InnerMatcher> |
| 2206 |
class ResultOfMatcher { |
| 2207 |
public: |
| 2208 |
ResultOfMatcher(Callable callable, InnerMatcher matcher) |
| 2209 |
: callable_(std::move(callable)), matcher_(std::move(matcher)) { |
| 2210 |
CallableTraits<Callable>::CheckIsValid(callable_); |
| 2211 |
} |
| 2212 |
|
| 2213 |
template <typename T> |
| 2214 |
operator Matcher<T>() const { |
| 2215 |
return Matcher<T>(new Impl<const T&>(callable_, matcher_)); |
| 2216 |
} |
| 2217 |
|
| 2218 |
private: |
| 2219 |
typedef typename CallableTraits<Callable>::StorageType CallableStorageType; |
| 2220 |
|
| 2221 |
template <typename T> |
| 2222 |
class Impl : public MatcherInterface<T> { |
| 2223 |
using ResultType = decltype(CallableTraits<Callable>::template Invoke<T>( |
| 2224 |
std::declval<CallableStorageType>(), std::declval<T>())); |
| 2225 |
|
| 2226 |
public: |
| 2227 |
template <typename M> |
| 2228 |
Impl(const CallableStorageType& callable, const M& matcher) |
| 2229 |
: callable_(callable), matcher_(MatcherCast<ResultType>(matcher)) {} |
| 2230 |
|
| 2231 |
void DescribeTo(::std::ostream* os) const override { |
| 2232 |
*os << "is mapped by the given callable to a value that "; |
| 2233 |
matcher_.DescribeTo(os); |
| 2234 |
} |
| 2235 |
|
| 2236 |
void DescribeNegationTo(::std::ostream* os) const override { |
| 2237 |
*os << "is mapped by the given callable to a value that "; |
| 2238 |
matcher_.DescribeNegationTo(os); |
| 2239 |
} |
| 2240 |
|
| 2241 |
bool MatchAndExplain(T obj, MatchResultListener* listener) const override { |
| 2242 |
*listener << "which is mapped by the given callable to "; |
| 2243 |
// Cannot pass the return value directly to MatchPrintAndExplain, which |
| 2244 |
// takes a non-const reference as argument. |
| 2245 |
// Also, specifying template argument explicitly is needed because T could |
| 2246 |
// be a non-const reference (e.g. Matcher<Uncopyable&>). |
| 2247 |
ResultType result = |
| 2248 |
CallableTraits<Callable>::template Invoke<T>(callable_, obj); |
| 2249 |
return MatchPrintAndExplain(result, matcher_, listener); |
| 2250 |
} |
| 2251 |
|
| 2252 |
private: |
| 2253 |
// Functors often define operator() as non-const method even though |
| 2254 |
// they are actually stateless. But we need to use them even when |
| 2255 |
// 'this' is a const pointer. It's the user's responsibility not to |
| 2256 |
// use stateful callables with ResultOf(), which doesn't guarantee |
| 2257 |
// how many times the callable will be invoked. |
| 2258 |
mutable CallableStorageType callable_; |
| 2259 |
const Matcher<ResultType> matcher_; |
| 2260 |
}; // class Impl |
| 2261 |
|
| 2262 |
const CallableStorageType callable_; |
| 2263 |
const InnerMatcher matcher_; |
| 2264 |
}; |
| 2265 |
|
| 2266 |
// Implements a matcher that checks the size of an STL-style container. |
| 2267 |
template <typename SizeMatcher> |
| 2268 |
class SizeIsMatcher { |
| 2269 |
public: |
| 2270 |
explicit SizeIsMatcher(const SizeMatcher& size_matcher) |
| 2271 |
: size_matcher_(size_matcher) {} |
| 2272 |
|
| 2273 |
template <typename Container> |
| 2274 |
operator Matcher<Container>() const { |
| 2275 |
return Matcher<Container>(new Impl<const Container&>(size_matcher_)); |
| 2276 |
} |
| 2277 |
|
| 2278 |
template <typename Container> |
| 2279 |
class Impl : public MatcherInterface<Container> { |
| 2280 |
public: |
| 2281 |
using SizeType = decltype(std::declval<Container>().size()); |
| 2282 |
explicit Impl(const SizeMatcher& size_matcher) |
| 2283 |
: size_matcher_(MatcherCast<SizeType>(size_matcher)) {} |
| 2284 |
|
| 2285 |
void DescribeTo(::std::ostream* os) const override { |
| 2286 |
*os << "size "; |
| 2287 |
size_matcher_.DescribeTo(os); |
| 2288 |
} |
| 2289 |
void DescribeNegationTo(::std::ostream* os) const override { |
| 2290 |
*os << "size "; |
| 2291 |
size_matcher_.DescribeNegationTo(os); |
| 2292 |
} |
| 2293 |
|
| 2294 |
bool MatchAndExplain(Container container, |
| 2295 |
MatchResultListener* listener) const override { |
| 2296 |
SizeType size = container.size(); |
| 2297 |
StringMatchResultListener size_listener; |
| 2298 |
const bool result = size_matcher_.MatchAndExplain(size, &size_listener); |
| 2299 |
*listener << "whose size " << size |
| 2300 |
<< (result ? " matches" : " doesn't match"); |
| 2301 |
PrintIfNotEmpty(size_listener.str(), listener->stream()); |
| 2302 |
return result; |
| 2303 |
} |
| 2304 |
|
| 2305 |
private: |
| 2306 |
const Matcher<SizeType> size_matcher_; |
| 2307 |
}; |
| 2308 |
|
| 2309 |
private: |
| 2310 |
const SizeMatcher size_matcher_; |
| 2311 |
}; |
| 2312 |
|
| 2313 |
// Implements a matcher that checks the begin()..end() distance of an STL-style |
| 2314 |
// container. |
| 2315 |
template <typename DistanceMatcher> |
| 2316 |
class BeginEndDistanceIsMatcher { |
| 2317 |
public: |
| 2318 |
explicit BeginEndDistanceIsMatcher(const DistanceMatcher& distance_matcher) |
| 2319 |
: distance_matcher_(distance_matcher) {} |
| 2320 |
|
| 2321 |
template <typename Container> |
| 2322 |
operator Matcher<Container>() const { |
| 2323 |
return Matcher<Container>(new Impl<const Container&>(distance_matcher_)); |
| 2324 |
} |
| 2325 |
|
| 2326 |
template <typename Container> |
| 2327 |
class Impl : public MatcherInterface<Container> { |
| 2328 |
public: |
| 2329 |
typedef internal::StlContainerView<GTEST_REMOVE_REFERENCE_AND_CONST_( |
| 2330 |
Container)> |
| 2331 |
ContainerView; |
| 2332 |
typedef typename std::iterator_traits< |
| 2333 |
typename ContainerView::type::const_iterator>::difference_type |
| 2334 |
DistanceType; |
| 2335 |
explicit Impl(const DistanceMatcher& distance_matcher) |
| 2336 |
: distance_matcher_(MatcherCast<DistanceType>(distance_matcher)) {} |
| 2337 |
|
| 2338 |
void DescribeTo(::std::ostream* os) const override { |
| 2339 |
*os << "distance between begin() and end() "; |
| 2340 |
distance_matcher_.DescribeTo(os); |
| 2341 |
} |
| 2342 |
void DescribeNegationTo(::std::ostream* os) const override { |
| 2343 |
*os << "distance between begin() and end() "; |
| 2344 |
distance_matcher_.DescribeNegationTo(os); |
| 2345 |
} |
| 2346 |
|
| 2347 |
bool MatchAndExplain(Container container, |
| 2348 |
MatchResultListener* listener) const override { |
| 2349 |
using std::begin; |
| 2350 |
using std::end; |
| 2351 |
DistanceType distance = std::distance(begin(container), end(container)); |
| 2352 |
StringMatchResultListener distance_listener; |
| 2353 |
const bool result = |
| 2354 |
distance_matcher_.MatchAndExplain(distance, &distance_listener); |
| 2355 |
*listener << "whose distance between begin() and end() " << distance |
| 2356 |
<< (result ? " matches" : " doesn't match"); |
| 2357 |
PrintIfNotEmpty(distance_listener.str(), listener->stream()); |
| 2358 |
return result; |
| 2359 |
} |
| 2360 |
|
| 2361 |
private: |
| 2362 |
const Matcher<DistanceType> distance_matcher_; |
| 2363 |
}; |
| 2364 |
|
| 2365 |
private: |
| 2366 |
const DistanceMatcher distance_matcher_; |
| 2367 |
}; |
| 2368 |
|
| 2369 |
// Implements an equality matcher for any STL-style container whose elements |
| 2370 |
// support ==. This matcher is like Eq(), but its failure explanations provide |
| 2371 |
// more detailed information that is useful when the container is used as a set. |
| 2372 |
// The failure message reports elements that are in one of the operands but not |
| 2373 |
// the other. The failure messages do not report duplicate or out-of-order |
| 2374 |
// elements in the containers (which don't properly matter to sets, but can |
| 2375 |
// occur if the containers are vectors or lists, for example). |
| 2376 |
// |
| 2377 |
// Uses the container's const_iterator, value_type, operator ==, |
| 2378 |
// begin(), and end(). |
| 2379 |
template <typename Container> |
| 2380 |
class ContainerEqMatcher { |
| 2381 |
public: |
| 2382 |
typedef internal::StlContainerView<Container> View; |
| 2383 |
typedef typename View::type StlContainer; |
| 2384 |
typedef typename View::const_reference StlContainerReference; |
| 2385 |
|
| 2386 |
static_assert(!std::is_const<Container>::value, |
| 2387 |
"Container type must not be const"); |
| 2388 |
static_assert(!std::is_reference<Container>::value, |
| 2389 |
"Container type must not be a reference"); |
| 2390 |
|
| 2391 |
// We make a copy of expected in case the elements in it are modified |
| 2392 |
// after this matcher is created. |
| 2393 |
explicit ContainerEqMatcher(const Container& expected) |
| 2394 |
: expected_(View::Copy(expected)) {} |
| 2395 |
|
| 2396 |
void DescribeTo(::std::ostream* os) const { |
| 2397 |
*os << "equals "; |
| 2398 |
UniversalPrint(expected_, os); |
| 2399 |
} |
| 2400 |
void DescribeNegationTo(::std::ostream* os) const { |
| 2401 |
*os << "does not equal "; |
| 2402 |
UniversalPrint(expected_, os); |
| 2403 |
} |
| 2404 |
|
| 2405 |
template <typename LhsContainer> |
| 2406 |
bool MatchAndExplain(const LhsContainer& lhs, |
| 2407 |
MatchResultListener* listener) const { |
| 2408 |
typedef internal::StlContainerView< |
| 2409 |
typename std::remove_const<LhsContainer>::type> |
| 2410 |
LhsView; |
| 2411 |
StlContainerReference lhs_stl_container = LhsView::ConstReference(lhs); |
| 2412 |
if (lhs_stl_container == expected_) return true; |
| 2413 |
|
| 2414 |
::std::ostream* const os = listener->stream(); |
| 2415 |
if (os != nullptr) { |
| 2416 |
// Something is different. Check for extra values first. |
| 2417 |
bool printed_header = false; |
| 2418 |
for (auto it = lhs_stl_container.begin(); it != lhs_stl_container.end(); |
| 2419 |
++it) { |
| 2420 |
if (internal::ArrayAwareFind(expected_.begin(), expected_.end(), *it) == |
| 2421 |
expected_.end()) { |
| 2422 |
if (printed_header) { |
| 2423 |
*os << ", "; |
| 2424 |
} else { |
| 2425 |
*os << "which has these unexpected elements: "; |
| 2426 |
printed_header = true; |
| 2427 |
} |
| 2428 |
UniversalPrint(*it, os); |
| 2429 |
} |
| 2430 |
} |
| 2431 |
|
| 2432 |
// Now check for missing values. |
| 2433 |
bool printed_header2 = false; |
| 2434 |
for (auto it = expected_.begin(); it != expected_.end(); ++it) { |
| 2435 |
if (internal::ArrayAwareFind(lhs_stl_container.begin(), |
| 2436 |
lhs_stl_container.end(), |
| 2437 |
*it) == lhs_stl_container.end()) { |
| 2438 |
if (printed_header2) { |
| 2439 |
*os << ", "; |
| 2440 |
} else { |
| 2441 |
*os << (printed_header ? ",\nand" : "which") |
| 2442 |
<< " doesn't have these expected elements: "; |
| 2443 |
printed_header2 = true; |
| 2444 |
} |
| 2445 |
UniversalPrint(*it, os); |
| 2446 |
} |
| 2447 |
} |
| 2448 |
} |
| 2449 |
|
| 2450 |
return false; |
| 2451 |
} |
| 2452 |
|
| 2453 |
private: |
| 2454 |
const StlContainer expected_; |
| 2455 |
}; |
| 2456 |
|
| 2457 |
// A comparator functor that uses the < operator to compare two values. |
| 2458 |
struct LessComparator { |
| 2459 |
template <typename T, typename U> |
| 2460 |
bool operator()(const T& lhs, const U& rhs) const { |
| 2461 |
return lhs < rhs; |
| 2462 |
} |
| 2463 |
}; |
| 2464 |
|
| 2465 |
// Implements WhenSortedBy(comparator, container_matcher). |
| 2466 |
template <typename Comparator, typename ContainerMatcher> |
| 2467 |
class WhenSortedByMatcher { |
| 2468 |
public: |
| 2469 |
WhenSortedByMatcher(const Comparator& comparator, |
| 2470 |
const ContainerMatcher& matcher) |
| 2471 |
: comparator_(comparator), matcher_(matcher) {} |
| 2472 |
|
| 2473 |
template <typename LhsContainer> |
| 2474 |
operator Matcher<LhsContainer>() const { |
| 2475 |
return MakeMatcher(new Impl<LhsContainer>(comparator_, matcher_)); |
| 2476 |
} |
| 2477 |
|
| 2478 |
template <typename LhsContainer> |
| 2479 |
class Impl : public MatcherInterface<LhsContainer> { |
| 2480 |
public: |
| 2481 |
typedef internal::StlContainerView<GTEST_REMOVE_REFERENCE_AND_CONST_( |
| 2482 |
LhsContainer)> |
| 2483 |
LhsView; |
| 2484 |
typedef typename LhsView::type LhsStlContainer; |
| 2485 |
typedef typename LhsView::const_reference LhsStlContainerReference; |
| 2486 |
// Transforms std::pair<const Key, Value> into std::pair<Key, Value> |
| 2487 |
// so that we can match associative containers. |
| 2488 |
typedef |
| 2489 |
typename RemoveConstFromKey<typename LhsStlContainer::value_type>::type |
| 2490 |
LhsValue; |
| 2491 |
|
| 2492 |
Impl(const Comparator& comparator, const ContainerMatcher& matcher) |
| 2493 |
: comparator_(comparator), matcher_(matcher) {} |
| 2494 |
|
| 2495 |
void DescribeTo(::std::ostream* os) const override { |
| 2496 |
*os << "(when sorted) "; |
| 2497 |
matcher_.DescribeTo(os); |
| 2498 |
} |
| 2499 |
|
| 2500 |
void DescribeNegationTo(::std::ostream* os) const override { |
| 2501 |
*os << "(when sorted) "; |
| 2502 |
matcher_.DescribeNegationTo(os); |
| 2503 |
} |
| 2504 |
|
| 2505 |
bool MatchAndExplain(LhsContainer lhs, |
| 2506 |
MatchResultListener* listener) const override { |
| 2507 |
LhsStlContainerReference lhs_stl_container = LhsView::ConstReference(lhs); |
| 2508 |
::std::vector<LhsValue> sorted_container(lhs_stl_container.begin(), |
| 2509 |
lhs_stl_container.end()); |
| 2510 |
::std::sort(sorted_container.begin(), sorted_container.end(), |
| 2511 |
comparator_); |
| 2512 |
|
| 2513 |
if (!listener->IsInterested()) { |
| 2514 |
// If the listener is not interested, we do not need to |
| 2515 |
// construct the inner explanation. |
| 2516 |
return matcher_.Matches(sorted_container); |
| 2517 |
} |
| 2518 |
|
| 2519 |
*listener << "which is "; |
| 2520 |
UniversalPrint(sorted_container, listener->stream()); |
| 2521 |
*listener << " when sorted"; |
| 2522 |
|
| 2523 |
StringMatchResultListener inner_listener; |
| 2524 |
const bool match = |
| 2525 |
matcher_.MatchAndExplain(sorted_container, &inner_listener); |
| 2526 |
PrintIfNotEmpty(inner_listener.str(), listener->stream()); |
| 2527 |
return match; |
| 2528 |
} |
| 2529 |
|
| 2530 |
private: |
| 2531 |
const Comparator comparator_; |
| 2532 |
const Matcher<const ::std::vector<LhsValue>&> matcher_; |
| 2533 |
|
| 2534 |
GTEST_DISALLOW_COPY_AND_ASSIGN_(Impl); |
| 2535 |
}; |
| 2536 |
|
| 2537 |
private: |
| 2538 |
const Comparator comparator_; |
| 2539 |
const ContainerMatcher matcher_; |
| 2540 |
}; |
| 2541 |
|
| 2542 |
// Implements Pointwise(tuple_matcher, rhs_container). tuple_matcher |
| 2543 |
// must be able to be safely cast to Matcher<std::tuple<const T1&, const |
| 2544 |
// T2&> >, where T1 and T2 are the types of elements in the LHS |
| 2545 |
// container and the RHS container respectively. |
| 2546 |
template <typename TupleMatcher, typename RhsContainer> |
| 2547 |
class PointwiseMatcher { |
| 2548 |
GTEST_COMPILE_ASSERT_( |
| 2549 |
!IsHashTable<GTEST_REMOVE_REFERENCE_AND_CONST_(RhsContainer)>::value, |
| 2550 |
use_UnorderedPointwise_with_hash_tables); |
| 2551 |
|
| 2552 |
public: |
| 2553 |
typedef internal::StlContainerView<RhsContainer> RhsView; |
| 2554 |
typedef typename RhsView::type RhsStlContainer; |
| 2555 |
typedef typename RhsStlContainer::value_type RhsValue; |
| 2556 |
|
| 2557 |
static_assert(!std::is_const<RhsContainer>::value, |
| 2558 |
"RhsContainer type must not be const"); |
| 2559 |
static_assert(!std::is_reference<RhsContainer>::value, |
| 2560 |
"RhsContainer type must not be a reference"); |
| 2561 |
|
| 2562 |
// Like ContainerEq, we make a copy of rhs in case the elements in |
| 2563 |
// it are modified after this matcher is created. |
| 2564 |
PointwiseMatcher(const TupleMatcher& tuple_matcher, const RhsContainer& rhs) |
| 2565 |
: tuple_matcher_(tuple_matcher), rhs_(RhsView::Copy(rhs)) {} |
| 2566 |
|
| 2567 |
template <typename LhsContainer> |
| 2568 |
operator Matcher<LhsContainer>() const { |
| 2569 |
GTEST_COMPILE_ASSERT_( |
| 2570 |
!IsHashTable<GTEST_REMOVE_REFERENCE_AND_CONST_(LhsContainer)>::value, |
| 2571 |
use_UnorderedPointwise_with_hash_tables); |
| 2572 |
|
| 2573 |
return Matcher<LhsContainer>( |
| 2574 |
new Impl<const LhsContainer&>(tuple_matcher_, rhs_)); |
| 2575 |
} |
| 2576 |
|
| 2577 |
template <typename LhsContainer> |
| 2578 |
class Impl : public MatcherInterface<LhsContainer> { |
| 2579 |
public: |
| 2580 |
typedef internal::StlContainerView<GTEST_REMOVE_REFERENCE_AND_CONST_( |
| 2581 |
LhsContainer)> |
| 2582 |
LhsView; |
| 2583 |
typedef typename LhsView::type LhsStlContainer; |
| 2584 |
typedef typename LhsView::const_reference LhsStlContainerReference; |
| 2585 |
typedef typename LhsStlContainer::value_type LhsValue; |
| 2586 |
// We pass the LHS value and the RHS value to the inner matcher by |
| 2587 |
// reference, as they may be expensive to copy. We must use tuple |
| 2588 |
// instead of pair here, as a pair cannot hold references (C++ 98, |
| 2589 |
// 20.2.2 [lib.pairs]). |
| 2590 |
typedef ::std::tuple<const LhsValue&, const RhsValue&> InnerMatcherArg; |
| 2591 |
|
| 2592 |
Impl(const TupleMatcher& tuple_matcher, const RhsStlContainer& rhs) |
| 2593 |
// mono_tuple_matcher_ holds a monomorphic version of the tuple matcher. |
| 2594 |
: mono_tuple_matcher_(SafeMatcherCast<InnerMatcherArg>(tuple_matcher)), |
| 2595 |
rhs_(rhs) {} |
| 2596 |
|
| 2597 |
void DescribeTo(::std::ostream* os) const override { |
| 2598 |
*os << "contains " << rhs_.size() |
| 2599 |
<< " values, where each value and its corresponding value in "; |
| 2600 |
UniversalPrinter<RhsStlContainer>::Print(rhs_, os); |
| 2601 |
*os << " "; |
| 2602 |
mono_tuple_matcher_.DescribeTo(os); |
| 2603 |
} |
| 2604 |
void DescribeNegationTo(::std::ostream* os) const override { |
| 2605 |
*os << "doesn't contain exactly " << rhs_.size() |
| 2606 |
<< " values, or contains a value x at some index i" |
| 2607 |
<< " where x and the i-th value of "; |
| 2608 |
UniversalPrint(rhs_, os); |
| 2609 |
*os << " "; |
| 2610 |
mono_tuple_matcher_.DescribeNegationTo(os); |
| 2611 |
} |
| 2612 |
|
| 2613 |
bool MatchAndExplain(LhsContainer lhs, |
| 2614 |
MatchResultListener* listener) const override { |
| 2615 |
LhsStlContainerReference lhs_stl_container = LhsView::ConstReference(lhs); |
| 2616 |
const size_t actual_size = lhs_stl_container.size(); |
| 2617 |
if (actual_size != rhs_.size()) { |
| 2618 |
*listener << "which contains " << actual_size << " values"; |
| 2619 |
return false; |
| 2620 |
} |
| 2621 |
|
| 2622 |
auto left = lhs_stl_container.begin(); |
| 2623 |
auto right = rhs_.begin(); |
| 2624 |
for (size_t i = 0; i != actual_size; ++i, ++left, ++right) { |
| 2625 |
if (listener->IsInterested()) { |
| 2626 |
StringMatchResultListener inner_listener; |
| 2627 |
// Create InnerMatcherArg as a temporarily object to avoid it outlives |
| 2628 |
// *left and *right. Dereference or the conversion to `const T&` may |
| 2629 |
// return temp objects, e.g. for vector<bool>. |
| 2630 |
if (!mono_tuple_matcher_.MatchAndExplain( |
| 2631 |
InnerMatcherArg(ImplicitCast_<const LhsValue&>(*left), |
| 2632 |
ImplicitCast_<const RhsValue&>(*right)), |
| 2633 |
&inner_listener)) { |
| 2634 |
*listener << "where the value pair ("; |
| 2635 |
UniversalPrint(*left, listener->stream()); |
| 2636 |
*listener << ", "; |
| 2637 |
UniversalPrint(*right, listener->stream()); |
| 2638 |
*listener << ") at index #" << i << " don't match"; |
| 2639 |
PrintIfNotEmpty(inner_listener.str(), listener->stream()); |
| 2640 |
return false; |
| 2641 |
} |
| 2642 |
} else { |
| 2643 |
if (!mono_tuple_matcher_.Matches( |
| 2644 |
InnerMatcherArg(ImplicitCast_<const LhsValue&>(*left), |
| 2645 |
ImplicitCast_<const RhsValue&>(*right)))) |
| 2646 |
return false; |
| 2647 |
} |
| 2648 |
} |
| 2649 |
|
| 2650 |
return true; |
| 2651 |
} |
| 2652 |
|
| 2653 |
private: |
| 2654 |
const Matcher<InnerMatcherArg> mono_tuple_matcher_; |
| 2655 |
const RhsStlContainer rhs_; |
| 2656 |
}; |
| 2657 |
|
| 2658 |
private: |
| 2659 |
const TupleMatcher tuple_matcher_; |
| 2660 |
const RhsStlContainer rhs_; |
| 2661 |
}; |
| 2662 |
|
| 2663 |
// Holds the logic common to ContainsMatcherImpl and EachMatcherImpl. |
| 2664 |
template <typename Container> |
| 2665 |
class QuantifierMatcherImpl : public MatcherInterface<Container> { |
| 2666 |
public: |
| 2667 |
typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer; |
| 2668 |
typedef StlContainerView<RawContainer> View; |
| 2669 |
typedef typename View::type StlContainer; |
| 2670 |
typedef typename View::const_reference StlContainerReference; |
| 2671 |
typedef typename StlContainer::value_type Element; |
| 2672 |
|
| 2673 |
template <typename InnerMatcher> |
| 2674 |
explicit QuantifierMatcherImpl(InnerMatcher inner_matcher) |
| 2675 |
: inner_matcher_( |
| 2676 |
testing::SafeMatcherCast<const Element&>(inner_matcher)) {} |
| 2677 |
|
| 2678 |
// Checks whether: |
| 2679 |
// * All elements in the container match, if all_elements_should_match. |
| 2680 |
// * Any element in the container matches, if !all_elements_should_match. |
| 2681 |
bool MatchAndExplainImpl(bool all_elements_should_match, Container container, |
| 2682 |
MatchResultListener* listener) const { |
| 2683 |
StlContainerReference stl_container = View::ConstReference(container); |
| 2684 |
size_t i = 0; |
| 2685 |
for (auto it = stl_container.begin(); it != stl_container.end(); |
| 2686 |
++it, ++i) { |
| 2687 |
StringMatchResultListener inner_listener; |
| 2688 |
const bool matches = inner_matcher_.MatchAndExplain(*it, &inner_listener); |
| 2689 |
|
| 2690 |
if (matches != all_elements_should_match) { |
| 2691 |
*listener << "whose element #" << i |
| 2692 |
<< (matches ? " matches" : " doesn't match"); |
| 2693 |
PrintIfNotEmpty(inner_listener.str(), listener->stream()); |
| 2694 |
return !all_elements_should_match; |
| 2695 |
} |
| 2696 |
} |
| 2697 |
return all_elements_should_match; |
| 2698 |
} |
| 2699 |
|
| 2700 |
bool MatchAndExplainImpl(const Matcher<size_t>& count_matcher, |
| 2701 |
Container container, |
| 2702 |
MatchResultListener* listener) const { |
| 2703 |
StlContainerReference stl_container = View::ConstReference(container); |
| 2704 |
size_t i = 0; |
| 2705 |
std::vector<size_t> match_elements; |
| 2706 |
for (auto it = stl_container.begin(); it != stl_container.end(); |
| 2707 |
++it, ++i) { |
| 2708 |
StringMatchResultListener inner_listener; |
| 2709 |
const bool matches = inner_matcher_.MatchAndExplain(*it, &inner_listener); |
| 2710 |
if (matches) { |
| 2711 |
match_elements.push_back(i); |
| 2712 |
} |
| 2713 |
} |
| 2714 |
if (listener->IsInterested()) { |
| 2715 |
if (match_elements.empty()) { |
| 2716 |
*listener << "has no element that matches"; |
| 2717 |
} else if (match_elements.size() == 1) { |
| 2718 |
*listener << "whose element #" << match_elements[0] << " matches"; |
| 2719 |
} else { |
| 2720 |
*listener << "whose elements ("; |
| 2721 |
std::string sep = ""; |
| 2722 |
for (size_t e : match_elements) { |
| 2723 |
*listener << sep << e; |
| 2724 |
sep = ", "; |
| 2725 |
} |
| 2726 |
*listener << ") match"; |
| 2727 |
} |
| 2728 |
} |
| 2729 |
StringMatchResultListener count_listener; |
| 2730 |
if (count_matcher.MatchAndExplain(match_elements.size(), &count_listener)) { |
| 2731 |
*listener << " and whose match quantity of " << match_elements.size() |
| 2732 |
<< " matches"; |
| 2733 |
PrintIfNotEmpty(count_listener.str(), listener->stream()); |
| 2734 |
return true; |
| 2735 |
} else { |
| 2736 |
if (match_elements.empty()) { |
| 2737 |
*listener << " and"; |
| 2738 |
} else { |
| 2739 |
*listener << " but"; |
| 2740 |
} |
| 2741 |
*listener << " whose match quantity of " << match_elements.size() |
| 2742 |
<< " does not match"; |
| 2743 |
PrintIfNotEmpty(count_listener.str(), listener->stream()); |
| 2744 |
return false; |
| 2745 |
} |
| 2746 |
} |
| 2747 |
|
| 2748 |
protected: |
| 2749 |
const Matcher<const Element&> inner_matcher_; |
| 2750 |
}; |
| 2751 |
|
| 2752 |
// Implements Contains(element_matcher) for the given argument type Container. |
| 2753 |
// Symmetric to EachMatcherImpl. |
| 2754 |
template <typename Container> |
| 2755 |
class ContainsMatcherImpl : public QuantifierMatcherImpl<Container> { |
| 2756 |
public: |
| 2757 |
template <typename InnerMatcher> |
| 2758 |
explicit ContainsMatcherImpl(InnerMatcher inner_matcher) |
| 2759 |
: QuantifierMatcherImpl<Container>(inner_matcher) {} |
| 2760 |
|
| 2761 |
// Describes what this matcher does. |
| 2762 |
void DescribeTo(::std::ostream* os) const override { |
| 2763 |
*os << "contains at least one element that "; |
| 2764 |
this->inner_matcher_.DescribeTo(os); |
| 2765 |
} |
| 2766 |
|
| 2767 |
void DescribeNegationTo(::std::ostream* os) const override { |
| 2768 |
*os << "doesn't contain any element that "; |
| 2769 |
this->inner_matcher_.DescribeTo(os); |
| 2770 |
} |
| 2771 |
|
| 2772 |
bool MatchAndExplain(Container container, |
| 2773 |
MatchResultListener* listener) const override { |
| 2774 |
return this->MatchAndExplainImpl(false, container, listener); |
| 2775 |
} |
| 2776 |
}; |
| 2777 |
|
| 2778 |
// Implements Each(element_matcher) for the given argument type Container. |
| 2779 |
// Symmetric to ContainsMatcherImpl. |
| 2780 |
template <typename Container> |
| 2781 |
class EachMatcherImpl : public QuantifierMatcherImpl<Container> { |
| 2782 |
public: |
| 2783 |
template <typename InnerMatcher> |
| 2784 |
explicit EachMatcherImpl(InnerMatcher inner_matcher) |
| 2785 |
: QuantifierMatcherImpl<Container>(inner_matcher) {} |
| 2786 |
|
| 2787 |
// Describes what this matcher does. |
| 2788 |
void DescribeTo(::std::ostream* os) const override { |
| 2789 |
*os << "only contains elements that "; |
| 2790 |
this->inner_matcher_.DescribeTo(os); |
| 2791 |
} |
| 2792 |
|
| 2793 |
void DescribeNegationTo(::std::ostream* os) const override { |
| 2794 |
*os << "contains some element that "; |
| 2795 |
this->inner_matcher_.DescribeNegationTo(os); |
| 2796 |
} |
| 2797 |
|
| 2798 |
bool MatchAndExplain(Container container, |
| 2799 |
MatchResultListener* listener) const override { |
| 2800 |
return this->MatchAndExplainImpl(true, container, listener); |
| 2801 |
} |
| 2802 |
}; |
| 2803 |
|
| 2804 |
// Implements Contains(element_matcher).Times(n) for the given argument type |
| 2805 |
// Container. |
| 2806 |
template <typename Container> |
| 2807 |
class ContainsTimesMatcherImpl : public QuantifierMatcherImpl<Container> { |
| 2808 |
public: |
| 2809 |
template <typename InnerMatcher> |
| 2810 |
explicit ContainsTimesMatcherImpl(InnerMatcher inner_matcher, |
| 2811 |
Matcher<size_t> count_matcher) |
| 2812 |
: QuantifierMatcherImpl<Container>(inner_matcher), |
| 2813 |
count_matcher_(std::move(count_matcher)) {} |
| 2814 |
|
| 2815 |
void DescribeTo(::std::ostream* os) const override { |
| 2816 |
*os << "quantity of elements that match "; |
| 2817 |
this->inner_matcher_.DescribeTo(os); |
| 2818 |
*os << " "; |
| 2819 |
count_matcher_.DescribeTo(os); |
| 2820 |
} |
| 2821 |
|
| 2822 |
void DescribeNegationTo(::std::ostream* os) const override { |
| 2823 |
*os << "quantity of elements that match "; |
| 2824 |
this->inner_matcher_.DescribeTo(os); |
| 2825 |
*os << " "; |
| 2826 |
count_matcher_.DescribeNegationTo(os); |
| 2827 |
} |
| 2828 |
|
| 2829 |
bool MatchAndExplain(Container container, |
| 2830 |
MatchResultListener* listener) const override { |
| 2831 |
return this->MatchAndExplainImpl(count_matcher_, container, listener); |
| 2832 |
} |
| 2833 |
|
| 2834 |
private: |
| 2835 |
const Matcher<size_t> count_matcher_; |
| 2836 |
}; |
| 2837 |
|
| 2838 |
// Implements polymorphic Contains(element_matcher).Times(n). |
| 2839 |
template <typename M> |
| 2840 |
class ContainsTimesMatcher { |
| 2841 |
public: |
| 2842 |
explicit ContainsTimesMatcher(M m, Matcher<size_t> count_matcher) |
| 2843 |
: inner_matcher_(m), count_matcher_(std::move(count_matcher)) {} |
| 2844 |
|
| 2845 |
template <typename Container> |
| 2846 |
operator Matcher<Container>() const { // NOLINT |
| 2847 |
return Matcher<Container>(new ContainsTimesMatcherImpl<const Container&>( |
| 2848 |
inner_matcher_, count_matcher_)); |
| 2849 |
} |
| 2850 |
|
| 2851 |
private: |
| 2852 |
const M inner_matcher_; |
| 2853 |
const Matcher<size_t> count_matcher_; |
| 2854 |
}; |
| 2855 |
|
| 2856 |
// Implements polymorphic Contains(element_matcher). |
| 2857 |
template <typename M> |
| 2858 |
class ContainsMatcher { |
| 2859 |
public: |
| 2860 |
explicit ContainsMatcher(M m) : inner_matcher_(m) {} |
| 2861 |
|
| 2862 |
template <typename Container> |
| 2863 |
operator Matcher<Container>() const { // NOLINT |
| 2864 |
return Matcher<Container>( |
| 2865 |
new ContainsMatcherImpl<const Container&>(inner_matcher_)); |
| 2866 |
} |
| 2867 |
|
| 2868 |
ContainsTimesMatcher<M> Times(Matcher<size_t> count_matcher) const { |
| 2869 |
return ContainsTimesMatcher<M>(inner_matcher_, std::move(count_matcher)); |
| 2870 |
} |
| 2871 |
|
| 2872 |
private: |
| 2873 |
const M inner_matcher_; |
| 2874 |
}; |
| 2875 |
|
| 2876 |
// Implements polymorphic Each(element_matcher). |
| 2877 |
template <typename M> |
| 2878 |
class EachMatcher { |
| 2879 |
public: |
| 2880 |
explicit EachMatcher(M m) : inner_matcher_(m) {} |
| 2881 |
|
| 2882 |
template <typename Container> |
| 2883 |
operator Matcher<Container>() const { // NOLINT |
| 2884 |
return Matcher<Container>( |
| 2885 |
new EachMatcherImpl<const Container&>(inner_matcher_)); |
| 2886 |
} |
| 2887 |
|
| 2888 |
private: |
| 2889 |
const M inner_matcher_; |
| 2890 |
}; |
| 2891 |
|
| 2892 |
struct Rank1 {}; |
| 2893 |
struct Rank0 : Rank1 {}; |
| 2894 |
|
| 2895 |
namespace pair_getters { |
| 2896 |
using std::get; |
| 2897 |
template <typename T> |
| 2898 |
auto First(T& x, Rank1) -> decltype(get<0>(x)) { // NOLINT |
| 2899 |
return get<0>(x); |
| 2900 |
} |
| 2901 |
template <typename T> |
| 2902 |
auto First(T& x, Rank0) -> decltype((x.first)) { // NOLINT |
| 2903 |
return x.first; |
| 2904 |
} |
| 2905 |
|
| 2906 |
template <typename T> |
| 2907 |
auto Second(T& x, Rank1) -> decltype(get<1>(x)) { // NOLINT |
| 2908 |
return get<1>(x); |
| 2909 |
} |
| 2910 |
template <typename T> |
| 2911 |
auto Second(T& x, Rank0) -> decltype((x.second)) { // NOLINT |
| 2912 |
return x.second; |
| 2913 |
} |
| 2914 |
} // namespace pair_getters |
| 2915 |
|
| 2916 |
// Implements Key(inner_matcher) for the given argument pair type. |
| 2917 |
// Key(inner_matcher) matches an std::pair whose 'first' field matches |
| 2918 |
// inner_matcher. For example, Contains(Key(Ge(5))) can be used to match an |
| 2919 |
// std::map that contains at least one element whose key is >= 5. |
| 2920 |
template <typename PairType> |
| 2921 |
class KeyMatcherImpl : public MatcherInterface<PairType> { |
| 2922 |
public: |
| 2923 |
typedef GTEST_REMOVE_REFERENCE_AND_CONST_(PairType) RawPairType; |
| 2924 |
typedef typename RawPairType::first_type KeyType; |
| 2925 |
|
| 2926 |
template <typename InnerMatcher> |
| 2927 |
explicit KeyMatcherImpl(InnerMatcher inner_matcher) |
| 2928 |
: inner_matcher_( |
| 2929 |
testing::SafeMatcherCast<const KeyType&>(inner_matcher)) {} |
| 2930 |
|
| 2931 |
// Returns true if and only if 'key_value.first' (the key) matches the inner |
| 2932 |
// matcher. |
| 2933 |
bool MatchAndExplain(PairType key_value, |
| 2934 |
MatchResultListener* listener) const override { |
| 2935 |
StringMatchResultListener inner_listener; |
| 2936 |
const bool match = inner_matcher_.MatchAndExplain( |
| 2937 |
pair_getters::First(key_value, Rank0()), &inner_listener); |
| 2938 |
const std::string explanation = inner_listener.str(); |
| 2939 |
if (explanation != "") { |
| 2940 |
*listener << "whose first field is a value " << explanation; |
| 2941 |
} |
| 2942 |
return match; |
| 2943 |
} |
| 2944 |
|
| 2945 |
// Describes what this matcher does. |
| 2946 |
void DescribeTo(::std::ostream* os) const override { |
| 2947 |
*os << "has a key that "; |
| 2948 |
inner_matcher_.DescribeTo(os); |
| 2949 |
} |
| 2950 |
|
| 2951 |
// Describes what the negation of this matcher does. |
| 2952 |
void DescribeNegationTo(::std::ostream* os) const override { |
| 2953 |
*os << "doesn't have a key that "; |
| 2954 |
inner_matcher_.DescribeTo(os); |
| 2955 |
} |
| 2956 |
|
| 2957 |
private: |
| 2958 |
const Matcher<const KeyType&> inner_matcher_; |
| 2959 |
}; |
| 2960 |
|
| 2961 |
// Implements polymorphic Key(matcher_for_key). |
| 2962 |
template <typename M> |
| 2963 |
class KeyMatcher { |
| 2964 |
public: |
| 2965 |
explicit KeyMatcher(M m) : matcher_for_key_(m) {} |
| 2966 |
|
| 2967 |
template <typename PairType> |
| 2968 |
operator Matcher<PairType>() const { |
| 2969 |
return Matcher<PairType>( |
| 2970 |
new KeyMatcherImpl<const PairType&>(matcher_for_key_)); |
| 2971 |
} |
| 2972 |
|
| 2973 |
private: |
| 2974 |
const M matcher_for_key_; |
| 2975 |
}; |
| 2976 |
|
| 2977 |
// Implements polymorphic Address(matcher_for_address). |
| 2978 |
template <typename InnerMatcher> |
| 2979 |
class AddressMatcher { |
| 2980 |
public: |
| 2981 |
explicit AddressMatcher(InnerMatcher m) : matcher_(m) {} |
| 2982 |
|
| 2983 |
template <typename Type> |
| 2984 |
operator Matcher<Type>() const { // NOLINT |
| 2985 |
return Matcher<Type>(new Impl<const Type&>(matcher_)); |
| 2986 |
} |
| 2987 |
|
| 2988 |
private: |
| 2989 |
// The monomorphic implementation that works for a particular object type. |
| 2990 |
template <typename Type> |
| 2991 |
class Impl : public MatcherInterface<Type> { |
| 2992 |
public: |
| 2993 |
using Address = const GTEST_REMOVE_REFERENCE_AND_CONST_(Type) *; |
| 2994 |
explicit Impl(const InnerMatcher& matcher) |
| 2995 |
: matcher_(MatcherCast<Address>(matcher)) {} |
| 2996 |
|
| 2997 |
void DescribeTo(::std::ostream* os) const override { |
| 2998 |
*os << "has address that "; |
| 2999 |
matcher_.DescribeTo(os); |
| 3000 |
} |
| 3001 |
|
| 3002 |
void DescribeNegationTo(::std::ostream* os) const override { |
| 3003 |
*os << "does not have address that "; |
| 3004 |
matcher_.DescribeTo(os); |
| 3005 |
} |
| 3006 |
|
| 3007 |
bool MatchAndExplain(Type object, |
| 3008 |
MatchResultListener* listener) const override { |
| 3009 |
*listener << "which has address "; |
| 3010 |
Address address = std::addressof(object); |
| 3011 |
return MatchPrintAndExplain(address, matcher_, listener); |
| 3012 |
} |
| 3013 |
|
| 3014 |
private: |
| 3015 |
const Matcher<Address> matcher_; |
| 3016 |
}; |
| 3017 |
const InnerMatcher matcher_; |
| 3018 |
}; |
| 3019 |
|
| 3020 |
// Implements Pair(first_matcher, second_matcher) for the given argument pair |
| 3021 |
// type with its two matchers. See Pair() function below. |
| 3022 |
template <typename PairType> |
| 3023 |
class PairMatcherImpl : public MatcherInterface<PairType> { |
| 3024 |
public: |
| 3025 |
typedef GTEST_REMOVE_REFERENCE_AND_CONST_(PairType) RawPairType; |
| 3026 |
typedef typename RawPairType::first_type FirstType; |
| 3027 |
typedef typename RawPairType::second_type SecondType; |
| 3028 |
|
| 3029 |
template <typename FirstMatcher, typename SecondMatcher> |
| 3030 |
PairMatcherImpl(FirstMatcher first_matcher, SecondMatcher second_matcher) |
| 3031 |
: first_matcher_( |
| 3032 |
testing::SafeMatcherCast<const FirstType&>(first_matcher)), |
| 3033 |
second_matcher_( |
| 3034 |
testing::SafeMatcherCast<const SecondType&>(second_matcher)) {} |
| 3035 |
|
| 3036 |
// Describes what this matcher does. |
| 3037 |
void DescribeTo(::std::ostream* os) const override { |
| 3038 |
*os << "has a first field that "; |
| 3039 |
first_matcher_.DescribeTo(os); |
| 3040 |
*os << ", and has a second field that "; |
| 3041 |
second_matcher_.DescribeTo(os); |
| 3042 |
} |
| 3043 |
|
| 3044 |
// Describes what the negation of this matcher does. |
| 3045 |
void DescribeNegationTo(::std::ostream* os) const override { |
| 3046 |
*os << "has a first field that "; |
| 3047 |
first_matcher_.DescribeNegationTo(os); |
| 3048 |
*os << ", or has a second field that "; |
| 3049 |
second_matcher_.DescribeNegationTo(os); |
| 3050 |
} |
| 3051 |
|
| 3052 |
// Returns true if and only if 'a_pair.first' matches first_matcher and |
| 3053 |
// 'a_pair.second' matches second_matcher. |
| 3054 |
bool MatchAndExplain(PairType a_pair, |
| 3055 |
MatchResultListener* listener) const override { |
| 3056 |
if (!listener->IsInterested()) { |
| 3057 |
// If the listener is not interested, we don't need to construct the |
| 3058 |
// explanation. |
| 3059 |
return first_matcher_.Matches(pair_getters::First(a_pair, Rank0())) && |
| 3060 |
second_matcher_.Matches(pair_getters::Second(a_pair, Rank0())); |
| 3061 |
} |
| 3062 |
StringMatchResultListener first_inner_listener; |
| 3063 |
if (!first_matcher_.MatchAndExplain(pair_getters::First(a_pair, Rank0()), |
| 3064 |
&first_inner_listener)) { |
| 3065 |
*listener << "whose first field does not match"; |
| 3066 |
PrintIfNotEmpty(first_inner_listener.str(), listener->stream()); |
| 3067 |
return false; |
| 3068 |
} |
| 3069 |
StringMatchResultListener second_inner_listener; |
| 3070 |
if (!second_matcher_.MatchAndExplain(pair_getters::Second(a_pair, Rank0()), |
| 3071 |
&second_inner_listener)) { |
| 3072 |
*listener << "whose second field does not match"; |
| 3073 |
PrintIfNotEmpty(second_inner_listener.str(), listener->stream()); |
| 3074 |
return false; |
| 3075 |
} |
| 3076 |
ExplainSuccess(first_inner_listener.str(), second_inner_listener.str(), |
| 3077 |
listener); |
| 3078 |
return true; |
| 3079 |
} |
| 3080 |
|
| 3081 |
private: |
| 3082 |
void ExplainSuccess(const std::string& first_explanation, |
| 3083 |
const std::string& second_explanation, |
| 3084 |
MatchResultListener* listener) const { |
| 3085 |
*listener << "whose both fields match"; |
| 3086 |
if (first_explanation != "") { |
| 3087 |
*listener << ", where the first field is a value " << first_explanation; |
| 3088 |
} |
| 3089 |
if (second_explanation != "") { |
| 3090 |
*listener << ", "; |
| 3091 |
if (first_explanation != "") { |
| 3092 |
*listener << "and "; |
| 3093 |
} else { |
| 3094 |
*listener << "where "; |
| 3095 |
} |
| 3096 |
*listener << "the second field is a value " << second_explanation; |
| 3097 |
} |
| 3098 |
} |
| 3099 |
|
| 3100 |
const Matcher<const FirstType&> first_matcher_; |
| 3101 |
const Matcher<const SecondType&> second_matcher_; |
| 3102 |
}; |
| 3103 |
|
| 3104 |
// Implements polymorphic Pair(first_matcher, second_matcher). |
| 3105 |
template <typename FirstMatcher, typename SecondMatcher> |
| 3106 |
class PairMatcher { |
| 3107 |
public: |
| 3108 |
PairMatcher(FirstMatcher first_matcher, SecondMatcher second_matcher) |
| 3109 |
: first_matcher_(first_matcher), second_matcher_(second_matcher) {} |
| 3110 |
|
| 3111 |
template <typename PairType> |
| 3112 |
operator Matcher<PairType>() const { |
| 3113 |
return Matcher<PairType>( |
| 3114 |
new PairMatcherImpl<const PairType&>(first_matcher_, second_matcher_)); |
| 3115 |
} |
| 3116 |
|
| 3117 |
private: |
| 3118 |
const FirstMatcher first_matcher_; |
| 3119 |
const SecondMatcher second_matcher_; |
| 3120 |
}; |
| 3121 |
|
| 3122 |
template <typename T, size_t... I> |
| 3123 |
auto UnpackStructImpl(const T& t, IndexSequence<I...>, int) |
| 3124 |
-> decltype(std::tie(get<I>(t)...)) { |
| 3125 |
static_assert(std::tuple_size<T>::value == sizeof...(I), |
| 3126 |
"Number of arguments doesn't match the number of fields."); |
| 3127 |
return std::tie(get<I>(t)...); |
| 3128 |
} |
| 3129 |
|
| 3130 |
#if defined(__cpp_structured_bindings) && __cpp_structured_bindings >= 201606 |
| 3131 |
template <typename T> |
| 3132 |
auto UnpackStructImpl(const T& t, MakeIndexSequence<1>, char) { |
| 3133 |
const auto& [a] = t; |
| 3134 |
return std::tie(a); |
| 3135 |
} |
| 3136 |
template <typename T> |
| 3137 |
auto UnpackStructImpl(const T& t, MakeIndexSequence<2>, char) { |
| 3138 |
const auto& [a, b] = t; |
| 3139 |
return std::tie(a, b); |
| 3140 |
} |
| 3141 |
template <typename T> |
| 3142 |
auto UnpackStructImpl(const T& t, MakeIndexSequence<3>, char) { |
| 3143 |
const auto& [a, b, c] = t; |
| 3144 |
return std::tie(a, b, c); |
| 3145 |
} |
| 3146 |
template <typename T> |
| 3147 |
auto UnpackStructImpl(const T& t, MakeIndexSequence<4>, char) { |
| 3148 |
const auto& [a, b, c, d] = t; |
| 3149 |
return std::tie(a, b, c, d); |
| 3150 |
} |
| 3151 |
template <typename T> |
| 3152 |
auto UnpackStructImpl(const T& t, MakeIndexSequence<5>, char) { |
| 3153 |
const auto& [a, b, c, d, e] = t; |
| 3154 |
return std::tie(a, b, c, d, e); |
| 3155 |
} |
| 3156 |
template <typename T> |
| 3157 |
auto UnpackStructImpl(const T& t, MakeIndexSequence<6>, char) { |
| 3158 |
const auto& [a, b, c, d, e, f] = t; |
| 3159 |
return std::tie(a, b, c, d, e, f); |
| 3160 |
} |
| 3161 |
template <typename T> |
| 3162 |
auto UnpackStructImpl(const T& t, MakeIndexSequence<7>, char) { |
| 3163 |
const auto& [a, b, c, d, e, f, g] = t; |
| 3164 |
return std::tie(a, b, c, d, e, f, g); |
| 3165 |
} |
| 3166 |
template <typename T> |
| 3167 |
auto UnpackStructImpl(const T& t, MakeIndexSequence<8>, char) { |
| 3168 |
const auto& [a, b, c, d, e, f, g, h] = t; |
| 3169 |
return std::tie(a, b, c, d, e, f, g, h); |
| 3170 |
} |
| 3171 |
template <typename T> |
| 3172 |
auto UnpackStructImpl(const T& t, MakeIndexSequence<9>, char) { |
| 3173 |
const auto& [a, b, c, d, e, f, g, h, i] = t; |
| 3174 |
return std::tie(a, b, c, d, e, f, g, h, i); |
| 3175 |
} |
| 3176 |
template <typename T> |
| 3177 |
auto UnpackStructImpl(const T& t, MakeIndexSequence<10>, char) { |
| 3178 |
const auto& [a, b, c, d, e, f, g, h, i, j] = t; |
| 3179 |
return std::tie(a, b, c, d, e, f, g, h, i, j); |
| 3180 |
} |
| 3181 |
template <typename T> |
| 3182 |
auto UnpackStructImpl(const T& t, MakeIndexSequence<11>, char) { |
| 3183 |
const auto& [a, b, c, d, e, f, g, h, i, j, k] = t; |
| 3184 |
return std::tie(a, b, c, d, e, f, g, h, i, j, k); |
| 3185 |
} |
| 3186 |
template <typename T> |
| 3187 |
auto UnpackStructImpl(const T& t, MakeIndexSequence<12>, char) { |
| 3188 |
const auto& [a, b, c, d, e, f, g, h, i, j, k, l] = t; |
| 3189 |
return std::tie(a, b, c, d, e, f, g, h, i, j, k, l); |
| 3190 |
} |
| 3191 |
template <typename T> |
| 3192 |
auto UnpackStructImpl(const T& t, MakeIndexSequence<13>, char) { |
| 3193 |
const auto& [a, b, c, d, e, f, g, h, i, j, k, l, m] = t; |
| 3194 |
return std::tie(a, b, c, d, e, f, g, h, i, j, k, l, m); |
| 3195 |
} |
| 3196 |
template <typename T> |
| 3197 |
auto UnpackStructImpl(const T& t, MakeIndexSequence<14>, char) { |
| 3198 |
const auto& [a, b, c, d, e, f, g, h, i, j, k, l, m, n] = t; |
| 3199 |
return std::tie(a, b, c, d, e, f, g, h, i, j, k, l, m, n); |
| 3200 |
} |
| 3201 |
template <typename T> |
| 3202 |
auto UnpackStructImpl(const T& t, MakeIndexSequence<15>, char) { |
| 3203 |
const auto& [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o] = t; |
| 3204 |
return std::tie(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o); |
| 3205 |
} |
| 3206 |
template <typename T> |
| 3207 |
auto UnpackStructImpl(const T& t, MakeIndexSequence<16>, char) { |
| 3208 |
const auto& [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p] = t; |
| 3209 |
return std::tie(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p); |
| 3210 |
} |
| 3211 |
#endif // defined(__cpp_structured_bindings) |
| 3212 |
|
| 3213 |
template <size_t I, typename T> |
| 3214 |
auto UnpackStruct(const T& t) |
| 3215 |
-> decltype((UnpackStructImpl)(t, MakeIndexSequence<I>{}, 0)) { |
| 3216 |
return (UnpackStructImpl)(t, MakeIndexSequence<I>{}, 0); |
| 3217 |
} |
| 3218 |
|
| 3219 |
// Helper function to do comma folding in C++11. |
| 3220 |
// The array ensures left-to-right order of evaluation. |
| 3221 |
// Usage: VariadicExpand({expr...}); |
| 3222 |
template <typename T, size_t N> |
| 3223 |
void VariadicExpand(const T (&)[N]) {} |
| 3224 |
|
| 3225 |
template <typename Struct, typename StructSize> |
| 3226 |
class FieldsAreMatcherImpl; |
| 3227 |
|
| 3228 |
template <typename Struct, size_t... I> |
| 3229 |
class FieldsAreMatcherImpl<Struct, IndexSequence<I...>> |
| 3230 |
: public MatcherInterface<Struct> { |
| 3231 |
using UnpackedType = |
| 3232 |
decltype(UnpackStruct<sizeof...(I)>(std::declval<const Struct&>())); |
| 3233 |
using MatchersType = std::tuple< |
| 3234 |
Matcher<const typename std::tuple_element<I, UnpackedType>::type&>...>; |
| 3235 |
|
| 3236 |
public: |
| 3237 |
template <typename Inner> |
| 3238 |
explicit FieldsAreMatcherImpl(const Inner& matchers) |
| 3239 |
: matchers_(testing::SafeMatcherCast< |
| 3240 |
const typename std::tuple_element<I, UnpackedType>::type&>( |
| 3241 |
std::get<I>(matchers))...) {} |
| 3242 |
|
| 3243 |
void DescribeTo(::std::ostream* os) const override { |
| 3244 |
const char* separator = ""; |
| 3245 |
VariadicExpand( |
| 3246 |
{(*os << separator << "has field #" << I << " that ", |
| 3247 |
std::get<I>(matchers_).DescribeTo(os), separator = ", and ")...}); |
| 3248 |
} |
| 3249 |
|
| 3250 |
void DescribeNegationTo(::std::ostream* os) const override { |
| 3251 |
const char* separator = ""; |
| 3252 |
VariadicExpand({(*os << separator << "has field #" << I << " that ", |
| 3253 |
std::get<I>(matchers_).DescribeNegationTo(os), |
| 3254 |
separator = ", or ")...}); |
| 3255 |
} |
| 3256 |
|
| 3257 |
bool MatchAndExplain(Struct t, MatchResultListener* listener) const override { |
| 3258 |
return MatchInternal((UnpackStruct<sizeof...(I)>)(t), listener); |
| 3259 |
} |
| 3260 |
|
| 3261 |
private: |
| 3262 |
bool MatchInternal(UnpackedType tuple, MatchResultListener* listener) const { |
| 3263 |
if (!listener->IsInterested()) { |
| 3264 |
// If the listener is not interested, we don't need to construct the |
| 3265 |
// explanation. |
| 3266 |
bool good = true; |
| 3267 |
VariadicExpand({good = good && std::get<I>(matchers_).Matches( |
| 3268 |
std::get<I>(tuple))...}); |
| 3269 |
return good; |
| 3270 |
} |
| 3271 |
|
| 3272 |
size_t failed_pos = ~size_t{}; |
| 3273 |
|
| 3274 |
std::vector<StringMatchResultListener> inner_listener(sizeof...(I)); |
| 3275 |
|
| 3276 |
VariadicExpand( |
| 3277 |
{failed_pos == ~size_t{} && !std::get<I>(matchers_).MatchAndExplain( |
| 3278 |
std::get<I>(tuple), &inner_listener[I]) |
| 3279 |
? failed_pos = I |
| 3280 |
: 0 ...}); |
| 3281 |
if (failed_pos != ~size_t{}) { |
| 3282 |
*listener << "whose field #" << failed_pos << " does not match"; |
| 3283 |
PrintIfNotEmpty(inner_listener[failed_pos].str(), listener->stream()); |
| 3284 |
return false; |
| 3285 |
} |
| 3286 |
|
| 3287 |
*listener << "whose all elements match"; |
| 3288 |
const char* separator = ", where"; |
| 3289 |
for (size_t index = 0; index < sizeof...(I); ++index) { |
| 3290 |
const std::string str = inner_listener[index].str(); |
| 3291 |
if (!str.empty()) { |
| 3292 |
*listener << separator << " field #" << index << " is a value " << str; |
| 3293 |
separator = ", and"; |
| 3294 |
} |
| 3295 |
} |
| 3296 |
|
| 3297 |
return true; |
| 3298 |
} |
| 3299 |
|
| 3300 |
MatchersType matchers_; |
| 3301 |
}; |
| 3302 |
|
| 3303 |
template <typename... Inner> |
| 3304 |
class FieldsAreMatcher { |
| 3305 |
public: |
| 3306 |
explicit FieldsAreMatcher(Inner... inner) : matchers_(std::move(inner)...) {} |
| 3307 |
|
| 3308 |
template <typename Struct> |
| 3309 |
operator Matcher<Struct>() const { // NOLINT |
| 3310 |
return Matcher<Struct>( |
| 3311 |
new FieldsAreMatcherImpl<const Struct&, IndexSequenceFor<Inner...>>( |
| 3312 |
matchers_)); |
| 3313 |
} |
| 3314 |
|
| 3315 |
private: |
| 3316 |
std::tuple<Inner...> matchers_; |
| 3317 |
}; |
| 3318 |
|
| 3319 |
// Implements ElementsAre() and ElementsAreArray(). |
| 3320 |
template <typename Container> |
| 3321 |
class ElementsAreMatcherImpl : public MatcherInterface<Container> { |
| 3322 |
public: |
| 3323 |
typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer; |
| 3324 |
typedef internal::StlContainerView<RawContainer> View; |
| 3325 |
typedef typename View::type StlContainer; |
| 3326 |
typedef typename View::const_reference StlContainerReference; |
| 3327 |
typedef typename StlContainer::value_type Element; |
| 3328 |
|
| 3329 |
// Constructs the matcher from a sequence of element values or |
| 3330 |
// element matchers. |
| 3331 |
template <typename InputIter> |
| 3332 |
ElementsAreMatcherImpl(InputIter first, InputIter last) { |
| 3333 |
while (first != last) { |
| 3334 |
matchers_.push_back(MatcherCast<const Element&>(*first++)); |
| 3335 |
} |
| 3336 |
} |
| 3337 |
|
| 3338 |
// Describes what this matcher does. |
| 3339 |
void DescribeTo(::std::ostream* os) const override { |
| 3340 |
if (count() == 0) { |
| 3341 |
*os << "is empty"; |
| 3342 |
} else if (count() == 1) { |
| 3343 |
*os << "has 1 element that "; |
| 3344 |
matchers_[0].DescribeTo(os); |
| 3345 |
} else { |
| 3346 |
*os << "has " << Elements(count()) << " where\n"; |
| 3347 |
for (size_t i = 0; i != count(); ++i) { |
| 3348 |
*os << "element #" << i << " "; |
| 3349 |
matchers_[i].DescribeTo(os); |
| 3350 |
if (i + 1 < count()) { |
| 3351 |
*os << ",\n"; |
| 3352 |
} |
| 3353 |
} |
| 3354 |
} |
| 3355 |
} |
| 3356 |
|
| 3357 |
// Describes what the negation of this matcher does. |
| 3358 |
void DescribeNegationTo(::std::ostream* os) const override { |
| 3359 |
if (count() == 0) { |
| 3360 |
*os << "isn't empty"; |
| 3361 |
return; |
| 3362 |
} |
| 3363 |
|
| 3364 |
*os << "doesn't have " << Elements(count()) << ", or\n"; |
| 3365 |
for (size_t i = 0; i != count(); ++i) { |
| 3366 |
*os << "element #" << i << " "; |
| 3367 |
matchers_[i].DescribeNegationTo(os); |
| 3368 |
if (i + 1 < count()) { |
| 3369 |
*os << ", or\n"; |
| 3370 |
} |
| 3371 |
} |
| 3372 |
} |
| 3373 |
|
| 3374 |
bool MatchAndExplain(Container container, |
| 3375 |
MatchResultListener* listener) const override { |
| 3376 |
// To work with stream-like "containers", we must only walk |
| 3377 |
// through the elements in one pass. |
| 3378 |
|
| 3379 |
const bool listener_interested = listener->IsInterested(); |
| 3380 |
|
| 3381 |
// explanations[i] is the explanation of the element at index i. |
| 3382 |
::std::vector<std::string> explanations(count()); |
| 3383 |
StlContainerReference stl_container = View::ConstReference(container); |
| 3384 |
auto it = stl_container.begin(); |
| 3385 |
size_t exam_pos = 0; |
| 3386 |
bool mismatch_found = false; // Have we found a mismatched element yet? |
| 3387 |
|
| 3388 |
// Go through the elements and matchers in pairs, until we reach |
| 3389 |
// the end of either the elements or the matchers, or until we find a |
| 3390 |
// mismatch. |
| 3391 |
for (; it != stl_container.end() && exam_pos != count(); ++it, ++exam_pos) { |
| 3392 |
bool match; // Does the current element match the current matcher? |
| 3393 |
if (listener_interested) { |
| 3394 |
StringMatchResultListener s; |
| 3395 |
match = matchers_[exam_pos].MatchAndExplain(*it, &s); |
| 3396 |
explanations[exam_pos] = s.str(); |
| 3397 |
} else { |
| 3398 |
match = matchers_[exam_pos].Matches(*it); |
| 3399 |
} |
| 3400 |
|
| 3401 |
if (!match) { |
| 3402 |
mismatch_found = true; |
| 3403 |
break; |
| 3404 |
} |
| 3405 |
} |
| 3406 |
// If mismatch_found is true, 'exam_pos' is the index of the mismatch. |
| 3407 |
|
| 3408 |
// Find how many elements the actual container has. We avoid |
| 3409 |
// calling size() s.t. this code works for stream-like "containers" |
| 3410 |
// that don't define size(). |
| 3411 |
size_t actual_count = exam_pos; |
| 3412 |
for (; it != stl_container.end(); ++it) { |
| 3413 |
++actual_count; |
| 3414 |
} |
| 3415 |
|
| 3416 |
if (actual_count != count()) { |
| 3417 |
// The element count doesn't match. If the container is empty, |
| 3418 |
// there's no need to explain anything as Google Mock already |
| 3419 |
// prints the empty container. Otherwise we just need to show |
| 3420 |
// how many elements there actually are. |
| 3421 |
if (listener_interested && (actual_count != 0)) { |
| 3422 |
*listener << "which has " << Elements(actual_count); |
| 3423 |
} |
| 3424 |
return false; |
| 3425 |
} |
| 3426 |
|
| 3427 |
if (mismatch_found) { |
| 3428 |
// The element count matches, but the exam_pos-th element doesn't match. |
| 3429 |
if (listener_interested) { |
| 3430 |
*listener << "whose element #" << exam_pos << " doesn't match"; |
| 3431 |
PrintIfNotEmpty(explanations[exam_pos], listener->stream()); |
| 3432 |
} |
| 3433 |
return false; |
| 3434 |
} |
| 3435 |
|
| 3436 |
// Every element matches its expectation. We need to explain why |
| 3437 |
// (the obvious ones can be skipped). |
| 3438 |
if (listener_interested) { |
| 3439 |
bool reason_printed = false; |
| 3440 |
for (size_t i = 0; i != count(); ++i) { |
| 3441 |
const std::string& s = explanations[i]; |
| 3442 |
if (!s.empty()) { |
| 3443 |
if (reason_printed) { |
| 3444 |
*listener << ",\nand "; |
| 3445 |
} |
| 3446 |
*listener << "whose element #" << i << " matches, " << s; |
| 3447 |
reason_printed = true; |
| 3448 |
} |
| 3449 |
} |
| 3450 |
} |
| 3451 |
return true; |
| 3452 |
} |
| 3453 |
|
| 3454 |
private: |
| 3455 |
static Message Elements(size_t count) { |
| 3456 |
return Message() << count << (count == 1 ? " element" : " elements"); |
| 3457 |
} |
| 3458 |
|
| 3459 |
size_t count() const { return matchers_.size(); } |
| 3460 |
|
| 3461 |
::std::vector<Matcher<const Element&>> matchers_; |
| 3462 |
}; |
| 3463 |
|
| 3464 |
// Connectivity matrix of (elements X matchers), in element-major order. |
| 3465 |
// Initially, there are no edges. |
| 3466 |
// Use NextGraph() to iterate over all possible edge configurations. |
| 3467 |
// Use Randomize() to generate a random edge configuration. |
| 3468 |
class GTEST_API_ MatchMatrix { |
| 3469 |
public: |
| 3470 |
MatchMatrix(size_t num_elements, size_t num_matchers) |
| 3471 |
: num_elements_(num_elements), |
| 3472 |
num_matchers_(num_matchers), |
| 3473 |
matched_(num_elements_ * num_matchers_, 0) {} |
| 3474 |
|
| 3475 |
size_t LhsSize() const { return num_elements_; } |
| 3476 |
size_t RhsSize() const { return num_matchers_; } |
| 3477 |
bool HasEdge(size_t ilhs, size_t irhs) const { |
| 3478 |
return matched_[SpaceIndex(ilhs, irhs)] == 1; |
| 3479 |
} |
| 3480 |
void SetEdge(size_t ilhs, size_t irhs, bool b) { |
| 3481 |
matched_[SpaceIndex(ilhs, irhs)] = b ? 1 : 0; |
| 3482 |
} |
| 3483 |
|
| 3484 |
// Treating the connectivity matrix as a (LhsSize()*RhsSize())-bit number, |
| 3485 |
// adds 1 to that number; returns false if incrementing the graph left it |
| 3486 |
// empty. |
| 3487 |
bool NextGraph(); |
| 3488 |
|
| 3489 |
void Randomize(); |
| 3490 |
|
| 3491 |
std::string DebugString() const; |
| 3492 |
|
| 3493 |
private: |
| 3494 |
size_t SpaceIndex(size_t ilhs, size_t irhs) const { |
| 3495 |
return ilhs * num_matchers_ + irhs; |
| 3496 |
} |
| 3497 |
|
| 3498 |
size_t num_elements_; |
| 3499 |
size_t num_matchers_; |
| 3500 |
|
| 3501 |
// Each element is a char interpreted as bool. They are stored as a |
| 3502 |
// flattened array in lhs-major order, use 'SpaceIndex()' to translate |
| 3503 |
// a (ilhs, irhs) matrix coordinate into an offset. |
| 3504 |
::std::vector<char> matched_; |
| 3505 |
}; |
| 3506 |
|
| 3507 |
typedef ::std::pair<size_t, size_t> ElementMatcherPair; |
| 3508 |
typedef ::std::vector<ElementMatcherPair> ElementMatcherPairs; |
| 3509 |
|
| 3510 |
// Returns a maximum bipartite matching for the specified graph 'g'. |
| 3511 |
// The matching is represented as a vector of {element, matcher} pairs. |
| 3512 |
GTEST_API_ ElementMatcherPairs FindMaxBipartiteMatching(const MatchMatrix& g); |
| 3513 |
|
| 3514 |
struct UnorderedMatcherRequire { |
| 3515 |
enum Flags { |
| 3516 |
Superset = 1 << 0, |
| 3517 |
Subset = 1 << 1, |
| 3518 |
ExactMatch = Superset | Subset, |
| 3519 |
}; |
| 3520 |
}; |
| 3521 |
|
| 3522 |
// Untyped base class for implementing UnorderedElementsAre. By |
| 3523 |
// putting logic that's not specific to the element type here, we |
| 3524 |
// reduce binary bloat and increase compilation speed. |
| 3525 |
class GTEST_API_ UnorderedElementsAreMatcherImplBase { |
| 3526 |
protected: |
| 3527 |
explicit UnorderedElementsAreMatcherImplBase( |
| 3528 |
UnorderedMatcherRequire::Flags matcher_flags) |
| 3529 |
: match_flags_(matcher_flags) {} |
| 3530 |
|
| 3531 |
// A vector of matcher describers, one for each element matcher. |
| 3532 |
// Does not own the describers (and thus can be used only when the |
| 3533 |
// element matchers are alive). |
| 3534 |
typedef ::std::vector<const MatcherDescriberInterface*> MatcherDescriberVec; |
| 3535 |
|
| 3536 |
// Describes this UnorderedElementsAre matcher. |
| 3537 |
void DescribeToImpl(::std::ostream* os) const; |
| 3538 |
|
| 3539 |
// Describes the negation of this UnorderedElementsAre matcher. |
| 3540 |
void DescribeNegationToImpl(::std::ostream* os) const; |
| 3541 |
|
| 3542 |
bool VerifyMatchMatrix(const ::std::vector<std::string>& element_printouts, |
| 3543 |
const MatchMatrix& matrix, |
| 3544 |
MatchResultListener* listener) const; |
| 3545 |
|
| 3546 |
bool FindPairing(const MatchMatrix& matrix, |
| 3547 |
MatchResultListener* listener) const; |
| 3548 |
|
| 3549 |
MatcherDescriberVec& matcher_describers() { return matcher_describers_; } |
| 3550 |
|
| 3551 |
static Message Elements(size_t n) { |
| 3552 |
return Message() << n << " element" << (n == 1 ? "" : "s"); |
| 3553 |
} |
| 3554 |
|
| 3555 |
UnorderedMatcherRequire::Flags match_flags() const { return match_flags_; } |
| 3556 |
|
| 3557 |
private: |
| 3558 |
UnorderedMatcherRequire::Flags match_flags_; |
| 3559 |
MatcherDescriberVec matcher_describers_; |
| 3560 |
}; |
| 3561 |
|
| 3562 |
// Implements UnorderedElementsAre, UnorderedElementsAreArray, IsSubsetOf, and |
| 3563 |
// IsSupersetOf. |
| 3564 |
template <typename Container> |
| 3565 |
class UnorderedElementsAreMatcherImpl |
| 3566 |
: public MatcherInterface<Container>, |
| 3567 |
public UnorderedElementsAreMatcherImplBase { |
| 3568 |
public: |
| 3569 |
typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer; |
| 3570 |
typedef internal::StlContainerView<RawContainer> View; |
| 3571 |
typedef typename View::type StlContainer; |
| 3572 |
typedef typename View::const_reference StlContainerReference; |
| 3573 |
typedef typename StlContainer::value_type Element; |
| 3574 |
|
| 3575 |
template <typename InputIter> |
| 3576 |
UnorderedElementsAreMatcherImpl(UnorderedMatcherRequire::Flags matcher_flags, |
| 3577 |
InputIter first, InputIter last) |
| 3578 |
: UnorderedElementsAreMatcherImplBase(matcher_flags) { |
| 3579 |
for (; first != last; ++first) { |
| 3580 |
matchers_.push_back(MatcherCast<const Element&>(*first)); |
| 3581 |
} |
| 3582 |
for (const auto& m : matchers_) { |
| 3583 |
matcher_describers().push_back(m.GetDescriber()); |
| 3584 |
} |
| 3585 |
} |
| 3586 |
|
| 3587 |
// Describes what this matcher does. |
| 3588 |
void DescribeTo(::std::ostream* os) const override { |
| 3589 |
return UnorderedElementsAreMatcherImplBase::DescribeToImpl(os); |
| 3590 |
} |
| 3591 |
|
| 3592 |
// Describes what the negation of this matcher does. |
| 3593 |
void DescribeNegationTo(::std::ostream* os) const override { |
| 3594 |
return UnorderedElementsAreMatcherImplBase::DescribeNegationToImpl(os); |
| 3595 |
} |
| 3596 |
|
| 3597 |
bool MatchAndExplain(Container container, |
| 3598 |
MatchResultListener* listener) const override { |
| 3599 |
StlContainerReference stl_container = View::ConstReference(container); |
| 3600 |
::std::vector<std::string> element_printouts; |
| 3601 |
MatchMatrix matrix = |
| 3602 |
AnalyzeElements(stl_container.begin(), stl_container.end(), |
| 3603 |
&element_printouts, listener); |
| 3604 |
|
| 3605 |
if (matrix.LhsSize() == 0 && matrix.RhsSize() == 0) { |
| 3606 |
return true; |
| 3607 |
} |
| 3608 |
|
| 3609 |
if (match_flags() == UnorderedMatcherRequire::ExactMatch) { |
| 3610 |
if (matrix.LhsSize() != matrix.RhsSize()) { |
| 3611 |
// The element count doesn't match. If the container is empty, |
| 3612 |
// there's no need to explain anything as Google Mock already |
| 3613 |
// prints the empty container. Otherwise we just need to show |
| 3614 |
// how many elements there actually are. |
| 3615 |
if (matrix.LhsSize() != 0 && listener->IsInterested()) { |
| 3616 |
*listener << "which has " << Elements(matrix.LhsSize()); |
| 3617 |
} |
| 3618 |
return false; |
| 3619 |
} |
| 3620 |
} |
| 3621 |
|
| 3622 |
return VerifyMatchMatrix(element_printouts, matrix, listener) && |
| 3623 |
FindPairing(matrix, listener); |
| 3624 |
} |
| 3625 |
|
| 3626 |
private: |
| 3627 |
template <typename ElementIter> |
| 3628 |
MatchMatrix AnalyzeElements(ElementIter elem_first, ElementIter elem_last, |
| 3629 |
::std::vector<std::string>* element_printouts, |
| 3630 |
MatchResultListener* listener) const { |
| 3631 |
element_printouts->clear(); |
| 3632 |
::std::vector<char> did_match; |
| 3633 |
size_t num_elements = 0; |
| 3634 |
DummyMatchResultListener dummy; |
| 3635 |
for (; elem_first != elem_last; ++num_elements, ++elem_first) { |
| 3636 |
if (listener->IsInterested()) { |
| 3637 |
element_printouts->push_back(PrintToString(*elem_first)); |
| 3638 |
} |
| 3639 |
for (size_t irhs = 0; irhs != matchers_.size(); ++irhs) { |
| 3640 |
did_match.push_back( |
| 3641 |
matchers_[irhs].MatchAndExplain(*elem_first, &dummy)); |
| 3642 |
} |
| 3643 |
} |
| 3644 |
|
| 3645 |
MatchMatrix matrix(num_elements, matchers_.size()); |
| 3646 |
::std::vector<char>::const_iterator did_match_iter = did_match.begin(); |
| 3647 |
for (size_t ilhs = 0; ilhs != num_elements; ++ilhs) { |
| 3648 |
for (size_t irhs = 0; irhs != matchers_.size(); ++irhs) { |
| 3649 |
matrix.SetEdge(ilhs, irhs, *did_match_iter++ != 0); |
| 3650 |
} |
| 3651 |
} |
| 3652 |
return matrix; |
| 3653 |
} |
| 3654 |
|
| 3655 |
::std::vector<Matcher<const Element&>> matchers_; |
| 3656 |
}; |
| 3657 |
|
| 3658 |
// Functor for use in TransformTuple. |
| 3659 |
// Performs MatcherCast<Target> on an input argument of any type. |
| 3660 |
template <typename Target> |
| 3661 |
struct CastAndAppendTransform { |
| 3662 |
template <typename Arg> |
| 3663 |
Matcher<Target> operator()(const Arg& a) const { |
| 3664 |
return MatcherCast<Target>(a); |
| 3665 |
} |
| 3666 |
}; |
| 3667 |
|
| 3668 |
// Implements UnorderedElementsAre. |
| 3669 |
template <typename MatcherTuple> |
| 3670 |
class UnorderedElementsAreMatcher { |
| 3671 |
public: |
| 3672 |
explicit UnorderedElementsAreMatcher(const MatcherTuple& args) |
| 3673 |
: matchers_(args) {} |
| 3674 |
|
| 3675 |
template <typename Container> |
| 3676 |
operator Matcher<Container>() const { |
| 3677 |
typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer; |
| 3678 |
typedef typename internal::StlContainerView<RawContainer>::type View; |
| 3679 |
typedef typename View::value_type Element; |
| 3680 |
typedef ::std::vector<Matcher<const Element&>> MatcherVec; |
| 3681 |
MatcherVec matchers; |
| 3682 |
matchers.reserve(::std::tuple_size<MatcherTuple>::value); |
| 3683 |
TransformTupleValues(CastAndAppendTransform<const Element&>(), matchers_, |
| 3684 |
::std::back_inserter(matchers)); |
| 3685 |
return Matcher<Container>( |
| 3686 |
new UnorderedElementsAreMatcherImpl<const Container&>( |
| 3687 |
UnorderedMatcherRequire::ExactMatch, matchers.begin(), |
| 3688 |
matchers.end())); |
| 3689 |
} |
| 3690 |
|
| 3691 |
private: |
| 3692 |
const MatcherTuple matchers_; |
| 3693 |
}; |
| 3694 |
|
| 3695 |
// Implements ElementsAre. |
| 3696 |
template <typename MatcherTuple> |
| 3697 |
class ElementsAreMatcher { |
| 3698 |
public: |
| 3699 |
explicit ElementsAreMatcher(const MatcherTuple& args) : matchers_(args) {} |
| 3700 |
|
| 3701 |
template <typename Container> |
| 3702 |
operator Matcher<Container>() const { |
| 3703 |
GTEST_COMPILE_ASSERT_( |
| 3704 |
!IsHashTable<GTEST_REMOVE_REFERENCE_AND_CONST_(Container)>::value || |
| 3705 |
::std::tuple_size<MatcherTuple>::value < 2, |
| 3706 |
use_UnorderedElementsAre_with_hash_tables); |
| 3707 |
|
| 3708 |
typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer; |
| 3709 |
typedef typename internal::StlContainerView<RawContainer>::type View; |
| 3710 |
typedef typename View::value_type Element; |
| 3711 |
typedef ::std::vector<Matcher<const Element&>> MatcherVec; |
| 3712 |
MatcherVec matchers; |
| 3713 |
matchers.reserve(::std::tuple_size<MatcherTuple>::value); |
| 3714 |
TransformTupleValues(CastAndAppendTransform<const Element&>(), matchers_, |
| 3715 |
::std::back_inserter(matchers)); |
| 3716 |
return Matcher<Container>(new ElementsAreMatcherImpl<const Container&>( |
| 3717 |
matchers.begin(), matchers.end())); |
| 3718 |
} |
| 3719 |
|
| 3720 |
private: |
| 3721 |
const MatcherTuple matchers_; |
| 3722 |
}; |
| 3723 |
|
| 3724 |
// Implements UnorderedElementsAreArray(), IsSubsetOf(), and IsSupersetOf(). |
| 3725 |
template <typename T> |
| 3726 |
class UnorderedElementsAreArrayMatcher { |
| 3727 |
public: |
| 3728 |
template <typename Iter> |
| 3729 |
UnorderedElementsAreArrayMatcher(UnorderedMatcherRequire::Flags match_flags, |
| 3730 |
Iter first, Iter last) |
| 3731 |
: match_flags_(match_flags), matchers_(first, last) {} |
| 3732 |
|
| 3733 |
template <typename Container> |
| 3734 |
operator Matcher<Container>() const { |
| 3735 |
return Matcher<Container>( |
| 3736 |
new UnorderedElementsAreMatcherImpl<const Container&>( |
| 3737 |
match_flags_, matchers_.begin(), matchers_.end())); |
| 3738 |
} |
| 3739 |
|
| 3740 |
private: |
| 3741 |
UnorderedMatcherRequire::Flags match_flags_; |
| 3742 |
::std::vector<T> matchers_; |
| 3743 |
}; |
| 3744 |
|
| 3745 |
// Implements ElementsAreArray(). |
| 3746 |
template <typename T> |
| 3747 |
class ElementsAreArrayMatcher { |
| 3748 |
public: |
| 3749 |
template <typename Iter> |
| 3750 |
ElementsAreArrayMatcher(Iter first, Iter last) : matchers_(first, last) {} |
| 3751 |
|
| 3752 |
template <typename Container> |
| 3753 |
operator Matcher<Container>() const { |
| 3754 |
GTEST_COMPILE_ASSERT_( |
| 3755 |
!IsHashTable<GTEST_REMOVE_REFERENCE_AND_CONST_(Container)>::value, |
| 3756 |
use_UnorderedElementsAreArray_with_hash_tables); |
| 3757 |
|
| 3758 |
return Matcher<Container>(new ElementsAreMatcherImpl<const Container&>( |
| 3759 |
matchers_.begin(), matchers_.end())); |
| 3760 |
} |
| 3761 |
|
| 3762 |
private: |
| 3763 |
const ::std::vector<T> matchers_; |
| 3764 |
}; |
| 3765 |
|
| 3766 |
// Given a 2-tuple matcher tm of type Tuple2Matcher and a value second |
| 3767 |
// of type Second, BoundSecondMatcher<Tuple2Matcher, Second>(tm, |
| 3768 |
// second) is a polymorphic matcher that matches a value x if and only if |
| 3769 |
// tm matches tuple (x, second). Useful for implementing |
| 3770 |
// UnorderedPointwise() in terms of UnorderedElementsAreArray(). |
| 3771 |
// |
| 3772 |
// BoundSecondMatcher is copyable and assignable, as we need to put |
| 3773 |
// instances of this class in a vector when implementing |
| 3774 |
// UnorderedPointwise(). |
| 3775 |
template <typename Tuple2Matcher, typename Second> |
| 3776 |
class BoundSecondMatcher { |
| 3777 |
public: |
| 3778 |
BoundSecondMatcher(const Tuple2Matcher& tm, const Second& second) |
| 3779 |
: tuple2_matcher_(tm), second_value_(second) {} |
| 3780 |
|
| 3781 |
BoundSecondMatcher(const BoundSecondMatcher& other) = default; |
| 3782 |
|
| 3783 |
template <typename T> |
| 3784 |
operator Matcher<T>() const { |
| 3785 |
return MakeMatcher(new Impl<T>(tuple2_matcher_, second_value_)); |
| 3786 |
} |
| 3787 |
|
| 3788 |
// We have to define this for UnorderedPointwise() to compile in |
| 3789 |
// C++98 mode, as it puts BoundSecondMatcher instances in a vector, |
| 3790 |
// which requires the elements to be assignable in C++98. The |
| 3791 |
// compiler cannot generate the operator= for us, as Tuple2Matcher |
| 3792 |
// and Second may not be assignable. |
| 3793 |
// |
| 3794 |
// However, this should never be called, so the implementation just |
| 3795 |
// need to assert. |
| 3796 |
void operator=(const BoundSecondMatcher& /*rhs*/) { |
| 3797 |
GTEST_LOG_(FATAL) << "BoundSecondMatcher should never be assigned."; |
| 3798 |
} |
| 3799 |
|
| 3800 |
private: |
| 3801 |
template <typename T> |
| 3802 |
class Impl : public MatcherInterface<T> { |
| 3803 |
public: |
| 3804 |
typedef ::std::tuple<T, Second> ArgTuple; |
| 3805 |
|
| 3806 |
Impl(const Tuple2Matcher& tm, const Second& second) |
| 3807 |
: mono_tuple2_matcher_(SafeMatcherCast<const ArgTuple&>(tm)), |
| 3808 |
second_value_(second) {} |
| 3809 |
|
| 3810 |
void DescribeTo(::std::ostream* os) const override { |
| 3811 |
*os << "and "; |
| 3812 |
UniversalPrint(second_value_, os); |
| 3813 |
*os << " "; |
| 3814 |
mono_tuple2_matcher_.DescribeTo(os); |
| 3815 |
} |
| 3816 |
|
| 3817 |
bool MatchAndExplain(T x, MatchResultListener* listener) const override { |
| 3818 |
return mono_tuple2_matcher_.MatchAndExplain(ArgTuple(x, second_value_), |
| 3819 |
listener); |
| 3820 |
} |
| 3821 |
|
| 3822 |
private: |
| 3823 |
const Matcher<const ArgTuple&> mono_tuple2_matcher_; |
| 3824 |
const Second second_value_; |
| 3825 |
}; |
| 3826 |
|
| 3827 |
const Tuple2Matcher tuple2_matcher_; |
| 3828 |
const Second second_value_; |
| 3829 |
}; |
| 3830 |
|
| 3831 |
// Given a 2-tuple matcher tm and a value second, |
| 3832 |
// MatcherBindSecond(tm, second) returns a matcher that matches a |
| 3833 |
// value x if and only if tm matches tuple (x, second). Useful for |
| 3834 |
// implementing UnorderedPointwise() in terms of UnorderedElementsAreArray(). |
| 3835 |
template <typename Tuple2Matcher, typename Second> |
| 3836 |
BoundSecondMatcher<Tuple2Matcher, Second> MatcherBindSecond( |
| 3837 |
const Tuple2Matcher& tm, const Second& second) { |
| 3838 |
return BoundSecondMatcher<Tuple2Matcher, Second>(tm, second); |
| 3839 |
} |
| 3840 |
|
| 3841 |
// Returns the description for a matcher defined using the MATCHER*() |
| 3842 |
// macro where the user-supplied description string is "", if |
| 3843 |
// 'negation' is false; otherwise returns the description of the |
| 3844 |
// negation of the matcher. 'param_values' contains a list of strings |
| 3845 |
// that are the print-out of the matcher's parameters. |
| 3846 |
GTEST_API_ std::string FormatMatcherDescription( |
| 3847 |
bool negation, const char* matcher_name, |
| 3848 |
const std::vector<const char*>& param_names, const Strings& param_values); |
| 3849 |
|
| 3850 |
// Implements a matcher that checks the value of a optional<> type variable. |
| 3851 |
template <typename ValueMatcher> |
| 3852 |
class OptionalMatcher { |
| 3853 |
public: |
| 3854 |
explicit OptionalMatcher(const ValueMatcher& value_matcher) |
| 3855 |
: value_matcher_(value_matcher) {} |
| 3856 |
|
| 3857 |
template <typename Optional> |
| 3858 |
operator Matcher<Optional>() const { |
| 3859 |
return Matcher<Optional>(new Impl<const Optional&>(value_matcher_)); |
| 3860 |
} |
| 3861 |
|
| 3862 |
template <typename Optional> |
| 3863 |
class Impl : public MatcherInterface<Optional> { |
| 3864 |
public: |
| 3865 |
typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Optional) OptionalView; |
| 3866 |
typedef typename OptionalView::value_type ValueType; |
| 3867 |
explicit Impl(const ValueMatcher& value_matcher) |
| 3868 |
: value_matcher_(MatcherCast<ValueType>(value_matcher)) {} |
| 3869 |
|
| 3870 |
void DescribeTo(::std::ostream* os) const override { |
| 3871 |
*os << "value "; |
| 3872 |
value_matcher_.DescribeTo(os); |
| 3873 |
} |
| 3874 |
|
| 3875 |
void DescribeNegationTo(::std::ostream* os) const override { |
| 3876 |
*os << "value "; |
| 3877 |
value_matcher_.DescribeNegationTo(os); |
| 3878 |
} |
| 3879 |
|
| 3880 |
bool MatchAndExplain(Optional optional, |
| 3881 |
MatchResultListener* listener) const override { |
| 3882 |
if (!optional) { |
| 3883 |
*listener << "which is not engaged"; |
| 3884 |
return false; |
| 3885 |
} |
| 3886 |
const ValueType& value = *optional; |
| 3887 |
StringMatchResultListener value_listener; |
| 3888 |
const bool match = value_matcher_.MatchAndExplain(value, &value_listener); |
| 3889 |
*listener << "whose value " << PrintToString(value) |
| 3890 |
<< (match ? " matches" : " doesn't match"); |
| 3891 |
PrintIfNotEmpty(value_listener.str(), listener->stream()); |
| 3892 |
return match; |
| 3893 |
} |
| 3894 |
|
| 3895 |
private: |
| 3896 |
const Matcher<ValueType> value_matcher_; |
| 3897 |
}; |
| 3898 |
|
| 3899 |
private: |
| 3900 |
const ValueMatcher value_matcher_; |
| 3901 |
}; |
| 3902 |
|
| 3903 |
namespace variant_matcher { |
| 3904 |
// Overloads to allow VariantMatcher to do proper ADL lookup. |
| 3905 |
template <typename T> |
| 3906 |
void holds_alternative() {} |
| 3907 |
template <typename T> |
| 3908 |
void get() {} |
| 3909 |
|
| 3910 |
// Implements a matcher that checks the value of a variant<> type variable. |
| 3911 |
template <typename T> |
| 3912 |
class VariantMatcher { |
| 3913 |
public: |
| 3914 |
explicit VariantMatcher(::testing::Matcher<const T&> matcher) |
| 3915 |
: matcher_(std::move(matcher)) {} |
| 3916 |
|
| 3917 |
template <typename Variant> |
| 3918 |
bool MatchAndExplain(const Variant& value, |
| 3919 |
::testing::MatchResultListener* listener) const { |
| 3920 |
using std::get; |
| 3921 |
if (!listener->IsInterested()) { |
| 3922 |
return holds_alternative<T>(value) && matcher_.Matches(get<T>(value)); |
| 3923 |
} |
| 3924 |
|
| 3925 |
if (!holds_alternative<T>(value)) { |
| 3926 |
*listener << "whose value is not of type '" << GetTypeName() << "'"; |
| 3927 |
return false; |
| 3928 |
} |
| 3929 |
|
| 3930 |
const T& elem = get<T>(value); |
| 3931 |
StringMatchResultListener elem_listener; |
| 3932 |
const bool match = matcher_.MatchAndExplain(elem, &elem_listener); |
| 3933 |
*listener << "whose value " << PrintToString(elem) |
| 3934 |
<< (match ? " matches" : " doesn't match"); |
| 3935 |
PrintIfNotEmpty(elem_listener.str(), listener->stream()); |
| 3936 |
return match; |
| 3937 |
} |
| 3938 |
|
| 3939 |
void DescribeTo(std::ostream* os) const { |
| 3940 |
*os << "is a variant<> with value of type '" << GetTypeName() |
| 3941 |
<< "' and the value "; |
| 3942 |
matcher_.DescribeTo(os); |
| 3943 |
} |
| 3944 |
|
| 3945 |
void DescribeNegationTo(std::ostream* os) const { |
| 3946 |
*os << "is a variant<> with value of type other than '" << GetTypeName() |
| 3947 |
<< "' or the value "; |
| 3948 |
matcher_.DescribeNegationTo(os); |
| 3949 |
} |
| 3950 |
|
| 3951 |
private: |
| 3952 |
static std::string GetTypeName() { |
| 3953 |
#if GTEST_HAS_RTTI |
| 3954 |
GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_( |
| 3955 |
return internal::GetTypeName<T>()); |
| 3956 |
#endif |
| 3957 |
return "the element type"; |
| 3958 |
} |
| 3959 |
|
| 3960 |
const ::testing::Matcher<const T&> matcher_; |
| 3961 |
}; |
| 3962 |
|
| 3963 |
} // namespace variant_matcher |
| 3964 |
|
| 3965 |
namespace any_cast_matcher { |
| 3966 |
|
| 3967 |
// Overloads to allow AnyCastMatcher to do proper ADL lookup. |
| 3968 |
template <typename T> |
| 3969 |
void any_cast() {} |
| 3970 |
|
| 3971 |
// Implements a matcher that any_casts the value. |
| 3972 |
template <typename T> |
| 3973 |
class AnyCastMatcher { |
| 3974 |
public: |
| 3975 |
explicit AnyCastMatcher(const ::testing::Matcher<const T&>& matcher) |
| 3976 |
: matcher_(matcher) {} |
| 3977 |
|
| 3978 |
template <typename AnyType> |
| 3979 |
bool MatchAndExplain(const AnyType& value, |
| 3980 |
::testing::MatchResultListener* listener) const { |
| 3981 |
if (!listener->IsInterested()) { |
| 3982 |
const T* ptr = any_cast<T>(&value); |
| 3983 |
return ptr != nullptr && matcher_.Matches(*ptr); |
| 3984 |
} |
| 3985 |
|
| 3986 |
const T* elem = any_cast<T>(&value); |
| 3987 |
if (elem == nullptr) { |
| 3988 |
*listener << "whose value is not of type '" << GetTypeName() << "'"; |
| 3989 |
return false; |
| 3990 |
} |
| 3991 |
|
| 3992 |
StringMatchResultListener elem_listener; |
| 3993 |
const bool match = matcher_.MatchAndExplain(*elem, &elem_listener); |
| 3994 |
*listener << "whose value " << PrintToString(*elem) |
| 3995 |
<< (match ? " matches" : " doesn't match"); |
| 3996 |
PrintIfNotEmpty(elem_listener.str(), listener->stream()); |
| 3997 |
return match; |
| 3998 |
} |
| 3999 |
|
| 4000 |
void DescribeTo(std::ostream* os) const { |
| 4001 |
*os << "is an 'any' type with value of type '" << GetTypeName() |
| 4002 |
<< "' and the value "; |
| 4003 |
matcher_.DescribeTo(os); |
| 4004 |
} |
| 4005 |
|
| 4006 |
void DescribeNegationTo(std::ostream* os) const { |
| 4007 |
*os << "is an 'any' type with value of type other than '" << GetTypeName() |
| 4008 |
<< "' or the value "; |
| 4009 |
matcher_.DescribeNegationTo(os); |
| 4010 |
} |
| 4011 |
|
| 4012 |
private: |
| 4013 |
static std::string GetTypeName() { |
| 4014 |
#if GTEST_HAS_RTTI |
| 4015 |
GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_( |
| 4016 |
return internal::GetTypeName<T>()); |
| 4017 |
#endif |
| 4018 |
return "the element type"; |
| 4019 |
} |
| 4020 |
|
| 4021 |
const ::testing::Matcher<const T&> matcher_; |
| 4022 |
}; |
| 4023 |
|
| 4024 |
} // namespace any_cast_matcher |
| 4025 |
|
| 4026 |
// Implements the Args() matcher. |
| 4027 |
template <class ArgsTuple, size_t... k> |
| 4028 |
class ArgsMatcherImpl : public MatcherInterface<ArgsTuple> { |
| 4029 |
public: |
| 4030 |
using RawArgsTuple = typename std::decay<ArgsTuple>::type; |
| 4031 |
using SelectedArgs = |
| 4032 |
std::tuple<typename std::tuple_element<k, RawArgsTuple>::type...>; |
| 4033 |
using MonomorphicInnerMatcher = Matcher<const SelectedArgs&>; |
| 4034 |
|
| 4035 |
template <typename InnerMatcher> |
| 4036 |
explicit ArgsMatcherImpl(const InnerMatcher& inner_matcher) |
| 4037 |
: inner_matcher_(SafeMatcherCast<const SelectedArgs&>(inner_matcher)) {} |
| 4038 |
|
| 4039 |
bool MatchAndExplain(ArgsTuple args, |
| 4040 |
MatchResultListener* listener) const override { |
| 4041 |
// Workaround spurious C4100 on MSVC<=15.7 when k is empty. |
| 4042 |
(void)args; |
| 4043 |
const SelectedArgs& selected_args = |
| 4044 |
std::forward_as_tuple(std::get<k>(args)...); |
| 4045 |
if (!listener->IsInterested()) return inner_matcher_.Matches(selected_args); |
| 4046 |
|
| 4047 |
PrintIndices(listener->stream()); |
| 4048 |
*listener << "are " << PrintToString(selected_args); |
| 4049 |
|
| 4050 |
StringMatchResultListener inner_listener; |
| 4051 |
const bool match = |
| 4052 |
inner_matcher_.MatchAndExplain(selected_args, &inner_listener); |
| 4053 |
PrintIfNotEmpty(inner_listener.str(), listener->stream()); |
| 4054 |
return match; |
| 4055 |
} |
| 4056 |
|
| 4057 |
void DescribeTo(::std::ostream* os) const override { |
| 4058 |
*os << "are a tuple "; |
| 4059 |
PrintIndices(os); |
| 4060 |
inner_matcher_.DescribeTo(os); |
| 4061 |
} |
| 4062 |
|
| 4063 |
void DescribeNegationTo(::std::ostream* os) const override { |
| 4064 |
*os << "are a tuple "; |
| 4065 |
PrintIndices(os); |
| 4066 |
inner_matcher_.DescribeNegationTo(os); |
| 4067 |
} |
| 4068 |
|
| 4069 |
private: |
| 4070 |
// Prints the indices of the selected fields. |
| 4071 |
static void PrintIndices(::std::ostream* os) { |
| 4072 |
*os << "whose fields ("; |
| 4073 |
const char* sep = ""; |
| 4074 |
// Workaround spurious C4189 on MSVC<=15.7 when k is empty. |
| 4075 |
(void)sep; |
| 4076 |
const char* dummy[] = {"", (*os << sep << "#" << k, sep = ", ")...}; |
| 4077 |
(void)dummy; |
| 4078 |
*os << ") "; |
| 4079 |
} |
| 4080 |
|
| 4081 |
MonomorphicInnerMatcher inner_matcher_; |
| 4082 |
}; |
| 4083 |
|
| 4084 |
template <class InnerMatcher, size_t... k> |
| 4085 |
class ArgsMatcher { |
| 4086 |
public: |
| 4087 |
explicit ArgsMatcher(InnerMatcher inner_matcher) |
| 4088 |
: inner_matcher_(std::move(inner_matcher)) {} |
| 4089 |
|
| 4090 |
template <typename ArgsTuple> |
| 4091 |
operator Matcher<ArgsTuple>() const { // NOLINT |
| 4092 |
return MakeMatcher(new ArgsMatcherImpl<ArgsTuple, k...>(inner_matcher_)); |
| 4093 |
} |
| 4094 |
|
| 4095 |
private: |
| 4096 |
InnerMatcher inner_matcher_; |
| 4097 |
}; |
| 4098 |
|
| 4099 |
} // namespace internal |
| 4100 |
|
| 4101 |
// ElementsAreArray(iterator_first, iterator_last) |
| 4102 |
// ElementsAreArray(pointer, count) |
| 4103 |
// ElementsAreArray(array) |
| 4104 |
// ElementsAreArray(container) |
| 4105 |
// ElementsAreArray({ e1, e2, ..., en }) |
| 4106 |
// |
| 4107 |
// The ElementsAreArray() functions are like ElementsAre(...), except |
| 4108 |
// that they are given a homogeneous sequence rather than taking each |
| 4109 |
// element as a function argument. The sequence can be specified as an |
| 4110 |
// array, a pointer and count, a vector, an initializer list, or an |
| 4111 |
// STL iterator range. In each of these cases, the underlying sequence |
| 4112 |
// can be either a sequence of values or a sequence of matchers. |
| 4113 |
// |
| 4114 |
// All forms of ElementsAreArray() make a copy of the input matcher sequence. |
| 4115 |
|
| 4116 |
template <typename Iter> |
| 4117 |
inline internal::ElementsAreArrayMatcher< |
| 4118 |
typename ::std::iterator_traits<Iter>::value_type> |
| 4119 |
ElementsAreArray(Iter first, Iter last) { |
| 4120 |
typedef typename ::std::iterator_traits<Iter>::value_type T; |
| 4121 |
return internal::ElementsAreArrayMatcher<T>(first, last); |
| 4122 |
} |
| 4123 |
|
| 4124 |
template <typename T> |
| 4125 |
inline auto ElementsAreArray(const T* pointer, size_t count) |
| 4126 |
-> decltype(ElementsAreArray(pointer, pointer + count)) { |
| 4127 |
return ElementsAreArray(pointer, pointer + count); |
| 4128 |
} |
| 4129 |
|
| 4130 |
template <typename T, size_t N> |
| 4131 |
inline auto ElementsAreArray(const T (&array)[N]) |
| 4132 |
-> decltype(ElementsAreArray(array, N)) { |
| 4133 |
return ElementsAreArray(array, N); |
| 4134 |
} |
| 4135 |
|
| 4136 |
template <typename Container> |
| 4137 |
inline auto ElementsAreArray(const Container& container) |
| 4138 |
-> decltype(ElementsAreArray(container.begin(), container.end())) { |
| 4139 |
return ElementsAreArray(container.begin(), container.end()); |
| 4140 |
} |
| 4141 |
|
| 4142 |
template <typename T> |
| 4143 |
inline auto ElementsAreArray(::std::initializer_list<T> xs) |
| 4144 |
-> decltype(ElementsAreArray(xs.begin(), xs.end())) { |
| 4145 |
return ElementsAreArray(xs.begin(), xs.end()); |
| 4146 |
} |
| 4147 |
|
| 4148 |
// UnorderedElementsAreArray(iterator_first, iterator_last) |
| 4149 |
// UnorderedElementsAreArray(pointer, count) |
| 4150 |
// UnorderedElementsAreArray(array) |
| 4151 |
// UnorderedElementsAreArray(container) |
| 4152 |
// UnorderedElementsAreArray({ e1, e2, ..., en }) |
| 4153 |
// |
| 4154 |
// UnorderedElementsAreArray() verifies that a bijective mapping onto a |
| 4155 |
// collection of matchers exists. |
| 4156 |
// |
| 4157 |
// The matchers can be specified as an array, a pointer and count, a container, |
| 4158 |
// an initializer list, or an STL iterator range. In each of these cases, the |
| 4159 |
// underlying matchers can be either values or matchers. |
| 4160 |
|
| 4161 |
template <typename Iter> |
| 4162 |
inline internal::UnorderedElementsAreArrayMatcher< |
| 4163 |
typename ::std::iterator_traits<Iter>::value_type> |
| 4164 |
UnorderedElementsAreArray(Iter first, Iter last) { |
| 4165 |
typedef typename ::std::iterator_traits<Iter>::value_type T; |
| 4166 |
return internal::UnorderedElementsAreArrayMatcher<T>( |
| 4167 |
internal::UnorderedMatcherRequire::ExactMatch, first, last); |
| 4168 |
} |
| 4169 |
|
| 4170 |
template <typename T> |
| 4171 |
inline internal::UnorderedElementsAreArrayMatcher<T> UnorderedElementsAreArray( |
| 4172 |
const T* pointer, size_t count) { |
| 4173 |
return UnorderedElementsAreArray(pointer, pointer + count); |
| 4174 |
} |
| 4175 |
|
| 4176 |
template <typename T, size_t N> |
| 4177 |
inline internal::UnorderedElementsAreArrayMatcher<T> UnorderedElementsAreArray( |
| 4178 |
const T (&array)[N]) { |
| 4179 |
return UnorderedElementsAreArray(array, N); |
| 4180 |
} |
| 4181 |
|
| 4182 |
template <typename Container> |
| 4183 |
inline internal::UnorderedElementsAreArrayMatcher< |
| 4184 |
typename Container::value_type> |
| 4185 |
UnorderedElementsAreArray(const Container& container) { |
| 4186 |
return UnorderedElementsAreArray(container.begin(), container.end()); |
| 4187 |
} |
| 4188 |
|
| 4189 |
template <typename T> |
| 4190 |
inline internal::UnorderedElementsAreArrayMatcher<T> UnorderedElementsAreArray( |
| 4191 |
::std::initializer_list<T> xs) { |
| 4192 |
return UnorderedElementsAreArray(xs.begin(), xs.end()); |
| 4193 |
} |
| 4194 |
|
| 4195 |
// _ is a matcher that matches anything of any type. |
| 4196 |
// |
| 4197 |
// This definition is fine as: |
| 4198 |
// |
| 4199 |
// 1. The C++ standard permits using the name _ in a namespace that |
| 4200 |
// is not the global namespace or ::std. |
| 4201 |
// 2. The AnythingMatcher class has no data member or constructor, |
| 4202 |
// so it's OK to create global variables of this type. |
| 4203 |
// 3. c-style has approved of using _ in this case. |
| 4204 |
const internal::AnythingMatcher _ = {}; |
| 4205 |
// Creates a matcher that matches any value of the given type T. |
| 4206 |
template <typename T> |
| 4207 |
inline Matcher<T> A() { |
| 4208 |
return _; |
| 4209 |
} |
| 4210 |
|
| 4211 |
// Creates a matcher that matches any value of the given type T. |
| 4212 |
template <typename T> |
| 4213 |
inline Matcher<T> An() { |
| 4214 |
return _; |
| 4215 |
} |
| 4216 |
|
| 4217 |
template <typename T, typename M> |
| 4218 |
Matcher<T> internal::MatcherCastImpl<T, M>::CastImpl( |
| 4219 |
const M& value, std::false_type /* convertible_to_matcher */, |
| 4220 |
std::false_type /* convertible_to_T */) { |
| 4221 |
return Eq(value); |
| 4222 |
} |
| 4223 |
|
| 4224 |
// Creates a polymorphic matcher that matches any NULL pointer. |
| 4225 |
inline PolymorphicMatcher<internal::IsNullMatcher> IsNull() { |
| 4226 |
return MakePolymorphicMatcher(internal::IsNullMatcher()); |
| 4227 |
} |
| 4228 |
|
| 4229 |
// Creates a polymorphic matcher that matches any non-NULL pointer. |
| 4230 |
// This is convenient as Not(NULL) doesn't compile (the compiler |
| 4231 |
// thinks that that expression is comparing a pointer with an integer). |
| 4232 |
inline PolymorphicMatcher<internal::NotNullMatcher> NotNull() { |
| 4233 |
return MakePolymorphicMatcher(internal::NotNullMatcher()); |
| 4234 |
} |
| 4235 |
|
| 4236 |
// Creates a polymorphic matcher that matches any argument that |
| 4237 |
// references variable x. |
| 4238 |
template <typename T> |
| 4239 |
inline internal::RefMatcher<T&> Ref(T& x) { // NOLINT |
| 4240 |
return internal::RefMatcher<T&>(x); |
| 4241 |
} |
| 4242 |
|
| 4243 |
// Creates a polymorphic matcher that matches any NaN floating point. |
| 4244 |
inline PolymorphicMatcher<internal::IsNanMatcher> IsNan() { |
| 4245 |
return MakePolymorphicMatcher(internal::IsNanMatcher()); |
| 4246 |
} |
| 4247 |
|
| 4248 |
// Creates a matcher that matches any double argument approximately |
| 4249 |
// equal to rhs, where two NANs are considered unequal. |
| 4250 |
inline internal::FloatingEqMatcher<double> DoubleEq(double rhs) { |
| 4251 |
return internal::FloatingEqMatcher<double>(rhs, false); |
| 4252 |
} |
| 4253 |
|
| 4254 |
// Creates a matcher that matches any double argument approximately |
| 4255 |
// equal to rhs, including NaN values when rhs is NaN. |
| 4256 |
inline internal::FloatingEqMatcher<double> NanSensitiveDoubleEq(double rhs) { |
| 4257 |
return internal::FloatingEqMatcher<double>(rhs, true); |
| 4258 |
} |
| 4259 |
|
| 4260 |
// Creates a matcher that matches any double argument approximately equal to |
| 4261 |
// rhs, up to the specified max absolute error bound, where two NANs are |
| 4262 |
// considered unequal. The max absolute error bound must be non-negative. |
| 4263 |
inline internal::FloatingEqMatcher<double> DoubleNear(double rhs, |
| 4264 |
double max_abs_error) { |
| 4265 |
return internal::FloatingEqMatcher<double>(rhs, false, max_abs_error); |
| 4266 |
} |
| 4267 |
|
| 4268 |
// Creates a matcher that matches any double argument approximately equal to |
| 4269 |
// rhs, up to the specified max absolute error bound, including NaN values when |
| 4270 |
// rhs is NaN. The max absolute error bound must be non-negative. |
| 4271 |
inline internal::FloatingEqMatcher<double> NanSensitiveDoubleNear( |
| 4272 |
double rhs, double max_abs_error) { |
| 4273 |
return internal::FloatingEqMatcher<double>(rhs, true, max_abs_error); |
| 4274 |
} |
| 4275 |
|
| 4276 |
// Creates a matcher that matches any float argument approximately |
| 4277 |
// equal to rhs, where two NANs are considered unequal. |
| 4278 |
inline internal::FloatingEqMatcher<float> FloatEq(float rhs) { |
| 4279 |
return internal::FloatingEqMatcher<float>(rhs, false); |
| 4280 |
} |
| 4281 |
|
| 4282 |
// Creates a matcher that matches any float argument approximately |
| 4283 |
// equal to rhs, including NaN values when rhs is NaN. |
| 4284 |
inline internal::FloatingEqMatcher<float> NanSensitiveFloatEq(float rhs) { |
| 4285 |
return internal::FloatingEqMatcher<float>(rhs, true); |
| 4286 |
} |
| 4287 |
|
| 4288 |
// Creates a matcher that matches any float argument approximately equal to |
| 4289 |
// rhs, up to the specified max absolute error bound, where two NANs are |
| 4290 |
// considered unequal. The max absolute error bound must be non-negative. |
| 4291 |
inline internal::FloatingEqMatcher<float> FloatNear(float rhs, |
| 4292 |
float max_abs_error) { |
| 4293 |
return internal::FloatingEqMatcher<float>(rhs, false, max_abs_error); |
| 4294 |
} |
| 4295 |
|
| 4296 |
// Creates a matcher that matches any float argument approximately equal to |
| 4297 |
// rhs, up to the specified max absolute error bound, including NaN values when |
| 4298 |
// rhs is NaN. The max absolute error bound must be non-negative. |
| 4299 |
inline internal::FloatingEqMatcher<float> NanSensitiveFloatNear( |
| 4300 |
float rhs, float max_abs_error) { |
| 4301 |
return internal::FloatingEqMatcher<float>(rhs, true, max_abs_error); |
| 4302 |
} |
| 4303 |
|
| 4304 |
// Creates a matcher that matches a pointer (raw or smart) that points |
| 4305 |
// to a value that matches inner_matcher. |
| 4306 |
template <typename InnerMatcher> |
| 4307 |
inline internal::PointeeMatcher<InnerMatcher> Pointee( |
| 4308 |
const InnerMatcher& inner_matcher) { |
| 4309 |
return internal::PointeeMatcher<InnerMatcher>(inner_matcher); |
| 4310 |
} |
| 4311 |
|
| 4312 |
#if GTEST_HAS_RTTI |
| 4313 |
// Creates a matcher that matches a pointer or reference that matches |
| 4314 |
// inner_matcher when dynamic_cast<To> is applied. |
| 4315 |
// The result of dynamic_cast<To> is forwarded to the inner matcher. |
| 4316 |
// If To is a pointer and the cast fails, the inner matcher will receive NULL. |
| 4317 |
// If To is a reference and the cast fails, this matcher returns false |
| 4318 |
// immediately. |
| 4319 |
template <typename To> |
| 4320 |
inline PolymorphicMatcher<internal::WhenDynamicCastToMatcher<To>> |
| 4321 |
WhenDynamicCastTo(const Matcher<To>& inner_matcher) { |
| 4322 |
return MakePolymorphicMatcher( |
| 4323 |
internal::WhenDynamicCastToMatcher<To>(inner_matcher)); |
| 4324 |
} |
| 4325 |
#endif // GTEST_HAS_RTTI |
| 4326 |
|
| 4327 |
// Creates a matcher that matches an object whose given field matches |
| 4328 |
// 'matcher'. For example, |
| 4329 |
// Field(&Foo::number, Ge(5)) |
| 4330 |
// matches a Foo object x if and only if x.number >= 5. |
| 4331 |
template <typename Class, typename FieldType, typename FieldMatcher> |
| 4332 |
inline PolymorphicMatcher<internal::FieldMatcher<Class, FieldType>> Field( |
| 4333 |
FieldType Class::*field, const FieldMatcher& matcher) { |
| 4334 |
return MakePolymorphicMatcher(internal::FieldMatcher<Class, FieldType>( |
| 4335 |
field, MatcherCast<const FieldType&>(matcher))); |
| 4336 |
// The call to MatcherCast() is required for supporting inner |
| 4337 |
// matchers of compatible types. For example, it allows |
| 4338 |
// Field(&Foo::bar, m) |
| 4339 |
// to compile where bar is an int32 and m is a matcher for int64. |
| 4340 |
} |
| 4341 |
|
| 4342 |
// Same as Field() but also takes the name of the field to provide better error |
| 4343 |
// messages. |
| 4344 |
template <typename Class, typename FieldType, typename FieldMatcher> |
| 4345 |
inline PolymorphicMatcher<internal::FieldMatcher<Class, FieldType>> Field( |
| 4346 |
const std::string& field_name, FieldType Class::*field, |
| 4347 |
const FieldMatcher& matcher) { |
| 4348 |
return MakePolymorphicMatcher(internal::FieldMatcher<Class, FieldType>( |
| 4349 |
field_name, field, MatcherCast<const FieldType&>(matcher))); |
| 4350 |
} |
| 4351 |
|
| 4352 |
// Creates a matcher that matches an object whose given property |
| 4353 |
// matches 'matcher'. For example, |
| 4354 |
// Property(&Foo::str, StartsWith("hi")) |
| 4355 |
// matches a Foo object x if and only if x.str() starts with "hi". |
| 4356 |
template <typename Class, typename PropertyType, typename PropertyMatcher> |
| 4357 |
inline PolymorphicMatcher<internal::PropertyMatcher< |
| 4358 |
Class, PropertyType, PropertyType (Class::*)() const>> |
| 4359 |
Property(PropertyType (Class::*property)() const, |
| 4360 |
const PropertyMatcher& matcher) { |
| 4361 |
return MakePolymorphicMatcher( |
| 4362 |
internal::PropertyMatcher<Class, PropertyType, |
| 4363 |
PropertyType (Class::*)() const>( |
| 4364 |
property, MatcherCast<const PropertyType&>(matcher))); |
| 4365 |
// The call to MatcherCast() is required for supporting inner |
| 4366 |
// matchers of compatible types. For example, it allows |
| 4367 |
// Property(&Foo::bar, m) |
| 4368 |
// to compile where bar() returns an int32 and m is a matcher for int64. |
| 4369 |
} |
| 4370 |
|
| 4371 |
// Same as Property() above, but also takes the name of the property to provide |
| 4372 |
// better error messages. |
| 4373 |
template <typename Class, typename PropertyType, typename PropertyMatcher> |
| 4374 |
inline PolymorphicMatcher<internal::PropertyMatcher< |
| 4375 |
Class, PropertyType, PropertyType (Class::*)() const>> |
| 4376 |
Property(const std::string& property_name, |
| 4377 |
PropertyType (Class::*property)() const, |
| 4378 |
const PropertyMatcher& matcher) { |
| 4379 |
return MakePolymorphicMatcher( |
| 4380 |
internal::PropertyMatcher<Class, PropertyType, |
| 4381 |
PropertyType (Class::*)() const>( |
| 4382 |
property_name, property, MatcherCast<const PropertyType&>(matcher))); |
| 4383 |
} |
| 4384 |
|
| 4385 |
// The same as above but for reference-qualified member functions. |
| 4386 |
template <typename Class, typename PropertyType, typename PropertyMatcher> |
| 4387 |
inline PolymorphicMatcher<internal::PropertyMatcher< |
| 4388 |
Class, PropertyType, PropertyType (Class::*)() const&>> |
| 4389 |
Property(PropertyType (Class::*property)() const&, |
| 4390 |
const PropertyMatcher& matcher) { |
| 4391 |
return MakePolymorphicMatcher( |
| 4392 |
internal::PropertyMatcher<Class, PropertyType, |
| 4393 |
PropertyType (Class::*)() const&>( |
| 4394 |
property, MatcherCast<const PropertyType&>(matcher))); |
| 4395 |
} |
| 4396 |
|
| 4397 |
// Three-argument form for reference-qualified member functions. |
| 4398 |
template <typename Class, typename PropertyType, typename PropertyMatcher> |
| 4399 |
inline PolymorphicMatcher<internal::PropertyMatcher< |
| 4400 |
Class, PropertyType, PropertyType (Class::*)() const&>> |
| 4401 |
Property(const std::string& property_name, |
| 4402 |
PropertyType (Class::*property)() const&, |
| 4403 |
const PropertyMatcher& matcher) { |
| 4404 |
return MakePolymorphicMatcher( |
| 4405 |
internal::PropertyMatcher<Class, PropertyType, |
| 4406 |
PropertyType (Class::*)() const&>( |
| 4407 |
property_name, property, MatcherCast<const PropertyType&>(matcher))); |
| 4408 |
} |
| 4409 |
|
| 4410 |
// Creates a matcher that matches an object if and only if the result of |
| 4411 |
// applying a callable to x matches 'matcher'. For example, |
| 4412 |
// ResultOf(f, StartsWith("hi")) |
| 4413 |
// matches a Foo object x if and only if f(x) starts with "hi". |
| 4414 |
// `callable` parameter can be a function, function pointer, or a functor. It is |
| 4415 |
// required to keep no state affecting the results of the calls on it and make |
| 4416 |
// no assumptions about how many calls will be made. Any state it keeps must be |
| 4417 |
// protected from the concurrent access. |
| 4418 |
template <typename Callable, typename InnerMatcher> |
| 4419 |
internal::ResultOfMatcher<Callable, InnerMatcher> ResultOf( |
| 4420 |
Callable callable, InnerMatcher matcher) { |
| 4421 |
return internal::ResultOfMatcher<Callable, InnerMatcher>(std::move(callable), |
| 4422 |
std::move(matcher)); |
| 4423 |
} |
| 4424 |
|
| 4425 |
// String matchers. |
| 4426 |
|
| 4427 |
// Matches a string equal to str. |
| 4428 |
template <typename T = std::string> |
| 4429 |
PolymorphicMatcher<internal::StrEqualityMatcher<std::string>> StrEq( |
| 4430 |
const internal::StringLike<T>& str) { |
| 4431 |
return MakePolymorphicMatcher( |
| 4432 |
internal::StrEqualityMatcher<std::string>(std::string(str), true, true)); |
| 4433 |
} |
| 4434 |
|
| 4435 |
// Matches a string not equal to str. |
| 4436 |
template <typename T = std::string> |
| 4437 |
PolymorphicMatcher<internal::StrEqualityMatcher<std::string>> StrNe( |
| 4438 |
const internal::StringLike<T>& str) { |
| 4439 |
return MakePolymorphicMatcher( |
| 4440 |
internal::StrEqualityMatcher<std::string>(std::string(str), false, true)); |
| 4441 |
} |
| 4442 |
|
| 4443 |
// Matches a string equal to str, ignoring case. |
| 4444 |
template <typename T = std::string> |
| 4445 |
PolymorphicMatcher<internal::StrEqualityMatcher<std::string>> StrCaseEq( |
| 4446 |
const internal::StringLike<T>& str) { |
| 4447 |
return MakePolymorphicMatcher( |
| 4448 |
internal::StrEqualityMatcher<std::string>(std::string(str), true, false)); |
| 4449 |
} |
| 4450 |
|
| 4451 |
// Matches a string not equal to str, ignoring case. |
| 4452 |
template <typename T = std::string> |
| 4453 |
PolymorphicMatcher<internal::StrEqualityMatcher<std::string>> StrCaseNe( |
| 4454 |
const internal::StringLike<T>& str) { |
| 4455 |
return MakePolymorphicMatcher(internal::StrEqualityMatcher<std::string>( |
| 4456 |
std::string(str), false, false)); |
| 4457 |
} |
| 4458 |
|
| 4459 |
// Creates a matcher that matches any string, std::string, or C string |
| 4460 |
// that contains the given substring. |
| 4461 |
template <typename T = std::string> |
| 4462 |
PolymorphicMatcher<internal::HasSubstrMatcher<std::string>> HasSubstr( |
| 4463 |
const internal::StringLike<T>& substring) { |
| 4464 |
return MakePolymorphicMatcher( |
| 4465 |
internal::HasSubstrMatcher<std::string>(std::string(substring))); |
| 4466 |
} |
| 4467 |
|
| 4468 |
// Matches a string that starts with 'prefix' (case-sensitive). |
| 4469 |
template <typename T = std::string> |
| 4470 |
PolymorphicMatcher<internal::StartsWithMatcher<std::string>> StartsWith( |
| 4471 |
const internal::StringLike<T>& prefix) { |
| 4472 |
return MakePolymorphicMatcher( |
| 4473 |
internal::StartsWithMatcher<std::string>(std::string(prefix))); |
| 4474 |
} |
| 4475 |
|
| 4476 |
// Matches a string that ends with 'suffix' (case-sensitive). |
| 4477 |
template <typename T = std::string> |
| 4478 |
PolymorphicMatcher<internal::EndsWithMatcher<std::string>> EndsWith( |
| 4479 |
const internal::StringLike<T>& suffix) { |
| 4480 |
return MakePolymorphicMatcher( |
| 4481 |
internal::EndsWithMatcher<std::string>(std::string(suffix))); |
| 4482 |
} |
| 4483 |
|
| 4484 |
#if GTEST_HAS_STD_WSTRING |
| 4485 |
// Wide string matchers. |
| 4486 |
|
| 4487 |
// Matches a string equal to str. |
| 4488 |
inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring>> StrEq( |
| 4489 |
const std::wstring& str) { |
| 4490 |
return MakePolymorphicMatcher( |
| 4491 |
internal::StrEqualityMatcher<std::wstring>(str, true, true)); |
| 4492 |
} |
| 4493 |
|
| 4494 |
// Matches a string not equal to str. |
| 4495 |
inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring>> StrNe( |
| 4496 |
const std::wstring& str) { |
| 4497 |
return MakePolymorphicMatcher( |
| 4498 |
internal::StrEqualityMatcher<std::wstring>(str, false, true)); |
| 4499 |
} |
| 4500 |
|
| 4501 |
// Matches a string equal to str, ignoring case. |
| 4502 |
inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring>> StrCaseEq( |
| 4503 |
const std::wstring& str) { |
| 4504 |
return MakePolymorphicMatcher( |
| 4505 |
internal::StrEqualityMatcher<std::wstring>(str, true, false)); |
| 4506 |
} |
| 4507 |
|
| 4508 |
// Matches a string not equal to str, ignoring case. |
| 4509 |
inline PolymorphicMatcher<internal::StrEqualityMatcher<std::wstring>> StrCaseNe( |
| 4510 |
const std::wstring& str) { |
| 4511 |
return MakePolymorphicMatcher( |
| 4512 |
internal::StrEqualityMatcher<std::wstring>(str, false, false)); |
| 4513 |
} |
| 4514 |
|
| 4515 |
// Creates a matcher that matches any ::wstring, std::wstring, or C wide string |
| 4516 |
// that contains the given substring. |
| 4517 |
inline PolymorphicMatcher<internal::HasSubstrMatcher<std::wstring>> HasSubstr( |
| 4518 |
const std::wstring& substring) { |
| 4519 |
return MakePolymorphicMatcher( |
| 4520 |
internal::HasSubstrMatcher<std::wstring>(substring)); |
| 4521 |
} |
| 4522 |
|
| 4523 |
// Matches a string that starts with 'prefix' (case-sensitive). |
| 4524 |
inline PolymorphicMatcher<internal::StartsWithMatcher<std::wstring>> StartsWith( |
| 4525 |
const std::wstring& prefix) { |
| 4526 |
return MakePolymorphicMatcher( |
| 4527 |
internal::StartsWithMatcher<std::wstring>(prefix)); |
| 4528 |
} |
| 4529 |
|
| 4530 |
// Matches a string that ends with 'suffix' (case-sensitive). |
| 4531 |
inline PolymorphicMatcher<internal::EndsWithMatcher<std::wstring>> EndsWith( |
| 4532 |
const std::wstring& suffix) { |
| 4533 |
return MakePolymorphicMatcher( |
| 4534 |
internal::EndsWithMatcher<std::wstring>(suffix)); |
| 4535 |
} |
| 4536 |
|
| 4537 |
#endif // GTEST_HAS_STD_WSTRING |
| 4538 |
|
| 4539 |
// Creates a polymorphic matcher that matches a 2-tuple where the |
| 4540 |
// first field == the second field. |
| 4541 |
inline internal::Eq2Matcher Eq() { return internal::Eq2Matcher(); } |
| 4542 |
|
| 4543 |
// Creates a polymorphic matcher that matches a 2-tuple where the |
| 4544 |
// first field >= the second field. |
| 4545 |
inline internal::Ge2Matcher Ge() { return internal::Ge2Matcher(); } |
| 4546 |
|
| 4547 |
// Creates a polymorphic matcher that matches a 2-tuple where the |
| 4548 |
// first field > the second field. |
| 4549 |
inline internal::Gt2Matcher Gt() { return internal::Gt2Matcher(); } |
| 4550 |
|
| 4551 |
// Creates a polymorphic matcher that matches a 2-tuple where the |
| 4552 |
// first field <= the second field. |
| 4553 |
inline internal::Le2Matcher Le() { return internal::Le2Matcher(); } |
| 4554 |
|
| 4555 |
// Creates a polymorphic matcher that matches a 2-tuple where the |
| 4556 |
// first field < the second field. |
| 4557 |
inline internal::Lt2Matcher Lt() { return internal::Lt2Matcher(); } |
| 4558 |
|
| 4559 |
// Creates a polymorphic matcher that matches a 2-tuple where the |
| 4560 |
// first field != the second field. |
| 4561 |
inline internal::Ne2Matcher Ne() { return internal::Ne2Matcher(); } |
| 4562 |
|
| 4563 |
// Creates a polymorphic matcher that matches a 2-tuple where |
| 4564 |
// FloatEq(first field) matches the second field. |
| 4565 |
inline internal::FloatingEq2Matcher<float> FloatEq() { |
| 4566 |
return internal::FloatingEq2Matcher<float>(); |
| 4567 |
} |
| 4568 |
|
| 4569 |
// Creates a polymorphic matcher that matches a 2-tuple where |
| 4570 |
// DoubleEq(first field) matches the second field. |
| 4571 |
inline internal::FloatingEq2Matcher<double> DoubleEq() { |
| 4572 |
return internal::FloatingEq2Matcher<double>(); |
| 4573 |
} |
| 4574 |
|
| 4575 |
// Creates a polymorphic matcher that matches a 2-tuple where |
| 4576 |
// FloatEq(first field) matches the second field with NaN equality. |
| 4577 |
inline internal::FloatingEq2Matcher<float> NanSensitiveFloatEq() { |
| 4578 |
return internal::FloatingEq2Matcher<float>(true); |
| 4579 |
} |
| 4580 |
|
| 4581 |
// Creates a polymorphic matcher that matches a 2-tuple where |
| 4582 |
// DoubleEq(first field) matches the second field with NaN equality. |
| 4583 |
inline internal::FloatingEq2Matcher<double> NanSensitiveDoubleEq() { |
| 4584 |
return internal::FloatingEq2Matcher<double>(true); |
| 4585 |
} |
| 4586 |
|
| 4587 |
// Creates a polymorphic matcher that matches a 2-tuple where |
| 4588 |
// FloatNear(first field, max_abs_error) matches the second field. |
| 4589 |
inline internal::FloatingEq2Matcher<float> FloatNear(float max_abs_error) { |
| 4590 |
return internal::FloatingEq2Matcher<float>(max_abs_error); |
| 4591 |
} |
| 4592 |
|
| 4593 |
// Creates a polymorphic matcher that matches a 2-tuple where |
| 4594 |
// DoubleNear(first field, max_abs_error) matches the second field. |
| 4595 |
inline internal::FloatingEq2Matcher<double> DoubleNear(double max_abs_error) { |
| 4596 |
return internal::FloatingEq2Matcher<double>(max_abs_error); |
| 4597 |
} |
| 4598 |
|
| 4599 |
// Creates a polymorphic matcher that matches a 2-tuple where |
| 4600 |
// FloatNear(first field, max_abs_error) matches the second field with NaN |
| 4601 |
// equality. |
| 4602 |
inline internal::FloatingEq2Matcher<float> NanSensitiveFloatNear( |
| 4603 |
float max_abs_error) { |
| 4604 |
return internal::FloatingEq2Matcher<float>(max_abs_error, true); |
| 4605 |
} |
| 4606 |
|
| 4607 |
// Creates a polymorphic matcher that matches a 2-tuple where |
| 4608 |
// DoubleNear(first field, max_abs_error) matches the second field with NaN |
| 4609 |
// equality. |
| 4610 |
inline internal::FloatingEq2Matcher<double> NanSensitiveDoubleNear( |
| 4611 |
double max_abs_error) { |
| 4612 |
return internal::FloatingEq2Matcher<double>(max_abs_error, true); |
| 4613 |
} |
| 4614 |
|
| 4615 |
// Creates a matcher that matches any value of type T that m doesn't |
| 4616 |
// match. |
| 4617 |
template <typename InnerMatcher> |
| 4618 |
inline internal::NotMatcher<InnerMatcher> Not(InnerMatcher m) { |
| 4619 |
return internal::NotMatcher<InnerMatcher>(m); |
| 4620 |
} |
| 4621 |
|
| 4622 |
// Returns a matcher that matches anything that satisfies the given |
| 4623 |
// predicate. The predicate can be any unary function or functor |
| 4624 |
// whose return type can be implicitly converted to bool. |
| 4625 |
template <typename Predicate> |
| 4626 |
inline PolymorphicMatcher<internal::TrulyMatcher<Predicate>> Truly( |
| 4627 |
Predicate pred) { |
| 4628 |
return MakePolymorphicMatcher(internal::TrulyMatcher<Predicate>(pred)); |
| 4629 |
} |
| 4630 |
|
| 4631 |
// Returns a matcher that matches the container size. The container must |
| 4632 |
// support both size() and size_type which all STL-like containers provide. |
| 4633 |
// Note that the parameter 'size' can be a value of type size_type as well as |
| 4634 |
// matcher. For instance: |
| 4635 |
// EXPECT_THAT(container, SizeIs(2)); // Checks container has 2 elements. |
| 4636 |
// EXPECT_THAT(container, SizeIs(Le(2)); // Checks container has at most 2. |
| 4637 |
template <typename SizeMatcher> |
| 4638 |
inline internal::SizeIsMatcher<SizeMatcher> SizeIs( |
| 4639 |
const SizeMatcher& size_matcher) { |
| 4640 |
return internal::SizeIsMatcher<SizeMatcher>(size_matcher); |
| 4641 |
} |
| 4642 |
|
| 4643 |
// Returns a matcher that matches the distance between the container's begin() |
| 4644 |
// iterator and its end() iterator, i.e. the size of the container. This matcher |
| 4645 |
// can be used instead of SizeIs with containers such as std::forward_list which |
| 4646 |
// do not implement size(). The container must provide const_iterator (with |
| 4647 |
// valid iterator_traits), begin() and end(). |
| 4648 |
template <typename DistanceMatcher> |
| 4649 |
inline internal::BeginEndDistanceIsMatcher<DistanceMatcher> BeginEndDistanceIs( |
| 4650 |
const DistanceMatcher& distance_matcher) { |
| 4651 |
return internal::BeginEndDistanceIsMatcher<DistanceMatcher>(distance_matcher); |
| 4652 |
} |
| 4653 |
|
| 4654 |
// Returns a matcher that matches an equal container. |
| 4655 |
// This matcher behaves like Eq(), but in the event of mismatch lists the |
| 4656 |
// values that are included in one container but not the other. (Duplicate |
| 4657 |
// values and order differences are not explained.) |
| 4658 |
template <typename Container> |
| 4659 |
inline PolymorphicMatcher< |
| 4660 |
internal::ContainerEqMatcher<typename std::remove_const<Container>::type>> |
| 4661 |
ContainerEq(const Container& rhs) { |
| 4662 |
return MakePolymorphicMatcher(internal::ContainerEqMatcher<Container>(rhs)); |
| 4663 |
} |
| 4664 |
|
| 4665 |
// Returns a matcher that matches a container that, when sorted using |
| 4666 |
// the given comparator, matches container_matcher. |
| 4667 |
template <typename Comparator, typename ContainerMatcher> |
| 4668 |
inline internal::WhenSortedByMatcher<Comparator, ContainerMatcher> WhenSortedBy( |
| 4669 |
const Comparator& comparator, const ContainerMatcher& container_matcher) { |
| 4670 |
return internal::WhenSortedByMatcher<Comparator, ContainerMatcher>( |
| 4671 |
comparator, container_matcher); |
| 4672 |
} |
| 4673 |
|
| 4674 |
// Returns a matcher that matches a container that, when sorted using |
| 4675 |
// the < operator, matches container_matcher. |
| 4676 |
template <typename ContainerMatcher> |
| 4677 |
inline internal::WhenSortedByMatcher<internal::LessComparator, ContainerMatcher> |
| 4678 |
WhenSorted(const ContainerMatcher& container_matcher) { |
| 4679 |
return internal::WhenSortedByMatcher<internal::LessComparator, |
| 4680 |
ContainerMatcher>( |
| 4681 |
internal::LessComparator(), container_matcher); |
| 4682 |
} |
| 4683 |
|
| 4684 |
// Matches an STL-style container or a native array that contains the |
| 4685 |
// same number of elements as in rhs, where its i-th element and rhs's |
| 4686 |
// i-th element (as a pair) satisfy the given pair matcher, for all i. |
| 4687 |
// TupleMatcher must be able to be safely cast to Matcher<std::tuple<const |
| 4688 |
// T1&, const T2&> >, where T1 and T2 are the types of elements in the |
| 4689 |
// LHS container and the RHS container respectively. |
| 4690 |
template <typename TupleMatcher, typename Container> |
| 4691 |
inline internal::PointwiseMatcher<TupleMatcher, |
| 4692 |
typename std::remove_const<Container>::type> |
| 4693 |
Pointwise(const TupleMatcher& tuple_matcher, const Container& rhs) { |
| 4694 |
return internal::PointwiseMatcher<TupleMatcher, Container>(tuple_matcher, |
| 4695 |
rhs); |
| 4696 |
} |
| 4697 |
|
| 4698 |
// Supports the Pointwise(m, {a, b, c}) syntax. |
| 4699 |
template <typename TupleMatcher, typename T> |
| 4700 |
inline internal::PointwiseMatcher<TupleMatcher, std::vector<T>> Pointwise( |
| 4701 |
const TupleMatcher& tuple_matcher, std::initializer_list<T> rhs) { |
| 4702 |
return Pointwise(tuple_matcher, std::vector<T>(rhs)); |
| 4703 |
} |
| 4704 |
|
| 4705 |
// UnorderedPointwise(pair_matcher, rhs) matches an STL-style |
| 4706 |
// container or a native array that contains the same number of |
| 4707 |
// elements as in rhs, where in some permutation of the container, its |
| 4708 |
// i-th element and rhs's i-th element (as a pair) satisfy the given |
| 4709 |
// pair matcher, for all i. Tuple2Matcher must be able to be safely |
| 4710 |
// cast to Matcher<std::tuple<const T1&, const T2&> >, where T1 and T2 are |
| 4711 |
// the types of elements in the LHS container and the RHS container |
| 4712 |
// respectively. |
| 4713 |
// |
| 4714 |
// This is like Pointwise(pair_matcher, rhs), except that the element |
| 4715 |
// order doesn't matter. |
| 4716 |
template <typename Tuple2Matcher, typename RhsContainer> |
| 4717 |
inline internal::UnorderedElementsAreArrayMatcher< |
| 4718 |
typename internal::BoundSecondMatcher< |
| 4719 |
Tuple2Matcher, |
| 4720 |
typename internal::StlContainerView< |
| 4721 |
typename std::remove_const<RhsContainer>::type>::type::value_type>> |
| 4722 |
UnorderedPointwise(const Tuple2Matcher& tuple2_matcher, |
| 4723 |
const RhsContainer& rhs_container) { |
| 4724 |
// RhsView allows the same code to handle RhsContainer being a |
| 4725 |
// STL-style container and it being a native C-style array. |
| 4726 |
typedef typename internal::StlContainerView<RhsContainer> RhsView; |
| 4727 |
typedef typename RhsView::type RhsStlContainer; |
| 4728 |
typedef typename RhsStlContainer::value_type Second; |
| 4729 |
const RhsStlContainer& rhs_stl_container = |
| 4730 |
RhsView::ConstReference(rhs_container); |
| 4731 |
|
| 4732 |
// Create a matcher for each element in rhs_container. |
| 4733 |
::std::vector<internal::BoundSecondMatcher<Tuple2Matcher, Second>> matchers; |
| 4734 |
for (auto it = rhs_stl_container.begin(); it != rhs_stl_container.end(); |
| 4735 |
++it) { |
| 4736 |
matchers.push_back(internal::MatcherBindSecond(tuple2_matcher, *it)); |
| 4737 |
} |
| 4738 |
|
| 4739 |
// Delegate the work to UnorderedElementsAreArray(). |
| 4740 |
return UnorderedElementsAreArray(matchers); |
| 4741 |
} |
| 4742 |
|
| 4743 |
// Supports the UnorderedPointwise(m, {a, b, c}) syntax. |
| 4744 |
template <typename Tuple2Matcher, typename T> |
| 4745 |
inline internal::UnorderedElementsAreArrayMatcher< |
| 4746 |
typename internal::BoundSecondMatcher<Tuple2Matcher, T>> |
| 4747 |
UnorderedPointwise(const Tuple2Matcher& tuple2_matcher, |
| 4748 |
std::initializer_list<T> rhs) { |
| 4749 |
return UnorderedPointwise(tuple2_matcher, std::vector<T>(rhs)); |
| 4750 |
} |
| 4751 |
|
| 4752 |
// Matches an STL-style container or a native array that contains at |
| 4753 |
// least one element matching the given value or matcher. |
| 4754 |
// |
| 4755 |
// Examples: |
| 4756 |
// ::std::set<int> page_ids; |
| 4757 |
// page_ids.insert(3); |
| 4758 |
// page_ids.insert(1); |
| 4759 |
// EXPECT_THAT(page_ids, Contains(1)); |
| 4760 |
// EXPECT_THAT(page_ids, Contains(Gt(2))); |
| 4761 |
// EXPECT_THAT(page_ids, Not(Contains(4))); // See below for Times(0) |
| 4762 |
// |
| 4763 |
// ::std::map<int, size_t> page_lengths; |
| 4764 |
// page_lengths[1] = 100; |
| 4765 |
// EXPECT_THAT(page_lengths, |
| 4766 |
// Contains(::std::pair<const int, size_t>(1, 100))); |
| 4767 |
// |
| 4768 |
// const char* user_ids[] = { "joe", "mike", "tom" }; |
| 4769 |
// EXPECT_THAT(user_ids, Contains(Eq(::std::string("tom")))); |
| 4770 |
// |
| 4771 |
// The matcher supports a modifier `Times` that allows to check for arbitrary |
| 4772 |
// occurrences including testing for absence with Times(0). |
| 4773 |
// |
| 4774 |
// Examples: |
| 4775 |
// ::std::vector<int> ids; |
| 4776 |
// ids.insert(1); |
| 4777 |
// ids.insert(1); |
| 4778 |
// ids.insert(3); |
| 4779 |
// EXPECT_THAT(ids, Contains(1).Times(2)); // 1 occurs 2 times |
| 4780 |
// EXPECT_THAT(ids, Contains(2).Times(0)); // 2 is not present |
| 4781 |
// EXPECT_THAT(ids, Contains(3).Times(Ge(1))); // 3 occurs at least once |
| 4782 |
|
| 4783 |
template <typename M> |
| 4784 |
inline internal::ContainsMatcher<M> Contains(M matcher) { |
| 4785 |
return internal::ContainsMatcher<M>(matcher); |
| 4786 |
} |
| 4787 |
|
| 4788 |
// IsSupersetOf(iterator_first, iterator_last) |
| 4789 |
// IsSupersetOf(pointer, count) |
| 4790 |
// IsSupersetOf(array) |
| 4791 |
// IsSupersetOf(container) |
| 4792 |
// IsSupersetOf({e1, e2, ..., en}) |
| 4793 |
// |
| 4794 |
// IsSupersetOf() verifies that a surjective partial mapping onto a collection |
| 4795 |
// of matchers exists. In other words, a container matches |
| 4796 |
// IsSupersetOf({e1, ..., en}) if and only if there is a permutation |
| 4797 |
// {y1, ..., yn} of some of the container's elements where y1 matches e1, |
| 4798 |
// ..., and yn matches en. Obviously, the size of the container must be >= n |
| 4799 |
// in order to have a match. Examples: |
| 4800 |
// |
| 4801 |
// - {1, 2, 3} matches IsSupersetOf({Ge(3), Ne(0)}), as 3 matches Ge(3) and |
| 4802 |
// 1 matches Ne(0). |
| 4803 |
// - {1, 2} doesn't match IsSupersetOf({Eq(1), Lt(2)}), even though 1 matches |
| 4804 |
// both Eq(1) and Lt(2). The reason is that different matchers must be used |
| 4805 |
// for elements in different slots of the container. |
| 4806 |
// - {1, 1, 2} matches IsSupersetOf({Eq(1), Lt(2)}), as (the first) 1 matches |
| 4807 |
// Eq(1) and (the second) 1 matches Lt(2). |
| 4808 |
// - {1, 2, 3} matches IsSupersetOf(Gt(1), Gt(1)), as 2 matches (the first) |
| 4809 |
// Gt(1) and 3 matches (the second) Gt(1). |
| 4810 |
// |
| 4811 |
// The matchers can be specified as an array, a pointer and count, a container, |
| 4812 |
// an initializer list, or an STL iterator range. In each of these cases, the |
| 4813 |
// underlying matchers can be either values or matchers. |
| 4814 |
|
| 4815 |
template <typename Iter> |
| 4816 |
inline internal::UnorderedElementsAreArrayMatcher< |
| 4817 |
typename ::std::iterator_traits<Iter>::value_type> |
| 4818 |
IsSupersetOf(Iter first, Iter last) { |
| 4819 |
typedef typename ::std::iterator_traits<Iter>::value_type T; |
| 4820 |
return internal::UnorderedElementsAreArrayMatcher<T>( |
| 4821 |
internal::UnorderedMatcherRequire::Superset, first, last); |
| 4822 |
} |
| 4823 |
|
| 4824 |
template <typename T> |
| 4825 |
inline internal::UnorderedElementsAreArrayMatcher<T> IsSupersetOf( |
| 4826 |
const T* pointer, size_t count) { |
| 4827 |
return IsSupersetOf(pointer, pointer + count); |
| 4828 |
} |
| 4829 |
|
| 4830 |
template <typename T, size_t N> |
| 4831 |
inline internal::UnorderedElementsAreArrayMatcher<T> IsSupersetOf( |
| 4832 |
const T (&array)[N]) { |
| 4833 |
return IsSupersetOf(array, N); |
| 4834 |
} |
| 4835 |
|
| 4836 |
template <typename Container> |
| 4837 |
inline internal::UnorderedElementsAreArrayMatcher< |
| 4838 |
typename Container::value_type> |
| 4839 |
IsSupersetOf(const Container& container) { |
| 4840 |
return IsSupersetOf(container.begin(), container.end()); |
| 4841 |
} |
| 4842 |
|
| 4843 |
template <typename T> |
| 4844 |
inline internal::UnorderedElementsAreArrayMatcher<T> IsSupersetOf( |
| 4845 |
::std::initializer_list<T> xs) { |
| 4846 |
return IsSupersetOf(xs.begin(), xs.end()); |
| 4847 |
} |
| 4848 |
|
| 4849 |
// IsSubsetOf(iterator_first, iterator_last) |
| 4850 |
// IsSubsetOf(pointer, count) |
| 4851 |
// IsSubsetOf(array) |
| 4852 |
// IsSubsetOf(container) |
| 4853 |
// IsSubsetOf({e1, e2, ..., en}) |
| 4854 |
// |
| 4855 |
// IsSubsetOf() verifies that an injective mapping onto a collection of matchers |
| 4856 |
// exists. In other words, a container matches IsSubsetOf({e1, ..., en}) if and |
| 4857 |
// only if there is a subset of matchers {m1, ..., mk} which would match the |
| 4858 |
// container using UnorderedElementsAre. Obviously, the size of the container |
| 4859 |
// must be <= n in order to have a match. Examples: |
| 4860 |
// |
| 4861 |
// - {1} matches IsSubsetOf({Gt(0), Lt(0)}), as 1 matches Gt(0). |
| 4862 |
// - {1, -1} matches IsSubsetOf({Lt(0), Gt(0)}), as 1 matches Gt(0) and -1 |
| 4863 |
// matches Lt(0). |
| 4864 |
// - {1, 2} doesn't matches IsSubsetOf({Gt(0), Lt(0)}), even though 1 and 2 both |
| 4865 |
// match Gt(0). The reason is that different matchers must be used for |
| 4866 |
// elements in different slots of the container. |
| 4867 |
// |
| 4868 |
// The matchers can be specified as an array, a pointer and count, a container, |
| 4869 |
// an initializer list, or an STL iterator range. In each of these cases, the |
| 4870 |
// underlying matchers can be either values or matchers. |
| 4871 |
|
| 4872 |
template <typename Iter> |
| 4873 |
inline internal::UnorderedElementsAreArrayMatcher< |
| 4874 |
typename ::std::iterator_traits<Iter>::value_type> |
| 4875 |
IsSubsetOf(Iter first, Iter last) { |
| 4876 |
typedef typename ::std::iterator_traits<Iter>::value_type T; |
| 4877 |
return internal::UnorderedElementsAreArrayMatcher<T>( |
| 4878 |
internal::UnorderedMatcherRequire::Subset, first, last); |
| 4879 |
} |
| 4880 |
|
| 4881 |
template <typename T> |
| 4882 |
inline internal::UnorderedElementsAreArrayMatcher<T> IsSubsetOf( |
| 4883 |
const T* pointer, size_t count) { |
| 4884 |
return IsSubsetOf(pointer, pointer + count); |
| 4885 |
} |
| 4886 |
|
| 4887 |
template <typename T, size_t N> |
| 4888 |
inline internal::UnorderedElementsAreArrayMatcher<T> IsSubsetOf( |
| 4889 |
const T (&array)[N]) { |
| 4890 |
return IsSubsetOf(array, N); |
| 4891 |
} |
| 4892 |
|
| 4893 |
template <typename Container> |
| 4894 |
inline internal::UnorderedElementsAreArrayMatcher< |
| 4895 |
typename Container::value_type> |
| 4896 |
IsSubsetOf(const Container& container) { |
| 4897 |
return IsSubsetOf(container.begin(), container.end()); |
| 4898 |
} |
| 4899 |
|
| 4900 |
template <typename T> |
| 4901 |
inline internal::UnorderedElementsAreArrayMatcher<T> IsSubsetOf( |
| 4902 |
::std::initializer_list<T> xs) { |
| 4903 |
return IsSubsetOf(xs.begin(), xs.end()); |
| 4904 |
} |
| 4905 |
|
| 4906 |
// Matches an STL-style container or a native array that contains only |
| 4907 |
// elements matching the given value or matcher. |
| 4908 |
// |
| 4909 |
// Each(m) is semantically equivalent to `Not(Contains(Not(m)))`. Only |
| 4910 |
// the messages are different. |
| 4911 |
// |
| 4912 |
// Examples: |
| 4913 |
// ::std::set<int> page_ids; |
| 4914 |
// // Each(m) matches an empty container, regardless of what m is. |
| 4915 |
// EXPECT_THAT(page_ids, Each(Eq(1))); |
| 4916 |
// EXPECT_THAT(page_ids, Each(Eq(77))); |
| 4917 |
// |
| 4918 |
// page_ids.insert(3); |
| 4919 |
// EXPECT_THAT(page_ids, Each(Gt(0))); |
| 4920 |
// EXPECT_THAT(page_ids, Not(Each(Gt(4)))); |
| 4921 |
// page_ids.insert(1); |
| 4922 |
// EXPECT_THAT(page_ids, Not(Each(Lt(2)))); |
| 4923 |
// |
| 4924 |
// ::std::map<int, size_t> page_lengths; |
| 4925 |
// page_lengths[1] = 100; |
| 4926 |
// page_lengths[2] = 200; |
| 4927 |
// page_lengths[3] = 300; |
| 4928 |
// EXPECT_THAT(page_lengths, Not(Each(Pair(1, 100)))); |
| 4929 |
// EXPECT_THAT(page_lengths, Each(Key(Le(3)))); |
| 4930 |
// |
| 4931 |
// const char* user_ids[] = { "joe", "mike", "tom" }; |
| 4932 |
// EXPECT_THAT(user_ids, Not(Each(Eq(::std::string("tom"))))); |
| 4933 |
template <typename M> |
| 4934 |
inline internal::EachMatcher<M> Each(M matcher) { |
| 4935 |
return internal::EachMatcher<M>(matcher); |
| 4936 |
} |
| 4937 |
|
| 4938 |
// Key(inner_matcher) matches an std::pair whose 'first' field matches |
| 4939 |
// inner_matcher. For example, Contains(Key(Ge(5))) can be used to match an |
| 4940 |
// std::map that contains at least one element whose key is >= 5. |
| 4941 |
template <typename M> |
| 4942 |
inline internal::KeyMatcher<M> Key(M inner_matcher) { |
| 4943 |
return internal::KeyMatcher<M>(inner_matcher); |
| 4944 |
} |
| 4945 |
|
| 4946 |
// Pair(first_matcher, second_matcher) matches a std::pair whose 'first' field |
| 4947 |
// matches first_matcher and whose 'second' field matches second_matcher. For |
| 4948 |
// example, EXPECT_THAT(map_type, ElementsAre(Pair(Ge(5), "foo"))) can be used |
| 4949 |
// to match a std::map<int, string> that contains exactly one element whose key |
| 4950 |
// is >= 5 and whose value equals "foo". |
| 4951 |
template <typename FirstMatcher, typename SecondMatcher> |
| 4952 |
inline internal::PairMatcher<FirstMatcher, SecondMatcher> Pair( |
| 4953 |
FirstMatcher first_matcher, SecondMatcher second_matcher) { |
| 4954 |
return internal::PairMatcher<FirstMatcher, SecondMatcher>(first_matcher, |
| 4955 |
second_matcher); |
| 4956 |
} |
| 4957 |
|
| 4958 |
namespace no_adl { |
| 4959 |
// Conditional() creates a matcher that conditionally uses either the first or |
| 4960 |
// second matcher provided. For example, we could create an `equal if, and only |
| 4961 |
// if' matcher using the Conditional wrapper as follows: |
| 4962 |
// |
| 4963 |
// EXPECT_THAT(result, Conditional(condition, Eq(expected), Ne(expected))); |
| 4964 |
template <typename MatcherTrue, typename MatcherFalse> |
| 4965 |
internal::ConditionalMatcher<MatcherTrue, MatcherFalse> Conditional( |
| 4966 |
bool condition, MatcherTrue matcher_true, MatcherFalse matcher_false) { |
| 4967 |
return internal::ConditionalMatcher<MatcherTrue, MatcherFalse>( |
| 4968 |
condition, std::move(matcher_true), std::move(matcher_false)); |
| 4969 |
} |
| 4970 |
|
| 4971 |
// FieldsAre(matchers...) matches piecewise the fields of compatible structs. |
| 4972 |
// These include those that support `get<I>(obj)`, and when structured bindings |
| 4973 |
// are enabled any class that supports them. |
| 4974 |
// In particular, `std::tuple`, `std::pair`, `std::array` and aggregate types. |
| 4975 |
template <typename... M> |
| 4976 |
internal::FieldsAreMatcher<typename std::decay<M>::type...> FieldsAre( |
| 4977 |
M&&... matchers) { |
| 4978 |
return internal::FieldsAreMatcher<typename std::decay<M>::type...>( |
| 4979 |
std::forward<M>(matchers)...); |
| 4980 |
} |
| 4981 |
|
| 4982 |
// Creates a matcher that matches a pointer (raw or smart) that matches |
| 4983 |
// inner_matcher. |
| 4984 |
template <typename InnerMatcher> |
| 4985 |
inline internal::PointerMatcher<InnerMatcher> Pointer( |
| 4986 |
const InnerMatcher& inner_matcher) { |
| 4987 |
return internal::PointerMatcher<InnerMatcher>(inner_matcher); |
| 4988 |
} |
| 4989 |
|
| 4990 |
// Creates a matcher that matches an object that has an address that matches |
| 4991 |
// inner_matcher. |
| 4992 |
template <typename InnerMatcher> |
| 4993 |
inline internal::AddressMatcher<InnerMatcher> Address( |
| 4994 |
const InnerMatcher& inner_matcher) { |
| 4995 |
return internal::AddressMatcher<InnerMatcher>(inner_matcher); |
| 4996 |
} |
| 4997 |
|
| 4998 |
// Matches a base64 escaped string, when the unescaped string matches the |
| 4999 |
// internal matcher. |
| 5000 |
template <typename MatcherType> |
| 5001 |
internal::WhenBase64UnescapedMatcher WhenBase64Unescaped( |
| 5002 |
const MatcherType& internal_matcher) { |
| 5003 |
return internal::WhenBase64UnescapedMatcher(internal_matcher); |
| 5004 |
} |
| 5005 |
} // namespace no_adl |
| 5006 |
|
| 5007 |
// Returns a predicate that is satisfied by anything that matches the |
| 5008 |
// given matcher. |
| 5009 |
template <typename M> |
| 5010 |
inline internal::MatcherAsPredicate<M> Matches(M matcher) { |
| 5011 |
return internal::MatcherAsPredicate<M>(matcher); |
| 5012 |
} |
| 5013 |
|
| 5014 |
// Returns true if and only if the value matches the matcher. |
| 5015 |
template <typename T, typename M> |
| 5016 |
inline bool Value(const T& value, M matcher) { |
| 5017 |
return testing::Matches(matcher)(value); |
| 5018 |
} |
| 5019 |
|
| 5020 |
// Matches the value against the given matcher and explains the match |
| 5021 |
// result to listener. |
| 5022 |
template <typename T, typename M> |
| 5023 |
inline bool ExplainMatchResult(M matcher, const T& value, |
| 5024 |
MatchResultListener* listener) { |
| 5025 |
return SafeMatcherCast<const T&>(matcher).MatchAndExplain(value, listener); |
| 5026 |
} |
| 5027 |
|
| 5028 |
// Returns a string representation of the given matcher. Useful for description |
| 5029 |
// strings of matchers defined using MATCHER_P* macros that accept matchers as |
| 5030 |
// their arguments. For example: |
| 5031 |
// |
| 5032 |
// MATCHER_P(XAndYThat, matcher, |
| 5033 |
// "X that " + DescribeMatcher<int>(matcher, negation) + |
| 5034 |
// " and Y that " + DescribeMatcher<double>(matcher, negation)) { |
| 5035 |
// return ExplainMatchResult(matcher, arg.x(), result_listener) && |
| 5036 |
// ExplainMatchResult(matcher, arg.y(), result_listener); |
| 5037 |
// } |
| 5038 |
template <typename T, typename M> |
| 5039 |
std::string DescribeMatcher(const M& matcher, bool negation = false) { |
| 5040 |
::std::stringstream ss; |
| 5041 |
Matcher<T> monomorphic_matcher = SafeMatcherCast<T>(matcher); |
| 5042 |
if (negation) { |
| 5043 |
monomorphic_matcher.DescribeNegationTo(&ss); |
| 5044 |
} else { |
| 5045 |
monomorphic_matcher.DescribeTo(&ss); |
| 5046 |
} |
| 5047 |
return ss.str(); |
| 5048 |
} |
| 5049 |
|
| 5050 |
template <typename... Args> |
| 5051 |
internal::ElementsAreMatcher< |
| 5052 |
std::tuple<typename std::decay<const Args&>::type...>> |
| 5053 |
ElementsAre(const Args&... matchers) { |
| 5054 |
return internal::ElementsAreMatcher< |
| 5055 |
std::tuple<typename std::decay<const Args&>::type...>>( |
| 5056 |
std::make_tuple(matchers...)); |
| 5057 |
} |
| 5058 |
|
| 5059 |
template <typename... Args> |
| 5060 |
internal::UnorderedElementsAreMatcher< |
| 5061 |
std::tuple<typename std::decay<const Args&>::type...>> |
| 5062 |
UnorderedElementsAre(const Args&... matchers) { |
| 5063 |
return internal::UnorderedElementsAreMatcher< |
| 5064 |
std::tuple<typename std::decay<const Args&>::type...>>( |
| 5065 |
std::make_tuple(matchers...)); |
| 5066 |
} |
| 5067 |
|
| 5068 |
// Define variadic matcher versions. |
| 5069 |
template <typename... Args> |
| 5070 |
internal::AllOfMatcher<typename std::decay<const Args&>::type...> AllOf( |
| 5071 |
const Args&... matchers) { |
| 5072 |
return internal::AllOfMatcher<typename std::decay<const Args&>::type...>( |
| 5073 |
matchers...); |
| 5074 |
} |
| 5075 |
|
| 5076 |
template <typename... Args> |
| 5077 |
internal::AnyOfMatcher<typename std::decay<const Args&>::type...> AnyOf( |
| 5078 |
const Args&... matchers) { |
| 5079 |
return internal::AnyOfMatcher<typename std::decay<const Args&>::type...>( |
| 5080 |
matchers...); |
| 5081 |
} |
| 5082 |
|
| 5083 |
// AnyOfArray(array) |
| 5084 |
// AnyOfArray(pointer, count) |
| 5085 |
// AnyOfArray(container) |
| 5086 |
// AnyOfArray({ e1, e2, ..., en }) |
| 5087 |
// AnyOfArray(iterator_first, iterator_last) |
| 5088 |
// |
| 5089 |
// AnyOfArray() verifies whether a given value matches any member of a |
| 5090 |
// collection of matchers. |
| 5091 |
// |
| 5092 |
// AllOfArray(array) |
| 5093 |
// AllOfArray(pointer, count) |
| 5094 |
// AllOfArray(container) |
| 5095 |
// AllOfArray({ e1, e2, ..., en }) |
| 5096 |
// AllOfArray(iterator_first, iterator_last) |
| 5097 |
// |
| 5098 |
// AllOfArray() verifies whether a given value matches all members of a |
| 5099 |
// collection of matchers. |
| 5100 |
// |
| 5101 |
// The matchers can be specified as an array, a pointer and count, a container, |
| 5102 |
// an initializer list, or an STL iterator range. In each of these cases, the |
| 5103 |
// underlying matchers can be either values or matchers. |
| 5104 |
|
| 5105 |
template <typename Iter> |
| 5106 |
inline internal::AnyOfArrayMatcher< |
| 5107 |
typename ::std::iterator_traits<Iter>::value_type> |
| 5108 |
AnyOfArray(Iter first, Iter last) { |
| 5109 |
return internal::AnyOfArrayMatcher< |
| 5110 |
typename ::std::iterator_traits<Iter>::value_type>(first, last); |
| 5111 |
} |
| 5112 |
|
| 5113 |
template <typename Iter> |
| 5114 |
inline internal::AllOfArrayMatcher< |
| 5115 |
typename ::std::iterator_traits<Iter>::value_type> |
| 5116 |
AllOfArray(Iter first, Iter last) { |
| 5117 |
return internal::AllOfArrayMatcher< |
| 5118 |
typename ::std::iterator_traits<Iter>::value_type>(first, last); |
| 5119 |
} |
| 5120 |
|
| 5121 |
template <typename T> |
| 5122 |
inline internal::AnyOfArrayMatcher<T> AnyOfArray(const T* ptr, size_t count) { |
| 5123 |
return AnyOfArray(ptr, ptr + count); |
| 5124 |
} |
| 5125 |
|
| 5126 |
template <typename T> |
| 5127 |
inline internal::AllOfArrayMatcher<T> AllOfArray(const T* ptr, size_t count) { |
| 5128 |
return AllOfArray(ptr, ptr + count); |
| 5129 |
} |
| 5130 |
|
| 5131 |
template <typename T, size_t N> |
| 5132 |
inline internal::AnyOfArrayMatcher<T> AnyOfArray(const T (&array)[N]) { |
| 5133 |
return AnyOfArray(array, N); |
| 5134 |
} |
| 5135 |
|
| 5136 |
template <typename T, size_t N> |
| 5137 |
inline internal::AllOfArrayMatcher<T> AllOfArray(const T (&array)[N]) { |
| 5138 |
return AllOfArray(array, N); |
| 5139 |
} |
| 5140 |
|
| 5141 |
template <typename Container> |
| 5142 |
inline internal::AnyOfArrayMatcher<typename Container::value_type> AnyOfArray( |
| 5143 |
const Container& container) { |
| 5144 |
return AnyOfArray(container.begin(), container.end()); |
| 5145 |
} |
| 5146 |
|
| 5147 |
template <typename Container> |
| 5148 |
inline internal::AllOfArrayMatcher<typename Container::value_type> AllOfArray( |
| 5149 |
const Container& container) { |
| 5150 |
return AllOfArray(container.begin(), container.end()); |
| 5151 |
} |
| 5152 |
|
| 5153 |
template <typename T> |
| 5154 |
inline internal::AnyOfArrayMatcher<T> AnyOfArray( |
| 5155 |
::std::initializer_list<T> xs) { |
| 5156 |
return AnyOfArray(xs.begin(), xs.end()); |
| 5157 |
} |
| 5158 |
|
| 5159 |
template <typename T> |
| 5160 |
inline internal::AllOfArrayMatcher<T> AllOfArray( |
| 5161 |
::std::initializer_list<T> xs) { |
| 5162 |
return AllOfArray(xs.begin(), xs.end()); |
| 5163 |
} |
| 5164 |
|
| 5165 |
// Args<N1, N2, ..., Nk>(a_matcher) matches a tuple if the selected |
| 5166 |
// fields of it matches a_matcher. C++ doesn't support default |
| 5167 |
// arguments for function templates, so we have to overload it. |
| 5168 |
template <size_t... k, typename InnerMatcher> |
| 5169 |
internal::ArgsMatcher<typename std::decay<InnerMatcher>::type, k...> Args( |
| 5170 |
InnerMatcher&& matcher) { |
| 5171 |
return internal::ArgsMatcher<typename std::decay<InnerMatcher>::type, k...>( |
| 5172 |
std::forward<InnerMatcher>(matcher)); |
| 5173 |
} |
| 5174 |
|
| 5175 |
// AllArgs(m) is a synonym of m. This is useful in |
| 5176 |
// |
| 5177 |
// EXPECT_CALL(foo, Bar(_, _)).With(AllArgs(Eq())); |
| 5178 |
// |
| 5179 |
// which is easier to read than |
| 5180 |
// |
| 5181 |
// EXPECT_CALL(foo, Bar(_, _)).With(Eq()); |
| 5182 |
template <typename InnerMatcher> |
| 5183 |
inline InnerMatcher AllArgs(const InnerMatcher& matcher) { |
| 5184 |
return matcher; |
| 5185 |
} |
| 5186 |
|
| 5187 |
// Returns a matcher that matches the value of an optional<> type variable. |
| 5188 |
// The matcher implementation only uses '!arg' and requires that the optional<> |
| 5189 |
// type has a 'value_type' member type and that '*arg' is of type 'value_type' |
| 5190 |
// and is printable using 'PrintToString'. It is compatible with |
| 5191 |
// std::optional/std::experimental::optional. |
| 5192 |
// Note that to compare an optional type variable against nullopt you should |
| 5193 |
// use Eq(nullopt) and not Eq(Optional(nullopt)). The latter implies that the |
| 5194 |
// optional value contains an optional itself. |
| 5195 |
template <typename ValueMatcher> |
| 5196 |
inline internal::OptionalMatcher<ValueMatcher> Optional( |
| 5197 |
const ValueMatcher& value_matcher) { |
| 5198 |
return internal::OptionalMatcher<ValueMatcher>(value_matcher); |
| 5199 |
} |
| 5200 |
|
| 5201 |
// Returns a matcher that matches the value of a absl::any type variable. |
| 5202 |
template <typename T> |
| 5203 |
PolymorphicMatcher<internal::any_cast_matcher::AnyCastMatcher<T>> AnyWith( |
| 5204 |
const Matcher<const T&>& matcher) { |
| 5205 |
return MakePolymorphicMatcher( |
| 5206 |
internal::any_cast_matcher::AnyCastMatcher<T>(matcher)); |
| 5207 |
} |
| 5208 |
|
| 5209 |
// Returns a matcher that matches the value of a variant<> type variable. |
| 5210 |
// The matcher implementation uses ADL to find the holds_alternative and get |
| 5211 |
// functions. |
| 5212 |
// It is compatible with std::variant. |
| 5213 |
template <typename T> |
| 5214 |
PolymorphicMatcher<internal::variant_matcher::VariantMatcher<T>> VariantWith( |
| 5215 |
const Matcher<const T&>& matcher) { |
| 5216 |
return MakePolymorphicMatcher( |
| 5217 |
internal::variant_matcher::VariantMatcher<T>(matcher)); |
| 5218 |
} |
| 5219 |
|
| 5220 |
#if GTEST_HAS_EXCEPTIONS |
| 5221 |
|
| 5222 |
// Anything inside the `internal` namespace is internal to the implementation |
| 5223 |
// and must not be used in user code! |
| 5224 |
namespace internal { |
| 5225 |
|
| 5226 |
class WithWhatMatcherImpl { |
| 5227 |
public: |
| 5228 |
WithWhatMatcherImpl(Matcher<std::string> matcher) |
| 5229 |
: matcher_(std::move(matcher)) {} |
| 5230 |
|
| 5231 |
void DescribeTo(std::ostream* os) const { |
| 5232 |
*os << "contains .what() that "; |
| 5233 |
matcher_.DescribeTo(os); |
| 5234 |
} |
| 5235 |
|
| 5236 |
void DescribeNegationTo(std::ostream* os) const { |
| 5237 |
*os << "contains .what() that does not "; |
| 5238 |
matcher_.DescribeTo(os); |
| 5239 |
} |
| 5240 |
|
| 5241 |
template <typename Err> |
| 5242 |
bool MatchAndExplain(const Err& err, MatchResultListener* listener) const { |
| 5243 |
*listener << "which contains .what() (of value = " << err.what() |
| 5244 |
<< ") that "; |
| 5245 |
return matcher_.MatchAndExplain(err.what(), listener); |
| 5246 |
} |
| 5247 |
|
| 5248 |
private: |
| 5249 |
const Matcher<std::string> matcher_; |
| 5250 |
}; |
| 5251 |
|
| 5252 |
inline PolymorphicMatcher<WithWhatMatcherImpl> WithWhat( |
| 5253 |
Matcher<std::string> m) { |
| 5254 |
return MakePolymorphicMatcher(WithWhatMatcherImpl(std::move(m))); |
| 5255 |
} |
| 5256 |
|
| 5257 |
template <typename Err> |
| 5258 |
class ExceptionMatcherImpl { |
| 5259 |
class NeverThrown { |
| 5260 |
public: |
| 5261 |
const char* what() const noexcept { |
| 5262 |
return "this exception should never be thrown"; |
| 5263 |
} |
| 5264 |
}; |
| 5265 |
|
| 5266 |
// If the matchee raises an exception of a wrong type, we'd like to |
| 5267 |
// catch it and print its message and type. To do that, we add an additional |
| 5268 |
// catch clause: |
| 5269 |
// |
| 5270 |
// try { ... } |
| 5271 |
// catch (const Err&) { /* an expected exception */ } |
| 5272 |
// catch (const std::exception&) { /* exception of a wrong type */ } |
| 5273 |
// |
| 5274 |
// However, if the `Err` itself is `std::exception`, we'd end up with two |
| 5275 |
// identical `catch` clauses: |
| 5276 |
// |
| 5277 |
// try { ... } |
| 5278 |
// catch (const std::exception&) { /* an expected exception */ } |
| 5279 |
// catch (const std::exception&) { /* exception of a wrong type */ } |
| 5280 |
// |
| 5281 |
// This can cause a warning or an error in some compilers. To resolve |
| 5282 |
// the issue, we use a fake error type whenever `Err` is `std::exception`: |
| 5283 |
// |
| 5284 |
// try { ... } |
| 5285 |
// catch (const std::exception&) { /* an expected exception */ } |
| 5286 |
// catch (const NeverThrown&) { /* exception of a wrong type */ } |
| 5287 |
using DefaultExceptionType = typename std::conditional< |
| 5288 |
std::is_same<typename std::remove_cv< |
| 5289 |
typename std::remove_reference<Err>::type>::type, |
| 5290 |
std::exception>::value, |
| 5291 |
const NeverThrown&, const std::exception&>::type; |
| 5292 |
|
| 5293 |
public: |
| 5294 |
ExceptionMatcherImpl(Matcher<const Err&> matcher) |
| 5295 |
: matcher_(std::move(matcher)) {} |
| 5296 |
|
| 5297 |
void DescribeTo(std::ostream* os) const { |
| 5298 |
*os << "throws an exception which is a " << GetTypeName<Err>(); |
| 5299 |
*os << " which "; |
| 5300 |
matcher_.DescribeTo(os); |
| 5301 |
} |
| 5302 |
|
| 5303 |
void DescribeNegationTo(std::ostream* os) const { |
| 5304 |
*os << "throws an exception which is not a " << GetTypeName<Err>(); |
| 5305 |
*os << " which "; |
| 5306 |
matcher_.DescribeNegationTo(os); |
| 5307 |
} |
| 5308 |
|
| 5309 |
template <typename T> |
| 5310 |
bool MatchAndExplain(T&& x, MatchResultListener* listener) const { |
| 5311 |
try { |
| 5312 |
(void)(std::forward<T>(x)()); |
| 5313 |
} catch (const Err& err) { |
| 5314 |
*listener << "throws an exception which is a " << GetTypeName<Err>(); |
| 5315 |
*listener << " "; |
| 5316 |
return matcher_.MatchAndExplain(err, listener); |
| 5317 |
} catch (DefaultExceptionType err) { |
| 5318 |
#if GTEST_HAS_RTTI |
| 5319 |
*listener << "throws an exception of type " << GetTypeName(typeid(err)); |
| 5320 |
*listener << " "; |
| 5321 |
#else |
| 5322 |
*listener << "throws an std::exception-derived type "; |
| 5323 |
#endif |
| 5324 |
*listener << "with description \"" << err.what() << "\""; |
| 5325 |
return false; |
| 5326 |
} catch (...) { |
| 5327 |
*listener << "throws an exception of an unknown type"; |
| 5328 |
return false; |
| 5329 |
} |
| 5330 |
|
| 5331 |
*listener << "does not throw any exception"; |
| 5332 |
return false; |
| 5333 |
} |
| 5334 |
|
| 5335 |
private: |
| 5336 |
const Matcher<const Err&> matcher_; |
| 5337 |
}; |
| 5338 |
|
| 5339 |
} // namespace internal |
| 5340 |
|
| 5341 |
// Throws() |
| 5342 |
// Throws(exceptionMatcher) |
| 5343 |
// ThrowsMessage(messageMatcher) |
| 5344 |
// |
| 5345 |
// This matcher accepts a callable and verifies that when invoked, it throws |
| 5346 |
// an exception with the given type and properties. |
| 5347 |
// |
| 5348 |
// Examples: |
| 5349 |
// |
| 5350 |
// EXPECT_THAT( |
| 5351 |
// []() { throw std::runtime_error("message"); }, |
| 5352 |
// Throws<std::runtime_error>()); |
| 5353 |
// |
| 5354 |
// EXPECT_THAT( |
| 5355 |
// []() { throw std::runtime_error("message"); }, |
| 5356 |
// ThrowsMessage<std::runtime_error>(HasSubstr("message"))); |
| 5357 |
// |
| 5358 |
// EXPECT_THAT( |
| 5359 |
// []() { throw std::runtime_error("message"); }, |
| 5360 |
// Throws<std::runtime_error>( |
| 5361 |
// Property(&std::runtime_error::what, HasSubstr("message")))); |
| 5362 |
|
| 5363 |
template <typename Err> |
| 5364 |
PolymorphicMatcher<internal::ExceptionMatcherImpl<Err>> Throws() { |
| 5365 |
return MakePolymorphicMatcher( |
| 5366 |
internal::ExceptionMatcherImpl<Err>(A<const Err&>())); |
| 5367 |
} |
| 5368 |
|
| 5369 |
template <typename Err, typename ExceptionMatcher> |
| 5370 |
PolymorphicMatcher<internal::ExceptionMatcherImpl<Err>> Throws( |
| 5371 |
const ExceptionMatcher& exception_matcher) { |
| 5372 |
// Using matcher cast allows users to pass a matcher of a more broad type. |
| 5373 |
// For example user may want to pass Matcher<std::exception> |
| 5374 |
// to Throws<std::runtime_error>, or Matcher<int64> to Throws<int32>. |
| 5375 |
return MakePolymorphicMatcher(internal::ExceptionMatcherImpl<Err>( |
| 5376 |
SafeMatcherCast<const Err&>(exception_matcher))); |
| 5377 |
} |
| 5378 |
|
| 5379 |
template <typename Err, typename MessageMatcher> |
| 5380 |
PolymorphicMatcher<internal::ExceptionMatcherImpl<Err>> ThrowsMessage( |
| 5381 |
MessageMatcher&& message_matcher) { |
| 5382 |
static_assert(std::is_base_of<std::exception, Err>::value, |
| 5383 |
"expected an std::exception-derived type"); |
| 5384 |
return Throws<Err>(internal::WithWhat( |
| 5385 |
MatcherCast<std::string>(std::forward<MessageMatcher>(message_matcher)))); |
| 5386 |
} |
| 5387 |
|
| 5388 |
#endif // GTEST_HAS_EXCEPTIONS |
| 5389 |
|
| 5390 |
// These macros allow using matchers to check values in Google Test |
| 5391 |
// tests. ASSERT_THAT(value, matcher) and EXPECT_THAT(value, matcher) |
| 5392 |
// succeed if and only if the value matches the matcher. If the assertion |
| 5393 |
// fails, the value and the description of the matcher will be printed. |
| 5394 |
#define ASSERT_THAT(value, matcher) \ |
| 5395 |
ASSERT_PRED_FORMAT1( \ |
| 5396 |
::testing::internal::MakePredicateFormatterFromMatcher(matcher), value) |
| 5397 |
#define EXPECT_THAT(value, matcher) \ |
| 5398 |
EXPECT_PRED_FORMAT1( \ |
| 5399 |
::testing::internal::MakePredicateFormatterFromMatcher(matcher), value) |
| 5400 |
|
| 5401 |
// MATCHER* macros itself are listed below. |
| 5402 |
#define MATCHER(name, description) \ |
| 5403 |
class name##Matcher \ |
| 5404 |
: public ::testing::internal::MatcherBaseImpl<name##Matcher> { \ |
| 5405 |
public: \ |
| 5406 |
template <typename arg_type> \ |
| 5407 |
class gmock_Impl : public ::testing::MatcherInterface<const arg_type&> { \ |
| 5408 |
public: \ |
| 5409 |
gmock_Impl() {} \ |
| 5410 |
bool MatchAndExplain( \ |
| 5411 |
const arg_type& arg, \ |
| 5412 |
::testing::MatchResultListener* result_listener) const override; \ |
| 5413 |
void DescribeTo(::std::ostream* gmock_os) const override { \ |
| 5414 |
*gmock_os << FormatDescription(false); \ |
| 5415 |
} \ |
| 5416 |
void DescribeNegationTo(::std::ostream* gmock_os) const override { \ |
| 5417 |
*gmock_os << FormatDescription(true); \ |
| 5418 |
} \ |
| 5419 |
\ |
| 5420 |
private: \ |
| 5421 |
::std::string FormatDescription(bool negation) const { \ |
| 5422 |
/* NOLINTNEXTLINE readability-redundant-string-init */ \ |
| 5423 |
::std::string gmock_description = (description); \ |
| 5424 |
if (!gmock_description.empty()) { \ |
| 5425 |
return gmock_description; \ |
| 5426 |
} \ |
| 5427 |
return ::testing::internal::FormatMatcherDescription(negation, #name, \ |
| 5428 |
{}, {}); \ |
| 5429 |
} \ |
| 5430 |
}; \ |
| 5431 |
}; \ |
| 5432 |
GTEST_ATTRIBUTE_UNUSED_ inline name##Matcher name() { return {}; } \ |
| 5433 |
template <typename arg_type> \ |
| 5434 |
bool name##Matcher::gmock_Impl<arg_type>::MatchAndExplain( \ |
| 5435 |
const arg_type& arg, \ |
| 5436 |
::testing::MatchResultListener* result_listener GTEST_ATTRIBUTE_UNUSED_) \ |
| 5437 |
const |
| 5438 |
|
| 5439 |
#define MATCHER_P(name, p0, description) \ |
| 5440 |
GMOCK_INTERNAL_MATCHER(name, name##MatcherP, description, (#p0), (p0)) |
| 5441 |
#define MATCHER_P2(name, p0, p1, description) \ |
| 5442 |
GMOCK_INTERNAL_MATCHER(name, name##MatcherP2, description, (#p0, #p1), \ |
| 5443 |
(p0, p1)) |
| 5444 |
#define MATCHER_P3(name, p0, p1, p2, description) \ |
| 5445 |
GMOCK_INTERNAL_MATCHER(name, name##MatcherP3, description, (#p0, #p1, #p2), \ |
| 5446 |
(p0, p1, p2)) |
| 5447 |
#define MATCHER_P4(name, p0, p1, p2, p3, description) \ |
| 5448 |
GMOCK_INTERNAL_MATCHER(name, name##MatcherP4, description, \ |
| 5449 |
(#p0, #p1, #p2, #p3), (p0, p1, p2, p3)) |
| 5450 |
#define MATCHER_P5(name, p0, p1, p2, p3, p4, description) \ |
| 5451 |
GMOCK_INTERNAL_MATCHER(name, name##MatcherP5, description, \ |
| 5452 |
(#p0, #p1, #p2, #p3, #p4), (p0, p1, p2, p3, p4)) |
| 5453 |
#define MATCHER_P6(name, p0, p1, p2, p3, p4, p5, description) \ |
| 5454 |
GMOCK_INTERNAL_MATCHER(name, name##MatcherP6, description, \ |
| 5455 |
(#p0, #p1, #p2, #p3, #p4, #p5), \ |
| 5456 |
(p0, p1, p2, p3, p4, p5)) |
| 5457 |
#define MATCHER_P7(name, p0, p1, p2, p3, p4, p5, p6, description) \ |
| 5458 |
GMOCK_INTERNAL_MATCHER(name, name##MatcherP7, description, \ |
| 5459 |
(#p0, #p1, #p2, #p3, #p4, #p5, #p6), \ |
| 5460 |
(p0, p1, p2, p3, p4, p5, p6)) |
| 5461 |
#define MATCHER_P8(name, p0, p1, p2, p3, p4, p5, p6, p7, description) \ |
| 5462 |
GMOCK_INTERNAL_MATCHER(name, name##MatcherP8, description, \ |
| 5463 |
(#p0, #p1, #p2, #p3, #p4, #p5, #p6, #p7), \ |
| 5464 |
(p0, p1, p2, p3, p4, p5, p6, p7)) |
| 5465 |
#define MATCHER_P9(name, p0, p1, p2, p3, p4, p5, p6, p7, p8, description) \ |
| 5466 |
GMOCK_INTERNAL_MATCHER(name, name##MatcherP9, description, \ |
| 5467 |
(#p0, #p1, #p2, #p3, #p4, #p5, #p6, #p7, #p8), \ |
| 5468 |
(p0, p1, p2, p3, p4, p5, p6, p7, p8)) |
| 5469 |
#define MATCHER_P10(name, p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, description) \ |
| 5470 |
GMOCK_INTERNAL_MATCHER(name, name##MatcherP10, description, \ |
| 5471 |
(#p0, #p1, #p2, #p3, #p4, #p5, #p6, #p7, #p8, #p9), \ |
| 5472 |
(p0, p1, p2, p3, p4, p5, p6, p7, p8, p9)) |
| 5473 |
|
| 5474 |
#define GMOCK_INTERNAL_MATCHER(name, full_name, description, arg_names, args) \ |
| 5475 |
template <GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args)> \ |
| 5476 |
class full_name : public ::testing::internal::MatcherBaseImpl< \ |
| 5477 |
full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>> { \ |
| 5478 |
public: \ |
| 5479 |
using full_name::MatcherBaseImpl::MatcherBaseImpl; \ |
| 5480 |
template <typename arg_type> \ |
| 5481 |
class gmock_Impl : public ::testing::MatcherInterface<const arg_type&> { \ |
| 5482 |
public: \ |
| 5483 |
explicit gmock_Impl(GMOCK_INTERNAL_MATCHER_FUNCTION_ARGS(args)) \ |
| 5484 |
: GMOCK_INTERNAL_MATCHER_FORWARD_ARGS(args) {} \ |
| 5485 |
bool MatchAndExplain( \ |
| 5486 |
const arg_type& arg, \ |
| 5487 |
::testing::MatchResultListener* result_listener) const override; \ |
| 5488 |
void DescribeTo(::std::ostream* gmock_os) const override { \ |
| 5489 |
*gmock_os << FormatDescription(false); \ |
| 5490 |
} \ |
| 5491 |
void DescribeNegationTo(::std::ostream* gmock_os) const override { \ |
| 5492 |
*gmock_os << FormatDescription(true); \ |
| 5493 |
} \ |
| 5494 |
GMOCK_INTERNAL_MATCHER_MEMBERS(args) \ |
| 5495 |
\ |
| 5496 |
private: \ |
| 5497 |
::std::string FormatDescription(bool negation) const { \ |
| 5498 |
::std::string gmock_description = (description); \ |
| 5499 |
if (!gmock_description.empty()) { \ |
| 5500 |
return gmock_description; \ |
| 5501 |
} \ |
| 5502 |
return ::testing::internal::FormatMatcherDescription( \ |
| 5503 |
negation, #name, {GMOCK_PP_REMOVE_PARENS(arg_names)}, \ |
| 5504 |
::testing::internal::UniversalTersePrintTupleFieldsToStrings( \ |
| 5505 |
::std::tuple<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>( \ |
| 5506 |
GMOCK_INTERNAL_MATCHER_MEMBERS_USAGE(args)))); \ |
| 5507 |
} \ |
| 5508 |
}; \ |
| 5509 |
}; \ |
| 5510 |
template <GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args)> \ |
| 5511 |
inline full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)> name( \ |
| 5512 |
GMOCK_INTERNAL_MATCHER_FUNCTION_ARGS(args)) { \ |
| 5513 |
return full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>( \ |
| 5514 |
GMOCK_INTERNAL_MATCHER_ARGS_USAGE(args)); \ |
| 5515 |
} \ |
| 5516 |
template <GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args)> \ |
| 5517 |
template <typename arg_type> \ |
| 5518 |
bool full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>::gmock_Impl< \ |
| 5519 |
arg_type>::MatchAndExplain(const arg_type& arg, \ |
| 5520 |
::testing::MatchResultListener* \ |
| 5521 |
result_listener GTEST_ATTRIBUTE_UNUSED_) \ |
| 5522 |
const |
| 5523 |
|
| 5524 |
#define GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args) \ |
| 5525 |
GMOCK_PP_TAIL( \ |
| 5526 |
GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAM, , args)) |
| 5527 |
#define GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAM(i_unused, data_unused, arg) \ |
| 5528 |
, typename arg##_type |
| 5529 |
|
| 5530 |
#define GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args) \ |
| 5531 |
GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_TYPE_PARAM, , args)) |
| 5532 |
#define GMOCK_INTERNAL_MATCHER_TYPE_PARAM(i_unused, data_unused, arg) \ |
| 5533 |
, arg##_type |
| 5534 |
|
| 5535 |
#define GMOCK_INTERNAL_MATCHER_FUNCTION_ARGS(args) \ |
| 5536 |
GMOCK_PP_TAIL(dummy_first GMOCK_PP_FOR_EACH( \ |
| 5537 |
GMOCK_INTERNAL_MATCHER_FUNCTION_ARG, , args)) |
| 5538 |
#define GMOCK_INTERNAL_MATCHER_FUNCTION_ARG(i, data_unused, arg) \ |
| 5539 |
, arg##_type gmock_p##i |
| 5540 |
|
| 5541 |
#define GMOCK_INTERNAL_MATCHER_FORWARD_ARGS(args) \ |
| 5542 |
GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_FORWARD_ARG, , args)) |
| 5543 |
#define GMOCK_INTERNAL_MATCHER_FORWARD_ARG(i, data_unused, arg) \ |
| 5544 |
, arg(::std::forward<arg##_type>(gmock_p##i)) |
| 5545 |
|
| 5546 |
#define GMOCK_INTERNAL_MATCHER_MEMBERS(args) \ |
| 5547 |
GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_MEMBER, , args) |
| 5548 |
#define GMOCK_INTERNAL_MATCHER_MEMBER(i_unused, data_unused, arg) \ |
| 5549 |
const arg##_type arg; |
| 5550 |
|
| 5551 |
#define GMOCK_INTERNAL_MATCHER_MEMBERS_USAGE(args) \ |
| 5552 |
GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_MEMBER_USAGE, , args)) |
| 5553 |
#define GMOCK_INTERNAL_MATCHER_MEMBER_USAGE(i_unused, data_unused, arg) , arg |
| 5554 |
|
| 5555 |
#define GMOCK_INTERNAL_MATCHER_ARGS_USAGE(args) \ |
| 5556 |
GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_MATCHER_ARG_USAGE, , args)) |
| 5557 |
#define GMOCK_INTERNAL_MATCHER_ARG_USAGE(i, data_unused, arg_unused) \ |
| 5558 |
, gmock_p##i |
| 5559 |
|
| 5560 |
// To prevent ADL on certain functions we put them on a separate namespace. |
| 5561 |
using namespace no_adl; // NOLINT |
| 5562 |
|
| 5563 |
} // namespace testing |
| 5564 |
|
| 5565 |
GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 5046 |
| 5566 |
|
| 5567 |
// Include any custom callback matchers added by the local installation. |
| 5568 |
// We must include this header at the end to make sure it can use the |
| 5569 |
// declarations from this file. |
| 5570 |
#include "gmock/internal/custom/gmock-matchers.h" |
| 5571 |
|
| 5572 |
#endif // GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_ |