| 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 |
|
| 31 |
// Google Mock - a framework for writing C++ mock classes. |
| 32 |
// |
| 33 |
// This file tests some commonly used argument matchers. |
| 34 |
|
| 35 |
// Silence warning C4244: 'initializing': conversion from 'int' to 'short', |
| 36 |
// possible loss of data and C4100, unreferenced local parameter |
| 37 |
#ifdef _MSC_VER |
| 38 |
# pragma warning(push) |
| 39 |
# pragma warning(disable:4244) |
| 40 |
# pragma warning(disable:4100) |
| 41 |
#endif |
| 42 |
|
| 43 |
#include "gmock/gmock-matchers.h" |
| 44 |
|
| 45 |
#include <string.h> |
| 46 |
#include <time.h> |
| 47 |
|
| 48 |
#include <array> |
| 49 |
#include <cstdint> |
| 50 |
#include <deque> |
| 51 |
#include <forward_list> |
| 52 |
#include <functional> |
| 53 |
#include <iostream> |
| 54 |
#include <iterator> |
| 55 |
#include <limits> |
| 56 |
#include <list> |
| 57 |
#include <map> |
| 58 |
#include <memory> |
| 59 |
#include <set> |
| 60 |
#include <sstream> |
| 61 |
#include <string> |
| 62 |
#include <type_traits> |
| 63 |
#include <unordered_map> |
| 64 |
#include <unordered_set> |
| 65 |
#include <utility> |
| 66 |
#include <vector> |
| 67 |
|
| 68 |
#include "gmock/gmock-more-matchers.h" |
| 69 |
#include "gmock/gmock.h" |
| 70 |
#include "gtest/gtest-spi.h" |
| 71 |
#include "gtest/gtest.h" |
| 72 |
|
| 73 |
namespace testing { |
| 74 |
namespace gmock_matchers_test { |
| 75 |
namespace { |
| 76 |
|
| 77 |
using std::greater; |
| 78 |
using std::less; |
| 79 |
using std::list; |
| 80 |
using std::make_pair; |
| 81 |
using std::map; |
| 82 |
using std::multimap; |
| 83 |
using std::multiset; |
| 84 |
using std::ostream; |
| 85 |
using std::pair; |
| 86 |
using std::set; |
| 87 |
using std::stringstream; |
| 88 |
using std::vector; |
| 89 |
using testing::internal::DummyMatchResultListener; |
| 90 |
using testing::internal::ElementMatcherPair; |
| 91 |
using testing::internal::ElementMatcherPairs; |
| 92 |
using testing::internal::ElementsAreArrayMatcher; |
| 93 |
using testing::internal::ExplainMatchFailureTupleTo; |
| 94 |
using testing::internal::FloatingEqMatcher; |
| 95 |
using testing::internal::FormatMatcherDescription; |
| 96 |
using testing::internal::IsReadableTypeName; |
| 97 |
using testing::internal::MatchMatrix; |
| 98 |
using testing::internal::PredicateFormatterFromMatcher; |
| 99 |
using testing::internal::RE; |
| 100 |
using testing::internal::StreamMatchResultListener; |
| 101 |
using testing::internal::Strings; |
| 102 |
|
| 103 |
// Helper for testing container-valued matchers in mock method context. It is |
| 104 |
// important to test matchers in this context, since it requires additional type |
| 105 |
// deduction beyond what EXPECT_THAT does, thus making it more restrictive. |
| 106 |
struct ContainerHelper { |
| 107 |
MOCK_METHOD1(Call, void(std::vector<std::unique_ptr<int>>)); |
| 108 |
}; |
| 109 |
|
| 110 |
std::vector<std::unique_ptr<int>> MakeUniquePtrs(const std::vector<int>& ints) { |
| 111 |
std::vector<std::unique_ptr<int>> pointers; |
| 112 |
for (int i : ints) pointers.emplace_back(new int(i)); |
| 113 |
return pointers; |
| 114 |
} |
| 115 |
|
| 116 |
// For testing ExplainMatchResultTo(). |
| 117 |
template <typename T = int> |
| 118 |
class GreaterThanMatcher : public MatcherInterface<T> { |
| 119 |
public: |
| 120 |
explicit GreaterThanMatcher(T rhs) : rhs_(rhs) {} |
| 121 |
|
| 122 |
void DescribeTo(ostream* os) const override { *os << "is > " << rhs_; } |
| 123 |
|
| 124 |
bool MatchAndExplain(T lhs, MatchResultListener* listener) const override { |
| 125 |
if (lhs > rhs_) { |
| 126 |
*listener << "which is " << (lhs - rhs_) << " more than " << rhs_; |
| 127 |
} else if (lhs == rhs_) { |
| 128 |
*listener << "which is the same as " << rhs_; |
| 129 |
} else { |
| 130 |
*listener << "which is " << (rhs_ - lhs) << " less than " << rhs_; |
| 131 |
} |
| 132 |
|
| 133 |
return lhs > rhs_; |
| 134 |
} |
| 135 |
|
| 136 |
private: |
| 137 |
const T rhs_; |
| 138 |
}; |
| 139 |
|
| 140 |
template <typename T> |
| 141 |
Matcher<T> GreaterThan(T n) { |
| 142 |
return MakeMatcher(new GreaterThanMatcher<T>(n)); |
| 143 |
} |
| 144 |
|
| 145 |
std::string OfType(const std::string& type_name) { |
| 146 |
#if GTEST_HAS_RTTI |
| 147 |
return IsReadableTypeName(type_name) ? " (of type " + type_name + ")" : ""; |
| 148 |
#else |
| 149 |
return ""; |
| 150 |
#endif |
| 151 |
} |
| 152 |
|
| 153 |
// Returns the description of the given matcher. |
| 154 |
template <typename T> |
| 155 |
std::string Describe(const Matcher<T>& m) { |
| 156 |
return DescribeMatcher<T>(m); |
| 157 |
} |
| 158 |
|
| 159 |
// Returns the description of the negation of the given matcher. |
| 160 |
template <typename T> |
| 161 |
std::string DescribeNegation(const Matcher<T>& m) { |
| 162 |
return DescribeMatcher<T>(m, true); |
| 163 |
} |
| 164 |
|
| 165 |
// Returns the reason why x matches, or doesn't match, m. |
| 166 |
template <typename MatcherType, typename Value> |
| 167 |
std::string Explain(const MatcherType& m, const Value& x) { |
| 168 |
StringMatchResultListener listener; |
| 169 |
ExplainMatchResult(m, x, &listener); |
| 170 |
return listener.str(); |
| 171 |
} |
| 172 |
|
| 173 |
TEST(MonotonicMatcherTest, IsPrintable) { |
| 174 |
stringstream ss; |
| 175 |
ss << GreaterThan(5); |
| 176 |
EXPECT_EQ("is > 5", ss.str()); |
| 177 |
} |
| 178 |
|
| 179 |
TEST(MatchResultListenerTest, StreamingWorks) { |
| 180 |
StringMatchResultListener listener; |
| 181 |
listener << "hi" << 5; |
| 182 |
EXPECT_EQ("hi5", listener.str()); |
| 183 |
|
| 184 |
listener.Clear(); |
| 185 |
EXPECT_EQ("", listener.str()); |
| 186 |
|
| 187 |
listener << 42; |
| 188 |
EXPECT_EQ("42", listener.str()); |
| 189 |
|
| 190 |
// Streaming shouldn't crash when the underlying ostream is NULL. |
| 191 |
DummyMatchResultListener dummy; |
| 192 |
dummy << "hi" << 5; |
| 193 |
} |
| 194 |
|
| 195 |
TEST(MatchResultListenerTest, CanAccessUnderlyingStream) { |
| 196 |
EXPECT_TRUE(DummyMatchResultListener().stream() == nullptr); |
| 197 |
EXPECT_TRUE(StreamMatchResultListener(nullptr).stream() == nullptr); |
| 198 |
|
| 199 |
EXPECT_EQ(&std::cout, StreamMatchResultListener(&std::cout).stream()); |
| 200 |
} |
| 201 |
|
| 202 |
TEST(MatchResultListenerTest, IsInterestedWorks) { |
| 203 |
EXPECT_TRUE(StringMatchResultListener().IsInterested()); |
| 204 |
EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested()); |
| 205 |
|
| 206 |
EXPECT_FALSE(DummyMatchResultListener().IsInterested()); |
| 207 |
EXPECT_FALSE(StreamMatchResultListener(nullptr).IsInterested()); |
| 208 |
} |
| 209 |
|
| 210 |
// Makes sure that the MatcherInterface<T> interface doesn't |
| 211 |
// change. |
| 212 |
class EvenMatcherImpl : public MatcherInterface<int> { |
| 213 |
public: |
| 214 |
bool MatchAndExplain(int x, |
| 215 |
MatchResultListener* /* listener */) const override { |
| 216 |
return x % 2 == 0; |
| 217 |
} |
| 218 |
|
| 219 |
void DescribeTo(ostream* os) const override { *os << "is an even number"; } |
| 220 |
|
| 221 |
// We deliberately don't define DescribeNegationTo() and |
| 222 |
// ExplainMatchResultTo() here, to make sure the definition of these |
| 223 |
// two methods is optional. |
| 224 |
}; |
| 225 |
|
| 226 |
// Makes sure that the MatcherInterface API doesn't change. |
| 227 |
TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) { |
| 228 |
EvenMatcherImpl m; |
| 229 |
} |
| 230 |
|
| 231 |
// Tests implementing a monomorphic matcher using MatchAndExplain(). |
| 232 |
|
| 233 |
class NewEvenMatcherImpl : public MatcherInterface<int> { |
| 234 |
public: |
| 235 |
bool MatchAndExplain(int x, MatchResultListener* listener) const override { |
| 236 |
const bool match = x % 2 == 0; |
| 237 |
// Verifies that we can stream to a listener directly. |
| 238 |
*listener << "value % " << 2; |
| 239 |
if (listener->stream() != nullptr) { |
| 240 |
// Verifies that we can stream to a listener's underlying stream |
| 241 |
// too. |
| 242 |
*listener->stream() << " == " << (x % 2); |
| 243 |
} |
| 244 |
return match; |
| 245 |
} |
| 246 |
|
| 247 |
void DescribeTo(ostream* os) const override { *os << "is an even number"; } |
| 248 |
}; |
| 249 |
|
| 250 |
TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) { |
| 251 |
Matcher<int> m = MakeMatcher(new NewEvenMatcherImpl); |
| 252 |
EXPECT_TRUE(m.Matches(2)); |
| 253 |
EXPECT_FALSE(m.Matches(3)); |
| 254 |
EXPECT_EQ("value % 2 == 0", Explain(m, 2)); |
| 255 |
EXPECT_EQ("value % 2 == 1", Explain(m, 3)); |
| 256 |
} |
| 257 |
|
| 258 |
// Tests default-constructing a matcher. |
| 259 |
TEST(MatcherTest, CanBeDefaultConstructed) { |
| 260 |
Matcher<double> m; |
| 261 |
} |
| 262 |
|
| 263 |
// Tests that Matcher<T> can be constructed from a MatcherInterface<T>*. |
| 264 |
TEST(MatcherTest, CanBeConstructedFromMatcherInterface) { |
| 265 |
const MatcherInterface<int>* impl = new EvenMatcherImpl; |
| 266 |
Matcher<int> m(impl); |
| 267 |
EXPECT_TRUE(m.Matches(4)); |
| 268 |
EXPECT_FALSE(m.Matches(5)); |
| 269 |
} |
| 270 |
|
| 271 |
// Tests that value can be used in place of Eq(value). |
| 272 |
TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) { |
| 273 |
Matcher<int> m1 = 5; |
| 274 |
EXPECT_TRUE(m1.Matches(5)); |
| 275 |
EXPECT_FALSE(m1.Matches(6)); |
| 276 |
} |
| 277 |
|
| 278 |
// Tests that NULL can be used in place of Eq(NULL). |
| 279 |
TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) { |
| 280 |
Matcher<int*> m1 = nullptr; |
| 281 |
EXPECT_TRUE(m1.Matches(nullptr)); |
| 282 |
int n = 0; |
| 283 |
EXPECT_FALSE(m1.Matches(&n)); |
| 284 |
} |
| 285 |
|
| 286 |
// Tests that matchers can be constructed from a variable that is not properly |
| 287 |
// defined. This should be illegal, but many users rely on this accidentally. |
| 288 |
struct Undefined { |
| 289 |
virtual ~Undefined() = 0; |
| 290 |
static const int kInt = 1; |
| 291 |
}; |
| 292 |
|
| 293 |
TEST(MatcherTest, CanBeConstructedFromUndefinedVariable) { |
| 294 |
Matcher<int> m1 = Undefined::kInt; |
| 295 |
EXPECT_TRUE(m1.Matches(1)); |
| 296 |
EXPECT_FALSE(m1.Matches(2)); |
| 297 |
} |
| 298 |
|
| 299 |
// Test that a matcher parameterized with an abstract class compiles. |
| 300 |
TEST(MatcherTest, CanAcceptAbstractClass) { Matcher<const Undefined&> m = _; } |
| 301 |
|
| 302 |
// Tests that matchers are copyable. |
| 303 |
TEST(MatcherTest, IsCopyable) { |
| 304 |
// Tests the copy constructor. |
| 305 |
Matcher<bool> m1 = Eq(false); |
| 306 |
EXPECT_TRUE(m1.Matches(false)); |
| 307 |
EXPECT_FALSE(m1.Matches(true)); |
| 308 |
|
| 309 |
// Tests the assignment operator. |
| 310 |
m1 = Eq(true); |
| 311 |
EXPECT_TRUE(m1.Matches(true)); |
| 312 |
EXPECT_FALSE(m1.Matches(false)); |
| 313 |
} |
| 314 |
|
| 315 |
// Tests that Matcher<T>::DescribeTo() calls |
| 316 |
// MatcherInterface<T>::DescribeTo(). |
| 317 |
TEST(MatcherTest, CanDescribeItself) { |
| 318 |
EXPECT_EQ("is an even number", |
| 319 |
Describe(Matcher<int>(new EvenMatcherImpl))); |
| 320 |
} |
| 321 |
|
| 322 |
// Tests Matcher<T>::MatchAndExplain(). |
| 323 |
TEST(MatcherTest, MatchAndExplain) { |
| 324 |
Matcher<int> m = GreaterThan(0); |
| 325 |
StringMatchResultListener listener1; |
| 326 |
EXPECT_TRUE(m.MatchAndExplain(42, &listener1)); |
| 327 |
EXPECT_EQ("which is 42 more than 0", listener1.str()); |
| 328 |
|
| 329 |
StringMatchResultListener listener2; |
| 330 |
EXPECT_FALSE(m.MatchAndExplain(-9, &listener2)); |
| 331 |
EXPECT_EQ("which is 9 less than 0", listener2.str()); |
| 332 |
} |
| 333 |
|
| 334 |
// Tests that a C-string literal can be implicitly converted to a |
| 335 |
// Matcher<std::string> or Matcher<const std::string&>. |
| 336 |
TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) { |
| 337 |
Matcher<std::string> m1 = "hi"; |
| 338 |
EXPECT_TRUE(m1.Matches("hi")); |
| 339 |
EXPECT_FALSE(m1.Matches("hello")); |
| 340 |
|
| 341 |
Matcher<const std::string&> m2 = "hi"; |
| 342 |
EXPECT_TRUE(m2.Matches("hi")); |
| 343 |
EXPECT_FALSE(m2.Matches("hello")); |
| 344 |
} |
| 345 |
|
| 346 |
// Tests that a string object can be implicitly converted to a |
| 347 |
// Matcher<std::string> or Matcher<const std::string&>. |
| 348 |
TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) { |
| 349 |
Matcher<std::string> m1 = std::string("hi"); |
| 350 |
EXPECT_TRUE(m1.Matches("hi")); |
| 351 |
EXPECT_FALSE(m1.Matches("hello")); |
| 352 |
|
| 353 |
Matcher<const std::string&> m2 = std::string("hi"); |
| 354 |
EXPECT_TRUE(m2.Matches("hi")); |
| 355 |
EXPECT_FALSE(m2.Matches("hello")); |
| 356 |
} |
| 357 |
|
| 358 |
#if GTEST_INTERNAL_HAS_STRING_VIEW |
| 359 |
// Tests that a C-string literal can be implicitly converted to a |
| 360 |
// Matcher<StringView> or Matcher<const StringView&>. |
| 361 |
TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) { |
| 362 |
Matcher<internal::StringView> m1 = "cats"; |
| 363 |
EXPECT_TRUE(m1.Matches("cats")); |
| 364 |
EXPECT_FALSE(m1.Matches("dogs")); |
| 365 |
|
| 366 |
Matcher<const internal::StringView&> m2 = "cats"; |
| 367 |
EXPECT_TRUE(m2.Matches("cats")); |
| 368 |
EXPECT_FALSE(m2.Matches("dogs")); |
| 369 |
} |
| 370 |
|
| 371 |
// Tests that a std::string object can be implicitly converted to a |
| 372 |
// Matcher<StringView> or Matcher<const StringView&>. |
| 373 |
TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromString) { |
| 374 |
Matcher<internal::StringView> m1 = std::string("cats"); |
| 375 |
EXPECT_TRUE(m1.Matches("cats")); |
| 376 |
EXPECT_FALSE(m1.Matches("dogs")); |
| 377 |
|
| 378 |
Matcher<const internal::StringView&> m2 = std::string("cats"); |
| 379 |
EXPECT_TRUE(m2.Matches("cats")); |
| 380 |
EXPECT_FALSE(m2.Matches("dogs")); |
| 381 |
} |
| 382 |
|
| 383 |
// Tests that a StringView object can be implicitly converted to a |
| 384 |
// Matcher<StringView> or Matcher<const StringView&>. |
| 385 |
TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromStringView) { |
| 386 |
Matcher<internal::StringView> m1 = internal::StringView("cats"); |
| 387 |
EXPECT_TRUE(m1.Matches("cats")); |
| 388 |
EXPECT_FALSE(m1.Matches("dogs")); |
| 389 |
|
| 390 |
Matcher<const internal::StringView&> m2 = internal::StringView("cats"); |
| 391 |
EXPECT_TRUE(m2.Matches("cats")); |
| 392 |
EXPECT_FALSE(m2.Matches("dogs")); |
| 393 |
} |
| 394 |
#endif // GTEST_INTERNAL_HAS_STRING_VIEW |
| 395 |
|
| 396 |
// Tests that a std::reference_wrapper<std::string> object can be implicitly |
| 397 |
// converted to a Matcher<std::string> or Matcher<const std::string&> via Eq(). |
| 398 |
TEST(StringMatcherTest, |
| 399 |
CanBeImplicitlyConstructedFromEqReferenceWrapperString) { |
| 400 |
std::string value = "cats"; |
| 401 |
Matcher<std::string> m1 = Eq(std::ref(value)); |
| 402 |
EXPECT_TRUE(m1.Matches("cats")); |
| 403 |
EXPECT_FALSE(m1.Matches("dogs")); |
| 404 |
|
| 405 |
Matcher<const std::string&> m2 = Eq(std::ref(value)); |
| 406 |
EXPECT_TRUE(m2.Matches("cats")); |
| 407 |
EXPECT_FALSE(m2.Matches("dogs")); |
| 408 |
} |
| 409 |
|
| 410 |
// Tests that MakeMatcher() constructs a Matcher<T> from a |
| 411 |
// MatcherInterface* without requiring the user to explicitly |
| 412 |
// write the type. |
| 413 |
TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) { |
| 414 |
const MatcherInterface<int>* dummy_impl = new EvenMatcherImpl; |
| 415 |
Matcher<int> m = MakeMatcher(dummy_impl); |
| 416 |
} |
| 417 |
|
| 418 |
// Tests that MakePolymorphicMatcher() can construct a polymorphic |
| 419 |
// matcher from its implementation using the old API. |
| 420 |
const int g_bar = 1; |
| 421 |
class ReferencesBarOrIsZeroImpl { |
| 422 |
public: |
| 423 |
template <typename T> |
| 424 |
bool MatchAndExplain(const T& x, |
| 425 |
MatchResultListener* /* listener */) const { |
| 426 |
const void* p = &x; |
| 427 |
return p == &g_bar || x == 0; |
| 428 |
} |
| 429 |
|
| 430 |
void DescribeTo(ostream* os) const { *os << "g_bar or zero"; } |
| 431 |
|
| 432 |
void DescribeNegationTo(ostream* os) const { |
| 433 |
*os << "doesn't reference g_bar and is not zero"; |
| 434 |
} |
| 435 |
}; |
| 436 |
|
| 437 |
// This function verifies that MakePolymorphicMatcher() returns a |
| 438 |
// PolymorphicMatcher<T> where T is the argument's type. |
| 439 |
PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() { |
| 440 |
return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl()); |
| 441 |
} |
| 442 |
|
| 443 |
TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) { |
| 444 |
// Using a polymorphic matcher to match a reference type. |
| 445 |
Matcher<const int&> m1 = ReferencesBarOrIsZero(); |
| 446 |
EXPECT_TRUE(m1.Matches(0)); |
| 447 |
// Verifies that the identity of a by-reference argument is preserved. |
| 448 |
EXPECT_TRUE(m1.Matches(g_bar)); |
| 449 |
EXPECT_FALSE(m1.Matches(1)); |
| 450 |
EXPECT_EQ("g_bar or zero", Describe(m1)); |
| 451 |
|
| 452 |
// Using a polymorphic matcher to match a value type. |
| 453 |
Matcher<double> m2 = ReferencesBarOrIsZero(); |
| 454 |
EXPECT_TRUE(m2.Matches(0.0)); |
| 455 |
EXPECT_FALSE(m2.Matches(0.1)); |
| 456 |
EXPECT_EQ("g_bar or zero", Describe(m2)); |
| 457 |
} |
| 458 |
|
| 459 |
// Tests implementing a polymorphic matcher using MatchAndExplain(). |
| 460 |
|
| 461 |
class PolymorphicIsEvenImpl { |
| 462 |
public: |
| 463 |
void DescribeTo(ostream* os) const { *os << "is even"; } |
| 464 |
|
| 465 |
void DescribeNegationTo(ostream* os) const { |
| 466 |
*os << "is odd"; |
| 467 |
} |
| 468 |
|
| 469 |
template <typename T> |
| 470 |
bool MatchAndExplain(const T& x, MatchResultListener* listener) const { |
| 471 |
// Verifies that we can stream to the listener directly. |
| 472 |
*listener << "% " << 2; |
| 473 |
if (listener->stream() != nullptr) { |
| 474 |
// Verifies that we can stream to the listener's underlying stream |
| 475 |
// too. |
| 476 |
*listener->stream() << " == " << (x % 2); |
| 477 |
} |
| 478 |
return (x % 2) == 0; |
| 479 |
} |
| 480 |
}; |
| 481 |
|
| 482 |
PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() { |
| 483 |
return MakePolymorphicMatcher(PolymorphicIsEvenImpl()); |
| 484 |
} |
| 485 |
|
| 486 |
TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) { |
| 487 |
// Using PolymorphicIsEven() as a Matcher<int>. |
| 488 |
const Matcher<int> m1 = PolymorphicIsEven(); |
| 489 |
EXPECT_TRUE(m1.Matches(42)); |
| 490 |
EXPECT_FALSE(m1.Matches(43)); |
| 491 |
EXPECT_EQ("is even", Describe(m1)); |
| 492 |
|
| 493 |
const Matcher<int> not_m1 = Not(m1); |
| 494 |
EXPECT_EQ("is odd", Describe(not_m1)); |
| 495 |
|
| 496 |
EXPECT_EQ("% 2 == 0", Explain(m1, 42)); |
| 497 |
|
| 498 |
// Using PolymorphicIsEven() as a Matcher<char>. |
| 499 |
const Matcher<char> m2 = PolymorphicIsEven(); |
| 500 |
EXPECT_TRUE(m2.Matches('\x42')); |
| 501 |
EXPECT_FALSE(m2.Matches('\x43')); |
| 502 |
EXPECT_EQ("is even", Describe(m2)); |
| 503 |
|
| 504 |
const Matcher<char> not_m2 = Not(m2); |
| 505 |
EXPECT_EQ("is odd", Describe(not_m2)); |
| 506 |
|
| 507 |
EXPECT_EQ("% 2 == 0", Explain(m2, '\x42')); |
| 508 |
} |
| 509 |
|
| 510 |
// Tests that MatcherCast<T>(m) works when m is a polymorphic matcher. |
| 511 |
TEST(MatcherCastTest, FromPolymorphicMatcher) { |
| 512 |
Matcher<int> m = MatcherCast<int>(Eq(5)); |
| 513 |
EXPECT_TRUE(m.Matches(5)); |
| 514 |
EXPECT_FALSE(m.Matches(6)); |
| 515 |
} |
| 516 |
|
| 517 |
// For testing casting matchers between compatible types. |
| 518 |
class IntValue { |
| 519 |
public: |
| 520 |
// An int can be statically (although not implicitly) cast to a |
| 521 |
// IntValue. |
| 522 |
explicit IntValue(int a_value) : value_(a_value) {} |
| 523 |
|
| 524 |
int value() const { return value_; } |
| 525 |
private: |
| 526 |
int value_; |
| 527 |
}; |
| 528 |
|
| 529 |
// For testing casting matchers between compatible types. |
| 530 |
bool IsPositiveIntValue(const IntValue& foo) { |
| 531 |
return foo.value() > 0; |
| 532 |
} |
| 533 |
|
| 534 |
// Tests that MatcherCast<T>(m) works when m is a Matcher<U> where T |
| 535 |
// can be statically converted to U. |
| 536 |
TEST(MatcherCastTest, FromCompatibleType) { |
| 537 |
Matcher<double> m1 = Eq(2.0); |
| 538 |
Matcher<int> m2 = MatcherCast<int>(m1); |
| 539 |
EXPECT_TRUE(m2.Matches(2)); |
| 540 |
EXPECT_FALSE(m2.Matches(3)); |
| 541 |
|
| 542 |
Matcher<IntValue> m3 = Truly(IsPositiveIntValue); |
| 543 |
Matcher<int> m4 = MatcherCast<int>(m3); |
| 544 |
// In the following, the arguments 1 and 0 are statically converted |
| 545 |
// to IntValue objects, and then tested by the IsPositiveIntValue() |
| 546 |
// predicate. |
| 547 |
EXPECT_TRUE(m4.Matches(1)); |
| 548 |
EXPECT_FALSE(m4.Matches(0)); |
| 549 |
} |
| 550 |
|
| 551 |
// Tests that MatcherCast<T>(m) works when m is a Matcher<const T&>. |
| 552 |
TEST(MatcherCastTest, FromConstReferenceToNonReference) { |
| 553 |
Matcher<const int&> m1 = Eq(0); |
| 554 |
Matcher<int> m2 = MatcherCast<int>(m1); |
| 555 |
EXPECT_TRUE(m2.Matches(0)); |
| 556 |
EXPECT_FALSE(m2.Matches(1)); |
| 557 |
} |
| 558 |
|
| 559 |
// Tests that MatcherCast<T>(m) works when m is a Matcher<T&>. |
| 560 |
TEST(MatcherCastTest, FromReferenceToNonReference) { |
| 561 |
Matcher<int&> m1 = Eq(0); |
| 562 |
Matcher<int> m2 = MatcherCast<int>(m1); |
| 563 |
EXPECT_TRUE(m2.Matches(0)); |
| 564 |
EXPECT_FALSE(m2.Matches(1)); |
| 565 |
} |
| 566 |
|
| 567 |
// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>. |
| 568 |
TEST(MatcherCastTest, FromNonReferenceToConstReference) { |
| 569 |
Matcher<int> m1 = Eq(0); |
| 570 |
Matcher<const int&> m2 = MatcherCast<const int&>(m1); |
| 571 |
EXPECT_TRUE(m2.Matches(0)); |
| 572 |
EXPECT_FALSE(m2.Matches(1)); |
| 573 |
} |
| 574 |
|
| 575 |
// Tests that MatcherCast<T&>(m) works when m is a Matcher<T>. |
| 576 |
TEST(MatcherCastTest, FromNonReferenceToReference) { |
| 577 |
Matcher<int> m1 = Eq(0); |
| 578 |
Matcher<int&> m2 = MatcherCast<int&>(m1); |
| 579 |
int n = 0; |
| 580 |
EXPECT_TRUE(m2.Matches(n)); |
| 581 |
n = 1; |
| 582 |
EXPECT_FALSE(m2.Matches(n)); |
| 583 |
} |
| 584 |
|
| 585 |
// Tests that MatcherCast<T>(m) works when m is a Matcher<T>. |
| 586 |
TEST(MatcherCastTest, FromSameType) { |
| 587 |
Matcher<int> m1 = Eq(0); |
| 588 |
Matcher<int> m2 = MatcherCast<int>(m1); |
| 589 |
EXPECT_TRUE(m2.Matches(0)); |
| 590 |
EXPECT_FALSE(m2.Matches(1)); |
| 591 |
} |
| 592 |
|
| 593 |
// Tests that MatcherCast<T>(m) works when m is a value of the same type as the |
| 594 |
// value type of the Matcher. |
| 595 |
TEST(MatcherCastTest, FromAValue) { |
| 596 |
Matcher<int> m = MatcherCast<int>(42); |
| 597 |
EXPECT_TRUE(m.Matches(42)); |
| 598 |
EXPECT_FALSE(m.Matches(239)); |
| 599 |
} |
| 600 |
|
| 601 |
// Tests that MatcherCast<T>(m) works when m is a value of the type implicitly |
| 602 |
// convertible to the value type of the Matcher. |
| 603 |
TEST(MatcherCastTest, FromAnImplicitlyConvertibleValue) { |
| 604 |
const int kExpected = 'c'; |
| 605 |
Matcher<int> m = MatcherCast<int>('c'); |
| 606 |
EXPECT_TRUE(m.Matches(kExpected)); |
| 607 |
EXPECT_FALSE(m.Matches(kExpected + 1)); |
| 608 |
} |
| 609 |
|
| 610 |
struct NonImplicitlyConstructibleTypeWithOperatorEq { |
| 611 |
friend bool operator==( |
| 612 |
const NonImplicitlyConstructibleTypeWithOperatorEq& /* ignored */, |
| 613 |
int rhs) { |
| 614 |
return 42 == rhs; |
| 615 |
} |
| 616 |
friend bool operator==( |
| 617 |
int lhs, |
| 618 |
const NonImplicitlyConstructibleTypeWithOperatorEq& /* ignored */) { |
| 619 |
return lhs == 42; |
| 620 |
} |
| 621 |
}; |
| 622 |
|
| 623 |
// Tests that MatcherCast<T>(m) works when m is a neither a matcher nor |
| 624 |
// implicitly convertible to the value type of the Matcher, but the value type |
| 625 |
// of the matcher has operator==() overload accepting m. |
| 626 |
TEST(MatcherCastTest, NonImplicitlyConstructibleTypeWithOperatorEq) { |
| 627 |
Matcher<NonImplicitlyConstructibleTypeWithOperatorEq> m1 = |
| 628 |
MatcherCast<NonImplicitlyConstructibleTypeWithOperatorEq>(42); |
| 629 |
EXPECT_TRUE(m1.Matches(NonImplicitlyConstructibleTypeWithOperatorEq())); |
| 630 |
|
| 631 |
Matcher<NonImplicitlyConstructibleTypeWithOperatorEq> m2 = |
| 632 |
MatcherCast<NonImplicitlyConstructibleTypeWithOperatorEq>(239); |
| 633 |
EXPECT_FALSE(m2.Matches(NonImplicitlyConstructibleTypeWithOperatorEq())); |
| 634 |
|
| 635 |
// When updating the following lines please also change the comment to |
| 636 |
// namespace convertible_from_any. |
| 637 |
Matcher<int> m3 = |
| 638 |
MatcherCast<int>(NonImplicitlyConstructibleTypeWithOperatorEq()); |
| 639 |
EXPECT_TRUE(m3.Matches(42)); |
| 640 |
EXPECT_FALSE(m3.Matches(239)); |
| 641 |
} |
| 642 |
|
| 643 |
// ConvertibleFromAny does not work with MSVC. resulting in |
| 644 |
// error C2440: 'initializing': cannot convert from 'Eq' to 'M' |
| 645 |
// No constructor could take the source type, or constructor overload |
| 646 |
// resolution was ambiguous |
| 647 |
|
| 648 |
#if !defined _MSC_VER |
| 649 |
|
| 650 |
// The below ConvertibleFromAny struct is implicitly constructible from anything |
| 651 |
// and when in the same namespace can interact with other tests. In particular, |
| 652 |
// if it is in the same namespace as other tests and one removes |
| 653 |
// NonImplicitlyConstructibleTypeWithOperatorEq::operator==(int lhs, ...); |
| 654 |
// then the corresponding test still compiles (and it should not!) by implicitly |
| 655 |
// converting NonImplicitlyConstructibleTypeWithOperatorEq to ConvertibleFromAny |
| 656 |
// in m3.Matcher(). |
| 657 |
namespace convertible_from_any { |
| 658 |
// Implicitly convertible from any type. |
| 659 |
struct ConvertibleFromAny { |
| 660 |
ConvertibleFromAny(int a_value) : value(a_value) {} |
| 661 |
template <typename T> |
| 662 |
ConvertibleFromAny(const T& /*a_value*/) : value(-1) { |
| 663 |
ADD_FAILURE() << "Conversion constructor called"; |
| 664 |
} |
| 665 |
int value; |
| 666 |
}; |
| 667 |
|
| 668 |
bool operator==(const ConvertibleFromAny& a, const ConvertibleFromAny& b) { |
| 669 |
return a.value == b.value; |
| 670 |
} |
| 671 |
|
| 672 |
ostream& operator<<(ostream& os, const ConvertibleFromAny& a) { |
| 673 |
return os << a.value; |
| 674 |
} |
| 675 |
|
| 676 |
TEST(MatcherCastTest, ConversionConstructorIsUsed) { |
| 677 |
Matcher<ConvertibleFromAny> m = MatcherCast<ConvertibleFromAny>(1); |
| 678 |
EXPECT_TRUE(m.Matches(ConvertibleFromAny(1))); |
| 679 |
EXPECT_FALSE(m.Matches(ConvertibleFromAny(2))); |
| 680 |
} |
| 681 |
|
| 682 |
TEST(MatcherCastTest, FromConvertibleFromAny) { |
| 683 |
Matcher<ConvertibleFromAny> m = |
| 684 |
MatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1))); |
| 685 |
EXPECT_TRUE(m.Matches(ConvertibleFromAny(1))); |
| 686 |
EXPECT_FALSE(m.Matches(ConvertibleFromAny(2))); |
| 687 |
} |
| 688 |
} // namespace convertible_from_any |
| 689 |
|
| 690 |
#endif // !defined _MSC_VER |
| 691 |
|
| 692 |
struct IntReferenceWrapper { |
| 693 |
IntReferenceWrapper(const int& a_value) : value(&a_value) {} |
| 694 |
const int* value; |
| 695 |
}; |
| 696 |
|
| 697 |
bool operator==(const IntReferenceWrapper& a, const IntReferenceWrapper& b) { |
| 698 |
return a.value == b.value; |
| 699 |
} |
| 700 |
|
| 701 |
TEST(MatcherCastTest, ValueIsNotCopied) { |
| 702 |
int n = 42; |
| 703 |
Matcher<IntReferenceWrapper> m = MatcherCast<IntReferenceWrapper>(n); |
| 704 |
// Verify that the matcher holds a reference to n, not to its temporary copy. |
| 705 |
EXPECT_TRUE(m.Matches(n)); |
| 706 |
} |
| 707 |
|
| 708 |
class Base { |
| 709 |
public: |
| 710 |
virtual ~Base() {} |
| 711 |
Base() {} |
| 712 |
private: |
| 713 |
GTEST_DISALLOW_COPY_AND_ASSIGN_(Base); |
| 714 |
}; |
| 715 |
|
| 716 |
class Derived : public Base { |
| 717 |
public: |
| 718 |
Derived() : Base() {} |
| 719 |
int i; |
| 720 |
}; |
| 721 |
|
| 722 |
class OtherDerived : public Base {}; |
| 723 |
|
| 724 |
// Tests that SafeMatcherCast<T>(m) works when m is a polymorphic matcher. |
| 725 |
TEST(SafeMatcherCastTest, FromPolymorphicMatcher) { |
| 726 |
Matcher<char> m2 = SafeMatcherCast<char>(Eq(32)); |
| 727 |
EXPECT_TRUE(m2.Matches(' ')); |
| 728 |
EXPECT_FALSE(m2.Matches('\n')); |
| 729 |
} |
| 730 |
|
| 731 |
// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where |
| 732 |
// T and U are arithmetic types and T can be losslessly converted to |
| 733 |
// U. |
| 734 |
TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) { |
| 735 |
Matcher<double> m1 = DoubleEq(1.0); |
| 736 |
Matcher<float> m2 = SafeMatcherCast<float>(m1); |
| 737 |
EXPECT_TRUE(m2.Matches(1.0f)); |
| 738 |
EXPECT_FALSE(m2.Matches(2.0f)); |
| 739 |
|
| 740 |
Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>('a')); |
| 741 |
EXPECT_TRUE(m3.Matches('a')); |
| 742 |
EXPECT_FALSE(m3.Matches('b')); |
| 743 |
} |
| 744 |
|
| 745 |
// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where T and U |
| 746 |
// are pointers or references to a derived and a base class, correspondingly. |
| 747 |
TEST(SafeMatcherCastTest, FromBaseClass) { |
| 748 |
Derived d, d2; |
| 749 |
Matcher<Base*> m1 = Eq(&d); |
| 750 |
Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1); |
| 751 |
EXPECT_TRUE(m2.Matches(&d)); |
| 752 |
EXPECT_FALSE(m2.Matches(&d2)); |
| 753 |
|
| 754 |
Matcher<Base&> m3 = Ref(d); |
| 755 |
Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3); |
| 756 |
EXPECT_TRUE(m4.Matches(d)); |
| 757 |
EXPECT_FALSE(m4.Matches(d2)); |
| 758 |
} |
| 759 |
|
| 760 |
// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<const T&>. |
| 761 |
TEST(SafeMatcherCastTest, FromConstReferenceToReference) { |
| 762 |
int n = 0; |
| 763 |
Matcher<const int&> m1 = Ref(n); |
| 764 |
Matcher<int&> m2 = SafeMatcherCast<int&>(m1); |
| 765 |
int n1 = 0; |
| 766 |
EXPECT_TRUE(m2.Matches(n)); |
| 767 |
EXPECT_FALSE(m2.Matches(n1)); |
| 768 |
} |
| 769 |
|
| 770 |
// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>. |
| 771 |
TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) { |
| 772 |
Matcher<std::unique_ptr<int>> m1 = IsNull(); |
| 773 |
Matcher<const std::unique_ptr<int>&> m2 = |
| 774 |
SafeMatcherCast<const std::unique_ptr<int>&>(m1); |
| 775 |
EXPECT_TRUE(m2.Matches(std::unique_ptr<int>())); |
| 776 |
EXPECT_FALSE(m2.Matches(std::unique_ptr<int>(new int))); |
| 777 |
} |
| 778 |
|
| 779 |
// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<T>. |
| 780 |
TEST(SafeMatcherCastTest, FromNonReferenceToReference) { |
| 781 |
Matcher<int> m1 = Eq(0); |
| 782 |
Matcher<int&> m2 = SafeMatcherCast<int&>(m1); |
| 783 |
int n = 0; |
| 784 |
EXPECT_TRUE(m2.Matches(n)); |
| 785 |
n = 1; |
| 786 |
EXPECT_FALSE(m2.Matches(n)); |
| 787 |
} |
| 788 |
|
| 789 |
// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<T>. |
| 790 |
TEST(SafeMatcherCastTest, FromSameType) { |
| 791 |
Matcher<int> m1 = Eq(0); |
| 792 |
Matcher<int> m2 = SafeMatcherCast<int>(m1); |
| 793 |
EXPECT_TRUE(m2.Matches(0)); |
| 794 |
EXPECT_FALSE(m2.Matches(1)); |
| 795 |
} |
| 796 |
|
| 797 |
#if !defined _MSC_VER |
| 798 |
|
| 799 |
namespace convertible_from_any { |
| 800 |
TEST(SafeMatcherCastTest, ConversionConstructorIsUsed) { |
| 801 |
Matcher<ConvertibleFromAny> m = SafeMatcherCast<ConvertibleFromAny>(1); |
| 802 |
EXPECT_TRUE(m.Matches(ConvertibleFromAny(1))); |
| 803 |
EXPECT_FALSE(m.Matches(ConvertibleFromAny(2))); |
| 804 |
} |
| 805 |
|
| 806 |
TEST(SafeMatcherCastTest, FromConvertibleFromAny) { |
| 807 |
Matcher<ConvertibleFromAny> m = |
| 808 |
SafeMatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1))); |
| 809 |
EXPECT_TRUE(m.Matches(ConvertibleFromAny(1))); |
| 810 |
EXPECT_FALSE(m.Matches(ConvertibleFromAny(2))); |
| 811 |
} |
| 812 |
} // namespace convertible_from_any |
| 813 |
|
| 814 |
#endif // !defined _MSC_VER |
| 815 |
|
| 816 |
TEST(SafeMatcherCastTest, ValueIsNotCopied) { |
| 817 |
int n = 42; |
| 818 |
Matcher<IntReferenceWrapper> m = SafeMatcherCast<IntReferenceWrapper>(n); |
| 819 |
// Verify that the matcher holds a reference to n, not to its temporary copy. |
| 820 |
EXPECT_TRUE(m.Matches(n)); |
| 821 |
} |
| 822 |
|
| 823 |
TEST(ExpectThat, TakesLiterals) { |
| 824 |
EXPECT_THAT(1, 1); |
| 825 |
EXPECT_THAT(1.0, 1.0); |
| 826 |
EXPECT_THAT(std::string(), ""); |
| 827 |
} |
| 828 |
|
| 829 |
TEST(ExpectThat, TakesFunctions) { |
| 830 |
struct Helper { |
| 831 |
static void Func() {} |
| 832 |
}; |
| 833 |
void (*func)() = Helper::Func; |
| 834 |
EXPECT_THAT(func, Helper::Func); |
| 835 |
EXPECT_THAT(func, &Helper::Func); |
| 836 |
} |
| 837 |
|
| 838 |
// Tests that A<T>() matches any value of type T. |
| 839 |
TEST(ATest, MatchesAnyValue) { |
| 840 |
// Tests a matcher for a value type. |
| 841 |
Matcher<double> m1 = A<double>(); |
| 842 |
EXPECT_TRUE(m1.Matches(91.43)); |
| 843 |
EXPECT_TRUE(m1.Matches(-15.32)); |
| 844 |
|
| 845 |
// Tests a matcher for a reference type. |
| 846 |
int a = 2; |
| 847 |
int b = -6; |
| 848 |
Matcher<int&> m2 = A<int&>(); |
| 849 |
EXPECT_TRUE(m2.Matches(a)); |
| 850 |
EXPECT_TRUE(m2.Matches(b)); |
| 851 |
} |
| 852 |
|
| 853 |
TEST(ATest, WorksForDerivedClass) { |
| 854 |
Base base; |
| 855 |
Derived derived; |
| 856 |
EXPECT_THAT(&base, A<Base*>()); |
| 857 |
// This shouldn't compile: EXPECT_THAT(&base, A<Derived*>()); |
| 858 |
EXPECT_THAT(&derived, A<Base*>()); |
| 859 |
EXPECT_THAT(&derived, A<Derived*>()); |
| 860 |
} |
| 861 |
|
| 862 |
// Tests that A<T>() describes itself properly. |
| 863 |
TEST(ATest, CanDescribeSelf) { |
| 864 |
EXPECT_EQ("is anything", Describe(A<bool>())); |
| 865 |
} |
| 866 |
|
| 867 |
// Tests that An<T>() matches any value of type T. |
| 868 |
TEST(AnTest, MatchesAnyValue) { |
| 869 |
// Tests a matcher for a value type. |
| 870 |
Matcher<int> m1 = An<int>(); |
| 871 |
EXPECT_TRUE(m1.Matches(9143)); |
| 872 |
EXPECT_TRUE(m1.Matches(-1532)); |
| 873 |
|
| 874 |
// Tests a matcher for a reference type. |
| 875 |
int a = 2; |
| 876 |
int b = -6; |
| 877 |
Matcher<int&> m2 = An<int&>(); |
| 878 |
EXPECT_TRUE(m2.Matches(a)); |
| 879 |
EXPECT_TRUE(m2.Matches(b)); |
| 880 |
} |
| 881 |
|
| 882 |
// Tests that An<T>() describes itself properly. |
| 883 |
TEST(AnTest, CanDescribeSelf) { |
| 884 |
EXPECT_EQ("is anything", Describe(An<int>())); |
| 885 |
} |
| 886 |
|
| 887 |
// Tests that _ can be used as a matcher for any type and matches any |
| 888 |
// value of that type. |
| 889 |
TEST(UnderscoreTest, MatchesAnyValue) { |
| 890 |
// Uses _ as a matcher for a value type. |
| 891 |
Matcher<int> m1 = _; |
| 892 |
EXPECT_TRUE(m1.Matches(123)); |
| 893 |
EXPECT_TRUE(m1.Matches(-242)); |
| 894 |
|
| 895 |
// Uses _ as a matcher for a reference type. |
| 896 |
bool a = false; |
| 897 |
const bool b = true; |
| 898 |
Matcher<const bool&> m2 = _; |
| 899 |
EXPECT_TRUE(m2.Matches(a)); |
| 900 |
EXPECT_TRUE(m2.Matches(b)); |
| 901 |
} |
| 902 |
|
| 903 |
// Tests that _ describes itself properly. |
| 904 |
TEST(UnderscoreTest, CanDescribeSelf) { |
| 905 |
Matcher<int> m = _; |
| 906 |
EXPECT_EQ("is anything", Describe(m)); |
| 907 |
} |
| 908 |
|
| 909 |
// Tests that Eq(x) matches any value equal to x. |
| 910 |
TEST(EqTest, MatchesEqualValue) { |
| 911 |
// 2 C-strings with same content but different addresses. |
| 912 |
const char a1[] = "hi"; |
| 913 |
const char a2[] = "hi"; |
| 914 |
|
| 915 |
Matcher<const char*> m1 = Eq(a1); |
| 916 |
EXPECT_TRUE(m1.Matches(a1)); |
| 917 |
EXPECT_FALSE(m1.Matches(a2)); |
| 918 |
} |
| 919 |
|
| 920 |
// Tests that Eq(v) describes itself properly. |
| 921 |
|
| 922 |
class Unprintable { |
| 923 |
public: |
| 924 |
Unprintable() : c_('a') {} |
| 925 |
|
| 926 |
bool operator==(const Unprintable& /* rhs */) const { return true; } |
| 927 |
// -Wunused-private-field: dummy accessor for `c_`. |
| 928 |
char dummy_c() { return c_; } |
| 929 |
private: |
| 930 |
char c_; |
| 931 |
}; |
| 932 |
|
| 933 |
TEST(EqTest, CanDescribeSelf) { |
| 934 |
Matcher<Unprintable> m = Eq(Unprintable()); |
| 935 |
EXPECT_EQ("is equal to 1-byte object <61>", Describe(m)); |
| 936 |
} |
| 937 |
|
| 938 |
// Tests that Eq(v) can be used to match any type that supports |
| 939 |
// comparing with type T, where T is v's type. |
| 940 |
TEST(EqTest, IsPolymorphic) { |
| 941 |
Matcher<int> m1 = Eq(1); |
| 942 |
EXPECT_TRUE(m1.Matches(1)); |
| 943 |
EXPECT_FALSE(m1.Matches(2)); |
| 944 |
|
| 945 |
Matcher<char> m2 = Eq(1); |
| 946 |
EXPECT_TRUE(m2.Matches('\1')); |
| 947 |
EXPECT_FALSE(m2.Matches('a')); |
| 948 |
} |
| 949 |
|
| 950 |
// Tests that TypedEq<T>(v) matches values of type T that's equal to v. |
| 951 |
TEST(TypedEqTest, ChecksEqualityForGivenType) { |
| 952 |
Matcher<char> m1 = TypedEq<char>('a'); |
| 953 |
EXPECT_TRUE(m1.Matches('a')); |
| 954 |
EXPECT_FALSE(m1.Matches('b')); |
| 955 |
|
| 956 |
Matcher<int> m2 = TypedEq<int>(6); |
| 957 |
EXPECT_TRUE(m2.Matches(6)); |
| 958 |
EXPECT_FALSE(m2.Matches(7)); |
| 959 |
} |
| 960 |
|
| 961 |
// Tests that TypedEq(v) describes itself properly. |
| 962 |
TEST(TypedEqTest, CanDescribeSelf) { |
| 963 |
EXPECT_EQ("is equal to 2", Describe(TypedEq<int>(2))); |
| 964 |
} |
| 965 |
|
| 966 |
// Tests that TypedEq<T>(v) has type Matcher<T>. |
| 967 |
|
| 968 |
// Type<T>::IsTypeOf(v) compiles if and only if the type of value v is T, where |
| 969 |
// T is a "bare" type (i.e. not in the form of const U or U&). If v's type is |
| 970 |
// not T, the compiler will generate a message about "undefined reference". |
| 971 |
template <typename T> |
| 972 |
struct Type { |
| 973 |
static bool IsTypeOf(const T& /* v */) { return true; } |
| 974 |
|
| 975 |
template <typename T2> |
| 976 |
static void IsTypeOf(T2 v); |
| 977 |
}; |
| 978 |
|
| 979 |
TEST(TypedEqTest, HasSpecifiedType) { |
| 980 |
// Verfies that the type of TypedEq<T>(v) is Matcher<T>. |
| 981 |
Type<Matcher<int> >::IsTypeOf(TypedEq<int>(5)); |
| 982 |
Type<Matcher<double> >::IsTypeOf(TypedEq<double>(5)); |
| 983 |
} |
| 984 |
|
| 985 |
// Tests that Ge(v) matches anything >= v. |
| 986 |
TEST(GeTest, ImplementsGreaterThanOrEqual) { |
| 987 |
Matcher<int> m1 = Ge(0); |
| 988 |
EXPECT_TRUE(m1.Matches(1)); |
| 989 |
EXPECT_TRUE(m1.Matches(0)); |
| 990 |
EXPECT_FALSE(m1.Matches(-1)); |
| 991 |
} |
| 992 |
|
| 993 |
// Tests that Ge(v) describes itself properly. |
| 994 |
TEST(GeTest, CanDescribeSelf) { |
| 995 |
Matcher<int> m = Ge(5); |
| 996 |
EXPECT_EQ("is >= 5", Describe(m)); |
| 997 |
} |
| 998 |
|
| 999 |
// Tests that Gt(v) matches anything > v. |
| 1000 |
TEST(GtTest, ImplementsGreaterThan) { |
| 1001 |
Matcher<double> m1 = Gt(0); |
| 1002 |
EXPECT_TRUE(m1.Matches(1.0)); |
| 1003 |
EXPECT_FALSE(m1.Matches(0.0)); |
| 1004 |
EXPECT_FALSE(m1.Matches(-1.0)); |
| 1005 |
} |
| 1006 |
|
| 1007 |
// Tests that Gt(v) describes itself properly. |
| 1008 |
TEST(GtTest, CanDescribeSelf) { |
| 1009 |
Matcher<int> m = Gt(5); |
| 1010 |
EXPECT_EQ("is > 5", Describe(m)); |
| 1011 |
} |
| 1012 |
|
| 1013 |
// Tests that Le(v) matches anything <= v. |
| 1014 |
TEST(LeTest, ImplementsLessThanOrEqual) { |
| 1015 |
Matcher<char> m1 = Le('b'); |
| 1016 |
EXPECT_TRUE(m1.Matches('a')); |
| 1017 |
EXPECT_TRUE(m1.Matches('b')); |
| 1018 |
EXPECT_FALSE(m1.Matches('c')); |
| 1019 |
} |
| 1020 |
|
| 1021 |
// Tests that Le(v) describes itself properly. |
| 1022 |
TEST(LeTest, CanDescribeSelf) { |
| 1023 |
Matcher<int> m = Le(5); |
| 1024 |
EXPECT_EQ("is <= 5", Describe(m)); |
| 1025 |
} |
| 1026 |
|
| 1027 |
// Tests that Lt(v) matches anything < v. |
| 1028 |
TEST(LtTest, ImplementsLessThan) { |
| 1029 |
Matcher<const std::string&> m1 = Lt("Hello"); |
| 1030 |
EXPECT_TRUE(m1.Matches("Abc")); |
| 1031 |
EXPECT_FALSE(m1.Matches("Hello")); |
| 1032 |
EXPECT_FALSE(m1.Matches("Hello, world!")); |
| 1033 |
} |
| 1034 |
|
| 1035 |
// Tests that Lt(v) describes itself properly. |
| 1036 |
TEST(LtTest, CanDescribeSelf) { |
| 1037 |
Matcher<int> m = Lt(5); |
| 1038 |
EXPECT_EQ("is < 5", Describe(m)); |
| 1039 |
} |
| 1040 |
|
| 1041 |
// Tests that Ne(v) matches anything != v. |
| 1042 |
TEST(NeTest, ImplementsNotEqual) { |
| 1043 |
Matcher<int> m1 = Ne(0); |
| 1044 |
EXPECT_TRUE(m1.Matches(1)); |
| 1045 |
EXPECT_TRUE(m1.Matches(-1)); |
| 1046 |
EXPECT_FALSE(m1.Matches(0)); |
| 1047 |
} |
| 1048 |
|
| 1049 |
// Tests that Ne(v) describes itself properly. |
| 1050 |
TEST(NeTest, CanDescribeSelf) { |
| 1051 |
Matcher<int> m = Ne(5); |
| 1052 |
EXPECT_EQ("isn't equal to 5", Describe(m)); |
| 1053 |
} |
| 1054 |
|
| 1055 |
class MoveOnly { |
| 1056 |
public: |
| 1057 |
explicit MoveOnly(int i) : i_(i) {} |
| 1058 |
MoveOnly(const MoveOnly&) = delete; |
| 1059 |
MoveOnly(MoveOnly&&) = default; |
| 1060 |
MoveOnly& operator=(const MoveOnly&) = delete; |
| 1061 |
MoveOnly& operator=(MoveOnly&&) = default; |
| 1062 |
|
| 1063 |
bool operator==(const MoveOnly& other) const { return i_ == other.i_; } |
| 1064 |
bool operator!=(const MoveOnly& other) const { return i_ != other.i_; } |
| 1065 |
bool operator<(const MoveOnly& other) const { return i_ < other.i_; } |
| 1066 |
bool operator<=(const MoveOnly& other) const { return i_ <= other.i_; } |
| 1067 |
bool operator>(const MoveOnly& other) const { return i_ > other.i_; } |
| 1068 |
bool operator>=(const MoveOnly& other) const { return i_ >= other.i_; } |
| 1069 |
|
| 1070 |
private: |
| 1071 |
int i_; |
| 1072 |
}; |
| 1073 |
|
| 1074 |
struct MoveHelper { |
| 1075 |
MOCK_METHOD1(Call, void(MoveOnly)); |
| 1076 |
}; |
| 1077 |
|
| 1078 |
// Disable this test in VS 2015 (version 14), where it fails when SEH is enabled |
| 1079 |
#if defined(_MSC_VER) && (_MSC_VER < 1910) |
| 1080 |
TEST(ComparisonBaseTest, DISABLED_WorksWithMoveOnly) { |
| 1081 |
#else |
| 1082 |
TEST(ComparisonBaseTest, WorksWithMoveOnly) { |
| 1083 |
#endif |
| 1084 |
MoveOnly m{0}; |
| 1085 |
MoveHelper helper; |
| 1086 |
|
| 1087 |
EXPECT_CALL(helper, Call(Eq(ByRef(m)))); |
| 1088 |
helper.Call(MoveOnly(0)); |
| 1089 |
EXPECT_CALL(helper, Call(Ne(ByRef(m)))); |
| 1090 |
helper.Call(MoveOnly(1)); |
| 1091 |
EXPECT_CALL(helper, Call(Le(ByRef(m)))); |
| 1092 |
helper.Call(MoveOnly(0)); |
| 1093 |
EXPECT_CALL(helper, Call(Lt(ByRef(m)))); |
| 1094 |
helper.Call(MoveOnly(-1)); |
| 1095 |
EXPECT_CALL(helper, Call(Ge(ByRef(m)))); |
| 1096 |
helper.Call(MoveOnly(0)); |
| 1097 |
EXPECT_CALL(helper, Call(Gt(ByRef(m)))); |
| 1098 |
helper.Call(MoveOnly(1)); |
| 1099 |
} |
| 1100 |
|
| 1101 |
// Tests that IsNull() matches any NULL pointer of any type. |
| 1102 |
TEST(IsNullTest, MatchesNullPointer) { |
| 1103 |
Matcher<int*> m1 = IsNull(); |
| 1104 |
int* p1 = nullptr; |
| 1105 |
int n = 0; |
| 1106 |
EXPECT_TRUE(m1.Matches(p1)); |
| 1107 |
EXPECT_FALSE(m1.Matches(&n)); |
| 1108 |
|
| 1109 |
Matcher<const char*> m2 = IsNull(); |
| 1110 |
const char* p2 = nullptr; |
| 1111 |
EXPECT_TRUE(m2.Matches(p2)); |
| 1112 |
EXPECT_FALSE(m2.Matches("hi")); |
| 1113 |
|
| 1114 |
Matcher<void*> m3 = IsNull(); |
| 1115 |
void* p3 = nullptr; |
| 1116 |
EXPECT_TRUE(m3.Matches(p3)); |
| 1117 |
EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef))); |
| 1118 |
} |
| 1119 |
|
| 1120 |
TEST(IsNullTest, StdFunction) { |
| 1121 |
const Matcher<std::function<void()>> m = IsNull(); |
| 1122 |
|
| 1123 |
EXPECT_TRUE(m.Matches(std::function<void()>())); |
| 1124 |
EXPECT_FALSE(m.Matches([]{})); |
| 1125 |
} |
| 1126 |
|
| 1127 |
// Tests that IsNull() describes itself properly. |
| 1128 |
TEST(IsNullTest, CanDescribeSelf) { |
| 1129 |
Matcher<int*> m = IsNull(); |
| 1130 |
EXPECT_EQ("is NULL", Describe(m)); |
| 1131 |
EXPECT_EQ("isn't NULL", DescribeNegation(m)); |
| 1132 |
} |
| 1133 |
|
| 1134 |
// Tests that NotNull() matches any non-NULL pointer of any type. |
| 1135 |
TEST(NotNullTest, MatchesNonNullPointer) { |
| 1136 |
Matcher<int*> m1 = NotNull(); |
| 1137 |
int* p1 = nullptr; |
| 1138 |
int n = 0; |
| 1139 |
EXPECT_FALSE(m1.Matches(p1)); |
| 1140 |
EXPECT_TRUE(m1.Matches(&n)); |
| 1141 |
|
| 1142 |
Matcher<const char*> m2 = NotNull(); |
| 1143 |
const char* p2 = nullptr; |
| 1144 |
EXPECT_FALSE(m2.Matches(p2)); |
| 1145 |
EXPECT_TRUE(m2.Matches("hi")); |
| 1146 |
} |
| 1147 |
|
| 1148 |
TEST(NotNullTest, LinkedPtr) { |
| 1149 |
const Matcher<std::shared_ptr<int>> m = NotNull(); |
| 1150 |
const std::shared_ptr<int> null_p; |
| 1151 |
const std::shared_ptr<int> non_null_p(new int); |
| 1152 |
|
| 1153 |
EXPECT_FALSE(m.Matches(null_p)); |
| 1154 |
EXPECT_TRUE(m.Matches(non_null_p)); |
| 1155 |
} |
| 1156 |
|
| 1157 |
TEST(NotNullTest, ReferenceToConstLinkedPtr) { |
| 1158 |
const Matcher<const std::shared_ptr<double>&> m = NotNull(); |
| 1159 |
const std::shared_ptr<double> null_p; |
| 1160 |
const std::shared_ptr<double> non_null_p(new double); |
| 1161 |
|
| 1162 |
EXPECT_FALSE(m.Matches(null_p)); |
| 1163 |
EXPECT_TRUE(m.Matches(non_null_p)); |
| 1164 |
} |
| 1165 |
|
| 1166 |
TEST(NotNullTest, StdFunction) { |
| 1167 |
const Matcher<std::function<void()>> m = NotNull(); |
| 1168 |
|
| 1169 |
EXPECT_TRUE(m.Matches([]{})); |
| 1170 |
EXPECT_FALSE(m.Matches(std::function<void()>())); |
| 1171 |
} |
| 1172 |
|
| 1173 |
// Tests that NotNull() describes itself properly. |
| 1174 |
TEST(NotNullTest, CanDescribeSelf) { |
| 1175 |
Matcher<int*> m = NotNull(); |
| 1176 |
EXPECT_EQ("isn't NULL", Describe(m)); |
| 1177 |
} |
| 1178 |
|
| 1179 |
// Tests that Ref(variable) matches an argument that references |
| 1180 |
// 'variable'. |
| 1181 |
TEST(RefTest, MatchesSameVariable) { |
| 1182 |
int a = 0; |
| 1183 |
int b = 0; |
| 1184 |
Matcher<int&> m = Ref(a); |
| 1185 |
EXPECT_TRUE(m.Matches(a)); |
| 1186 |
EXPECT_FALSE(m.Matches(b)); |
| 1187 |
} |
| 1188 |
|
| 1189 |
// Tests that Ref(variable) describes itself properly. |
| 1190 |
TEST(RefTest, CanDescribeSelf) { |
| 1191 |
int n = 5; |
| 1192 |
Matcher<int&> m = Ref(n); |
| 1193 |
stringstream ss; |
| 1194 |
ss << "references the variable @" << &n << " 5"; |
| 1195 |
EXPECT_EQ(ss.str(), Describe(m)); |
| 1196 |
} |
| 1197 |
|
| 1198 |
// Test that Ref(non_const_varialbe) can be used as a matcher for a |
| 1199 |
// const reference. |
| 1200 |
TEST(RefTest, CanBeUsedAsMatcherForConstReference) { |
| 1201 |
int a = 0; |
| 1202 |
int b = 0; |
| 1203 |
Matcher<const int&> m = Ref(a); |
| 1204 |
EXPECT_TRUE(m.Matches(a)); |
| 1205 |
EXPECT_FALSE(m.Matches(b)); |
| 1206 |
} |
| 1207 |
|
| 1208 |
// Tests that Ref(variable) is covariant, i.e. Ref(derived) can be |
| 1209 |
// used wherever Ref(base) can be used (Ref(derived) is a sub-type |
| 1210 |
// of Ref(base), but not vice versa. |
| 1211 |
|
| 1212 |
TEST(RefTest, IsCovariant) { |
| 1213 |
Base base, base2; |
| 1214 |
Derived derived; |
| 1215 |
Matcher<const Base&> m1 = Ref(base); |
| 1216 |
EXPECT_TRUE(m1.Matches(base)); |
| 1217 |
EXPECT_FALSE(m1.Matches(base2)); |
| 1218 |
EXPECT_FALSE(m1.Matches(derived)); |
| 1219 |
|
| 1220 |
m1 = Ref(derived); |
| 1221 |
EXPECT_TRUE(m1.Matches(derived)); |
| 1222 |
EXPECT_FALSE(m1.Matches(base)); |
| 1223 |
EXPECT_FALSE(m1.Matches(base2)); |
| 1224 |
} |
| 1225 |
|
| 1226 |
TEST(RefTest, ExplainsResult) { |
| 1227 |
int n = 0; |
| 1228 |
EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), n), |
| 1229 |
StartsWith("which is located @")); |
| 1230 |
|
| 1231 |
int m = 0; |
| 1232 |
EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), m), |
| 1233 |
StartsWith("which is located @")); |
| 1234 |
} |
| 1235 |
|
| 1236 |
// Tests string comparison matchers. |
| 1237 |
|
| 1238 |
template <typename T = std::string> |
| 1239 |
std::string FromStringLike(internal::StringLike<T> str) { |
| 1240 |
return std::string(str); |
| 1241 |
} |
| 1242 |
|
| 1243 |
TEST(StringLike, TestConversions) { |
| 1244 |
EXPECT_EQ("foo", FromStringLike("foo")); |
| 1245 |
EXPECT_EQ("foo", FromStringLike(std::string("foo"))); |
| 1246 |
#if GTEST_INTERNAL_HAS_STRING_VIEW |
| 1247 |
EXPECT_EQ("foo", FromStringLike(internal::StringView("foo"))); |
| 1248 |
#endif // GTEST_INTERNAL_HAS_STRING_VIEW |
| 1249 |
|
| 1250 |
// Non deducible types. |
| 1251 |
EXPECT_EQ("", FromStringLike({})); |
| 1252 |
EXPECT_EQ("foo", FromStringLike({'f', 'o', 'o'})); |
| 1253 |
const char buf[] = "foo"; |
| 1254 |
EXPECT_EQ("foo", FromStringLike({buf, buf + 3})); |
| 1255 |
} |
| 1256 |
|
| 1257 |
TEST(StrEqTest, MatchesEqualString) { |
| 1258 |
Matcher<const char*> m = StrEq(std::string("Hello")); |
| 1259 |
EXPECT_TRUE(m.Matches("Hello")); |
| 1260 |
EXPECT_FALSE(m.Matches("hello")); |
| 1261 |
EXPECT_FALSE(m.Matches(nullptr)); |
| 1262 |
|
| 1263 |
Matcher<const std::string&> m2 = StrEq("Hello"); |
| 1264 |
EXPECT_TRUE(m2.Matches("Hello")); |
| 1265 |
EXPECT_FALSE(m2.Matches("Hi")); |
| 1266 |
|
| 1267 |
#if GTEST_INTERNAL_HAS_STRING_VIEW |
| 1268 |
Matcher<const internal::StringView&> m3 = |
| 1269 |
StrEq(internal::StringView("Hello")); |
| 1270 |
EXPECT_TRUE(m3.Matches(internal::StringView("Hello"))); |
| 1271 |
EXPECT_FALSE(m3.Matches(internal::StringView("hello"))); |
| 1272 |
EXPECT_FALSE(m3.Matches(internal::StringView())); |
| 1273 |
|
| 1274 |
Matcher<const internal::StringView&> m_empty = StrEq(""); |
| 1275 |
EXPECT_TRUE(m_empty.Matches(internal::StringView(""))); |
| 1276 |
EXPECT_TRUE(m_empty.Matches(internal::StringView())); |
| 1277 |
EXPECT_FALSE(m_empty.Matches(internal::StringView("hello"))); |
| 1278 |
#endif // GTEST_INTERNAL_HAS_STRING_VIEW |
| 1279 |
} |
| 1280 |
|
| 1281 |
TEST(StrEqTest, CanDescribeSelf) { |
| 1282 |
Matcher<std::string> m = StrEq("Hi-\'\"?\\\a\b\f\n\r\t\v\xD3"); |
| 1283 |
EXPECT_EQ("is equal to \"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"", |
| 1284 |
Describe(m)); |
| 1285 |
|
| 1286 |
std::string str("01204500800"); |
| 1287 |
str[3] = '\0'; |
| 1288 |
Matcher<std::string> m2 = StrEq(str); |
| 1289 |
EXPECT_EQ("is equal to \"012\\04500800\"", Describe(m2)); |
| 1290 |
str[0] = str[6] = str[7] = str[9] = str[10] = '\0'; |
| 1291 |
Matcher<std::string> m3 = StrEq(str); |
| 1292 |
EXPECT_EQ("is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3)); |
| 1293 |
} |
| 1294 |
|
| 1295 |
TEST(StrNeTest, MatchesUnequalString) { |
| 1296 |
Matcher<const char*> m = StrNe("Hello"); |
| 1297 |
EXPECT_TRUE(m.Matches("")); |
| 1298 |
EXPECT_TRUE(m.Matches(nullptr)); |
| 1299 |
EXPECT_FALSE(m.Matches("Hello")); |
| 1300 |
|
| 1301 |
Matcher<std::string> m2 = StrNe(std::string("Hello")); |
| 1302 |
EXPECT_TRUE(m2.Matches("hello")); |
| 1303 |
EXPECT_FALSE(m2.Matches("Hello")); |
| 1304 |
|
| 1305 |
#if GTEST_INTERNAL_HAS_STRING_VIEW |
| 1306 |
Matcher<const internal::StringView> m3 = StrNe(internal::StringView("Hello")); |
| 1307 |
EXPECT_TRUE(m3.Matches(internal::StringView(""))); |
| 1308 |
EXPECT_TRUE(m3.Matches(internal::StringView())); |
| 1309 |
EXPECT_FALSE(m3.Matches(internal::StringView("Hello"))); |
| 1310 |
#endif // GTEST_INTERNAL_HAS_STRING_VIEW |
| 1311 |
} |
| 1312 |
|
| 1313 |
TEST(StrNeTest, CanDescribeSelf) { |
| 1314 |
Matcher<const char*> m = StrNe("Hi"); |
| 1315 |
EXPECT_EQ("isn't equal to \"Hi\"", Describe(m)); |
| 1316 |
} |
| 1317 |
|
| 1318 |
TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) { |
| 1319 |
Matcher<const char*> m = StrCaseEq(std::string("Hello")); |
| 1320 |
EXPECT_TRUE(m.Matches("Hello")); |
| 1321 |
EXPECT_TRUE(m.Matches("hello")); |
| 1322 |
EXPECT_FALSE(m.Matches("Hi")); |
| 1323 |
EXPECT_FALSE(m.Matches(nullptr)); |
| 1324 |
|
| 1325 |
Matcher<const std::string&> m2 = StrCaseEq("Hello"); |
| 1326 |
EXPECT_TRUE(m2.Matches("hello")); |
| 1327 |
EXPECT_FALSE(m2.Matches("Hi")); |
| 1328 |
|
| 1329 |
#if GTEST_INTERNAL_HAS_STRING_VIEW |
| 1330 |
Matcher<const internal::StringView&> m3 = |
| 1331 |
StrCaseEq(internal::StringView("Hello")); |
| 1332 |
EXPECT_TRUE(m3.Matches(internal::StringView("Hello"))); |
| 1333 |
EXPECT_TRUE(m3.Matches(internal::StringView("hello"))); |
| 1334 |
EXPECT_FALSE(m3.Matches(internal::StringView("Hi"))); |
| 1335 |
EXPECT_FALSE(m3.Matches(internal::StringView())); |
| 1336 |
#endif // GTEST_INTERNAL_HAS_STRING_VIEW |
| 1337 |
} |
| 1338 |
|
| 1339 |
TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) { |
| 1340 |
std::string str1("oabocdooeoo"); |
| 1341 |
std::string str2("OABOCDOOEOO"); |
| 1342 |
Matcher<const std::string&> m0 = StrCaseEq(str1); |
| 1343 |
EXPECT_FALSE(m0.Matches(str2 + std::string(1, '\0'))); |
| 1344 |
|
| 1345 |
str1[3] = str2[3] = '\0'; |
| 1346 |
Matcher<const std::string&> m1 = StrCaseEq(str1); |
| 1347 |
EXPECT_TRUE(m1.Matches(str2)); |
| 1348 |
|
| 1349 |
str1[0] = str1[6] = str1[7] = str1[10] = '\0'; |
| 1350 |
str2[0] = str2[6] = str2[7] = str2[10] = '\0'; |
| 1351 |
Matcher<const std::string&> m2 = StrCaseEq(str1); |
| 1352 |
str1[9] = str2[9] = '\0'; |
| 1353 |
EXPECT_FALSE(m2.Matches(str2)); |
| 1354 |
|
| 1355 |
Matcher<const std::string&> m3 = StrCaseEq(str1); |
| 1356 |
EXPECT_TRUE(m3.Matches(str2)); |
| 1357 |
|
| 1358 |
EXPECT_FALSE(m3.Matches(str2 + "x")); |
| 1359 |
str2.append(1, '\0'); |
| 1360 |
EXPECT_FALSE(m3.Matches(str2)); |
| 1361 |
EXPECT_FALSE(m3.Matches(std::string(str2, 0, 9))); |
| 1362 |
} |
| 1363 |
|
| 1364 |
TEST(StrCaseEqTest, CanDescribeSelf) { |
| 1365 |
Matcher<std::string> m = StrCaseEq("Hi"); |
| 1366 |
EXPECT_EQ("is equal to (ignoring case) \"Hi\"", Describe(m)); |
| 1367 |
} |
| 1368 |
|
| 1369 |
TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) { |
| 1370 |
Matcher<const char*> m = StrCaseNe("Hello"); |
| 1371 |
EXPECT_TRUE(m.Matches("Hi")); |
| 1372 |
EXPECT_TRUE(m.Matches(nullptr)); |
| 1373 |
EXPECT_FALSE(m.Matches("Hello")); |
| 1374 |
EXPECT_FALSE(m.Matches("hello")); |
| 1375 |
|
| 1376 |
Matcher<std::string> m2 = StrCaseNe(std::string("Hello")); |
| 1377 |
EXPECT_TRUE(m2.Matches("")); |
| 1378 |
EXPECT_FALSE(m2.Matches("Hello")); |
| 1379 |
|
| 1380 |
#if GTEST_INTERNAL_HAS_STRING_VIEW |
| 1381 |
Matcher<const internal::StringView> m3 = |
| 1382 |
StrCaseNe(internal::StringView("Hello")); |
| 1383 |
EXPECT_TRUE(m3.Matches(internal::StringView("Hi"))); |
| 1384 |
EXPECT_TRUE(m3.Matches(internal::StringView())); |
| 1385 |
EXPECT_FALSE(m3.Matches(internal::StringView("Hello"))); |
| 1386 |
EXPECT_FALSE(m3.Matches(internal::StringView("hello"))); |
| 1387 |
#endif // GTEST_INTERNAL_HAS_STRING_VIEW |
| 1388 |
} |
| 1389 |
|
| 1390 |
TEST(StrCaseNeTest, CanDescribeSelf) { |
| 1391 |
Matcher<const char*> m = StrCaseNe("Hi"); |
| 1392 |
EXPECT_EQ("isn't equal to (ignoring case) \"Hi\"", Describe(m)); |
| 1393 |
} |
| 1394 |
|
| 1395 |
// Tests that HasSubstr() works for matching string-typed values. |
| 1396 |
TEST(HasSubstrTest, WorksForStringClasses) { |
| 1397 |
const Matcher<std::string> m1 = HasSubstr("foo"); |
| 1398 |
EXPECT_TRUE(m1.Matches(std::string("I love food."))); |
| 1399 |
EXPECT_FALSE(m1.Matches(std::string("tofo"))); |
| 1400 |
|
| 1401 |
const Matcher<const std::string&> m2 = HasSubstr("foo"); |
| 1402 |
EXPECT_TRUE(m2.Matches(std::string("I love food."))); |
| 1403 |
EXPECT_FALSE(m2.Matches(std::string("tofo"))); |
| 1404 |
|
| 1405 |
const Matcher<std::string> m_empty = HasSubstr(""); |
| 1406 |
EXPECT_TRUE(m_empty.Matches(std::string())); |
| 1407 |
EXPECT_TRUE(m_empty.Matches(std::string("not empty"))); |
| 1408 |
} |
| 1409 |
|
| 1410 |
// Tests that HasSubstr() works for matching C-string-typed values. |
| 1411 |
TEST(HasSubstrTest, WorksForCStrings) { |
| 1412 |
const Matcher<char*> m1 = HasSubstr("foo"); |
| 1413 |
EXPECT_TRUE(m1.Matches(const_cast<char*>("I love food."))); |
| 1414 |
EXPECT_FALSE(m1.Matches(const_cast<char*>("tofo"))); |
| 1415 |
EXPECT_FALSE(m1.Matches(nullptr)); |
| 1416 |
|
| 1417 |
const Matcher<const char*> m2 = HasSubstr("foo"); |
| 1418 |
EXPECT_TRUE(m2.Matches("I love food.")); |
| 1419 |
EXPECT_FALSE(m2.Matches("tofo")); |
| 1420 |
EXPECT_FALSE(m2.Matches(nullptr)); |
| 1421 |
|
| 1422 |
const Matcher<const char*> m_empty = HasSubstr(""); |
| 1423 |
EXPECT_TRUE(m_empty.Matches("not empty")); |
| 1424 |
EXPECT_TRUE(m_empty.Matches("")); |
| 1425 |
EXPECT_FALSE(m_empty.Matches(nullptr)); |
| 1426 |
} |
| 1427 |
|
| 1428 |
#if GTEST_INTERNAL_HAS_STRING_VIEW |
| 1429 |
// Tests that HasSubstr() works for matching StringView-typed values. |
| 1430 |
TEST(HasSubstrTest, WorksForStringViewClasses) { |
| 1431 |
const Matcher<internal::StringView> m1 = |
| 1432 |
HasSubstr(internal::StringView("foo")); |
| 1433 |
EXPECT_TRUE(m1.Matches(internal::StringView("I love food."))); |
| 1434 |
EXPECT_FALSE(m1.Matches(internal::StringView("tofo"))); |
| 1435 |
EXPECT_FALSE(m1.Matches(internal::StringView())); |
| 1436 |
|
| 1437 |
const Matcher<const internal::StringView&> m2 = HasSubstr("foo"); |
| 1438 |
EXPECT_TRUE(m2.Matches(internal::StringView("I love food."))); |
| 1439 |
EXPECT_FALSE(m2.Matches(internal::StringView("tofo"))); |
| 1440 |
EXPECT_FALSE(m2.Matches(internal::StringView())); |
| 1441 |
|
| 1442 |
const Matcher<const internal::StringView&> m3 = HasSubstr(""); |
| 1443 |
EXPECT_TRUE(m3.Matches(internal::StringView("foo"))); |
| 1444 |
EXPECT_TRUE(m3.Matches(internal::StringView(""))); |
| 1445 |
EXPECT_TRUE(m3.Matches(internal::StringView())); |
| 1446 |
} |
| 1447 |
#endif // GTEST_INTERNAL_HAS_STRING_VIEW |
| 1448 |
|
| 1449 |
// Tests that HasSubstr(s) describes itself properly. |
| 1450 |
TEST(HasSubstrTest, CanDescribeSelf) { |
| 1451 |
Matcher<std::string> m = HasSubstr("foo\n\""); |
| 1452 |
EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m)); |
| 1453 |
} |
| 1454 |
|
| 1455 |
TEST(KeyTest, CanDescribeSelf) { |
| 1456 |
Matcher<const pair<std::string, int>&> m = Key("foo"); |
| 1457 |
EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m)); |
| 1458 |
EXPECT_EQ("doesn't have a key that is equal to \"foo\"", DescribeNegation(m)); |
| 1459 |
} |
| 1460 |
|
| 1461 |
TEST(KeyTest, ExplainsResult) { |
| 1462 |
Matcher<pair<int, bool> > m = Key(GreaterThan(10)); |
| 1463 |
EXPECT_EQ("whose first field is a value which is 5 less than 10", |
| 1464 |
Explain(m, make_pair(5, true))); |
| 1465 |
EXPECT_EQ("whose first field is a value which is 5 more than 10", |
| 1466 |
Explain(m, make_pair(15, true))); |
| 1467 |
} |
| 1468 |
|
| 1469 |
TEST(KeyTest, MatchesCorrectly) { |
| 1470 |
pair<int, std::string> p(25, "foo"); |
| 1471 |
EXPECT_THAT(p, Key(25)); |
| 1472 |
EXPECT_THAT(p, Not(Key(42))); |
| 1473 |
EXPECT_THAT(p, Key(Ge(20))); |
| 1474 |
EXPECT_THAT(p, Not(Key(Lt(25)))); |
| 1475 |
} |
| 1476 |
|
| 1477 |
TEST(KeyTest, WorksWithMoveOnly) { |
| 1478 |
pair<std::unique_ptr<int>, std::unique_ptr<int>> p; |
| 1479 |
EXPECT_THAT(p, Key(Eq(nullptr))); |
| 1480 |
} |
| 1481 |
|
| 1482 |
template <size_t I> |
| 1483 |
struct Tag {}; |
| 1484 |
|
| 1485 |
struct PairWithGet { |
| 1486 |
int member_1; |
| 1487 |
std::string member_2; |
| 1488 |
using first_type = int; |
| 1489 |
using second_type = std::string; |
| 1490 |
|
| 1491 |
const int& GetImpl(Tag<0>) const { return member_1; } |
| 1492 |
const std::string& GetImpl(Tag<1>) const { return member_2; } |
| 1493 |
}; |
| 1494 |
template <size_t I> |
| 1495 |
auto get(const PairWithGet& value) -> decltype(value.GetImpl(Tag<I>())) { |
| 1496 |
return value.GetImpl(Tag<I>()); |
| 1497 |
} |
| 1498 |
TEST(PairTest, MatchesPairWithGetCorrectly) { |
| 1499 |
PairWithGet p{25, "foo"}; |
| 1500 |
EXPECT_THAT(p, Key(25)); |
| 1501 |
EXPECT_THAT(p, Not(Key(42))); |
| 1502 |
EXPECT_THAT(p, Key(Ge(20))); |
| 1503 |
EXPECT_THAT(p, Not(Key(Lt(25)))); |
| 1504 |
|
| 1505 |
std::vector<PairWithGet> v = {{11, "Foo"}, {29, "gMockIsBestMock"}}; |
| 1506 |
EXPECT_THAT(v, Contains(Key(29))); |
| 1507 |
} |
| 1508 |
|
| 1509 |
TEST(KeyTest, SafelyCastsInnerMatcher) { |
| 1510 |
Matcher<int> is_positive = Gt(0); |
| 1511 |
Matcher<int> is_negative = Lt(0); |
| 1512 |
pair<char, bool> p('a', true); |
| 1513 |
EXPECT_THAT(p, Key(is_positive)); |
| 1514 |
EXPECT_THAT(p, Not(Key(is_negative))); |
| 1515 |
} |
| 1516 |
|
| 1517 |
TEST(KeyTest, InsideContainsUsingMap) { |
| 1518 |
map<int, char> container; |
| 1519 |
container.insert(make_pair(1, 'a')); |
| 1520 |
container.insert(make_pair(2, 'b')); |
| 1521 |
container.insert(make_pair(4, 'c')); |
| 1522 |
EXPECT_THAT(container, Contains(Key(1))); |
| 1523 |
EXPECT_THAT(container, Not(Contains(Key(3)))); |
| 1524 |
} |
| 1525 |
|
| 1526 |
TEST(KeyTest, InsideContainsUsingMultimap) { |
| 1527 |
multimap<int, char> container; |
| 1528 |
container.insert(make_pair(1, 'a')); |
| 1529 |
container.insert(make_pair(2, 'b')); |
| 1530 |
container.insert(make_pair(4, 'c')); |
| 1531 |
|
| 1532 |
EXPECT_THAT(container, Not(Contains(Key(25)))); |
| 1533 |
container.insert(make_pair(25, 'd')); |
| 1534 |
EXPECT_THAT(container, Contains(Key(25))); |
| 1535 |
container.insert(make_pair(25, 'e')); |
| 1536 |
EXPECT_THAT(container, Contains(Key(25))); |
| 1537 |
|
| 1538 |
EXPECT_THAT(container, Contains(Key(1))); |
| 1539 |
EXPECT_THAT(container, Not(Contains(Key(3)))); |
| 1540 |
} |
| 1541 |
|
| 1542 |
TEST(PairTest, Typing) { |
| 1543 |
// Test verifies the following type conversions can be compiled. |
| 1544 |
Matcher<const pair<const char*, int>&> m1 = Pair("foo", 42); |
| 1545 |
Matcher<const pair<const char*, int> > m2 = Pair("foo", 42); |
| 1546 |
Matcher<pair<const char*, int> > m3 = Pair("foo", 42); |
| 1547 |
|
| 1548 |
Matcher<pair<int, const std::string> > m4 = Pair(25, "42"); |
| 1549 |
Matcher<pair<const std::string, int> > m5 = Pair("25", 42); |
| 1550 |
} |
| 1551 |
|
| 1552 |
TEST(PairTest, CanDescribeSelf) { |
| 1553 |
Matcher<const pair<std::string, int>&> m1 = Pair("foo", 42); |
| 1554 |
EXPECT_EQ("has a first field that is equal to \"foo\"" |
| 1555 |
", and has a second field that is equal to 42", |
| 1556 |
Describe(m1)); |
| 1557 |
EXPECT_EQ("has a first field that isn't equal to \"foo\"" |
| 1558 |
", or has a second field that isn't equal to 42", |
| 1559 |
DescribeNegation(m1)); |
| 1560 |
// Double and triple negation (1 or 2 times not and description of negation). |
| 1561 |
Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42)); |
| 1562 |
EXPECT_EQ("has a first field that isn't equal to 13" |
| 1563 |
", and has a second field that is equal to 42", |
| 1564 |
DescribeNegation(m2)); |
| 1565 |
} |
| 1566 |
|
| 1567 |
TEST(PairTest, CanExplainMatchResultTo) { |
| 1568 |
// If neither field matches, Pair() should explain about the first |
| 1569 |
// field. |
| 1570 |
const Matcher<pair<int, int> > m = Pair(GreaterThan(0), GreaterThan(0)); |
| 1571 |
EXPECT_EQ("whose first field does not match, which is 1 less than 0", |
| 1572 |
Explain(m, make_pair(-1, -2))); |
| 1573 |
|
| 1574 |
// If the first field matches but the second doesn't, Pair() should |
| 1575 |
// explain about the second field. |
| 1576 |
EXPECT_EQ("whose second field does not match, which is 2 less than 0", |
| 1577 |
Explain(m, make_pair(1, -2))); |
| 1578 |
|
| 1579 |
// If the first field doesn't match but the second does, Pair() |
| 1580 |
// should explain about the first field. |
| 1581 |
EXPECT_EQ("whose first field does not match, which is 1 less than 0", |
| 1582 |
Explain(m, make_pair(-1, 2))); |
| 1583 |
|
| 1584 |
// If both fields match, Pair() should explain about them both. |
| 1585 |
EXPECT_EQ("whose both fields match, where the first field is a value " |
| 1586 |
"which is 1 more than 0, and the second field is a value " |
| 1587 |
"which is 2 more than 0", |
| 1588 |
Explain(m, make_pair(1, 2))); |
| 1589 |
|
| 1590 |
// If only the first match has an explanation, only this explanation should |
| 1591 |
// be printed. |
| 1592 |
const Matcher<pair<int, int> > explain_first = Pair(GreaterThan(0), 0); |
| 1593 |
EXPECT_EQ("whose both fields match, where the first field is a value " |
| 1594 |
"which is 1 more than 0", |
| 1595 |
Explain(explain_first, make_pair(1, 0))); |
| 1596 |
|
| 1597 |
// If only the second match has an explanation, only this explanation should |
| 1598 |
// be printed. |
| 1599 |
const Matcher<pair<int, int> > explain_second = Pair(0, GreaterThan(0)); |
| 1600 |
EXPECT_EQ("whose both fields match, where the second field is a value " |
| 1601 |
"which is 1 more than 0", |
| 1602 |
Explain(explain_second, make_pair(0, 1))); |
| 1603 |
} |
| 1604 |
|
| 1605 |
TEST(PairTest, MatchesCorrectly) { |
| 1606 |
pair<int, std::string> p(25, "foo"); |
| 1607 |
|
| 1608 |
// Both fields match. |
| 1609 |
EXPECT_THAT(p, Pair(25, "foo")); |
| 1610 |
EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o"))); |
| 1611 |
|
| 1612 |
// 'first' doesnt' match, but 'second' matches. |
| 1613 |
EXPECT_THAT(p, Not(Pair(42, "foo"))); |
| 1614 |
EXPECT_THAT(p, Not(Pair(Lt(25), "foo"))); |
| 1615 |
|
| 1616 |
// 'first' matches, but 'second' doesn't match. |
| 1617 |
EXPECT_THAT(p, Not(Pair(25, "bar"))); |
| 1618 |
EXPECT_THAT(p, Not(Pair(25, Not("foo")))); |
| 1619 |
|
| 1620 |
// Neither field matches. |
| 1621 |
EXPECT_THAT(p, Not(Pair(13, "bar"))); |
| 1622 |
EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a")))); |
| 1623 |
} |
| 1624 |
|
| 1625 |
TEST(PairTest, WorksWithMoveOnly) { |
| 1626 |
pair<std::unique_ptr<int>, std::unique_ptr<int>> p; |
| 1627 |
p.second.reset(new int(7)); |
| 1628 |
EXPECT_THAT(p, Pair(Eq(nullptr), Ne(nullptr))); |
| 1629 |
} |
| 1630 |
|
| 1631 |
TEST(PairTest, SafelyCastsInnerMatchers) { |
| 1632 |
Matcher<int> is_positive = Gt(0); |
| 1633 |
Matcher<int> is_negative = Lt(0); |
| 1634 |
pair<char, bool> p('a', true); |
| 1635 |
EXPECT_THAT(p, Pair(is_positive, _)); |
| 1636 |
EXPECT_THAT(p, Not(Pair(is_negative, _))); |
| 1637 |
EXPECT_THAT(p, Pair(_, is_positive)); |
| 1638 |
EXPECT_THAT(p, Not(Pair(_, is_negative))); |
| 1639 |
} |
| 1640 |
|
| 1641 |
TEST(PairTest, InsideContainsUsingMap) { |
| 1642 |
map<int, char> container; |
| 1643 |
container.insert(make_pair(1, 'a')); |
| 1644 |
container.insert(make_pair(2, 'b')); |
| 1645 |
container.insert(make_pair(4, 'c')); |
| 1646 |
EXPECT_THAT(container, Contains(Pair(1, 'a'))); |
| 1647 |
EXPECT_THAT(container, Contains(Pair(1, _))); |
| 1648 |
EXPECT_THAT(container, Contains(Pair(_, 'a'))); |
| 1649 |
EXPECT_THAT(container, Not(Contains(Pair(3, _)))); |
| 1650 |
} |
| 1651 |
|
| 1652 |
TEST(FieldsAreTest, MatchesCorrectly) { |
| 1653 |
std::tuple<int, std::string, double> p(25, "foo", .5); |
| 1654 |
|
| 1655 |
// All fields match. |
| 1656 |
EXPECT_THAT(p, FieldsAre(25, "foo", .5)); |
| 1657 |
EXPECT_THAT(p, FieldsAre(Ge(20), HasSubstr("o"), DoubleEq(.5))); |
| 1658 |
|
| 1659 |
// Some don't match. |
| 1660 |
EXPECT_THAT(p, Not(FieldsAre(26, "foo", .5))); |
| 1661 |
EXPECT_THAT(p, Not(FieldsAre(25, "fo", .5))); |
| 1662 |
EXPECT_THAT(p, Not(FieldsAre(25, "foo", .6))); |
| 1663 |
} |
| 1664 |
|
| 1665 |
TEST(FieldsAreTest, CanDescribeSelf) { |
| 1666 |
Matcher<const pair<std::string, int>&> m1 = FieldsAre("foo", 42); |
| 1667 |
EXPECT_EQ( |
| 1668 |
"has field #0 that is equal to \"foo\"" |
| 1669 |
", and has field #1 that is equal to 42", |
| 1670 |
Describe(m1)); |
| 1671 |
EXPECT_EQ( |
| 1672 |
"has field #0 that isn't equal to \"foo\"" |
| 1673 |
", or has field #1 that isn't equal to 42", |
| 1674 |
DescribeNegation(m1)); |
| 1675 |
} |
| 1676 |
|
| 1677 |
TEST(FieldsAreTest, CanExplainMatchResultTo) { |
| 1678 |
// The first one that fails is the one that gives the error. |
| 1679 |
Matcher<std::tuple<int, int, int>> m = |
| 1680 |
FieldsAre(GreaterThan(0), GreaterThan(0), GreaterThan(0)); |
| 1681 |
|
| 1682 |
EXPECT_EQ("whose field #0 does not match, which is 1 less than 0", |
| 1683 |
Explain(m, std::make_tuple(-1, -2, -3))); |
| 1684 |
EXPECT_EQ("whose field #1 does not match, which is 2 less than 0", |
| 1685 |
Explain(m, std::make_tuple(1, -2, -3))); |
| 1686 |
EXPECT_EQ("whose field #2 does not match, which is 3 less than 0", |
| 1687 |
Explain(m, std::make_tuple(1, 2, -3))); |
| 1688 |
|
| 1689 |
// If they all match, we get a long explanation of success. |
| 1690 |
EXPECT_EQ( |
| 1691 |
"whose all elements match, " |
| 1692 |
"where field #0 is a value which is 1 more than 0" |
| 1693 |
", and field #1 is a value which is 2 more than 0" |
| 1694 |
", and field #2 is a value which is 3 more than 0", |
| 1695 |
Explain(m, std::make_tuple(1, 2, 3))); |
| 1696 |
|
| 1697 |
// Only print those that have an explanation. |
| 1698 |
m = FieldsAre(GreaterThan(0), 0, GreaterThan(0)); |
| 1699 |
EXPECT_EQ( |
| 1700 |
"whose all elements match, " |
| 1701 |
"where field #0 is a value which is 1 more than 0" |
| 1702 |
", and field #2 is a value which is 3 more than 0", |
| 1703 |
Explain(m, std::make_tuple(1, 0, 3))); |
| 1704 |
|
| 1705 |
// If only one has an explanation, then print that one. |
| 1706 |
m = FieldsAre(0, GreaterThan(0), 0); |
| 1707 |
EXPECT_EQ( |
| 1708 |
"whose all elements match, " |
| 1709 |
"where field #1 is a value which is 1 more than 0", |
| 1710 |
Explain(m, std::make_tuple(0, 1, 0))); |
| 1711 |
} |
| 1712 |
|
| 1713 |
#if defined(__cpp_structured_bindings) && __cpp_structured_bindings >= 201606 |
| 1714 |
TEST(FieldsAreTest, StructuredBindings) { |
| 1715 |
// testing::FieldsAre can also match aggregates and such with C++17 and up. |
| 1716 |
struct MyType { |
| 1717 |
int i; |
| 1718 |
std::string str; |
| 1719 |
}; |
| 1720 |
EXPECT_THAT((MyType{17, "foo"}), FieldsAre(Eq(17), HasSubstr("oo"))); |
| 1721 |
|
| 1722 |
// Test all the supported arities. |
| 1723 |
struct MyVarType1 { |
| 1724 |
int a; |
| 1725 |
}; |
| 1726 |
EXPECT_THAT(MyVarType1{}, FieldsAre(0)); |
| 1727 |
struct MyVarType2 { |
| 1728 |
int a, b; |
| 1729 |
}; |
| 1730 |
EXPECT_THAT(MyVarType2{}, FieldsAre(0, 0)); |
| 1731 |
struct MyVarType3 { |
| 1732 |
int a, b, c; |
| 1733 |
}; |
| 1734 |
EXPECT_THAT(MyVarType3{}, FieldsAre(0, 0, 0)); |
| 1735 |
struct MyVarType4 { |
| 1736 |
int a, b, c, d; |
| 1737 |
}; |
| 1738 |
EXPECT_THAT(MyVarType4{}, FieldsAre(0, 0, 0, 0)); |
| 1739 |
struct MyVarType5 { |
| 1740 |
int a, b, c, d, e; |
| 1741 |
}; |
| 1742 |
EXPECT_THAT(MyVarType5{}, FieldsAre(0, 0, 0, 0, 0)); |
| 1743 |
struct MyVarType6 { |
| 1744 |
int a, b, c, d, e, f; |
| 1745 |
}; |
| 1746 |
EXPECT_THAT(MyVarType6{}, FieldsAre(0, 0, 0, 0, 0, 0)); |
| 1747 |
struct MyVarType7 { |
| 1748 |
int a, b, c, d, e, f, g; |
| 1749 |
}; |
| 1750 |
EXPECT_THAT(MyVarType7{}, FieldsAre(0, 0, 0, 0, 0, 0, 0)); |
| 1751 |
struct MyVarType8 { |
| 1752 |
int a, b, c, d, e, f, g, h; |
| 1753 |
}; |
| 1754 |
EXPECT_THAT(MyVarType8{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0)); |
| 1755 |
struct MyVarType9 { |
| 1756 |
int a, b, c, d, e, f, g, h, i; |
| 1757 |
}; |
| 1758 |
EXPECT_THAT(MyVarType9{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0)); |
| 1759 |
struct MyVarType10 { |
| 1760 |
int a, b, c, d, e, f, g, h, i, j; |
| 1761 |
}; |
| 1762 |
EXPECT_THAT(MyVarType10{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)); |
| 1763 |
struct MyVarType11 { |
| 1764 |
int a, b, c, d, e, f, g, h, i, j, k; |
| 1765 |
}; |
| 1766 |
EXPECT_THAT(MyVarType11{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)); |
| 1767 |
struct MyVarType12 { |
| 1768 |
int a, b, c, d, e, f, g, h, i, j, k, l; |
| 1769 |
}; |
| 1770 |
EXPECT_THAT(MyVarType12{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)); |
| 1771 |
struct MyVarType13 { |
| 1772 |
int a, b, c, d, e, f, g, h, i, j, k, l, m; |
| 1773 |
}; |
| 1774 |
EXPECT_THAT(MyVarType13{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)); |
| 1775 |
struct MyVarType14 { |
| 1776 |
int a, b, c, d, e, f, g, h, i, j, k, l, m, n; |
| 1777 |
}; |
| 1778 |
EXPECT_THAT(MyVarType14{}, |
| 1779 |
FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)); |
| 1780 |
struct MyVarType15 { |
| 1781 |
int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o; |
| 1782 |
}; |
| 1783 |
EXPECT_THAT(MyVarType15{}, |
| 1784 |
FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)); |
| 1785 |
struct MyVarType16 { |
| 1786 |
int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p; |
| 1787 |
}; |
| 1788 |
EXPECT_THAT(MyVarType16{}, |
| 1789 |
FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)); |
| 1790 |
} |
| 1791 |
#endif |
| 1792 |
|
| 1793 |
TEST(ContainsTest, WorksWithMoveOnly) { |
| 1794 |
ContainerHelper helper; |
| 1795 |
EXPECT_CALL(helper, Call(Contains(Pointee(2)))); |
| 1796 |
helper.Call(MakeUniquePtrs({1, 2})); |
| 1797 |
} |
| 1798 |
|
| 1799 |
TEST(PairTest, UseGetInsteadOfMembers) { |
| 1800 |
PairWithGet pair{7, "ABC"}; |
| 1801 |
EXPECT_THAT(pair, Pair(7, "ABC")); |
| 1802 |
EXPECT_THAT(pair, Pair(Ge(7), HasSubstr("AB"))); |
| 1803 |
EXPECT_THAT(pair, Not(Pair(Lt(7), "ABC"))); |
| 1804 |
|
| 1805 |
std::vector<PairWithGet> v = {{11, "Foo"}, {29, "gMockIsBestMock"}}; |
| 1806 |
EXPECT_THAT(v, |
| 1807 |
ElementsAre(Pair(11, std::string("Foo")), Pair(Ge(10), Not("")))); |
| 1808 |
} |
| 1809 |
|
| 1810 |
// Tests StartsWith(s). |
| 1811 |
|
| 1812 |
TEST(StartsWithTest, MatchesStringWithGivenPrefix) { |
| 1813 |
const Matcher<const char*> m1 = StartsWith(std::string("")); |
| 1814 |
EXPECT_TRUE(m1.Matches("Hi")); |
| 1815 |
EXPECT_TRUE(m1.Matches("")); |
| 1816 |
EXPECT_FALSE(m1.Matches(nullptr)); |
| 1817 |
|
| 1818 |
const Matcher<const std::string&> m2 = StartsWith("Hi"); |
| 1819 |
EXPECT_TRUE(m2.Matches("Hi")); |
| 1820 |
EXPECT_TRUE(m2.Matches("Hi Hi!")); |
| 1821 |
EXPECT_TRUE(m2.Matches("High")); |
| 1822 |
EXPECT_FALSE(m2.Matches("H")); |
| 1823 |
EXPECT_FALSE(m2.Matches(" Hi")); |
| 1824 |
|
| 1825 |
#if GTEST_INTERNAL_HAS_STRING_VIEW |
| 1826 |
const Matcher<internal::StringView> m_empty = |
| 1827 |
StartsWith(internal::StringView("")); |
| 1828 |
EXPECT_TRUE(m_empty.Matches(internal::StringView())); |
| 1829 |
EXPECT_TRUE(m_empty.Matches(internal::StringView(""))); |
| 1830 |
EXPECT_TRUE(m_empty.Matches(internal::StringView("not empty"))); |
| 1831 |
#endif // GTEST_INTERNAL_HAS_STRING_VIEW |
| 1832 |
} |
| 1833 |
|
| 1834 |
TEST(StartsWithTest, CanDescribeSelf) { |
| 1835 |
Matcher<const std::string> m = StartsWith("Hi"); |
| 1836 |
EXPECT_EQ("starts with \"Hi\"", Describe(m)); |
| 1837 |
} |
| 1838 |
|
| 1839 |
// Tests EndsWith(s). |
| 1840 |
|
| 1841 |
TEST(EndsWithTest, MatchesStringWithGivenSuffix) { |
| 1842 |
const Matcher<const char*> m1 = EndsWith(""); |
| 1843 |
EXPECT_TRUE(m1.Matches("Hi")); |
| 1844 |
EXPECT_TRUE(m1.Matches("")); |
| 1845 |
EXPECT_FALSE(m1.Matches(nullptr)); |
| 1846 |
|
| 1847 |
const Matcher<const std::string&> m2 = EndsWith(std::string("Hi")); |
| 1848 |
EXPECT_TRUE(m2.Matches("Hi")); |
| 1849 |
EXPECT_TRUE(m2.Matches("Wow Hi Hi")); |
| 1850 |
EXPECT_TRUE(m2.Matches("Super Hi")); |
| 1851 |
EXPECT_FALSE(m2.Matches("i")); |
| 1852 |
EXPECT_FALSE(m2.Matches("Hi ")); |
| 1853 |
|
| 1854 |
#if GTEST_INTERNAL_HAS_STRING_VIEW |
| 1855 |
const Matcher<const internal::StringView&> m4 = |
| 1856 |
EndsWith(internal::StringView("")); |
| 1857 |
EXPECT_TRUE(m4.Matches("Hi")); |
| 1858 |
EXPECT_TRUE(m4.Matches("")); |
| 1859 |
EXPECT_TRUE(m4.Matches(internal::StringView())); |
| 1860 |
EXPECT_TRUE(m4.Matches(internal::StringView(""))); |
| 1861 |
#endif // GTEST_INTERNAL_HAS_STRING_VIEW |
| 1862 |
} |
| 1863 |
|
| 1864 |
TEST(EndsWithTest, CanDescribeSelf) { |
| 1865 |
Matcher<const std::string> m = EndsWith("Hi"); |
| 1866 |
EXPECT_EQ("ends with \"Hi\"", Describe(m)); |
| 1867 |
} |
| 1868 |
|
| 1869 |
// Tests WhenBase64Unescaped. |
| 1870 |
|
| 1871 |
TEST(WhenBase64UnescapedTest, MatchesUnescapedBase64Strings) { |
| 1872 |
const Matcher<const char*> m1 = WhenBase64Unescaped(EndsWith("!")); |
| 1873 |
EXPECT_FALSE(m1.Matches("invalid base64")); |
| 1874 |
EXPECT_FALSE(m1.Matches("aGVsbG8gd29ybGQ=")); // hello world |
| 1875 |
EXPECT_TRUE(m1.Matches("aGVsbG8gd29ybGQh")); // hello world! |
| 1876 |
|
| 1877 |
const Matcher<const std::string&> m2 = WhenBase64Unescaped(EndsWith("!")); |
| 1878 |
EXPECT_FALSE(m2.Matches("invalid base64")); |
| 1879 |
EXPECT_FALSE(m2.Matches("aGVsbG8gd29ybGQ=")); // hello world |
| 1880 |
EXPECT_TRUE(m2.Matches("aGVsbG8gd29ybGQh")); // hello world! |
| 1881 |
|
| 1882 |
#if GTEST_INTERNAL_HAS_STRING_VIEW |
| 1883 |
const Matcher<const internal::StringView&> m3 = |
| 1884 |
WhenBase64Unescaped(EndsWith("!")); |
| 1885 |
EXPECT_FALSE(m3.Matches("invalid base64")); |
| 1886 |
EXPECT_FALSE(m3.Matches("aGVsbG8gd29ybGQ=")); // hello world |
| 1887 |
EXPECT_TRUE(m3.Matches("aGVsbG8gd29ybGQh")); // hello world! |
| 1888 |
#endif // GTEST_INTERNAL_HAS_STRING_VIEW |
| 1889 |
} |
| 1890 |
|
| 1891 |
TEST(WhenBase64UnescapedTest, CanDescribeSelf) { |
| 1892 |
const Matcher<const char*> m = WhenBase64Unescaped(EndsWith("!")); |
| 1893 |
EXPECT_EQ("matches after Base64Unescape ends with \"!\"", Describe(m)); |
| 1894 |
} |
| 1895 |
|
| 1896 |
// Tests MatchesRegex(). |
| 1897 |
|
| 1898 |
TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) { |
| 1899 |
const Matcher<const char*> m1 = MatchesRegex("a.*z"); |
| 1900 |
EXPECT_TRUE(m1.Matches("az")); |
| 1901 |
EXPECT_TRUE(m1.Matches("abcz")); |
| 1902 |
EXPECT_FALSE(m1.Matches(nullptr)); |
| 1903 |
|
| 1904 |
const Matcher<const std::string&> m2 = MatchesRegex(new RE("a.*z")); |
| 1905 |
EXPECT_TRUE(m2.Matches("azbz")); |
| 1906 |
EXPECT_FALSE(m2.Matches("az1")); |
| 1907 |
EXPECT_FALSE(m2.Matches("1az")); |
| 1908 |
|
| 1909 |
#if GTEST_INTERNAL_HAS_STRING_VIEW |
| 1910 |
const Matcher<const internal::StringView&> m3 = MatchesRegex("a.*z"); |
| 1911 |
EXPECT_TRUE(m3.Matches(internal::StringView("az"))); |
| 1912 |
EXPECT_TRUE(m3.Matches(internal::StringView("abcz"))); |
| 1913 |
EXPECT_FALSE(m3.Matches(internal::StringView("1az"))); |
| 1914 |
EXPECT_FALSE(m3.Matches(internal::StringView())); |
| 1915 |
const Matcher<const internal::StringView&> m4 = |
| 1916 |
MatchesRegex(internal::StringView("")); |
| 1917 |
EXPECT_TRUE(m4.Matches(internal::StringView(""))); |
| 1918 |
EXPECT_TRUE(m4.Matches(internal::StringView())); |
| 1919 |
#endif // GTEST_INTERNAL_HAS_STRING_VIEW |
| 1920 |
} |
| 1921 |
|
| 1922 |
TEST(MatchesRegexTest, CanDescribeSelf) { |
| 1923 |
Matcher<const std::string> m1 = MatchesRegex(std::string("Hi.*")); |
| 1924 |
EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1)); |
| 1925 |
|
| 1926 |
Matcher<const char*> m2 = MatchesRegex(new RE("a.*")); |
| 1927 |
EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2)); |
| 1928 |
|
| 1929 |
#if GTEST_INTERNAL_HAS_STRING_VIEW |
| 1930 |
Matcher<const internal::StringView> m3 = MatchesRegex(new RE("0.*")); |
| 1931 |
EXPECT_EQ("matches regular expression \"0.*\"", Describe(m3)); |
| 1932 |
#endif // GTEST_INTERNAL_HAS_STRING_VIEW |
| 1933 |
} |
| 1934 |
|
| 1935 |
// Tests ContainsRegex(). |
| 1936 |
|
| 1937 |
TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) { |
| 1938 |
const Matcher<const char*> m1 = ContainsRegex(std::string("a.*z")); |
| 1939 |
EXPECT_TRUE(m1.Matches("az")); |
| 1940 |
EXPECT_TRUE(m1.Matches("0abcz1")); |
| 1941 |
EXPECT_FALSE(m1.Matches(nullptr)); |
| 1942 |
|
| 1943 |
const Matcher<const std::string&> m2 = ContainsRegex(new RE("a.*z")); |
| 1944 |
EXPECT_TRUE(m2.Matches("azbz")); |
| 1945 |
EXPECT_TRUE(m2.Matches("az1")); |
| 1946 |
EXPECT_FALSE(m2.Matches("1a")); |
| 1947 |
|
| 1948 |
#if GTEST_INTERNAL_HAS_STRING_VIEW |
| 1949 |
const Matcher<const internal::StringView&> m3 = |
| 1950 |
ContainsRegex(new RE("a.*z")); |
| 1951 |
EXPECT_TRUE(m3.Matches(internal::StringView("azbz"))); |
| 1952 |
EXPECT_TRUE(m3.Matches(internal::StringView("az1"))); |
| 1953 |
EXPECT_FALSE(m3.Matches(internal::StringView("1a"))); |
| 1954 |
EXPECT_FALSE(m3.Matches(internal::StringView())); |
| 1955 |
const Matcher<const internal::StringView&> m4 = |
| 1956 |
ContainsRegex(internal::StringView("")); |
| 1957 |
EXPECT_TRUE(m4.Matches(internal::StringView(""))); |
| 1958 |
EXPECT_TRUE(m4.Matches(internal::StringView())); |
| 1959 |
#endif // GTEST_INTERNAL_HAS_STRING_VIEW |
| 1960 |
} |
| 1961 |
|
| 1962 |
TEST(ContainsRegexTest, CanDescribeSelf) { |
| 1963 |
Matcher<const std::string> m1 = ContainsRegex("Hi.*"); |
| 1964 |
EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1)); |
| 1965 |
|
| 1966 |
Matcher<const char*> m2 = ContainsRegex(new RE("a.*")); |
| 1967 |
EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2)); |
| 1968 |
|
| 1969 |
#if GTEST_INTERNAL_HAS_STRING_VIEW |
| 1970 |
Matcher<const internal::StringView> m3 = ContainsRegex(new RE("0.*")); |
| 1971 |
EXPECT_EQ("contains regular expression \"0.*\"", Describe(m3)); |
| 1972 |
#endif // GTEST_INTERNAL_HAS_STRING_VIEW |
| 1973 |
} |
| 1974 |
|
| 1975 |
// Tests for wide strings. |
| 1976 |
#if GTEST_HAS_STD_WSTRING |
| 1977 |
TEST(StdWideStrEqTest, MatchesEqual) { |
| 1978 |
Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello")); |
| 1979 |
EXPECT_TRUE(m.Matches(L"Hello")); |
| 1980 |
EXPECT_FALSE(m.Matches(L"hello")); |
| 1981 |
EXPECT_FALSE(m.Matches(nullptr)); |
| 1982 |
|
| 1983 |
Matcher<const ::std::wstring&> m2 = StrEq(L"Hello"); |
| 1984 |
EXPECT_TRUE(m2.Matches(L"Hello")); |
| 1985 |
EXPECT_FALSE(m2.Matches(L"Hi")); |
| 1986 |
|
| 1987 |
Matcher<const ::std::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D"); |
| 1988 |
EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D")); |
| 1989 |
EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E")); |
| 1990 |
|
| 1991 |
::std::wstring str(L"01204500800"); |
| 1992 |
str[3] = L'\0'; |
| 1993 |
Matcher<const ::std::wstring&> m4 = StrEq(str); |
| 1994 |
EXPECT_TRUE(m4.Matches(str)); |
| 1995 |
str[0] = str[6] = str[7] = str[9] = str[10] = L'\0'; |
| 1996 |
Matcher<const ::std::wstring&> m5 = StrEq(str); |
| 1997 |
EXPECT_TRUE(m5.Matches(str)); |
| 1998 |
} |
| 1999 |
|
| 2000 |
TEST(StdWideStrEqTest, CanDescribeSelf) { |
| 2001 |
Matcher< ::std::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v"); |
| 2002 |
EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"", |
| 2003 |
Describe(m)); |
| 2004 |
|
| 2005 |
Matcher< ::std::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D"); |
| 2006 |
EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"", |
| 2007 |
Describe(m2)); |
| 2008 |
|
| 2009 |
::std::wstring str(L"01204500800"); |
| 2010 |
str[3] = L'\0'; |
| 2011 |
Matcher<const ::std::wstring&> m4 = StrEq(str); |
| 2012 |
EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4)); |
| 2013 |
str[0] = str[6] = str[7] = str[9] = str[10] = L'\0'; |
| 2014 |
Matcher<const ::std::wstring&> m5 = StrEq(str); |
| 2015 |
EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5)); |
| 2016 |
} |
| 2017 |
|
| 2018 |
TEST(StdWideStrNeTest, MatchesUnequalString) { |
| 2019 |
Matcher<const wchar_t*> m = StrNe(L"Hello"); |
| 2020 |
EXPECT_TRUE(m.Matches(L"")); |
| 2021 |
EXPECT_TRUE(m.Matches(nullptr)); |
| 2022 |
EXPECT_FALSE(m.Matches(L"Hello")); |
| 2023 |
|
| 2024 |
Matcher< ::std::wstring> m2 = StrNe(::std::wstring(L"Hello")); |
| 2025 |
EXPECT_TRUE(m2.Matches(L"hello")); |
| 2026 |
EXPECT_FALSE(m2.Matches(L"Hello")); |
| 2027 |
} |
| 2028 |
|
| 2029 |
TEST(StdWideStrNeTest, CanDescribeSelf) { |
| 2030 |
Matcher<const wchar_t*> m = StrNe(L"Hi"); |
| 2031 |
EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m)); |
| 2032 |
} |
| 2033 |
|
| 2034 |
TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) { |
| 2035 |
Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello")); |
| 2036 |
EXPECT_TRUE(m.Matches(L"Hello")); |
| 2037 |
EXPECT_TRUE(m.Matches(L"hello")); |
| 2038 |
EXPECT_FALSE(m.Matches(L"Hi")); |
| 2039 |
EXPECT_FALSE(m.Matches(nullptr)); |
| 2040 |
|
| 2041 |
Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello"); |
| 2042 |
EXPECT_TRUE(m2.Matches(L"hello")); |
| 2043 |
EXPECT_FALSE(m2.Matches(L"Hi")); |
| 2044 |
} |
| 2045 |
|
| 2046 |
TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) { |
| 2047 |
::std::wstring str1(L"oabocdooeoo"); |
| 2048 |
::std::wstring str2(L"OABOCDOOEOO"); |
| 2049 |
Matcher<const ::std::wstring&> m0 = StrCaseEq(str1); |
| 2050 |
EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L'\0'))); |
| 2051 |
|
| 2052 |
str1[3] = str2[3] = L'\0'; |
| 2053 |
Matcher<const ::std::wstring&> m1 = StrCaseEq(str1); |
| 2054 |
EXPECT_TRUE(m1.Matches(str2)); |
| 2055 |
|
| 2056 |
str1[0] = str1[6] = str1[7] = str1[10] = L'\0'; |
| 2057 |
str2[0] = str2[6] = str2[7] = str2[10] = L'\0'; |
| 2058 |
Matcher<const ::std::wstring&> m2 = StrCaseEq(str1); |
| 2059 |
str1[9] = str2[9] = L'\0'; |
| 2060 |
EXPECT_FALSE(m2.Matches(str2)); |
| 2061 |
|
| 2062 |
Matcher<const ::std::wstring&> m3 = StrCaseEq(str1); |
| 2063 |
EXPECT_TRUE(m3.Matches(str2)); |
| 2064 |
|
| 2065 |
EXPECT_FALSE(m3.Matches(str2 + L"x")); |
| 2066 |
str2.append(1, L'\0'); |
| 2067 |
EXPECT_FALSE(m3.Matches(str2)); |
| 2068 |
EXPECT_FALSE(m3.Matches(::std::wstring(str2, 0, 9))); |
| 2069 |
} |
| 2070 |
|
| 2071 |
TEST(StdWideStrCaseEqTest, CanDescribeSelf) { |
| 2072 |
Matcher< ::std::wstring> m = StrCaseEq(L"Hi"); |
| 2073 |
EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m)); |
| 2074 |
} |
| 2075 |
|
| 2076 |
TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) { |
| 2077 |
Matcher<const wchar_t*> m = StrCaseNe(L"Hello"); |
| 2078 |
EXPECT_TRUE(m.Matches(L"Hi")); |
| 2079 |
EXPECT_TRUE(m.Matches(nullptr)); |
| 2080 |
EXPECT_FALSE(m.Matches(L"Hello")); |
| 2081 |
EXPECT_FALSE(m.Matches(L"hello")); |
| 2082 |
|
| 2083 |
Matcher< ::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello")); |
| 2084 |
EXPECT_TRUE(m2.Matches(L"")); |
| 2085 |
EXPECT_FALSE(m2.Matches(L"Hello")); |
| 2086 |
} |
| 2087 |
|
| 2088 |
TEST(StdWideStrCaseNeTest, CanDescribeSelf) { |
| 2089 |
Matcher<const wchar_t*> m = StrCaseNe(L"Hi"); |
| 2090 |
EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m)); |
| 2091 |
} |
| 2092 |
|
| 2093 |
// Tests that HasSubstr() works for matching wstring-typed values. |
| 2094 |
TEST(StdWideHasSubstrTest, WorksForStringClasses) { |
| 2095 |
const Matcher< ::std::wstring> m1 = HasSubstr(L"foo"); |
| 2096 |
EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food."))); |
| 2097 |
EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo"))); |
| 2098 |
|
| 2099 |
const Matcher<const ::std::wstring&> m2 = HasSubstr(L"foo"); |
| 2100 |
EXPECT_TRUE(m2.Matches(::std::wstring(L"I love food."))); |
| 2101 |
EXPECT_FALSE(m2.Matches(::std::wstring(L"tofo"))); |
| 2102 |
} |
| 2103 |
|
| 2104 |
// Tests that HasSubstr() works for matching C-wide-string-typed values. |
| 2105 |
TEST(StdWideHasSubstrTest, WorksForCStrings) { |
| 2106 |
const Matcher<wchar_t*> m1 = HasSubstr(L"foo"); |
| 2107 |
EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food."))); |
| 2108 |
EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo"))); |
| 2109 |
EXPECT_FALSE(m1.Matches(nullptr)); |
| 2110 |
|
| 2111 |
const Matcher<const wchar_t*> m2 = HasSubstr(L"foo"); |
| 2112 |
EXPECT_TRUE(m2.Matches(L"I love food.")); |
| 2113 |
EXPECT_FALSE(m2.Matches(L"tofo")); |
| 2114 |
EXPECT_FALSE(m2.Matches(nullptr)); |
| 2115 |
} |
| 2116 |
|
| 2117 |
// Tests that HasSubstr(s) describes itself properly. |
| 2118 |
TEST(StdWideHasSubstrTest, CanDescribeSelf) { |
| 2119 |
Matcher< ::std::wstring> m = HasSubstr(L"foo\n\""); |
| 2120 |
EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m)); |
| 2121 |
} |
| 2122 |
|
| 2123 |
// Tests StartsWith(s). |
| 2124 |
|
| 2125 |
TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) { |
| 2126 |
const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(L"")); |
| 2127 |
EXPECT_TRUE(m1.Matches(L"Hi")); |
| 2128 |
EXPECT_TRUE(m1.Matches(L"")); |
| 2129 |
EXPECT_FALSE(m1.Matches(nullptr)); |
| 2130 |
|
| 2131 |
const Matcher<const ::std::wstring&> m2 = StartsWith(L"Hi"); |
| 2132 |
EXPECT_TRUE(m2.Matches(L"Hi")); |
| 2133 |
EXPECT_TRUE(m2.Matches(L"Hi Hi!")); |
| 2134 |
EXPECT_TRUE(m2.Matches(L"High")); |
| 2135 |
EXPECT_FALSE(m2.Matches(L"H")); |
| 2136 |
EXPECT_FALSE(m2.Matches(L" Hi")); |
| 2137 |
} |
| 2138 |
|
| 2139 |
TEST(StdWideStartsWithTest, CanDescribeSelf) { |
| 2140 |
Matcher<const ::std::wstring> m = StartsWith(L"Hi"); |
| 2141 |
EXPECT_EQ("starts with L\"Hi\"", Describe(m)); |
| 2142 |
} |
| 2143 |
|
| 2144 |
// Tests EndsWith(s). |
| 2145 |
|
| 2146 |
TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) { |
| 2147 |
const Matcher<const wchar_t*> m1 = EndsWith(L""); |
| 2148 |
EXPECT_TRUE(m1.Matches(L"Hi")); |
| 2149 |
EXPECT_TRUE(m1.Matches(L"")); |
| 2150 |
EXPECT_FALSE(m1.Matches(nullptr)); |
| 2151 |
|
| 2152 |
const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L"Hi")); |
| 2153 |
EXPECT_TRUE(m2.Matches(L"Hi")); |
| 2154 |
EXPECT_TRUE(m2.Matches(L"Wow Hi Hi")); |
| 2155 |
EXPECT_TRUE(m2.Matches(L"Super Hi")); |
| 2156 |
EXPECT_FALSE(m2.Matches(L"i")); |
| 2157 |
EXPECT_FALSE(m2.Matches(L"Hi ")); |
| 2158 |
} |
| 2159 |
|
| 2160 |
TEST(StdWideEndsWithTest, CanDescribeSelf) { |
| 2161 |
Matcher<const ::std::wstring> m = EndsWith(L"Hi"); |
| 2162 |
EXPECT_EQ("ends with L\"Hi\"", Describe(m)); |
| 2163 |
} |
| 2164 |
|
| 2165 |
#endif // GTEST_HAS_STD_WSTRING |
| 2166 |
|
| 2167 |
typedef ::std::tuple<long, int> Tuple2; // NOLINT |
| 2168 |
|
| 2169 |
// Tests that Eq() matches a 2-tuple where the first field == the |
| 2170 |
// second field. |
| 2171 |
TEST(Eq2Test, MatchesEqualArguments) { |
| 2172 |
Matcher<const Tuple2&> m = Eq(); |
| 2173 |
EXPECT_TRUE(m.Matches(Tuple2(5L, 5))); |
| 2174 |
EXPECT_FALSE(m.Matches(Tuple2(5L, 6))); |
| 2175 |
} |
| 2176 |
|
| 2177 |
// Tests that Eq() describes itself properly. |
| 2178 |
TEST(Eq2Test, CanDescribeSelf) { |
| 2179 |
Matcher<const Tuple2&> m = Eq(); |
| 2180 |
EXPECT_EQ("are an equal pair", Describe(m)); |
| 2181 |
} |
| 2182 |
|
| 2183 |
// Tests that Ge() matches a 2-tuple where the first field >= the |
| 2184 |
// second field. |
| 2185 |
TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) { |
| 2186 |
Matcher<const Tuple2&> m = Ge(); |
| 2187 |
EXPECT_TRUE(m.Matches(Tuple2(5L, 4))); |
| 2188 |
EXPECT_TRUE(m.Matches(Tuple2(5L, 5))); |
| 2189 |
EXPECT_FALSE(m.Matches(Tuple2(5L, 6))); |
| 2190 |
} |
| 2191 |
|
| 2192 |
// Tests that Ge() describes itself properly. |
| 2193 |
TEST(Ge2Test, CanDescribeSelf) { |
| 2194 |
Matcher<const Tuple2&> m = Ge(); |
| 2195 |
EXPECT_EQ("are a pair where the first >= the second", Describe(m)); |
| 2196 |
} |
| 2197 |
|
| 2198 |
// Tests that Gt() matches a 2-tuple where the first field > the |
| 2199 |
// second field. |
| 2200 |
TEST(Gt2Test, MatchesGreaterThanArguments) { |
| 2201 |
Matcher<const Tuple2&> m = Gt(); |
| 2202 |
EXPECT_TRUE(m.Matches(Tuple2(5L, 4))); |
| 2203 |
EXPECT_FALSE(m.Matches(Tuple2(5L, 5))); |
| 2204 |
EXPECT_FALSE(m.Matches(Tuple2(5L, 6))); |
| 2205 |
} |
| 2206 |
|
| 2207 |
// Tests that Gt() describes itself properly. |
| 2208 |
TEST(Gt2Test, CanDescribeSelf) { |
| 2209 |
Matcher<const Tuple2&> m = Gt(); |
| 2210 |
EXPECT_EQ("are a pair where the first > the second", Describe(m)); |
| 2211 |
} |
| 2212 |
|
| 2213 |
// Tests that Le() matches a 2-tuple where the first field <= the |
| 2214 |
// second field. |
| 2215 |
TEST(Le2Test, MatchesLessThanOrEqualArguments) { |
| 2216 |
Matcher<const Tuple2&> m = Le(); |
| 2217 |
EXPECT_TRUE(m.Matches(Tuple2(5L, 6))); |
| 2218 |
EXPECT_TRUE(m.Matches(Tuple2(5L, 5))); |
| 2219 |
EXPECT_FALSE(m.Matches(Tuple2(5L, 4))); |
| 2220 |
} |
| 2221 |
|
| 2222 |
// Tests that Le() describes itself properly. |
| 2223 |
TEST(Le2Test, CanDescribeSelf) { |
| 2224 |
Matcher<const Tuple2&> m = Le(); |
| 2225 |
EXPECT_EQ("are a pair where the first <= the second", Describe(m)); |
| 2226 |
} |
| 2227 |
|
| 2228 |
// Tests that Lt() matches a 2-tuple where the first field < the |
| 2229 |
// second field. |
| 2230 |
TEST(Lt2Test, MatchesLessThanArguments) { |
| 2231 |
Matcher<const Tuple2&> m = Lt(); |
| 2232 |
EXPECT_TRUE(m.Matches(Tuple2(5L, 6))); |
| 2233 |
EXPECT_FALSE(m.Matches(Tuple2(5L, 5))); |
| 2234 |
EXPECT_FALSE(m.Matches(Tuple2(5L, 4))); |
| 2235 |
} |
| 2236 |
|
| 2237 |
// Tests that Lt() describes itself properly. |
| 2238 |
TEST(Lt2Test, CanDescribeSelf) { |
| 2239 |
Matcher<const Tuple2&> m = Lt(); |
| 2240 |
EXPECT_EQ("are a pair where the first < the second", Describe(m)); |
| 2241 |
} |
| 2242 |
|
| 2243 |
// Tests that Ne() matches a 2-tuple where the first field != the |
| 2244 |
// second field. |
| 2245 |
TEST(Ne2Test, MatchesUnequalArguments) { |
| 2246 |
Matcher<const Tuple2&> m = Ne(); |
| 2247 |
EXPECT_TRUE(m.Matches(Tuple2(5L, 6))); |
| 2248 |
EXPECT_TRUE(m.Matches(Tuple2(5L, 4))); |
| 2249 |
EXPECT_FALSE(m.Matches(Tuple2(5L, 5))); |
| 2250 |
} |
| 2251 |
|
| 2252 |
// Tests that Ne() describes itself properly. |
| 2253 |
TEST(Ne2Test, CanDescribeSelf) { |
| 2254 |
Matcher<const Tuple2&> m = Ne(); |
| 2255 |
EXPECT_EQ("are an unequal pair", Describe(m)); |
| 2256 |
} |
| 2257 |
|
| 2258 |
TEST(PairMatchBaseTest, WorksWithMoveOnly) { |
| 2259 |
using Pointers = std::tuple<std::unique_ptr<int>, std::unique_ptr<int>>; |
| 2260 |
Matcher<Pointers> matcher = Eq(); |
| 2261 |
Pointers pointers; |
| 2262 |
// Tested values don't matter; the point is that matcher does not copy the |
| 2263 |
// matched values. |
| 2264 |
EXPECT_TRUE(matcher.Matches(pointers)); |
| 2265 |
} |
| 2266 |
|
| 2267 |
// Tests that IsNan() matches a NaN, with float. |
| 2268 |
TEST(IsNan, FloatMatchesNan) { |
| 2269 |
float quiet_nan = std::numeric_limits<float>::quiet_NaN(); |
| 2270 |
float other_nan = std::nanf("1"); |
| 2271 |
float real_value = 1.0f; |
| 2272 |
|
| 2273 |
Matcher<float> m = IsNan(); |
| 2274 |
EXPECT_TRUE(m.Matches(quiet_nan)); |
| 2275 |
EXPECT_TRUE(m.Matches(other_nan)); |
| 2276 |
EXPECT_FALSE(m.Matches(real_value)); |
| 2277 |
|
| 2278 |
Matcher<float&> m_ref = IsNan(); |
| 2279 |
EXPECT_TRUE(m_ref.Matches(quiet_nan)); |
| 2280 |
EXPECT_TRUE(m_ref.Matches(other_nan)); |
| 2281 |
EXPECT_FALSE(m_ref.Matches(real_value)); |
| 2282 |
|
| 2283 |
Matcher<const float&> m_cref = IsNan(); |
| 2284 |
EXPECT_TRUE(m_cref.Matches(quiet_nan)); |
| 2285 |
EXPECT_TRUE(m_cref.Matches(other_nan)); |
| 2286 |
EXPECT_FALSE(m_cref.Matches(real_value)); |
| 2287 |
} |
| 2288 |
|
| 2289 |
// Tests that IsNan() matches a NaN, with double. |
| 2290 |
TEST(IsNan, DoubleMatchesNan) { |
| 2291 |
double quiet_nan = std::numeric_limits<double>::quiet_NaN(); |
| 2292 |
double other_nan = std::nan("1"); |
| 2293 |
double real_value = 1.0; |
| 2294 |
|
| 2295 |
Matcher<double> m = IsNan(); |
| 2296 |
EXPECT_TRUE(m.Matches(quiet_nan)); |
| 2297 |
EXPECT_TRUE(m.Matches(other_nan)); |
| 2298 |
EXPECT_FALSE(m.Matches(real_value)); |
| 2299 |
|
| 2300 |
Matcher<double&> m_ref = IsNan(); |
| 2301 |
EXPECT_TRUE(m_ref.Matches(quiet_nan)); |
| 2302 |
EXPECT_TRUE(m_ref.Matches(other_nan)); |
| 2303 |
EXPECT_FALSE(m_ref.Matches(real_value)); |
| 2304 |
|
| 2305 |
Matcher<const double&> m_cref = IsNan(); |
| 2306 |
EXPECT_TRUE(m_cref.Matches(quiet_nan)); |
| 2307 |
EXPECT_TRUE(m_cref.Matches(other_nan)); |
| 2308 |
EXPECT_FALSE(m_cref.Matches(real_value)); |
| 2309 |
} |
| 2310 |
|
| 2311 |
// Tests that IsNan() matches a NaN, with long double. |
| 2312 |
TEST(IsNan, LongDoubleMatchesNan) { |
| 2313 |
long double quiet_nan = std::numeric_limits<long double>::quiet_NaN(); |
| 2314 |
long double other_nan = std::nan("1"); |
| 2315 |
long double real_value = 1.0; |
| 2316 |
|
| 2317 |
Matcher<long double> m = IsNan(); |
| 2318 |
EXPECT_TRUE(m.Matches(quiet_nan)); |
| 2319 |
EXPECT_TRUE(m.Matches(other_nan)); |
| 2320 |
EXPECT_FALSE(m.Matches(real_value)); |
| 2321 |
|
| 2322 |
Matcher<long double&> m_ref = IsNan(); |
| 2323 |
EXPECT_TRUE(m_ref.Matches(quiet_nan)); |
| 2324 |
EXPECT_TRUE(m_ref.Matches(other_nan)); |
| 2325 |
EXPECT_FALSE(m_ref.Matches(real_value)); |
| 2326 |
|
| 2327 |
Matcher<const long double&> m_cref = IsNan(); |
| 2328 |
EXPECT_TRUE(m_cref.Matches(quiet_nan)); |
| 2329 |
EXPECT_TRUE(m_cref.Matches(other_nan)); |
| 2330 |
EXPECT_FALSE(m_cref.Matches(real_value)); |
| 2331 |
} |
| 2332 |
|
| 2333 |
// Tests that IsNan() works with Not. |
| 2334 |
TEST(IsNan, NotMatchesNan) { |
| 2335 |
Matcher<float> mf = Not(IsNan()); |
| 2336 |
EXPECT_FALSE(mf.Matches(std::numeric_limits<float>::quiet_NaN())); |
| 2337 |
EXPECT_FALSE(mf.Matches(std::nanf("1"))); |
| 2338 |
EXPECT_TRUE(mf.Matches(1.0)); |
| 2339 |
|
| 2340 |
Matcher<double> md = Not(IsNan()); |
| 2341 |
EXPECT_FALSE(md.Matches(std::numeric_limits<double>::quiet_NaN())); |
| 2342 |
EXPECT_FALSE(md.Matches(std::nan("1"))); |
| 2343 |
EXPECT_TRUE(md.Matches(1.0)); |
| 2344 |
|
| 2345 |
Matcher<long double> mld = Not(IsNan()); |
| 2346 |
EXPECT_FALSE(mld.Matches(std::numeric_limits<long double>::quiet_NaN())); |
| 2347 |
EXPECT_FALSE(mld.Matches(std::nanl("1"))); |
| 2348 |
EXPECT_TRUE(mld.Matches(1.0)); |
| 2349 |
} |
| 2350 |
|
| 2351 |
// Tests that IsNan() can describe itself. |
| 2352 |
TEST(IsNan, CanDescribeSelf) { |
| 2353 |
Matcher<float> mf = IsNan(); |
| 2354 |
EXPECT_EQ("is NaN", Describe(mf)); |
| 2355 |
|
| 2356 |
Matcher<double> md = IsNan(); |
| 2357 |
EXPECT_EQ("is NaN", Describe(md)); |
| 2358 |
|
| 2359 |
Matcher<long double> mld = IsNan(); |
| 2360 |
EXPECT_EQ("is NaN", Describe(mld)); |
| 2361 |
} |
| 2362 |
|
| 2363 |
// Tests that IsNan() can describe itself with Not. |
| 2364 |
TEST(IsNan, CanDescribeSelfWithNot) { |
| 2365 |
Matcher<float> mf = Not(IsNan()); |
| 2366 |
EXPECT_EQ("isn't NaN", Describe(mf)); |
| 2367 |
|
| 2368 |
Matcher<double> md = Not(IsNan()); |
| 2369 |
EXPECT_EQ("isn't NaN", Describe(md)); |
| 2370 |
|
| 2371 |
Matcher<long double> mld = Not(IsNan()); |
| 2372 |
EXPECT_EQ("isn't NaN", Describe(mld)); |
| 2373 |
} |
| 2374 |
|
| 2375 |
// Tests that FloatEq() matches a 2-tuple where |
| 2376 |
// FloatEq(first field) matches the second field. |
| 2377 |
TEST(FloatEq2Test, MatchesEqualArguments) { |
| 2378 |
typedef ::std::tuple<float, float> Tpl; |
| 2379 |
Matcher<const Tpl&> m = FloatEq(); |
| 2380 |
EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f))); |
| 2381 |
EXPECT_TRUE(m.Matches(Tpl(0.3f, 0.1f + 0.1f + 0.1f))); |
| 2382 |
EXPECT_FALSE(m.Matches(Tpl(1.1f, 1.0f))); |
| 2383 |
} |
| 2384 |
|
| 2385 |
// Tests that FloatEq() describes itself properly. |
| 2386 |
TEST(FloatEq2Test, CanDescribeSelf) { |
| 2387 |
Matcher<const ::std::tuple<float, float>&> m = FloatEq(); |
| 2388 |
EXPECT_EQ("are an almost-equal pair", Describe(m)); |
| 2389 |
} |
| 2390 |
|
| 2391 |
// Tests that NanSensitiveFloatEq() matches a 2-tuple where |
| 2392 |
// NanSensitiveFloatEq(first field) matches the second field. |
| 2393 |
TEST(NanSensitiveFloatEqTest, MatchesEqualArgumentsWithNaN) { |
| 2394 |
typedef ::std::tuple<float, float> Tpl; |
| 2395 |
Matcher<const Tpl&> m = NanSensitiveFloatEq(); |
| 2396 |
EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f))); |
| 2397 |
EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(), |
| 2398 |
std::numeric_limits<float>::quiet_NaN()))); |
| 2399 |
EXPECT_FALSE(m.Matches(Tpl(1.1f, 1.0f))); |
| 2400 |
EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<float>::quiet_NaN()))); |
| 2401 |
EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(), 1.0f))); |
| 2402 |
} |
| 2403 |
|
| 2404 |
// Tests that NanSensitiveFloatEq() describes itself properly. |
| 2405 |
TEST(NanSensitiveFloatEqTest, CanDescribeSelfWithNaNs) { |
| 2406 |
Matcher<const ::std::tuple<float, float>&> m = NanSensitiveFloatEq(); |
| 2407 |
EXPECT_EQ("are an almost-equal pair", Describe(m)); |
| 2408 |
} |
| 2409 |
|
| 2410 |
// Tests that DoubleEq() matches a 2-tuple where |
| 2411 |
// DoubleEq(first field) matches the second field. |
| 2412 |
TEST(DoubleEq2Test, MatchesEqualArguments) { |
| 2413 |
typedef ::std::tuple<double, double> Tpl; |
| 2414 |
Matcher<const Tpl&> m = DoubleEq(); |
| 2415 |
EXPECT_TRUE(m.Matches(Tpl(1.0, 1.0))); |
| 2416 |
EXPECT_TRUE(m.Matches(Tpl(0.3, 0.1 + 0.1 + 0.1))); |
| 2417 |
EXPECT_FALSE(m.Matches(Tpl(1.1, 1.0))); |
| 2418 |
} |
| 2419 |
|
| 2420 |
// Tests that DoubleEq() describes itself properly. |
| 2421 |
TEST(DoubleEq2Test, CanDescribeSelf) { |
| 2422 |
Matcher<const ::std::tuple<double, double>&> m = DoubleEq(); |
| 2423 |
EXPECT_EQ("are an almost-equal pair", Describe(m)); |
| 2424 |
} |
| 2425 |
|
| 2426 |
// Tests that NanSensitiveDoubleEq() matches a 2-tuple where |
| 2427 |
// NanSensitiveDoubleEq(first field) matches the second field. |
| 2428 |
TEST(NanSensitiveDoubleEqTest, MatchesEqualArgumentsWithNaN) { |
| 2429 |
typedef ::std::tuple<double, double> Tpl; |
| 2430 |
Matcher<const Tpl&> m = NanSensitiveDoubleEq(); |
| 2431 |
EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f))); |
| 2432 |
EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(), |
| 2433 |
std::numeric_limits<double>::quiet_NaN()))); |
| 2434 |
EXPECT_FALSE(m.Matches(Tpl(1.1f, 1.0f))); |
| 2435 |
EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<double>::quiet_NaN()))); |
| 2436 |
EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(), 1.0f))); |
| 2437 |
} |
| 2438 |
|
| 2439 |
// Tests that DoubleEq() describes itself properly. |
| 2440 |
TEST(NanSensitiveDoubleEqTest, CanDescribeSelfWithNaNs) { |
| 2441 |
Matcher<const ::std::tuple<double, double>&> m = NanSensitiveDoubleEq(); |
| 2442 |
EXPECT_EQ("are an almost-equal pair", Describe(m)); |
| 2443 |
} |
| 2444 |
|
| 2445 |
// Tests that FloatEq() matches a 2-tuple where |
| 2446 |
// FloatNear(first field, max_abs_error) matches the second field. |
| 2447 |
TEST(FloatNear2Test, MatchesEqualArguments) { |
| 2448 |
typedef ::std::tuple<float, float> Tpl; |
| 2449 |
Matcher<const Tpl&> m = FloatNear(0.5f); |
| 2450 |
EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f))); |
| 2451 |
EXPECT_TRUE(m.Matches(Tpl(1.3f, 1.0f))); |
| 2452 |
EXPECT_FALSE(m.Matches(Tpl(1.8f, 1.0f))); |
| 2453 |
} |
| 2454 |
|
| 2455 |
// Tests that FloatNear() describes itself properly. |
| 2456 |
TEST(FloatNear2Test, CanDescribeSelf) { |
| 2457 |
Matcher<const ::std::tuple<float, float>&> m = FloatNear(0.5f); |
| 2458 |
EXPECT_EQ("are an almost-equal pair", Describe(m)); |
| 2459 |
} |
| 2460 |
|
| 2461 |
// Tests that NanSensitiveFloatNear() matches a 2-tuple where |
| 2462 |
// NanSensitiveFloatNear(first field) matches the second field. |
| 2463 |
TEST(NanSensitiveFloatNearTest, MatchesNearbyArgumentsWithNaN) { |
| 2464 |
typedef ::std::tuple<float, float> Tpl; |
| 2465 |
Matcher<const Tpl&> m = NanSensitiveFloatNear(0.5f); |
| 2466 |
EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f))); |
| 2467 |
EXPECT_TRUE(m.Matches(Tpl(1.1f, 1.0f))); |
| 2468 |
EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(), |
| 2469 |
std::numeric_limits<float>::quiet_NaN()))); |
| 2470 |
EXPECT_FALSE(m.Matches(Tpl(1.6f, 1.0f))); |
| 2471 |
EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<float>::quiet_NaN()))); |
| 2472 |
EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(), 1.0f))); |
| 2473 |
} |
| 2474 |
|
| 2475 |
// Tests that NanSensitiveFloatNear() describes itself properly. |
| 2476 |
TEST(NanSensitiveFloatNearTest, CanDescribeSelfWithNaNs) { |
| 2477 |
Matcher<const ::std::tuple<float, float>&> m = NanSensitiveFloatNear(0.5f); |
| 2478 |
EXPECT_EQ("are an almost-equal pair", Describe(m)); |
| 2479 |
} |
| 2480 |
|
| 2481 |
// Tests that FloatEq() matches a 2-tuple where |
| 2482 |
// DoubleNear(first field, max_abs_error) matches the second field. |
| 2483 |
TEST(DoubleNear2Test, MatchesEqualArguments) { |
| 2484 |
typedef ::std::tuple<double, double> Tpl; |
| 2485 |
Matcher<const Tpl&> m = DoubleNear(0.5); |
| 2486 |
EXPECT_TRUE(m.Matches(Tpl(1.0, 1.0))); |
| 2487 |
EXPECT_TRUE(m.Matches(Tpl(1.3, 1.0))); |
| 2488 |
EXPECT_FALSE(m.Matches(Tpl(1.8, 1.0))); |
| 2489 |
} |
| 2490 |
|
| 2491 |
// Tests that DoubleNear() describes itself properly. |
| 2492 |
TEST(DoubleNear2Test, CanDescribeSelf) { |
| 2493 |
Matcher<const ::std::tuple<double, double>&> m = DoubleNear(0.5); |
| 2494 |
EXPECT_EQ("are an almost-equal pair", Describe(m)); |
| 2495 |
} |
| 2496 |
|
| 2497 |
// Tests that NanSensitiveDoubleNear() matches a 2-tuple where |
| 2498 |
// NanSensitiveDoubleNear(first field) matches the second field. |
| 2499 |
TEST(NanSensitiveDoubleNearTest, MatchesNearbyArgumentsWithNaN) { |
| 2500 |
typedef ::std::tuple<double, double> Tpl; |
| 2501 |
Matcher<const Tpl&> m = NanSensitiveDoubleNear(0.5f); |
| 2502 |
EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f))); |
| 2503 |
EXPECT_TRUE(m.Matches(Tpl(1.1f, 1.0f))); |
| 2504 |
EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(), |
| 2505 |
std::numeric_limits<double>::quiet_NaN()))); |
| 2506 |
EXPECT_FALSE(m.Matches(Tpl(1.6f, 1.0f))); |
| 2507 |
EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<double>::quiet_NaN()))); |
| 2508 |
EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(), 1.0f))); |
| 2509 |
} |
| 2510 |
|
| 2511 |
// Tests that NanSensitiveDoubleNear() describes itself properly. |
| 2512 |
TEST(NanSensitiveDoubleNearTest, CanDescribeSelfWithNaNs) { |
| 2513 |
Matcher<const ::std::tuple<double, double>&> m = NanSensitiveDoubleNear(0.5f); |
| 2514 |
EXPECT_EQ("are an almost-equal pair", Describe(m)); |
| 2515 |
} |
| 2516 |
|
| 2517 |
// Tests that Not(m) matches any value that doesn't match m. |
| 2518 |
TEST(NotTest, NegatesMatcher) { |
| 2519 |
Matcher<int> m; |
| 2520 |
m = Not(Eq(2)); |
| 2521 |
EXPECT_TRUE(m.Matches(3)); |
| 2522 |
EXPECT_FALSE(m.Matches(2)); |
| 2523 |
} |
| 2524 |
|
| 2525 |
// Tests that Not(m) describes itself properly. |
| 2526 |
TEST(NotTest, CanDescribeSelf) { |
| 2527 |
Matcher<int> m = Not(Eq(5)); |
| 2528 |
EXPECT_EQ("isn't equal to 5", Describe(m)); |
| 2529 |
} |
| 2530 |
|
| 2531 |
// Tests that monomorphic matchers are safely cast by the Not matcher. |
| 2532 |
TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) { |
| 2533 |
// greater_than_5 is a monomorphic matcher. |
| 2534 |
Matcher<int> greater_than_5 = Gt(5); |
| 2535 |
|
| 2536 |
Matcher<const int&> m = Not(greater_than_5); |
| 2537 |
Matcher<int&> m2 = Not(greater_than_5); |
| 2538 |
Matcher<int&> m3 = Not(m); |
| 2539 |
} |
| 2540 |
|
| 2541 |
// Helper to allow easy testing of AllOf matchers with num parameters. |
| 2542 |
void AllOfMatches(int num, const Matcher<int>& m) { |
| 2543 |
SCOPED_TRACE(Describe(m)); |
| 2544 |
EXPECT_TRUE(m.Matches(0)); |
| 2545 |
for (int i = 1; i <= num; ++i) { |
| 2546 |
EXPECT_FALSE(m.Matches(i)); |
| 2547 |
} |
| 2548 |
EXPECT_TRUE(m.Matches(num + 1)); |
| 2549 |
} |
| 2550 |
|
| 2551 |
// Tests that AllOf(m1, ..., mn) matches any value that matches all of |
| 2552 |
// the given matchers. |
| 2553 |
TEST(AllOfTest, MatchesWhenAllMatch) { |
| 2554 |
Matcher<int> m; |
| 2555 |
m = AllOf(Le(2), Ge(1)); |
| 2556 |
EXPECT_TRUE(m.Matches(1)); |
| 2557 |
EXPECT_TRUE(m.Matches(2)); |
| 2558 |
EXPECT_FALSE(m.Matches(0)); |
| 2559 |
EXPECT_FALSE(m.Matches(3)); |
| 2560 |
|
| 2561 |
m = AllOf(Gt(0), Ne(1), Ne(2)); |
| 2562 |
EXPECT_TRUE(m.Matches(3)); |
| 2563 |
EXPECT_FALSE(m.Matches(2)); |
| 2564 |
EXPECT_FALSE(m.Matches(1)); |
| 2565 |
EXPECT_FALSE(m.Matches(0)); |
| 2566 |
|
| 2567 |
m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3)); |
| 2568 |
EXPECT_TRUE(m.Matches(4)); |
| 2569 |
EXPECT_FALSE(m.Matches(3)); |
| 2570 |
EXPECT_FALSE(m.Matches(2)); |
| 2571 |
EXPECT_FALSE(m.Matches(1)); |
| 2572 |
EXPECT_FALSE(m.Matches(0)); |
| 2573 |
|
| 2574 |
m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7)); |
| 2575 |
EXPECT_TRUE(m.Matches(0)); |
| 2576 |
EXPECT_TRUE(m.Matches(1)); |
| 2577 |
EXPECT_FALSE(m.Matches(3)); |
| 2578 |
|
| 2579 |
// The following tests for varying number of sub-matchers. Due to the way |
| 2580 |
// the sub-matchers are handled it is enough to test every sub-matcher once |
| 2581 |
// with sub-matchers using the same matcher type. Varying matcher types are |
| 2582 |
// checked for above. |
| 2583 |
AllOfMatches(2, AllOf(Ne(1), Ne(2))); |
| 2584 |
AllOfMatches(3, AllOf(Ne(1), Ne(2), Ne(3))); |
| 2585 |
AllOfMatches(4, AllOf(Ne(1), Ne(2), Ne(3), Ne(4))); |
| 2586 |
AllOfMatches(5, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5))); |
| 2587 |
AllOfMatches(6, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6))); |
| 2588 |
AllOfMatches(7, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7))); |
| 2589 |
AllOfMatches(8, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), |
| 2590 |
Ne(8))); |
| 2591 |
AllOfMatches(9, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), |
| 2592 |
Ne(8), Ne(9))); |
| 2593 |
AllOfMatches(10, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), |
| 2594 |
Ne(9), Ne(10))); |
| 2595 |
AllOfMatches( |
| 2596 |
50, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9), |
| 2597 |
Ne(10), Ne(11), Ne(12), Ne(13), Ne(14), Ne(15), Ne(16), Ne(17), |
| 2598 |
Ne(18), Ne(19), Ne(20), Ne(21), Ne(22), Ne(23), Ne(24), Ne(25), |
| 2599 |
Ne(26), Ne(27), Ne(28), Ne(29), Ne(30), Ne(31), Ne(32), Ne(33), |
| 2600 |
Ne(34), Ne(35), Ne(36), Ne(37), Ne(38), Ne(39), Ne(40), Ne(41), |
| 2601 |
Ne(42), Ne(43), Ne(44), Ne(45), Ne(46), Ne(47), Ne(48), Ne(49), |
| 2602 |
Ne(50))); |
| 2603 |
} |
| 2604 |
|
| 2605 |
|
| 2606 |
// Tests that AllOf(m1, ..., mn) describes itself properly. |
| 2607 |
TEST(AllOfTest, CanDescribeSelf) { |
| 2608 |
Matcher<int> m; |
| 2609 |
m = AllOf(Le(2), Ge(1)); |
| 2610 |
EXPECT_EQ("(is <= 2) and (is >= 1)", Describe(m)); |
| 2611 |
|
| 2612 |
m = AllOf(Gt(0), Ne(1), Ne(2)); |
| 2613 |
std::string expected_descr1 = |
| 2614 |
"(is > 0) and (isn't equal to 1) and (isn't equal to 2)"; |
| 2615 |
EXPECT_EQ(expected_descr1, Describe(m)); |
| 2616 |
|
| 2617 |
m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3)); |
| 2618 |
std::string expected_descr2 = |
| 2619 |
"(is > 0) and (isn't equal to 1) and (isn't equal to 2) and (isn't equal " |
| 2620 |
"to 3)"; |
| 2621 |
EXPECT_EQ(expected_descr2, Describe(m)); |
| 2622 |
|
| 2623 |
m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7)); |
| 2624 |
std::string expected_descr3 = |
| 2625 |
"(is >= 0) and (is < 10) and (isn't equal to 3) and (isn't equal to 5) " |
| 2626 |
"and (isn't equal to 7)"; |
| 2627 |
EXPECT_EQ(expected_descr3, Describe(m)); |
| 2628 |
} |
| 2629 |
|
| 2630 |
// Tests that AllOf(m1, ..., mn) describes its negation properly. |
| 2631 |
TEST(AllOfTest, CanDescribeNegation) { |
| 2632 |
Matcher<int> m; |
| 2633 |
m = AllOf(Le(2), Ge(1)); |
| 2634 |
std::string expected_descr4 = "(isn't <= 2) or (isn't >= 1)"; |
| 2635 |
EXPECT_EQ(expected_descr4, DescribeNegation(m)); |
| 2636 |
|
| 2637 |
m = AllOf(Gt(0), Ne(1), Ne(2)); |
| 2638 |
std::string expected_descr5 = |
| 2639 |
"(isn't > 0) or (is equal to 1) or (is equal to 2)"; |
| 2640 |
EXPECT_EQ(expected_descr5, DescribeNegation(m)); |
| 2641 |
|
| 2642 |
m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3)); |
| 2643 |
std::string expected_descr6 = |
| 2644 |
"(isn't > 0) or (is equal to 1) or (is equal to 2) or (is equal to 3)"; |
| 2645 |
EXPECT_EQ(expected_descr6, DescribeNegation(m)); |
| 2646 |
|
| 2647 |
m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7)); |
| 2648 |
std::string expected_desr7 = |
| 2649 |
"(isn't >= 0) or (isn't < 10) or (is equal to 3) or (is equal to 5) or " |
| 2650 |
"(is equal to 7)"; |
| 2651 |
EXPECT_EQ(expected_desr7, DescribeNegation(m)); |
| 2652 |
|
| 2653 |
m = AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9), |
| 2654 |
Ne(10), Ne(11)); |
| 2655 |
AllOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11); |
| 2656 |
EXPECT_THAT(Describe(m), EndsWith("and (isn't equal to 11)")); |
| 2657 |
AllOfMatches(11, m); |
| 2658 |
} |
| 2659 |
|
| 2660 |
// Tests that monomorphic matchers are safely cast by the AllOf matcher. |
| 2661 |
TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) { |
| 2662 |
// greater_than_5 and less_than_10 are monomorphic matchers. |
| 2663 |
Matcher<int> greater_than_5 = Gt(5); |
| 2664 |
Matcher<int> less_than_10 = Lt(10); |
| 2665 |
|
| 2666 |
Matcher<const int&> m = AllOf(greater_than_5, less_than_10); |
| 2667 |
Matcher<int&> m2 = AllOf(greater_than_5, less_than_10); |
| 2668 |
Matcher<int&> m3 = AllOf(greater_than_5, m2); |
| 2669 |
|
| 2670 |
// Tests that BothOf works when composing itself. |
| 2671 |
Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10); |
| 2672 |
Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10); |
| 2673 |
} |
| 2674 |
|
| 2675 |
TEST(AllOfTest, ExplainsResult) { |
| 2676 |
Matcher<int> m; |
| 2677 |
|
| 2678 |
// Successful match. Both matchers need to explain. The second |
| 2679 |
// matcher doesn't give an explanation, so only the first matcher's |
| 2680 |
// explanation is printed. |
| 2681 |
m = AllOf(GreaterThan(10), Lt(30)); |
| 2682 |
EXPECT_EQ("which is 15 more than 10", Explain(m, 25)); |
| 2683 |
|
| 2684 |
// Successful match. Both matchers need to explain. |
| 2685 |
m = AllOf(GreaterThan(10), GreaterThan(20)); |
| 2686 |
EXPECT_EQ("which is 20 more than 10, and which is 10 more than 20", |
| 2687 |
Explain(m, 30)); |
| 2688 |
|
| 2689 |
// Successful match. All matchers need to explain. The second |
| 2690 |
// matcher doesn't given an explanation. |
| 2691 |
m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20)); |
| 2692 |
EXPECT_EQ("which is 15 more than 10, and which is 5 more than 20", |
| 2693 |
Explain(m, 25)); |
| 2694 |
|
| 2695 |
// Successful match. All matchers need to explain. |
| 2696 |
m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30)); |
| 2697 |
EXPECT_EQ("which is 30 more than 10, and which is 20 more than 20, " |
| 2698 |
"and which is 10 more than 30", |
| 2699 |
Explain(m, 40)); |
| 2700 |
|
| 2701 |
// Failed match. The first matcher, which failed, needs to |
| 2702 |
// explain. |
| 2703 |
m = AllOf(GreaterThan(10), GreaterThan(20)); |
| 2704 |
EXPECT_EQ("which is 5 less than 10", Explain(m, 5)); |
| 2705 |
|
| 2706 |
// Failed match. The second matcher, which failed, needs to |
| 2707 |
// explain. Since it doesn't given an explanation, nothing is |
| 2708 |
// printed. |
| 2709 |
m = AllOf(GreaterThan(10), Lt(30)); |
| 2710 |
EXPECT_EQ("", Explain(m, 40)); |
| 2711 |
|
| 2712 |
// Failed match. The second matcher, which failed, needs to |
| 2713 |
// explain. |
| 2714 |
m = AllOf(GreaterThan(10), GreaterThan(20)); |
| 2715 |
EXPECT_EQ("which is 5 less than 20", Explain(m, 15)); |
| 2716 |
} |
| 2717 |
|
| 2718 |
// Helper to allow easy testing of AnyOf matchers with num parameters. |
| 2719 |
static void AnyOfMatches(int num, const Matcher<int>& m) { |
| 2720 |
SCOPED_TRACE(Describe(m)); |
| 2721 |
EXPECT_FALSE(m.Matches(0)); |
| 2722 |
for (int i = 1; i <= num; ++i) { |
| 2723 |
EXPECT_TRUE(m.Matches(i)); |
| 2724 |
} |
| 2725 |
EXPECT_FALSE(m.Matches(num + 1)); |
| 2726 |
} |
| 2727 |
|
| 2728 |
static void AnyOfStringMatches(int num, const Matcher<std::string>& m) { |
| 2729 |
SCOPED_TRACE(Describe(m)); |
| 2730 |
EXPECT_FALSE(m.Matches(std::to_string(0))); |
| 2731 |
|
| 2732 |
for (int i = 1; i <= num; ++i) { |
| 2733 |
EXPECT_TRUE(m.Matches(std::to_string(i))); |
| 2734 |
} |
| 2735 |
EXPECT_FALSE(m.Matches(std::to_string(num + 1))); |
| 2736 |
} |
| 2737 |
|
| 2738 |
// Tests that AnyOf(m1, ..., mn) matches any value that matches at |
| 2739 |
// least one of the given matchers. |
| 2740 |
TEST(AnyOfTest, MatchesWhenAnyMatches) { |
| 2741 |
Matcher<int> m; |
| 2742 |
m = AnyOf(Le(1), Ge(3)); |
| 2743 |
EXPECT_TRUE(m.Matches(1)); |
| 2744 |
EXPECT_TRUE(m.Matches(4)); |
| 2745 |
EXPECT_FALSE(m.Matches(2)); |
| 2746 |
|
| 2747 |
m = AnyOf(Lt(0), Eq(1), Eq(2)); |
| 2748 |
EXPECT_TRUE(m.Matches(-1)); |
| 2749 |
EXPECT_TRUE(m.Matches(1)); |
| 2750 |
EXPECT_TRUE(m.Matches(2)); |
| 2751 |
EXPECT_FALSE(m.Matches(0)); |
| 2752 |
|
| 2753 |
m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3)); |
| 2754 |
EXPECT_TRUE(m.Matches(-1)); |
| 2755 |
EXPECT_TRUE(m.Matches(1)); |
| 2756 |
EXPECT_TRUE(m.Matches(2)); |
| 2757 |
EXPECT_TRUE(m.Matches(3)); |
| 2758 |
EXPECT_FALSE(m.Matches(0)); |
| 2759 |
|
| 2760 |
m = AnyOf(Le(0), Gt(10), 3, 5, 7); |
| 2761 |
EXPECT_TRUE(m.Matches(0)); |
| 2762 |
EXPECT_TRUE(m.Matches(11)); |
| 2763 |
EXPECT_TRUE(m.Matches(3)); |
| 2764 |
EXPECT_FALSE(m.Matches(2)); |
| 2765 |
|
| 2766 |
// The following tests for varying number of sub-matchers. Due to the way |
| 2767 |
// the sub-matchers are handled it is enough to test every sub-matcher once |
| 2768 |
// with sub-matchers using the same matcher type. Varying matcher types are |
| 2769 |
// checked for above. |
| 2770 |
AnyOfMatches(2, AnyOf(1, 2)); |
| 2771 |
AnyOfMatches(3, AnyOf(1, 2, 3)); |
| 2772 |
AnyOfMatches(4, AnyOf(1, 2, 3, 4)); |
| 2773 |
AnyOfMatches(5, AnyOf(1, 2, 3, 4, 5)); |
| 2774 |
AnyOfMatches(6, AnyOf(1, 2, 3, 4, 5, 6)); |
| 2775 |
AnyOfMatches(7, AnyOf(1, 2, 3, 4, 5, 6, 7)); |
| 2776 |
AnyOfMatches(8, AnyOf(1, 2, 3, 4, 5, 6, 7, 8)); |
| 2777 |
AnyOfMatches(9, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9)); |
| 2778 |
AnyOfMatches(10, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); |
| 2779 |
} |
| 2780 |
|
| 2781 |
// Tests the variadic version of the AnyOfMatcher. |
| 2782 |
TEST(AnyOfTest, VariadicMatchesWhenAnyMatches) { |
| 2783 |
// Also make sure AnyOf is defined in the right namespace and does not depend |
| 2784 |
// on ADL. |
| 2785 |
Matcher<int> m = ::testing::AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11); |
| 2786 |
|
| 2787 |
EXPECT_THAT(Describe(m), EndsWith("or (is equal to 11)")); |
| 2788 |
AnyOfMatches(11, m); |
| 2789 |
AnyOfMatches(50, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, |
| 2790 |
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, |
| 2791 |
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 2792 |
31, 32, 33, 34, 35, 36, 37, 38, 39, 40, |
| 2793 |
41, 42, 43, 44, 45, 46, 47, 48, 49, 50)); |
| 2794 |
AnyOfStringMatches( |
| 2795 |
50, AnyOf("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", |
| 2796 |
"13", "14", "15", "16", "17", "18", "19", "20", "21", "22", |
| 2797 |
"23", "24", "25", "26", "27", "28", "29", "30", "31", "32", |
| 2798 |
"33", "34", "35", "36", "37", "38", "39", "40", "41", "42", |
| 2799 |
"43", "44", "45", "46", "47", "48", "49", "50")); |
| 2800 |
} |
| 2801 |
|
| 2802 |
TEST(ConditionalTest, MatchesFirstIfCondition) { |
| 2803 |
Matcher<std::string> eq_red = Eq("red"); |
| 2804 |
Matcher<std::string> ne_red = Ne("red"); |
| 2805 |
Matcher<std::string> m = Conditional(true, eq_red, ne_red); |
| 2806 |
EXPECT_TRUE(m.Matches("red")); |
| 2807 |
EXPECT_FALSE(m.Matches("green")); |
| 2808 |
|
| 2809 |
StringMatchResultListener listener; |
| 2810 |
StringMatchResultListener expected; |
| 2811 |
EXPECT_FALSE(m.MatchAndExplain("green", &listener)); |
| 2812 |
EXPECT_FALSE(eq_red.MatchAndExplain("green", &expected)); |
| 2813 |
EXPECT_THAT(listener.str(), Eq(expected.str())); |
| 2814 |
} |
| 2815 |
|
| 2816 |
TEST(ConditionalTest, MatchesSecondIfCondition) { |
| 2817 |
Matcher<std::string> eq_red = Eq("red"); |
| 2818 |
Matcher<std::string> ne_red = Ne("red"); |
| 2819 |
Matcher<std::string> m = Conditional(false, eq_red, ne_red); |
| 2820 |
EXPECT_FALSE(m.Matches("red")); |
| 2821 |
EXPECT_TRUE(m.Matches("green")); |
| 2822 |
|
| 2823 |
StringMatchResultListener listener; |
| 2824 |
StringMatchResultListener expected; |
| 2825 |
EXPECT_FALSE(m.MatchAndExplain("red", &listener)); |
| 2826 |
EXPECT_FALSE(ne_red.MatchAndExplain("red", &expected)); |
| 2827 |
EXPECT_THAT(listener.str(), Eq(expected.str())); |
| 2828 |
} |
| 2829 |
|
| 2830 |
// Tests the variadic version of the ElementsAreMatcher |
| 2831 |
TEST(ElementsAreTest, HugeMatcher) { |
| 2832 |
vector<int> test_vector{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; |
| 2833 |
|
| 2834 |
EXPECT_THAT(test_vector, |
| 2835 |
ElementsAre(Eq(1), Eq(2), Lt(13), Eq(4), Eq(5), Eq(6), Eq(7), |
| 2836 |
Eq(8), Eq(9), Eq(10), Gt(1), Eq(12))); |
| 2837 |
} |
| 2838 |
|
| 2839 |
// Tests the variadic version of the UnorderedElementsAreMatcher |
| 2840 |
TEST(ElementsAreTest, HugeMatcherStr) { |
| 2841 |
vector<std::string> test_vector{ |
| 2842 |
"literal_string", "", "", "", "", "", "", "", "", "", "", ""}; |
| 2843 |
|
| 2844 |
EXPECT_THAT(test_vector, UnorderedElementsAre("literal_string", _, _, _, _, _, |
| 2845 |
_, _, _, _, _, _)); |
| 2846 |
} |
| 2847 |
|
| 2848 |
// Tests the variadic version of the UnorderedElementsAreMatcher |
| 2849 |
TEST(ElementsAreTest, HugeMatcherUnordered) { |
| 2850 |
vector<int> test_vector{2, 1, 8, 5, 4, 6, 7, 3, 9, 12, 11, 10}; |
| 2851 |
|
| 2852 |
EXPECT_THAT(test_vector, UnorderedElementsAre( |
| 2853 |
Eq(2), Eq(1), Gt(7), Eq(5), Eq(4), Eq(6), Eq(7), |
| 2854 |
Eq(3), Eq(9), Eq(12), Eq(11), Ne(122))); |
| 2855 |
} |
| 2856 |
|
| 2857 |
|
| 2858 |
// Tests that AnyOf(m1, ..., mn) describes itself properly. |
| 2859 |
TEST(AnyOfTest, CanDescribeSelf) { |
| 2860 |
Matcher<int> m; |
| 2861 |
m = AnyOf(Le(1), Ge(3)); |
| 2862 |
|
| 2863 |
EXPECT_EQ("(is <= 1) or (is >= 3)", |
| 2864 |
Describe(m)); |
| 2865 |
|
| 2866 |
m = AnyOf(Lt(0), Eq(1), Eq(2)); |
| 2867 |
EXPECT_EQ("(is < 0) or (is equal to 1) or (is equal to 2)", Describe(m)); |
| 2868 |
|
| 2869 |
m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3)); |
| 2870 |
EXPECT_EQ("(is < 0) or (is equal to 1) or (is equal to 2) or (is equal to 3)", |
| 2871 |
Describe(m)); |
| 2872 |
|
| 2873 |
m = AnyOf(Le(0), Gt(10), 3, 5, 7); |
| 2874 |
EXPECT_EQ( |
| 2875 |
"(is <= 0) or (is > 10) or (is equal to 3) or (is equal to 5) or (is " |
| 2876 |
"equal to 7)", |
| 2877 |
Describe(m)); |
| 2878 |
} |
| 2879 |
|
| 2880 |
// Tests that AnyOf(m1, ..., mn) describes its negation properly. |
| 2881 |
TEST(AnyOfTest, CanDescribeNegation) { |
| 2882 |
Matcher<int> m; |
| 2883 |
m = AnyOf(Le(1), Ge(3)); |
| 2884 |
EXPECT_EQ("(isn't <= 1) and (isn't >= 3)", |
| 2885 |
DescribeNegation(m)); |
| 2886 |
|
| 2887 |
m = AnyOf(Lt(0), Eq(1), Eq(2)); |
| 2888 |
EXPECT_EQ("(isn't < 0) and (isn't equal to 1) and (isn't equal to 2)", |
| 2889 |
DescribeNegation(m)); |
| 2890 |
|
| 2891 |
m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3)); |
| 2892 |
EXPECT_EQ( |
| 2893 |
"(isn't < 0) and (isn't equal to 1) and (isn't equal to 2) and (isn't " |
| 2894 |
"equal to 3)", |
| 2895 |
DescribeNegation(m)); |
| 2896 |
|
| 2897 |
m = AnyOf(Le(0), Gt(10), 3, 5, 7); |
| 2898 |
EXPECT_EQ( |
| 2899 |
"(isn't <= 0) and (isn't > 10) and (isn't equal to 3) and (isn't equal " |
| 2900 |
"to 5) and (isn't equal to 7)", |
| 2901 |
DescribeNegation(m)); |
| 2902 |
} |
| 2903 |
|
| 2904 |
// Tests that monomorphic matchers are safely cast by the AnyOf matcher. |
| 2905 |
TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) { |
| 2906 |
// greater_than_5 and less_than_10 are monomorphic matchers. |
| 2907 |
Matcher<int> greater_than_5 = Gt(5); |
| 2908 |
Matcher<int> less_than_10 = Lt(10); |
| 2909 |
|
| 2910 |
Matcher<const int&> m = AnyOf(greater_than_5, less_than_10); |
| 2911 |
Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10); |
| 2912 |
Matcher<int&> m3 = AnyOf(greater_than_5, m2); |
| 2913 |
|
| 2914 |
// Tests that EitherOf works when composing itself. |
| 2915 |
Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10); |
| 2916 |
Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10); |
| 2917 |
} |
| 2918 |
|
| 2919 |
TEST(AnyOfTest, ExplainsResult) { |
| 2920 |
Matcher<int> m; |
| 2921 |
|
| 2922 |
// Failed match. Both matchers need to explain. The second |
| 2923 |
// matcher doesn't give an explanation, so only the first matcher's |
| 2924 |
// explanation is printed. |
| 2925 |
m = AnyOf(GreaterThan(10), Lt(0)); |
| 2926 |
EXPECT_EQ("which is 5 less than 10", Explain(m, 5)); |
| 2927 |
|
| 2928 |
// Failed match. Both matchers need to explain. |
| 2929 |
m = AnyOf(GreaterThan(10), GreaterThan(20)); |
| 2930 |
EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20", |
| 2931 |
Explain(m, 5)); |
| 2932 |
|
| 2933 |
// Failed match. All matchers need to explain. The second |
| 2934 |
// matcher doesn't given an explanation. |
| 2935 |
m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30)); |
| 2936 |
EXPECT_EQ("which is 5 less than 10, and which is 25 less than 30", |
| 2937 |
Explain(m, 5)); |
| 2938 |
|
| 2939 |
// Failed match. All matchers need to explain. |
| 2940 |
m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30)); |
| 2941 |
EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20, " |
| 2942 |
"and which is 25 less than 30", |
| 2943 |
Explain(m, 5)); |
| 2944 |
|
| 2945 |
// Successful match. The first matcher, which succeeded, needs to |
| 2946 |
// explain. |
| 2947 |
m = AnyOf(GreaterThan(10), GreaterThan(20)); |
| 2948 |
EXPECT_EQ("which is 5 more than 10", Explain(m, 15)); |
| 2949 |
|
| 2950 |
// Successful match. The second matcher, which succeeded, needs to |
| 2951 |
// explain. Since it doesn't given an explanation, nothing is |
| 2952 |
// printed. |
| 2953 |
m = AnyOf(GreaterThan(10), Lt(30)); |
| 2954 |
EXPECT_EQ("", Explain(m, 0)); |
| 2955 |
|
| 2956 |
// Successful match. The second matcher, which succeeded, needs to |
| 2957 |
// explain. |
| 2958 |
m = AnyOf(GreaterThan(30), GreaterThan(20)); |
| 2959 |
EXPECT_EQ("which is 5 more than 20", Explain(m, 25)); |
| 2960 |
} |
| 2961 |
|
| 2962 |
// The following predicate function and predicate functor are for |
| 2963 |
// testing the Truly(predicate) matcher. |
| 2964 |
|
| 2965 |
// Returns non-zero if the input is positive. Note that the return |
| 2966 |
// type of this function is not bool. It's OK as Truly() accepts any |
| 2967 |
// unary function or functor whose return type can be implicitly |
| 2968 |
// converted to bool. |
| 2969 |
int IsPositive(double x) { |
| 2970 |
return x > 0 ? 1 : 0; |
| 2971 |
} |
| 2972 |
|
| 2973 |
// This functor returns true if the input is greater than the given |
| 2974 |
// number. |
| 2975 |
class IsGreaterThan { |
| 2976 |
public: |
| 2977 |
explicit IsGreaterThan(int threshold) : threshold_(threshold) {} |
| 2978 |
|
| 2979 |
bool operator()(int n) const { return n > threshold_; } |
| 2980 |
|
| 2981 |
private: |
| 2982 |
int threshold_; |
| 2983 |
}; |
| 2984 |
|
| 2985 |
// For testing Truly(). |
| 2986 |
const int foo = 0; |
| 2987 |
|
| 2988 |
// This predicate returns true if and only if the argument references foo and |
| 2989 |
// has a zero value. |
| 2990 |
bool ReferencesFooAndIsZero(const int& n) { |
| 2991 |
return (&n == &foo) && (n == 0); |
| 2992 |
} |
| 2993 |
|
| 2994 |
// Tests that Truly(predicate) matches what satisfies the given |
| 2995 |
// predicate. |
| 2996 |
TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) { |
| 2997 |
Matcher<double> m = Truly(IsPositive); |
| 2998 |
EXPECT_TRUE(m.Matches(2.0)); |
| 2999 |
EXPECT_FALSE(m.Matches(-1.5)); |
| 3000 |
} |
| 3001 |
|
| 3002 |
// Tests that Truly(predicate_functor) works too. |
| 3003 |
TEST(TrulyTest, CanBeUsedWithFunctor) { |
| 3004 |
Matcher<int> m = Truly(IsGreaterThan(5)); |
| 3005 |
EXPECT_TRUE(m.Matches(6)); |
| 3006 |
EXPECT_FALSE(m.Matches(4)); |
| 3007 |
} |
| 3008 |
|
| 3009 |
// A class that can be implicitly converted to bool. |
| 3010 |
class ConvertibleToBool { |
| 3011 |
public: |
| 3012 |
explicit ConvertibleToBool(int number) : number_(number) {} |
| 3013 |
operator bool() const { return number_ != 0; } |
| 3014 |
|
| 3015 |
private: |
| 3016 |
int number_; |
| 3017 |
}; |
| 3018 |
|
| 3019 |
ConvertibleToBool IsNotZero(int number) { |
| 3020 |
return ConvertibleToBool(number); |
| 3021 |
} |
| 3022 |
|
| 3023 |
// Tests that the predicate used in Truly() may return a class that's |
| 3024 |
// implicitly convertible to bool, even when the class has no |
| 3025 |
// operator!(). |
| 3026 |
TEST(TrulyTest, PredicateCanReturnAClassConvertibleToBool) { |
| 3027 |
Matcher<int> m = Truly(IsNotZero); |
| 3028 |
EXPECT_TRUE(m.Matches(1)); |
| 3029 |
EXPECT_FALSE(m.Matches(0)); |
| 3030 |
} |
| 3031 |
|
| 3032 |
// Tests that Truly(predicate) can describe itself properly. |
| 3033 |
TEST(TrulyTest, CanDescribeSelf) { |
| 3034 |
Matcher<double> m = Truly(IsPositive); |
| 3035 |
EXPECT_EQ("satisfies the given predicate", |
| 3036 |
Describe(m)); |
| 3037 |
} |
| 3038 |
|
| 3039 |
// Tests that Truly(predicate) works when the matcher takes its |
| 3040 |
// argument by reference. |
| 3041 |
TEST(TrulyTest, WorksForByRefArguments) { |
| 3042 |
Matcher<const int&> m = Truly(ReferencesFooAndIsZero); |
| 3043 |
EXPECT_TRUE(m.Matches(foo)); |
| 3044 |
int n = 0; |
| 3045 |
EXPECT_FALSE(m.Matches(n)); |
| 3046 |
} |
| 3047 |
|
| 3048 |
// Tests that Truly(predicate) provides a helpful reason when it fails. |
| 3049 |
TEST(TrulyTest, ExplainsFailures) { |
| 3050 |
StringMatchResultListener listener; |
| 3051 |
EXPECT_FALSE(ExplainMatchResult(Truly(IsPositive), -1, &listener)); |
| 3052 |
EXPECT_EQ(listener.str(), "didn't satisfy the given predicate"); |
| 3053 |
} |
| 3054 |
|
| 3055 |
// Tests that Matches(m) is a predicate satisfied by whatever that |
| 3056 |
// matches matcher m. |
| 3057 |
TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) { |
| 3058 |
EXPECT_TRUE(Matches(Ge(0))(1)); |
| 3059 |
EXPECT_FALSE(Matches(Eq('a'))('b')); |
| 3060 |
} |
| 3061 |
|
| 3062 |
// Tests that Matches(m) works when the matcher takes its argument by |
| 3063 |
// reference. |
| 3064 |
TEST(MatchesTest, WorksOnByRefArguments) { |
| 3065 |
int m = 0, n = 0; |
| 3066 |
EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n)); |
| 3067 |
EXPECT_FALSE(Matches(Ref(m))(n)); |
| 3068 |
} |
| 3069 |
|
| 3070 |
// Tests that a Matcher on non-reference type can be used in |
| 3071 |
// Matches(). |
| 3072 |
TEST(MatchesTest, WorksWithMatcherOnNonRefType) { |
| 3073 |
Matcher<int> eq5 = Eq(5); |
| 3074 |
EXPECT_TRUE(Matches(eq5)(5)); |
| 3075 |
EXPECT_FALSE(Matches(eq5)(2)); |
| 3076 |
} |
| 3077 |
|
| 3078 |
// Tests Value(value, matcher). Since Value() is a simple wrapper for |
| 3079 |
// Matches(), which has been tested already, we don't spend a lot of |
| 3080 |
// effort on testing Value(). |
| 3081 |
TEST(ValueTest, WorksWithPolymorphicMatcher) { |
| 3082 |
EXPECT_TRUE(Value("hi", StartsWith("h"))); |
| 3083 |
EXPECT_FALSE(Value(5, Gt(10))); |
| 3084 |
} |
| 3085 |
|
| 3086 |
TEST(ValueTest, WorksWithMonomorphicMatcher) { |
| 3087 |
const Matcher<int> is_zero = Eq(0); |
| 3088 |
EXPECT_TRUE(Value(0, is_zero)); |
| 3089 |
EXPECT_FALSE(Value('a', is_zero)); |
| 3090 |
|
| 3091 |
int n = 0; |
| 3092 |
const Matcher<const int&> ref_n = Ref(n); |
| 3093 |
EXPECT_TRUE(Value(n, ref_n)); |
| 3094 |
EXPECT_FALSE(Value(1, ref_n)); |
| 3095 |
} |
| 3096 |
|
| 3097 |
TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) { |
| 3098 |
StringMatchResultListener listener1; |
| 3099 |
EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1)); |
| 3100 |
EXPECT_EQ("% 2 == 0", listener1.str()); |
| 3101 |
|
| 3102 |
StringMatchResultListener listener2; |
| 3103 |
EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2)); |
| 3104 |
EXPECT_EQ("", listener2.str()); |
| 3105 |
} |
| 3106 |
|
| 3107 |
TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) { |
| 3108 |
const Matcher<int> is_even = PolymorphicIsEven(); |
| 3109 |
StringMatchResultListener listener1; |
| 3110 |
EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1)); |
| 3111 |
EXPECT_EQ("% 2 == 0", listener1.str()); |
| 3112 |
|
| 3113 |
const Matcher<const double&> is_zero = Eq(0); |
| 3114 |
StringMatchResultListener listener2; |
| 3115 |
EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2)); |
| 3116 |
EXPECT_EQ("", listener2.str()); |
| 3117 |
} |
| 3118 |
|
| 3119 |
MATCHER(ConstructNoArg, "") { return true; } |
| 3120 |
MATCHER_P(Construct1Arg, arg1, "") { return true; } |
| 3121 |
MATCHER_P2(Construct2Args, arg1, arg2, "") { return true; } |
| 3122 |
|
| 3123 |
TEST(MatcherConstruct, ExplicitVsImplicit) { |
| 3124 |
{ |
| 3125 |
// No arg constructor can be constructed with empty brace. |
| 3126 |
ConstructNoArgMatcher m = {}; |
| 3127 |
(void)m; |
| 3128 |
// And with no args |
| 3129 |
ConstructNoArgMatcher m2; |
| 3130 |
(void)m2; |
| 3131 |
} |
| 3132 |
{ |
| 3133 |
// The one arg constructor has an explicit constructor. |
| 3134 |
// This is to prevent the implicit conversion. |
| 3135 |
using M = Construct1ArgMatcherP<int>; |
| 3136 |
EXPECT_TRUE((std::is_constructible<M, int>::value)); |
| 3137 |
EXPECT_FALSE((std::is_convertible<int, M>::value)); |
| 3138 |
} |
| 3139 |
{ |
| 3140 |
// Multiple arg matchers can be constructed with an implicit construction. |
| 3141 |
Construct2ArgsMatcherP2<int, double> m = {1, 2.2}; |
| 3142 |
(void)m; |
| 3143 |
} |
| 3144 |
} |
| 3145 |
|
| 3146 |
MATCHER_P(Really, inner_matcher, "") { |
| 3147 |
return ExplainMatchResult(inner_matcher, arg, result_listener); |
| 3148 |
} |
| 3149 |
|
| 3150 |
TEST(ExplainMatchResultTest, WorksInsideMATCHER) { |
| 3151 |
EXPECT_THAT(0, Really(Eq(0))); |
| 3152 |
} |
| 3153 |
|
| 3154 |
TEST(DescribeMatcherTest, WorksWithValue) { |
| 3155 |
EXPECT_EQ("is equal to 42", DescribeMatcher<int>(42)); |
| 3156 |
EXPECT_EQ("isn't equal to 42", DescribeMatcher<int>(42, true)); |
| 3157 |
} |
| 3158 |
|
| 3159 |
TEST(DescribeMatcherTest, WorksWithMonomorphicMatcher) { |
| 3160 |
const Matcher<int> monomorphic = Le(0); |
| 3161 |
EXPECT_EQ("is <= 0", DescribeMatcher<int>(monomorphic)); |
| 3162 |
EXPECT_EQ("isn't <= 0", DescribeMatcher<int>(monomorphic, true)); |
| 3163 |
} |
| 3164 |
|
| 3165 |
TEST(DescribeMatcherTest, WorksWithPolymorphicMatcher) { |
| 3166 |
EXPECT_EQ("is even", DescribeMatcher<int>(PolymorphicIsEven())); |
| 3167 |
EXPECT_EQ("is odd", DescribeMatcher<int>(PolymorphicIsEven(), true)); |
| 3168 |
} |
| 3169 |
|
| 3170 |
TEST(AllArgsTest, WorksForTuple) { |
| 3171 |
EXPECT_THAT(std::make_tuple(1, 2L), AllArgs(Lt())); |
| 3172 |
EXPECT_THAT(std::make_tuple(2L, 1), Not(AllArgs(Lt()))); |
| 3173 |
} |
| 3174 |
|
| 3175 |
TEST(AllArgsTest, WorksForNonTuple) { |
| 3176 |
EXPECT_THAT(42, AllArgs(Gt(0))); |
| 3177 |
EXPECT_THAT('a', Not(AllArgs(Eq('b')))); |
| 3178 |
} |
| 3179 |
|
| 3180 |
class AllArgsHelper { |
| 3181 |
public: |
| 3182 |
AllArgsHelper() {} |
| 3183 |
|
| 3184 |
MOCK_METHOD2(Helper, int(char x, int y)); |
| 3185 |
|
| 3186 |
private: |
| 3187 |
GTEST_DISALLOW_COPY_AND_ASSIGN_(AllArgsHelper); |
| 3188 |
}; |
| 3189 |
|
| 3190 |
TEST(AllArgsTest, WorksInWithClause) { |
| 3191 |
AllArgsHelper helper; |
| 3192 |
ON_CALL(helper, Helper(_, _)) |
| 3193 |
.With(AllArgs(Lt())) |
| 3194 |
.WillByDefault(Return(1)); |
| 3195 |
EXPECT_CALL(helper, Helper(_, _)); |
| 3196 |
EXPECT_CALL(helper, Helper(_, _)) |
| 3197 |
.With(AllArgs(Gt())) |
| 3198 |
.WillOnce(Return(2)); |
| 3199 |
|
| 3200 |
EXPECT_EQ(1, helper.Helper('\1', 2)); |
| 3201 |
EXPECT_EQ(2, helper.Helper('a', 1)); |
| 3202 |
} |
| 3203 |
|
| 3204 |
class OptionalMatchersHelper { |
| 3205 |
public: |
| 3206 |
OptionalMatchersHelper() {} |
| 3207 |
|
| 3208 |
MOCK_METHOD0(NoArgs, int()); |
| 3209 |
|
| 3210 |
MOCK_METHOD1(OneArg, int(int y)); |
| 3211 |
|
| 3212 |
MOCK_METHOD2(TwoArgs, int(char x, int y)); |
| 3213 |
|
| 3214 |
MOCK_METHOD1(Overloaded, int(char x)); |
| 3215 |
MOCK_METHOD2(Overloaded, int(char x, int y)); |
| 3216 |
|
| 3217 |
private: |
| 3218 |
GTEST_DISALLOW_COPY_AND_ASSIGN_(OptionalMatchersHelper); |
| 3219 |
}; |
| 3220 |
|
| 3221 |
TEST(AllArgsTest, WorksWithoutMatchers) { |
| 3222 |
OptionalMatchersHelper helper; |
| 3223 |
|
| 3224 |
ON_CALL(helper, NoArgs).WillByDefault(Return(10)); |
| 3225 |
ON_CALL(helper, OneArg).WillByDefault(Return(20)); |
| 3226 |
ON_CALL(helper, TwoArgs).WillByDefault(Return(30)); |
| 3227 |
|
| 3228 |
EXPECT_EQ(10, helper.NoArgs()); |
| 3229 |
EXPECT_EQ(20, helper.OneArg(1)); |
| 3230 |
EXPECT_EQ(30, helper.TwoArgs('\1', 2)); |
| 3231 |
|
| 3232 |
EXPECT_CALL(helper, NoArgs).Times(1); |
| 3233 |
EXPECT_CALL(helper, OneArg).WillOnce(Return(100)); |
| 3234 |
EXPECT_CALL(helper, OneArg(17)).WillOnce(Return(200)); |
| 3235 |
EXPECT_CALL(helper, TwoArgs).Times(0); |
| 3236 |
|
| 3237 |
EXPECT_EQ(10, helper.NoArgs()); |
| 3238 |
EXPECT_EQ(100, helper.OneArg(1)); |
| 3239 |
EXPECT_EQ(200, helper.OneArg(17)); |
| 3240 |
} |
| 3241 |
|
| 3242 |
// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value |
| 3243 |
// matches the matcher. |
| 3244 |
TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) { |
| 3245 |
ASSERT_THAT(5, Ge(2)) << "This should succeed."; |
| 3246 |
ASSERT_THAT("Foo", EndsWith("oo")); |
| 3247 |
EXPECT_THAT(2, AllOf(Le(7), Ge(0))) << "This should succeed too."; |
| 3248 |
EXPECT_THAT("Hello", StartsWith("Hell")); |
| 3249 |
} |
| 3250 |
|
| 3251 |
// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value |
| 3252 |
// doesn't match the matcher. |
| 3253 |
TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) { |
| 3254 |
// 'n' must be static as it is used in an EXPECT_FATAL_FAILURE(), |
| 3255 |
// which cannot reference auto variables. |
| 3256 |
static unsigned short n; // NOLINT |
| 3257 |
n = 5; |
| 3258 |
|
| 3259 |
EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Gt(10)), |
| 3260 |
"Value of: n\n" |
| 3261 |
"Expected: is > 10\n" |
| 3262 |
" Actual: 5" + OfType("unsigned short")); |
| 3263 |
n = 0; |
| 3264 |
EXPECT_NONFATAL_FAILURE( |
| 3265 |
EXPECT_THAT(n, AllOf(Le(7), Ge(5))), |
| 3266 |
"Value of: n\n" |
| 3267 |
"Expected: (is <= 7) and (is >= 5)\n" |
| 3268 |
" Actual: 0" + OfType("unsigned short")); |
| 3269 |
} |
| 3270 |
|
| 3271 |
// Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument |
| 3272 |
// has a reference type. |
| 3273 |
TEST(MatcherAssertionTest, WorksForByRefArguments) { |
| 3274 |
// We use a static variable here as EXPECT_FATAL_FAILURE() cannot |
| 3275 |
// reference auto variables. |
| 3276 |
static int n; |
| 3277 |
n = 0; |
| 3278 |
EXPECT_THAT(n, AllOf(Le(7), Ref(n))); |
| 3279 |
EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Not(Ref(n))), |
| 3280 |
"Value of: n\n" |
| 3281 |
"Expected: does not reference the variable @"); |
| 3282 |
// Tests the "Actual" part. |
| 3283 |
EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Not(Ref(n))), |
| 3284 |
"Actual: 0" + OfType("int") + ", which is located @"); |
| 3285 |
} |
| 3286 |
|
| 3287 |
// Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is |
| 3288 |
// monomorphic. |
| 3289 |
TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) { |
| 3290 |
Matcher<const char*> starts_with_he = StartsWith("he"); |
| 3291 |
ASSERT_THAT("hello", starts_with_he); |
| 3292 |
|
| 3293 |
Matcher<const std::string&> ends_with_ok = EndsWith("ok"); |
| 3294 |
ASSERT_THAT("book", ends_with_ok); |
| 3295 |
const std::string bad = "bad"; |
| 3296 |
EXPECT_NONFATAL_FAILURE(EXPECT_THAT(bad, ends_with_ok), |
| 3297 |
"Value of: bad\n" |
| 3298 |
"Expected: ends with \"ok\"\n" |
| 3299 |
" Actual: \"bad\""); |
| 3300 |
Matcher<int> is_greater_than_5 = Gt(5); |
| 3301 |
EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5), |
| 3302 |
"Value of: 5\n" |
| 3303 |
"Expected: is > 5\n" |
| 3304 |
" Actual: 5" + OfType("int")); |
| 3305 |
} |
| 3306 |
|
| 3307 |
// Tests floating-point matchers. |
| 3308 |
template <typename RawType> |
| 3309 |
class FloatingPointTest : public testing::Test { |
| 3310 |
protected: |
| 3311 |
typedef testing::internal::FloatingPoint<RawType> Floating; |
| 3312 |
typedef typename Floating::Bits Bits; |
| 3313 |
|
| 3314 |
FloatingPointTest() |
| 3315 |
: max_ulps_(Floating::kMaxUlps), |
| 3316 |
zero_bits_(Floating(0).bits()), |
| 3317 |
one_bits_(Floating(1).bits()), |
| 3318 |
infinity_bits_(Floating(Floating::Infinity()).bits()), |
| 3319 |
close_to_positive_zero_( |
| 3320 |
Floating::ReinterpretBits(zero_bits_ + max_ulps_/2)), |
| 3321 |
close_to_negative_zero_( |
| 3322 |
-Floating::ReinterpretBits(zero_bits_ + max_ulps_ - max_ulps_/2)), |
| 3323 |
further_from_negative_zero_(-Floating::ReinterpretBits( |
| 3324 |
zero_bits_ + max_ulps_ + 1 - max_ulps_/2)), |
| 3325 |
close_to_one_(Floating::ReinterpretBits(one_bits_ + max_ulps_)), |
| 3326 |
further_from_one_(Floating::ReinterpretBits(one_bits_ + max_ulps_ + 1)), |
| 3327 |
infinity_(Floating::Infinity()), |
| 3328 |
close_to_infinity_( |
| 3329 |
Floating::ReinterpretBits(infinity_bits_ - max_ulps_)), |
| 3330 |
further_from_infinity_( |
| 3331 |
Floating::ReinterpretBits(infinity_bits_ - max_ulps_ - 1)), |
| 3332 |
max_(Floating::Max()), |
| 3333 |
nan1_(Floating::ReinterpretBits(Floating::kExponentBitMask | 1)), |
| 3334 |
nan2_(Floating::ReinterpretBits(Floating::kExponentBitMask | 200)) { |
| 3335 |
} |
| 3336 |
|
| 3337 |
void TestSize() { |
| 3338 |
EXPECT_EQ(sizeof(RawType), sizeof(Bits)); |
| 3339 |
} |
| 3340 |
|
| 3341 |
// A battery of tests for FloatingEqMatcher::Matches. |
| 3342 |
// matcher_maker is a pointer to a function which creates a FloatingEqMatcher. |
| 3343 |
void TestMatches( |
| 3344 |
testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) { |
| 3345 |
Matcher<RawType> m1 = matcher_maker(0.0); |
| 3346 |
EXPECT_TRUE(m1.Matches(-0.0)); |
| 3347 |
EXPECT_TRUE(m1.Matches(close_to_positive_zero_)); |
| 3348 |
EXPECT_TRUE(m1.Matches(close_to_negative_zero_)); |
| 3349 |
EXPECT_FALSE(m1.Matches(1.0)); |
| 3350 |
|
| 3351 |
Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_); |
| 3352 |
EXPECT_FALSE(m2.Matches(further_from_negative_zero_)); |
| 3353 |
|
| 3354 |
Matcher<RawType> m3 = matcher_maker(1.0); |
| 3355 |
EXPECT_TRUE(m3.Matches(close_to_one_)); |
| 3356 |
EXPECT_FALSE(m3.Matches(further_from_one_)); |
| 3357 |
|
| 3358 |
// Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above. |
| 3359 |
EXPECT_FALSE(m3.Matches(0.0)); |
| 3360 |
|
| 3361 |
Matcher<RawType> m4 = matcher_maker(-infinity_); |
| 3362 |
EXPECT_TRUE(m4.Matches(-close_to_infinity_)); |
| 3363 |
|
| 3364 |
Matcher<RawType> m5 = matcher_maker(infinity_); |
| 3365 |
EXPECT_TRUE(m5.Matches(close_to_infinity_)); |
| 3366 |
|
| 3367 |
// This is interesting as the representations of infinity_ and nan1_ |
| 3368 |
// are only 1 DLP apart. |
| 3369 |
EXPECT_FALSE(m5.Matches(nan1_)); |
| 3370 |
|
| 3371 |
// matcher_maker can produce a Matcher<const RawType&>, which is needed in |
| 3372 |
// some cases. |
| 3373 |
Matcher<const RawType&> m6 = matcher_maker(0.0); |
| 3374 |
EXPECT_TRUE(m6.Matches(-0.0)); |
| 3375 |
EXPECT_TRUE(m6.Matches(close_to_positive_zero_)); |
| 3376 |
EXPECT_FALSE(m6.Matches(1.0)); |
| 3377 |
|
| 3378 |
// matcher_maker can produce a Matcher<RawType&>, which is needed in some |
| 3379 |
// cases. |
| 3380 |
Matcher<RawType&> m7 = matcher_maker(0.0); |
| 3381 |
RawType x = 0.0; |
| 3382 |
EXPECT_TRUE(m7.Matches(x)); |
| 3383 |
x = 0.01f; |
| 3384 |
EXPECT_FALSE(m7.Matches(x)); |
| 3385 |
} |
| 3386 |
|
| 3387 |
// Pre-calculated numbers to be used by the tests. |
| 3388 |
|
| 3389 |
const Bits max_ulps_; |
| 3390 |
|
| 3391 |
const Bits zero_bits_; // The bits that represent 0.0. |
| 3392 |
const Bits one_bits_; // The bits that represent 1.0. |
| 3393 |
const Bits infinity_bits_; // The bits that represent +infinity. |
| 3394 |
|
| 3395 |
// Some numbers close to 0.0. |
| 3396 |
const RawType close_to_positive_zero_; |
| 3397 |
const RawType close_to_negative_zero_; |
| 3398 |
const RawType further_from_negative_zero_; |
| 3399 |
|
| 3400 |
// Some numbers close to 1.0. |
| 3401 |
const RawType close_to_one_; |
| 3402 |
const RawType further_from_one_; |
| 3403 |
|
| 3404 |
// Some numbers close to +infinity. |
| 3405 |
const RawType infinity_; |
| 3406 |
const RawType close_to_infinity_; |
| 3407 |
const RawType further_from_infinity_; |
| 3408 |
|
| 3409 |
// Maximum representable value that's not infinity. |
| 3410 |
const RawType max_; |
| 3411 |
|
| 3412 |
// Some NaNs. |
| 3413 |
const RawType nan1_; |
| 3414 |
const RawType nan2_; |
| 3415 |
}; |
| 3416 |
|
| 3417 |
// Tests floating-point matchers with fixed epsilons. |
| 3418 |
template <typename RawType> |
| 3419 |
class FloatingPointNearTest : public FloatingPointTest<RawType> { |
| 3420 |
protected: |
| 3421 |
typedef FloatingPointTest<RawType> ParentType; |
| 3422 |
|
| 3423 |
// A battery of tests for FloatingEqMatcher::Matches with a fixed epsilon. |
| 3424 |
// matcher_maker is a pointer to a function which creates a FloatingEqMatcher. |
| 3425 |
void TestNearMatches( |
| 3426 |
testing::internal::FloatingEqMatcher<RawType> |
| 3427 |
(*matcher_maker)(RawType, RawType)) { |
| 3428 |
Matcher<RawType> m1 = matcher_maker(0.0, 0.0); |
| 3429 |
EXPECT_TRUE(m1.Matches(0.0)); |
| 3430 |
EXPECT_TRUE(m1.Matches(-0.0)); |
| 3431 |
EXPECT_FALSE(m1.Matches(ParentType::close_to_positive_zero_)); |
| 3432 |
EXPECT_FALSE(m1.Matches(ParentType::close_to_negative_zero_)); |
| 3433 |
EXPECT_FALSE(m1.Matches(1.0)); |
| 3434 |
|
| 3435 |
Matcher<RawType> m2 = matcher_maker(0.0, 1.0); |
| 3436 |
EXPECT_TRUE(m2.Matches(0.0)); |
| 3437 |
EXPECT_TRUE(m2.Matches(-0.0)); |
| 3438 |
EXPECT_TRUE(m2.Matches(1.0)); |
| 3439 |
EXPECT_TRUE(m2.Matches(-1.0)); |
| 3440 |
EXPECT_FALSE(m2.Matches(ParentType::close_to_one_)); |
| 3441 |
EXPECT_FALSE(m2.Matches(-ParentType::close_to_one_)); |
| 3442 |
|
| 3443 |
// Check that inf matches inf, regardless of the of the specified max |
| 3444 |
// absolute error. |
| 3445 |
Matcher<RawType> m3 = matcher_maker(ParentType::infinity_, 0.0); |
| 3446 |
EXPECT_TRUE(m3.Matches(ParentType::infinity_)); |
| 3447 |
EXPECT_FALSE(m3.Matches(ParentType::close_to_infinity_)); |
| 3448 |
EXPECT_FALSE(m3.Matches(-ParentType::infinity_)); |
| 3449 |
|
| 3450 |
Matcher<RawType> m4 = matcher_maker(-ParentType::infinity_, 0.0); |
| 3451 |
EXPECT_TRUE(m4.Matches(-ParentType::infinity_)); |
| 3452 |
EXPECT_FALSE(m4.Matches(-ParentType::close_to_infinity_)); |
| 3453 |
EXPECT_FALSE(m4.Matches(ParentType::infinity_)); |
| 3454 |
|
| 3455 |
// Test various overflow scenarios. |
| 3456 |
Matcher<RawType> m5 = matcher_maker(ParentType::max_, ParentType::max_); |
| 3457 |
EXPECT_TRUE(m5.Matches(ParentType::max_)); |
| 3458 |
EXPECT_FALSE(m5.Matches(-ParentType::max_)); |
| 3459 |
|
| 3460 |
Matcher<RawType> m6 = matcher_maker(-ParentType::max_, ParentType::max_); |
| 3461 |
EXPECT_FALSE(m6.Matches(ParentType::max_)); |
| 3462 |
EXPECT_TRUE(m6.Matches(-ParentType::max_)); |
| 3463 |
|
| 3464 |
Matcher<RawType> m7 = matcher_maker(ParentType::max_, 0); |
| 3465 |
EXPECT_TRUE(m7.Matches(ParentType::max_)); |
| 3466 |
EXPECT_FALSE(m7.Matches(-ParentType::max_)); |
| 3467 |
|
| 3468 |
Matcher<RawType> m8 = matcher_maker(-ParentType::max_, 0); |
| 3469 |
EXPECT_FALSE(m8.Matches(ParentType::max_)); |
| 3470 |
EXPECT_TRUE(m8.Matches(-ParentType::max_)); |
| 3471 |
|
| 3472 |
// The difference between max() and -max() normally overflows to infinity, |
| 3473 |
// but it should still match if the max_abs_error is also infinity. |
| 3474 |
Matcher<RawType> m9 = matcher_maker( |
| 3475 |
ParentType::max_, ParentType::infinity_); |
| 3476 |
EXPECT_TRUE(m8.Matches(-ParentType::max_)); |
| 3477 |
|
| 3478 |
// matcher_maker can produce a Matcher<const RawType&>, which is needed in |
| 3479 |
// some cases. |
| 3480 |
Matcher<const RawType&> m10 = matcher_maker(0.0, 1.0); |
| 3481 |
EXPECT_TRUE(m10.Matches(-0.0)); |
| 3482 |
EXPECT_TRUE(m10.Matches(ParentType::close_to_positive_zero_)); |
| 3483 |
EXPECT_FALSE(m10.Matches(ParentType::close_to_one_)); |
| 3484 |
|
| 3485 |
// matcher_maker can produce a Matcher<RawType&>, which is needed in some |
| 3486 |
// cases. |
| 3487 |
Matcher<RawType&> m11 = matcher_maker(0.0, 1.0); |
| 3488 |
RawType x = 0.0; |
| 3489 |
EXPECT_TRUE(m11.Matches(x)); |
| 3490 |
x = 1.0f; |
| 3491 |
EXPECT_TRUE(m11.Matches(x)); |
| 3492 |
x = -1.0f; |
| 3493 |
EXPECT_TRUE(m11.Matches(x)); |
| 3494 |
x = 1.1f; |
| 3495 |
EXPECT_FALSE(m11.Matches(x)); |
| 3496 |
x = -1.1f; |
| 3497 |
EXPECT_FALSE(m11.Matches(x)); |
| 3498 |
} |
| 3499 |
}; |
| 3500 |
|
| 3501 |
// Instantiate FloatingPointTest for testing floats. |
| 3502 |
typedef FloatingPointTest<float> FloatTest; |
| 3503 |
|
| 3504 |
TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) { |
| 3505 |
TestMatches(&FloatEq); |
| 3506 |
} |
| 3507 |
|
| 3508 |
TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) { |
| 3509 |
TestMatches(&NanSensitiveFloatEq); |
| 3510 |
} |
| 3511 |
|
| 3512 |
TEST_F(FloatTest, FloatEqCannotMatchNaN) { |
| 3513 |
// FloatEq never matches NaN. |
| 3514 |
Matcher<float> m = FloatEq(nan1_); |
| 3515 |
EXPECT_FALSE(m.Matches(nan1_)); |
| 3516 |
EXPECT_FALSE(m.Matches(nan2_)); |
| 3517 |
EXPECT_FALSE(m.Matches(1.0)); |
| 3518 |
} |
| 3519 |
|
| 3520 |
TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) { |
| 3521 |
// NanSensitiveFloatEq will match NaN. |
| 3522 |
Matcher<float> m = NanSensitiveFloatEq(nan1_); |
| 3523 |
EXPECT_TRUE(m.Matches(nan1_)); |
| 3524 |
EXPECT_TRUE(m.Matches(nan2_)); |
| 3525 |
EXPECT_FALSE(m.Matches(1.0)); |
| 3526 |
} |
| 3527 |
|
| 3528 |
TEST_F(FloatTest, FloatEqCanDescribeSelf) { |
| 3529 |
Matcher<float> m1 = FloatEq(2.0f); |
| 3530 |
EXPECT_EQ("is approximately 2", Describe(m1)); |
| 3531 |
EXPECT_EQ("isn't approximately 2", DescribeNegation(m1)); |
| 3532 |
|
| 3533 |
Matcher<float> m2 = FloatEq(0.5f); |
| 3534 |
EXPECT_EQ("is approximately 0.5", Describe(m2)); |
| 3535 |
EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2)); |
| 3536 |
|
| 3537 |
Matcher<float> m3 = FloatEq(nan1_); |
| 3538 |
EXPECT_EQ("never matches", Describe(m3)); |
| 3539 |
EXPECT_EQ("is anything", DescribeNegation(m3)); |
| 3540 |
} |
| 3541 |
|
| 3542 |
TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) { |
| 3543 |
Matcher<float> m1 = NanSensitiveFloatEq(2.0f); |
| 3544 |
EXPECT_EQ("is approximately 2", Describe(m1)); |
| 3545 |
EXPECT_EQ("isn't approximately 2", DescribeNegation(m1)); |
| 3546 |
|
| 3547 |
Matcher<float> m2 = NanSensitiveFloatEq(0.5f); |
| 3548 |
EXPECT_EQ("is approximately 0.5", Describe(m2)); |
| 3549 |
EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2)); |
| 3550 |
|
| 3551 |
Matcher<float> m3 = NanSensitiveFloatEq(nan1_); |
| 3552 |
EXPECT_EQ("is NaN", Describe(m3)); |
| 3553 |
EXPECT_EQ("isn't NaN", DescribeNegation(m3)); |
| 3554 |
} |
| 3555 |
|
| 3556 |
// Instantiate FloatingPointTest for testing floats with a user-specified |
| 3557 |
// max absolute error. |
| 3558 |
typedef FloatingPointNearTest<float> FloatNearTest; |
| 3559 |
|
| 3560 |
TEST_F(FloatNearTest, FloatNearMatches) { |
| 3561 |
TestNearMatches(&FloatNear); |
| 3562 |
} |
| 3563 |
|
| 3564 |
TEST_F(FloatNearTest, NanSensitiveFloatNearApproximatelyMatchesFloats) { |
| 3565 |
TestNearMatches(&NanSensitiveFloatNear); |
| 3566 |
} |
| 3567 |
|
| 3568 |
TEST_F(FloatNearTest, FloatNearCanDescribeSelf) { |
| 3569 |
Matcher<float> m1 = FloatNear(2.0f, 0.5f); |
| 3570 |
EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1)); |
| 3571 |
EXPECT_EQ( |
| 3572 |
"isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1)); |
| 3573 |
|
| 3574 |
Matcher<float> m2 = FloatNear(0.5f, 0.5f); |
| 3575 |
EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2)); |
| 3576 |
EXPECT_EQ( |
| 3577 |
"isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2)); |
| 3578 |
|
| 3579 |
Matcher<float> m3 = FloatNear(nan1_, 0.0); |
| 3580 |
EXPECT_EQ("never matches", Describe(m3)); |
| 3581 |
EXPECT_EQ("is anything", DescribeNegation(m3)); |
| 3582 |
} |
| 3583 |
|
| 3584 |
TEST_F(FloatNearTest, NanSensitiveFloatNearCanDescribeSelf) { |
| 3585 |
Matcher<float> m1 = NanSensitiveFloatNear(2.0f, 0.5f); |
| 3586 |
EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1)); |
| 3587 |
EXPECT_EQ( |
| 3588 |
"isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1)); |
| 3589 |
|
| 3590 |
Matcher<float> m2 = NanSensitiveFloatNear(0.5f, 0.5f); |
| 3591 |
EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2)); |
| 3592 |
EXPECT_EQ( |
| 3593 |
"isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2)); |
| 3594 |
|
| 3595 |
Matcher<float> m3 = NanSensitiveFloatNear(nan1_, 0.1f); |
| 3596 |
EXPECT_EQ("is NaN", Describe(m3)); |
| 3597 |
EXPECT_EQ("isn't NaN", DescribeNegation(m3)); |
| 3598 |
} |
| 3599 |
|
| 3600 |
TEST_F(FloatNearTest, FloatNearCannotMatchNaN) { |
| 3601 |
// FloatNear never matches NaN. |
| 3602 |
Matcher<float> m = FloatNear(ParentType::nan1_, 0.1f); |
| 3603 |
EXPECT_FALSE(m.Matches(nan1_)); |
| 3604 |
EXPECT_FALSE(m.Matches(nan2_)); |
| 3605 |
EXPECT_FALSE(m.Matches(1.0)); |
| 3606 |
} |
| 3607 |
|
| 3608 |
TEST_F(FloatNearTest, NanSensitiveFloatNearCanMatchNaN) { |
| 3609 |
// NanSensitiveFloatNear will match NaN. |
| 3610 |
Matcher<float> m = NanSensitiveFloatNear(nan1_, 0.1f); |
| 3611 |
EXPECT_TRUE(m.Matches(nan1_)); |
| 3612 |
EXPECT_TRUE(m.Matches(nan2_)); |
| 3613 |
EXPECT_FALSE(m.Matches(1.0)); |
| 3614 |
} |
| 3615 |
|
| 3616 |
// Instantiate FloatingPointTest for testing doubles. |
| 3617 |
typedef FloatingPointTest<double> DoubleTest; |
| 3618 |
|
| 3619 |
TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) { |
| 3620 |
TestMatches(&DoubleEq); |
| 3621 |
} |
| 3622 |
|
| 3623 |
TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) { |
| 3624 |
TestMatches(&NanSensitiveDoubleEq); |
| 3625 |
} |
| 3626 |
|
| 3627 |
TEST_F(DoubleTest, DoubleEqCannotMatchNaN) { |
| 3628 |
// DoubleEq never matches NaN. |
| 3629 |
Matcher<double> m = DoubleEq(nan1_); |
| 3630 |
EXPECT_FALSE(m.Matches(nan1_)); |
| 3631 |
EXPECT_FALSE(m.Matches(nan2_)); |
| 3632 |
EXPECT_FALSE(m.Matches(1.0)); |
| 3633 |
} |
| 3634 |
|
| 3635 |
TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) { |
| 3636 |
// NanSensitiveDoubleEq will match NaN. |
| 3637 |
Matcher<double> m = NanSensitiveDoubleEq(nan1_); |
| 3638 |
EXPECT_TRUE(m.Matches(nan1_)); |
| 3639 |
EXPECT_TRUE(m.Matches(nan2_)); |
| 3640 |
EXPECT_FALSE(m.Matches(1.0)); |
| 3641 |
} |
| 3642 |
|
| 3643 |
TEST_F(DoubleTest, DoubleEqCanDescribeSelf) { |
| 3644 |
Matcher<double> m1 = DoubleEq(2.0); |
| 3645 |
EXPECT_EQ("is approximately 2", Describe(m1)); |
| 3646 |
EXPECT_EQ("isn't approximately 2", DescribeNegation(m1)); |
| 3647 |
|
| 3648 |
Matcher<double> m2 = DoubleEq(0.5); |
| 3649 |
EXPECT_EQ("is approximately 0.5", Describe(m2)); |
| 3650 |
EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2)); |
| 3651 |
|
| 3652 |
Matcher<double> m3 = DoubleEq(nan1_); |
| 3653 |
EXPECT_EQ("never matches", Describe(m3)); |
| 3654 |
EXPECT_EQ("is anything", DescribeNegation(m3)); |
| 3655 |
} |
| 3656 |
|
| 3657 |
TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) { |
| 3658 |
Matcher<double> m1 = NanSensitiveDoubleEq(2.0); |
| 3659 |
EXPECT_EQ("is approximately 2", Describe(m1)); |
| 3660 |
EXPECT_EQ("isn't approximately 2", DescribeNegation(m1)); |
| 3661 |
|
| 3662 |
Matcher<double> m2 = NanSensitiveDoubleEq(0.5); |
| 3663 |
EXPECT_EQ("is approximately 0.5", Describe(m2)); |
| 3664 |
EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2)); |
| 3665 |
|
| 3666 |
Matcher<double> m3 = NanSensitiveDoubleEq(nan1_); |
| 3667 |
EXPECT_EQ("is NaN", Describe(m3)); |
| 3668 |
EXPECT_EQ("isn't NaN", DescribeNegation(m3)); |
| 3669 |
} |
| 3670 |
|
| 3671 |
// Instantiate FloatingPointTest for testing floats with a user-specified |
| 3672 |
// max absolute error. |
| 3673 |
typedef FloatingPointNearTest<double> DoubleNearTest; |
| 3674 |
|
| 3675 |
TEST_F(DoubleNearTest, DoubleNearMatches) { |
| 3676 |
TestNearMatches(&DoubleNear); |
| 3677 |
} |
| 3678 |
|
| 3679 |
TEST_F(DoubleNearTest, NanSensitiveDoubleNearApproximatelyMatchesDoubles) { |
| 3680 |
TestNearMatches(&NanSensitiveDoubleNear); |
| 3681 |
} |
| 3682 |
|
| 3683 |
TEST_F(DoubleNearTest, DoubleNearCanDescribeSelf) { |
| 3684 |
Matcher<double> m1 = DoubleNear(2.0, 0.5); |
| 3685 |
EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1)); |
| 3686 |
EXPECT_EQ( |
| 3687 |
"isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1)); |
| 3688 |
|
| 3689 |
Matcher<double> m2 = DoubleNear(0.5, 0.5); |
| 3690 |
EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2)); |
| 3691 |
EXPECT_EQ( |
| 3692 |
"isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2)); |
| 3693 |
|
| 3694 |
Matcher<double> m3 = DoubleNear(nan1_, 0.0); |
| 3695 |
EXPECT_EQ("never matches", Describe(m3)); |
| 3696 |
EXPECT_EQ("is anything", DescribeNegation(m3)); |
| 3697 |
} |
| 3698 |
|
| 3699 |
TEST_F(DoubleNearTest, ExplainsResultWhenMatchFails) { |
| 3700 |
EXPECT_EQ("", Explain(DoubleNear(2.0, 0.1), 2.05)); |
| 3701 |
EXPECT_EQ("which is 0.2 from 2", Explain(DoubleNear(2.0, 0.1), 2.2)); |
| 3702 |
EXPECT_EQ("which is -0.3 from 2", Explain(DoubleNear(2.0, 0.1), 1.7)); |
| 3703 |
|
| 3704 |
const std::string explanation = |
| 3705 |
Explain(DoubleNear(2.1, 1e-10), 2.1 + 1.2e-10); |
| 3706 |
// Different C++ implementations may print floating-point numbers |
| 3707 |
// slightly differently. |
| 3708 |
EXPECT_TRUE(explanation == "which is 1.2e-10 from 2.1" || // GCC |
| 3709 |
explanation == "which is 1.2e-010 from 2.1") // MSVC |
| 3710 |
<< " where explanation is \"" << explanation << "\"."; |
| 3711 |
} |
| 3712 |
|
| 3713 |
TEST_F(DoubleNearTest, NanSensitiveDoubleNearCanDescribeSelf) { |
| 3714 |
Matcher<double> m1 = NanSensitiveDoubleNear(2.0, 0.5); |
| 3715 |
EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1)); |
| 3716 |
EXPECT_EQ( |
| 3717 |
"isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1)); |
| 3718 |
|
| 3719 |
Matcher<double> m2 = NanSensitiveDoubleNear(0.5, 0.5); |
| 3720 |
EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2)); |
| 3721 |
EXPECT_EQ( |
| 3722 |
"isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2)); |
| 3723 |
|
| 3724 |
Matcher<double> m3 = NanSensitiveDoubleNear(nan1_, 0.1); |
| 3725 |
EXPECT_EQ("is NaN", Describe(m3)); |
| 3726 |
EXPECT_EQ("isn't NaN", DescribeNegation(m3)); |
| 3727 |
} |
| 3728 |
|
| 3729 |
TEST_F(DoubleNearTest, DoubleNearCannotMatchNaN) { |
| 3730 |
// DoubleNear never matches NaN. |
| 3731 |
Matcher<double> m = DoubleNear(ParentType::nan1_, 0.1); |
| 3732 |
EXPECT_FALSE(m.Matches(nan1_)); |
| 3733 |
EXPECT_FALSE(m.Matches(nan2_)); |
| 3734 |
EXPECT_FALSE(m.Matches(1.0)); |
| 3735 |
} |
| 3736 |
|
| 3737 |
TEST_F(DoubleNearTest, NanSensitiveDoubleNearCanMatchNaN) { |
| 3738 |
// NanSensitiveDoubleNear will match NaN. |
| 3739 |
Matcher<double> m = NanSensitiveDoubleNear(nan1_, 0.1); |
| 3740 |
EXPECT_TRUE(m.Matches(nan1_)); |
| 3741 |
EXPECT_TRUE(m.Matches(nan2_)); |
| 3742 |
EXPECT_FALSE(m.Matches(1.0)); |
| 3743 |
} |
| 3744 |
|
| 3745 |
TEST(PointeeTest, RawPointer) { |
| 3746 |
const Matcher<int*> m = Pointee(Ge(0)); |
| 3747 |
|
| 3748 |
int n = 1; |
| 3749 |
EXPECT_TRUE(m.Matches(&n)); |
| 3750 |
n = -1; |
| 3751 |
EXPECT_FALSE(m.Matches(&n)); |
| 3752 |
EXPECT_FALSE(m.Matches(nullptr)); |
| 3753 |
} |
| 3754 |
|
| 3755 |
TEST(PointeeTest, RawPointerToConst) { |
| 3756 |
const Matcher<const double*> m = Pointee(Ge(0)); |
| 3757 |
|
| 3758 |
double x = 1; |
| 3759 |
EXPECT_TRUE(m.Matches(&x)); |
| 3760 |
x = -1; |
| 3761 |
EXPECT_FALSE(m.Matches(&x)); |
| 3762 |
EXPECT_FALSE(m.Matches(nullptr)); |
| 3763 |
} |
| 3764 |
|
| 3765 |
TEST(PointeeTest, ReferenceToConstRawPointer) { |
| 3766 |
const Matcher<int* const &> m = Pointee(Ge(0)); |
| 3767 |
|
| 3768 |
int n = 1; |
| 3769 |
EXPECT_TRUE(m.Matches(&n)); |
| 3770 |
n = -1; |
| 3771 |
EXPECT_FALSE(m.Matches(&n)); |
| 3772 |
EXPECT_FALSE(m.Matches(nullptr)); |
| 3773 |
} |
| 3774 |
|
| 3775 |
TEST(PointeeTest, ReferenceToNonConstRawPointer) { |
| 3776 |
const Matcher<double* &> m = Pointee(Ge(0)); |
| 3777 |
|
| 3778 |
double x = 1.0; |
| 3779 |
double* p = &x; |
| 3780 |
EXPECT_TRUE(m.Matches(p)); |
| 3781 |
x = -1; |
| 3782 |
EXPECT_FALSE(m.Matches(p)); |
| 3783 |
p = nullptr; |
| 3784 |
EXPECT_FALSE(m.Matches(p)); |
| 3785 |
} |
| 3786 |
|
| 3787 |
TEST(PointeeTest, SmartPointer) { |
| 3788 |
const Matcher<std::unique_ptr<int>> m = Pointee(Ge(0)); |
| 3789 |
|
| 3790 |
std::unique_ptr<int> n(new int(1)); |
| 3791 |
EXPECT_TRUE(m.Matches(n)); |
| 3792 |
} |
| 3793 |
|
| 3794 |
TEST(PointeeTest, SmartPointerToConst) { |
| 3795 |
const Matcher<std::unique_ptr<const int>> m = Pointee(Ge(0)); |
| 3796 |
|
| 3797 |
// There's no implicit conversion from unique_ptr<int> to const |
| 3798 |
// unique_ptr<const int>, so we must pass a unique_ptr<const int> into the |
| 3799 |
// matcher. |
| 3800 |
std::unique_ptr<const int> n(new int(1)); |
| 3801 |
EXPECT_TRUE(m.Matches(n)); |
| 3802 |
} |
| 3803 |
|
| 3804 |
TEST(PointerTest, RawPointer) { |
| 3805 |
int n = 1; |
| 3806 |
const Matcher<int*> m = Pointer(Eq(&n)); |
| 3807 |
|
| 3808 |
EXPECT_TRUE(m.Matches(&n)); |
| 3809 |
|
| 3810 |
int* p = nullptr; |
| 3811 |
EXPECT_FALSE(m.Matches(p)); |
| 3812 |
EXPECT_FALSE(m.Matches(nullptr)); |
| 3813 |
} |
| 3814 |
|
| 3815 |
TEST(PointerTest, RawPointerToConst) { |
| 3816 |
int n = 1; |
| 3817 |
const Matcher<const int*> m = Pointer(Eq(&n)); |
| 3818 |
|
| 3819 |
EXPECT_TRUE(m.Matches(&n)); |
| 3820 |
|
| 3821 |
int* p = nullptr; |
| 3822 |
EXPECT_FALSE(m.Matches(p)); |
| 3823 |
EXPECT_FALSE(m.Matches(nullptr)); |
| 3824 |
} |
| 3825 |
|
| 3826 |
TEST(PointerTest, SmartPointer) { |
| 3827 |
std::unique_ptr<int> n(new int(10)); |
| 3828 |
int* raw_n = n.get(); |
| 3829 |
const Matcher<std::unique_ptr<int>> m = Pointer(Eq(raw_n)); |
| 3830 |
|
| 3831 |
EXPECT_TRUE(m.Matches(n)); |
| 3832 |
} |
| 3833 |
|
| 3834 |
TEST(PointerTest, SmartPointerToConst) { |
| 3835 |
std::unique_ptr<const int> n(new int(10)); |
| 3836 |
const int* raw_n = n.get(); |
| 3837 |
const Matcher<std::unique_ptr<const int>> m = Pointer(Eq(raw_n)); |
| 3838 |
|
| 3839 |
// There's no implicit conversion from unique_ptr<int> to const |
| 3840 |
// unique_ptr<const int>, so we must pass a unique_ptr<const int> into the |
| 3841 |
// matcher. |
| 3842 |
std::unique_ptr<const int> p(new int(10)); |
| 3843 |
EXPECT_FALSE(m.Matches(p)); |
| 3844 |
} |
| 3845 |
|
| 3846 |
TEST(AddressTest, NonConst) { |
| 3847 |
int n = 1; |
| 3848 |
const Matcher<int> m = Address(Eq(&n)); |
| 3849 |
|
| 3850 |
EXPECT_TRUE(m.Matches(n)); |
| 3851 |
|
| 3852 |
int other = 5; |
| 3853 |
|
| 3854 |
EXPECT_FALSE(m.Matches(other)); |
| 3855 |
|
| 3856 |
int& n_ref = n; |
| 3857 |
|
| 3858 |
EXPECT_TRUE(m.Matches(n_ref)); |
| 3859 |
} |
| 3860 |
|
| 3861 |
TEST(AddressTest, Const) { |
| 3862 |
const int n = 1; |
| 3863 |
const Matcher<int> m = Address(Eq(&n)); |
| 3864 |
|
| 3865 |
EXPECT_TRUE(m.Matches(n)); |
| 3866 |
|
| 3867 |
int other = 5; |
| 3868 |
|
| 3869 |
EXPECT_FALSE(m.Matches(other)); |
| 3870 |
} |
| 3871 |
|
| 3872 |
TEST(AddressTest, MatcherDoesntCopy) { |
| 3873 |
std::unique_ptr<int> n(new int(1)); |
| 3874 |
const Matcher<std::unique_ptr<int>> m = Address(Eq(&n)); |
| 3875 |
|
| 3876 |
EXPECT_TRUE(m.Matches(n)); |
| 3877 |
} |
| 3878 |
|
| 3879 |
TEST(AddressTest, Describe) { |
| 3880 |
Matcher<int> matcher = Address(_); |
| 3881 |
EXPECT_EQ("has address that is anything", Describe(matcher)); |
| 3882 |
EXPECT_EQ("does not have address that is anything", |
| 3883 |
DescribeNegation(matcher)); |
| 3884 |
} |
| 3885 |
|
| 3886 |
MATCHER_P(FieldIIs, inner_matcher, "") { |
| 3887 |
return ExplainMatchResult(inner_matcher, arg.i, result_listener); |
| 3888 |
} |
| 3889 |
|
| 3890 |
#if GTEST_HAS_RTTI |
| 3891 |
TEST(WhenDynamicCastToTest, SameType) { |
| 3892 |
Derived derived; |
| 3893 |
derived.i = 4; |
| 3894 |
|
| 3895 |
// Right type. A pointer is passed down. |
| 3896 |
Base* as_base_ptr = &derived; |
| 3897 |
EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(Not(IsNull()))); |
| 3898 |
EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(Pointee(FieldIIs(4)))); |
| 3899 |
EXPECT_THAT(as_base_ptr, |
| 3900 |
Not(WhenDynamicCastTo<Derived*>(Pointee(FieldIIs(5))))); |
| 3901 |
} |
| 3902 |
|
| 3903 |
TEST(WhenDynamicCastToTest, WrongTypes) { |
| 3904 |
Base base; |
| 3905 |
Derived derived; |
| 3906 |
OtherDerived other_derived; |
| 3907 |
|
| 3908 |
// Wrong types. NULL is passed. |
| 3909 |
EXPECT_THAT(&base, Not(WhenDynamicCastTo<Derived*>(Pointee(_)))); |
| 3910 |
EXPECT_THAT(&base, WhenDynamicCastTo<Derived*>(IsNull())); |
| 3911 |
Base* as_base_ptr = &derived; |
| 3912 |
EXPECT_THAT(as_base_ptr, Not(WhenDynamicCastTo<OtherDerived*>(Pointee(_)))); |
| 3913 |
EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<OtherDerived*>(IsNull())); |
| 3914 |
as_base_ptr = &other_derived; |
| 3915 |
EXPECT_THAT(as_base_ptr, Not(WhenDynamicCastTo<Derived*>(Pointee(_)))); |
| 3916 |
EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(IsNull())); |
| 3917 |
} |
| 3918 |
|
| 3919 |
TEST(WhenDynamicCastToTest, AlreadyNull) { |
| 3920 |
// Already NULL. |
| 3921 |
Base* as_base_ptr = nullptr; |
| 3922 |
EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(IsNull())); |
| 3923 |
} |
| 3924 |
|
| 3925 |
struct AmbiguousCastTypes { |
| 3926 |
class VirtualDerived : public virtual Base {}; |
| 3927 |
class DerivedSub1 : public VirtualDerived {}; |
| 3928 |
class DerivedSub2 : public VirtualDerived {}; |
| 3929 |
class ManyDerivedInHierarchy : public DerivedSub1, public DerivedSub2 {}; |
| 3930 |
}; |
| 3931 |
|
| 3932 |
TEST(WhenDynamicCastToTest, AmbiguousCast) { |
| 3933 |
AmbiguousCastTypes::DerivedSub1 sub1; |
| 3934 |
AmbiguousCastTypes::ManyDerivedInHierarchy many_derived; |
| 3935 |
// Multiply derived from Base. dynamic_cast<> returns NULL. |
| 3936 |
Base* as_base_ptr = |
| 3937 |
static_cast<AmbiguousCastTypes::DerivedSub1*>(&many_derived); |
| 3938 |
EXPECT_THAT(as_base_ptr, |
| 3939 |
WhenDynamicCastTo<AmbiguousCastTypes::VirtualDerived*>(IsNull())); |
| 3940 |
as_base_ptr = &sub1; |
| 3941 |
EXPECT_THAT( |
| 3942 |
as_base_ptr, |
| 3943 |
WhenDynamicCastTo<AmbiguousCastTypes::VirtualDerived*>(Not(IsNull()))); |
| 3944 |
} |
| 3945 |
|
| 3946 |
TEST(WhenDynamicCastToTest, Describe) { |
| 3947 |
Matcher<Base*> matcher = WhenDynamicCastTo<Derived*>(Pointee(_)); |
| 3948 |
const std::string prefix = |
| 3949 |
"when dynamic_cast to " + internal::GetTypeName<Derived*>() + ", "; |
| 3950 |
EXPECT_EQ(prefix + "points to a value that is anything", Describe(matcher)); |
| 3951 |
EXPECT_EQ(prefix + "does not point to a value that is anything", |
| 3952 |
DescribeNegation(matcher)); |
| 3953 |
} |
| 3954 |
|
| 3955 |
TEST(WhenDynamicCastToTest, Explain) { |
| 3956 |
Matcher<Base*> matcher = WhenDynamicCastTo<Derived*>(Pointee(_)); |
| 3957 |
Base* null = nullptr; |
| 3958 |
EXPECT_THAT(Explain(matcher, null), HasSubstr("NULL")); |
| 3959 |
Derived derived; |
| 3960 |
EXPECT_TRUE(matcher.Matches(&derived)); |
| 3961 |
EXPECT_THAT(Explain(matcher, &derived), HasSubstr("which points to ")); |
| 3962 |
|
| 3963 |
// With references, the matcher itself can fail. Test for that one. |
| 3964 |
Matcher<const Base&> ref_matcher = WhenDynamicCastTo<const OtherDerived&>(_); |
| 3965 |
EXPECT_THAT(Explain(ref_matcher, derived), |
| 3966 |
HasSubstr("which cannot be dynamic_cast")); |
| 3967 |
} |
| 3968 |
|
| 3969 |
TEST(WhenDynamicCastToTest, GoodReference) { |
| 3970 |
Derived derived; |
| 3971 |
derived.i = 4; |
| 3972 |
Base& as_base_ref = derived; |
| 3973 |
EXPECT_THAT(as_base_ref, WhenDynamicCastTo<const Derived&>(FieldIIs(4))); |
| 3974 |
EXPECT_THAT(as_base_ref, WhenDynamicCastTo<const Derived&>(Not(FieldIIs(5)))); |
| 3975 |
} |
| 3976 |
|
| 3977 |
TEST(WhenDynamicCastToTest, BadReference) { |
| 3978 |
Derived derived; |
| 3979 |
Base& as_base_ref = derived; |
| 3980 |
EXPECT_THAT(as_base_ref, Not(WhenDynamicCastTo<const OtherDerived&>(_))); |
| 3981 |
} |
| 3982 |
#endif // GTEST_HAS_RTTI |
| 3983 |
|
| 3984 |
// Minimal const-propagating pointer. |
| 3985 |
template <typename T> |
| 3986 |
class ConstPropagatingPtr { |
| 3987 |
public: |
| 3988 |
typedef T element_type; |
| 3989 |
|
| 3990 |
ConstPropagatingPtr() : val_() {} |
| 3991 |
explicit ConstPropagatingPtr(T* t) : val_(t) {} |
| 3992 |
ConstPropagatingPtr(const ConstPropagatingPtr& other) : val_(other.val_) {} |
| 3993 |
|
| 3994 |
T* get() { return val_; } |
| 3995 |
T& operator*() { return *val_; } |
| 3996 |
// Most smart pointers return non-const T* and T& from the next methods. |
| 3997 |
const T* get() const { return val_; } |
| 3998 |
const T& operator*() const { return *val_; } |
| 3999 |
|
| 4000 |
private: |
| 4001 |
T* val_; |
| 4002 |
}; |
| 4003 |
|
| 4004 |
TEST(PointeeTest, WorksWithConstPropagatingPointers) { |
| 4005 |
const Matcher< ConstPropagatingPtr<int> > m = Pointee(Lt(5)); |
| 4006 |
int three = 3; |
| 4007 |
const ConstPropagatingPtr<int> co(&three); |
| 4008 |
ConstPropagatingPtr<int> o(&three); |
| 4009 |
EXPECT_TRUE(m.Matches(o)); |
| 4010 |
EXPECT_TRUE(m.Matches(co)); |
| 4011 |
*o = 6; |
| 4012 |
EXPECT_FALSE(m.Matches(o)); |
| 4013 |
EXPECT_FALSE(m.Matches(ConstPropagatingPtr<int>())); |
| 4014 |
} |
| 4015 |
|
| 4016 |
TEST(PointeeTest, NeverMatchesNull) { |
| 4017 |
const Matcher<const char*> m = Pointee(_); |
| 4018 |
EXPECT_FALSE(m.Matches(nullptr)); |
| 4019 |
} |
| 4020 |
|
| 4021 |
// Tests that we can write Pointee(value) instead of Pointee(Eq(value)). |
| 4022 |
TEST(PointeeTest, MatchesAgainstAValue) { |
| 4023 |
const Matcher<int*> m = Pointee(5); |
| 4024 |
|
| 4025 |
int n = 5; |
| 4026 |
EXPECT_TRUE(m.Matches(&n)); |
| 4027 |
n = -1; |
| 4028 |
EXPECT_FALSE(m.Matches(&n)); |
| 4029 |
EXPECT_FALSE(m.Matches(nullptr)); |
| 4030 |
} |
| 4031 |
|
| 4032 |
TEST(PointeeTest, CanDescribeSelf) { |
| 4033 |
const Matcher<int*> m = Pointee(Gt(3)); |
| 4034 |
EXPECT_EQ("points to a value that is > 3", Describe(m)); |
| 4035 |
EXPECT_EQ("does not point to a value that is > 3", |
| 4036 |
DescribeNegation(m)); |
| 4037 |
} |
| 4038 |
|
| 4039 |
TEST(PointeeTest, CanExplainMatchResult) { |
| 4040 |
const Matcher<const std::string*> m = Pointee(StartsWith("Hi")); |
| 4041 |
|
| 4042 |
EXPECT_EQ("", Explain(m, static_cast<const std::string*>(nullptr))); |
| 4043 |
|
| 4044 |
const Matcher<long*> m2 = Pointee(GreaterThan(1)); // NOLINT |
| 4045 |
long n = 3; // NOLINT |
| 4046 |
EXPECT_EQ("which points to 3" + OfType("long") + ", which is 2 more than 1", |
| 4047 |
Explain(m2, &n)); |
| 4048 |
} |
| 4049 |
|
| 4050 |
TEST(PointeeTest, AlwaysExplainsPointee) { |
| 4051 |
const Matcher<int*> m = Pointee(0); |
| 4052 |
int n = 42; |
| 4053 |
EXPECT_EQ("which points to 42" + OfType("int"), Explain(m, &n)); |
| 4054 |
} |
| 4055 |
|
| 4056 |
// An uncopyable class. |
| 4057 |
class Uncopyable { |
| 4058 |
public: |
| 4059 |
Uncopyable() : value_(-1) {} |
| 4060 |
explicit Uncopyable(int a_value) : value_(a_value) {} |
| 4061 |
|
| 4062 |
int value() const { return value_; } |
| 4063 |
void set_value(int i) { value_ = i; } |
| 4064 |
|
| 4065 |
private: |
| 4066 |
int value_; |
| 4067 |
GTEST_DISALLOW_COPY_AND_ASSIGN_(Uncopyable); |
| 4068 |
}; |
| 4069 |
|
| 4070 |
// Returns true if and only if x.value() is positive. |
| 4071 |
bool ValueIsPositive(const Uncopyable& x) { return x.value() > 0; } |
| 4072 |
|
| 4073 |
MATCHER_P(UncopyableIs, inner_matcher, "") { |
| 4074 |
return ExplainMatchResult(inner_matcher, arg.value(), result_listener); |
| 4075 |
} |
| 4076 |
|
| 4077 |
// A user-defined struct for testing Field(). |
| 4078 |
struct AStruct { |
| 4079 |
AStruct() : x(0), y(1.0), z(5), p(nullptr) {} |
| 4080 |
AStruct(const AStruct& rhs) |
| 4081 |
: x(rhs.x), y(rhs.y), z(rhs.z.value()), p(rhs.p) {} |
| 4082 |
|
| 4083 |
int x; // A non-const field. |
| 4084 |
const double y; // A const field. |
| 4085 |
Uncopyable z; // An uncopyable field. |
| 4086 |
const char* p; // A pointer field. |
| 4087 |
}; |
| 4088 |
|
| 4089 |
// A derived struct for testing Field(). |
| 4090 |
struct DerivedStruct : public AStruct { |
| 4091 |
char ch; |
| 4092 |
}; |
| 4093 |
|
| 4094 |
// Tests that Field(&Foo::field, ...) works when field is non-const. |
| 4095 |
TEST(FieldTest, WorksForNonConstField) { |
| 4096 |
Matcher<AStruct> m = Field(&AStruct::x, Ge(0)); |
| 4097 |
Matcher<AStruct> m_with_name = Field("x", &AStruct::x, Ge(0)); |
| 4098 |
|
| 4099 |
AStruct a; |
| 4100 |
EXPECT_TRUE(m.Matches(a)); |
| 4101 |
EXPECT_TRUE(m_with_name.Matches(a)); |
| 4102 |
a.x = -1; |
| 4103 |
EXPECT_FALSE(m.Matches(a)); |
| 4104 |
EXPECT_FALSE(m_with_name.Matches(a)); |
| 4105 |
} |
| 4106 |
|
| 4107 |
// Tests that Field(&Foo::field, ...) works when field is const. |
| 4108 |
TEST(FieldTest, WorksForConstField) { |
| 4109 |
AStruct a; |
| 4110 |
|
| 4111 |
Matcher<AStruct> m = Field(&AStruct::y, Ge(0.0)); |
| 4112 |
Matcher<AStruct> m_with_name = Field("y", &AStruct::y, Ge(0.0)); |
| 4113 |
EXPECT_TRUE(m.Matches(a)); |
| 4114 |
EXPECT_TRUE(m_with_name.Matches(a)); |
| 4115 |
m = Field(&AStruct::y, Le(0.0)); |
| 4116 |
m_with_name = Field("y", &AStruct::y, Le(0.0)); |
| 4117 |
EXPECT_FALSE(m.Matches(a)); |
| 4118 |
EXPECT_FALSE(m_with_name.Matches(a)); |
| 4119 |
} |
| 4120 |
|
| 4121 |
// Tests that Field(&Foo::field, ...) works when field is not copyable. |
| 4122 |
TEST(FieldTest, WorksForUncopyableField) { |
| 4123 |
AStruct a; |
| 4124 |
|
| 4125 |
Matcher<AStruct> m = Field(&AStruct::z, Truly(ValueIsPositive)); |
| 4126 |
EXPECT_TRUE(m.Matches(a)); |
| 4127 |
m = Field(&AStruct::z, Not(Truly(ValueIsPositive))); |
| 4128 |
EXPECT_FALSE(m.Matches(a)); |
| 4129 |
} |
| 4130 |
|
| 4131 |
// Tests that Field(&Foo::field, ...) works when field is a pointer. |
| 4132 |
TEST(FieldTest, WorksForPointerField) { |
| 4133 |
// Matching against NULL. |
| 4134 |
Matcher<AStruct> m = Field(&AStruct::p, static_cast<const char*>(nullptr)); |
| 4135 |
AStruct a; |
| 4136 |
EXPECT_TRUE(m.Matches(a)); |
| 4137 |
a.p = "hi"; |
| 4138 |
EXPECT_FALSE(m.Matches(a)); |
| 4139 |
|
| 4140 |
// Matching a pointer that is not NULL. |
| 4141 |
m = Field(&AStruct::p, StartsWith("hi")); |
| 4142 |
a.p = "hill"; |
| 4143 |
EXPECT_TRUE(m.Matches(a)); |
| 4144 |
a.p = "hole"; |
| 4145 |
EXPECT_FALSE(m.Matches(a)); |
| 4146 |
} |
| 4147 |
|
| 4148 |
// Tests that Field() works when the object is passed by reference. |
| 4149 |
TEST(FieldTest, WorksForByRefArgument) { |
| 4150 |
Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0)); |
| 4151 |
|
| 4152 |
AStruct a; |
| 4153 |
EXPECT_TRUE(m.Matches(a)); |
| 4154 |
a.x = -1; |
| 4155 |
EXPECT_FALSE(m.Matches(a)); |
| 4156 |
} |
| 4157 |
|
| 4158 |
// Tests that Field(&Foo::field, ...) works when the argument's type |
| 4159 |
// is a sub-type of Foo. |
| 4160 |
TEST(FieldTest, WorksForArgumentOfSubType) { |
| 4161 |
// Note that the matcher expects DerivedStruct but we say AStruct |
| 4162 |
// inside Field(). |
| 4163 |
Matcher<const DerivedStruct&> m = Field(&AStruct::x, Ge(0)); |
| 4164 |
|
| 4165 |
DerivedStruct d; |
| 4166 |
EXPECT_TRUE(m.Matches(d)); |
| 4167 |
d.x = -1; |
| 4168 |
EXPECT_FALSE(m.Matches(d)); |
| 4169 |
} |
| 4170 |
|
| 4171 |
// Tests that Field(&Foo::field, m) works when field's type and m's |
| 4172 |
// argument type are compatible but not the same. |
| 4173 |
TEST(FieldTest, WorksForCompatibleMatcherType) { |
| 4174 |
// The field is an int, but the inner matcher expects a signed char. |
| 4175 |
Matcher<const AStruct&> m = Field(&AStruct::x, |
| 4176 |
Matcher<signed char>(Ge(0))); |
| 4177 |
|
| 4178 |
AStruct a; |
| 4179 |
EXPECT_TRUE(m.Matches(a)); |
| 4180 |
a.x = -1; |
| 4181 |
EXPECT_FALSE(m.Matches(a)); |
| 4182 |
} |
| 4183 |
|
| 4184 |
// Tests that Field() can describe itself. |
| 4185 |
TEST(FieldTest, CanDescribeSelf) { |
| 4186 |
Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0)); |
| 4187 |
|
| 4188 |
EXPECT_EQ("is an object whose given field is >= 0", Describe(m)); |
| 4189 |
EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m)); |
| 4190 |
} |
| 4191 |
|
| 4192 |
TEST(FieldTest, CanDescribeSelfWithFieldName) { |
| 4193 |
Matcher<const AStruct&> m = Field("field_name", &AStruct::x, Ge(0)); |
| 4194 |
|
| 4195 |
EXPECT_EQ("is an object whose field `field_name` is >= 0", Describe(m)); |
| 4196 |
EXPECT_EQ("is an object whose field `field_name` isn't >= 0", |
| 4197 |
DescribeNegation(m)); |
| 4198 |
} |
| 4199 |
|
| 4200 |
// Tests that Field() can explain the match result. |
| 4201 |
TEST(FieldTest, CanExplainMatchResult) { |
| 4202 |
Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0)); |
| 4203 |
|
| 4204 |
AStruct a; |
| 4205 |
a.x = 1; |
| 4206 |
EXPECT_EQ("whose given field is 1" + OfType("int"), Explain(m, a)); |
| 4207 |
|
| 4208 |
m = Field(&AStruct::x, GreaterThan(0)); |
| 4209 |
EXPECT_EQ( |
| 4210 |
"whose given field is 1" + OfType("int") + ", which is 1 more than 0", |
| 4211 |
Explain(m, a)); |
| 4212 |
} |
| 4213 |
|
| 4214 |
TEST(FieldTest, CanExplainMatchResultWithFieldName) { |
| 4215 |
Matcher<const AStruct&> m = Field("field_name", &AStruct::x, Ge(0)); |
| 4216 |
|
| 4217 |
AStruct a; |
| 4218 |
a.x = 1; |
| 4219 |
EXPECT_EQ("whose field `field_name` is 1" + OfType("int"), Explain(m, a)); |
| 4220 |
|
| 4221 |
m = Field("field_name", &AStruct::x, GreaterThan(0)); |
| 4222 |
EXPECT_EQ("whose field `field_name` is 1" + OfType("int") + |
| 4223 |
", which is 1 more than 0", |
| 4224 |
Explain(m, a)); |
| 4225 |
} |
| 4226 |
|
| 4227 |
// Tests that Field() works when the argument is a pointer to const. |
| 4228 |
TEST(FieldForPointerTest, WorksForPointerToConst) { |
| 4229 |
Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0)); |
| 4230 |
|
| 4231 |
AStruct a; |
| 4232 |
EXPECT_TRUE(m.Matches(&a)); |
| 4233 |
a.x = -1; |
| 4234 |
EXPECT_FALSE(m.Matches(&a)); |
| 4235 |
} |
| 4236 |
|
| 4237 |
// Tests that Field() works when the argument is a pointer to non-const. |
| 4238 |
TEST(FieldForPointerTest, WorksForPointerToNonConst) { |
| 4239 |
Matcher<AStruct*> m = Field(&AStruct::x, Ge(0)); |
| 4240 |
|
| 4241 |
AStruct a; |
| 4242 |
EXPECT_TRUE(m.Matches(&a)); |
| 4243 |
a.x = -1; |
| 4244 |
EXPECT_FALSE(m.Matches(&a)); |
| 4245 |
} |
| 4246 |
|
| 4247 |
// Tests that Field() works when the argument is a reference to a const pointer. |
| 4248 |
TEST(FieldForPointerTest, WorksForReferenceToConstPointer) { |
| 4249 |
Matcher<AStruct* const&> m = Field(&AStruct::x, Ge(0)); |
| 4250 |
|
| 4251 |
AStruct a; |
| 4252 |
EXPECT_TRUE(m.Matches(&a)); |
| 4253 |
a.x = -1; |
| 4254 |
EXPECT_FALSE(m.Matches(&a)); |
| 4255 |
} |
| 4256 |
|
| 4257 |
// Tests that Field() does not match the NULL pointer. |
| 4258 |
TEST(FieldForPointerTest, DoesNotMatchNull) { |
| 4259 |
Matcher<const AStruct*> m = Field(&AStruct::x, _); |
| 4260 |
EXPECT_FALSE(m.Matches(nullptr)); |
| 4261 |
} |
| 4262 |
|
| 4263 |
// Tests that Field(&Foo::field, ...) works when the argument's type |
| 4264 |
// is a sub-type of const Foo*. |
| 4265 |
TEST(FieldForPointerTest, WorksForArgumentOfSubType) { |
| 4266 |
// Note that the matcher expects DerivedStruct but we say AStruct |
| 4267 |
// inside Field(). |
| 4268 |
Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0)); |
| 4269 |
|
| 4270 |
DerivedStruct d; |
| 4271 |
EXPECT_TRUE(m.Matches(&d)); |
| 4272 |
d.x = -1; |
| 4273 |
EXPECT_FALSE(m.Matches(&d)); |
| 4274 |
} |
| 4275 |
|
| 4276 |
// Tests that Field() can describe itself when used to match a pointer. |
| 4277 |
TEST(FieldForPointerTest, CanDescribeSelf) { |
| 4278 |
Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0)); |
| 4279 |
|
| 4280 |
EXPECT_EQ("is an object whose given field is >= 0", Describe(m)); |
| 4281 |
EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m)); |
| 4282 |
} |
| 4283 |
|
| 4284 |
TEST(FieldForPointerTest, CanDescribeSelfWithFieldName) { |
| 4285 |
Matcher<const AStruct*> m = Field("field_name", &AStruct::x, Ge(0)); |
| 4286 |
|
| 4287 |
EXPECT_EQ("is an object whose field `field_name` is >= 0", Describe(m)); |
| 4288 |
EXPECT_EQ("is an object whose field `field_name` isn't >= 0", |
| 4289 |
DescribeNegation(m)); |
| 4290 |
} |
| 4291 |
|
| 4292 |
// Tests that Field() can explain the result of matching a pointer. |
| 4293 |
TEST(FieldForPointerTest, CanExplainMatchResult) { |
| 4294 |
Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0)); |
| 4295 |
|
| 4296 |
AStruct a; |
| 4297 |
a.x = 1; |
| 4298 |
EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(nullptr))); |
| 4299 |
EXPECT_EQ("which points to an object whose given field is 1" + OfType("int"), |
| 4300 |
Explain(m, &a)); |
| 4301 |
|
| 4302 |
m = Field(&AStruct::x, GreaterThan(0)); |
| 4303 |
EXPECT_EQ("which points to an object whose given field is 1" + OfType("int") + |
| 4304 |
", which is 1 more than 0", Explain(m, &a)); |
| 4305 |
} |
| 4306 |
|
| 4307 |
TEST(FieldForPointerTest, CanExplainMatchResultWithFieldName) { |
| 4308 |
Matcher<const AStruct*> m = Field("field_name", &AStruct::x, Ge(0)); |
| 4309 |
|
| 4310 |
AStruct a; |
| 4311 |
a.x = 1; |
| 4312 |
EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(nullptr))); |
| 4313 |
EXPECT_EQ( |
| 4314 |
"which points to an object whose field `field_name` is 1" + OfType("int"), |
| 4315 |
Explain(m, &a)); |
| 4316 |
|
| 4317 |
m = Field("field_name", &AStruct::x, GreaterThan(0)); |
| 4318 |
EXPECT_EQ("which points to an object whose field `field_name` is 1" + |
| 4319 |
OfType("int") + ", which is 1 more than 0", |
| 4320 |
Explain(m, &a)); |
| 4321 |
} |
| 4322 |
|
| 4323 |
// A user-defined class for testing Property(). |
| 4324 |
class AClass { |
| 4325 |
public: |
| 4326 |
AClass() : n_(0) {} |
| 4327 |
|
| 4328 |
// A getter that returns a non-reference. |
| 4329 |
int n() const { return n_; } |
| 4330 |
|
| 4331 |
void set_n(int new_n) { n_ = new_n; } |
| 4332 |
|
| 4333 |
// A getter that returns a reference to const. |
| 4334 |
const std::string& s() const { return s_; } |
| 4335 |
|
| 4336 |
const std::string& s_ref() const & { return s_; } |
| 4337 |
|
| 4338 |
void set_s(const std::string& new_s) { s_ = new_s; } |
| 4339 |
|
| 4340 |
// A getter that returns a reference to non-const. |
| 4341 |
double& x() const { return x_; } |
| 4342 |
|
| 4343 |
private: |
| 4344 |
int n_; |
| 4345 |
std::string s_; |
| 4346 |
|
| 4347 |
static double x_; |
| 4348 |
}; |
| 4349 |
|
| 4350 |
double AClass::x_ = 0.0; |
| 4351 |
|
| 4352 |
// A derived class for testing Property(). |
| 4353 |
class DerivedClass : public AClass { |
| 4354 |
public: |
| 4355 |
int k() const { return k_; } |
| 4356 |
private: |
| 4357 |
int k_; |
| 4358 |
}; |
| 4359 |
|
| 4360 |
// Tests that Property(&Foo::property, ...) works when property() |
| 4361 |
// returns a non-reference. |
| 4362 |
TEST(PropertyTest, WorksForNonReferenceProperty) { |
| 4363 |
Matcher<const AClass&> m = Property(&AClass::n, Ge(0)); |
| 4364 |
Matcher<const AClass&> m_with_name = Property("n", &AClass::n, Ge(0)); |
| 4365 |
|
| 4366 |
AClass a; |
| 4367 |
a.set_n(1); |
| 4368 |
EXPECT_TRUE(m.Matches(a)); |
| 4369 |
EXPECT_TRUE(m_with_name.Matches(a)); |
| 4370 |
|
| 4371 |
a.set_n(-1); |
| 4372 |
EXPECT_FALSE(m.Matches(a)); |
| 4373 |
EXPECT_FALSE(m_with_name.Matches(a)); |
| 4374 |
} |
| 4375 |
|
| 4376 |
// Tests that Property(&Foo::property, ...) works when property() |
| 4377 |
// returns a reference to const. |
| 4378 |
TEST(PropertyTest, WorksForReferenceToConstProperty) { |
| 4379 |
Matcher<const AClass&> m = Property(&AClass::s, StartsWith("hi")); |
| 4380 |
Matcher<const AClass&> m_with_name = |
| 4381 |
Property("s", &AClass::s, StartsWith("hi")); |
| 4382 |
|
| 4383 |
AClass a; |
| 4384 |
a.set_s("hill"); |
| 4385 |
EXPECT_TRUE(m.Matches(a)); |
| 4386 |
EXPECT_TRUE(m_with_name.Matches(a)); |
| 4387 |
|
| 4388 |
a.set_s("hole"); |
| 4389 |
EXPECT_FALSE(m.Matches(a)); |
| 4390 |
EXPECT_FALSE(m_with_name.Matches(a)); |
| 4391 |
} |
| 4392 |
|
| 4393 |
// Tests that Property(&Foo::property, ...) works when property() is |
| 4394 |
// ref-qualified. |
| 4395 |
TEST(PropertyTest, WorksForRefQualifiedProperty) { |
| 4396 |
Matcher<const AClass&> m = Property(&AClass::s_ref, StartsWith("hi")); |
| 4397 |
Matcher<const AClass&> m_with_name = |
| 4398 |
Property("s", &AClass::s_ref, StartsWith("hi")); |
| 4399 |
|
| 4400 |
AClass a; |
| 4401 |
a.set_s("hill"); |
| 4402 |
EXPECT_TRUE(m.Matches(a)); |
| 4403 |
EXPECT_TRUE(m_with_name.Matches(a)); |
| 4404 |
|
| 4405 |
a.set_s("hole"); |
| 4406 |
EXPECT_FALSE(m.Matches(a)); |
| 4407 |
EXPECT_FALSE(m_with_name.Matches(a)); |
| 4408 |
} |
| 4409 |
|
| 4410 |
// Tests that Property(&Foo::property, ...) works when property() |
| 4411 |
// returns a reference to non-const. |
| 4412 |
TEST(PropertyTest, WorksForReferenceToNonConstProperty) { |
| 4413 |
double x = 0.0; |
| 4414 |
AClass a; |
| 4415 |
|
| 4416 |
Matcher<const AClass&> m = Property(&AClass::x, Ref(x)); |
| 4417 |
EXPECT_FALSE(m.Matches(a)); |
| 4418 |
|
| 4419 |
m = Property(&AClass::x, Not(Ref(x))); |
| 4420 |
EXPECT_TRUE(m.Matches(a)); |
| 4421 |
} |
| 4422 |
|
| 4423 |
// Tests that Property(&Foo::property, ...) works when the argument is |
| 4424 |
// passed by value. |
| 4425 |
TEST(PropertyTest, WorksForByValueArgument) { |
| 4426 |
Matcher<AClass> m = Property(&AClass::s, StartsWith("hi")); |
| 4427 |
|
| 4428 |
AClass a; |
| 4429 |
a.set_s("hill"); |
| 4430 |
EXPECT_TRUE(m.Matches(a)); |
| 4431 |
|
| 4432 |
a.set_s("hole"); |
| 4433 |
EXPECT_FALSE(m.Matches(a)); |
| 4434 |
} |
| 4435 |
|
| 4436 |
// Tests that Property(&Foo::property, ...) works when the argument's |
| 4437 |
// type is a sub-type of Foo. |
| 4438 |
TEST(PropertyTest, WorksForArgumentOfSubType) { |
| 4439 |
// The matcher expects a DerivedClass, but inside the Property() we |
| 4440 |
// say AClass. |
| 4441 |
Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0)); |
| 4442 |
|
| 4443 |
DerivedClass d; |
| 4444 |
d.set_n(1); |
| 4445 |
EXPECT_TRUE(m.Matches(d)); |
| 4446 |
|
| 4447 |
d.set_n(-1); |
| 4448 |
EXPECT_FALSE(m.Matches(d)); |
| 4449 |
} |
| 4450 |
|
| 4451 |
// Tests that Property(&Foo::property, m) works when property()'s type |
| 4452 |
// and m's argument type are compatible but different. |
| 4453 |
TEST(PropertyTest, WorksForCompatibleMatcherType) { |
| 4454 |
// n() returns an int but the inner matcher expects a signed char. |
| 4455 |
Matcher<const AClass&> m = Property(&AClass::n, |
| 4456 |
Matcher<signed char>(Ge(0))); |
| 4457 |
|
| 4458 |
Matcher<const AClass&> m_with_name = |
| 4459 |
Property("n", &AClass::n, Matcher<signed char>(Ge(0))); |
| 4460 |
|
| 4461 |
AClass a; |
| 4462 |
EXPECT_TRUE(m.Matches(a)); |
| 4463 |
EXPECT_TRUE(m_with_name.Matches(a)); |
| 4464 |
a.set_n(-1); |
| 4465 |
EXPECT_FALSE(m.Matches(a)); |
| 4466 |
EXPECT_FALSE(m_with_name.Matches(a)); |
| 4467 |
} |
| 4468 |
|
| 4469 |
// Tests that Property() can describe itself. |
| 4470 |
TEST(PropertyTest, CanDescribeSelf) { |
| 4471 |
Matcher<const AClass&> m = Property(&AClass::n, Ge(0)); |
| 4472 |
|
| 4473 |
EXPECT_EQ("is an object whose given property is >= 0", Describe(m)); |
| 4474 |
EXPECT_EQ("is an object whose given property isn't >= 0", |
| 4475 |
DescribeNegation(m)); |
| 4476 |
} |
| 4477 |
|
| 4478 |
TEST(PropertyTest, CanDescribeSelfWithPropertyName) { |
| 4479 |
Matcher<const AClass&> m = Property("fancy_name", &AClass::n, Ge(0)); |
| 4480 |
|
| 4481 |
EXPECT_EQ("is an object whose property `fancy_name` is >= 0", Describe(m)); |
| 4482 |
EXPECT_EQ("is an object whose property `fancy_name` isn't >= 0", |
| 4483 |
DescribeNegation(m)); |
| 4484 |
} |
| 4485 |
|
| 4486 |
// Tests that Property() can explain the match result. |
| 4487 |
TEST(PropertyTest, CanExplainMatchResult) { |
| 4488 |
Matcher<const AClass&> m = Property(&AClass::n, Ge(0)); |
| 4489 |
|
| 4490 |
AClass a; |
| 4491 |
a.set_n(1); |
| 4492 |
EXPECT_EQ("whose given property is 1" + OfType("int"), Explain(m, a)); |
| 4493 |
|
| 4494 |
m = Property(&AClass::n, GreaterThan(0)); |
| 4495 |
EXPECT_EQ( |
| 4496 |
"whose given property is 1" + OfType("int") + ", which is 1 more than 0", |
| 4497 |
Explain(m, a)); |
| 4498 |
} |
| 4499 |
|
| 4500 |
TEST(PropertyTest, CanExplainMatchResultWithPropertyName) { |
| 4501 |
Matcher<const AClass&> m = Property("fancy_name", &AClass::n, Ge(0)); |
| 4502 |
|
| 4503 |
AClass a; |
| 4504 |
a.set_n(1); |
| 4505 |
EXPECT_EQ("whose property `fancy_name` is 1" + OfType("int"), Explain(m, a)); |
| 4506 |
|
| 4507 |
m = Property("fancy_name", &AClass::n, GreaterThan(0)); |
| 4508 |
EXPECT_EQ("whose property `fancy_name` is 1" + OfType("int") + |
| 4509 |
", which is 1 more than 0", |
| 4510 |
Explain(m, a)); |
| 4511 |
} |
| 4512 |
|
| 4513 |
// Tests that Property() works when the argument is a pointer to const. |
| 4514 |
TEST(PropertyForPointerTest, WorksForPointerToConst) { |
| 4515 |
Matcher<const AClass*> m = Property(&AClass::n, Ge(0)); |
| 4516 |
|
| 4517 |
AClass a; |
| 4518 |
a.set_n(1); |
| 4519 |
EXPECT_TRUE(m.Matches(&a)); |
| 4520 |
|
| 4521 |
a.set_n(-1); |
| 4522 |
EXPECT_FALSE(m.Matches(&a)); |
| 4523 |
} |
| 4524 |
|
| 4525 |
// Tests that Property() works when the argument is a pointer to non-const. |
| 4526 |
TEST(PropertyForPointerTest, WorksForPointerToNonConst) { |
| 4527 |
Matcher<AClass*> m = Property(&AClass::s, StartsWith("hi")); |
| 4528 |
|
| 4529 |
AClass a; |
| 4530 |
a.set_s("hill"); |
| 4531 |
EXPECT_TRUE(m.Matches(&a)); |
| 4532 |
|
| 4533 |
a.set_s("hole"); |
| 4534 |
EXPECT_FALSE(m.Matches(&a)); |
| 4535 |
} |
| 4536 |
|
| 4537 |
// Tests that Property() works when the argument is a reference to a |
| 4538 |
// const pointer. |
| 4539 |
TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) { |
| 4540 |
Matcher<AClass* const&> m = Property(&AClass::s, StartsWith("hi")); |
| 4541 |
|
| 4542 |
AClass a; |
| 4543 |
a.set_s("hill"); |
| 4544 |
EXPECT_TRUE(m.Matches(&a)); |
| 4545 |
|
| 4546 |
a.set_s("hole"); |
| 4547 |
EXPECT_FALSE(m.Matches(&a)); |
| 4548 |
} |
| 4549 |
|
| 4550 |
// Tests that Property() does not match the NULL pointer. |
| 4551 |
TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) { |
| 4552 |
Matcher<const AClass*> m = Property(&AClass::x, _); |
| 4553 |
EXPECT_FALSE(m.Matches(nullptr)); |
| 4554 |
} |
| 4555 |
|
| 4556 |
// Tests that Property(&Foo::property, ...) works when the argument's |
| 4557 |
// type is a sub-type of const Foo*. |
| 4558 |
TEST(PropertyForPointerTest, WorksForArgumentOfSubType) { |
| 4559 |
// The matcher expects a DerivedClass, but inside the Property() we |
| 4560 |
// say AClass. |
| 4561 |
Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0)); |
| 4562 |
|
| 4563 |
DerivedClass d; |
| 4564 |
d.set_n(1); |
| 4565 |
EXPECT_TRUE(m.Matches(&d)); |
| 4566 |
|
| 4567 |
d.set_n(-1); |
| 4568 |
EXPECT_FALSE(m.Matches(&d)); |
| 4569 |
} |
| 4570 |
|
| 4571 |
// Tests that Property() can describe itself when used to match a pointer. |
| 4572 |
TEST(PropertyForPointerTest, CanDescribeSelf) { |
| 4573 |
Matcher<const AClass*> m = Property(&AClass::n, Ge(0)); |
| 4574 |
|
| 4575 |
EXPECT_EQ("is an object whose given property is >= 0", Describe(m)); |
| 4576 |
EXPECT_EQ("is an object whose given property isn't >= 0", |
| 4577 |
DescribeNegation(m)); |
| 4578 |
} |
| 4579 |
|
| 4580 |
TEST(PropertyForPointerTest, CanDescribeSelfWithPropertyDescription) { |
| 4581 |
Matcher<const AClass*> m = Property("fancy_name", &AClass::n, Ge(0)); |
| 4582 |
|
| 4583 |
EXPECT_EQ("is an object whose property `fancy_name` is >= 0", Describe(m)); |
| 4584 |
EXPECT_EQ("is an object whose property `fancy_name` isn't >= 0", |
| 4585 |
DescribeNegation(m)); |
| 4586 |
} |
| 4587 |
|
| 4588 |
// Tests that Property() can explain the result of matching a pointer. |
| 4589 |
TEST(PropertyForPointerTest, CanExplainMatchResult) { |
| 4590 |
Matcher<const AClass*> m = Property(&AClass::n, Ge(0)); |
| 4591 |
|
| 4592 |
AClass a; |
| 4593 |
a.set_n(1); |
| 4594 |
EXPECT_EQ("", Explain(m, static_cast<const AClass*>(nullptr))); |
| 4595 |
EXPECT_EQ( |
| 4596 |
"which points to an object whose given property is 1" + OfType("int"), |
| 4597 |
Explain(m, &a)); |
| 4598 |
|
| 4599 |
m = Property(&AClass::n, GreaterThan(0)); |
| 4600 |
EXPECT_EQ("which points to an object whose given property is 1" + |
| 4601 |
OfType("int") + ", which is 1 more than 0", |
| 4602 |
Explain(m, &a)); |
| 4603 |
} |
| 4604 |
|
| 4605 |
TEST(PropertyForPointerTest, CanExplainMatchResultWithPropertyName) { |
| 4606 |
Matcher<const AClass*> m = Property("fancy_name", &AClass::n, Ge(0)); |
| 4607 |
|
| 4608 |
AClass a; |
| 4609 |
a.set_n(1); |
| 4610 |
EXPECT_EQ("", Explain(m, static_cast<const AClass*>(nullptr))); |
| 4611 |
EXPECT_EQ("which points to an object whose property `fancy_name` is 1" + |
| 4612 |
OfType("int"), |
| 4613 |
Explain(m, &a)); |
| 4614 |
|
| 4615 |
m = Property("fancy_name", &AClass::n, GreaterThan(0)); |
| 4616 |
EXPECT_EQ("which points to an object whose property `fancy_name` is 1" + |
| 4617 |
OfType("int") + ", which is 1 more than 0", |
| 4618 |
Explain(m, &a)); |
| 4619 |
} |
| 4620 |
|
| 4621 |
// Tests ResultOf. |
| 4622 |
|
| 4623 |
// Tests that ResultOf(f, ...) compiles and works as expected when f is a |
| 4624 |
// function pointer. |
| 4625 |
std::string IntToStringFunction(int input) { |
| 4626 |
return input == 1 ? "foo" : "bar"; |
| 4627 |
} |
| 4628 |
|
| 4629 |
TEST(ResultOfTest, WorksForFunctionPointers) { |
| 4630 |
Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(std::string("foo"))); |
| 4631 |
|
| 4632 |
EXPECT_TRUE(matcher.Matches(1)); |
| 4633 |
EXPECT_FALSE(matcher.Matches(2)); |
| 4634 |
} |
| 4635 |
|
| 4636 |
// Tests that ResultOf() can describe itself. |
| 4637 |
TEST(ResultOfTest, CanDescribeItself) { |
| 4638 |
Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo")); |
| 4639 |
|
| 4640 |
EXPECT_EQ("is mapped by the given callable to a value that " |
| 4641 |
"is equal to \"foo\"", Describe(matcher)); |
| 4642 |
EXPECT_EQ("is mapped by the given callable to a value that " |
| 4643 |
"isn't equal to \"foo\"", DescribeNegation(matcher)); |
| 4644 |
} |
| 4645 |
|
| 4646 |
// Tests that ResultOf() can explain the match result. |
| 4647 |
int IntFunction(int input) { return input == 42 ? 80 : 90; } |
| 4648 |
|
| 4649 |
TEST(ResultOfTest, CanExplainMatchResult) { |
| 4650 |
Matcher<int> matcher = ResultOf(&IntFunction, Ge(85)); |
| 4651 |
EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int"), |
| 4652 |
Explain(matcher, 36)); |
| 4653 |
|
| 4654 |
matcher = ResultOf(&IntFunction, GreaterThan(85)); |
| 4655 |
EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int") + |
| 4656 |
", which is 5 more than 85", Explain(matcher, 36)); |
| 4657 |
} |
| 4658 |
|
| 4659 |
// Tests that ResultOf(f, ...) compiles and works as expected when f(x) |
| 4660 |
// returns a non-reference. |
| 4661 |
TEST(ResultOfTest, WorksForNonReferenceResults) { |
| 4662 |
Matcher<int> matcher = ResultOf(&IntFunction, Eq(80)); |
| 4663 |
|
| 4664 |
EXPECT_TRUE(matcher.Matches(42)); |
| 4665 |
EXPECT_FALSE(matcher.Matches(36)); |
| 4666 |
} |
| 4667 |
|
| 4668 |
// Tests that ResultOf(f, ...) compiles and works as expected when f(x) |
| 4669 |
// returns a reference to non-const. |
| 4670 |
double& DoubleFunction(double& input) { return input; } // NOLINT |
| 4671 |
|
| 4672 |
Uncopyable& RefUncopyableFunction(Uncopyable& obj) { // NOLINT |
| 4673 |
return obj; |
| 4674 |
} |
| 4675 |
|
| 4676 |
TEST(ResultOfTest, WorksForReferenceToNonConstResults) { |
| 4677 |
double x = 3.14; |
| 4678 |
double x2 = x; |
| 4679 |
Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(x)); |
| 4680 |
|
| 4681 |
EXPECT_TRUE(matcher.Matches(x)); |
| 4682 |
EXPECT_FALSE(matcher.Matches(x2)); |
| 4683 |
|
| 4684 |
// Test that ResultOf works with uncopyable objects |
| 4685 |
Uncopyable obj(0); |
| 4686 |
Uncopyable obj2(0); |
| 4687 |
Matcher<Uncopyable&> matcher2 = |
| 4688 |
ResultOf(&RefUncopyableFunction, Ref(obj)); |
| 4689 |
|
| 4690 |
EXPECT_TRUE(matcher2.Matches(obj)); |
| 4691 |
EXPECT_FALSE(matcher2.Matches(obj2)); |
| 4692 |
} |
| 4693 |
|
| 4694 |
// Tests that ResultOf(f, ...) compiles and works as expected when f(x) |
| 4695 |
// returns a reference to const. |
| 4696 |
const std::string& StringFunction(const std::string& input) { return input; } |
| 4697 |
|
| 4698 |
TEST(ResultOfTest, WorksForReferenceToConstResults) { |
| 4699 |
std::string s = "foo"; |
| 4700 |
std::string s2 = s; |
| 4701 |
Matcher<const std::string&> matcher = ResultOf(&StringFunction, Ref(s)); |
| 4702 |
|
| 4703 |
EXPECT_TRUE(matcher.Matches(s)); |
| 4704 |
EXPECT_FALSE(matcher.Matches(s2)); |
| 4705 |
} |
| 4706 |
|
| 4707 |
// Tests that ResultOf(f, m) works when f(x) and m's |
| 4708 |
// argument types are compatible but different. |
| 4709 |
TEST(ResultOfTest, WorksForCompatibleMatcherTypes) { |
| 4710 |
// IntFunction() returns int but the inner matcher expects a signed char. |
| 4711 |
Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85))); |
| 4712 |
|
| 4713 |
EXPECT_TRUE(matcher.Matches(36)); |
| 4714 |
EXPECT_FALSE(matcher.Matches(42)); |
| 4715 |
} |
| 4716 |
|
| 4717 |
// Tests that the program aborts when ResultOf is passed |
| 4718 |
// a NULL function pointer. |
| 4719 |
TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) { |
| 4720 |
EXPECT_DEATH_IF_SUPPORTED( |
| 4721 |
ResultOf(static_cast<std::string (*)(int dummy)>(nullptr), |
| 4722 |
Eq(std::string("foo"))), |
| 4723 |
"NULL function pointer is passed into ResultOf\\(\\)\\."); |
| 4724 |
} |
| 4725 |
|
| 4726 |
// Tests that ResultOf(f, ...) compiles and works as expected when f is a |
| 4727 |
// function reference. |
| 4728 |
TEST(ResultOfTest, WorksForFunctionReferences) { |
| 4729 |
Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq("foo")); |
| 4730 |
EXPECT_TRUE(matcher.Matches(1)); |
| 4731 |
EXPECT_FALSE(matcher.Matches(2)); |
| 4732 |
} |
| 4733 |
|
| 4734 |
// Tests that ResultOf(f, ...) compiles and works as expected when f is a |
| 4735 |
// function object. |
| 4736 |
struct Functor { |
| 4737 |
std::string operator()(int input) const { |
| 4738 |
return IntToStringFunction(input); |
| 4739 |
} |
| 4740 |
}; |
| 4741 |
|
| 4742 |
TEST(ResultOfTest, WorksForFunctors) { |
| 4743 |
Matcher<int> matcher = ResultOf(Functor(), Eq(std::string("foo"))); |
| 4744 |
|
| 4745 |
EXPECT_TRUE(matcher.Matches(1)); |
| 4746 |
EXPECT_FALSE(matcher.Matches(2)); |
| 4747 |
} |
| 4748 |
|
| 4749 |
// Tests that ResultOf(f, ...) compiles and works as expected when f is a |
| 4750 |
// functor with more than one operator() defined. ResultOf() must work |
| 4751 |
// for each defined operator(). |
| 4752 |
struct PolymorphicFunctor { |
| 4753 |
typedef int result_type; |
| 4754 |
int operator()(int n) { return n; } |
| 4755 |
int operator()(const char* s) { return static_cast<int>(strlen(s)); } |
| 4756 |
std::string operator()(int *p) { return p ? "good ptr" : "null"; } |
| 4757 |
}; |
| 4758 |
|
| 4759 |
TEST(ResultOfTest, WorksForPolymorphicFunctors) { |
| 4760 |
Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5)); |
| 4761 |
|
| 4762 |
EXPECT_TRUE(matcher_int.Matches(10)); |
| 4763 |
EXPECT_FALSE(matcher_int.Matches(2)); |
| 4764 |
|
| 4765 |
Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5)); |
| 4766 |
|
| 4767 |
EXPECT_TRUE(matcher_string.Matches("long string")); |
| 4768 |
EXPECT_FALSE(matcher_string.Matches("shrt")); |
| 4769 |
} |
| 4770 |
|
| 4771 |
TEST(ResultOfTest, WorksForPolymorphicFunctorsIgnoringResultType) { |
| 4772 |
Matcher<int*> matcher = ResultOf(PolymorphicFunctor(), "good ptr"); |
| 4773 |
|
| 4774 |
int n = 0; |
| 4775 |
EXPECT_TRUE(matcher.Matches(&n)); |
| 4776 |
EXPECT_FALSE(matcher.Matches(nullptr)); |
| 4777 |
} |
| 4778 |
|
| 4779 |
TEST(ResultOfTest, WorksForLambdas) { |
| 4780 |
Matcher<int> matcher = ResultOf( |
| 4781 |
[](int str_len) { |
| 4782 |
return std::string(static_cast<size_t>(str_len), 'x'); |
| 4783 |
}, |
| 4784 |
"xxx"); |
| 4785 |
EXPECT_TRUE(matcher.Matches(3)); |
| 4786 |
EXPECT_FALSE(matcher.Matches(1)); |
| 4787 |
} |
| 4788 |
|
| 4789 |
TEST(ResultOfTest, WorksForNonCopyableArguments) { |
| 4790 |
Matcher<std::unique_ptr<int>> matcher = ResultOf( |
| 4791 |
[](const std::unique_ptr<int>& str_len) { |
| 4792 |
return std::string(static_cast<size_t>(*str_len), 'x'); |
| 4793 |
}, |
| 4794 |
"xxx"); |
| 4795 |
EXPECT_TRUE(matcher.Matches(std::unique_ptr<int>(new int(3)))); |
| 4796 |
EXPECT_FALSE(matcher.Matches(std::unique_ptr<int>(new int(1)))); |
| 4797 |
} |
| 4798 |
|
| 4799 |
const int* ReferencingFunction(const int& n) { return &n; } |
| 4800 |
|
| 4801 |
struct ReferencingFunctor { |
| 4802 |
typedef const int* result_type; |
| 4803 |
result_type operator()(const int& n) { return &n; } |
| 4804 |
}; |
| 4805 |
|
| 4806 |
TEST(ResultOfTest, WorksForReferencingCallables) { |
| 4807 |
const int n = 1; |
| 4808 |
const int n2 = 1; |
| 4809 |
Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n)); |
| 4810 |
EXPECT_TRUE(matcher2.Matches(n)); |
| 4811 |
EXPECT_FALSE(matcher2.Matches(n2)); |
| 4812 |
|
| 4813 |
Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n)); |
| 4814 |
EXPECT_TRUE(matcher3.Matches(n)); |
| 4815 |
EXPECT_FALSE(matcher3.Matches(n2)); |
| 4816 |
} |
| 4817 |
|
| 4818 |
class DivisibleByImpl { |
| 4819 |
public: |
| 4820 |
explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {} |
| 4821 |
|
| 4822 |
// For testing using ExplainMatchResultTo() with polymorphic matchers. |
| 4823 |
template <typename T> |
| 4824 |
bool MatchAndExplain(const T& n, MatchResultListener* listener) const { |
| 4825 |
*listener << "which is " << (n % divider_) << " modulo " |
| 4826 |
<< divider_; |
| 4827 |
return (n % divider_) == 0; |
| 4828 |
} |
| 4829 |
|
| 4830 |
void DescribeTo(ostream* os) const { |
| 4831 |
*os << "is divisible by " << divider_; |
| 4832 |
} |
| 4833 |
|
| 4834 |
void DescribeNegationTo(ostream* os) const { |
| 4835 |
*os << "is not divisible by " << divider_; |
| 4836 |
} |
| 4837 |
|
| 4838 |
void set_divider(int a_divider) { divider_ = a_divider; } |
| 4839 |
int divider() const { return divider_; } |
| 4840 |
|
| 4841 |
private: |
| 4842 |
int divider_; |
| 4843 |
}; |
| 4844 |
|
| 4845 |
PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) { |
| 4846 |
return MakePolymorphicMatcher(DivisibleByImpl(n)); |
| 4847 |
} |
| 4848 |
|
| 4849 |
// Tests that when AllOf() fails, only the first failing matcher is |
| 4850 |
// asked to explain why. |
| 4851 |
TEST(ExplainMatchResultTest, AllOf_False_False) { |
| 4852 |
const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3)); |
| 4853 |
EXPECT_EQ("which is 1 modulo 4", Explain(m, 5)); |
| 4854 |
} |
| 4855 |
|
| 4856 |
// Tests that when AllOf() fails, only the first failing matcher is |
| 4857 |
// asked to explain why. |
| 4858 |
TEST(ExplainMatchResultTest, AllOf_False_True) { |
| 4859 |
const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3)); |
| 4860 |
EXPECT_EQ("which is 2 modulo 4", Explain(m, 6)); |
| 4861 |
} |
| 4862 |
|
| 4863 |
// Tests that when AllOf() fails, only the first failing matcher is |
| 4864 |
// asked to explain why. |
| 4865 |
TEST(ExplainMatchResultTest, AllOf_True_False) { |
| 4866 |
const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3)); |
| 4867 |
EXPECT_EQ("which is 2 modulo 3", Explain(m, 5)); |
| 4868 |
} |
| 4869 |
|
| 4870 |
// Tests that when AllOf() succeeds, all matchers are asked to explain |
| 4871 |
// why. |
| 4872 |
TEST(ExplainMatchResultTest, AllOf_True_True) { |
| 4873 |
const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3)); |
| 4874 |
EXPECT_EQ("which is 0 modulo 2, and which is 0 modulo 3", Explain(m, 6)); |
| 4875 |
} |
| 4876 |
|
| 4877 |
TEST(ExplainMatchResultTest, AllOf_True_True_2) { |
| 4878 |
const Matcher<int> m = AllOf(Ge(2), Le(3)); |
| 4879 |
EXPECT_EQ("", Explain(m, 2)); |
| 4880 |
} |
| 4881 |
|
| 4882 |
TEST(ExplainmatcherResultTest, MonomorphicMatcher) { |
| 4883 |
const Matcher<int> m = GreaterThan(5); |
| 4884 |
EXPECT_EQ("which is 1 more than 5", Explain(m, 6)); |
| 4885 |
} |
| 4886 |
|
| 4887 |
// The following two tests verify that values without a public copy |
| 4888 |
// ctor can be used as arguments to matchers like Eq(), Ge(), and etc |
| 4889 |
// with the help of ByRef(). |
| 4890 |
|
| 4891 |
class NotCopyable { |
| 4892 |
public: |
| 4893 |
explicit NotCopyable(int a_value) : value_(a_value) {} |
| 4894 |
|
| 4895 |
int value() const { return value_; } |
| 4896 |
|
| 4897 |
bool operator==(const NotCopyable& rhs) const { |
| 4898 |
return value() == rhs.value(); |
| 4899 |
} |
| 4900 |
|
| 4901 |
bool operator>=(const NotCopyable& rhs) const { |
| 4902 |
return value() >= rhs.value(); |
| 4903 |
} |
| 4904 |
private: |
| 4905 |
int value_; |
| 4906 |
|
| 4907 |
GTEST_DISALLOW_COPY_AND_ASSIGN_(NotCopyable); |
| 4908 |
}; |
| 4909 |
|
| 4910 |
TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) { |
| 4911 |
const NotCopyable const_value1(1); |
| 4912 |
const Matcher<const NotCopyable&> m = Eq(ByRef(const_value1)); |
| 4913 |
|
| 4914 |
const NotCopyable n1(1), n2(2); |
| 4915 |
EXPECT_TRUE(m.Matches(n1)); |
| 4916 |
EXPECT_FALSE(m.Matches(n2)); |
| 4917 |
} |
| 4918 |
|
| 4919 |
TEST(ByRefTest, AllowsNotCopyableValueInMatchers) { |
| 4920 |
NotCopyable value2(2); |
| 4921 |
const Matcher<NotCopyable&> m = Ge(ByRef(value2)); |
| 4922 |
|
| 4923 |
NotCopyable n1(1), n2(2); |
| 4924 |
EXPECT_FALSE(m.Matches(n1)); |
| 4925 |
EXPECT_TRUE(m.Matches(n2)); |
| 4926 |
} |
| 4927 |
|
| 4928 |
TEST(IsEmptyTest, ImplementsIsEmpty) { |
| 4929 |
vector<int> container; |
| 4930 |
EXPECT_THAT(container, IsEmpty()); |
| 4931 |
container.push_back(0); |
| 4932 |
EXPECT_THAT(container, Not(IsEmpty())); |
| 4933 |
container.push_back(1); |
| 4934 |
EXPECT_THAT(container, Not(IsEmpty())); |
| 4935 |
} |
| 4936 |
|
| 4937 |
TEST(IsEmptyTest, WorksWithString) { |
| 4938 |
std::string text; |
| 4939 |
EXPECT_THAT(text, IsEmpty()); |
| 4940 |
text = "foo"; |
| 4941 |
EXPECT_THAT(text, Not(IsEmpty())); |
| 4942 |
text = std::string("\0", 1); |
| 4943 |
EXPECT_THAT(text, Not(IsEmpty())); |
| 4944 |
} |
| 4945 |
|
| 4946 |
TEST(IsEmptyTest, CanDescribeSelf) { |
| 4947 |
Matcher<vector<int> > m = IsEmpty(); |
| 4948 |
EXPECT_EQ("is empty", Describe(m)); |
| 4949 |
EXPECT_EQ("isn't empty", DescribeNegation(m)); |
| 4950 |
} |
| 4951 |
|
| 4952 |
TEST(IsEmptyTest, ExplainsResult) { |
| 4953 |
Matcher<vector<int> > m = IsEmpty(); |
| 4954 |
vector<int> container; |
| 4955 |
EXPECT_EQ("", Explain(m, container)); |
| 4956 |
container.push_back(0); |
| 4957 |
EXPECT_EQ("whose size is 1", Explain(m, container)); |
| 4958 |
} |
| 4959 |
|
| 4960 |
TEST(IsEmptyTest, WorksWithMoveOnly) { |
| 4961 |
ContainerHelper helper; |
| 4962 |
EXPECT_CALL(helper, Call(IsEmpty())); |
| 4963 |
helper.Call({}); |
| 4964 |
} |
| 4965 |
|
| 4966 |
TEST(IsTrueTest, IsTrueIsFalse) { |
| 4967 |
EXPECT_THAT(true, IsTrue()); |
| 4968 |
EXPECT_THAT(false, IsFalse()); |
| 4969 |
EXPECT_THAT(true, Not(IsFalse())); |
| 4970 |
EXPECT_THAT(false, Not(IsTrue())); |
| 4971 |
EXPECT_THAT(0, Not(IsTrue())); |
| 4972 |
EXPECT_THAT(0, IsFalse()); |
| 4973 |
EXPECT_THAT(nullptr, Not(IsTrue())); |
| 4974 |
EXPECT_THAT(nullptr, IsFalse()); |
| 4975 |
EXPECT_THAT(-1, IsTrue()); |
| 4976 |
EXPECT_THAT(-1, Not(IsFalse())); |
| 4977 |
EXPECT_THAT(1, IsTrue()); |
| 4978 |
EXPECT_THAT(1, Not(IsFalse())); |
| 4979 |
EXPECT_THAT(2, IsTrue()); |
| 4980 |
EXPECT_THAT(2, Not(IsFalse())); |
| 4981 |
int a = 42; |
| 4982 |
EXPECT_THAT(a, IsTrue()); |
| 4983 |
EXPECT_THAT(a, Not(IsFalse())); |
| 4984 |
EXPECT_THAT(&a, IsTrue()); |
| 4985 |
EXPECT_THAT(&a, Not(IsFalse())); |
| 4986 |
EXPECT_THAT(false, Not(IsTrue())); |
| 4987 |
EXPECT_THAT(true, Not(IsFalse())); |
| 4988 |
EXPECT_THAT(std::true_type(), IsTrue()); |
| 4989 |
EXPECT_THAT(std::true_type(), Not(IsFalse())); |
| 4990 |
EXPECT_THAT(std::false_type(), IsFalse()); |
| 4991 |
EXPECT_THAT(std::false_type(), Not(IsTrue())); |
| 4992 |
EXPECT_THAT(nullptr, Not(IsTrue())); |
| 4993 |
EXPECT_THAT(nullptr, IsFalse()); |
| 4994 |
std::unique_ptr<int> null_unique; |
| 4995 |
std::unique_ptr<int> nonnull_unique(new int(0)); |
| 4996 |
EXPECT_THAT(null_unique, Not(IsTrue())); |
| 4997 |
EXPECT_THAT(null_unique, IsFalse()); |
| 4998 |
EXPECT_THAT(nonnull_unique, IsTrue()); |
| 4999 |
EXPECT_THAT(nonnull_unique, Not(IsFalse())); |
| 5000 |
} |
| 5001 |
|
| 5002 |
TEST(SizeIsTest, ImplementsSizeIs) { |
| 5003 |
vector<int> container; |
| 5004 |
EXPECT_THAT(container, SizeIs(0)); |
| 5005 |
EXPECT_THAT(container, Not(SizeIs(1))); |
| 5006 |
container.push_back(0); |
| 5007 |
EXPECT_THAT(container, Not(SizeIs(0))); |
| 5008 |
EXPECT_THAT(container, SizeIs(1)); |
| 5009 |
container.push_back(0); |
| 5010 |
EXPECT_THAT(container, Not(SizeIs(0))); |
| 5011 |
EXPECT_THAT(container, SizeIs(2)); |
| 5012 |
} |
| 5013 |
|
| 5014 |
TEST(SizeIsTest, WorksWithMap) { |
| 5015 |
map<std::string, int> container; |
| 5016 |
EXPECT_THAT(container, SizeIs(0)); |
| 5017 |
EXPECT_THAT(container, Not(SizeIs(1))); |
| 5018 |
container.insert(make_pair("foo", 1)); |
| 5019 |
EXPECT_THAT(container, Not(SizeIs(0))); |
| 5020 |
EXPECT_THAT(container, SizeIs(1)); |
| 5021 |
container.insert(make_pair("bar", 2)); |
| 5022 |
EXPECT_THAT(container, Not(SizeIs(0))); |
| 5023 |
EXPECT_THAT(container, SizeIs(2)); |
| 5024 |
} |
| 5025 |
|
| 5026 |
TEST(SizeIsTest, WorksWithReferences) { |
| 5027 |
vector<int> container; |
| 5028 |
Matcher<const vector<int>&> m = SizeIs(1); |
| 5029 |
EXPECT_THAT(container, Not(m)); |
| 5030 |
container.push_back(0); |
| 5031 |
EXPECT_THAT(container, m); |
| 5032 |
} |
| 5033 |
|
| 5034 |
TEST(SizeIsTest, WorksWithMoveOnly) { |
| 5035 |
ContainerHelper helper; |
| 5036 |
EXPECT_CALL(helper, Call(SizeIs(3))); |
| 5037 |
helper.Call(MakeUniquePtrs({1, 2, 3})); |
| 5038 |
} |
| 5039 |
|
| 5040 |
// SizeIs should work for any type that provides a size() member function. |
| 5041 |
// For example, a size_type member type should not need to be provided. |
| 5042 |
struct MinimalistCustomType { |
| 5043 |
int size() const { return 1; } |
| 5044 |
}; |
| 5045 |
TEST(SizeIsTest, WorksWithMinimalistCustomType) { |
| 5046 |
MinimalistCustomType container; |
| 5047 |
EXPECT_THAT(container, SizeIs(1)); |
| 5048 |
EXPECT_THAT(container, Not(SizeIs(0))); |
| 5049 |
} |
| 5050 |
|
| 5051 |
TEST(SizeIsTest, CanDescribeSelf) { |
| 5052 |
Matcher<vector<int> > m = SizeIs(2); |
| 5053 |
EXPECT_EQ("size is equal to 2", Describe(m)); |
| 5054 |
EXPECT_EQ("size isn't equal to 2", DescribeNegation(m)); |
| 5055 |
} |
| 5056 |
|
| 5057 |
TEST(SizeIsTest, ExplainsResult) { |
| 5058 |
Matcher<vector<int> > m1 = SizeIs(2); |
| 5059 |
Matcher<vector<int> > m2 = SizeIs(Lt(2u)); |
| 5060 |
Matcher<vector<int> > m3 = SizeIs(AnyOf(0, 3)); |
| 5061 |
Matcher<vector<int> > m4 = SizeIs(Gt(1u)); |
| 5062 |
vector<int> container; |
| 5063 |
EXPECT_EQ("whose size 0 doesn't match", Explain(m1, container)); |
| 5064 |
EXPECT_EQ("whose size 0 matches", Explain(m2, container)); |
| 5065 |
EXPECT_EQ("whose size 0 matches", Explain(m3, container)); |
| 5066 |
EXPECT_EQ("whose size 0 doesn't match", Explain(m4, container)); |
| 5067 |
container.push_back(0); |
| 5068 |
container.push_back(0); |
| 5069 |
EXPECT_EQ("whose size 2 matches", Explain(m1, container)); |
| 5070 |
EXPECT_EQ("whose size 2 doesn't match", Explain(m2, container)); |
| 5071 |
EXPECT_EQ("whose size 2 doesn't match", Explain(m3, container)); |
| 5072 |
EXPECT_EQ("whose size 2 matches", Explain(m4, container)); |
| 5073 |
} |
| 5074 |
|
| 5075 |
#if GTEST_HAS_TYPED_TEST |
| 5076 |
// Tests ContainerEq with different container types, and |
| 5077 |
// different element types. |
| 5078 |
|
| 5079 |
template <typename T> |
| 5080 |
class ContainerEqTest : public testing::Test {}; |
| 5081 |
|
| 5082 |
typedef testing::Types< |
| 5083 |
set<int>, |
| 5084 |
vector<size_t>, |
| 5085 |
multiset<size_t>, |
| 5086 |
list<int> > |
| 5087 |
ContainerEqTestTypes; |
| 5088 |
|
| 5089 |
TYPED_TEST_SUITE(ContainerEqTest, ContainerEqTestTypes); |
| 5090 |
|
| 5091 |
// Tests that the filled container is equal to itself. |
| 5092 |
TYPED_TEST(ContainerEqTest, EqualsSelf) { |
| 5093 |
static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 5094 |
TypeParam my_set(vals, vals + 6); |
| 5095 |
const Matcher<TypeParam> m = ContainerEq(my_set); |
| 5096 |
EXPECT_TRUE(m.Matches(my_set)); |
| 5097 |
EXPECT_EQ("", Explain(m, my_set)); |
| 5098 |
} |
| 5099 |
|
| 5100 |
// Tests that missing values are reported. |
| 5101 |
TYPED_TEST(ContainerEqTest, ValueMissing) { |
| 5102 |
static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 5103 |
static const int test_vals[] = {2, 1, 8, 5}; |
| 5104 |
TypeParam my_set(vals, vals + 6); |
| 5105 |
TypeParam test_set(test_vals, test_vals + 4); |
| 5106 |
const Matcher<TypeParam> m = ContainerEq(my_set); |
| 5107 |
EXPECT_FALSE(m.Matches(test_set)); |
| 5108 |
EXPECT_EQ("which doesn't have these expected elements: 3", |
| 5109 |
Explain(m, test_set)); |
| 5110 |
} |
| 5111 |
|
| 5112 |
// Tests that added values are reported. |
| 5113 |
TYPED_TEST(ContainerEqTest, ValueAdded) { |
| 5114 |
static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 5115 |
static const int test_vals[] = {1, 2, 3, 5, 8, 46}; |
| 5116 |
TypeParam my_set(vals, vals + 6); |
| 5117 |
TypeParam test_set(test_vals, test_vals + 6); |
| 5118 |
const Matcher<const TypeParam&> m = ContainerEq(my_set); |
| 5119 |
EXPECT_FALSE(m.Matches(test_set)); |
| 5120 |
EXPECT_EQ("which has these unexpected elements: 46", Explain(m, test_set)); |
| 5121 |
} |
| 5122 |
|
| 5123 |
// Tests that added and missing values are reported together. |
| 5124 |
TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) { |
| 5125 |
static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 5126 |
static const int test_vals[] = {1, 2, 3, 8, 46}; |
| 5127 |
TypeParam my_set(vals, vals + 6); |
| 5128 |
TypeParam test_set(test_vals, test_vals + 5); |
| 5129 |
const Matcher<TypeParam> m = ContainerEq(my_set); |
| 5130 |
EXPECT_FALSE(m.Matches(test_set)); |
| 5131 |
EXPECT_EQ("which has these unexpected elements: 46,\n" |
| 5132 |
"and doesn't have these expected elements: 5", |
| 5133 |
Explain(m, test_set)); |
| 5134 |
} |
| 5135 |
|
| 5136 |
// Tests duplicated value -- expect no explanation. |
| 5137 |
TYPED_TEST(ContainerEqTest, DuplicateDifference) { |
| 5138 |
static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 5139 |
static const int test_vals[] = {1, 2, 3, 5, 8}; |
| 5140 |
TypeParam my_set(vals, vals + 6); |
| 5141 |
TypeParam test_set(test_vals, test_vals + 5); |
| 5142 |
const Matcher<const TypeParam&> m = ContainerEq(my_set); |
| 5143 |
// Depending on the container, match may be true or false |
| 5144 |
// But in any case there should be no explanation. |
| 5145 |
EXPECT_EQ("", Explain(m, test_set)); |
| 5146 |
} |
| 5147 |
#endif // GTEST_HAS_TYPED_TEST |
| 5148 |
|
| 5149 |
// Tests that multiple missing values are reported. |
| 5150 |
// Using just vector here, so order is predictable. |
| 5151 |
TEST(ContainerEqExtraTest, MultipleValuesMissing) { |
| 5152 |
static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 5153 |
static const int test_vals[] = {2, 1, 5}; |
| 5154 |
vector<int> my_set(vals, vals + 6); |
| 5155 |
vector<int> test_set(test_vals, test_vals + 3); |
| 5156 |
const Matcher<vector<int> > m = ContainerEq(my_set); |
| 5157 |
EXPECT_FALSE(m.Matches(test_set)); |
| 5158 |
EXPECT_EQ("which doesn't have these expected elements: 3, 8", |
| 5159 |
Explain(m, test_set)); |
| 5160 |
} |
| 5161 |
|
| 5162 |
// Tests that added values are reported. |
| 5163 |
// Using just vector here, so order is predictable. |
| 5164 |
TEST(ContainerEqExtraTest, MultipleValuesAdded) { |
| 5165 |
static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 5166 |
static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46}; |
| 5167 |
list<size_t> my_set(vals, vals + 6); |
| 5168 |
list<size_t> test_set(test_vals, test_vals + 7); |
| 5169 |
const Matcher<const list<size_t>&> m = ContainerEq(my_set); |
| 5170 |
EXPECT_FALSE(m.Matches(test_set)); |
| 5171 |
EXPECT_EQ("which has these unexpected elements: 92, 46", |
| 5172 |
Explain(m, test_set)); |
| 5173 |
} |
| 5174 |
|
| 5175 |
// Tests that added and missing values are reported together. |
| 5176 |
TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) { |
| 5177 |
static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 5178 |
static const int test_vals[] = {1, 2, 3, 92, 46}; |
| 5179 |
list<size_t> my_set(vals, vals + 6); |
| 5180 |
list<size_t> test_set(test_vals, test_vals + 5); |
| 5181 |
const Matcher<const list<size_t> > m = ContainerEq(my_set); |
| 5182 |
EXPECT_FALSE(m.Matches(test_set)); |
| 5183 |
EXPECT_EQ("which has these unexpected elements: 92, 46,\n" |
| 5184 |
"and doesn't have these expected elements: 5, 8", |
| 5185 |
Explain(m, test_set)); |
| 5186 |
} |
| 5187 |
|
| 5188 |
// Tests to see that duplicate elements are detected, |
| 5189 |
// but (as above) not reported in the explanation. |
| 5190 |
TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) { |
| 5191 |
static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 5192 |
static const int test_vals[] = {1, 2, 3, 5, 8}; |
| 5193 |
vector<int> my_set(vals, vals + 6); |
| 5194 |
vector<int> test_set(test_vals, test_vals + 5); |
| 5195 |
const Matcher<vector<int> > m = ContainerEq(my_set); |
| 5196 |
EXPECT_TRUE(m.Matches(my_set)); |
| 5197 |
EXPECT_FALSE(m.Matches(test_set)); |
| 5198 |
// There is nothing to report when both sets contain all the same values. |
| 5199 |
EXPECT_EQ("", Explain(m, test_set)); |
| 5200 |
} |
| 5201 |
|
| 5202 |
// Tests that ContainerEq works for non-trivial associative containers, |
| 5203 |
// like maps. |
| 5204 |
TEST(ContainerEqExtraTest, WorksForMaps) { |
| 5205 |
map<int, std::string> my_map; |
| 5206 |
my_map[0] = "a"; |
| 5207 |
my_map[1] = "b"; |
| 5208 |
|
| 5209 |
map<int, std::string> test_map; |
| 5210 |
test_map[0] = "aa"; |
| 5211 |
test_map[1] = "b"; |
| 5212 |
|
| 5213 |
const Matcher<const map<int, std::string>&> m = ContainerEq(my_map); |
| 5214 |
EXPECT_TRUE(m.Matches(my_map)); |
| 5215 |
EXPECT_FALSE(m.Matches(test_map)); |
| 5216 |
|
| 5217 |
EXPECT_EQ("which has these unexpected elements: (0, \"aa\"),\n" |
| 5218 |
"and doesn't have these expected elements: (0, \"a\")", |
| 5219 |
Explain(m, test_map)); |
| 5220 |
} |
| 5221 |
|
| 5222 |
TEST(ContainerEqExtraTest, WorksForNativeArray) { |
| 5223 |
int a1[] = {1, 2, 3}; |
| 5224 |
int a2[] = {1, 2, 3}; |
| 5225 |
int b[] = {1, 2, 4}; |
| 5226 |
|
| 5227 |
EXPECT_THAT(a1, ContainerEq(a2)); |
| 5228 |
EXPECT_THAT(a1, Not(ContainerEq(b))); |
| 5229 |
} |
| 5230 |
|
| 5231 |
TEST(ContainerEqExtraTest, WorksForTwoDimensionalNativeArray) { |
| 5232 |
const char a1[][3] = {"hi", "lo"}; |
| 5233 |
const char a2[][3] = {"hi", "lo"}; |
| 5234 |
const char b[][3] = {"lo", "hi"}; |
| 5235 |
|
| 5236 |
// Tests using ContainerEq() in the first dimension. |
| 5237 |
EXPECT_THAT(a1, ContainerEq(a2)); |
| 5238 |
EXPECT_THAT(a1, Not(ContainerEq(b))); |
| 5239 |
|
| 5240 |
// Tests using ContainerEq() in the second dimension. |
| 5241 |
EXPECT_THAT(a1, ElementsAre(ContainerEq(a2[0]), ContainerEq(a2[1]))); |
| 5242 |
EXPECT_THAT(a1, ElementsAre(Not(ContainerEq(b[0])), ContainerEq(a2[1]))); |
| 5243 |
} |
| 5244 |
|
| 5245 |
TEST(ContainerEqExtraTest, WorksForNativeArrayAsTuple) { |
| 5246 |
const int a1[] = {1, 2, 3}; |
| 5247 |
const int a2[] = {1, 2, 3}; |
| 5248 |
const int b[] = {1, 2, 3, 4}; |
| 5249 |
|
| 5250 |
const int* const p1 = a1; |
| 5251 |
EXPECT_THAT(std::make_tuple(p1, 3), ContainerEq(a2)); |
| 5252 |
EXPECT_THAT(std::make_tuple(p1, 3), Not(ContainerEq(b))); |
| 5253 |
|
| 5254 |
const int c[] = {1, 3, 2}; |
| 5255 |
EXPECT_THAT(std::make_tuple(p1, 3), Not(ContainerEq(c))); |
| 5256 |
} |
| 5257 |
|
| 5258 |
TEST(ContainerEqExtraTest, CopiesNativeArrayParameter) { |
| 5259 |
std::string a1[][3] = { |
| 5260 |
{"hi", "hello", "ciao"}, |
| 5261 |
{"bye", "see you", "ciao"} |
| 5262 |
}; |
| 5263 |
|
| 5264 |
std::string a2[][3] = { |
| 5265 |
{"hi", "hello", "ciao"}, |
| 5266 |
{"bye", "see you", "ciao"} |
| 5267 |
}; |
| 5268 |
|
| 5269 |
const Matcher<const std::string(&)[2][3]> m = ContainerEq(a2); |
| 5270 |
EXPECT_THAT(a1, m); |
| 5271 |
|
| 5272 |
a2[0][0] = "ha"; |
| 5273 |
EXPECT_THAT(a1, m); |
| 5274 |
} |
| 5275 |
|
| 5276 |
TEST(WhenSortedByTest, WorksForEmptyContainer) { |
| 5277 |
const vector<int> numbers; |
| 5278 |
EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre())); |
| 5279 |
EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1)))); |
| 5280 |
} |
| 5281 |
|
| 5282 |
TEST(WhenSortedByTest, WorksForNonEmptyContainer) { |
| 5283 |
vector<unsigned> numbers; |
| 5284 |
numbers.push_back(3); |
| 5285 |
numbers.push_back(1); |
| 5286 |
numbers.push_back(2); |
| 5287 |
numbers.push_back(2); |
| 5288 |
EXPECT_THAT(numbers, WhenSortedBy(greater<unsigned>(), |
| 5289 |
ElementsAre(3, 2, 2, 1))); |
| 5290 |
EXPECT_THAT(numbers, Not(WhenSortedBy(greater<unsigned>(), |
| 5291 |
ElementsAre(1, 2, 2, 3)))); |
| 5292 |
} |
| 5293 |
|
| 5294 |
TEST(WhenSortedByTest, WorksForNonVectorContainer) { |
| 5295 |
list<std::string> words; |
| 5296 |
words.push_back("say"); |
| 5297 |
words.push_back("hello"); |
| 5298 |
words.push_back("world"); |
| 5299 |
EXPECT_THAT(words, WhenSortedBy(less<std::string>(), |
| 5300 |
ElementsAre("hello", "say", "world"))); |
| 5301 |
EXPECT_THAT(words, Not(WhenSortedBy(less<std::string>(), |
| 5302 |
ElementsAre("say", "hello", "world")))); |
| 5303 |
} |
| 5304 |
|
| 5305 |
TEST(WhenSortedByTest, WorksForNativeArray) { |
| 5306 |
const int numbers[] = {1, 3, 2, 4}; |
| 5307 |
const int sorted_numbers[] = {1, 2, 3, 4}; |
| 5308 |
EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre(1, 2, 3, 4))); |
| 5309 |
EXPECT_THAT(numbers, WhenSortedBy(less<int>(), |
| 5310 |
ElementsAreArray(sorted_numbers))); |
| 5311 |
EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1, 3, 2, 4)))); |
| 5312 |
} |
| 5313 |
|
| 5314 |
TEST(WhenSortedByTest, CanDescribeSelf) { |
| 5315 |
const Matcher<vector<int> > m = WhenSortedBy(less<int>(), ElementsAre(1, 2)); |
| 5316 |
EXPECT_EQ("(when sorted) has 2 elements where\n" |
| 5317 |
"element #0 is equal to 1,\n" |
| 5318 |
"element #1 is equal to 2", |
| 5319 |
Describe(m)); |
| 5320 |
EXPECT_EQ("(when sorted) doesn't have 2 elements, or\n" |
| 5321 |
"element #0 isn't equal to 1, or\n" |
| 5322 |
"element #1 isn't equal to 2", |
| 5323 |
DescribeNegation(m)); |
| 5324 |
} |
| 5325 |
|
| 5326 |
TEST(WhenSortedByTest, ExplainsMatchResult) { |
| 5327 |
const int a[] = {2, 1}; |
| 5328 |
EXPECT_EQ("which is { 1, 2 } when sorted, whose element #0 doesn't match", |
| 5329 |
Explain(WhenSortedBy(less<int>(), ElementsAre(2, 3)), a)); |
| 5330 |
EXPECT_EQ("which is { 1, 2 } when sorted", |
| 5331 |
Explain(WhenSortedBy(less<int>(), ElementsAre(1, 2)), a)); |
| 5332 |
} |
| 5333 |
|
| 5334 |
// WhenSorted() is a simple wrapper on WhenSortedBy(). Hence we don't |
| 5335 |
// need to test it as exhaustively as we test the latter. |
| 5336 |
|
| 5337 |
TEST(WhenSortedTest, WorksForEmptyContainer) { |
| 5338 |
const vector<int> numbers; |
| 5339 |
EXPECT_THAT(numbers, WhenSorted(ElementsAre())); |
| 5340 |
EXPECT_THAT(numbers, Not(WhenSorted(ElementsAre(1)))); |
| 5341 |
} |
| 5342 |
|
| 5343 |
TEST(WhenSortedTest, WorksForNonEmptyContainer) { |
| 5344 |
list<std::string> words; |
| 5345 |
words.push_back("3"); |
| 5346 |
words.push_back("1"); |
| 5347 |
words.push_back("2"); |
| 5348 |
words.push_back("2"); |
| 5349 |
EXPECT_THAT(words, WhenSorted(ElementsAre("1", "2", "2", "3"))); |
| 5350 |
EXPECT_THAT(words, Not(WhenSorted(ElementsAre("3", "1", "2", "2")))); |
| 5351 |
} |
| 5352 |
|
| 5353 |
TEST(WhenSortedTest, WorksForMapTypes) { |
| 5354 |
map<std::string, int> word_counts; |
| 5355 |
word_counts["and"] = 1; |
| 5356 |
word_counts["the"] = 1; |
| 5357 |
word_counts["buffalo"] = 2; |
| 5358 |
EXPECT_THAT(word_counts, |
| 5359 |
WhenSorted(ElementsAre(Pair("and", 1), Pair("buffalo", 2), |
| 5360 |
Pair("the", 1)))); |
| 5361 |
EXPECT_THAT(word_counts, |
| 5362 |
Not(WhenSorted(ElementsAre(Pair("and", 1), Pair("the", 1), |
| 5363 |
Pair("buffalo", 2))))); |
| 5364 |
} |
| 5365 |
|
| 5366 |
TEST(WhenSortedTest, WorksForMultiMapTypes) { |
| 5367 |
multimap<int, int> ifib; |
| 5368 |
ifib.insert(make_pair(8, 6)); |
| 5369 |
ifib.insert(make_pair(2, 3)); |
| 5370 |
ifib.insert(make_pair(1, 1)); |
| 5371 |
ifib.insert(make_pair(3, 4)); |
| 5372 |
ifib.insert(make_pair(1, 2)); |
| 5373 |
ifib.insert(make_pair(5, 5)); |
| 5374 |
EXPECT_THAT(ifib, WhenSorted(ElementsAre(Pair(1, 1), |
| 5375 |
Pair(1, 2), |
| 5376 |
Pair(2, 3), |
| 5377 |
Pair(3, 4), |
| 5378 |
Pair(5, 5), |
| 5379 |
Pair(8, 6)))); |
| 5380 |
EXPECT_THAT(ifib, Not(WhenSorted(ElementsAre(Pair(8, 6), |
| 5381 |
Pair(2, 3), |
| 5382 |
Pair(1, 1), |
| 5383 |
Pair(3, 4), |
| 5384 |
Pair(1, 2), |
| 5385 |
Pair(5, 5))))); |
| 5386 |
} |
| 5387 |
|
| 5388 |
TEST(WhenSortedTest, WorksForPolymorphicMatcher) { |
| 5389 |
std::deque<int> d; |
| 5390 |
d.push_back(2); |
| 5391 |
d.push_back(1); |
| 5392 |
EXPECT_THAT(d, WhenSorted(ElementsAre(1, 2))); |
| 5393 |
EXPECT_THAT(d, Not(WhenSorted(ElementsAre(2, 1)))); |
| 5394 |
} |
| 5395 |
|
| 5396 |
TEST(WhenSortedTest, WorksForVectorConstRefMatcher) { |
| 5397 |
std::deque<int> d; |
| 5398 |
d.push_back(2); |
| 5399 |
d.push_back(1); |
| 5400 |
Matcher<const std::vector<int>&> vector_match = ElementsAre(1, 2); |
| 5401 |
EXPECT_THAT(d, WhenSorted(vector_match)); |
| 5402 |
Matcher<const std::vector<int>&> not_vector_match = ElementsAre(2, 1); |
| 5403 |
EXPECT_THAT(d, Not(WhenSorted(not_vector_match))); |
| 5404 |
} |
| 5405 |
|
| 5406 |
// Deliberately bare pseudo-container. |
| 5407 |
// Offers only begin() and end() accessors, yielding InputIterator. |
| 5408 |
template <typename T> |
| 5409 |
class Streamlike { |
| 5410 |
private: |
| 5411 |
class ConstIter; |
| 5412 |
public: |
| 5413 |
typedef ConstIter const_iterator; |
| 5414 |
typedef T value_type; |
| 5415 |
|
| 5416 |
template <typename InIter> |
| 5417 |
Streamlike(InIter first, InIter last) : remainder_(first, last) {} |
| 5418 |
|
| 5419 |
const_iterator begin() const { |
| 5420 |
return const_iterator(this, remainder_.begin()); |
| 5421 |
} |
| 5422 |
const_iterator end() const { |
| 5423 |
return const_iterator(this, remainder_.end()); |
| 5424 |
} |
| 5425 |
|
| 5426 |
private: |
| 5427 |
class ConstIter { |
| 5428 |
public: |
| 5429 |
using iterator_category = std::input_iterator_tag; |
| 5430 |
using value_type = T; |
| 5431 |
using difference_type = ptrdiff_t; |
| 5432 |
using pointer = const value_type*; |
| 5433 |
using reference = const value_type&; |
| 5434 |
|
| 5435 |
ConstIter(const Streamlike* s, |
| 5436 |
typename std::list<value_type>::iterator pos) |
| 5437 |
: s_(s), pos_(pos) {} |
| 5438 |
|
| 5439 |
const value_type& operator*() const { return *pos_; } |
| 5440 |
const value_type* operator->() const { return &*pos_; } |
| 5441 |
ConstIter& operator++() { |
| 5442 |
s_->remainder_.erase(pos_++); |
| 5443 |
return *this; |
| 5444 |
} |
| 5445 |
|
| 5446 |
// *iter++ is required to work (see std::istreambuf_iterator). |
| 5447 |
// (void)iter++ is also required to work. |
| 5448 |
class PostIncrProxy { |
| 5449 |
public: |
| 5450 |
explicit PostIncrProxy(const value_type& value) : value_(value) {} |
| 5451 |
value_type operator*() const { return value_; } |
| 5452 |
private: |
| 5453 |
value_type value_; |
| 5454 |
}; |
| 5455 |
PostIncrProxy operator++(int) { |
| 5456 |
PostIncrProxy proxy(**this); |
| 5457 |
++(*this); |
| 5458 |
return proxy; |
| 5459 |
} |
| 5460 |
|
| 5461 |
friend bool operator==(const ConstIter& a, const ConstIter& b) { |
| 5462 |
return a.s_ == b.s_ && a.pos_ == b.pos_; |
| 5463 |
} |
| 5464 |
friend bool operator!=(const ConstIter& a, const ConstIter& b) { |
| 5465 |
return !(a == b); |
| 5466 |
} |
| 5467 |
|
| 5468 |
private: |
| 5469 |
const Streamlike* s_; |
| 5470 |
typename std::list<value_type>::iterator pos_; |
| 5471 |
}; |
| 5472 |
|
| 5473 |
friend std::ostream& operator<<(std::ostream& os, const Streamlike& s) { |
| 5474 |
os << "["; |
| 5475 |
typedef typename std::list<value_type>::const_iterator Iter; |
| 5476 |
const char* sep = ""; |
| 5477 |
for (Iter it = s.remainder_.begin(); it != s.remainder_.end(); ++it) { |
| 5478 |
os << sep << *it; |
| 5479 |
sep = ","; |
| 5480 |
} |
| 5481 |
os << "]"; |
| 5482 |
return os; |
| 5483 |
} |
| 5484 |
|
| 5485 |
mutable std::list<value_type> remainder_; // modified by iteration |
| 5486 |
}; |
| 5487 |
|
| 5488 |
TEST(StreamlikeTest, Iteration) { |
| 5489 |
const int a[5] = {2, 1, 4, 5, 3}; |
| 5490 |
Streamlike<int> s(a, a + 5); |
| 5491 |
Streamlike<int>::const_iterator it = s.begin(); |
| 5492 |
const int* ip = a; |
| 5493 |
while (it != s.end()) { |
| 5494 |
SCOPED_TRACE(ip - a); |
| 5495 |
EXPECT_EQ(*ip++, *it++); |
| 5496 |
} |
| 5497 |
} |
| 5498 |
|
| 5499 |
TEST(BeginEndDistanceIsTest, WorksWithForwardList) { |
| 5500 |
std::forward_list<int> container; |
| 5501 |
EXPECT_THAT(container, BeginEndDistanceIs(0)); |
| 5502 |
EXPECT_THAT(container, Not(BeginEndDistanceIs(1))); |
| 5503 |
container.push_front(0); |
| 5504 |
EXPECT_THAT(container, Not(BeginEndDistanceIs(0))); |
| 5505 |
EXPECT_THAT(container, BeginEndDistanceIs(1)); |
| 5506 |
container.push_front(0); |
| 5507 |
EXPECT_THAT(container, Not(BeginEndDistanceIs(0))); |
| 5508 |
EXPECT_THAT(container, BeginEndDistanceIs(2)); |
| 5509 |
} |
| 5510 |
|
| 5511 |
TEST(BeginEndDistanceIsTest, WorksWithNonStdList) { |
| 5512 |
const int a[5] = {1, 2, 3, 4, 5}; |
| 5513 |
Streamlike<int> s(a, a + 5); |
| 5514 |
EXPECT_THAT(s, BeginEndDistanceIs(5)); |
| 5515 |
} |
| 5516 |
|
| 5517 |
TEST(BeginEndDistanceIsTest, CanDescribeSelf) { |
| 5518 |
Matcher<vector<int> > m = BeginEndDistanceIs(2); |
| 5519 |
EXPECT_EQ("distance between begin() and end() is equal to 2", Describe(m)); |
| 5520 |
EXPECT_EQ("distance between begin() and end() isn't equal to 2", |
| 5521 |
DescribeNegation(m)); |
| 5522 |
} |
| 5523 |
|
| 5524 |
TEST(BeginEndDistanceIsTest, WorksWithMoveOnly) { |
| 5525 |
ContainerHelper helper; |
| 5526 |
EXPECT_CALL(helper, Call(BeginEndDistanceIs(2))); |
| 5527 |
helper.Call(MakeUniquePtrs({1, 2})); |
| 5528 |
} |
| 5529 |
|
| 5530 |
TEST(BeginEndDistanceIsTest, ExplainsResult) { |
| 5531 |
Matcher<vector<int> > m1 = BeginEndDistanceIs(2); |
| 5532 |
Matcher<vector<int> > m2 = BeginEndDistanceIs(Lt(2)); |
| 5533 |
Matcher<vector<int> > m3 = BeginEndDistanceIs(AnyOf(0, 3)); |
| 5534 |
Matcher<vector<int> > m4 = BeginEndDistanceIs(GreaterThan(1)); |
| 5535 |
vector<int> container; |
| 5536 |
EXPECT_EQ("whose distance between begin() and end() 0 doesn't match", |
| 5537 |
Explain(m1, container)); |
| 5538 |
EXPECT_EQ("whose distance between begin() and end() 0 matches", |
| 5539 |
Explain(m2, container)); |
| 5540 |
EXPECT_EQ("whose distance between begin() and end() 0 matches", |
| 5541 |
Explain(m3, container)); |
| 5542 |
EXPECT_EQ( |
| 5543 |
"whose distance between begin() and end() 0 doesn't match, which is 1 " |
| 5544 |
"less than 1", |
| 5545 |
Explain(m4, container)); |
| 5546 |
container.push_back(0); |
| 5547 |
container.push_back(0); |
| 5548 |
EXPECT_EQ("whose distance between begin() and end() 2 matches", |
| 5549 |
Explain(m1, container)); |
| 5550 |
EXPECT_EQ("whose distance between begin() and end() 2 doesn't match", |
| 5551 |
Explain(m2, container)); |
| 5552 |
EXPECT_EQ("whose distance between begin() and end() 2 doesn't match", |
| 5553 |
Explain(m3, container)); |
| 5554 |
EXPECT_EQ( |
| 5555 |
"whose distance between begin() and end() 2 matches, which is 1 more " |
| 5556 |
"than 1", |
| 5557 |
Explain(m4, container)); |
| 5558 |
} |
| 5559 |
|
| 5560 |
TEST(WhenSortedTest, WorksForStreamlike) { |
| 5561 |
// Streamlike 'container' provides only minimal iterator support. |
| 5562 |
// Its iterators are tagged with input_iterator_tag. |
| 5563 |
const int a[5] = {2, 1, 4, 5, 3}; |
| 5564 |
Streamlike<int> s(std::begin(a), std::end(a)); |
| 5565 |
EXPECT_THAT(s, WhenSorted(ElementsAre(1, 2, 3, 4, 5))); |
| 5566 |
EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3)))); |
| 5567 |
} |
| 5568 |
|
| 5569 |
TEST(WhenSortedTest, WorksForVectorConstRefMatcherOnStreamlike) { |
| 5570 |
const int a[] = {2, 1, 4, 5, 3}; |
| 5571 |
Streamlike<int> s(std::begin(a), std::end(a)); |
| 5572 |
Matcher<const std::vector<int>&> vector_match = ElementsAre(1, 2, 3, 4, 5); |
| 5573 |
EXPECT_THAT(s, WhenSorted(vector_match)); |
| 5574 |
EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3)))); |
| 5575 |
} |
| 5576 |
|
| 5577 |
TEST(IsSupersetOfTest, WorksForNativeArray) { |
| 5578 |
const int subset[] = {1, 4}; |
| 5579 |
const int superset[] = {1, 2, 4}; |
| 5580 |
const int disjoint[] = {1, 0, 3}; |
| 5581 |
EXPECT_THAT(subset, IsSupersetOf(subset)); |
| 5582 |
EXPECT_THAT(subset, Not(IsSupersetOf(superset))); |
| 5583 |
EXPECT_THAT(superset, IsSupersetOf(subset)); |
| 5584 |
EXPECT_THAT(subset, Not(IsSupersetOf(disjoint))); |
| 5585 |
EXPECT_THAT(disjoint, Not(IsSupersetOf(subset))); |
| 5586 |
} |
| 5587 |
|
| 5588 |
TEST(IsSupersetOfTest, WorksWithDuplicates) { |
| 5589 |
const int not_enough[] = {1, 2}; |
| 5590 |
const int enough[] = {1, 1, 2}; |
| 5591 |
const int expected[] = {1, 1}; |
| 5592 |
EXPECT_THAT(not_enough, Not(IsSupersetOf(expected))); |
| 5593 |
EXPECT_THAT(enough, IsSupersetOf(expected)); |
| 5594 |
} |
| 5595 |
|
| 5596 |
TEST(IsSupersetOfTest, WorksForEmpty) { |
| 5597 |
vector<int> numbers; |
| 5598 |
vector<int> expected; |
| 5599 |
EXPECT_THAT(numbers, IsSupersetOf(expected)); |
| 5600 |
expected.push_back(1); |
| 5601 |
EXPECT_THAT(numbers, Not(IsSupersetOf(expected))); |
| 5602 |
expected.clear(); |
| 5603 |
numbers.push_back(1); |
| 5604 |
numbers.push_back(2); |
| 5605 |
EXPECT_THAT(numbers, IsSupersetOf(expected)); |
| 5606 |
expected.push_back(1); |
| 5607 |
EXPECT_THAT(numbers, IsSupersetOf(expected)); |
| 5608 |
expected.push_back(2); |
| 5609 |
EXPECT_THAT(numbers, IsSupersetOf(expected)); |
| 5610 |
expected.push_back(3); |
| 5611 |
EXPECT_THAT(numbers, Not(IsSupersetOf(expected))); |
| 5612 |
} |
| 5613 |
|
| 5614 |
TEST(IsSupersetOfTest, WorksForStreamlike) { |
| 5615 |
const int a[5] = {1, 2, 3, 4, 5}; |
| 5616 |
Streamlike<int> s(std::begin(a), std::end(a)); |
| 5617 |
|
| 5618 |
vector<int> expected; |
| 5619 |
expected.push_back(1); |
| 5620 |
expected.push_back(2); |
| 5621 |
expected.push_back(5); |
| 5622 |
EXPECT_THAT(s, IsSupersetOf(expected)); |
| 5623 |
|
| 5624 |
expected.push_back(0); |
| 5625 |
EXPECT_THAT(s, Not(IsSupersetOf(expected))); |
| 5626 |
} |
| 5627 |
|
| 5628 |
TEST(IsSupersetOfTest, TakesStlContainer) { |
| 5629 |
const int actual[] = {3, 1, 2}; |
| 5630 |
|
| 5631 |
::std::list<int> expected; |
| 5632 |
expected.push_back(1); |
| 5633 |
expected.push_back(3); |
| 5634 |
EXPECT_THAT(actual, IsSupersetOf(expected)); |
| 5635 |
|
| 5636 |
expected.push_back(4); |
| 5637 |
EXPECT_THAT(actual, Not(IsSupersetOf(expected))); |
| 5638 |
} |
| 5639 |
|
| 5640 |
TEST(IsSupersetOfTest, Describe) { |
| 5641 |
typedef std::vector<int> IntVec; |
| 5642 |
IntVec expected; |
| 5643 |
expected.push_back(111); |
| 5644 |
expected.push_back(222); |
| 5645 |
expected.push_back(333); |
| 5646 |
EXPECT_THAT( |
| 5647 |
Describe<IntVec>(IsSupersetOf(expected)), |
| 5648 |
Eq("a surjection from elements to requirements exists such that:\n" |
| 5649 |
" - an element is equal to 111\n" |
| 5650 |
" - an element is equal to 222\n" |
| 5651 |
" - an element is equal to 333")); |
| 5652 |
} |
| 5653 |
|
| 5654 |
TEST(IsSupersetOfTest, DescribeNegation) { |
| 5655 |
typedef std::vector<int> IntVec; |
| 5656 |
IntVec expected; |
| 5657 |
expected.push_back(111); |
| 5658 |
expected.push_back(222); |
| 5659 |
expected.push_back(333); |
| 5660 |
EXPECT_THAT( |
| 5661 |
DescribeNegation<IntVec>(IsSupersetOf(expected)), |
| 5662 |
Eq("no surjection from elements to requirements exists such that:\n" |
| 5663 |
" - an element is equal to 111\n" |
| 5664 |
" - an element is equal to 222\n" |
| 5665 |
" - an element is equal to 333")); |
| 5666 |
} |
| 5667 |
|
| 5668 |
TEST(IsSupersetOfTest, MatchAndExplain) { |
| 5669 |
std::vector<int> v; |
| 5670 |
v.push_back(2); |
| 5671 |
v.push_back(3); |
| 5672 |
std::vector<int> expected; |
| 5673 |
expected.push_back(1); |
| 5674 |
expected.push_back(2); |
| 5675 |
StringMatchResultListener listener; |
| 5676 |
ASSERT_FALSE(ExplainMatchResult(IsSupersetOf(expected), v, &listener)) |
| 5677 |
<< listener.str(); |
| 5678 |
EXPECT_THAT(listener.str(), |
| 5679 |
Eq("where the following matchers don't match any elements:\n" |
| 5680 |
"matcher #0: is equal to 1")); |
| 5681 |
|
| 5682 |
v.push_back(1); |
| 5683 |
listener.Clear(); |
| 5684 |
ASSERT_TRUE(ExplainMatchResult(IsSupersetOf(expected), v, &listener)) |
| 5685 |
<< listener.str(); |
| 5686 |
EXPECT_THAT(listener.str(), Eq("where:\n" |
| 5687 |
" - element #0 is matched by matcher #1,\n" |
| 5688 |
" - element #2 is matched by matcher #0")); |
| 5689 |
} |
| 5690 |
|
| 5691 |
TEST(IsSupersetOfTest, WorksForRhsInitializerList) { |
| 5692 |
const int numbers[] = {1, 3, 6, 2, 4, 5}; |
| 5693 |
EXPECT_THAT(numbers, IsSupersetOf({1, 2})); |
| 5694 |
EXPECT_THAT(numbers, Not(IsSupersetOf({3, 0}))); |
| 5695 |
} |
| 5696 |
|
| 5697 |
TEST(IsSupersetOfTest, WorksWithMoveOnly) { |
| 5698 |
ContainerHelper helper; |
| 5699 |
EXPECT_CALL(helper, Call(IsSupersetOf({Pointee(1)}))); |
| 5700 |
helper.Call(MakeUniquePtrs({1, 2})); |
| 5701 |
EXPECT_CALL(helper, Call(Not(IsSupersetOf({Pointee(1), Pointee(2)})))); |
| 5702 |
helper.Call(MakeUniquePtrs({2})); |
| 5703 |
} |
| 5704 |
|
| 5705 |
TEST(IsSubsetOfTest, WorksForNativeArray) { |
| 5706 |
const int subset[] = {1, 4}; |
| 5707 |
const int superset[] = {1, 2, 4}; |
| 5708 |
const int disjoint[] = {1, 0, 3}; |
| 5709 |
EXPECT_THAT(subset, IsSubsetOf(subset)); |
| 5710 |
EXPECT_THAT(subset, IsSubsetOf(superset)); |
| 5711 |
EXPECT_THAT(superset, Not(IsSubsetOf(subset))); |
| 5712 |
EXPECT_THAT(subset, Not(IsSubsetOf(disjoint))); |
| 5713 |
EXPECT_THAT(disjoint, Not(IsSubsetOf(subset))); |
| 5714 |
} |
| 5715 |
|
| 5716 |
TEST(IsSubsetOfTest, WorksWithDuplicates) { |
| 5717 |
const int not_enough[] = {1, 2}; |
| 5718 |
const int enough[] = {1, 1, 2}; |
| 5719 |
const int actual[] = {1, 1}; |
| 5720 |
EXPECT_THAT(actual, Not(IsSubsetOf(not_enough))); |
| 5721 |
EXPECT_THAT(actual, IsSubsetOf(enough)); |
| 5722 |
} |
| 5723 |
|
| 5724 |
TEST(IsSubsetOfTest, WorksForEmpty) { |
| 5725 |
vector<int> numbers; |
| 5726 |
vector<int> expected; |
| 5727 |
EXPECT_THAT(numbers, IsSubsetOf(expected)); |
| 5728 |
expected.push_back(1); |
| 5729 |
EXPECT_THAT(numbers, IsSubsetOf(expected)); |
| 5730 |
expected.clear(); |
| 5731 |
numbers.push_back(1); |
| 5732 |
numbers.push_back(2); |
| 5733 |
EXPECT_THAT(numbers, Not(IsSubsetOf(expected))); |
| 5734 |
expected.push_back(1); |
| 5735 |
EXPECT_THAT(numbers, Not(IsSubsetOf(expected))); |
| 5736 |
expected.push_back(2); |
| 5737 |
EXPECT_THAT(numbers, IsSubsetOf(expected)); |
| 5738 |
expected.push_back(3); |
| 5739 |
EXPECT_THAT(numbers, IsSubsetOf(expected)); |
| 5740 |
} |
| 5741 |
|
| 5742 |
TEST(IsSubsetOfTest, WorksForStreamlike) { |
| 5743 |
const int a[5] = {1, 2}; |
| 5744 |
Streamlike<int> s(std::begin(a), std::end(a)); |
| 5745 |
|
| 5746 |
vector<int> expected; |
| 5747 |
expected.push_back(1); |
| 5748 |
EXPECT_THAT(s, Not(IsSubsetOf(expected))); |
| 5749 |
expected.push_back(2); |
| 5750 |
expected.push_back(5); |
| 5751 |
EXPECT_THAT(s, IsSubsetOf(expected)); |
| 5752 |
} |
| 5753 |
|
| 5754 |
TEST(IsSubsetOfTest, TakesStlContainer) { |
| 5755 |
const int actual[] = {3, 1, 2}; |
| 5756 |
|
| 5757 |
::std::list<int> expected; |
| 5758 |
expected.push_back(1); |
| 5759 |
expected.push_back(3); |
| 5760 |
EXPECT_THAT(actual, Not(IsSubsetOf(expected))); |
| 5761 |
|
| 5762 |
expected.push_back(2); |
| 5763 |
expected.push_back(4); |
| 5764 |
EXPECT_THAT(actual, IsSubsetOf(expected)); |
| 5765 |
} |
| 5766 |
|
| 5767 |
TEST(IsSubsetOfTest, Describe) { |
| 5768 |
typedef std::vector<int> IntVec; |
| 5769 |
IntVec expected; |
| 5770 |
expected.push_back(111); |
| 5771 |
expected.push_back(222); |
| 5772 |
expected.push_back(333); |
| 5773 |
|
| 5774 |
EXPECT_THAT( |
| 5775 |
Describe<IntVec>(IsSubsetOf(expected)), |
| 5776 |
Eq("an injection from elements to requirements exists such that:\n" |
| 5777 |
" - an element is equal to 111\n" |
| 5778 |
" - an element is equal to 222\n" |
| 5779 |
" - an element is equal to 333")); |
| 5780 |
} |
| 5781 |
|
| 5782 |
TEST(IsSubsetOfTest, DescribeNegation) { |
| 5783 |
typedef std::vector<int> IntVec; |
| 5784 |
IntVec expected; |
| 5785 |
expected.push_back(111); |
| 5786 |
expected.push_back(222); |
| 5787 |
expected.push_back(333); |
| 5788 |
EXPECT_THAT( |
| 5789 |
DescribeNegation<IntVec>(IsSubsetOf(expected)), |
| 5790 |
Eq("no injection from elements to requirements exists such that:\n" |
| 5791 |
" - an element is equal to 111\n" |
| 5792 |
" - an element is equal to 222\n" |
| 5793 |
" - an element is equal to 333")); |
| 5794 |
} |
| 5795 |
|
| 5796 |
TEST(IsSubsetOfTest, MatchAndExplain) { |
| 5797 |
std::vector<int> v; |
| 5798 |
v.push_back(2); |
| 5799 |
v.push_back(3); |
| 5800 |
std::vector<int> expected; |
| 5801 |
expected.push_back(1); |
| 5802 |
expected.push_back(2); |
| 5803 |
StringMatchResultListener listener; |
| 5804 |
ASSERT_FALSE(ExplainMatchResult(IsSubsetOf(expected), v, &listener)) |
| 5805 |
<< listener.str(); |
| 5806 |
EXPECT_THAT(listener.str(), |
| 5807 |
Eq("where the following elements don't match any matchers:\n" |
| 5808 |
"element #1: 3")); |
| 5809 |
|
| 5810 |
expected.push_back(3); |
| 5811 |
listener.Clear(); |
| 5812 |
ASSERT_TRUE(ExplainMatchResult(IsSubsetOf(expected), v, &listener)) |
| 5813 |
<< listener.str(); |
| 5814 |
EXPECT_THAT(listener.str(), Eq("where:\n" |
| 5815 |
" - element #0 is matched by matcher #1,\n" |
| 5816 |
" - element #1 is matched by matcher #2")); |
| 5817 |
} |
| 5818 |
|
| 5819 |
TEST(IsSubsetOfTest, WorksForRhsInitializerList) { |
| 5820 |
const int numbers[] = {1, 2, 3}; |
| 5821 |
EXPECT_THAT(numbers, IsSubsetOf({1, 2, 3, 4})); |
| 5822 |
EXPECT_THAT(numbers, Not(IsSubsetOf({1, 2}))); |
| 5823 |
} |
| 5824 |
|
| 5825 |
TEST(IsSubsetOfTest, WorksWithMoveOnly) { |
| 5826 |
ContainerHelper helper; |
| 5827 |
EXPECT_CALL(helper, Call(IsSubsetOf({Pointee(1), Pointee(2)}))); |
| 5828 |
helper.Call(MakeUniquePtrs({1})); |
| 5829 |
EXPECT_CALL(helper, Call(Not(IsSubsetOf({Pointee(1)})))); |
| 5830 |
helper.Call(MakeUniquePtrs({2})); |
| 5831 |
} |
| 5832 |
|
| 5833 |
// Tests using ElementsAre() and ElementsAreArray() with stream-like |
| 5834 |
// "containers". |
| 5835 |
|
| 5836 |
TEST(ElemensAreStreamTest, WorksForStreamlike) { |
| 5837 |
const int a[5] = {1, 2, 3, 4, 5}; |
| 5838 |
Streamlike<int> s(std::begin(a), std::end(a)); |
| 5839 |
EXPECT_THAT(s, ElementsAre(1, 2, 3, 4, 5)); |
| 5840 |
EXPECT_THAT(s, Not(ElementsAre(2, 1, 4, 5, 3))); |
| 5841 |
} |
| 5842 |
|
| 5843 |
TEST(ElemensAreArrayStreamTest, WorksForStreamlike) { |
| 5844 |
const int a[5] = {1, 2, 3, 4, 5}; |
| 5845 |
Streamlike<int> s(std::begin(a), std::end(a)); |
| 5846 |
|
| 5847 |
vector<int> expected; |
| 5848 |
expected.push_back(1); |
| 5849 |
expected.push_back(2); |
| 5850 |
expected.push_back(3); |
| 5851 |
expected.push_back(4); |
| 5852 |
expected.push_back(5); |
| 5853 |
EXPECT_THAT(s, ElementsAreArray(expected)); |
| 5854 |
|
| 5855 |
expected[3] = 0; |
| 5856 |
EXPECT_THAT(s, Not(ElementsAreArray(expected))); |
| 5857 |
} |
| 5858 |
|
| 5859 |
TEST(ElementsAreTest, WorksWithUncopyable) { |
| 5860 |
Uncopyable objs[2]; |
| 5861 |
objs[0].set_value(-3); |
| 5862 |
objs[1].set_value(1); |
| 5863 |
EXPECT_THAT(objs, ElementsAre(UncopyableIs(-3), Truly(ValueIsPositive))); |
| 5864 |
} |
| 5865 |
|
| 5866 |
TEST(ElementsAreTest, WorksWithMoveOnly) { |
| 5867 |
ContainerHelper helper; |
| 5868 |
EXPECT_CALL(helper, Call(ElementsAre(Pointee(1), Pointee(2)))); |
| 5869 |
helper.Call(MakeUniquePtrs({1, 2})); |
| 5870 |
|
| 5871 |
EXPECT_CALL(helper, Call(ElementsAreArray({Pointee(3), Pointee(4)}))); |
| 5872 |
helper.Call(MakeUniquePtrs({3, 4})); |
| 5873 |
} |
| 5874 |
|
| 5875 |
TEST(ElementsAreTest, TakesStlContainer) { |
| 5876 |
const int actual[] = {3, 1, 2}; |
| 5877 |
|
| 5878 |
::std::list<int> expected; |
| 5879 |
expected.push_back(3); |
| 5880 |
expected.push_back(1); |
| 5881 |
expected.push_back(2); |
| 5882 |
EXPECT_THAT(actual, ElementsAreArray(expected)); |
| 5883 |
|
| 5884 |
expected.push_back(4); |
| 5885 |
EXPECT_THAT(actual, Not(ElementsAreArray(expected))); |
| 5886 |
} |
| 5887 |
|
| 5888 |
// Tests for UnorderedElementsAreArray() |
| 5889 |
|
| 5890 |
TEST(UnorderedElementsAreArrayTest, SucceedsWhenExpected) { |
| 5891 |
const int a[] = {0, 1, 2, 3, 4}; |
| 5892 |
std::vector<int> s(std::begin(a), std::end(a)); |
| 5893 |
do { |
| 5894 |
StringMatchResultListener listener; |
| 5895 |
EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(a), |
| 5896 |
s, &listener)) << listener.str(); |
| 5897 |
} while (std::next_permutation(s.begin(), s.end())); |
| 5898 |
} |
| 5899 |
|
| 5900 |
TEST(UnorderedElementsAreArrayTest, VectorBool) { |
| 5901 |
const bool a[] = {0, 1, 0, 1, 1}; |
| 5902 |
const bool b[] = {1, 0, 1, 1, 0}; |
| 5903 |
std::vector<bool> expected(std::begin(a), std::end(a)); |
| 5904 |
std::vector<bool> actual(std::begin(b), std::end(b)); |
| 5905 |
StringMatchResultListener listener; |
| 5906 |
EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(expected), |
| 5907 |
actual, &listener)) << listener.str(); |
| 5908 |
} |
| 5909 |
|
| 5910 |
TEST(UnorderedElementsAreArrayTest, WorksForStreamlike) { |
| 5911 |
// Streamlike 'container' provides only minimal iterator support. |
| 5912 |
// Its iterators are tagged with input_iterator_tag, and it has no |
| 5913 |
// size() or empty() methods. |
| 5914 |
const int a[5] = {2, 1, 4, 5, 3}; |
| 5915 |
Streamlike<int> s(std::begin(a), std::end(a)); |
| 5916 |
|
| 5917 |
::std::vector<int> expected; |
| 5918 |
expected.push_back(1); |
| 5919 |
expected.push_back(2); |
| 5920 |
expected.push_back(3); |
| 5921 |
expected.push_back(4); |
| 5922 |
expected.push_back(5); |
| 5923 |
EXPECT_THAT(s, UnorderedElementsAreArray(expected)); |
| 5924 |
|
| 5925 |
expected.push_back(6); |
| 5926 |
EXPECT_THAT(s, Not(UnorderedElementsAreArray(expected))); |
| 5927 |
} |
| 5928 |
|
| 5929 |
TEST(UnorderedElementsAreArrayTest, TakesStlContainer) { |
| 5930 |
const int actual[] = {3, 1, 2}; |
| 5931 |
|
| 5932 |
::std::list<int> expected; |
| 5933 |
expected.push_back(1); |
| 5934 |
expected.push_back(2); |
| 5935 |
expected.push_back(3); |
| 5936 |
EXPECT_THAT(actual, UnorderedElementsAreArray(expected)); |
| 5937 |
|
| 5938 |
expected.push_back(4); |
| 5939 |
EXPECT_THAT(actual, Not(UnorderedElementsAreArray(expected))); |
| 5940 |
} |
| 5941 |
|
| 5942 |
|
| 5943 |
TEST(UnorderedElementsAreArrayTest, TakesInitializerList) { |
| 5944 |
const int a[5] = {2, 1, 4, 5, 3}; |
| 5945 |
EXPECT_THAT(a, UnorderedElementsAreArray({1, 2, 3, 4, 5})); |
| 5946 |
EXPECT_THAT(a, Not(UnorderedElementsAreArray({1, 2, 3, 4, 6}))); |
| 5947 |
} |
| 5948 |
|
| 5949 |
TEST(UnorderedElementsAreArrayTest, TakesInitializerListOfCStrings) { |
| 5950 |
const std::string a[5] = {"a", "b", "c", "d", "e"}; |
| 5951 |
EXPECT_THAT(a, UnorderedElementsAreArray({"a", "b", "c", "d", "e"})); |
| 5952 |
EXPECT_THAT(a, Not(UnorderedElementsAreArray({"a", "b", "c", "d", "ef"}))); |
| 5953 |
} |
| 5954 |
|
| 5955 |
TEST(UnorderedElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) { |
| 5956 |
const int a[5] = {2, 1, 4, 5, 3}; |
| 5957 |
EXPECT_THAT(a, UnorderedElementsAreArray( |
| 5958 |
{Eq(1), Eq(2), Eq(3), Eq(4), Eq(5)})); |
| 5959 |
EXPECT_THAT(a, Not(UnorderedElementsAreArray( |
| 5960 |
{Eq(1), Eq(2), Eq(3), Eq(4), Eq(6)}))); |
| 5961 |
} |
| 5962 |
|
| 5963 |
TEST(UnorderedElementsAreArrayTest, |
| 5964 |
TakesInitializerListOfDifferentTypedMatchers) { |
| 5965 |
const int a[5] = {2, 1, 4, 5, 3}; |
| 5966 |
// The compiler cannot infer the type of the initializer list if its |
| 5967 |
// elements have different types. We must explicitly specify the |
| 5968 |
// unified element type in this case. |
| 5969 |
EXPECT_THAT(a, UnorderedElementsAreArray<Matcher<int> >( |
| 5970 |
{Eq(1), Ne(-2), Ge(3), Le(4), Eq(5)})); |
| 5971 |
EXPECT_THAT(a, Not(UnorderedElementsAreArray<Matcher<int> >( |
| 5972 |
{Eq(1), Ne(-2), Ge(3), Le(4), Eq(6)}))); |
| 5973 |
} |
| 5974 |
|
| 5975 |
|
| 5976 |
TEST(UnorderedElementsAreArrayTest, WorksWithMoveOnly) { |
| 5977 |
ContainerHelper helper; |
| 5978 |
EXPECT_CALL(helper, |
| 5979 |
Call(UnorderedElementsAreArray({Pointee(1), Pointee(2)}))); |
| 5980 |
helper.Call(MakeUniquePtrs({2, 1})); |
| 5981 |
} |
| 5982 |
|
| 5983 |
class UnorderedElementsAreTest : public testing::Test { |
| 5984 |
protected: |
| 5985 |
typedef std::vector<int> IntVec; |
| 5986 |
}; |
| 5987 |
|
| 5988 |
TEST_F(UnorderedElementsAreTest, WorksWithUncopyable) { |
| 5989 |
Uncopyable objs[2]; |
| 5990 |
objs[0].set_value(-3); |
| 5991 |
objs[1].set_value(1); |
| 5992 |
EXPECT_THAT(objs, |
| 5993 |
UnorderedElementsAre(Truly(ValueIsPositive), UncopyableIs(-3))); |
| 5994 |
} |
| 5995 |
|
| 5996 |
TEST_F(UnorderedElementsAreTest, SucceedsWhenExpected) { |
| 5997 |
const int a[] = {1, 2, 3}; |
| 5998 |
std::vector<int> s(std::begin(a), std::end(a)); |
| 5999 |
do { |
| 6000 |
StringMatchResultListener listener; |
| 6001 |
EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3), |
| 6002 |
s, &listener)) << listener.str(); |
| 6003 |
} while (std::next_permutation(s.begin(), s.end())); |
| 6004 |
} |
| 6005 |
|
| 6006 |
TEST_F(UnorderedElementsAreTest, FailsWhenAnElementMatchesNoMatcher) { |
| 6007 |
const int a[] = {1, 2, 3}; |
| 6008 |
std::vector<int> s(std::begin(a), std::end(a)); |
| 6009 |
std::vector<Matcher<int> > mv; |
| 6010 |
mv.push_back(1); |
| 6011 |
mv.push_back(2); |
| 6012 |
mv.push_back(2); |
| 6013 |
// The element with value '3' matches nothing: fail fast. |
| 6014 |
StringMatchResultListener listener; |
| 6015 |
EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAreArray(mv), |
| 6016 |
s, &listener)) << listener.str(); |
| 6017 |
} |
| 6018 |
|
| 6019 |
TEST_F(UnorderedElementsAreTest, WorksForStreamlike) { |
| 6020 |
// Streamlike 'container' provides only minimal iterator support. |
| 6021 |
// Its iterators are tagged with input_iterator_tag, and it has no |
| 6022 |
// size() or empty() methods. |
| 6023 |
const int a[5] = {2, 1, 4, 5, 3}; |
| 6024 |
Streamlike<int> s(std::begin(a), std::end(a)); |
| 6025 |
|
| 6026 |
EXPECT_THAT(s, UnorderedElementsAre(1, 2, 3, 4, 5)); |
| 6027 |
EXPECT_THAT(s, Not(UnorderedElementsAre(2, 2, 3, 4, 5))); |
| 6028 |
} |
| 6029 |
|
| 6030 |
TEST_F(UnorderedElementsAreTest, WorksWithMoveOnly) { |
| 6031 |
ContainerHelper helper; |
| 6032 |
EXPECT_CALL(helper, Call(UnorderedElementsAre(Pointee(1), Pointee(2)))); |
| 6033 |
helper.Call(MakeUniquePtrs({2, 1})); |
| 6034 |
} |
| 6035 |
|
| 6036 |
// One naive implementation of the matcher runs in O(N!) time, which is too |
| 6037 |
// slow for many real-world inputs. This test shows that our matcher can match |
| 6038 |
// 100 inputs very quickly (a few milliseconds). An O(100!) is 10^158 |
| 6039 |
// iterations and obviously effectively incomputable. |
| 6040 |
// [ RUN ] UnorderedElementsAreTest.Performance |
| 6041 |
// [ OK ] UnorderedElementsAreTest.Performance (4 ms) |
| 6042 |
TEST_F(UnorderedElementsAreTest, Performance) { |
| 6043 |
std::vector<int> s; |
| 6044 |
std::vector<Matcher<int> > mv; |
| 6045 |
for (int i = 0; i < 100; ++i) { |
| 6046 |
s.push_back(i); |
| 6047 |
mv.push_back(_); |
| 6048 |
} |
| 6049 |
mv[50] = Eq(0); |
| 6050 |
StringMatchResultListener listener; |
| 6051 |
EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(mv), |
| 6052 |
s, &listener)) << listener.str(); |
| 6053 |
} |
| 6054 |
|
| 6055 |
// Another variant of 'Performance' with similar expectations. |
| 6056 |
// [ RUN ] UnorderedElementsAreTest.PerformanceHalfStrict |
| 6057 |
// [ OK ] UnorderedElementsAreTest.PerformanceHalfStrict (4 ms) |
| 6058 |
TEST_F(UnorderedElementsAreTest, PerformanceHalfStrict) { |
| 6059 |
std::vector<int> s; |
| 6060 |
std::vector<Matcher<int> > mv; |
| 6061 |
for (int i = 0; i < 100; ++i) { |
| 6062 |
s.push_back(i); |
| 6063 |
if (i & 1) { |
| 6064 |
mv.push_back(_); |
| 6065 |
} else { |
| 6066 |
mv.push_back(i); |
| 6067 |
} |
| 6068 |
} |
| 6069 |
StringMatchResultListener listener; |
| 6070 |
EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(mv), |
| 6071 |
s, &listener)) << listener.str(); |
| 6072 |
} |
| 6073 |
|
| 6074 |
TEST_F(UnorderedElementsAreTest, FailMessageCountWrong) { |
| 6075 |
std::vector<int> v; |
| 6076 |
v.push_back(4); |
| 6077 |
StringMatchResultListener listener; |
| 6078 |
EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3), |
| 6079 |
v, &listener)) << listener.str(); |
| 6080 |
EXPECT_THAT(listener.str(), Eq("which has 1 element")); |
| 6081 |
} |
| 6082 |
|
| 6083 |
TEST_F(UnorderedElementsAreTest, FailMessageCountWrongZero) { |
| 6084 |
std::vector<int> v; |
| 6085 |
StringMatchResultListener listener; |
| 6086 |
EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3), |
| 6087 |
v, &listener)) << listener.str(); |
| 6088 |
EXPECT_THAT(listener.str(), Eq("")); |
| 6089 |
} |
| 6090 |
|
| 6091 |
TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedMatchers) { |
| 6092 |
std::vector<int> v; |
| 6093 |
v.push_back(1); |
| 6094 |
v.push_back(1); |
| 6095 |
StringMatchResultListener listener; |
| 6096 |
EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2), |
| 6097 |
v, &listener)) << listener.str(); |
| 6098 |
EXPECT_THAT( |
| 6099 |
listener.str(), |
| 6100 |
Eq("where the following matchers don't match any elements:\n" |
| 6101 |
"matcher #1: is equal to 2")); |
| 6102 |
} |
| 6103 |
|
| 6104 |
TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedElements) { |
| 6105 |
std::vector<int> v; |
| 6106 |
v.push_back(1); |
| 6107 |
v.push_back(2); |
| 6108 |
StringMatchResultListener listener; |
| 6109 |
EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 1), |
| 6110 |
v, &listener)) << listener.str(); |
| 6111 |
EXPECT_THAT( |
| 6112 |
listener.str(), |
| 6113 |
Eq("where the following elements don't match any matchers:\n" |
| 6114 |
"element #1: 2")); |
| 6115 |
} |
| 6116 |
|
| 6117 |
TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedMatcherAndElement) { |
| 6118 |
std::vector<int> v; |
| 6119 |
v.push_back(2); |
| 6120 |
v.push_back(3); |
| 6121 |
StringMatchResultListener listener; |
| 6122 |
EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2), |
| 6123 |
v, &listener)) << listener.str(); |
| 6124 |
EXPECT_THAT( |
| 6125 |
listener.str(), |
| 6126 |
Eq("where" |
| 6127 |
" the following matchers don't match any elements:\n" |
| 6128 |
"matcher #0: is equal to 1\n" |
| 6129 |
"and" |
| 6130 |
" where" |
| 6131 |
" the following elements don't match any matchers:\n" |
| 6132 |
"element #1: 3")); |
| 6133 |
} |
| 6134 |
|
| 6135 |
// Test helper for formatting element, matcher index pairs in expectations. |
| 6136 |
static std::string EMString(int element, int matcher) { |
| 6137 |
stringstream ss; |
| 6138 |
ss << "(element #" << element << ", matcher #" << matcher << ")"; |
| 6139 |
return ss.str(); |
| 6140 |
} |
| 6141 |
|
| 6142 |
TEST_F(UnorderedElementsAreTest, FailMessageImperfectMatchOnly) { |
| 6143 |
// A situation where all elements and matchers have a match |
| 6144 |
// associated with them, but the max matching is not perfect. |
| 6145 |
std::vector<std::string> v; |
| 6146 |
v.push_back("a"); |
| 6147 |
v.push_back("b"); |
| 6148 |
v.push_back("c"); |
| 6149 |
StringMatchResultListener listener; |
| 6150 |
EXPECT_FALSE(ExplainMatchResult( |
| 6151 |
UnorderedElementsAre("a", "a", AnyOf("b", "c")), v, &listener)) |
| 6152 |
<< listener.str(); |
| 6153 |
|
| 6154 |
std::string prefix = |
| 6155 |
"where no permutation of the elements can satisfy all matchers, " |
| 6156 |
"and the closest match is 2 of 3 matchers with the " |
| 6157 |
"pairings:\n"; |
| 6158 |
|
| 6159 |
// We have to be a bit loose here, because there are 4 valid max matches. |
| 6160 |
EXPECT_THAT( |
| 6161 |
listener.str(), |
| 6162 |
AnyOf(prefix + "{\n " + EMString(0, 0) + |
| 6163 |
",\n " + EMString(1, 2) + "\n}", |
| 6164 |
prefix + "{\n " + EMString(0, 1) + |
| 6165 |
",\n " + EMString(1, 2) + "\n}", |
| 6166 |
prefix + "{\n " + EMString(0, 0) + |
| 6167 |
",\n " + EMString(2, 2) + "\n}", |
| 6168 |
prefix + "{\n " + EMString(0, 1) + |
| 6169 |
",\n " + EMString(2, 2) + "\n}")); |
| 6170 |
} |
| 6171 |
|
| 6172 |
TEST_F(UnorderedElementsAreTest, Describe) { |
| 6173 |
EXPECT_THAT(Describe<IntVec>(UnorderedElementsAre()), |
| 6174 |
Eq("is empty")); |
| 6175 |
EXPECT_THAT( |
| 6176 |
Describe<IntVec>(UnorderedElementsAre(345)), |
| 6177 |
Eq("has 1 element and that element is equal to 345")); |
| 6178 |
EXPECT_THAT( |
| 6179 |
Describe<IntVec>(UnorderedElementsAre(111, 222, 333)), |
| 6180 |
Eq("has 3 elements and there exists some permutation " |
| 6181 |
"of elements such that:\n" |
| 6182 |
" - element #0 is equal to 111, and\n" |
| 6183 |
" - element #1 is equal to 222, and\n" |
| 6184 |
" - element #2 is equal to 333")); |
| 6185 |
} |
| 6186 |
|
| 6187 |
TEST_F(UnorderedElementsAreTest, DescribeNegation) { |
| 6188 |
EXPECT_THAT(DescribeNegation<IntVec>(UnorderedElementsAre()), |
| 6189 |
Eq("isn't empty")); |
| 6190 |
EXPECT_THAT( |
| 6191 |
DescribeNegation<IntVec>(UnorderedElementsAre(345)), |
| 6192 |
Eq("doesn't have 1 element, or has 1 element that isn't equal to 345")); |
| 6193 |
EXPECT_THAT( |
| 6194 |
DescribeNegation<IntVec>(UnorderedElementsAre(123, 234, 345)), |
| 6195 |
Eq("doesn't have 3 elements, or there exists no permutation " |
| 6196 |
"of elements such that:\n" |
| 6197 |
" - element #0 is equal to 123, and\n" |
| 6198 |
" - element #1 is equal to 234, and\n" |
| 6199 |
" - element #2 is equal to 345")); |
| 6200 |
} |
| 6201 |
|
| 6202 |
namespace { |
| 6203 |
|
| 6204 |
// Used as a check on the more complex max flow method used in the |
| 6205 |
// real testing::internal::FindMaxBipartiteMatching. This method is |
| 6206 |
// compatible but runs in worst-case factorial time, so we only |
| 6207 |
// use it in testing for small problem sizes. |
| 6208 |
template <typename Graph> |
| 6209 |
class BacktrackingMaxBPMState { |
| 6210 |
public: |
| 6211 |
// Does not take ownership of 'g'. |
| 6212 |
explicit BacktrackingMaxBPMState(const Graph* g) : graph_(g) { } |
| 6213 |
|
| 6214 |
ElementMatcherPairs Compute() { |
| 6215 |
if (graph_->LhsSize() == 0 || graph_->RhsSize() == 0) { |
| 6216 |
return best_so_far_; |
| 6217 |
} |
| 6218 |
lhs_used_.assign(graph_->LhsSize(), kUnused); |
| 6219 |
rhs_used_.assign(graph_->RhsSize(), kUnused); |
| 6220 |
for (size_t irhs = 0; irhs < graph_->RhsSize(); ++irhs) { |
| 6221 |
matches_.clear(); |
| 6222 |
RecurseInto(irhs); |
| 6223 |
if (best_so_far_.size() == graph_->RhsSize()) |
| 6224 |
break; |
| 6225 |
} |
| 6226 |
return best_so_far_; |
| 6227 |
} |
| 6228 |
|
| 6229 |
private: |
| 6230 |
static const size_t kUnused = static_cast<size_t>(-1); |
| 6231 |
|
| 6232 |
void PushMatch(size_t lhs, size_t rhs) { |
| 6233 |
matches_.push_back(ElementMatcherPair(lhs, rhs)); |
| 6234 |
lhs_used_[lhs] = rhs; |
| 6235 |
rhs_used_[rhs] = lhs; |
| 6236 |
if (matches_.size() > best_so_far_.size()) { |
| 6237 |
best_so_far_ = matches_; |
| 6238 |
} |
| 6239 |
} |
| 6240 |
|
| 6241 |
void PopMatch() { |
| 6242 |
const ElementMatcherPair& back = matches_.back(); |
| 6243 |
lhs_used_[back.first] = kUnused; |
| 6244 |
rhs_used_[back.second] = kUnused; |
| 6245 |
matches_.pop_back(); |
| 6246 |
} |
| 6247 |
|
| 6248 |
bool RecurseInto(size_t irhs) { |
| 6249 |
if (rhs_used_[irhs] != kUnused) { |
| 6250 |
return true; |
| 6251 |
} |
| 6252 |
for (size_t ilhs = 0; ilhs < graph_->LhsSize(); ++ilhs) { |
| 6253 |
if (lhs_used_[ilhs] != kUnused) { |
| 6254 |
continue; |
| 6255 |
} |
| 6256 |
if (!graph_->HasEdge(ilhs, irhs)) { |
| 6257 |
continue; |
| 6258 |
} |
| 6259 |
PushMatch(ilhs, irhs); |
| 6260 |
if (best_so_far_.size() == graph_->RhsSize()) { |
| 6261 |
return false; |
| 6262 |
} |
| 6263 |
for (size_t mi = irhs + 1; mi < graph_->RhsSize(); ++mi) { |
| 6264 |
if (!RecurseInto(mi)) return false; |
| 6265 |
} |
| 6266 |
PopMatch(); |
| 6267 |
} |
| 6268 |
return true; |
| 6269 |
} |
| 6270 |
|
| 6271 |
const Graph* graph_; // not owned |
| 6272 |
std::vector<size_t> lhs_used_; |
| 6273 |
std::vector<size_t> rhs_used_; |
| 6274 |
ElementMatcherPairs matches_; |
| 6275 |
ElementMatcherPairs best_so_far_; |
| 6276 |
}; |
| 6277 |
|
| 6278 |
template <typename Graph> |
| 6279 |
const size_t BacktrackingMaxBPMState<Graph>::kUnused; |
| 6280 |
|
| 6281 |
} // namespace |
| 6282 |
|
| 6283 |
// Implement a simple backtracking algorithm to determine if it is possible |
| 6284 |
// to find one element per matcher, without reusing elements. |
| 6285 |
template <typename Graph> |
| 6286 |
ElementMatcherPairs |
| 6287 |
FindBacktrackingMaxBPM(const Graph& g) { |
| 6288 |
return BacktrackingMaxBPMState<Graph>(&g).Compute(); |
| 6289 |
} |
| 6290 |
|
| 6291 |
class BacktrackingBPMTest : public ::testing::Test { }; |
| 6292 |
|
| 6293 |
// Tests the MaxBipartiteMatching algorithm with square matrices. |
| 6294 |
// The single int param is the # of nodes on each of the left and right sides. |
| 6295 |
class BipartiteTest : public ::testing::TestWithParam<size_t> {}; |
| 6296 |
|
| 6297 |
// Verify all match graphs up to some moderate number of edges. |
| 6298 |
TEST_P(BipartiteTest, Exhaustive) { |
| 6299 |
size_t nodes = GetParam(); |
| 6300 |
MatchMatrix graph(nodes, nodes); |
| 6301 |
do { |
| 6302 |
ElementMatcherPairs matches = |
| 6303 |
internal::FindMaxBipartiteMatching(graph); |
| 6304 |
EXPECT_EQ(FindBacktrackingMaxBPM(graph).size(), matches.size()) |
| 6305 |
<< "graph: " << graph.DebugString(); |
| 6306 |
// Check that all elements of matches are in the graph. |
| 6307 |
// Check that elements of first and second are unique. |
| 6308 |
std::vector<bool> seen_element(graph.LhsSize()); |
| 6309 |
std::vector<bool> seen_matcher(graph.RhsSize()); |
| 6310 |
SCOPED_TRACE(PrintToString(matches)); |
| 6311 |
for (size_t i = 0; i < matches.size(); ++i) { |
| 6312 |
size_t ilhs = matches[i].first; |
| 6313 |
size_t irhs = matches[i].second; |
| 6314 |
EXPECT_TRUE(graph.HasEdge(ilhs, irhs)); |
| 6315 |
EXPECT_FALSE(seen_element[ilhs]); |
| 6316 |
EXPECT_FALSE(seen_matcher[irhs]); |
| 6317 |
seen_element[ilhs] = true; |
| 6318 |
seen_matcher[irhs] = true; |
| 6319 |
} |
| 6320 |
} while (graph.NextGraph()); |
| 6321 |
} |
| 6322 |
|
| 6323 |
INSTANTIATE_TEST_SUITE_P(AllGraphs, BipartiteTest, |
| 6324 |
::testing::Range(size_t{0}, size_t{5})); |
| 6325 |
|
| 6326 |
// Parameterized by a pair interpreted as (LhsSize, RhsSize). |
| 6327 |
class BipartiteNonSquareTest |
| 6328 |
: public ::testing::TestWithParam<std::pair<size_t, size_t> > { |
| 6329 |
}; |
| 6330 |
|
| 6331 |
TEST_F(BipartiteNonSquareTest, SimpleBacktracking) { |
| 6332 |
// ....... |
| 6333 |
// 0:-----\ : |
| 6334 |
// 1:---\ | : |
| 6335 |
// 2:---\ | : |
| 6336 |
// 3:-\ | | : |
| 6337 |
// :.......: |
| 6338 |
// 0 1 2 |
| 6339 |
MatchMatrix g(4, 3); |
| 6340 |
constexpr std::array<std::array<size_t, 2>, 4> kEdges = { |
| 6341 |
{{{0, 2}}, {{1, 1}}, {{2, 1}}, {{3, 0}}}}; |
| 6342 |
for (size_t i = 0; i < kEdges.size(); ++i) { |
| 6343 |
g.SetEdge(kEdges[i][0], kEdges[i][1], true); |
| 6344 |
} |
| 6345 |
EXPECT_THAT(FindBacktrackingMaxBPM(g), |
| 6346 |
ElementsAre(Pair(3, 0), |
| 6347 |
Pair(AnyOf(1, 2), 1), |
| 6348 |
Pair(0, 2))) << g.DebugString(); |
| 6349 |
} |
| 6350 |
|
| 6351 |
// Verify a few nonsquare matrices. |
| 6352 |
TEST_P(BipartiteNonSquareTest, Exhaustive) { |
| 6353 |
size_t nlhs = GetParam().first; |
| 6354 |
size_t nrhs = GetParam().second; |
| 6355 |
MatchMatrix graph(nlhs, nrhs); |
| 6356 |
do { |
| 6357 |
EXPECT_EQ(FindBacktrackingMaxBPM(graph).size(), |
| 6358 |
internal::FindMaxBipartiteMatching(graph).size()) |
| 6359 |
<< "graph: " << graph.DebugString() |
| 6360 |
<< "\nbacktracking: " |
| 6361 |
<< PrintToString(FindBacktrackingMaxBPM(graph)) |
| 6362 |
<< "\nmax flow: " |
| 6363 |
<< PrintToString(internal::FindMaxBipartiteMatching(graph)); |
| 6364 |
} while (graph.NextGraph()); |
| 6365 |
} |
| 6366 |
|
| 6367 |
INSTANTIATE_TEST_SUITE_P(AllGraphs, BipartiteNonSquareTest, |
| 6368 |
testing::Values( |
| 6369 |
std::make_pair(1, 2), |
| 6370 |
std::make_pair(2, 1), |
| 6371 |
std::make_pair(3, 2), |
| 6372 |
std::make_pair(2, 3), |
| 6373 |
std::make_pair(4, 1), |
| 6374 |
std::make_pair(1, 4), |
| 6375 |
std::make_pair(4, 3), |
| 6376 |
std::make_pair(3, 4))); |
| 6377 |
|
| 6378 |
class BipartiteRandomTest |
| 6379 |
: public ::testing::TestWithParam<std::pair<int, int> > { |
| 6380 |
}; |
| 6381 |
|
| 6382 |
// Verifies a large sample of larger graphs. |
| 6383 |
TEST_P(BipartiteRandomTest, LargerNets) { |
| 6384 |
int nodes = GetParam().first; |
| 6385 |
int iters = GetParam().second; |
| 6386 |
MatchMatrix graph(static_cast<size_t>(nodes), static_cast<size_t>(nodes)); |
| 6387 |
|
| 6388 |
auto seed = static_cast<uint32_t>(GTEST_FLAG_GET(random_seed)); |
| 6389 |
if (seed == 0) { |
| 6390 |
seed = static_cast<uint32_t>(time(nullptr)); |
| 6391 |
} |
| 6392 |
|
| 6393 |
for (; iters > 0; --iters, ++seed) { |
| 6394 |
srand(static_cast<unsigned int>(seed)); |
| 6395 |
graph.Randomize(); |
| 6396 |
EXPECT_EQ(FindBacktrackingMaxBPM(graph).size(), |
| 6397 |
internal::FindMaxBipartiteMatching(graph).size()) |
| 6398 |
<< " graph: " << graph.DebugString() |
| 6399 |
<< "\nTo reproduce the failure, rerun the test with the flag" |
| 6400 |
" --" << GTEST_FLAG_PREFIX_ << "random_seed=" << seed; |
| 6401 |
} |
| 6402 |
} |
| 6403 |
|
| 6404 |
// Test argument is a std::pair<int, int> representing (nodes, iters). |
| 6405 |
INSTANTIATE_TEST_SUITE_P(Samples, BipartiteRandomTest, |
| 6406 |
testing::Values( |
| 6407 |
std::make_pair(5, 10000), |
| 6408 |
std::make_pair(6, 5000), |
| 6409 |
std::make_pair(7, 2000), |
| 6410 |
std::make_pair(8, 500), |
| 6411 |
std::make_pair(9, 100))); |
| 6412 |
|
| 6413 |
// Tests IsReadableTypeName(). |
| 6414 |
|
| 6415 |
TEST(IsReadableTypeNameTest, ReturnsTrueForShortNames) { |
| 6416 |
EXPECT_TRUE(IsReadableTypeName("int")); |
| 6417 |
EXPECT_TRUE(IsReadableTypeName("const unsigned char*")); |
| 6418 |
EXPECT_TRUE(IsReadableTypeName("MyMap<int, void*>")); |
| 6419 |
EXPECT_TRUE(IsReadableTypeName("void (*)(int, bool)")); |
| 6420 |
} |
| 6421 |
|
| 6422 |
TEST(IsReadableTypeNameTest, ReturnsTrueForLongNonTemplateNonFunctionNames) { |
| 6423 |
EXPECT_TRUE(IsReadableTypeName("my_long_namespace::MyClassName")); |
| 6424 |
EXPECT_TRUE(IsReadableTypeName("int [5][6][7][8][9][10][11]")); |
| 6425 |
EXPECT_TRUE(IsReadableTypeName("my_namespace::MyOuterClass::MyInnerClass")); |
| 6426 |
} |
| 6427 |
|
| 6428 |
TEST(IsReadableTypeNameTest, ReturnsFalseForLongTemplateNames) { |
| 6429 |
EXPECT_FALSE( |
| 6430 |
IsReadableTypeName("basic_string<char, std::char_traits<char> >")); |
| 6431 |
EXPECT_FALSE(IsReadableTypeName("std::vector<int, std::alloc_traits<int> >")); |
| 6432 |
} |
| 6433 |
|
| 6434 |
TEST(IsReadableTypeNameTest, ReturnsFalseForLongFunctionTypeNames) { |
| 6435 |
EXPECT_FALSE(IsReadableTypeName("void (&)(int, bool, char, float)")); |
| 6436 |
} |
| 6437 |
|
| 6438 |
// Tests FormatMatcherDescription(). |
| 6439 |
|
| 6440 |
TEST(FormatMatcherDescriptionTest, WorksForEmptyDescription) { |
| 6441 |
EXPECT_EQ("is even", |
| 6442 |
FormatMatcherDescription(false, "IsEven", {}, Strings())); |
| 6443 |
EXPECT_EQ("not (is even)", |
| 6444 |
FormatMatcherDescription(true, "IsEven", {}, Strings())); |
| 6445 |
|
| 6446 |
EXPECT_EQ("equals (a: 5)", |
| 6447 |
FormatMatcherDescription(false, "Equals", {"a"}, {"5"})); |
| 6448 |
|
| 6449 |
EXPECT_EQ( |
| 6450 |
"is in range (a: 5, b: 8)", |
| 6451 |
FormatMatcherDescription(false, "IsInRange", {"a", "b"}, {"5", "8"})); |
| 6452 |
} |
| 6453 |
|
| 6454 |
// Tests PolymorphicMatcher::mutable_impl(). |
| 6455 |
TEST(PolymorphicMatcherTest, CanAccessMutableImpl) { |
| 6456 |
PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42)); |
| 6457 |
DivisibleByImpl& impl = m.mutable_impl(); |
| 6458 |
EXPECT_EQ(42, impl.divider()); |
| 6459 |
|
| 6460 |
impl.set_divider(0); |
| 6461 |
EXPECT_EQ(0, m.mutable_impl().divider()); |
| 6462 |
} |
| 6463 |
|
| 6464 |
// Tests PolymorphicMatcher::impl(). |
| 6465 |
TEST(PolymorphicMatcherTest, CanAccessImpl) { |
| 6466 |
const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42)); |
| 6467 |
const DivisibleByImpl& impl = m.impl(); |
| 6468 |
EXPECT_EQ(42, impl.divider()); |
| 6469 |
} |
| 6470 |
|
| 6471 |
TEST(MatcherTupleTest, ExplainsMatchFailure) { |
| 6472 |
stringstream ss1; |
| 6473 |
ExplainMatchFailureTupleTo( |
| 6474 |
std::make_tuple(Matcher<char>(Eq('a')), GreaterThan(5)), |
| 6475 |
std::make_tuple('a', 10), &ss1); |
| 6476 |
EXPECT_EQ("", ss1.str()); // Successful match. |
| 6477 |
|
| 6478 |
stringstream ss2; |
| 6479 |
ExplainMatchFailureTupleTo( |
| 6480 |
std::make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))), |
| 6481 |
std::make_tuple(2, 'b'), &ss2); |
| 6482 |
EXPECT_EQ(" Expected arg #0: is > 5\n" |
| 6483 |
" Actual: 2, which is 3 less than 5\n" |
| 6484 |
" Expected arg #1: is equal to 'a' (97, 0x61)\n" |
| 6485 |
" Actual: 'b' (98, 0x62)\n", |
| 6486 |
ss2.str()); // Failed match where both arguments need explanation. |
| 6487 |
|
| 6488 |
stringstream ss3; |
| 6489 |
ExplainMatchFailureTupleTo( |
| 6490 |
std::make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))), |
| 6491 |
std::make_tuple(2, 'a'), &ss3); |
| 6492 |
EXPECT_EQ(" Expected arg #0: is > 5\n" |
| 6493 |
" Actual: 2, which is 3 less than 5\n", |
| 6494 |
ss3.str()); // Failed match where only one argument needs |
| 6495 |
// explanation. |
| 6496 |
} |
| 6497 |
|
| 6498 |
// Tests Each(). |
| 6499 |
|
| 6500 |
TEST(EachTest, ExplainsMatchResultCorrectly) { |
| 6501 |
set<int> a; // empty |
| 6502 |
|
| 6503 |
Matcher<set<int> > m = Each(2); |
| 6504 |
EXPECT_EQ("", Explain(m, a)); |
| 6505 |
|
| 6506 |
Matcher<const int(&)[1]> n = Each(1); // NOLINT |
| 6507 |
|
| 6508 |
const int b[1] = {1}; |
| 6509 |
EXPECT_EQ("", Explain(n, b)); |
| 6510 |
|
| 6511 |
n = Each(3); |
| 6512 |
EXPECT_EQ("whose element #0 doesn't match", Explain(n, b)); |
| 6513 |
|
| 6514 |
a.insert(1); |
| 6515 |
a.insert(2); |
| 6516 |
a.insert(3); |
| 6517 |
m = Each(GreaterThan(0)); |
| 6518 |
EXPECT_EQ("", Explain(m, a)); |
| 6519 |
|
| 6520 |
m = Each(GreaterThan(10)); |
| 6521 |
EXPECT_EQ("whose element #0 doesn't match, which is 9 less than 10", |
| 6522 |
Explain(m, a)); |
| 6523 |
} |
| 6524 |
|
| 6525 |
TEST(EachTest, DescribesItselfCorrectly) { |
| 6526 |
Matcher<vector<int> > m = Each(1); |
| 6527 |
EXPECT_EQ("only contains elements that is equal to 1", Describe(m)); |
| 6528 |
|
| 6529 |
Matcher<vector<int> > m2 = Not(m); |
| 6530 |
EXPECT_EQ("contains some element that isn't equal to 1", Describe(m2)); |
| 6531 |
} |
| 6532 |
|
| 6533 |
TEST(EachTest, MatchesVectorWhenAllElementsMatch) { |
| 6534 |
vector<int> some_vector; |
| 6535 |
EXPECT_THAT(some_vector, Each(1)); |
| 6536 |
some_vector.push_back(3); |
| 6537 |
EXPECT_THAT(some_vector, Not(Each(1))); |
| 6538 |
EXPECT_THAT(some_vector, Each(3)); |
| 6539 |
some_vector.push_back(1); |
| 6540 |
some_vector.push_back(2); |
| 6541 |
EXPECT_THAT(some_vector, Not(Each(3))); |
| 6542 |
EXPECT_THAT(some_vector, Each(Lt(3.5))); |
| 6543 |
|
| 6544 |
vector<std::string> another_vector; |
| 6545 |
another_vector.push_back("fee"); |
| 6546 |
EXPECT_THAT(another_vector, Each(std::string("fee"))); |
| 6547 |
another_vector.push_back("fie"); |
| 6548 |
another_vector.push_back("foe"); |
| 6549 |
another_vector.push_back("fum"); |
| 6550 |
EXPECT_THAT(another_vector, Not(Each(std::string("fee")))); |
| 6551 |
} |
| 6552 |
|
| 6553 |
TEST(EachTest, MatchesMapWhenAllElementsMatch) { |
| 6554 |
map<const char*, int> my_map; |
| 6555 |
const char* bar = "a string"; |
| 6556 |
my_map[bar] = 2; |
| 6557 |
EXPECT_THAT(my_map, Each(make_pair(bar, 2))); |
| 6558 |
|
| 6559 |
map<std::string, int> another_map; |
| 6560 |
EXPECT_THAT(another_map, Each(make_pair(std::string("fee"), 1))); |
| 6561 |
another_map["fee"] = 1; |
| 6562 |
EXPECT_THAT(another_map, Each(make_pair(std::string("fee"), 1))); |
| 6563 |
another_map["fie"] = 2; |
| 6564 |
another_map["foe"] = 3; |
| 6565 |
another_map["fum"] = 4; |
| 6566 |
EXPECT_THAT(another_map, Not(Each(make_pair(std::string("fee"), 1)))); |
| 6567 |
EXPECT_THAT(another_map, Not(Each(make_pair(std::string("fum"), 1)))); |
| 6568 |
EXPECT_THAT(another_map, Each(Pair(_, Gt(0)))); |
| 6569 |
} |
| 6570 |
|
| 6571 |
TEST(EachTest, AcceptsMatcher) { |
| 6572 |
const int a[] = {1, 2, 3}; |
| 6573 |
EXPECT_THAT(a, Each(Gt(0))); |
| 6574 |
EXPECT_THAT(a, Not(Each(Gt(1)))); |
| 6575 |
} |
| 6576 |
|
| 6577 |
TEST(EachTest, WorksForNativeArrayAsTuple) { |
| 6578 |
const int a[] = {1, 2}; |
| 6579 |
const int* const pointer = a; |
| 6580 |
EXPECT_THAT(std::make_tuple(pointer, 2), Each(Gt(0))); |
| 6581 |
EXPECT_THAT(std::make_tuple(pointer, 2), Not(Each(Gt(1)))); |
| 6582 |
} |
| 6583 |
|
| 6584 |
TEST(EachTest, WorksWithMoveOnly) { |
| 6585 |
ContainerHelper helper; |
| 6586 |
EXPECT_CALL(helper, Call(Each(Pointee(Gt(0))))); |
| 6587 |
helper.Call(MakeUniquePtrs({1, 2})); |
| 6588 |
} |
| 6589 |
|
| 6590 |
// For testing Pointwise(). |
| 6591 |
class IsHalfOfMatcher { |
| 6592 |
public: |
| 6593 |
template <typename T1, typename T2> |
| 6594 |
bool MatchAndExplain(const std::tuple<T1, T2>& a_pair, |
| 6595 |
MatchResultListener* listener) const { |
| 6596 |
if (std::get<0>(a_pair) == std::get<1>(a_pair) / 2) { |
| 6597 |
*listener << "where the second is " << std::get<1>(a_pair); |
| 6598 |
return true; |
| 6599 |
} else { |
| 6600 |
*listener << "where the second/2 is " << std::get<1>(a_pair) / 2; |
| 6601 |
return false; |
| 6602 |
} |
| 6603 |
} |
| 6604 |
|
| 6605 |
void DescribeTo(ostream* os) const { |
| 6606 |
*os << "are a pair where the first is half of the second"; |
| 6607 |
} |
| 6608 |
|
| 6609 |
void DescribeNegationTo(ostream* os) const { |
| 6610 |
*os << "are a pair where the first isn't half of the second"; |
| 6611 |
} |
| 6612 |
}; |
| 6613 |
|
| 6614 |
PolymorphicMatcher<IsHalfOfMatcher> IsHalfOf() { |
| 6615 |
return MakePolymorphicMatcher(IsHalfOfMatcher()); |
| 6616 |
} |
| 6617 |
|
| 6618 |
TEST(PointwiseTest, DescribesSelf) { |
| 6619 |
vector<int> rhs; |
| 6620 |
rhs.push_back(1); |
| 6621 |
rhs.push_back(2); |
| 6622 |
rhs.push_back(3); |
| 6623 |
const Matcher<const vector<int>&> m = Pointwise(IsHalfOf(), rhs); |
| 6624 |
EXPECT_EQ("contains 3 values, where each value and its corresponding value " |
| 6625 |
"in { 1, 2, 3 } are a pair where the first is half of the second", |
| 6626 |
Describe(m)); |
| 6627 |
EXPECT_EQ("doesn't contain exactly 3 values, or contains a value x at some " |
| 6628 |
"index i where x and the i-th value of { 1, 2, 3 } are a pair " |
| 6629 |
"where the first isn't half of the second", |
| 6630 |
DescribeNegation(m)); |
| 6631 |
} |
| 6632 |
|
| 6633 |
TEST(PointwiseTest, MakesCopyOfRhs) { |
| 6634 |
list<signed char> rhs; |
| 6635 |
rhs.push_back(2); |
| 6636 |
rhs.push_back(4); |
| 6637 |
|
| 6638 |
int lhs[] = {1, 2}; |
| 6639 |
const Matcher<const int (&)[2]> m = Pointwise(IsHalfOf(), rhs); |
| 6640 |
EXPECT_THAT(lhs, m); |
| 6641 |
|
| 6642 |
// Changing rhs now shouldn't affect m, which made a copy of rhs. |
| 6643 |
rhs.push_back(6); |
| 6644 |
EXPECT_THAT(lhs, m); |
| 6645 |
} |
| 6646 |
|
| 6647 |
TEST(PointwiseTest, WorksForLhsNativeArray) { |
| 6648 |
const int lhs[] = {1, 2, 3}; |
| 6649 |
vector<int> rhs; |
| 6650 |
rhs.push_back(2); |
| 6651 |
rhs.push_back(4); |
| 6652 |
rhs.push_back(6); |
| 6653 |
EXPECT_THAT(lhs, Pointwise(Lt(), rhs)); |
| 6654 |
EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs))); |
| 6655 |
} |
| 6656 |
|
| 6657 |
TEST(PointwiseTest, WorksForRhsNativeArray) { |
| 6658 |
const int rhs[] = {1, 2, 3}; |
| 6659 |
vector<int> lhs; |
| 6660 |
lhs.push_back(2); |
| 6661 |
lhs.push_back(4); |
| 6662 |
lhs.push_back(6); |
| 6663 |
EXPECT_THAT(lhs, Pointwise(Gt(), rhs)); |
| 6664 |
EXPECT_THAT(lhs, Not(Pointwise(Lt(), rhs))); |
| 6665 |
} |
| 6666 |
|
| 6667 |
// Test is effective only with sanitizers. |
| 6668 |
TEST(PointwiseTest, WorksForVectorOfBool) { |
| 6669 |
vector<bool> rhs(3, false); |
| 6670 |
rhs[1] = true; |
| 6671 |
vector<bool> lhs = rhs; |
| 6672 |
EXPECT_THAT(lhs, Pointwise(Eq(), rhs)); |
| 6673 |
rhs[0] = true; |
| 6674 |
EXPECT_THAT(lhs, Not(Pointwise(Eq(), rhs))); |
| 6675 |
} |
| 6676 |
|
| 6677 |
|
| 6678 |
TEST(PointwiseTest, WorksForRhsInitializerList) { |
| 6679 |
const vector<int> lhs{2, 4, 6}; |
| 6680 |
EXPECT_THAT(lhs, Pointwise(Gt(), {1, 2, 3})); |
| 6681 |
EXPECT_THAT(lhs, Not(Pointwise(Lt(), {3, 3, 7}))); |
| 6682 |
} |
| 6683 |
|
| 6684 |
|
| 6685 |
TEST(PointwiseTest, RejectsWrongSize) { |
| 6686 |
const double lhs[2] = {1, 2}; |
| 6687 |
const int rhs[1] = {0}; |
| 6688 |
EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs))); |
| 6689 |
EXPECT_EQ("which contains 2 values", |
| 6690 |
Explain(Pointwise(Gt(), rhs), lhs)); |
| 6691 |
|
| 6692 |
const int rhs2[3] = {0, 1, 2}; |
| 6693 |
EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs2))); |
| 6694 |
} |
| 6695 |
|
| 6696 |
TEST(PointwiseTest, RejectsWrongContent) { |
| 6697 |
const double lhs[3] = {1, 2, 3}; |
| 6698 |
const int rhs[3] = {2, 6, 4}; |
| 6699 |
EXPECT_THAT(lhs, Not(Pointwise(IsHalfOf(), rhs))); |
| 6700 |
EXPECT_EQ("where the value pair (2, 6) at index #1 don't match, " |
| 6701 |
"where the second/2 is 3", |
| 6702 |
Explain(Pointwise(IsHalfOf(), rhs), lhs)); |
| 6703 |
} |
| 6704 |
|
| 6705 |
TEST(PointwiseTest, AcceptsCorrectContent) { |
| 6706 |
const double lhs[3] = {1, 2, 3}; |
| 6707 |
const int rhs[3] = {2, 4, 6}; |
| 6708 |
EXPECT_THAT(lhs, Pointwise(IsHalfOf(), rhs)); |
| 6709 |
EXPECT_EQ("", Explain(Pointwise(IsHalfOf(), rhs), lhs)); |
| 6710 |
} |
| 6711 |
|
| 6712 |
TEST(PointwiseTest, AllowsMonomorphicInnerMatcher) { |
| 6713 |
const double lhs[3] = {1, 2, 3}; |
| 6714 |
const int rhs[3] = {2, 4, 6}; |
| 6715 |
const Matcher<std::tuple<const double&, const int&>> m1 = IsHalfOf(); |
| 6716 |
EXPECT_THAT(lhs, Pointwise(m1, rhs)); |
| 6717 |
EXPECT_EQ("", Explain(Pointwise(m1, rhs), lhs)); |
| 6718 |
|
| 6719 |
// This type works as a std::tuple<const double&, const int&> can be |
| 6720 |
// implicitly cast to std::tuple<double, int>. |
| 6721 |
const Matcher<std::tuple<double, int>> m2 = IsHalfOf(); |
| 6722 |
EXPECT_THAT(lhs, Pointwise(m2, rhs)); |
| 6723 |
EXPECT_EQ("", Explain(Pointwise(m2, rhs), lhs)); |
| 6724 |
} |
| 6725 |
|
| 6726 |
MATCHER(PointeeEquals, "Points to an equal value") { |
| 6727 |
return ExplainMatchResult(::testing::Pointee(::testing::get<1>(arg)), |
| 6728 |
::testing::get<0>(arg), result_listener); |
| 6729 |
} |
| 6730 |
|
| 6731 |
TEST(PointwiseTest, WorksWithMoveOnly) { |
| 6732 |
ContainerHelper helper; |
| 6733 |
EXPECT_CALL(helper, Call(Pointwise(PointeeEquals(), std::vector<int>{1, 2}))); |
| 6734 |
helper.Call(MakeUniquePtrs({1, 2})); |
| 6735 |
} |
| 6736 |
|
| 6737 |
TEST(UnorderedPointwiseTest, DescribesSelf) { |
| 6738 |
vector<int> rhs; |
| 6739 |
rhs.push_back(1); |
| 6740 |
rhs.push_back(2); |
| 6741 |
rhs.push_back(3); |
| 6742 |
const Matcher<const vector<int>&> m = UnorderedPointwise(IsHalfOf(), rhs); |
| 6743 |
EXPECT_EQ( |
| 6744 |
"has 3 elements and there exists some permutation of elements such " |
| 6745 |
"that:\n" |
| 6746 |
" - element #0 and 1 are a pair where the first is half of the second, " |
| 6747 |
"and\n" |
| 6748 |
" - element #1 and 2 are a pair where the first is half of the second, " |
| 6749 |
"and\n" |
| 6750 |
" - element #2 and 3 are a pair where the first is half of the second", |
| 6751 |
Describe(m)); |
| 6752 |
EXPECT_EQ( |
| 6753 |
"doesn't have 3 elements, or there exists no permutation of elements " |
| 6754 |
"such that:\n" |
| 6755 |
" - element #0 and 1 are a pair where the first is half of the second, " |
| 6756 |
"and\n" |
| 6757 |
" - element #1 and 2 are a pair where the first is half of the second, " |
| 6758 |
"and\n" |
| 6759 |
" - element #2 and 3 are a pair where the first is half of the second", |
| 6760 |
DescribeNegation(m)); |
| 6761 |
} |
| 6762 |
|
| 6763 |
TEST(UnorderedPointwiseTest, MakesCopyOfRhs) { |
| 6764 |
list<signed char> rhs; |
| 6765 |
rhs.push_back(2); |
| 6766 |
rhs.push_back(4); |
| 6767 |
|
| 6768 |
int lhs[] = {2, 1}; |
| 6769 |
const Matcher<const int (&)[2]> m = UnorderedPointwise(IsHalfOf(), rhs); |
| 6770 |
EXPECT_THAT(lhs, m); |
| 6771 |
|
| 6772 |
// Changing rhs now shouldn't affect m, which made a copy of rhs. |
| 6773 |
rhs.push_back(6); |
| 6774 |
EXPECT_THAT(lhs, m); |
| 6775 |
} |
| 6776 |
|
| 6777 |
TEST(UnorderedPointwiseTest, WorksForLhsNativeArray) { |
| 6778 |
const int lhs[] = {1, 2, 3}; |
| 6779 |
vector<int> rhs; |
| 6780 |
rhs.push_back(4); |
| 6781 |
rhs.push_back(6); |
| 6782 |
rhs.push_back(2); |
| 6783 |
EXPECT_THAT(lhs, UnorderedPointwise(Lt(), rhs)); |
| 6784 |
EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs))); |
| 6785 |
} |
| 6786 |
|
| 6787 |
TEST(UnorderedPointwiseTest, WorksForRhsNativeArray) { |
| 6788 |
const int rhs[] = {1, 2, 3}; |
| 6789 |
vector<int> lhs; |
| 6790 |
lhs.push_back(4); |
| 6791 |
lhs.push_back(2); |
| 6792 |
lhs.push_back(6); |
| 6793 |
EXPECT_THAT(lhs, UnorderedPointwise(Gt(), rhs)); |
| 6794 |
EXPECT_THAT(lhs, Not(UnorderedPointwise(Lt(), rhs))); |
| 6795 |
} |
| 6796 |
|
| 6797 |
|
| 6798 |
TEST(UnorderedPointwiseTest, WorksForRhsInitializerList) { |
| 6799 |
const vector<int> lhs{2, 4, 6}; |
| 6800 |
EXPECT_THAT(lhs, UnorderedPointwise(Gt(), {5, 1, 3})); |
| 6801 |
EXPECT_THAT(lhs, Not(UnorderedPointwise(Lt(), {1, 1, 7}))); |
| 6802 |
} |
| 6803 |
|
| 6804 |
|
| 6805 |
TEST(UnorderedPointwiseTest, RejectsWrongSize) { |
| 6806 |
const double lhs[2] = {1, 2}; |
| 6807 |
const int rhs[1] = {0}; |
| 6808 |
EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs))); |
| 6809 |
EXPECT_EQ("which has 2 elements", |
| 6810 |
Explain(UnorderedPointwise(Gt(), rhs), lhs)); |
| 6811 |
|
| 6812 |
const int rhs2[3] = {0, 1, 2}; |
| 6813 |
EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs2))); |
| 6814 |
} |
| 6815 |
|
| 6816 |
TEST(UnorderedPointwiseTest, RejectsWrongContent) { |
| 6817 |
const double lhs[3] = {1, 2, 3}; |
| 6818 |
const int rhs[3] = {2, 6, 6}; |
| 6819 |
EXPECT_THAT(lhs, Not(UnorderedPointwise(IsHalfOf(), rhs))); |
| 6820 |
EXPECT_EQ("where the following elements don't match any matchers:\n" |
| 6821 |
"element #1: 2", |
| 6822 |
Explain(UnorderedPointwise(IsHalfOf(), rhs), lhs)); |
| 6823 |
} |
| 6824 |
|
| 6825 |
TEST(UnorderedPointwiseTest, AcceptsCorrectContentInSameOrder) { |
| 6826 |
const double lhs[3] = {1, 2, 3}; |
| 6827 |
const int rhs[3] = {2, 4, 6}; |
| 6828 |
EXPECT_THAT(lhs, UnorderedPointwise(IsHalfOf(), rhs)); |
| 6829 |
} |
| 6830 |
|
| 6831 |
TEST(UnorderedPointwiseTest, AcceptsCorrectContentInDifferentOrder) { |
| 6832 |
const double lhs[3] = {1, 2, 3}; |
| 6833 |
const int rhs[3] = {6, 4, 2}; |
| 6834 |
EXPECT_THAT(lhs, UnorderedPointwise(IsHalfOf(), rhs)); |
| 6835 |
} |
| 6836 |
|
| 6837 |
TEST(UnorderedPointwiseTest, AllowsMonomorphicInnerMatcher) { |
| 6838 |
const double lhs[3] = {1, 2, 3}; |
| 6839 |
const int rhs[3] = {4, 6, 2}; |
| 6840 |
const Matcher<std::tuple<const double&, const int&>> m1 = IsHalfOf(); |
| 6841 |
EXPECT_THAT(lhs, UnorderedPointwise(m1, rhs)); |
| 6842 |
|
| 6843 |
// This type works as a std::tuple<const double&, const int&> can be |
| 6844 |
// implicitly cast to std::tuple<double, int>. |
| 6845 |
const Matcher<std::tuple<double, int>> m2 = IsHalfOf(); |
| 6846 |
EXPECT_THAT(lhs, UnorderedPointwise(m2, rhs)); |
| 6847 |
} |
| 6848 |
|
| 6849 |
TEST(UnorderedPointwiseTest, WorksWithMoveOnly) { |
| 6850 |
ContainerHelper helper; |
| 6851 |
EXPECT_CALL(helper, Call(UnorderedPointwise(PointeeEquals(), |
| 6852 |
std::vector<int>{1, 2}))); |
| 6853 |
helper.Call(MakeUniquePtrs({2, 1})); |
| 6854 |
} |
| 6855 |
|
| 6856 |
// Sample optional type implementation with minimal requirements for use with |
| 6857 |
// Optional matcher. |
| 6858 |
template <typename T> |
| 6859 |
class SampleOptional { |
| 6860 |
public: |
| 6861 |
using value_type = T; |
| 6862 |
explicit SampleOptional(T value) |
| 6863 |
: value_(std::move(value)), has_value_(true) {} |
| 6864 |
SampleOptional() : value_(), has_value_(false) {} |
| 6865 |
operator bool() const { return has_value_; } |
| 6866 |
const T& operator*() const { return value_; } |
| 6867 |
|
| 6868 |
private: |
| 6869 |
T value_; |
| 6870 |
bool has_value_; |
| 6871 |
}; |
| 6872 |
|
| 6873 |
TEST(OptionalTest, DescribesSelf) { |
| 6874 |
const Matcher<SampleOptional<int>> m = Optional(Eq(1)); |
| 6875 |
EXPECT_EQ("value is equal to 1", Describe(m)); |
| 6876 |
} |
| 6877 |
|
| 6878 |
TEST(OptionalTest, ExplainsSelf) { |
| 6879 |
const Matcher<SampleOptional<int>> m = Optional(Eq(1)); |
| 6880 |
EXPECT_EQ("whose value 1 matches", Explain(m, SampleOptional<int>(1))); |
| 6881 |
EXPECT_EQ("whose value 2 doesn't match", Explain(m, SampleOptional<int>(2))); |
| 6882 |
} |
| 6883 |
|
| 6884 |
TEST(OptionalTest, MatchesNonEmptyOptional) { |
| 6885 |
const Matcher<SampleOptional<int>> m1 = Optional(1); |
| 6886 |
const Matcher<SampleOptional<int>> m2 = Optional(Eq(2)); |
| 6887 |
const Matcher<SampleOptional<int>> m3 = Optional(Lt(3)); |
| 6888 |
SampleOptional<int> opt(1); |
| 6889 |
EXPECT_TRUE(m1.Matches(opt)); |
| 6890 |
EXPECT_FALSE(m2.Matches(opt)); |
| 6891 |
EXPECT_TRUE(m3.Matches(opt)); |
| 6892 |
} |
| 6893 |
|
| 6894 |
TEST(OptionalTest, DoesNotMatchNullopt) { |
| 6895 |
const Matcher<SampleOptional<int>> m = Optional(1); |
| 6896 |
SampleOptional<int> empty; |
| 6897 |
EXPECT_FALSE(m.Matches(empty)); |
| 6898 |
} |
| 6899 |
|
| 6900 |
TEST(OptionalTest, WorksWithMoveOnly) { |
| 6901 |
Matcher<SampleOptional<std::unique_ptr<int>>> m = Optional(Eq(nullptr)); |
| 6902 |
EXPECT_TRUE(m.Matches(SampleOptional<std::unique_ptr<int>>(nullptr))); |
| 6903 |
} |
| 6904 |
|
| 6905 |
class SampleVariantIntString { |
| 6906 |
public: |
| 6907 |
SampleVariantIntString(int i) : i_(i), has_int_(true) {} |
| 6908 |
SampleVariantIntString(const std::string& s) : s_(s), has_int_(false) {} |
| 6909 |
|
| 6910 |
template <typename T> |
| 6911 |
friend bool holds_alternative(const SampleVariantIntString& value) { |
| 6912 |
return value.has_int_ == std::is_same<T, int>::value; |
| 6913 |
} |
| 6914 |
|
| 6915 |
template <typename T> |
| 6916 |
friend const T& get(const SampleVariantIntString& value) { |
| 6917 |
return value.get_impl(static_cast<T*>(nullptr)); |
| 6918 |
} |
| 6919 |
|
| 6920 |
private: |
| 6921 |
const int& get_impl(int*) const { return i_; } |
| 6922 |
const std::string& get_impl(std::string*) const { return s_; } |
| 6923 |
|
| 6924 |
int i_; |
| 6925 |
std::string s_; |
| 6926 |
bool has_int_; |
| 6927 |
}; |
| 6928 |
|
| 6929 |
TEST(VariantTest, DescribesSelf) { |
| 6930 |
const Matcher<SampleVariantIntString> m = VariantWith<int>(Eq(1)); |
| 6931 |
EXPECT_THAT(Describe(m), ContainsRegex("is a variant<> with value of type " |
| 6932 |
"'.*' and the value is equal to 1")); |
| 6933 |
} |
| 6934 |
|
| 6935 |
TEST(VariantTest, ExplainsSelf) { |
| 6936 |
const Matcher<SampleVariantIntString> m = VariantWith<int>(Eq(1)); |
| 6937 |
EXPECT_THAT(Explain(m, SampleVariantIntString(1)), |
| 6938 |
ContainsRegex("whose value 1")); |
| 6939 |
EXPECT_THAT(Explain(m, SampleVariantIntString("A")), |
| 6940 |
HasSubstr("whose value is not of type '")); |
| 6941 |
EXPECT_THAT(Explain(m, SampleVariantIntString(2)), |
| 6942 |
"whose value 2 doesn't match"); |
| 6943 |
} |
| 6944 |
|
| 6945 |
TEST(VariantTest, FullMatch) { |
| 6946 |
Matcher<SampleVariantIntString> m = VariantWith<int>(Eq(1)); |
| 6947 |
EXPECT_TRUE(m.Matches(SampleVariantIntString(1))); |
| 6948 |
|
| 6949 |
m = VariantWith<std::string>(Eq("1")); |
| 6950 |
EXPECT_TRUE(m.Matches(SampleVariantIntString("1"))); |
| 6951 |
} |
| 6952 |
|
| 6953 |
TEST(VariantTest, TypeDoesNotMatch) { |
| 6954 |
Matcher<SampleVariantIntString> m = VariantWith<int>(Eq(1)); |
| 6955 |
EXPECT_FALSE(m.Matches(SampleVariantIntString("1"))); |
| 6956 |
|
| 6957 |
m = VariantWith<std::string>(Eq("1")); |
| 6958 |
EXPECT_FALSE(m.Matches(SampleVariantIntString(1))); |
| 6959 |
} |
| 6960 |
|
| 6961 |
TEST(VariantTest, InnerDoesNotMatch) { |
| 6962 |
Matcher<SampleVariantIntString> m = VariantWith<int>(Eq(1)); |
| 6963 |
EXPECT_FALSE(m.Matches(SampleVariantIntString(2))); |
| 6964 |
|
| 6965 |
m = VariantWith<std::string>(Eq("1")); |
| 6966 |
EXPECT_FALSE(m.Matches(SampleVariantIntString("2"))); |
| 6967 |
} |
| 6968 |
|
| 6969 |
class SampleAnyType { |
| 6970 |
public: |
| 6971 |
explicit SampleAnyType(int i) : index_(0), i_(i) {} |
| 6972 |
explicit SampleAnyType(const std::string& s) : index_(1), s_(s) {} |
| 6973 |
|
| 6974 |
template <typename T> |
| 6975 |
friend const T* any_cast(const SampleAnyType* any) { |
| 6976 |
return any->get_impl(static_cast<T*>(nullptr)); |
| 6977 |
} |
| 6978 |
|
| 6979 |
private: |
| 6980 |
int index_; |
| 6981 |
int i_; |
| 6982 |
std::string s_; |
| 6983 |
|
| 6984 |
const int* get_impl(int*) const { return index_ == 0 ? &i_ : nullptr; } |
| 6985 |
const std::string* get_impl(std::string*) const { |
| 6986 |
return index_ == 1 ? &s_ : nullptr; |
| 6987 |
} |
| 6988 |
}; |
| 6989 |
|
| 6990 |
TEST(AnyWithTest, FullMatch) { |
| 6991 |
Matcher<SampleAnyType> m = AnyWith<int>(Eq(1)); |
| 6992 |
EXPECT_TRUE(m.Matches(SampleAnyType(1))); |
| 6993 |
} |
| 6994 |
|
| 6995 |
TEST(AnyWithTest, TestBadCastType) { |
| 6996 |
Matcher<SampleAnyType> m = AnyWith<std::string>(Eq("fail")); |
| 6997 |
EXPECT_FALSE(m.Matches(SampleAnyType(1))); |
| 6998 |
} |
| 6999 |
|
| 7000 |
TEST(AnyWithTest, TestUseInContainers) { |
| 7001 |
std::vector<SampleAnyType> a; |
| 7002 |
a.emplace_back(1); |
| 7003 |
a.emplace_back(2); |
| 7004 |
a.emplace_back(3); |
| 7005 |
EXPECT_THAT( |
| 7006 |
a, ElementsAreArray({AnyWith<int>(1), AnyWith<int>(2), AnyWith<int>(3)})); |
| 7007 |
|
| 7008 |
std::vector<SampleAnyType> b; |
| 7009 |
b.emplace_back("hello"); |
| 7010 |
b.emplace_back("merhaba"); |
| 7011 |
b.emplace_back("salut"); |
| 7012 |
EXPECT_THAT(b, ElementsAreArray({AnyWith<std::string>("hello"), |
| 7013 |
AnyWith<std::string>("merhaba"), |
| 7014 |
AnyWith<std::string>("salut")})); |
| 7015 |
} |
| 7016 |
TEST(AnyWithTest, TestCompare) { |
| 7017 |
EXPECT_THAT(SampleAnyType(1), AnyWith<int>(Gt(0))); |
| 7018 |
} |
| 7019 |
|
| 7020 |
TEST(AnyWithTest, DescribesSelf) { |
| 7021 |
const Matcher<const SampleAnyType&> m = AnyWith<int>(Eq(1)); |
| 7022 |
EXPECT_THAT(Describe(m), ContainsRegex("is an 'any' type with value of type " |
| 7023 |
"'.*' and the value is equal to 1")); |
| 7024 |
} |
| 7025 |
|
| 7026 |
TEST(AnyWithTest, ExplainsSelf) { |
| 7027 |
const Matcher<const SampleAnyType&> m = AnyWith<int>(Eq(1)); |
| 7028 |
|
| 7029 |
EXPECT_THAT(Explain(m, SampleAnyType(1)), ContainsRegex("whose value 1")); |
| 7030 |
EXPECT_THAT(Explain(m, SampleAnyType("A")), |
| 7031 |
HasSubstr("whose value is not of type '")); |
| 7032 |
EXPECT_THAT(Explain(m, SampleAnyType(2)), "whose value 2 doesn't match"); |
| 7033 |
} |
| 7034 |
|
| 7035 |
TEST(PointeeTest, WorksOnMoveOnlyType) { |
| 7036 |
std::unique_ptr<int> p(new int(3)); |
| 7037 |
EXPECT_THAT(p, Pointee(Eq(3))); |
| 7038 |
EXPECT_THAT(p, Not(Pointee(Eq(2)))); |
| 7039 |
} |
| 7040 |
|
| 7041 |
TEST(NotTest, WorksOnMoveOnlyType) { |
| 7042 |
std::unique_ptr<int> p(new int(3)); |
| 7043 |
EXPECT_THAT(p, Pointee(Eq(3))); |
| 7044 |
EXPECT_THAT(p, Not(Pointee(Eq(2)))); |
| 7045 |
} |
| 7046 |
|
| 7047 |
// Tests Args<k0, ..., kn>(m). |
| 7048 |
|
| 7049 |
TEST(ArgsTest, AcceptsZeroTemplateArg) { |
| 7050 |
const std::tuple<int, bool> t(5, true); |
| 7051 |
EXPECT_THAT(t, Args<>(Eq(std::tuple<>()))); |
| 7052 |
EXPECT_THAT(t, Not(Args<>(Ne(std::tuple<>())))); |
| 7053 |
} |
| 7054 |
|
| 7055 |
TEST(ArgsTest, AcceptsOneTemplateArg) { |
| 7056 |
const std::tuple<int, bool> t(5, true); |
| 7057 |
EXPECT_THAT(t, Args<0>(Eq(std::make_tuple(5)))); |
| 7058 |
EXPECT_THAT(t, Args<1>(Eq(std::make_tuple(true)))); |
| 7059 |
EXPECT_THAT(t, Not(Args<1>(Eq(std::make_tuple(false))))); |
| 7060 |
} |
| 7061 |
|
| 7062 |
TEST(ArgsTest, AcceptsTwoTemplateArgs) { |
| 7063 |
const std::tuple<short, int, long> t(4, 5, 6L); // NOLINT |
| 7064 |
|
| 7065 |
EXPECT_THAT(t, (Args<0, 1>(Lt()))); |
| 7066 |
EXPECT_THAT(t, (Args<1, 2>(Lt()))); |
| 7067 |
EXPECT_THAT(t, Not(Args<0, 2>(Gt()))); |
| 7068 |
} |
| 7069 |
|
| 7070 |
TEST(ArgsTest, AcceptsRepeatedTemplateArgs) { |
| 7071 |
const std::tuple<short, int, long> t(4, 5, 6L); // NOLINT |
| 7072 |
EXPECT_THAT(t, (Args<0, 0>(Eq()))); |
| 7073 |
EXPECT_THAT(t, Not(Args<1, 1>(Ne()))); |
| 7074 |
} |
| 7075 |
|
| 7076 |
TEST(ArgsTest, AcceptsDecreasingTemplateArgs) { |
| 7077 |
const std::tuple<short, int, long> t(4, 5, 6L); // NOLINT |
| 7078 |
EXPECT_THAT(t, (Args<2, 0>(Gt()))); |
| 7079 |
EXPECT_THAT(t, Not(Args<2, 1>(Lt()))); |
| 7080 |
} |
| 7081 |
|
| 7082 |
MATCHER(SumIsZero, "") { |
| 7083 |
return std::get<0>(arg) + std::get<1>(arg) + std::get<2>(arg) == 0; |
| 7084 |
} |
| 7085 |
|
| 7086 |
TEST(ArgsTest, AcceptsMoreTemplateArgsThanArityOfOriginalTuple) { |
| 7087 |
EXPECT_THAT(std::make_tuple(-1, 2), (Args<0, 0, 1>(SumIsZero()))); |
| 7088 |
EXPECT_THAT(std::make_tuple(1, 2), Not(Args<0, 0, 1>(SumIsZero()))); |
| 7089 |
} |
| 7090 |
|
| 7091 |
TEST(ArgsTest, CanBeNested) { |
| 7092 |
const std::tuple<short, int, long, int> t(4, 5, 6L, 6); // NOLINT |
| 7093 |
EXPECT_THAT(t, (Args<1, 2, 3>(Args<1, 2>(Eq())))); |
| 7094 |
EXPECT_THAT(t, (Args<0, 1, 3>(Args<0, 2>(Lt())))); |
| 7095 |
} |
| 7096 |
|
| 7097 |
TEST(ArgsTest, CanMatchTupleByValue) { |
| 7098 |
typedef std::tuple<char, int, int> Tuple3; |
| 7099 |
const Matcher<Tuple3> m = Args<1, 2>(Lt()); |
| 7100 |
EXPECT_TRUE(m.Matches(Tuple3('a', 1, 2))); |
| 7101 |
EXPECT_FALSE(m.Matches(Tuple3('b', 2, 2))); |
| 7102 |
} |
| 7103 |
|
| 7104 |
TEST(ArgsTest, CanMatchTupleByReference) { |
| 7105 |
typedef std::tuple<char, char, int> Tuple3; |
| 7106 |
const Matcher<const Tuple3&> m = Args<0, 1>(Lt()); |
| 7107 |
EXPECT_TRUE(m.Matches(Tuple3('a', 'b', 2))); |
| 7108 |
EXPECT_FALSE(m.Matches(Tuple3('b', 'b', 2))); |
| 7109 |
} |
| 7110 |
|
| 7111 |
// Validates that arg is printed as str. |
| 7112 |
MATCHER_P(PrintsAs, str, "") { |
| 7113 |
return testing::PrintToString(arg) == str; |
| 7114 |
} |
| 7115 |
|
| 7116 |
TEST(ArgsTest, AcceptsTenTemplateArgs) { |
| 7117 |
EXPECT_THAT(std::make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9), |
| 7118 |
(Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>( |
| 7119 |
PrintsAs("(9, 8, 7, 6, 5, 4, 3, 2, 1, 0)")))); |
| 7120 |
EXPECT_THAT(std::make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9), |
| 7121 |
Not(Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>( |
| 7122 |
PrintsAs("(0, 8, 7, 6, 5, 4, 3, 2, 1, 0)")))); |
| 7123 |
} |
| 7124 |
|
| 7125 |
TEST(ArgsTest, DescirbesSelfCorrectly) { |
| 7126 |
const Matcher<std::tuple<int, bool, char> > m = Args<2, 0>(Lt()); |
| 7127 |
EXPECT_EQ("are a tuple whose fields (#2, #0) are a pair where " |
| 7128 |
"the first < the second", |
| 7129 |
Describe(m)); |
| 7130 |
} |
| 7131 |
|
| 7132 |
TEST(ArgsTest, DescirbesNestedArgsCorrectly) { |
| 7133 |
const Matcher<const std::tuple<int, bool, char, int>&> m = |
| 7134 |
Args<0, 2, 3>(Args<2, 0>(Lt())); |
| 7135 |
EXPECT_EQ("are a tuple whose fields (#0, #2, #3) are a tuple " |
| 7136 |
"whose fields (#2, #0) are a pair where the first < the second", |
| 7137 |
Describe(m)); |
| 7138 |
} |
| 7139 |
|
| 7140 |
TEST(ArgsTest, DescribesNegationCorrectly) { |
| 7141 |
const Matcher<std::tuple<int, char> > m = Args<1, 0>(Gt()); |
| 7142 |
EXPECT_EQ("are a tuple whose fields (#1, #0) aren't a pair " |
| 7143 |
"where the first > the second", |
| 7144 |
DescribeNegation(m)); |
| 7145 |
} |
| 7146 |
|
| 7147 |
TEST(ArgsTest, ExplainsMatchResultWithoutInnerExplanation) { |
| 7148 |
const Matcher<std::tuple<bool, int, int> > m = Args<1, 2>(Eq()); |
| 7149 |
EXPECT_EQ("whose fields (#1, #2) are (42, 42)", |
| 7150 |
Explain(m, std::make_tuple(false, 42, 42))); |
| 7151 |
EXPECT_EQ("whose fields (#1, #2) are (42, 43)", |
| 7152 |
Explain(m, std::make_tuple(false, 42, 43))); |
| 7153 |
} |
| 7154 |
|
| 7155 |
// For testing Args<>'s explanation. |
| 7156 |
class LessThanMatcher : public MatcherInterface<std::tuple<char, int> > { |
| 7157 |
public: |
| 7158 |
void DescribeTo(::std::ostream* /*os*/) const override {} |
| 7159 |
|
| 7160 |
bool MatchAndExplain(std::tuple<char, int> value, |
| 7161 |
MatchResultListener* listener) const override { |
| 7162 |
const int diff = std::get<0>(value) - std::get<1>(value); |
| 7163 |
if (diff > 0) { |
| 7164 |
*listener << "where the first value is " << diff |
| 7165 |
<< " more than the second"; |
| 7166 |
} |
| 7167 |
return diff < 0; |
| 7168 |
} |
| 7169 |
}; |
| 7170 |
|
| 7171 |
Matcher<std::tuple<char, int> > LessThan() { |
| 7172 |
return MakeMatcher(new LessThanMatcher); |
| 7173 |
} |
| 7174 |
|
| 7175 |
TEST(ArgsTest, ExplainsMatchResultWithInnerExplanation) { |
| 7176 |
const Matcher<std::tuple<char, int, int> > m = Args<0, 2>(LessThan()); |
| 7177 |
EXPECT_EQ( |
| 7178 |
"whose fields (#0, #2) are ('a' (97, 0x61), 42), " |
| 7179 |
"where the first value is 55 more than the second", |
| 7180 |
Explain(m, std::make_tuple('a', 42, 42))); |
| 7181 |
EXPECT_EQ("whose fields (#0, #2) are ('\\0', 43)", |
| 7182 |
Explain(m, std::make_tuple('\0', 42, 43))); |
| 7183 |
} |
| 7184 |
|
| 7185 |
class PredicateFormatterFromMatcherTest : public ::testing::Test { |
| 7186 |
protected: |
| 7187 |
enum Behavior { kInitialSuccess, kAlwaysFail, kFlaky }; |
| 7188 |
|
| 7189 |
// A matcher that can return different results when used multiple times on the |
| 7190 |
// same input. No real matcher should do this; but this lets us test that we |
| 7191 |
// detect such behavior and fail appropriately. |
| 7192 |
class MockMatcher : public MatcherInterface<Behavior> { |
| 7193 |
public: |
| 7194 |
bool MatchAndExplain(Behavior behavior, |
| 7195 |
MatchResultListener* listener) const override { |
| 7196 |
*listener << "[MatchAndExplain]"; |
| 7197 |
switch (behavior) { |
| 7198 |
case kInitialSuccess: |
| 7199 |
// The first call to MatchAndExplain should use a "not interested" |
| 7200 |
// listener; so this is expected to return |true|. There should be no |
| 7201 |
// subsequent calls. |
| 7202 |
return !listener->IsInterested(); |
| 7203 |
|
| 7204 |
case kAlwaysFail: |
| 7205 |
return false; |
| 7206 |
|
| 7207 |
case kFlaky: |
| 7208 |
// The first call to MatchAndExplain should use a "not interested" |
| 7209 |
// listener; so this will return |false|. Subsequent calls should have |
| 7210 |
// an "interested" listener; so this will return |true|, thus |
| 7211 |
// simulating a flaky matcher. |
| 7212 |
return listener->IsInterested(); |
| 7213 |
} |
| 7214 |
|
| 7215 |
GTEST_LOG_(FATAL) << "This should never be reached"; |
| 7216 |
return false; |
| 7217 |
} |
| 7218 |
|
| 7219 |
void DescribeTo(ostream* os) const override { *os << "[DescribeTo]"; } |
| 7220 |
|
| 7221 |
void DescribeNegationTo(ostream* os) const override { |
| 7222 |
*os << "[DescribeNegationTo]"; |
| 7223 |
} |
| 7224 |
}; |
| 7225 |
|
| 7226 |
AssertionResult RunPredicateFormatter(Behavior behavior) { |
| 7227 |
auto matcher = MakeMatcher(new MockMatcher); |
| 7228 |
PredicateFormatterFromMatcher<Matcher<Behavior>> predicate_formatter( |
| 7229 |
matcher); |
| 7230 |
return predicate_formatter("dummy-name", behavior); |
| 7231 |
} |
| 7232 |
}; |
| 7233 |
|
| 7234 |
TEST_F(PredicateFormatterFromMatcherTest, ShortCircuitOnSuccess) { |
| 7235 |
AssertionResult result = RunPredicateFormatter(kInitialSuccess); |
| 7236 |
EXPECT_TRUE(result); // Implicit cast to bool. |
| 7237 |
std::string expect; |
| 7238 |
EXPECT_EQ(expect, result.message()); |
| 7239 |
} |
| 7240 |
|
| 7241 |
TEST_F(PredicateFormatterFromMatcherTest, NoShortCircuitOnFailure) { |
| 7242 |
AssertionResult result = RunPredicateFormatter(kAlwaysFail); |
| 7243 |
EXPECT_FALSE(result); // Implicit cast to bool. |
| 7244 |
std::string expect = |
| 7245 |
"Value of: dummy-name\nExpected: [DescribeTo]\n" |
| 7246 |
" Actual: 1" + |
| 7247 |
OfType(internal::GetTypeName<Behavior>()) + ", [MatchAndExplain]"; |
| 7248 |
EXPECT_EQ(expect, result.message()); |
| 7249 |
} |
| 7250 |
|
| 7251 |
TEST_F(PredicateFormatterFromMatcherTest, DetectsFlakyShortCircuit) { |
| 7252 |
AssertionResult result = RunPredicateFormatter(kFlaky); |
| 7253 |
EXPECT_FALSE(result); // Implicit cast to bool. |
| 7254 |
std::string expect = |
| 7255 |
"Value of: dummy-name\nExpected: [DescribeTo]\n" |
| 7256 |
" The matcher failed on the initial attempt; but passed when rerun to " |
| 7257 |
"generate the explanation.\n" |
| 7258 |
" Actual: 2" + |
| 7259 |
OfType(internal::GetTypeName<Behavior>()) + ", [MatchAndExplain]"; |
| 7260 |
EXPECT_EQ(expect, result.message()); |
| 7261 |
} |
| 7262 |
|
| 7263 |
// Tests for ElementsAre(). |
| 7264 |
|
| 7265 |
TEST(ElementsAreTest, CanDescribeExpectingNoElement) { |
| 7266 |
Matcher<const vector<int>&> m = ElementsAre(); |
| 7267 |
EXPECT_EQ("is empty", Describe(m)); |
| 7268 |
} |
| 7269 |
|
| 7270 |
TEST(ElementsAreTest, CanDescribeExpectingOneElement) { |
| 7271 |
Matcher<vector<int>> m = ElementsAre(Gt(5)); |
| 7272 |
EXPECT_EQ("has 1 element that is > 5", Describe(m)); |
| 7273 |
} |
| 7274 |
|
| 7275 |
TEST(ElementsAreTest, CanDescribeExpectingManyElements) { |
| 7276 |
Matcher<list<std::string>> m = ElementsAre(StrEq("one"), "two"); |
| 7277 |
EXPECT_EQ( |
| 7278 |
"has 2 elements where\n" |
| 7279 |
"element #0 is equal to \"one\",\n" |
| 7280 |
"element #1 is equal to \"two\"", |
| 7281 |
Describe(m)); |
| 7282 |
} |
| 7283 |
|
| 7284 |
TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) { |
| 7285 |
Matcher<vector<int>> m = ElementsAre(); |
| 7286 |
EXPECT_EQ("isn't empty", DescribeNegation(m)); |
| 7287 |
} |
| 7288 |
|
| 7289 |
TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElement) { |
| 7290 |
Matcher<const list<int>&> m = ElementsAre(Gt(5)); |
| 7291 |
EXPECT_EQ( |
| 7292 |
"doesn't have 1 element, or\n" |
| 7293 |
"element #0 isn't > 5", |
| 7294 |
DescribeNegation(m)); |
| 7295 |
} |
| 7296 |
|
| 7297 |
TEST(ElementsAreTest, CanDescribeNegationOfExpectingManyElements) { |
| 7298 |
Matcher<const list<std::string>&> m = ElementsAre("one", "two"); |
| 7299 |
EXPECT_EQ( |
| 7300 |
"doesn't have 2 elements, or\n" |
| 7301 |
"element #0 isn't equal to \"one\", or\n" |
| 7302 |
"element #1 isn't equal to \"two\"", |
| 7303 |
DescribeNegation(m)); |
| 7304 |
} |
| 7305 |
|
| 7306 |
TEST(ElementsAreTest, DoesNotExplainTrivialMatch) { |
| 7307 |
Matcher<const list<int>&> m = ElementsAre(1, Ne(2)); |
| 7308 |
|
| 7309 |
list<int> test_list; |
| 7310 |
test_list.push_back(1); |
| 7311 |
test_list.push_back(3); |
| 7312 |
EXPECT_EQ("", Explain(m, test_list)); // No need to explain anything. |
| 7313 |
} |
| 7314 |
|
| 7315 |
TEST(ElementsAreTest, ExplainsNonTrivialMatch) { |
| 7316 |
Matcher<const vector<int>&> m = |
| 7317 |
ElementsAre(GreaterThan(1), 0, GreaterThan(2)); |
| 7318 |
|
| 7319 |
const int a[] = {10, 0, 100}; |
| 7320 |
vector<int> test_vector(std::begin(a), std::end(a)); |
| 7321 |
EXPECT_EQ( |
| 7322 |
"whose element #0 matches, which is 9 more than 1,\n" |
| 7323 |
"and whose element #2 matches, which is 98 more than 2", |
| 7324 |
Explain(m, test_vector)); |
| 7325 |
} |
| 7326 |
|
| 7327 |
TEST(ElementsAreTest, CanExplainMismatchWrongSize) { |
| 7328 |
Matcher<const list<int>&> m = ElementsAre(1, 3); |
| 7329 |
|
| 7330 |
list<int> test_list; |
| 7331 |
// No need to explain when the container is empty. |
| 7332 |
EXPECT_EQ("", Explain(m, test_list)); |
| 7333 |
|
| 7334 |
test_list.push_back(1); |
| 7335 |
EXPECT_EQ("which has 1 element", Explain(m, test_list)); |
| 7336 |
} |
| 7337 |
|
| 7338 |
TEST(ElementsAreTest, CanExplainMismatchRightSize) { |
| 7339 |
Matcher<const vector<int>&> m = ElementsAre(1, GreaterThan(5)); |
| 7340 |
|
| 7341 |
vector<int> v; |
| 7342 |
v.push_back(2); |
| 7343 |
v.push_back(1); |
| 7344 |
EXPECT_EQ("whose element #0 doesn't match", Explain(m, v)); |
| 7345 |
|
| 7346 |
v[0] = 1; |
| 7347 |
EXPECT_EQ("whose element #1 doesn't match, which is 4 less than 5", |
| 7348 |
Explain(m, v)); |
| 7349 |
} |
| 7350 |
|
| 7351 |
TEST(ElementsAreTest, MatchesOneElementVector) { |
| 7352 |
vector<std::string> test_vector; |
| 7353 |
test_vector.push_back("test string"); |
| 7354 |
|
| 7355 |
EXPECT_THAT(test_vector, ElementsAre(StrEq("test string"))); |
| 7356 |
} |
| 7357 |
|
| 7358 |
TEST(ElementsAreTest, MatchesOneElementList) { |
| 7359 |
list<std::string> test_list; |
| 7360 |
test_list.push_back("test string"); |
| 7361 |
|
| 7362 |
EXPECT_THAT(test_list, ElementsAre("test string")); |
| 7363 |
} |
| 7364 |
|
| 7365 |
TEST(ElementsAreTest, MatchesThreeElementVector) { |
| 7366 |
vector<std::string> test_vector; |
| 7367 |
test_vector.push_back("one"); |
| 7368 |
test_vector.push_back("two"); |
| 7369 |
test_vector.push_back("three"); |
| 7370 |
|
| 7371 |
EXPECT_THAT(test_vector, ElementsAre("one", StrEq("two"), _)); |
| 7372 |
} |
| 7373 |
|
| 7374 |
TEST(ElementsAreTest, MatchesOneElementEqMatcher) { |
| 7375 |
vector<int> test_vector; |
| 7376 |
test_vector.push_back(4); |
| 7377 |
|
| 7378 |
EXPECT_THAT(test_vector, ElementsAre(Eq(4))); |
| 7379 |
} |
| 7380 |
|
| 7381 |
TEST(ElementsAreTest, MatchesOneElementAnyMatcher) { |
| 7382 |
vector<int> test_vector; |
| 7383 |
test_vector.push_back(4); |
| 7384 |
|
| 7385 |
EXPECT_THAT(test_vector, ElementsAre(_)); |
| 7386 |
} |
| 7387 |
|
| 7388 |
TEST(ElementsAreTest, MatchesOneElementValue) { |
| 7389 |
vector<int> test_vector; |
| 7390 |
test_vector.push_back(4); |
| 7391 |
|
| 7392 |
EXPECT_THAT(test_vector, ElementsAre(4)); |
| 7393 |
} |
| 7394 |
|
| 7395 |
TEST(ElementsAreTest, MatchesThreeElementsMixedMatchers) { |
| 7396 |
vector<int> test_vector; |
| 7397 |
test_vector.push_back(1); |
| 7398 |
test_vector.push_back(2); |
| 7399 |
test_vector.push_back(3); |
| 7400 |
|
| 7401 |
EXPECT_THAT(test_vector, ElementsAre(1, Eq(2), _)); |
| 7402 |
} |
| 7403 |
|
| 7404 |
TEST(ElementsAreTest, MatchesTenElementVector) { |
| 7405 |
const int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 7406 |
vector<int> test_vector(std::begin(a), std::end(a)); |
| 7407 |
|
| 7408 |
EXPECT_THAT(test_vector, |
| 7409 |
// The element list can contain values and/or matchers |
| 7410 |
// of different types. |
| 7411 |
ElementsAre(0, Ge(0), _, 3, 4, Ne(2), Eq(6), 7, 8, _)); |
| 7412 |
} |
| 7413 |
|
| 7414 |
TEST(ElementsAreTest, DoesNotMatchWrongSize) { |
| 7415 |
vector<std::string> test_vector; |
| 7416 |
test_vector.push_back("test string"); |
| 7417 |
test_vector.push_back("test string"); |
| 7418 |
|
| 7419 |
Matcher<vector<std::string>> m = ElementsAre(StrEq("test string")); |
| 7420 |
EXPECT_FALSE(m.Matches(test_vector)); |
| 7421 |
} |
| 7422 |
|
| 7423 |
TEST(ElementsAreTest, DoesNotMatchWrongValue) { |
| 7424 |
vector<std::string> test_vector; |
| 7425 |
test_vector.push_back("other string"); |
| 7426 |
|
| 7427 |
Matcher<vector<std::string>> m = ElementsAre(StrEq("test string")); |
| 7428 |
EXPECT_FALSE(m.Matches(test_vector)); |
| 7429 |
} |
| 7430 |
|
| 7431 |
TEST(ElementsAreTest, DoesNotMatchWrongOrder) { |
| 7432 |
vector<std::string> test_vector; |
| 7433 |
test_vector.push_back("one"); |
| 7434 |
test_vector.push_back("three"); |
| 7435 |
test_vector.push_back("two"); |
| 7436 |
|
| 7437 |
Matcher<vector<std::string>> m = |
| 7438 |
ElementsAre(StrEq("one"), StrEq("two"), StrEq("three")); |
| 7439 |
EXPECT_FALSE(m.Matches(test_vector)); |
| 7440 |
} |
| 7441 |
|
| 7442 |
TEST(ElementsAreTest, WorksForNestedContainer) { |
| 7443 |
constexpr std::array<const char*, 2> strings = {{"Hi", "world"}}; |
| 7444 |
|
| 7445 |
vector<list<char>> nested; |
| 7446 |
for (const auto& s : strings) { |
| 7447 |
nested.emplace_back(s, s + strlen(s)); |
| 7448 |
} |
| 7449 |
|
| 7450 |
EXPECT_THAT(nested, ElementsAre(ElementsAre('H', Ne('e')), |
| 7451 |
ElementsAre('w', 'o', _, _, 'd'))); |
| 7452 |
EXPECT_THAT(nested, Not(ElementsAre(ElementsAre('H', 'e'), |
| 7453 |
ElementsAre('w', 'o', _, _, 'd')))); |
| 7454 |
} |
| 7455 |
|
| 7456 |
TEST(ElementsAreTest, WorksWithByRefElementMatchers) { |
| 7457 |
int a[] = {0, 1, 2}; |
| 7458 |
vector<int> v(std::begin(a), std::end(a)); |
| 7459 |
|
| 7460 |
EXPECT_THAT(v, ElementsAre(Ref(v[0]), Ref(v[1]), Ref(v[2]))); |
| 7461 |
EXPECT_THAT(v, Not(ElementsAre(Ref(v[0]), Ref(v[1]), Ref(a[2])))); |
| 7462 |
} |
| 7463 |
|
| 7464 |
TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) { |
| 7465 |
int a[] = {0, 1, 2}; |
| 7466 |
vector<int> v(std::begin(a), std::end(a)); |
| 7467 |
|
| 7468 |
EXPECT_THAT(&v, Pointee(ElementsAre(0, 1, _))); |
| 7469 |
EXPECT_THAT(&v, Not(Pointee(ElementsAre(0, _, 3)))); |
| 7470 |
} |
| 7471 |
|
| 7472 |
TEST(ElementsAreTest, WorksWithNativeArrayPassedByReference) { |
| 7473 |
int array[] = {0, 1, 2}; |
| 7474 |
EXPECT_THAT(array, ElementsAre(0, 1, _)); |
| 7475 |
EXPECT_THAT(array, Not(ElementsAre(1, _, _))); |
| 7476 |
EXPECT_THAT(array, Not(ElementsAre(0, _))); |
| 7477 |
} |
| 7478 |
|
| 7479 |
class NativeArrayPassedAsPointerAndSize { |
| 7480 |
public: |
| 7481 |
NativeArrayPassedAsPointerAndSize() {} |
| 7482 |
|
| 7483 |
MOCK_METHOD(void, Helper, (int* array, int size)); |
| 7484 |
|
| 7485 |
private: |
| 7486 |
GTEST_DISALLOW_COPY_AND_ASSIGN_(NativeArrayPassedAsPointerAndSize); |
| 7487 |
}; |
| 7488 |
|
| 7489 |
TEST(ElementsAreTest, WorksWithNativeArrayPassedAsPointerAndSize) { |
| 7490 |
int array[] = {0, 1}; |
| 7491 |
::std::tuple<int*, size_t> array_as_tuple(array, 2); |
| 7492 |
EXPECT_THAT(array_as_tuple, ElementsAre(0, 1)); |
| 7493 |
EXPECT_THAT(array_as_tuple, Not(ElementsAre(0))); |
| 7494 |
|
| 7495 |
NativeArrayPassedAsPointerAndSize helper; |
| 7496 |
EXPECT_CALL(helper, Helper(_, _)).With(ElementsAre(0, 1)); |
| 7497 |
helper.Helper(array, 2); |
| 7498 |
} |
| 7499 |
|
| 7500 |
TEST(ElementsAreTest, WorksWithTwoDimensionalNativeArray) { |
| 7501 |
const char a2[][3] = {"hi", "lo"}; |
| 7502 |
EXPECT_THAT(a2, ElementsAre(ElementsAre('h', 'i', '\0'), |
| 7503 |
ElementsAre('l', 'o', '\0'))); |
| 7504 |
EXPECT_THAT(a2, ElementsAre(StrEq("hi"), StrEq("lo"))); |
| 7505 |
EXPECT_THAT(a2, ElementsAre(Not(ElementsAre('h', 'o', '\0')), |
| 7506 |
ElementsAre('l', 'o', '\0'))); |
| 7507 |
} |
| 7508 |
|
| 7509 |
TEST(ElementsAreTest, AcceptsStringLiteral) { |
| 7510 |
std::string array[] = {"hi", "one", "two"}; |
| 7511 |
EXPECT_THAT(array, ElementsAre("hi", "one", "two")); |
| 7512 |
EXPECT_THAT(array, Not(ElementsAre("hi", "one", "too"))); |
| 7513 |
} |
| 7514 |
|
| 7515 |
// Declared here with the size unknown. Defined AFTER the following test. |
| 7516 |
extern const char kHi[]; |
| 7517 |
|
| 7518 |
TEST(ElementsAreTest, AcceptsArrayWithUnknownSize) { |
| 7519 |
// The size of kHi is not known in this test, but ElementsAre() should |
| 7520 |
// still accept it. |
| 7521 |
|
| 7522 |
std::string array1[] = {"hi"}; |
| 7523 |
EXPECT_THAT(array1, ElementsAre(kHi)); |
| 7524 |
|
| 7525 |
std::string array2[] = {"ho"}; |
| 7526 |
EXPECT_THAT(array2, Not(ElementsAre(kHi))); |
| 7527 |
} |
| 7528 |
|
| 7529 |
const char kHi[] = "hi"; |
| 7530 |
|
| 7531 |
TEST(ElementsAreTest, MakesCopyOfArguments) { |
| 7532 |
int x = 1; |
| 7533 |
int y = 2; |
| 7534 |
// This should make a copy of x and y. |
| 7535 |
::testing::internal::ElementsAreMatcher<std::tuple<int, int>> |
| 7536 |
polymorphic_matcher = ElementsAre(x, y); |
| 7537 |
// Changing x and y now shouldn't affect the meaning of the above matcher. |
| 7538 |
x = y = 0; |
| 7539 |
const int array1[] = {1, 2}; |
| 7540 |
EXPECT_THAT(array1, polymorphic_matcher); |
| 7541 |
const int array2[] = {0, 0}; |
| 7542 |
EXPECT_THAT(array2, Not(polymorphic_matcher)); |
| 7543 |
} |
| 7544 |
|
| 7545 |
// Tests for ElementsAreArray(). Since ElementsAreArray() shares most |
| 7546 |
// of the implementation with ElementsAre(), we don't test it as |
| 7547 |
// thoroughly here. |
| 7548 |
|
| 7549 |
TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) { |
| 7550 |
const int a[] = {1, 2, 3}; |
| 7551 |
|
| 7552 |
vector<int> test_vector(std::begin(a), std::end(a)); |
| 7553 |
EXPECT_THAT(test_vector, ElementsAreArray(a)); |
| 7554 |
|
| 7555 |
test_vector[2] = 0; |
| 7556 |
EXPECT_THAT(test_vector, Not(ElementsAreArray(a))); |
| 7557 |
} |
| 7558 |
|
| 7559 |
TEST(ElementsAreArrayTest, CanBeCreatedWithArraySize) { |
| 7560 |
std::array<const char*, 3> a = {{"one", "two", "three"}}; |
| 7561 |
|
| 7562 |
vector<std::string> test_vector(std::begin(a), std::end(a)); |
| 7563 |
EXPECT_THAT(test_vector, ElementsAreArray(a.data(), a.size())); |
| 7564 |
|
| 7565 |
const char** p = a.data(); |
| 7566 |
test_vector[0] = "1"; |
| 7567 |
EXPECT_THAT(test_vector, Not(ElementsAreArray(p, a.size()))); |
| 7568 |
} |
| 7569 |
|
| 7570 |
TEST(ElementsAreArrayTest, CanBeCreatedWithoutArraySize) { |
| 7571 |
const char* a[] = {"one", "two", "three"}; |
| 7572 |
|
| 7573 |
vector<std::string> test_vector(std::begin(a), std::end(a)); |
| 7574 |
EXPECT_THAT(test_vector, ElementsAreArray(a)); |
| 7575 |
|
| 7576 |
test_vector[0] = "1"; |
| 7577 |
EXPECT_THAT(test_vector, Not(ElementsAreArray(a))); |
| 7578 |
} |
| 7579 |
|
| 7580 |
TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) { |
| 7581 |
const Matcher<std::string> kMatcherArray[] = {StrEq("one"), StrEq("two"), |
| 7582 |
StrEq("three")}; |
| 7583 |
|
| 7584 |
vector<std::string> test_vector; |
| 7585 |
test_vector.push_back("one"); |
| 7586 |
test_vector.push_back("two"); |
| 7587 |
test_vector.push_back("three"); |
| 7588 |
EXPECT_THAT(test_vector, ElementsAreArray(kMatcherArray)); |
| 7589 |
|
| 7590 |
test_vector.push_back("three"); |
| 7591 |
EXPECT_THAT(test_vector, Not(ElementsAreArray(kMatcherArray))); |
| 7592 |
} |
| 7593 |
|
| 7594 |
TEST(ElementsAreArrayTest, CanBeCreatedWithVector) { |
| 7595 |
const int a[] = {1, 2, 3}; |
| 7596 |
vector<int> test_vector(std::begin(a), std::end(a)); |
| 7597 |
const vector<int> expected(std::begin(a), std::end(a)); |
| 7598 |
EXPECT_THAT(test_vector, ElementsAreArray(expected)); |
| 7599 |
test_vector.push_back(4); |
| 7600 |
EXPECT_THAT(test_vector, Not(ElementsAreArray(expected))); |
| 7601 |
} |
| 7602 |
|
| 7603 |
TEST(ElementsAreArrayTest, TakesInitializerList) { |
| 7604 |
const int a[5] = {1, 2, 3, 4, 5}; |
| 7605 |
EXPECT_THAT(a, ElementsAreArray({1, 2, 3, 4, 5})); |
| 7606 |
EXPECT_THAT(a, Not(ElementsAreArray({1, 2, 3, 5, 4}))); |
| 7607 |
EXPECT_THAT(a, Not(ElementsAreArray({1, 2, 3, 4, 6}))); |
| 7608 |
} |
| 7609 |
|
| 7610 |
TEST(ElementsAreArrayTest, TakesInitializerListOfCStrings) { |
| 7611 |
const std::string a[5] = {"a", "b", "c", "d", "e"}; |
| 7612 |
EXPECT_THAT(a, ElementsAreArray({"a", "b", "c", "d", "e"})); |
| 7613 |
EXPECT_THAT(a, Not(ElementsAreArray({"a", "b", "c", "e", "d"}))); |
| 7614 |
EXPECT_THAT(a, Not(ElementsAreArray({"a", "b", "c", "d", "ef"}))); |
| 7615 |
} |
| 7616 |
|
| 7617 |
TEST(ElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) { |
| 7618 |
const int a[5] = {1, 2, 3, 4, 5}; |
| 7619 |
EXPECT_THAT(a, ElementsAreArray({Eq(1), Eq(2), Eq(3), Eq(4), Eq(5)})); |
| 7620 |
EXPECT_THAT(a, Not(ElementsAreArray({Eq(1), Eq(2), Eq(3), Eq(4), Eq(6)}))); |
| 7621 |
} |
| 7622 |
|
| 7623 |
TEST(ElementsAreArrayTest, TakesInitializerListOfDifferentTypedMatchers) { |
| 7624 |
const int a[5] = {1, 2, 3, 4, 5}; |
| 7625 |
// The compiler cannot infer the type of the initializer list if its |
| 7626 |
// elements have different types. We must explicitly specify the |
| 7627 |
// unified element type in this case. |
| 7628 |
EXPECT_THAT( |
| 7629 |
a, ElementsAreArray<Matcher<int>>({Eq(1), Ne(-2), Ge(3), Le(4), Eq(5)})); |
| 7630 |
EXPECT_THAT(a, Not(ElementsAreArray<Matcher<int>>( |
| 7631 |
{Eq(1), Ne(-2), Ge(3), Le(4), Eq(6)}))); |
| 7632 |
} |
| 7633 |
|
| 7634 |
TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherVector) { |
| 7635 |
const int a[] = {1, 2, 3}; |
| 7636 |
const Matcher<int> kMatchers[] = {Eq(1), Eq(2), Eq(3)}; |
| 7637 |
vector<int> test_vector(std::begin(a), std::end(a)); |
| 7638 |
const vector<Matcher<int>> expected(std::begin(kMatchers), |
| 7639 |
std::end(kMatchers)); |
| 7640 |
EXPECT_THAT(test_vector, ElementsAreArray(expected)); |
| 7641 |
test_vector.push_back(4); |
| 7642 |
EXPECT_THAT(test_vector, Not(ElementsAreArray(expected))); |
| 7643 |
} |
| 7644 |
|
| 7645 |
TEST(ElementsAreArrayTest, CanBeCreatedWithIteratorRange) { |
| 7646 |
const int a[] = {1, 2, 3}; |
| 7647 |
const vector<int> test_vector(std::begin(a), std::end(a)); |
| 7648 |
const vector<int> expected(std::begin(a), std::end(a)); |
| 7649 |
EXPECT_THAT(test_vector, ElementsAreArray(expected.begin(), expected.end())); |
| 7650 |
// Pointers are iterators, too. |
| 7651 |
EXPECT_THAT(test_vector, ElementsAreArray(std::begin(a), std::end(a))); |
| 7652 |
// The empty range of NULL pointers should also be okay. |
| 7653 |
int* const null_int = nullptr; |
| 7654 |
EXPECT_THAT(test_vector, Not(ElementsAreArray(null_int, null_int))); |
| 7655 |
EXPECT_THAT((vector<int>()), ElementsAreArray(null_int, null_int)); |
| 7656 |
} |
| 7657 |
|
| 7658 |
// Since ElementsAre() and ElementsAreArray() share much of the |
| 7659 |
// implementation, we only do a sanity test for native arrays here. |
| 7660 |
TEST(ElementsAreArrayTest, WorksWithNativeArray) { |
| 7661 |
::std::string a[] = {"hi", "ho"}; |
| 7662 |
::std::string b[] = {"hi", "ho"}; |
| 7663 |
|
| 7664 |
EXPECT_THAT(a, ElementsAreArray(b)); |
| 7665 |
EXPECT_THAT(a, ElementsAreArray(b, 2)); |
| 7666 |
EXPECT_THAT(a, Not(ElementsAreArray(b, 1))); |
| 7667 |
} |
| 7668 |
|
| 7669 |
TEST(ElementsAreArrayTest, SourceLifeSpan) { |
| 7670 |
const int a[] = {1, 2, 3}; |
| 7671 |
vector<int> test_vector(std::begin(a), std::end(a)); |
| 7672 |
vector<int> expect(std::begin(a), std::end(a)); |
| 7673 |
ElementsAreArrayMatcher<int> matcher_maker = |
| 7674 |
ElementsAreArray(expect.begin(), expect.end()); |
| 7675 |
EXPECT_THAT(test_vector, matcher_maker); |
| 7676 |
// Changing in place the values that initialized matcher_maker should not |
| 7677 |
// affect matcher_maker anymore. It should have made its own copy of them. |
| 7678 |
for (int& i : expect) { |
| 7679 |
i += 10; |
| 7680 |
} |
| 7681 |
EXPECT_THAT(test_vector, matcher_maker); |
| 7682 |
test_vector.push_back(3); |
| 7683 |
EXPECT_THAT(test_vector, Not(matcher_maker)); |
| 7684 |
} |
| 7685 |
|
| 7686 |
// Tests for the MATCHER*() macro family. |
| 7687 |
|
| 7688 |
// Tests that a simple MATCHER() definition works. |
| 7689 |
|
| 7690 |
MATCHER(IsEven, "") { return (arg % 2) == 0; } |
| 7691 |
|
| 7692 |
TEST(MatcherMacroTest, Works) { |
| 7693 |
const Matcher<int> m = IsEven(); |
| 7694 |
EXPECT_TRUE(m.Matches(6)); |
| 7695 |
EXPECT_FALSE(m.Matches(7)); |
| 7696 |
|
| 7697 |
EXPECT_EQ("is even", Describe(m)); |
| 7698 |
EXPECT_EQ("not (is even)", DescribeNegation(m)); |
| 7699 |
EXPECT_EQ("", Explain(m, 6)); |
| 7700 |
EXPECT_EQ("", Explain(m, 7)); |
| 7701 |
} |
| 7702 |
|
| 7703 |
// This also tests that the description string can reference 'negation'. |
| 7704 |
MATCHER(IsEven2, negation ? "is odd" : "is even") { |
| 7705 |
if ((arg % 2) == 0) { |
| 7706 |
// Verifies that we can stream to result_listener, a listener |
| 7707 |
// supplied by the MATCHER macro implicitly. |
| 7708 |
*result_listener << "OK"; |
| 7709 |
return true; |
| 7710 |
} else { |
| 7711 |
*result_listener << "% 2 == " << (arg % 2); |
| 7712 |
return false; |
| 7713 |
} |
| 7714 |
} |
| 7715 |
|
| 7716 |
// This also tests that the description string can reference matcher |
| 7717 |
// parameters. |
| 7718 |
MATCHER_P2(EqSumOf, x, y, |
| 7719 |
std::string(negation ? "doesn't equal" : "equals") + " the sum of " + |
| 7720 |
PrintToString(x) + " and " + PrintToString(y)) { |
| 7721 |
if (arg == (x + y)) { |
| 7722 |
*result_listener << "OK"; |
| 7723 |
return true; |
| 7724 |
} else { |
| 7725 |
// Verifies that we can stream to the underlying stream of |
| 7726 |
// result_listener. |
| 7727 |
if (result_listener->stream() != nullptr) { |
| 7728 |
*result_listener->stream() << "diff == " << (x + y - arg); |
| 7729 |
} |
| 7730 |
return false; |
| 7731 |
} |
| 7732 |
} |
| 7733 |
|
| 7734 |
// Tests that the matcher description can reference 'negation' and the |
| 7735 |
// matcher parameters. |
| 7736 |
TEST(MatcherMacroTest, DescriptionCanReferenceNegationAndParameters) { |
| 7737 |
const Matcher<int> m1 = IsEven2(); |
| 7738 |
EXPECT_EQ("is even", Describe(m1)); |
| 7739 |
EXPECT_EQ("is odd", DescribeNegation(m1)); |
| 7740 |
|
| 7741 |
const Matcher<int> m2 = EqSumOf(5, 9); |
| 7742 |
EXPECT_EQ("equals the sum of 5 and 9", Describe(m2)); |
| 7743 |
EXPECT_EQ("doesn't equal the sum of 5 and 9", DescribeNegation(m2)); |
| 7744 |
} |
| 7745 |
|
| 7746 |
// Tests explaining match result in a MATCHER* macro. |
| 7747 |
TEST(MatcherMacroTest, CanExplainMatchResult) { |
| 7748 |
const Matcher<int> m1 = IsEven2(); |
| 7749 |
EXPECT_EQ("OK", Explain(m1, 4)); |
| 7750 |
EXPECT_EQ("% 2 == 1", Explain(m1, 5)); |
| 7751 |
|
| 7752 |
const Matcher<int> m2 = EqSumOf(1, 2); |
| 7753 |
EXPECT_EQ("OK", Explain(m2, 3)); |
| 7754 |
EXPECT_EQ("diff == -1", Explain(m2, 4)); |
| 7755 |
} |
| 7756 |
|
| 7757 |
// Tests that the body of MATCHER() can reference the type of the |
| 7758 |
// value being matched. |
| 7759 |
|
| 7760 |
MATCHER(IsEmptyString, "") { |
| 7761 |
StaticAssertTypeEq<::std::string, arg_type>(); |
| 7762 |
return arg.empty(); |
| 7763 |
} |
| 7764 |
|
| 7765 |
MATCHER(IsEmptyStringByRef, "") { |
| 7766 |
StaticAssertTypeEq<const ::std::string&, arg_type>(); |
| 7767 |
return arg.empty(); |
| 7768 |
} |
| 7769 |
|
| 7770 |
TEST(MatcherMacroTest, CanReferenceArgType) { |
| 7771 |
const Matcher<::std::string> m1 = IsEmptyString(); |
| 7772 |
EXPECT_TRUE(m1.Matches("")); |
| 7773 |
|
| 7774 |
const Matcher<const ::std::string&> m2 = IsEmptyStringByRef(); |
| 7775 |
EXPECT_TRUE(m2.Matches("")); |
| 7776 |
} |
| 7777 |
|
| 7778 |
// Tests that MATCHER() can be used in a namespace. |
| 7779 |
|
| 7780 |
namespace matcher_test { |
| 7781 |
MATCHER(IsOdd, "") { return (arg % 2) != 0; } |
| 7782 |
} // namespace matcher_test |
| 7783 |
|
| 7784 |
TEST(MatcherMacroTest, WorksInNamespace) { |
| 7785 |
Matcher<int> m = matcher_test::IsOdd(); |
| 7786 |
EXPECT_FALSE(m.Matches(4)); |
| 7787 |
EXPECT_TRUE(m.Matches(5)); |
| 7788 |
} |
| 7789 |
|
| 7790 |
// Tests that Value() can be used to compose matchers. |
| 7791 |
MATCHER(IsPositiveOdd, "") { |
| 7792 |
return Value(arg, matcher_test::IsOdd()) && arg > 0; |
| 7793 |
} |
| 7794 |
|
| 7795 |
TEST(MatcherMacroTest, CanBeComposedUsingValue) { |
| 7796 |
EXPECT_THAT(3, IsPositiveOdd()); |
| 7797 |
EXPECT_THAT(4, Not(IsPositiveOdd())); |
| 7798 |
EXPECT_THAT(-1, Not(IsPositiveOdd())); |
| 7799 |
} |
| 7800 |
|
| 7801 |
// Tests that a simple MATCHER_P() definition works. |
| 7802 |
|
| 7803 |
MATCHER_P(IsGreaterThan32And, n, "") { return arg > 32 && arg > n; } |
| 7804 |
|
| 7805 |
TEST(MatcherPMacroTest, Works) { |
| 7806 |
const Matcher<int> m = IsGreaterThan32And(5); |
| 7807 |
EXPECT_TRUE(m.Matches(36)); |
| 7808 |
EXPECT_FALSE(m.Matches(5)); |
| 7809 |
|
| 7810 |
EXPECT_EQ("is greater than 32 and (n: 5)", Describe(m)); |
| 7811 |
EXPECT_EQ("not (is greater than 32 and (n: 5))", DescribeNegation(m)); |
| 7812 |
EXPECT_EQ("", Explain(m, 36)); |
| 7813 |
EXPECT_EQ("", Explain(m, 5)); |
| 7814 |
} |
| 7815 |
|
| 7816 |
// Tests that the description is calculated correctly from the matcher name. |
| 7817 |
MATCHER_P(_is_Greater_Than32and_, n, "") { return arg > 32 && arg > n; } |
| 7818 |
|
| 7819 |
TEST(MatcherPMacroTest, GeneratesCorrectDescription) { |
| 7820 |
const Matcher<int> m = _is_Greater_Than32and_(5); |
| 7821 |
|
| 7822 |
EXPECT_EQ("is greater than 32 and (n: 5)", Describe(m)); |
| 7823 |
EXPECT_EQ("not (is greater than 32 and (n: 5))", DescribeNegation(m)); |
| 7824 |
EXPECT_EQ("", Explain(m, 36)); |
| 7825 |
EXPECT_EQ("", Explain(m, 5)); |
| 7826 |
} |
| 7827 |
|
| 7828 |
// Tests that a MATCHER_P matcher can be explicitly instantiated with |
| 7829 |
// a reference parameter type. |
| 7830 |
|
| 7831 |
class UncopyableFoo { |
| 7832 |
public: |
| 7833 |
explicit UncopyableFoo(char value) : value_(value) { (void)value_; } |
| 7834 |
|
| 7835 |
UncopyableFoo(const UncopyableFoo&) = delete; |
| 7836 |
void operator=(const UncopyableFoo&) = delete; |
| 7837 |
|
| 7838 |
private: |
| 7839 |
char value_; |
| 7840 |
}; |
| 7841 |
|
| 7842 |
MATCHER_P(ReferencesUncopyable, variable, "") { return &arg == &variable; } |
| 7843 |
|
| 7844 |
TEST(MatcherPMacroTest, WorksWhenExplicitlyInstantiatedWithReference) { |
| 7845 |
UncopyableFoo foo1('1'), foo2('2'); |
| 7846 |
const Matcher<const UncopyableFoo&> m = |
| 7847 |
ReferencesUncopyable<const UncopyableFoo&>(foo1); |
| 7848 |
|
| 7849 |
EXPECT_TRUE(m.Matches(foo1)); |
| 7850 |
EXPECT_FALSE(m.Matches(foo2)); |
| 7851 |
|
| 7852 |
// We don't want the address of the parameter printed, as most |
| 7853 |
// likely it will just annoy the user. If the address is |
| 7854 |
// interesting, the user should consider passing the parameter by |
| 7855 |
// pointer instead. |
| 7856 |
EXPECT_EQ("references uncopyable (variable: 1-byte object <31>)", |
| 7857 |
Describe(m)); |
| 7858 |
} |
| 7859 |
|
| 7860 |
// Tests that the body of MATCHER_Pn() can reference the parameter |
| 7861 |
// types. |
| 7862 |
|
| 7863 |
MATCHER_P3(ParamTypesAreIntLongAndChar, foo, bar, baz, "") { |
| 7864 |
StaticAssertTypeEq<int, foo_type>(); |
| 7865 |
StaticAssertTypeEq<long, bar_type>(); // NOLINT |
| 7866 |
StaticAssertTypeEq<char, baz_type>(); |
| 7867 |
return arg == 0; |
| 7868 |
} |
| 7869 |
|
| 7870 |
TEST(MatcherPnMacroTest, CanReferenceParamTypes) { |
| 7871 |
EXPECT_THAT(0, ParamTypesAreIntLongAndChar(10, 20L, 'a')); |
| 7872 |
} |
| 7873 |
|
| 7874 |
// Tests that a MATCHER_Pn matcher can be explicitly instantiated with |
| 7875 |
// reference parameter types. |
| 7876 |
|
| 7877 |
MATCHER_P2(ReferencesAnyOf, variable1, variable2, "") { |
| 7878 |
return &arg == &variable1 || &arg == &variable2; |
| 7879 |
} |
| 7880 |
|
| 7881 |
TEST(MatcherPnMacroTest, WorksWhenExplicitlyInstantiatedWithReferences) { |
| 7882 |
UncopyableFoo foo1('1'), foo2('2'), foo3('3'); |
| 7883 |
const Matcher<const UncopyableFoo&> const_m = |
| 7884 |
ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2); |
| 7885 |
|
| 7886 |
EXPECT_TRUE(const_m.Matches(foo1)); |
| 7887 |
EXPECT_TRUE(const_m.Matches(foo2)); |
| 7888 |
EXPECT_FALSE(const_m.Matches(foo3)); |
| 7889 |
|
| 7890 |
const Matcher<UncopyableFoo&> m = |
| 7891 |
ReferencesAnyOf<UncopyableFoo&, UncopyableFoo&>(foo1, foo2); |
| 7892 |
|
| 7893 |
EXPECT_TRUE(m.Matches(foo1)); |
| 7894 |
EXPECT_TRUE(m.Matches(foo2)); |
| 7895 |
EXPECT_FALSE(m.Matches(foo3)); |
| 7896 |
} |
| 7897 |
|
| 7898 |
TEST(MatcherPnMacroTest, |
| 7899 |
GeneratesCorretDescriptionWhenExplicitlyInstantiatedWithReferences) { |
| 7900 |
UncopyableFoo foo1('1'), foo2('2'); |
| 7901 |
const Matcher<const UncopyableFoo&> m = |
| 7902 |
ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2); |
| 7903 |
|
| 7904 |
// We don't want the addresses of the parameters printed, as most |
| 7905 |
// likely they will just annoy the user. If the addresses are |
| 7906 |
// interesting, the user should consider passing the parameters by |
| 7907 |
// pointers instead. |
| 7908 |
EXPECT_EQ( |
| 7909 |
"references any of (variable1: 1-byte object <31>, variable2: 1-byte " |
| 7910 |
"object <32>)", |
| 7911 |
Describe(m)); |
| 7912 |
} |
| 7913 |
|
| 7914 |
// Tests that a simple MATCHER_P2() definition works. |
| 7915 |
|
| 7916 |
MATCHER_P2(IsNotInClosedRange, low, hi, "") { return arg < low || arg > hi; } |
| 7917 |
|
| 7918 |
TEST(MatcherPnMacroTest, Works) { |
| 7919 |
const Matcher<const long&> m = IsNotInClosedRange(10, 20); // NOLINT |
| 7920 |
EXPECT_TRUE(m.Matches(36L)); |
| 7921 |
EXPECT_FALSE(m.Matches(15L)); |
| 7922 |
|
| 7923 |
EXPECT_EQ("is not in closed range (low: 10, hi: 20)", Describe(m)); |
| 7924 |
EXPECT_EQ("not (is not in closed range (low: 10, hi: 20))", |
| 7925 |
DescribeNegation(m)); |
| 7926 |
EXPECT_EQ("", Explain(m, 36L)); |
| 7927 |
EXPECT_EQ("", Explain(m, 15L)); |
| 7928 |
} |
| 7929 |
|
| 7930 |
// Tests that MATCHER*() definitions can be overloaded on the number |
| 7931 |
// of parameters; also tests MATCHER_Pn() where n >= 3. |
| 7932 |
|
| 7933 |
MATCHER(EqualsSumOf, "") { return arg == 0; } |
| 7934 |
MATCHER_P(EqualsSumOf, a, "") { return arg == a; } |
| 7935 |
MATCHER_P2(EqualsSumOf, a, b, "") { return arg == a + b; } |
| 7936 |
MATCHER_P3(EqualsSumOf, a, b, c, "") { return arg == a + b + c; } |
| 7937 |
MATCHER_P4(EqualsSumOf, a, b, c, d, "") { return arg == a + b + c + d; } |
| 7938 |
MATCHER_P5(EqualsSumOf, a, b, c, d, e, "") { return arg == a + b + c + d + e; } |
| 7939 |
MATCHER_P6(EqualsSumOf, a, b, c, d, e, f, "") { |
| 7940 |
return arg == a + b + c + d + e + f; |
| 7941 |
} |
| 7942 |
MATCHER_P7(EqualsSumOf, a, b, c, d, e, f, g, "") { |
| 7943 |
return arg == a + b + c + d + e + f + g; |
| 7944 |
} |
| 7945 |
MATCHER_P8(EqualsSumOf, a, b, c, d, e, f, g, h, "") { |
| 7946 |
return arg == a + b + c + d + e + f + g + h; |
| 7947 |
} |
| 7948 |
MATCHER_P9(EqualsSumOf, a, b, c, d, e, f, g, h, i, "") { |
| 7949 |
return arg == a + b + c + d + e + f + g + h + i; |
| 7950 |
} |
| 7951 |
MATCHER_P10(EqualsSumOf, a, b, c, d, e, f, g, h, i, j, "") { |
| 7952 |
return arg == a + b + c + d + e + f + g + h + i + j; |
| 7953 |
} |
| 7954 |
|
| 7955 |
TEST(MatcherPnMacroTest, CanBeOverloadedOnNumberOfParameters) { |
| 7956 |
EXPECT_THAT(0, EqualsSumOf()); |
| 7957 |
EXPECT_THAT(1, EqualsSumOf(1)); |
| 7958 |
EXPECT_THAT(12, EqualsSumOf(10, 2)); |
| 7959 |
EXPECT_THAT(123, EqualsSumOf(100, 20, 3)); |
| 7960 |
EXPECT_THAT(1234, EqualsSumOf(1000, 200, 30, 4)); |
| 7961 |
EXPECT_THAT(12345, EqualsSumOf(10000, 2000, 300, 40, 5)); |
| 7962 |
EXPECT_THAT("abcdef", |
| 7963 |
EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f')); |
| 7964 |
EXPECT_THAT("abcdefg", |
| 7965 |
EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g')); |
| 7966 |
EXPECT_THAT("abcdefgh", EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", |
| 7967 |
'f', 'g', "h")); |
| 7968 |
EXPECT_THAT("abcdefghi", EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", |
| 7969 |
'f', 'g', "h", 'i')); |
| 7970 |
EXPECT_THAT("abcdefghij", |
| 7971 |
EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g', "h", |
| 7972 |
'i', ::std::string("j"))); |
| 7973 |
|
| 7974 |
EXPECT_THAT(1, Not(EqualsSumOf())); |
| 7975 |
EXPECT_THAT(-1, Not(EqualsSumOf(1))); |
| 7976 |
EXPECT_THAT(-12, Not(EqualsSumOf(10, 2))); |
| 7977 |
EXPECT_THAT(-123, Not(EqualsSumOf(100, 20, 3))); |
| 7978 |
EXPECT_THAT(-1234, Not(EqualsSumOf(1000, 200, 30, 4))); |
| 7979 |
EXPECT_THAT(-12345, Not(EqualsSumOf(10000, 2000, 300, 40, 5))); |
| 7980 |
EXPECT_THAT("abcdef ", |
| 7981 |
Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f'))); |
| 7982 |
EXPECT_THAT("abcdefg ", Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", |
| 7983 |
"e", 'f', 'g'))); |
| 7984 |
EXPECT_THAT("abcdefgh ", Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", |
| 7985 |
"e", 'f', 'g', "h"))); |
| 7986 |
EXPECT_THAT("abcdefghi ", Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", |
| 7987 |
"e", 'f', 'g', "h", 'i'))); |
| 7988 |
EXPECT_THAT("abcdefghij ", |
| 7989 |
Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g', |
| 7990 |
"h", 'i', ::std::string("j")))); |
| 7991 |
} |
| 7992 |
|
| 7993 |
// Tests that a MATCHER_Pn() definition can be instantiated with any |
| 7994 |
// compatible parameter types. |
| 7995 |
TEST(MatcherPnMacroTest, WorksForDifferentParameterTypes) { |
| 7996 |
EXPECT_THAT(123, EqualsSumOf(100L, 20, static_cast<char>(3))); |
| 7997 |
EXPECT_THAT("abcd", EqualsSumOf(::std::string("a"), "b", 'c', "d")); |
| 7998 |
|
| 7999 |
EXPECT_THAT(124, Not(EqualsSumOf(100L, 20, static_cast<char>(3)))); |
| 8000 |
EXPECT_THAT("abcde", Not(EqualsSumOf(::std::string("a"), "b", 'c', "d"))); |
| 8001 |
} |
| 8002 |
|
| 8003 |
// Tests that the matcher body can promote the parameter types. |
| 8004 |
|
| 8005 |
MATCHER_P2(EqConcat, prefix, suffix, "") { |
| 8006 |
// The following lines promote the two parameters to desired types. |
| 8007 |
std::string prefix_str(prefix); |
| 8008 |
char suffix_char = static_cast<char>(suffix); |
| 8009 |
return arg == prefix_str + suffix_char; |
| 8010 |
} |
| 8011 |
|
| 8012 |
TEST(MatcherPnMacroTest, SimpleTypePromotion) { |
| 8013 |
Matcher<std::string> no_promo = EqConcat(std::string("foo"), 't'); |
| 8014 |
Matcher<const std::string&> promo = EqConcat("foo", static_cast<int>('t')); |
| 8015 |
EXPECT_FALSE(no_promo.Matches("fool")); |
| 8016 |
EXPECT_FALSE(promo.Matches("fool")); |
| 8017 |
EXPECT_TRUE(no_promo.Matches("foot")); |
| 8018 |
EXPECT_TRUE(promo.Matches("foot")); |
| 8019 |
} |
| 8020 |
|
| 8021 |
// Verifies the type of a MATCHER*. |
| 8022 |
|
| 8023 |
TEST(MatcherPnMacroTest, TypesAreCorrect) { |
| 8024 |
// EqualsSumOf() must be assignable to a EqualsSumOfMatcher variable. |
| 8025 |
EqualsSumOfMatcher a0 = EqualsSumOf(); |
| 8026 |
|
| 8027 |
// EqualsSumOf(1) must be assignable to a EqualsSumOfMatcherP variable. |
| 8028 |
EqualsSumOfMatcherP<int> a1 = EqualsSumOf(1); |
| 8029 |
|
| 8030 |
// EqualsSumOf(p1, ..., pk) must be assignable to a EqualsSumOfMatcherPk |
| 8031 |
// variable, and so on. |
| 8032 |
EqualsSumOfMatcherP2<int, char> a2 = EqualsSumOf(1, '2'); |
| 8033 |
EqualsSumOfMatcherP3<int, int, char> a3 = EqualsSumOf(1, 2, '3'); |
| 8034 |
EqualsSumOfMatcherP4<int, int, int, char> a4 = EqualsSumOf(1, 2, 3, '4'); |
| 8035 |
EqualsSumOfMatcherP5<int, int, int, int, char> a5 = |
| 8036 |
EqualsSumOf(1, 2, 3, 4, '5'); |
| 8037 |
EqualsSumOfMatcherP6<int, int, int, int, int, char> a6 = |
| 8038 |
EqualsSumOf(1, 2, 3, 4, 5, '6'); |
| 8039 |
EqualsSumOfMatcherP7<int, int, int, int, int, int, char> a7 = |
| 8040 |
EqualsSumOf(1, 2, 3, 4, 5, 6, '7'); |
| 8041 |
EqualsSumOfMatcherP8<int, int, int, int, int, int, int, char> a8 = |
| 8042 |
EqualsSumOf(1, 2, 3, 4, 5, 6, 7, '8'); |
| 8043 |
EqualsSumOfMatcherP9<int, int, int, int, int, int, int, int, char> a9 = |
| 8044 |
EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, '9'); |
| 8045 |
EqualsSumOfMatcherP10<int, int, int, int, int, int, int, int, int, char> a10 = |
| 8046 |
EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, 9, '0'); |
| 8047 |
|
| 8048 |
// Avoid "unused variable" warnings. |
| 8049 |
(void)a0; |
| 8050 |
(void)a1; |
| 8051 |
(void)a2; |
| 8052 |
(void)a3; |
| 8053 |
(void)a4; |
| 8054 |
(void)a5; |
| 8055 |
(void)a6; |
| 8056 |
(void)a7; |
| 8057 |
(void)a8; |
| 8058 |
(void)a9; |
| 8059 |
(void)a10; |
| 8060 |
} |
| 8061 |
|
| 8062 |
// Tests that matcher-typed parameters can be used in Value() inside a |
| 8063 |
// MATCHER_Pn definition. |
| 8064 |
|
| 8065 |
// Succeeds if arg matches exactly 2 of the 3 matchers. |
| 8066 |
MATCHER_P3(TwoOf, m1, m2, m3, "") { |
| 8067 |
const int count = static_cast<int>(Value(arg, m1)) + |
| 8068 |
static_cast<int>(Value(arg, m2)) + |
| 8069 |
static_cast<int>(Value(arg, m3)); |
| 8070 |
return count == 2; |
| 8071 |
} |
| 8072 |
|
| 8073 |
TEST(MatcherPnMacroTest, CanUseMatcherTypedParameterInValue) { |
| 8074 |
EXPECT_THAT(42, TwoOf(Gt(0), Lt(50), Eq(10))); |
| 8075 |
EXPECT_THAT(0, Not(TwoOf(Gt(-1), Lt(1), Eq(0)))); |
| 8076 |
} |
| 8077 |
|
| 8078 |
// Tests Contains(). |
| 8079 |
|
| 8080 |
TEST(ContainsTest, ListMatchesWhenElementIsInContainer) { |
| 8081 |
list<int> some_list; |
| 8082 |
some_list.push_back(3); |
| 8083 |
some_list.push_back(1); |
| 8084 |
some_list.push_back(2); |
| 8085 |
some_list.push_back(3); |
| 8086 |
EXPECT_THAT(some_list, Contains(1)); |
| 8087 |
EXPECT_THAT(some_list, Contains(Gt(2.5))); |
| 8088 |
EXPECT_THAT(some_list, Contains(Eq(2.0f))); |
| 8089 |
|
| 8090 |
list<std::string> another_list; |
| 8091 |
another_list.push_back("fee"); |
| 8092 |
another_list.push_back("fie"); |
| 8093 |
another_list.push_back("foe"); |
| 8094 |
another_list.push_back("fum"); |
| 8095 |
EXPECT_THAT(another_list, Contains(std::string("fee"))); |
| 8096 |
} |
| 8097 |
|
| 8098 |
TEST(ContainsTest, ListDoesNotMatchWhenElementIsNotInContainer) { |
| 8099 |
list<int> some_list; |
| 8100 |
some_list.push_back(3); |
| 8101 |
some_list.push_back(1); |
| 8102 |
EXPECT_THAT(some_list, Not(Contains(4))); |
| 8103 |
} |
| 8104 |
|
| 8105 |
TEST(ContainsTest, SetMatchesWhenElementIsInContainer) { |
| 8106 |
set<int> some_set; |
| 8107 |
some_set.insert(3); |
| 8108 |
some_set.insert(1); |
| 8109 |
some_set.insert(2); |
| 8110 |
EXPECT_THAT(some_set, Contains(Eq(1.0))); |
| 8111 |
EXPECT_THAT(some_set, Contains(Eq(3.0f))); |
| 8112 |
EXPECT_THAT(some_set, Contains(2)); |
| 8113 |
|
| 8114 |
set<std::string> another_set; |
| 8115 |
another_set.insert("fee"); |
| 8116 |
another_set.insert("fie"); |
| 8117 |
another_set.insert("foe"); |
| 8118 |
another_set.insert("fum"); |
| 8119 |
EXPECT_THAT(another_set, Contains(Eq(std::string("fum")))); |
| 8120 |
} |
| 8121 |
|
| 8122 |
TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) { |
| 8123 |
set<int> some_set; |
| 8124 |
some_set.insert(3); |
| 8125 |
some_set.insert(1); |
| 8126 |
EXPECT_THAT(some_set, Not(Contains(4))); |
| 8127 |
|
| 8128 |
set<std::string> c_string_set; |
| 8129 |
c_string_set.insert("hello"); |
| 8130 |
EXPECT_THAT(c_string_set, Not(Contains(std::string("goodbye")))); |
| 8131 |
} |
| 8132 |
|
| 8133 |
TEST(ContainsTest, ExplainsMatchResultCorrectly) { |
| 8134 |
const int a[2] = {1, 2}; |
| 8135 |
Matcher<const int(&)[2]> m = Contains(2); |
| 8136 |
EXPECT_EQ("whose element #1 matches", Explain(m, a)); |
| 8137 |
|
| 8138 |
m = Contains(3); |
| 8139 |
EXPECT_EQ("", Explain(m, a)); |
| 8140 |
|
| 8141 |
m = Contains(GreaterThan(0)); |
| 8142 |
EXPECT_EQ("whose element #0 matches, which is 1 more than 0", Explain(m, a)); |
| 8143 |
|
| 8144 |
m = Contains(GreaterThan(10)); |
| 8145 |
EXPECT_EQ("", Explain(m, a)); |
| 8146 |
} |
| 8147 |
|
| 8148 |
TEST(ContainsTest, DescribesItselfCorrectly) { |
| 8149 |
Matcher<vector<int>> m = Contains(1); |
| 8150 |
EXPECT_EQ("contains at least one element that is equal to 1", Describe(m)); |
| 8151 |
|
| 8152 |
Matcher<vector<int>> m2 = Not(m); |
| 8153 |
EXPECT_EQ("doesn't contain any element that is equal to 1", Describe(m2)); |
| 8154 |
} |
| 8155 |
|
| 8156 |
TEST(ContainsTest, MapMatchesWhenElementIsInContainer) { |
| 8157 |
map<std::string, int> my_map; |
| 8158 |
const char* bar = "a string"; |
| 8159 |
my_map[bar] = 2; |
| 8160 |
EXPECT_THAT(my_map, Contains(pair<const char* const, int>(bar, 2))); |
| 8161 |
|
| 8162 |
map<std::string, int> another_map; |
| 8163 |
another_map["fee"] = 1; |
| 8164 |
another_map["fie"] = 2; |
| 8165 |
another_map["foe"] = 3; |
| 8166 |
another_map["fum"] = 4; |
| 8167 |
EXPECT_THAT(another_map, |
| 8168 |
Contains(pair<const std::string, int>(std::string("fee"), 1))); |
| 8169 |
EXPECT_THAT(another_map, Contains(pair<const std::string, int>("fie", 2))); |
| 8170 |
} |
| 8171 |
|
| 8172 |
TEST(ContainsTest, MapDoesNotMatchWhenElementIsNotInContainer) { |
| 8173 |
map<int, int> some_map; |
| 8174 |
some_map[1] = 11; |
| 8175 |
some_map[2] = 22; |
| 8176 |
EXPECT_THAT(some_map, Not(Contains(pair<const int, int>(2, 23)))); |
| 8177 |
} |
| 8178 |
|
| 8179 |
TEST(ContainsTest, ArrayMatchesWhenElementIsInContainer) { |
| 8180 |
const char* string_array[] = {"fee", "fie", "foe", "fum"}; |
| 8181 |
EXPECT_THAT(string_array, Contains(Eq(std::string("fum")))); |
| 8182 |
} |
| 8183 |
|
| 8184 |
TEST(ContainsTest, ArrayDoesNotMatchWhenElementIsNotInContainer) { |
| 8185 |
int int_array[] = {1, 2, 3, 4}; |
| 8186 |
EXPECT_THAT(int_array, Not(Contains(5))); |
| 8187 |
} |
| 8188 |
|
| 8189 |
TEST(ContainsTest, AcceptsMatcher) { |
| 8190 |
const int a[] = {1, 2, 3}; |
| 8191 |
EXPECT_THAT(a, Contains(Gt(2))); |
| 8192 |
EXPECT_THAT(a, Not(Contains(Gt(4)))); |
| 8193 |
} |
| 8194 |
|
| 8195 |
TEST(ContainsTest, WorksForNativeArrayAsTuple) { |
| 8196 |
const int a[] = {1, 2}; |
| 8197 |
const int* const pointer = a; |
| 8198 |
EXPECT_THAT(std::make_tuple(pointer, 2), Contains(1)); |
| 8199 |
EXPECT_THAT(std::make_tuple(pointer, 2), Not(Contains(Gt(3)))); |
| 8200 |
} |
| 8201 |
|
| 8202 |
TEST(ContainsTest, WorksForTwoDimensionalNativeArray) { |
| 8203 |
int a[][3] = {{1, 2, 3}, {4, 5, 6}}; |
| 8204 |
EXPECT_THAT(a, Contains(ElementsAre(4, 5, 6))); |
| 8205 |
EXPECT_THAT(a, Contains(Contains(5))); |
| 8206 |
EXPECT_THAT(a, Not(Contains(ElementsAre(3, 4, 5)))); |
| 8207 |
EXPECT_THAT(a, Contains(Not(Contains(5)))); |
| 8208 |
} |
| 8209 |
|
| 8210 |
// Tests Contains().Times(). |
| 8211 |
|
| 8212 |
TEST(ContainsTimes, ListMatchesWhenElementQuantityMatches) { |
| 8213 |
list<int> some_list; |
| 8214 |
some_list.push_back(3); |
| 8215 |
some_list.push_back(1); |
| 8216 |
some_list.push_back(2); |
| 8217 |
some_list.push_back(3); |
| 8218 |
EXPECT_THAT(some_list, Contains(3).Times(2)); |
| 8219 |
EXPECT_THAT(some_list, Contains(2).Times(1)); |
| 8220 |
EXPECT_THAT(some_list, Contains(Ge(2)).Times(3)); |
| 8221 |
EXPECT_THAT(some_list, Contains(Ge(2)).Times(Gt(2))); |
| 8222 |
EXPECT_THAT(some_list, Contains(4).Times(0)); |
| 8223 |
EXPECT_THAT(some_list, Contains(_).Times(4)); |
| 8224 |
EXPECT_THAT(some_list, Not(Contains(5).Times(1))); |
| 8225 |
EXPECT_THAT(some_list, Contains(5).Times(_)); // Times(_) always matches |
| 8226 |
EXPECT_THAT(some_list, Not(Contains(3).Times(1))); |
| 8227 |
EXPECT_THAT(some_list, Contains(3).Times(Not(1))); |
| 8228 |
EXPECT_THAT(list<int>{}, Not(Contains(_))); |
| 8229 |
} |
| 8230 |
|
| 8231 |
TEST(ContainsTimes, ExplainsMatchResultCorrectly) { |
| 8232 |
const int a[2] = {1, 2}; |
| 8233 |
Matcher<const int(&)[2]> m = Contains(2).Times(3); |
| 8234 |
EXPECT_EQ( |
| 8235 |
"whose element #1 matches but whose match quantity of 1 does not match", |
| 8236 |
Explain(m, a)); |
| 8237 |
|
| 8238 |
m = Contains(3).Times(0); |
| 8239 |
EXPECT_EQ("has no element that matches and whose match quantity of 0 matches", |
| 8240 |
Explain(m, a)); |
| 8241 |
|
| 8242 |
m = Contains(3).Times(4); |
| 8243 |
EXPECT_EQ( |
| 8244 |
"has no element that matches and whose match quantity of 0 does not " |
| 8245 |
"match", |
| 8246 |
Explain(m, a)); |
| 8247 |
|
| 8248 |
m = Contains(2).Times(4); |
| 8249 |
EXPECT_EQ( |
| 8250 |
"whose element #1 matches but whose match quantity of 1 does not " |
| 8251 |
"match", |
| 8252 |
Explain(m, a)); |
| 8253 |
|
| 8254 |
m = Contains(GreaterThan(0)).Times(2); |
| 8255 |
EXPECT_EQ("whose elements (0, 1) match and whose match quantity of 2 matches", |
| 8256 |
Explain(m, a)); |
| 8257 |
|
| 8258 |
m = Contains(GreaterThan(10)).Times(Gt(1)); |
| 8259 |
EXPECT_EQ( |
| 8260 |
"has no element that matches and whose match quantity of 0 does not " |
| 8261 |
"match", |
| 8262 |
Explain(m, a)); |
| 8263 |
|
| 8264 |
m = Contains(GreaterThan(0)).Times(GreaterThan<size_t>(5)); |
| 8265 |
EXPECT_EQ( |
| 8266 |
"whose elements (0, 1) match but whose match quantity of 2 does not " |
| 8267 |
"match, which is 3 less than 5", |
| 8268 |
Explain(m, a)); |
| 8269 |
} |
| 8270 |
|
| 8271 |
TEST(ContainsTimes, DescribesItselfCorrectly) { |
| 8272 |
Matcher<vector<int>> m = Contains(1).Times(2); |
| 8273 |
EXPECT_EQ("quantity of elements that match is equal to 1 is equal to 2", |
| 8274 |
Describe(m)); |
| 8275 |
|
| 8276 |
Matcher<vector<int>> m2 = Not(m); |
| 8277 |
EXPECT_EQ("quantity of elements that match is equal to 1 isn't equal to 2", |
| 8278 |
Describe(m2)); |
| 8279 |
} |
| 8280 |
|
| 8281 |
// Tests AllOfArray() |
| 8282 |
|
| 8283 |
TEST(AllOfArrayTest, BasicForms) { |
| 8284 |
// Iterator |
| 8285 |
std::vector<int> v0{}; |
| 8286 |
std::vector<int> v1{1}; |
| 8287 |
std::vector<int> v2{2, 3}; |
| 8288 |
std::vector<int> v3{4, 4, 4}; |
| 8289 |
EXPECT_THAT(0, AllOfArray(v0.begin(), v0.end())); |
| 8290 |
EXPECT_THAT(1, AllOfArray(v1.begin(), v1.end())); |
| 8291 |
EXPECT_THAT(2, Not(AllOfArray(v1.begin(), v1.end()))); |
| 8292 |
EXPECT_THAT(3, Not(AllOfArray(v2.begin(), v2.end()))); |
| 8293 |
EXPECT_THAT(4, AllOfArray(v3.begin(), v3.end())); |
| 8294 |
// Pointer + size |
| 8295 |
int ar[6] = {1, 2, 3, 4, 4, 4}; |
| 8296 |
EXPECT_THAT(0, AllOfArray(ar, 0)); |
| 8297 |
EXPECT_THAT(1, AllOfArray(ar, 1)); |
| 8298 |
EXPECT_THAT(2, Not(AllOfArray(ar, 1))); |
| 8299 |
EXPECT_THAT(3, Not(AllOfArray(ar + 1, 3))); |
| 8300 |
EXPECT_THAT(4, AllOfArray(ar + 3, 3)); |
| 8301 |
// Array |
| 8302 |
// int ar0[0]; Not usable |
| 8303 |
int ar1[1] = {1}; |
| 8304 |
int ar2[2] = {2, 3}; |
| 8305 |
int ar3[3] = {4, 4, 4}; |
| 8306 |
// EXPECT_THAT(0, Not(AllOfArray(ar0))); // Cannot work |
| 8307 |
EXPECT_THAT(1, AllOfArray(ar1)); |
| 8308 |
EXPECT_THAT(2, Not(AllOfArray(ar1))); |
| 8309 |
EXPECT_THAT(3, Not(AllOfArray(ar2))); |
| 8310 |
EXPECT_THAT(4, AllOfArray(ar3)); |
| 8311 |
// Container |
| 8312 |
EXPECT_THAT(0, AllOfArray(v0)); |
| 8313 |
EXPECT_THAT(1, AllOfArray(v1)); |
| 8314 |
EXPECT_THAT(2, Not(AllOfArray(v1))); |
| 8315 |
EXPECT_THAT(3, Not(AllOfArray(v2))); |
| 8316 |
EXPECT_THAT(4, AllOfArray(v3)); |
| 8317 |
// Initializer |
| 8318 |
EXPECT_THAT(0, AllOfArray<int>({})); // Requires template arg. |
| 8319 |
EXPECT_THAT(1, AllOfArray({1})); |
| 8320 |
EXPECT_THAT(2, Not(AllOfArray({1}))); |
| 8321 |
EXPECT_THAT(3, Not(AllOfArray({2, 3}))); |
| 8322 |
EXPECT_THAT(4, AllOfArray({4, 4, 4})); |
| 8323 |
} |
| 8324 |
|
| 8325 |
TEST(AllOfArrayTest, Matchers) { |
| 8326 |
// vector |
| 8327 |
std::vector<Matcher<int>> matchers{Ge(1), Lt(2)}; |
| 8328 |
EXPECT_THAT(0, Not(AllOfArray(matchers))); |
| 8329 |
EXPECT_THAT(1, AllOfArray(matchers)); |
| 8330 |
EXPECT_THAT(2, Not(AllOfArray(matchers))); |
| 8331 |
// initializer_list |
| 8332 |
EXPECT_THAT(0, Not(AllOfArray({Ge(0), Ge(1)}))); |
| 8333 |
EXPECT_THAT(1, AllOfArray({Ge(0), Ge(1)})); |
| 8334 |
} |
| 8335 |
|
| 8336 |
TEST(AnyOfArrayTest, BasicForms) { |
| 8337 |
// Iterator |
| 8338 |
std::vector<int> v0{}; |
| 8339 |
std::vector<int> v1{1}; |
| 8340 |
std::vector<int> v2{2, 3}; |
| 8341 |
EXPECT_THAT(0, Not(AnyOfArray(v0.begin(), v0.end()))); |
| 8342 |
EXPECT_THAT(1, AnyOfArray(v1.begin(), v1.end())); |
| 8343 |
EXPECT_THAT(2, Not(AnyOfArray(v1.begin(), v1.end()))); |
| 8344 |
EXPECT_THAT(3, AnyOfArray(v2.begin(), v2.end())); |
| 8345 |
EXPECT_THAT(4, Not(AnyOfArray(v2.begin(), v2.end()))); |
| 8346 |
// Pointer + size |
| 8347 |
int ar[3] = {1, 2, 3}; |
| 8348 |
EXPECT_THAT(0, Not(AnyOfArray(ar, 0))); |
| 8349 |
EXPECT_THAT(1, AnyOfArray(ar, 1)); |
| 8350 |
EXPECT_THAT(2, Not(AnyOfArray(ar, 1))); |
| 8351 |
EXPECT_THAT(3, AnyOfArray(ar + 1, 2)); |
| 8352 |
EXPECT_THAT(4, Not(AnyOfArray(ar + 1, 2))); |
| 8353 |
// Array |
| 8354 |
// int ar0[0]; Not usable |
| 8355 |
int ar1[1] = {1}; |
| 8356 |
int ar2[2] = {2, 3}; |
| 8357 |
// EXPECT_THAT(0, Not(AnyOfArray(ar0))); // Cannot work |
| 8358 |
EXPECT_THAT(1, AnyOfArray(ar1)); |
| 8359 |
EXPECT_THAT(2, Not(AnyOfArray(ar1))); |
| 8360 |
EXPECT_THAT(3, AnyOfArray(ar2)); |
| 8361 |
EXPECT_THAT(4, Not(AnyOfArray(ar2))); |
| 8362 |
// Container |
| 8363 |
EXPECT_THAT(0, Not(AnyOfArray(v0))); |
| 8364 |
EXPECT_THAT(1, AnyOfArray(v1)); |
| 8365 |
EXPECT_THAT(2, Not(AnyOfArray(v1))); |
| 8366 |
EXPECT_THAT(3, AnyOfArray(v2)); |
| 8367 |
EXPECT_THAT(4, Not(AnyOfArray(v2))); |
| 8368 |
// Initializer |
| 8369 |
EXPECT_THAT(0, Not(AnyOfArray<int>({}))); // Requires template arg. |
| 8370 |
EXPECT_THAT(1, AnyOfArray({1})); |
| 8371 |
EXPECT_THAT(2, Not(AnyOfArray({1}))); |
| 8372 |
EXPECT_THAT(3, AnyOfArray({2, 3})); |
| 8373 |
EXPECT_THAT(4, Not(AnyOfArray({2, 3}))); |
| 8374 |
} |
| 8375 |
|
| 8376 |
TEST(AnyOfArrayTest, Matchers) { |
| 8377 |
// We negate test AllOfArrayTest.Matchers. |
| 8378 |
// vector |
| 8379 |
std::vector<Matcher<int>> matchers{Lt(1), Ge(2)}; |
| 8380 |
EXPECT_THAT(0, AnyOfArray(matchers)); |
| 8381 |
EXPECT_THAT(1, Not(AnyOfArray(matchers))); |
| 8382 |
EXPECT_THAT(2, AnyOfArray(matchers)); |
| 8383 |
// initializer_list |
| 8384 |
EXPECT_THAT(0, AnyOfArray({Lt(0), Lt(1)})); |
| 8385 |
EXPECT_THAT(1, Not(AllOfArray({Lt(0), Lt(1)}))); |
| 8386 |
} |
| 8387 |
|
| 8388 |
TEST(AnyOfArrayTest, ExplainsMatchResultCorrectly) { |
| 8389 |
// AnyOfArray and AllOfArry use the same underlying template-template, |
| 8390 |
// thus it is sufficient to test one here. |
| 8391 |
const std::vector<int> v0{}; |
| 8392 |
const std::vector<int> v1{1}; |
| 8393 |
const std::vector<int> v2{2, 3}; |
| 8394 |
const Matcher<int> m0 = AnyOfArray(v0); |
| 8395 |
const Matcher<int> m1 = AnyOfArray(v1); |
| 8396 |
const Matcher<int> m2 = AnyOfArray(v2); |
| 8397 |
EXPECT_EQ("", Explain(m0, 0)); |
| 8398 |
EXPECT_EQ("", Explain(m1, 1)); |
| 8399 |
EXPECT_EQ("", Explain(m1, 2)); |
| 8400 |
EXPECT_EQ("", Explain(m2, 3)); |
| 8401 |
EXPECT_EQ("", Explain(m2, 4)); |
| 8402 |
EXPECT_EQ("()", Describe(m0)); |
| 8403 |
EXPECT_EQ("(is equal to 1)", Describe(m1)); |
| 8404 |
EXPECT_EQ("(is equal to 2) or (is equal to 3)", Describe(m2)); |
| 8405 |
EXPECT_EQ("()", DescribeNegation(m0)); |
| 8406 |
EXPECT_EQ("(isn't equal to 1)", DescribeNegation(m1)); |
| 8407 |
EXPECT_EQ("(isn't equal to 2) and (isn't equal to 3)", DescribeNegation(m2)); |
| 8408 |
// Explain with matchers |
| 8409 |
const Matcher<int> g1 = AnyOfArray({GreaterThan(1)}); |
| 8410 |
const Matcher<int> g2 = AnyOfArray({GreaterThan(1), GreaterThan(2)}); |
| 8411 |
// Explains the first positive match and all prior negative matches... |
| 8412 |
EXPECT_EQ("which is 1 less than 1", Explain(g1, 0)); |
| 8413 |
EXPECT_EQ("which is the same as 1", Explain(g1, 1)); |
| 8414 |
EXPECT_EQ("which is 1 more than 1", Explain(g1, 2)); |
| 8415 |
EXPECT_EQ("which is 1 less than 1, and which is 2 less than 2", |
| 8416 |
Explain(g2, 0)); |
| 8417 |
EXPECT_EQ("which is the same as 1, and which is 1 less than 2", |
| 8418 |
Explain(g2, 1)); |
| 8419 |
EXPECT_EQ("which is 1 more than 1", // Only the first |
| 8420 |
Explain(g2, 2)); |
| 8421 |
} |
| 8422 |
|
| 8423 |
TEST(AllOfTest, HugeMatcher) { |
| 8424 |
// Verify that using AllOf with many arguments doesn't cause |
| 8425 |
// the compiler to exceed template instantiation depth limit. |
| 8426 |
EXPECT_THAT(0, testing::AllOf(_, _, _, _, _, _, _, _, _, |
| 8427 |
testing::AllOf(_, _, _, _, _, _, _, _, _, _))); |
| 8428 |
} |
| 8429 |
|
| 8430 |
TEST(AnyOfTest, HugeMatcher) { |
| 8431 |
// Verify that using AnyOf with many arguments doesn't cause |
| 8432 |
// the compiler to exceed template instantiation depth limit. |
| 8433 |
EXPECT_THAT(0, testing::AnyOf(_, _, _, _, _, _, _, _, _, |
| 8434 |
testing::AnyOf(_, _, _, _, _, _, _, _, _, _))); |
| 8435 |
} |
| 8436 |
|
| 8437 |
namespace adl_test { |
| 8438 |
|
| 8439 |
// Verifies that the implementation of ::testing::AllOf and ::testing::AnyOf |
| 8440 |
// don't issue unqualified recursive calls. If they do, the argument dependent |
| 8441 |
// name lookup will cause AllOf/AnyOf in the 'adl_test' namespace to be found |
| 8442 |
// as a candidate and the compilation will break due to an ambiguous overload. |
| 8443 |
|
| 8444 |
// The matcher must be in the same namespace as AllOf/AnyOf to make argument |
| 8445 |
// dependent lookup find those. |
| 8446 |
MATCHER(M, "") { |
| 8447 |
(void)arg; |
| 8448 |
return true; |
| 8449 |
} |
| 8450 |
|
| 8451 |
template <typename T1, typename T2> |
| 8452 |
bool AllOf(const T1& /*t1*/, const T2& /*t2*/) { |
| 8453 |
return true; |
| 8454 |
} |
| 8455 |
|
| 8456 |
TEST(AllOfTest, DoesNotCallAllOfUnqualified) { |
| 8457 |
EXPECT_THAT(42, |
| 8458 |
testing::AllOf(M(), M(), M(), M(), M(), M(), M(), M(), M(), M())); |
| 8459 |
} |
| 8460 |
|
| 8461 |
template <typename T1, typename T2> |
| 8462 |
bool AnyOf(const T1&, const T2&) { |
| 8463 |
return true; |
| 8464 |
} |
| 8465 |
|
| 8466 |
TEST(AnyOfTest, DoesNotCallAnyOfUnqualified) { |
| 8467 |
EXPECT_THAT(42, |
| 8468 |
testing::AnyOf(M(), M(), M(), M(), M(), M(), M(), M(), M(), M())); |
| 8469 |
} |
| 8470 |
|
| 8471 |
} // namespace adl_test |
| 8472 |
|
| 8473 |
TEST(AllOfTest, WorksOnMoveOnlyType) { |
| 8474 |
std::unique_ptr<int> p(new int(3)); |
| 8475 |
EXPECT_THAT(p, AllOf(Pointee(Eq(3)), Pointee(Gt(0)), Pointee(Lt(5)))); |
| 8476 |
EXPECT_THAT(p, Not(AllOf(Pointee(Eq(3)), Pointee(Gt(0)), Pointee(Lt(3))))); |
| 8477 |
} |
| 8478 |
|
| 8479 |
TEST(AnyOfTest, WorksOnMoveOnlyType) { |
| 8480 |
std::unique_ptr<int> p(new int(3)); |
| 8481 |
EXPECT_THAT(p, AnyOf(Pointee(Eq(5)), Pointee(Lt(0)), Pointee(Lt(5)))); |
| 8482 |
EXPECT_THAT(p, Not(AnyOf(Pointee(Eq(5)), Pointee(Lt(0)), Pointee(Gt(5))))); |
| 8483 |
} |
| 8484 |
|
| 8485 |
MATCHER(IsNotNull, "") { return arg != nullptr; } |
| 8486 |
|
| 8487 |
// Verifies that a matcher defined using MATCHER() can work on |
| 8488 |
// move-only types. |
| 8489 |
TEST(MatcherMacroTest, WorksOnMoveOnlyType) { |
| 8490 |
std::unique_ptr<int> p(new int(3)); |
| 8491 |
EXPECT_THAT(p, IsNotNull()); |
| 8492 |
EXPECT_THAT(std::unique_ptr<int>(), Not(IsNotNull())); |
| 8493 |
} |
| 8494 |
|
| 8495 |
MATCHER_P(UniquePointee, pointee, "") { return *arg == pointee; } |
| 8496 |
|
| 8497 |
// Verifies that a matcher defined using MATCHER_P*() can work on |
| 8498 |
// move-only types. |
| 8499 |
TEST(MatcherPMacroTest, WorksOnMoveOnlyType) { |
| 8500 |
std::unique_ptr<int> p(new int(3)); |
| 8501 |
EXPECT_THAT(p, UniquePointee(3)); |
| 8502 |
EXPECT_THAT(p, Not(UniquePointee(2))); |
| 8503 |
} |
| 8504 |
|
| 8505 |
#if GTEST_HAS_EXCEPTIONS |
| 8506 |
|
| 8507 |
// std::function<void()> is used below for compatibility with older copies of |
| 8508 |
// GCC. Normally, a raw lambda is all that is needed. |
| 8509 |
|
| 8510 |
// Test that examples from documentation compile |
| 8511 |
TEST(ThrowsTest, Examples) { |
| 8512 |
EXPECT_THAT( |
| 8513 |
std::function<void()>([]() { throw std::runtime_error("message"); }), |
| 8514 |
Throws<std::runtime_error>()); |
| 8515 |
|
| 8516 |
EXPECT_THAT( |
| 8517 |
std::function<void()>([]() { throw std::runtime_error("message"); }), |
| 8518 |
ThrowsMessage<std::runtime_error>(HasSubstr("message"))); |
| 8519 |
} |
| 8520 |
|
| 8521 |
TEST(ThrowsTest, PrintsExceptionWhat) { |
| 8522 |
EXPECT_THAT( |
| 8523 |
std::function<void()>([]() { throw std::runtime_error("ABC123XYZ"); }), |
| 8524 |
ThrowsMessage<std::runtime_error>(HasSubstr("ABC123XYZ"))); |
| 8525 |
} |
| 8526 |
|
| 8527 |
TEST(ThrowsTest, DoesNotGenerateDuplicateCatchClauseWarning) { |
| 8528 |
EXPECT_THAT(std::function<void()>([]() { throw std::exception(); }), |
| 8529 |
Throws<std::exception>()); |
| 8530 |
} |
| 8531 |
|
| 8532 |
TEST(ThrowsTest, CallableExecutedExactlyOnce) { |
| 8533 |
size_t a = 0; |
| 8534 |
|
| 8535 |
EXPECT_THAT(std::function<void()>([&a]() { |
| 8536 |
a++; |
| 8537 |
throw 10; |
| 8538 |
}), |
| 8539 |
Throws<int>()); |
| 8540 |
EXPECT_EQ(a, 1u); |
| 8541 |
|
| 8542 |
EXPECT_THAT(std::function<void()>([&a]() { |
| 8543 |
a++; |
| 8544 |
throw std::runtime_error("message"); |
| 8545 |
}), |
| 8546 |
Throws<std::runtime_error>()); |
| 8547 |
EXPECT_EQ(a, 2u); |
| 8548 |
|
| 8549 |
EXPECT_THAT(std::function<void()>([&a]() { |
| 8550 |
a++; |
| 8551 |
throw std::runtime_error("message"); |
| 8552 |
}), |
| 8553 |
ThrowsMessage<std::runtime_error>(HasSubstr("message"))); |
| 8554 |
EXPECT_EQ(a, 3u); |
| 8555 |
|
| 8556 |
EXPECT_THAT(std::function<void()>([&a]() { |
| 8557 |
a++; |
| 8558 |
throw std::runtime_error("message"); |
| 8559 |
}), |
| 8560 |
Throws<std::runtime_error>( |
| 8561 |
Property(&std::runtime_error::what, HasSubstr("message")))); |
| 8562 |
EXPECT_EQ(a, 4u); |
| 8563 |
} |
| 8564 |
|
| 8565 |
TEST(ThrowsTest, Describe) { |
| 8566 |
Matcher<std::function<void()>> matcher = Throws<std::runtime_error>(); |
| 8567 |
std::stringstream ss; |
| 8568 |
matcher.DescribeTo(&ss); |
| 8569 |
auto explanation = ss.str(); |
| 8570 |
EXPECT_THAT(explanation, HasSubstr("std::runtime_error")); |
| 8571 |
} |
| 8572 |
|
| 8573 |
TEST(ThrowsTest, Success) { |
| 8574 |
Matcher<std::function<void()>> matcher = Throws<std::runtime_error>(); |
| 8575 |
StringMatchResultListener listener; |
| 8576 |
EXPECT_TRUE(matcher.MatchAndExplain( |
| 8577 |
[]() { throw std::runtime_error("error message"); }, &listener)); |
| 8578 |
EXPECT_THAT(listener.str(), HasSubstr("std::runtime_error")); |
| 8579 |
} |
| 8580 |
|
| 8581 |
TEST(ThrowsTest, FailWrongType) { |
| 8582 |
Matcher<std::function<void()>> matcher = Throws<std::runtime_error>(); |
| 8583 |
StringMatchResultListener listener; |
| 8584 |
EXPECT_FALSE(matcher.MatchAndExplain( |
| 8585 |
[]() { throw std::logic_error("error message"); }, &listener)); |
| 8586 |
EXPECT_THAT(listener.str(), HasSubstr("std::logic_error")); |
| 8587 |
EXPECT_THAT(listener.str(), HasSubstr("\"error message\"")); |
| 8588 |
} |
| 8589 |
|
| 8590 |
TEST(ThrowsTest, FailWrongTypeNonStd) { |
| 8591 |
Matcher<std::function<void()>> matcher = Throws<std::runtime_error>(); |
| 8592 |
StringMatchResultListener listener; |
| 8593 |
EXPECT_FALSE(matcher.MatchAndExplain([]() { throw 10; }, &listener)); |
| 8594 |
EXPECT_THAT(listener.str(), |
| 8595 |
HasSubstr("throws an exception of an unknown type")); |
| 8596 |
} |
| 8597 |
|
| 8598 |
TEST(ThrowsTest, FailNoThrow) { |
| 8599 |
Matcher<std::function<void()>> matcher = Throws<std::runtime_error>(); |
| 8600 |
StringMatchResultListener listener; |
| 8601 |
EXPECT_FALSE(matcher.MatchAndExplain([]() { (void)0; }, &listener)); |
| 8602 |
EXPECT_THAT(listener.str(), HasSubstr("does not throw any exception")); |
| 8603 |
} |
| 8604 |
|
| 8605 |
class ThrowsPredicateTest |
| 8606 |
: public TestWithParam<Matcher<std::function<void()>>> {}; |
| 8607 |
|
| 8608 |
TEST_P(ThrowsPredicateTest, Describe) { |
| 8609 |
Matcher<std::function<void()>> matcher = GetParam(); |
| 8610 |
std::stringstream ss; |
| 8611 |
matcher.DescribeTo(&ss); |
| 8612 |
auto explanation = ss.str(); |
| 8613 |
EXPECT_THAT(explanation, HasSubstr("std::runtime_error")); |
| 8614 |
EXPECT_THAT(explanation, HasSubstr("error message")); |
| 8615 |
} |
| 8616 |
|
| 8617 |
TEST_P(ThrowsPredicateTest, Success) { |
| 8618 |
Matcher<std::function<void()>> matcher = GetParam(); |
| 8619 |
StringMatchResultListener listener; |
| 8620 |
EXPECT_TRUE(matcher.MatchAndExplain( |
| 8621 |
[]() { throw std::runtime_error("error message"); }, &listener)); |
| 8622 |
EXPECT_THAT(listener.str(), HasSubstr("std::runtime_error")); |
| 8623 |
} |
| 8624 |
|
| 8625 |
TEST_P(ThrowsPredicateTest, FailWrongType) { |
| 8626 |
Matcher<std::function<void()>> matcher = GetParam(); |
| 8627 |
StringMatchResultListener listener; |
| 8628 |
EXPECT_FALSE(matcher.MatchAndExplain( |
| 8629 |
[]() { throw std::logic_error("error message"); }, &listener)); |
| 8630 |
EXPECT_THAT(listener.str(), HasSubstr("std::logic_error")); |
| 8631 |
EXPECT_THAT(listener.str(), HasSubstr("\"error message\"")); |
| 8632 |
} |
| 8633 |
|
| 8634 |
TEST_P(ThrowsPredicateTest, FailWrongTypeNonStd) { |
| 8635 |
Matcher<std::function<void()>> matcher = GetParam(); |
| 8636 |
StringMatchResultListener listener; |
| 8637 |
EXPECT_FALSE(matcher.MatchAndExplain([]() { throw 10; }, &listener)); |
| 8638 |
EXPECT_THAT(listener.str(), |
| 8639 |
HasSubstr("throws an exception of an unknown type")); |
| 8640 |
} |
| 8641 |
|
| 8642 |
TEST_P(ThrowsPredicateTest, FailNoThrow) { |
| 8643 |
Matcher<std::function<void()>> matcher = GetParam(); |
| 8644 |
StringMatchResultListener listener; |
| 8645 |
EXPECT_FALSE(matcher.MatchAndExplain([]() {}, &listener)); |
| 8646 |
EXPECT_THAT(listener.str(), HasSubstr("does not throw any exception")); |
| 8647 |
} |
| 8648 |
|
| 8649 |
INSTANTIATE_TEST_SUITE_P( |
| 8650 |
AllMessagePredicates, ThrowsPredicateTest, |
| 8651 |
Values(Matcher<std::function<void()>>( |
| 8652 |
ThrowsMessage<std::runtime_error>(HasSubstr("error message"))))); |
| 8653 |
|
| 8654 |
// Tests that Throws<E1>(Matcher<E2>{}) compiles even when E2 != const E1&. |
| 8655 |
TEST(ThrowsPredicateCompilesTest, ExceptionMatcherAcceptsBroadType) { |
| 8656 |
{ |
| 8657 |
Matcher<std::function<void()>> matcher = |
| 8658 |
ThrowsMessage<std::runtime_error>(HasSubstr("error message")); |
| 8659 |
EXPECT_TRUE( |
| 8660 |
matcher.Matches([]() { throw std::runtime_error("error message"); })); |
| 8661 |
EXPECT_FALSE( |
| 8662 |
matcher.Matches([]() { throw std::runtime_error("wrong message"); })); |
| 8663 |
} |
| 8664 |
|
| 8665 |
{ |
| 8666 |
Matcher<uint64_t> inner = Eq(10); |
| 8667 |
Matcher<std::function<void()>> matcher = Throws<uint32_t>(inner); |
| 8668 |
EXPECT_TRUE(matcher.Matches([]() { throw(uint32_t) 10; })); |
| 8669 |
EXPECT_FALSE(matcher.Matches([]() { throw(uint32_t) 11; })); |
| 8670 |
} |
| 8671 |
} |
| 8672 |
|
| 8673 |
// Tests that ThrowsMessage("message") is equivalent |
| 8674 |
// to ThrowsMessage(Eq<std::string>("message")). |
| 8675 |
TEST(ThrowsPredicateCompilesTest, MessageMatcherAcceptsNonMatcher) { |
| 8676 |
Matcher<std::function<void()>> matcher = |
| 8677 |
ThrowsMessage<std::runtime_error>("error message"); |
| 8678 |
EXPECT_TRUE( |
| 8679 |
matcher.Matches([]() { throw std::runtime_error("error message"); })); |
| 8680 |
EXPECT_FALSE(matcher.Matches( |
| 8681 |
[]() { throw std::runtime_error("wrong error message"); })); |
| 8682 |
} |
| 8683 |
|
| 8684 |
#endif // GTEST_HAS_EXCEPTIONS |
| 8685 |
|
| 8686 |
} // namespace |
| 8687 |
} // namespace gmock_matchers_test |
| 8688 |
} // namespace testing |
| 8689 |
|
| 8690 |
#ifdef _MSC_VER |
| 8691 |
# pragma warning(pop) |
| 8692 |
#endif |