| OLD | NEW |
| (Empty) |
| 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 // Author: wan@google.com (Zhanyong Wan) | |
| 31 | |
| 32 // Google Mock - a framework for writing C++ mock classes. | |
| 33 // | |
| 34 // This file tests some commonly used argument matchers. | |
| 35 | |
| 36 #include <gmock/gmock-matchers.h> | |
| 37 | |
| 38 #include <string.h> | |
| 39 #include <functional> | |
| 40 #include <iostream> | |
| 41 #include <list> | |
| 42 #include <map> | |
| 43 #include <set> | |
| 44 #include <sstream> | |
| 45 #include <string> | |
| 46 #include <utility> | |
| 47 #include <vector> | |
| 48 #include <gmock/gmock.h> | |
| 49 #include <gtest/gtest.h> | |
| 50 #include <gtest/gtest-spi.h> | |
| 51 | |
| 52 namespace testing { | |
| 53 | |
| 54 namespace internal { | |
| 55 string FormatMatcherDescriptionSyntaxError(const char* description, | |
| 56 const char* error_pos); | |
| 57 int GetParamIndex(const char* param_names[], const string& param_name); | |
| 58 string JoinAsTuple(const Strings& fields); | |
| 59 bool SkipPrefix(const char* prefix, const char** pstr); | |
| 60 } // namespace internal | |
| 61 | |
| 62 namespace gmock_matchers_test { | |
| 63 | |
| 64 using std::list; | |
| 65 using std::make_pair; | |
| 66 using std::map; | |
| 67 using std::multimap; | |
| 68 using std::multiset; | |
| 69 using std::ostream; | |
| 70 using std::pair; | |
| 71 using std::set; | |
| 72 using std::stringstream; | |
| 73 using std::tr1::get; | |
| 74 using std::tr1::make_tuple; | |
| 75 using std::tr1::tuple; | |
| 76 using std::vector; | |
| 77 using testing::A; | |
| 78 using testing::AllArgs; | |
| 79 using testing::AllOf; | |
| 80 using testing::An; | |
| 81 using testing::AnyOf; | |
| 82 using testing::ByRef; | |
| 83 using testing::ContainsRegex; | |
| 84 using testing::DoubleEq; | |
| 85 using testing::EndsWith; | |
| 86 using testing::Eq; | |
| 87 using testing::ExplainMatchResult; | |
| 88 using testing::Field; | |
| 89 using testing::FloatEq; | |
| 90 using testing::Ge; | |
| 91 using testing::Gt; | |
| 92 using testing::HasSubstr; | |
| 93 using testing::IsNull; | |
| 94 using testing::Key; | |
| 95 using testing::Le; | |
| 96 using testing::Lt; | |
| 97 using testing::MakeMatcher; | |
| 98 using testing::MakePolymorphicMatcher; | |
| 99 using testing::MatchResultListener; | |
| 100 using testing::Matcher; | |
| 101 using testing::MatcherCast; | |
| 102 using testing::MatcherInterface; | |
| 103 using testing::Matches; | |
| 104 using testing::MatchesRegex; | |
| 105 using testing::NanSensitiveDoubleEq; | |
| 106 using testing::NanSensitiveFloatEq; | |
| 107 using testing::Ne; | |
| 108 using testing::Not; | |
| 109 using testing::NotNull; | |
| 110 using testing::Pair; | |
| 111 using testing::Pointee; | |
| 112 using testing::Pointwise; | |
| 113 using testing::PolymorphicMatcher; | |
| 114 using testing::Property; | |
| 115 using testing::Ref; | |
| 116 using testing::ResultOf; | |
| 117 using testing::StartsWith; | |
| 118 using testing::StrCaseEq; | |
| 119 using testing::StrCaseNe; | |
| 120 using testing::StrEq; | |
| 121 using testing::StrNe; | |
| 122 using testing::Truly; | |
| 123 using testing::TypedEq; | |
| 124 using testing::Value; | |
| 125 using testing::_; | |
| 126 using testing::internal::DummyMatchResultListener; | |
| 127 using testing::internal::ExplainMatchFailureTupleTo; | |
| 128 using testing::internal::FloatingEqMatcher; | |
| 129 using testing::internal::FormatMatcherDescriptionSyntaxError; | |
| 130 using testing::internal::GetParamIndex; | |
| 131 using testing::internal::Interpolation; | |
| 132 using testing::internal::Interpolations; | |
| 133 using testing::internal::JoinAsTuple; | |
| 134 using testing::internal::RE; | |
| 135 using testing::internal::SkipPrefix; | |
| 136 using testing::internal::StreamMatchResultListener; | |
| 137 using testing::internal::String; | |
| 138 using testing::internal::StringMatchResultListener; | |
| 139 using testing::internal::Strings; | |
| 140 using testing::internal::ValidateMatcherDescription; | |
| 141 using testing::internal::kInvalidInterpolation; | |
| 142 using testing::internal::kPercentInterpolation; | |
| 143 using testing::internal::kTupleInterpolation; | |
| 144 using testing::internal::linked_ptr; | |
| 145 using testing::internal::scoped_ptr; | |
| 146 using testing::internal::string; | |
| 147 | |
| 148 // For testing ExplainMatchResultTo(). | |
| 149 class GreaterThanMatcher : public MatcherInterface<int> { | |
| 150 public: | |
| 151 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {} | |
| 152 | |
| 153 virtual void DescribeTo(ostream* os) const { | |
| 154 *os << "is > " << rhs_; | |
| 155 } | |
| 156 | |
| 157 virtual bool MatchAndExplain(int lhs, | |
| 158 MatchResultListener* listener) const { | |
| 159 const int diff = lhs - rhs_; | |
| 160 if (diff > 0) { | |
| 161 *listener << "which is " << diff << " more than " << rhs_; | |
| 162 } else if (diff == 0) { | |
| 163 *listener << "which is the same as " << rhs_; | |
| 164 } else { | |
| 165 *listener << "which is " << -diff << " less than " << rhs_; | |
| 166 } | |
| 167 | |
| 168 return lhs > rhs_; | |
| 169 } | |
| 170 | |
| 171 private: | |
| 172 int rhs_; | |
| 173 }; | |
| 174 | |
| 175 Matcher<int> GreaterThan(int n) { | |
| 176 return MakeMatcher(new GreaterThanMatcher(n)); | |
| 177 } | |
| 178 | |
| 179 // Returns the description of the given matcher. | |
| 180 template <typename T> | |
| 181 string Describe(const Matcher<T>& m) { | |
| 182 stringstream ss; | |
| 183 m.DescribeTo(&ss); | |
| 184 return ss.str(); | |
| 185 } | |
| 186 | |
| 187 // Returns the description of the negation of the given matcher. | |
| 188 template <typename T> | |
| 189 string DescribeNegation(const Matcher<T>& m) { | |
| 190 stringstream ss; | |
| 191 m.DescribeNegationTo(&ss); | |
| 192 return ss.str(); | |
| 193 } | |
| 194 | |
| 195 // Returns the reason why x matches, or doesn't match, m. | |
| 196 template <typename MatcherType, typename Value> | |
| 197 string Explain(const MatcherType& m, const Value& x) { | |
| 198 StringMatchResultListener listener; | |
| 199 ExplainMatchResult(m, x, &listener); | |
| 200 return listener.str(); | |
| 201 } | |
| 202 | |
| 203 TEST(MatchResultListenerTest, StreamingWorks) { | |
| 204 StringMatchResultListener listener; | |
| 205 listener << "hi" << 5; | |
| 206 EXPECT_EQ("hi5", listener.str()); | |
| 207 | |
| 208 // Streaming shouldn't crash when the underlying ostream is NULL. | |
| 209 DummyMatchResultListener dummy; | |
| 210 dummy << "hi" << 5; | |
| 211 } | |
| 212 | |
| 213 TEST(MatchResultListenerTest, CanAccessUnderlyingStream) { | |
| 214 EXPECT_TRUE(DummyMatchResultListener().stream() == NULL); | |
| 215 EXPECT_TRUE(StreamMatchResultListener(NULL).stream() == NULL); | |
| 216 | |
| 217 EXPECT_EQ(&std::cout, StreamMatchResultListener(&std::cout).stream()); | |
| 218 } | |
| 219 | |
| 220 TEST(MatchResultListenerTest, IsInterestedWorks) { | |
| 221 EXPECT_TRUE(StringMatchResultListener().IsInterested()); | |
| 222 EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested()); | |
| 223 | |
| 224 EXPECT_FALSE(DummyMatchResultListener().IsInterested()); | |
| 225 EXPECT_FALSE(StreamMatchResultListener(NULL).IsInterested()); | |
| 226 } | |
| 227 | |
| 228 // Makes sure that the MatcherInterface<T> interface doesn't | |
| 229 // change. | |
| 230 class EvenMatcherImpl : public MatcherInterface<int> { | |
| 231 public: | |
| 232 virtual bool MatchAndExplain(int x, | |
| 233 MatchResultListener* /* listener */) const { | |
| 234 return x % 2 == 0; | |
| 235 } | |
| 236 | |
| 237 virtual void DescribeTo(ostream* os) const { | |
| 238 *os << "is an even number"; | |
| 239 } | |
| 240 | |
| 241 // We deliberately don't define DescribeNegationTo() and | |
| 242 // ExplainMatchResultTo() here, to make sure the definition of these | |
| 243 // two methods is optional. | |
| 244 }; | |
| 245 | |
| 246 // Makes sure that the MatcherInterface API doesn't change. | |
| 247 TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) { | |
| 248 EvenMatcherImpl m; | |
| 249 } | |
| 250 | |
| 251 // Tests implementing a monomorphic matcher using MatchAndExplain(). | |
| 252 | |
| 253 class NewEvenMatcherImpl : public MatcherInterface<int> { | |
| 254 public: | |
| 255 virtual bool MatchAndExplain(int x, MatchResultListener* listener) const { | |
| 256 const bool match = x % 2 == 0; | |
| 257 // Verifies that we can stream to a listener directly. | |
| 258 *listener << "value % " << 2; | |
| 259 if (listener->stream() != NULL) { | |
| 260 // Verifies that we can stream to a listener's underlying stream | |
| 261 // too. | |
| 262 *listener->stream() << " == " << (x % 2); | |
| 263 } | |
| 264 return match; | |
| 265 } | |
| 266 | |
| 267 virtual void DescribeTo(ostream* os) const { | |
| 268 *os << "is an even number"; | |
| 269 } | |
| 270 }; | |
| 271 | |
| 272 TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) { | |
| 273 Matcher<int> m = MakeMatcher(new NewEvenMatcherImpl); | |
| 274 EXPECT_TRUE(m.Matches(2)); | |
| 275 EXPECT_FALSE(m.Matches(3)); | |
| 276 EXPECT_EQ("value % 2 == 0", Explain(m, 2)); | |
| 277 EXPECT_EQ("value % 2 == 1", Explain(m, 3)); | |
| 278 } | |
| 279 | |
| 280 // Tests default-constructing a matcher. | |
| 281 TEST(MatcherTest, CanBeDefaultConstructed) { | |
| 282 Matcher<double> m; | |
| 283 } | |
| 284 | |
| 285 // Tests that Matcher<T> can be constructed from a MatcherInterface<T>*. | |
| 286 TEST(MatcherTest, CanBeConstructedFromMatcherInterface) { | |
| 287 const MatcherInterface<int>* impl = new EvenMatcherImpl; | |
| 288 Matcher<int> m(impl); | |
| 289 EXPECT_TRUE(m.Matches(4)); | |
| 290 EXPECT_FALSE(m.Matches(5)); | |
| 291 } | |
| 292 | |
| 293 // Tests that value can be used in place of Eq(value). | |
| 294 TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) { | |
| 295 Matcher<int> m1 = 5; | |
| 296 EXPECT_TRUE(m1.Matches(5)); | |
| 297 EXPECT_FALSE(m1.Matches(6)); | |
| 298 } | |
| 299 | |
| 300 // Tests that NULL can be used in place of Eq(NULL). | |
| 301 TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) { | |
| 302 Matcher<int*> m1 = NULL; | |
| 303 EXPECT_TRUE(m1.Matches(NULL)); | |
| 304 int n = 0; | |
| 305 EXPECT_FALSE(m1.Matches(&n)); | |
| 306 } | |
| 307 | |
| 308 // Tests that matchers are copyable. | |
| 309 TEST(MatcherTest, IsCopyable) { | |
| 310 // Tests the copy constructor. | |
| 311 Matcher<bool> m1 = Eq(false); | |
| 312 EXPECT_TRUE(m1.Matches(false)); | |
| 313 EXPECT_FALSE(m1.Matches(true)); | |
| 314 | |
| 315 // Tests the assignment operator. | |
| 316 m1 = Eq(true); | |
| 317 EXPECT_TRUE(m1.Matches(true)); | |
| 318 EXPECT_FALSE(m1.Matches(false)); | |
| 319 } | |
| 320 | |
| 321 // Tests that Matcher<T>::DescribeTo() calls | |
| 322 // MatcherInterface<T>::DescribeTo(). | |
| 323 TEST(MatcherTest, CanDescribeItself) { | |
| 324 EXPECT_EQ("is an even number", | |
| 325 Describe(Matcher<int>(new EvenMatcherImpl))); | |
| 326 } | |
| 327 | |
| 328 // Tests Matcher<T>::MatchAndExplain(). | |
| 329 TEST(MatcherTest, MatchAndExplain) { | |
| 330 Matcher<int> m = GreaterThan(0); | |
| 331 StringMatchResultListener listener1; | |
| 332 EXPECT_TRUE(m.MatchAndExplain(42, &listener1)); | |
| 333 EXPECT_EQ("which is 42 more than 0", listener1.str()); | |
| 334 | |
| 335 StringMatchResultListener listener2; | |
| 336 EXPECT_FALSE(m.MatchAndExplain(-9, &listener2)); | |
| 337 EXPECT_EQ("which is 9 less than 0", listener2.str()); | |
| 338 } | |
| 339 | |
| 340 // Tests that a C-string literal can be implicitly converted to a | |
| 341 // Matcher<string> or Matcher<const string&>. | |
| 342 TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) { | |
| 343 Matcher<string> m1 = "hi"; | |
| 344 EXPECT_TRUE(m1.Matches("hi")); | |
| 345 EXPECT_FALSE(m1.Matches("hello")); | |
| 346 | |
| 347 Matcher<const string&> m2 = "hi"; | |
| 348 EXPECT_TRUE(m2.Matches("hi")); | |
| 349 EXPECT_FALSE(m2.Matches("hello")); | |
| 350 } | |
| 351 | |
| 352 // Tests that a string object can be implicitly converted to a | |
| 353 // Matcher<string> or Matcher<const string&>. | |
| 354 TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) { | |
| 355 Matcher<string> m1 = string("hi"); | |
| 356 EXPECT_TRUE(m1.Matches("hi")); | |
| 357 EXPECT_FALSE(m1.Matches("hello")); | |
| 358 | |
| 359 Matcher<const string&> m2 = string("hi"); | |
| 360 EXPECT_TRUE(m2.Matches("hi")); | |
| 361 EXPECT_FALSE(m2.Matches("hello")); | |
| 362 } | |
| 363 | |
| 364 // Tests that MakeMatcher() constructs a Matcher<T> from a | |
| 365 // MatcherInterface* without requiring the user to explicitly | |
| 366 // write the type. | |
| 367 TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) { | |
| 368 const MatcherInterface<int>* dummy_impl = NULL; | |
| 369 Matcher<int> m = MakeMatcher(dummy_impl); | |
| 370 } | |
| 371 | |
| 372 // Tests that MakePolymorphicMatcher() can construct a polymorphic | |
| 373 // matcher from its implementation using the old API. | |
| 374 const int g_bar = 1; | |
| 375 class ReferencesBarOrIsZeroImpl { | |
| 376 public: | |
| 377 template <typename T> | |
| 378 bool MatchAndExplain(const T& x, | |
| 379 MatchResultListener* /* listener */) const { | |
| 380 const void* p = &x; | |
| 381 return p == &g_bar || x == 0; | |
| 382 } | |
| 383 | |
| 384 void DescribeTo(ostream* os) const { *os << "g_bar or zero"; } | |
| 385 | |
| 386 void DescribeNegationTo(ostream* os) const { | |
| 387 *os << "doesn't reference g_bar and is not zero"; | |
| 388 } | |
| 389 }; | |
| 390 | |
| 391 // This function verifies that MakePolymorphicMatcher() returns a | |
| 392 // PolymorphicMatcher<T> where T is the argument's type. | |
| 393 PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() { | |
| 394 return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl()); | |
| 395 } | |
| 396 | |
| 397 TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) { | |
| 398 // Using a polymorphic matcher to match a reference type. | |
| 399 Matcher<const int&> m1 = ReferencesBarOrIsZero(); | |
| 400 EXPECT_TRUE(m1.Matches(0)); | |
| 401 // Verifies that the identity of a by-reference argument is preserved. | |
| 402 EXPECT_TRUE(m1.Matches(g_bar)); | |
| 403 EXPECT_FALSE(m1.Matches(1)); | |
| 404 EXPECT_EQ("g_bar or zero", Describe(m1)); | |
| 405 | |
| 406 // Using a polymorphic matcher to match a value type. | |
| 407 Matcher<double> m2 = ReferencesBarOrIsZero(); | |
| 408 EXPECT_TRUE(m2.Matches(0.0)); | |
| 409 EXPECT_FALSE(m2.Matches(0.1)); | |
| 410 EXPECT_EQ("g_bar or zero", Describe(m2)); | |
| 411 } | |
| 412 | |
| 413 // Tests implementing a polymorphic matcher using MatchAndExplain(). | |
| 414 | |
| 415 class PolymorphicIsEvenImpl { | |
| 416 public: | |
| 417 void DescribeTo(ostream* os) const { *os << "is even"; } | |
| 418 | |
| 419 void DescribeNegationTo(ostream* os) const { | |
| 420 *os << "is odd"; | |
| 421 } | |
| 422 | |
| 423 template <typename T> | |
| 424 bool MatchAndExplain(const T& x, MatchResultListener* listener) const { | |
| 425 // Verifies that we can stream to the listener directly. | |
| 426 *listener << "% " << 2; | |
| 427 if (listener->stream() != NULL) { | |
| 428 // Verifies that we can stream to the listener's underlying stream | |
| 429 // too. | |
| 430 *listener->stream() << " == " << (x % 2); | |
| 431 } | |
| 432 return (x % 2) == 0; | |
| 433 } | |
| 434 }; | |
| 435 | |
| 436 PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() { | |
| 437 return MakePolymorphicMatcher(PolymorphicIsEvenImpl()); | |
| 438 } | |
| 439 | |
| 440 TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) { | |
| 441 // Using PolymorphicIsEven() as a Matcher<int>. | |
| 442 const Matcher<int> m1 = PolymorphicIsEven(); | |
| 443 EXPECT_TRUE(m1.Matches(42)); | |
| 444 EXPECT_FALSE(m1.Matches(43)); | |
| 445 EXPECT_EQ("is even", Describe(m1)); | |
| 446 | |
| 447 const Matcher<int> not_m1 = Not(m1); | |
| 448 EXPECT_EQ("is odd", Describe(not_m1)); | |
| 449 | |
| 450 EXPECT_EQ("% 2 == 0", Explain(m1, 42)); | |
| 451 | |
| 452 // Using PolymorphicIsEven() as a Matcher<char>. | |
| 453 const Matcher<char> m2 = PolymorphicIsEven(); | |
| 454 EXPECT_TRUE(m2.Matches('\x42')); | |
| 455 EXPECT_FALSE(m2.Matches('\x43')); | |
| 456 EXPECT_EQ("is even", Describe(m2)); | |
| 457 | |
| 458 const Matcher<char> not_m2 = Not(m2); | |
| 459 EXPECT_EQ("is odd", Describe(not_m2)); | |
| 460 | |
| 461 EXPECT_EQ("% 2 == 0", Explain(m2, '\x42')); | |
| 462 } | |
| 463 | |
| 464 // Tests that MatcherCast<T>(m) works when m is a polymorphic matcher. | |
| 465 TEST(MatcherCastTest, FromPolymorphicMatcher) { | |
| 466 Matcher<int> m = MatcherCast<int>(Eq(5)); | |
| 467 EXPECT_TRUE(m.Matches(5)); | |
| 468 EXPECT_FALSE(m.Matches(6)); | |
| 469 } | |
| 470 | |
| 471 // For testing casting matchers between compatible types. | |
| 472 class IntValue { | |
| 473 public: | |
| 474 // An int can be statically (although not implicitly) cast to a | |
| 475 // IntValue. | |
| 476 explicit IntValue(int a_value) : value_(a_value) {} | |
| 477 | |
| 478 int value() const { return value_; } | |
| 479 private: | |
| 480 int value_; | |
| 481 }; | |
| 482 | |
| 483 // For testing casting matchers between compatible types. | |
| 484 bool IsPositiveIntValue(const IntValue& foo) { | |
| 485 return foo.value() > 0; | |
| 486 } | |
| 487 | |
| 488 // Tests that MatcherCast<T>(m) works when m is a Matcher<U> where T | |
| 489 // can be statically converted to U. | |
| 490 TEST(MatcherCastTest, FromCompatibleType) { | |
| 491 Matcher<double> m1 = Eq(2.0); | |
| 492 Matcher<int> m2 = MatcherCast<int>(m1); | |
| 493 EXPECT_TRUE(m2.Matches(2)); | |
| 494 EXPECT_FALSE(m2.Matches(3)); | |
| 495 | |
| 496 Matcher<IntValue> m3 = Truly(IsPositiveIntValue); | |
| 497 Matcher<int> m4 = MatcherCast<int>(m3); | |
| 498 // In the following, the arguments 1 and 0 are statically converted | |
| 499 // to IntValue objects, and then tested by the IsPositiveIntValue() | |
| 500 // predicate. | |
| 501 EXPECT_TRUE(m4.Matches(1)); | |
| 502 EXPECT_FALSE(m4.Matches(0)); | |
| 503 } | |
| 504 | |
| 505 // Tests that MatcherCast<T>(m) works when m is a Matcher<const T&>. | |
| 506 TEST(MatcherCastTest, FromConstReferenceToNonReference) { | |
| 507 Matcher<const int&> m1 = Eq(0); | |
| 508 Matcher<int> m2 = MatcherCast<int>(m1); | |
| 509 EXPECT_TRUE(m2.Matches(0)); | |
| 510 EXPECT_FALSE(m2.Matches(1)); | |
| 511 } | |
| 512 | |
| 513 // Tests that MatcherCast<T>(m) works when m is a Matcher<T&>. | |
| 514 TEST(MatcherCastTest, FromReferenceToNonReference) { | |
| 515 Matcher<int&> m1 = Eq(0); | |
| 516 Matcher<int> m2 = MatcherCast<int>(m1); | |
| 517 EXPECT_TRUE(m2.Matches(0)); | |
| 518 EXPECT_FALSE(m2.Matches(1)); | |
| 519 } | |
| 520 | |
| 521 // Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>. | |
| 522 TEST(MatcherCastTest, FromNonReferenceToConstReference) { | |
| 523 Matcher<int> m1 = Eq(0); | |
| 524 Matcher<const int&> m2 = MatcherCast<const int&>(m1); | |
| 525 EXPECT_TRUE(m2.Matches(0)); | |
| 526 EXPECT_FALSE(m2.Matches(1)); | |
| 527 } | |
| 528 | |
| 529 // Tests that MatcherCast<T&>(m) works when m is a Matcher<T>. | |
| 530 TEST(MatcherCastTest, FromNonReferenceToReference) { | |
| 531 Matcher<int> m1 = Eq(0); | |
| 532 Matcher<int&> m2 = MatcherCast<int&>(m1); | |
| 533 int n = 0; | |
| 534 EXPECT_TRUE(m2.Matches(n)); | |
| 535 n = 1; | |
| 536 EXPECT_FALSE(m2.Matches(n)); | |
| 537 } | |
| 538 | |
| 539 // Tests that MatcherCast<T>(m) works when m is a Matcher<T>. | |
| 540 TEST(MatcherCastTest, FromSameType) { | |
| 541 Matcher<int> m1 = Eq(0); | |
| 542 Matcher<int> m2 = MatcherCast<int>(m1); | |
| 543 EXPECT_TRUE(m2.Matches(0)); | |
| 544 EXPECT_FALSE(m2.Matches(1)); | |
| 545 } | |
| 546 | |
| 547 class Base {}; | |
| 548 class Derived : public Base {}; | |
| 549 | |
| 550 // Tests that SafeMatcherCast<T>(m) works when m is a polymorphic matcher. | |
| 551 TEST(SafeMatcherCastTest, FromPolymorphicMatcher) { | |
| 552 Matcher<char> m2 = SafeMatcherCast<char>(Eq(32)); | |
| 553 EXPECT_TRUE(m2.Matches(' ')); | |
| 554 EXPECT_FALSE(m2.Matches('\n')); | |
| 555 } | |
| 556 | |
| 557 // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where | |
| 558 // T and U are arithmetic types and T can be losslessly converted to | |
| 559 // U. | |
| 560 TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) { | |
| 561 Matcher<double> m1 = DoubleEq(1.0); | |
| 562 Matcher<float> m2 = SafeMatcherCast<float>(m1); | |
| 563 EXPECT_TRUE(m2.Matches(1.0f)); | |
| 564 EXPECT_FALSE(m2.Matches(2.0f)); | |
| 565 | |
| 566 Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>('a')); | |
| 567 EXPECT_TRUE(m3.Matches('a')); | |
| 568 EXPECT_FALSE(m3.Matches('b')); | |
| 569 } | |
| 570 | |
| 571 // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where T and U | |
| 572 // are pointers or references to a derived and a base class, correspondingly. | |
| 573 TEST(SafeMatcherCastTest, FromBaseClass) { | |
| 574 Derived d, d2; | |
| 575 Matcher<Base*> m1 = Eq(&d); | |
| 576 Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1); | |
| 577 EXPECT_TRUE(m2.Matches(&d)); | |
| 578 EXPECT_FALSE(m2.Matches(&d2)); | |
| 579 | |
| 580 Matcher<Base&> m3 = Ref(d); | |
| 581 Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3); | |
| 582 EXPECT_TRUE(m4.Matches(d)); | |
| 583 EXPECT_FALSE(m4.Matches(d2)); | |
| 584 } | |
| 585 | |
| 586 // Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<const T&>. | |
| 587 TEST(SafeMatcherCastTest, FromConstReferenceToReference) { | |
| 588 int n = 0; | |
| 589 Matcher<const int&> m1 = Ref(n); | |
| 590 Matcher<int&> m2 = SafeMatcherCast<int&>(m1); | |
| 591 int n1 = 0; | |
| 592 EXPECT_TRUE(m2.Matches(n)); | |
| 593 EXPECT_FALSE(m2.Matches(n1)); | |
| 594 } | |
| 595 | |
| 596 // Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>. | |
| 597 TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) { | |
| 598 Matcher<int> m1 = Eq(0); | |
| 599 Matcher<const int&> m2 = SafeMatcherCast<const int&>(m1); | |
| 600 EXPECT_TRUE(m2.Matches(0)); | |
| 601 EXPECT_FALSE(m2.Matches(1)); | |
| 602 } | |
| 603 | |
| 604 // Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<T>. | |
| 605 TEST(SafeMatcherCastTest, FromNonReferenceToReference) { | |
| 606 Matcher<int> m1 = Eq(0); | |
| 607 Matcher<int&> m2 = SafeMatcherCast<int&>(m1); | |
| 608 int n = 0; | |
| 609 EXPECT_TRUE(m2.Matches(n)); | |
| 610 n = 1; | |
| 611 EXPECT_FALSE(m2.Matches(n)); | |
| 612 } | |
| 613 | |
| 614 // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<T>. | |
| 615 TEST(SafeMatcherCastTest, FromSameType) { | |
| 616 Matcher<int> m1 = Eq(0); | |
| 617 Matcher<int> m2 = SafeMatcherCast<int>(m1); | |
| 618 EXPECT_TRUE(m2.Matches(0)); | |
| 619 EXPECT_FALSE(m2.Matches(1)); | |
| 620 } | |
| 621 | |
| 622 // Tests that A<T>() matches any value of type T. | |
| 623 TEST(ATest, MatchesAnyValue) { | |
| 624 // Tests a matcher for a value type. | |
| 625 Matcher<double> m1 = A<double>(); | |
| 626 EXPECT_TRUE(m1.Matches(91.43)); | |
| 627 EXPECT_TRUE(m1.Matches(-15.32)); | |
| 628 | |
| 629 // Tests a matcher for a reference type. | |
| 630 int a = 2; | |
| 631 int b = -6; | |
| 632 Matcher<int&> m2 = A<int&>(); | |
| 633 EXPECT_TRUE(m2.Matches(a)); | |
| 634 EXPECT_TRUE(m2.Matches(b)); | |
| 635 } | |
| 636 | |
| 637 // Tests that A<T>() describes itself properly. | |
| 638 TEST(ATest, CanDescribeSelf) { | |
| 639 EXPECT_EQ("is anything", Describe(A<bool>())); | |
| 640 } | |
| 641 | |
| 642 // Tests that An<T>() matches any value of type T. | |
| 643 TEST(AnTest, MatchesAnyValue) { | |
| 644 // Tests a matcher for a value type. | |
| 645 Matcher<int> m1 = An<int>(); | |
| 646 EXPECT_TRUE(m1.Matches(9143)); | |
| 647 EXPECT_TRUE(m1.Matches(-1532)); | |
| 648 | |
| 649 // Tests a matcher for a reference type. | |
| 650 int a = 2; | |
| 651 int b = -6; | |
| 652 Matcher<int&> m2 = An<int&>(); | |
| 653 EXPECT_TRUE(m2.Matches(a)); | |
| 654 EXPECT_TRUE(m2.Matches(b)); | |
| 655 } | |
| 656 | |
| 657 // Tests that An<T>() describes itself properly. | |
| 658 TEST(AnTest, CanDescribeSelf) { | |
| 659 EXPECT_EQ("is anything", Describe(An<int>())); | |
| 660 } | |
| 661 | |
| 662 // Tests that _ can be used as a matcher for any type and matches any | |
| 663 // value of that type. | |
| 664 TEST(UnderscoreTest, MatchesAnyValue) { | |
| 665 // Uses _ as a matcher for a value type. | |
| 666 Matcher<int> m1 = _; | |
| 667 EXPECT_TRUE(m1.Matches(123)); | |
| 668 EXPECT_TRUE(m1.Matches(-242)); | |
| 669 | |
| 670 // Uses _ as a matcher for a reference type. | |
| 671 bool a = false; | |
| 672 const bool b = true; | |
| 673 Matcher<const bool&> m2 = _; | |
| 674 EXPECT_TRUE(m2.Matches(a)); | |
| 675 EXPECT_TRUE(m2.Matches(b)); | |
| 676 } | |
| 677 | |
| 678 // Tests that _ describes itself properly. | |
| 679 TEST(UnderscoreTest, CanDescribeSelf) { | |
| 680 Matcher<int> m = _; | |
| 681 EXPECT_EQ("is anything", Describe(m)); | |
| 682 } | |
| 683 | |
| 684 // Tests that Eq(x) matches any value equal to x. | |
| 685 TEST(EqTest, MatchesEqualValue) { | |
| 686 // 2 C-strings with same content but different addresses. | |
| 687 const char a1[] = "hi"; | |
| 688 const char a2[] = "hi"; | |
| 689 | |
| 690 Matcher<const char*> m1 = Eq(a1); | |
| 691 EXPECT_TRUE(m1.Matches(a1)); | |
| 692 EXPECT_FALSE(m1.Matches(a2)); | |
| 693 } | |
| 694 | |
| 695 // Tests that Eq(v) describes itself properly. | |
| 696 | |
| 697 class Unprintable { | |
| 698 public: | |
| 699 Unprintable() : c_('a') {} | |
| 700 | |
| 701 bool operator==(const Unprintable& /* rhs */) { return true; } | |
| 702 private: | |
| 703 char c_; | |
| 704 }; | |
| 705 | |
| 706 TEST(EqTest, CanDescribeSelf) { | |
| 707 Matcher<Unprintable> m = Eq(Unprintable()); | |
| 708 EXPECT_EQ("is equal to 1-byte object <61>", Describe(m)); | |
| 709 } | |
| 710 | |
| 711 // Tests that Eq(v) can be used to match any type that supports | |
| 712 // comparing with type T, where T is v's type. | |
| 713 TEST(EqTest, IsPolymorphic) { | |
| 714 Matcher<int> m1 = Eq(1); | |
| 715 EXPECT_TRUE(m1.Matches(1)); | |
| 716 EXPECT_FALSE(m1.Matches(2)); | |
| 717 | |
| 718 Matcher<char> m2 = Eq(1); | |
| 719 EXPECT_TRUE(m2.Matches('\1')); | |
| 720 EXPECT_FALSE(m2.Matches('a')); | |
| 721 } | |
| 722 | |
| 723 // Tests that TypedEq<T>(v) matches values of type T that's equal to v. | |
| 724 TEST(TypedEqTest, ChecksEqualityForGivenType) { | |
| 725 Matcher<char> m1 = TypedEq<char>('a'); | |
| 726 EXPECT_TRUE(m1.Matches('a')); | |
| 727 EXPECT_FALSE(m1.Matches('b')); | |
| 728 | |
| 729 Matcher<int> m2 = TypedEq<int>(6); | |
| 730 EXPECT_TRUE(m2.Matches(6)); | |
| 731 EXPECT_FALSE(m2.Matches(7)); | |
| 732 } | |
| 733 | |
| 734 // Tests that TypedEq(v) describes itself properly. | |
| 735 TEST(TypedEqTest, CanDescribeSelf) { | |
| 736 EXPECT_EQ("is equal to 2", Describe(TypedEq<int>(2))); | |
| 737 } | |
| 738 | |
| 739 // Tests that TypedEq<T>(v) has type Matcher<T>. | |
| 740 | |
| 741 // Type<T>::IsTypeOf(v) compiles iff the type of value v is T, where T | |
| 742 // is a "bare" type (i.e. not in the form of const U or U&). If v's | |
| 743 // type is not T, the compiler will generate a message about | |
| 744 // "undefined referece". | |
| 745 template <typename T> | |
| 746 struct Type { | |
| 747 static bool IsTypeOf(const T& /* v */) { return true; } | |
| 748 | |
| 749 template <typename T2> | |
| 750 static void IsTypeOf(T2 v); | |
| 751 }; | |
| 752 | |
| 753 TEST(TypedEqTest, HasSpecifiedType) { | |
| 754 // Verfies that the type of TypedEq<T>(v) is Matcher<T>. | |
| 755 Type<Matcher<int> >::IsTypeOf(TypedEq<int>(5)); | |
| 756 Type<Matcher<double> >::IsTypeOf(TypedEq<double>(5)); | |
| 757 } | |
| 758 | |
| 759 // Tests that Ge(v) matches anything >= v. | |
| 760 TEST(GeTest, ImplementsGreaterThanOrEqual) { | |
| 761 Matcher<int> m1 = Ge(0); | |
| 762 EXPECT_TRUE(m1.Matches(1)); | |
| 763 EXPECT_TRUE(m1.Matches(0)); | |
| 764 EXPECT_FALSE(m1.Matches(-1)); | |
| 765 } | |
| 766 | |
| 767 // Tests that Ge(v) describes itself properly. | |
| 768 TEST(GeTest, CanDescribeSelf) { | |
| 769 Matcher<int> m = Ge(5); | |
| 770 EXPECT_EQ("is >= 5", Describe(m)); | |
| 771 } | |
| 772 | |
| 773 // Tests that Gt(v) matches anything > v. | |
| 774 TEST(GtTest, ImplementsGreaterThan) { | |
| 775 Matcher<double> m1 = Gt(0); | |
| 776 EXPECT_TRUE(m1.Matches(1.0)); | |
| 777 EXPECT_FALSE(m1.Matches(0.0)); | |
| 778 EXPECT_FALSE(m1.Matches(-1.0)); | |
| 779 } | |
| 780 | |
| 781 // Tests that Gt(v) describes itself properly. | |
| 782 TEST(GtTest, CanDescribeSelf) { | |
| 783 Matcher<int> m = Gt(5); | |
| 784 EXPECT_EQ("is > 5", Describe(m)); | |
| 785 } | |
| 786 | |
| 787 // Tests that Le(v) matches anything <= v. | |
| 788 TEST(LeTest, ImplementsLessThanOrEqual) { | |
| 789 Matcher<char> m1 = Le('b'); | |
| 790 EXPECT_TRUE(m1.Matches('a')); | |
| 791 EXPECT_TRUE(m1.Matches('b')); | |
| 792 EXPECT_FALSE(m1.Matches('c')); | |
| 793 } | |
| 794 | |
| 795 // Tests that Le(v) describes itself properly. | |
| 796 TEST(LeTest, CanDescribeSelf) { | |
| 797 Matcher<int> m = Le(5); | |
| 798 EXPECT_EQ("is <= 5", Describe(m)); | |
| 799 } | |
| 800 | |
| 801 // Tests that Lt(v) matches anything < v. | |
| 802 TEST(LtTest, ImplementsLessThan) { | |
| 803 Matcher<const string&> m1 = Lt("Hello"); | |
| 804 EXPECT_TRUE(m1.Matches("Abc")); | |
| 805 EXPECT_FALSE(m1.Matches("Hello")); | |
| 806 EXPECT_FALSE(m1.Matches("Hello, world!")); | |
| 807 } | |
| 808 | |
| 809 // Tests that Lt(v) describes itself properly. | |
| 810 TEST(LtTest, CanDescribeSelf) { | |
| 811 Matcher<int> m = Lt(5); | |
| 812 EXPECT_EQ("is < 5", Describe(m)); | |
| 813 } | |
| 814 | |
| 815 // Tests that Ne(v) matches anything != v. | |
| 816 TEST(NeTest, ImplementsNotEqual) { | |
| 817 Matcher<int> m1 = Ne(0); | |
| 818 EXPECT_TRUE(m1.Matches(1)); | |
| 819 EXPECT_TRUE(m1.Matches(-1)); | |
| 820 EXPECT_FALSE(m1.Matches(0)); | |
| 821 } | |
| 822 | |
| 823 // Tests that Ne(v) describes itself properly. | |
| 824 TEST(NeTest, CanDescribeSelf) { | |
| 825 Matcher<int> m = Ne(5); | |
| 826 EXPECT_EQ("isn't equal to 5", Describe(m)); | |
| 827 } | |
| 828 | |
| 829 // Tests that IsNull() matches any NULL pointer of any type. | |
| 830 TEST(IsNullTest, MatchesNullPointer) { | |
| 831 Matcher<int*> m1 = IsNull(); | |
| 832 int* p1 = NULL; | |
| 833 int n = 0; | |
| 834 EXPECT_TRUE(m1.Matches(p1)); | |
| 835 EXPECT_FALSE(m1.Matches(&n)); | |
| 836 | |
| 837 Matcher<const char*> m2 = IsNull(); | |
| 838 const char* p2 = NULL; | |
| 839 EXPECT_TRUE(m2.Matches(p2)); | |
| 840 EXPECT_FALSE(m2.Matches("hi")); | |
| 841 | |
| 842 #if !GTEST_OS_SYMBIAN | |
| 843 // Nokia's Symbian compiler generates: | |
| 844 // gmock-matchers.h: ambiguous access to overloaded function | |
| 845 // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(void *)' | |
| 846 // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(const testing:: | |
| 847 // MatcherInterface<void *> *)' | |
| 848 // gmock-matchers.h: (point of instantiation: 'testing:: | |
| 849 // gmock_matchers_test::IsNullTest_MatchesNullPointer_Test::TestBody()') | |
| 850 // gmock-matchers.h: (instantiating: 'testing::PolymorphicMatc | |
| 851 Matcher<void*> m3 = IsNull(); | |
| 852 void* p3 = NULL; | |
| 853 EXPECT_TRUE(m3.Matches(p3)); | |
| 854 EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef))); | |
| 855 #endif | |
| 856 } | |
| 857 | |
| 858 TEST(IsNullTest, LinkedPtr) { | |
| 859 const Matcher<linked_ptr<int> > m = IsNull(); | |
| 860 const linked_ptr<int> null_p; | |
| 861 const linked_ptr<int> non_null_p(new int); | |
| 862 | |
| 863 EXPECT_TRUE(m.Matches(null_p)); | |
| 864 EXPECT_FALSE(m.Matches(non_null_p)); | |
| 865 } | |
| 866 | |
| 867 TEST(IsNullTest, ReferenceToConstLinkedPtr) { | |
| 868 const Matcher<const linked_ptr<double>&> m = IsNull(); | |
| 869 const linked_ptr<double> null_p; | |
| 870 const linked_ptr<double> non_null_p(new double); | |
| 871 | |
| 872 EXPECT_TRUE(m.Matches(null_p)); | |
| 873 EXPECT_FALSE(m.Matches(non_null_p)); | |
| 874 } | |
| 875 | |
| 876 TEST(IsNullTest, ReferenceToConstScopedPtr) { | |
| 877 const Matcher<const scoped_ptr<double>&> m = IsNull(); | |
| 878 const scoped_ptr<double> null_p; | |
| 879 const scoped_ptr<double> non_null_p(new double); | |
| 880 | |
| 881 EXPECT_TRUE(m.Matches(null_p)); | |
| 882 EXPECT_FALSE(m.Matches(non_null_p)); | |
| 883 } | |
| 884 | |
| 885 // Tests that IsNull() describes itself properly. | |
| 886 TEST(IsNullTest, CanDescribeSelf) { | |
| 887 Matcher<int*> m = IsNull(); | |
| 888 EXPECT_EQ("is NULL", Describe(m)); | |
| 889 EXPECT_EQ("isn't NULL", DescribeNegation(m)); | |
| 890 } | |
| 891 | |
| 892 // Tests that NotNull() matches any non-NULL pointer of any type. | |
| 893 TEST(NotNullTest, MatchesNonNullPointer) { | |
| 894 Matcher<int*> m1 = NotNull(); | |
| 895 int* p1 = NULL; | |
| 896 int n = 0; | |
| 897 EXPECT_FALSE(m1.Matches(p1)); | |
| 898 EXPECT_TRUE(m1.Matches(&n)); | |
| 899 | |
| 900 Matcher<const char*> m2 = NotNull(); | |
| 901 const char* p2 = NULL; | |
| 902 EXPECT_FALSE(m2.Matches(p2)); | |
| 903 EXPECT_TRUE(m2.Matches("hi")); | |
| 904 } | |
| 905 | |
| 906 TEST(NotNullTest, LinkedPtr) { | |
| 907 const Matcher<linked_ptr<int> > m = NotNull(); | |
| 908 const linked_ptr<int> null_p; | |
| 909 const linked_ptr<int> non_null_p(new int); | |
| 910 | |
| 911 EXPECT_FALSE(m.Matches(null_p)); | |
| 912 EXPECT_TRUE(m.Matches(non_null_p)); | |
| 913 } | |
| 914 | |
| 915 TEST(NotNullTest, ReferenceToConstLinkedPtr) { | |
| 916 const Matcher<const linked_ptr<double>&> m = NotNull(); | |
| 917 const linked_ptr<double> null_p; | |
| 918 const linked_ptr<double> non_null_p(new double); | |
| 919 | |
| 920 EXPECT_FALSE(m.Matches(null_p)); | |
| 921 EXPECT_TRUE(m.Matches(non_null_p)); | |
| 922 } | |
| 923 | |
| 924 TEST(NotNullTest, ReferenceToConstScopedPtr) { | |
| 925 const Matcher<const scoped_ptr<double>&> m = NotNull(); | |
| 926 const scoped_ptr<double> null_p; | |
| 927 const scoped_ptr<double> non_null_p(new double); | |
| 928 | |
| 929 EXPECT_FALSE(m.Matches(null_p)); | |
| 930 EXPECT_TRUE(m.Matches(non_null_p)); | |
| 931 } | |
| 932 | |
| 933 // Tests that NotNull() describes itself properly. | |
| 934 TEST(NotNullTest, CanDescribeSelf) { | |
| 935 Matcher<int*> m = NotNull(); | |
| 936 EXPECT_EQ("isn't NULL", Describe(m)); | |
| 937 } | |
| 938 | |
| 939 // Tests that Ref(variable) matches an argument that references | |
| 940 // 'variable'. | |
| 941 TEST(RefTest, MatchesSameVariable) { | |
| 942 int a = 0; | |
| 943 int b = 0; | |
| 944 Matcher<int&> m = Ref(a); | |
| 945 EXPECT_TRUE(m.Matches(a)); | |
| 946 EXPECT_FALSE(m.Matches(b)); | |
| 947 } | |
| 948 | |
| 949 // Tests that Ref(variable) describes itself properly. | |
| 950 TEST(RefTest, CanDescribeSelf) { | |
| 951 int n = 5; | |
| 952 Matcher<int&> m = Ref(n); | |
| 953 stringstream ss; | |
| 954 ss << "references the variable @" << &n << " 5"; | |
| 955 EXPECT_EQ(string(ss.str()), Describe(m)); | |
| 956 } | |
| 957 | |
| 958 // Test that Ref(non_const_varialbe) can be used as a matcher for a | |
| 959 // const reference. | |
| 960 TEST(RefTest, CanBeUsedAsMatcherForConstReference) { | |
| 961 int a = 0; | |
| 962 int b = 0; | |
| 963 Matcher<const int&> m = Ref(a); | |
| 964 EXPECT_TRUE(m.Matches(a)); | |
| 965 EXPECT_FALSE(m.Matches(b)); | |
| 966 } | |
| 967 | |
| 968 // Tests that Ref(variable) is covariant, i.e. Ref(derived) can be | |
| 969 // used wherever Ref(base) can be used (Ref(derived) is a sub-type | |
| 970 // of Ref(base), but not vice versa. | |
| 971 | |
| 972 TEST(RefTest, IsCovariant) { | |
| 973 Base base, base2; | |
| 974 Derived derived; | |
| 975 Matcher<const Base&> m1 = Ref(base); | |
| 976 EXPECT_TRUE(m1.Matches(base)); | |
| 977 EXPECT_FALSE(m1.Matches(base2)); | |
| 978 EXPECT_FALSE(m1.Matches(derived)); | |
| 979 | |
| 980 m1 = Ref(derived); | |
| 981 EXPECT_TRUE(m1.Matches(derived)); | |
| 982 EXPECT_FALSE(m1.Matches(base)); | |
| 983 EXPECT_FALSE(m1.Matches(base2)); | |
| 984 } | |
| 985 | |
| 986 TEST(RefTest, ExplainsResult) { | |
| 987 int n = 0; | |
| 988 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), n), | |
| 989 StartsWith("which is located @")); | |
| 990 | |
| 991 int m = 0; | |
| 992 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), m), | |
| 993 StartsWith("which is located @")); | |
| 994 } | |
| 995 | |
| 996 // Tests string comparison matchers. | |
| 997 | |
| 998 TEST(StrEqTest, MatchesEqualString) { | |
| 999 Matcher<const char*> m = StrEq(string("Hello")); | |
| 1000 EXPECT_TRUE(m.Matches("Hello")); | |
| 1001 EXPECT_FALSE(m.Matches("hello")); | |
| 1002 EXPECT_FALSE(m.Matches(NULL)); | |
| 1003 | |
| 1004 Matcher<const string&> m2 = StrEq("Hello"); | |
| 1005 EXPECT_TRUE(m2.Matches("Hello")); | |
| 1006 EXPECT_FALSE(m2.Matches("Hi")); | |
| 1007 } | |
| 1008 | |
| 1009 TEST(StrEqTest, CanDescribeSelf) { | |
| 1010 Matcher<string> m = StrEq("Hi-\'\"\?\\\a\b\f\n\r\t\v\xD3"); | |
| 1011 EXPECT_EQ("is equal to \"Hi-\'\\\"\\?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"", | |
| 1012 Describe(m)); | |
| 1013 | |
| 1014 string str("01204500800"); | |
| 1015 str[3] = '\0'; | |
| 1016 Matcher<string> m2 = StrEq(str); | |
| 1017 EXPECT_EQ("is equal to \"012\\04500800\"", Describe(m2)); | |
| 1018 str[0] = str[6] = str[7] = str[9] = str[10] = '\0'; | |
| 1019 Matcher<string> m3 = StrEq(str); | |
| 1020 EXPECT_EQ("is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3)); | |
| 1021 } | |
| 1022 | |
| 1023 TEST(StrNeTest, MatchesUnequalString) { | |
| 1024 Matcher<const char*> m = StrNe("Hello"); | |
| 1025 EXPECT_TRUE(m.Matches("")); | |
| 1026 EXPECT_TRUE(m.Matches(NULL)); | |
| 1027 EXPECT_FALSE(m.Matches("Hello")); | |
| 1028 | |
| 1029 Matcher<string> m2 = StrNe(string("Hello")); | |
| 1030 EXPECT_TRUE(m2.Matches("hello")); | |
| 1031 EXPECT_FALSE(m2.Matches("Hello")); | |
| 1032 } | |
| 1033 | |
| 1034 TEST(StrNeTest, CanDescribeSelf) { | |
| 1035 Matcher<const char*> m = StrNe("Hi"); | |
| 1036 EXPECT_EQ("isn't equal to \"Hi\"", Describe(m)); | |
| 1037 } | |
| 1038 | |
| 1039 TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) { | |
| 1040 Matcher<const char*> m = StrCaseEq(string("Hello")); | |
| 1041 EXPECT_TRUE(m.Matches("Hello")); | |
| 1042 EXPECT_TRUE(m.Matches("hello")); | |
| 1043 EXPECT_FALSE(m.Matches("Hi")); | |
| 1044 EXPECT_FALSE(m.Matches(NULL)); | |
| 1045 | |
| 1046 Matcher<const string&> m2 = StrCaseEq("Hello"); | |
| 1047 EXPECT_TRUE(m2.Matches("hello")); | |
| 1048 EXPECT_FALSE(m2.Matches("Hi")); | |
| 1049 } | |
| 1050 | |
| 1051 TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) { | |
| 1052 string str1("oabocdooeoo"); | |
| 1053 string str2("OABOCDOOEOO"); | |
| 1054 Matcher<const string&> m0 = StrCaseEq(str1); | |
| 1055 EXPECT_FALSE(m0.Matches(str2 + string(1, '\0'))); | |
| 1056 | |
| 1057 str1[3] = str2[3] = '\0'; | |
| 1058 Matcher<const string&> m1 = StrCaseEq(str1); | |
| 1059 EXPECT_TRUE(m1.Matches(str2)); | |
| 1060 | |
| 1061 str1[0] = str1[6] = str1[7] = str1[10] = '\0'; | |
| 1062 str2[0] = str2[6] = str2[7] = str2[10] = '\0'; | |
| 1063 Matcher<const string&> m2 = StrCaseEq(str1); | |
| 1064 str1[9] = str2[9] = '\0'; | |
| 1065 EXPECT_FALSE(m2.Matches(str2)); | |
| 1066 | |
| 1067 Matcher<const string&> m3 = StrCaseEq(str1); | |
| 1068 EXPECT_TRUE(m3.Matches(str2)); | |
| 1069 | |
| 1070 EXPECT_FALSE(m3.Matches(str2 + "x")); | |
| 1071 str2.append(1, '\0'); | |
| 1072 EXPECT_FALSE(m3.Matches(str2)); | |
| 1073 EXPECT_FALSE(m3.Matches(string(str2, 0, 9))); | |
| 1074 } | |
| 1075 | |
| 1076 TEST(StrCaseEqTest, CanDescribeSelf) { | |
| 1077 Matcher<string> m = StrCaseEq("Hi"); | |
| 1078 EXPECT_EQ("is equal to (ignoring case) \"Hi\"", Describe(m)); | |
| 1079 } | |
| 1080 | |
| 1081 TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) { | |
| 1082 Matcher<const char*> m = StrCaseNe("Hello"); | |
| 1083 EXPECT_TRUE(m.Matches("Hi")); | |
| 1084 EXPECT_TRUE(m.Matches(NULL)); | |
| 1085 EXPECT_FALSE(m.Matches("Hello")); | |
| 1086 EXPECT_FALSE(m.Matches("hello")); | |
| 1087 | |
| 1088 Matcher<string> m2 = StrCaseNe(string("Hello")); | |
| 1089 EXPECT_TRUE(m2.Matches("")); | |
| 1090 EXPECT_FALSE(m2.Matches("Hello")); | |
| 1091 } | |
| 1092 | |
| 1093 TEST(StrCaseNeTest, CanDescribeSelf) { | |
| 1094 Matcher<const char*> m = StrCaseNe("Hi"); | |
| 1095 EXPECT_EQ("isn't equal to (ignoring case) \"Hi\"", Describe(m)); | |
| 1096 } | |
| 1097 | |
| 1098 // Tests that HasSubstr() works for matching string-typed values. | |
| 1099 TEST(HasSubstrTest, WorksForStringClasses) { | |
| 1100 const Matcher<string> m1 = HasSubstr("foo"); | |
| 1101 EXPECT_TRUE(m1.Matches(string("I love food."))); | |
| 1102 EXPECT_FALSE(m1.Matches(string("tofo"))); | |
| 1103 | |
| 1104 const Matcher<const std::string&> m2 = HasSubstr("foo"); | |
| 1105 EXPECT_TRUE(m2.Matches(std::string("I love food."))); | |
| 1106 EXPECT_FALSE(m2.Matches(std::string("tofo"))); | |
| 1107 } | |
| 1108 | |
| 1109 // Tests that HasSubstr() works for matching C-string-typed values. | |
| 1110 TEST(HasSubstrTest, WorksForCStrings) { | |
| 1111 const Matcher<char*> m1 = HasSubstr("foo"); | |
| 1112 EXPECT_TRUE(m1.Matches(const_cast<char*>("I love food."))); | |
| 1113 EXPECT_FALSE(m1.Matches(const_cast<char*>("tofo"))); | |
| 1114 EXPECT_FALSE(m1.Matches(NULL)); | |
| 1115 | |
| 1116 const Matcher<const char*> m2 = HasSubstr("foo"); | |
| 1117 EXPECT_TRUE(m2.Matches("I love food.")); | |
| 1118 EXPECT_FALSE(m2.Matches("tofo")); | |
| 1119 EXPECT_FALSE(m2.Matches(NULL)); | |
| 1120 } | |
| 1121 | |
| 1122 // Tests that HasSubstr(s) describes itself properly. | |
| 1123 TEST(HasSubstrTest, CanDescribeSelf) { | |
| 1124 Matcher<string> m = HasSubstr("foo\n\""); | |
| 1125 EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m)); | |
| 1126 } | |
| 1127 | |
| 1128 TEST(KeyTest, CanDescribeSelf) { | |
| 1129 Matcher<const pair<std::string, int>&> m = Key("foo"); | |
| 1130 EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m)); | |
| 1131 EXPECT_EQ("doesn't have a key that is equal to \"foo\"", DescribeNegation(m)); | |
| 1132 } | |
| 1133 | |
| 1134 TEST(KeyTest, ExplainsResult) { | |
| 1135 Matcher<pair<int, bool> > m = Key(GreaterThan(10)); | |
| 1136 EXPECT_EQ("whose first field is a value which is 5 less than 10", | |
| 1137 Explain(m, make_pair(5, true))); | |
| 1138 EXPECT_EQ("whose first field is a value which is 5 more than 10", | |
| 1139 Explain(m, make_pair(15, true))); | |
| 1140 } | |
| 1141 | |
| 1142 TEST(KeyTest, MatchesCorrectly) { | |
| 1143 pair<int, std::string> p(25, "foo"); | |
| 1144 EXPECT_THAT(p, Key(25)); | |
| 1145 EXPECT_THAT(p, Not(Key(42))); | |
| 1146 EXPECT_THAT(p, Key(Ge(20))); | |
| 1147 EXPECT_THAT(p, Not(Key(Lt(25)))); | |
| 1148 } | |
| 1149 | |
| 1150 TEST(KeyTest, SafelyCastsInnerMatcher) { | |
| 1151 Matcher<int> is_positive = Gt(0); | |
| 1152 Matcher<int> is_negative = Lt(0); | |
| 1153 pair<char, bool> p('a', true); | |
| 1154 EXPECT_THAT(p, Key(is_positive)); | |
| 1155 EXPECT_THAT(p, Not(Key(is_negative))); | |
| 1156 } | |
| 1157 | |
| 1158 TEST(KeyTest, InsideContainsUsingMap) { | |
| 1159 map<int, char> container; | |
| 1160 container.insert(make_pair(1, 'a')); | |
| 1161 container.insert(make_pair(2, 'b')); | |
| 1162 container.insert(make_pair(4, 'c')); | |
| 1163 EXPECT_THAT(container, Contains(Key(1))); | |
| 1164 EXPECT_THAT(container, Not(Contains(Key(3)))); | |
| 1165 } | |
| 1166 | |
| 1167 TEST(KeyTest, InsideContainsUsingMultimap) { | |
| 1168 multimap<int, char> container; | |
| 1169 container.insert(make_pair(1, 'a')); | |
| 1170 container.insert(make_pair(2, 'b')); | |
| 1171 container.insert(make_pair(4, 'c')); | |
| 1172 | |
| 1173 EXPECT_THAT(container, Not(Contains(Key(25)))); | |
| 1174 container.insert(make_pair(25, 'd')); | |
| 1175 EXPECT_THAT(container, Contains(Key(25))); | |
| 1176 container.insert(make_pair(25, 'e')); | |
| 1177 EXPECT_THAT(container, Contains(Key(25))); | |
| 1178 | |
| 1179 EXPECT_THAT(container, Contains(Key(1))); | |
| 1180 EXPECT_THAT(container, Not(Contains(Key(3)))); | |
| 1181 } | |
| 1182 | |
| 1183 TEST(PairTest, Typing) { | |
| 1184 // Test verifies the following type conversions can be compiled. | |
| 1185 Matcher<const pair<const char*, int>&> m1 = Pair("foo", 42); | |
| 1186 Matcher<const pair<const char*, int> > m2 = Pair("foo", 42); | |
| 1187 Matcher<pair<const char*, int> > m3 = Pair("foo", 42); | |
| 1188 | |
| 1189 Matcher<pair<int, const std::string> > m4 = Pair(25, "42"); | |
| 1190 Matcher<pair<const std::string, int> > m5 = Pair("25", 42); | |
| 1191 } | |
| 1192 | |
| 1193 TEST(PairTest, CanDescribeSelf) { | |
| 1194 Matcher<const pair<std::string, int>&> m1 = Pair("foo", 42); | |
| 1195 EXPECT_EQ("has a first field that is equal to \"foo\"" | |
| 1196 ", and has a second field that is equal to 42", | |
| 1197 Describe(m1)); | |
| 1198 EXPECT_EQ("has a first field that isn't equal to \"foo\"" | |
| 1199 ", or has a second field that isn't equal to 42", | |
| 1200 DescribeNegation(m1)); | |
| 1201 // Double and triple negation (1 or 2 times not and description of negation). | |
| 1202 Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42)); | |
| 1203 EXPECT_EQ("has a first field that isn't equal to 13" | |
| 1204 ", and has a second field that is equal to 42", | |
| 1205 DescribeNegation(m2)); | |
| 1206 } | |
| 1207 | |
| 1208 TEST(PairTest, CanExplainMatchResultTo) { | |
| 1209 // If neither field matches, Pair() should explain about the first | |
| 1210 // field. | |
| 1211 const Matcher<pair<int, int> > m = Pair(GreaterThan(0), GreaterThan(0)); | |
| 1212 EXPECT_EQ("whose first field does not match, which is 1 less than 0", | |
| 1213 Explain(m, make_pair(-1, -2))); | |
| 1214 | |
| 1215 // If the first field matches but the second doesn't, Pair() should | |
| 1216 // explain about the second field. | |
| 1217 EXPECT_EQ("whose second field does not match, which is 2 less than 0", | |
| 1218 Explain(m, make_pair(1, -2))); | |
| 1219 | |
| 1220 // If the first field doesn't match but the second does, Pair() | |
| 1221 // should explain about the first field. | |
| 1222 EXPECT_EQ("whose first field does not match, which is 1 less than 0", | |
| 1223 Explain(m, make_pair(-1, 2))); | |
| 1224 | |
| 1225 // If both fields match, Pair() should explain about them both. | |
| 1226 EXPECT_EQ("whose both fields match, where the first field is a value " | |
| 1227 "which is 1 more than 0, and the second field is a value " | |
| 1228 "which is 2 more than 0", | |
| 1229 Explain(m, make_pair(1, 2))); | |
| 1230 | |
| 1231 // If only the first match has an explanation, only this explanation should | |
| 1232 // be printed. | |
| 1233 const Matcher<pair<int, int> > explain_first = Pair(GreaterThan(0), 0); | |
| 1234 EXPECT_EQ("whose both fields match, where the first field is a value " | |
| 1235 "which is 1 more than 0", | |
| 1236 Explain(explain_first, make_pair(1, 0))); | |
| 1237 | |
| 1238 // If only the second match has an explanation, only this explanation should | |
| 1239 // be printed. | |
| 1240 const Matcher<pair<int, int> > explain_second = Pair(0, GreaterThan(0)); | |
| 1241 EXPECT_EQ("whose both fields match, where the second field is a value " | |
| 1242 "which is 1 more than 0", | |
| 1243 Explain(explain_second, make_pair(0, 1))); | |
| 1244 } | |
| 1245 | |
| 1246 TEST(PairTest, MatchesCorrectly) { | |
| 1247 pair<int, std::string> p(25, "foo"); | |
| 1248 | |
| 1249 // Both fields match. | |
| 1250 EXPECT_THAT(p, Pair(25, "foo")); | |
| 1251 EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o"))); | |
| 1252 | |
| 1253 // 'first' doesnt' match, but 'second' matches. | |
| 1254 EXPECT_THAT(p, Not(Pair(42, "foo"))); | |
| 1255 EXPECT_THAT(p, Not(Pair(Lt(25), "foo"))); | |
| 1256 | |
| 1257 // 'first' matches, but 'second' doesn't match. | |
| 1258 EXPECT_THAT(p, Not(Pair(25, "bar"))); | |
| 1259 EXPECT_THAT(p, Not(Pair(25, Not("foo")))); | |
| 1260 | |
| 1261 // Neither field matches. | |
| 1262 EXPECT_THAT(p, Not(Pair(13, "bar"))); | |
| 1263 EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a")))); | |
| 1264 } | |
| 1265 | |
| 1266 TEST(PairTest, SafelyCastsInnerMatchers) { | |
| 1267 Matcher<int> is_positive = Gt(0); | |
| 1268 Matcher<int> is_negative = Lt(0); | |
| 1269 pair<char, bool> p('a', true); | |
| 1270 EXPECT_THAT(p, Pair(is_positive, _)); | |
| 1271 EXPECT_THAT(p, Not(Pair(is_negative, _))); | |
| 1272 EXPECT_THAT(p, Pair(_, is_positive)); | |
| 1273 EXPECT_THAT(p, Not(Pair(_, is_negative))); | |
| 1274 } | |
| 1275 | |
| 1276 TEST(PairTest, InsideContainsUsingMap) { | |
| 1277 map<int, char> container; | |
| 1278 container.insert(make_pair(1, 'a')); | |
| 1279 container.insert(make_pair(2, 'b')); | |
| 1280 container.insert(make_pair(4, 'c')); | |
| 1281 EXPECT_THAT(container, Contains(Pair(1, 'a'))); | |
| 1282 EXPECT_THAT(container, Contains(Pair(1, _))); | |
| 1283 EXPECT_THAT(container, Contains(Pair(_, 'a'))); | |
| 1284 EXPECT_THAT(container, Not(Contains(Pair(3, _)))); | |
| 1285 } | |
| 1286 | |
| 1287 // Tests StartsWith(s). | |
| 1288 | |
| 1289 TEST(StartsWithTest, MatchesStringWithGivenPrefix) { | |
| 1290 const Matcher<const char*> m1 = StartsWith(string("")); | |
| 1291 EXPECT_TRUE(m1.Matches("Hi")); | |
| 1292 EXPECT_TRUE(m1.Matches("")); | |
| 1293 EXPECT_FALSE(m1.Matches(NULL)); | |
| 1294 | |
| 1295 const Matcher<const string&> m2 = StartsWith("Hi"); | |
| 1296 EXPECT_TRUE(m2.Matches("Hi")); | |
| 1297 EXPECT_TRUE(m2.Matches("Hi Hi!")); | |
| 1298 EXPECT_TRUE(m2.Matches("High")); | |
| 1299 EXPECT_FALSE(m2.Matches("H")); | |
| 1300 EXPECT_FALSE(m2.Matches(" Hi")); | |
| 1301 } | |
| 1302 | |
| 1303 TEST(StartsWithTest, CanDescribeSelf) { | |
| 1304 Matcher<const std::string> m = StartsWith("Hi"); | |
| 1305 EXPECT_EQ("starts with \"Hi\"", Describe(m)); | |
| 1306 } | |
| 1307 | |
| 1308 // Tests EndsWith(s). | |
| 1309 | |
| 1310 TEST(EndsWithTest, MatchesStringWithGivenSuffix) { | |
| 1311 const Matcher<const char*> m1 = EndsWith(""); | |
| 1312 EXPECT_TRUE(m1.Matches("Hi")); | |
| 1313 EXPECT_TRUE(m1.Matches("")); | |
| 1314 EXPECT_FALSE(m1.Matches(NULL)); | |
| 1315 | |
| 1316 const Matcher<const string&> m2 = EndsWith(string("Hi")); | |
| 1317 EXPECT_TRUE(m2.Matches("Hi")); | |
| 1318 EXPECT_TRUE(m2.Matches("Wow Hi Hi")); | |
| 1319 EXPECT_TRUE(m2.Matches("Super Hi")); | |
| 1320 EXPECT_FALSE(m2.Matches("i")); | |
| 1321 EXPECT_FALSE(m2.Matches("Hi ")); | |
| 1322 } | |
| 1323 | |
| 1324 TEST(EndsWithTest, CanDescribeSelf) { | |
| 1325 Matcher<const std::string> m = EndsWith("Hi"); | |
| 1326 EXPECT_EQ("ends with \"Hi\"", Describe(m)); | |
| 1327 } | |
| 1328 | |
| 1329 // Tests MatchesRegex(). | |
| 1330 | |
| 1331 TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) { | |
| 1332 const Matcher<const char*> m1 = MatchesRegex("a.*z"); | |
| 1333 EXPECT_TRUE(m1.Matches("az")); | |
| 1334 EXPECT_TRUE(m1.Matches("abcz")); | |
| 1335 EXPECT_FALSE(m1.Matches(NULL)); | |
| 1336 | |
| 1337 const Matcher<const string&> m2 = MatchesRegex(new RE("a.*z")); | |
| 1338 EXPECT_TRUE(m2.Matches("azbz")); | |
| 1339 EXPECT_FALSE(m2.Matches("az1")); | |
| 1340 EXPECT_FALSE(m2.Matches("1az")); | |
| 1341 } | |
| 1342 | |
| 1343 TEST(MatchesRegexTest, CanDescribeSelf) { | |
| 1344 Matcher<const std::string> m1 = MatchesRegex(string("Hi.*")); | |
| 1345 EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1)); | |
| 1346 | |
| 1347 Matcher<const char*> m2 = MatchesRegex(new RE("a.*")); | |
| 1348 EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2)); | |
| 1349 } | |
| 1350 | |
| 1351 // Tests ContainsRegex(). | |
| 1352 | |
| 1353 TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) { | |
| 1354 const Matcher<const char*> m1 = ContainsRegex(string("a.*z")); | |
| 1355 EXPECT_TRUE(m1.Matches("az")); | |
| 1356 EXPECT_TRUE(m1.Matches("0abcz1")); | |
| 1357 EXPECT_FALSE(m1.Matches(NULL)); | |
| 1358 | |
| 1359 const Matcher<const string&> m2 = ContainsRegex(new RE("a.*z")); | |
| 1360 EXPECT_TRUE(m2.Matches("azbz")); | |
| 1361 EXPECT_TRUE(m2.Matches("az1")); | |
| 1362 EXPECT_FALSE(m2.Matches("1a")); | |
| 1363 } | |
| 1364 | |
| 1365 TEST(ContainsRegexTest, CanDescribeSelf) { | |
| 1366 Matcher<const std::string> m1 = ContainsRegex("Hi.*"); | |
| 1367 EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1)); | |
| 1368 | |
| 1369 Matcher<const char*> m2 = ContainsRegex(new RE("a.*")); | |
| 1370 EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2)); | |
| 1371 } | |
| 1372 | |
| 1373 // Tests for wide strings. | |
| 1374 #if GTEST_HAS_STD_WSTRING | |
| 1375 TEST(StdWideStrEqTest, MatchesEqual) { | |
| 1376 Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello")); | |
| 1377 EXPECT_TRUE(m.Matches(L"Hello")); | |
| 1378 EXPECT_FALSE(m.Matches(L"hello")); | |
| 1379 EXPECT_FALSE(m.Matches(NULL)); | |
| 1380 | |
| 1381 Matcher<const ::std::wstring&> m2 = StrEq(L"Hello"); | |
| 1382 EXPECT_TRUE(m2.Matches(L"Hello")); | |
| 1383 EXPECT_FALSE(m2.Matches(L"Hi")); | |
| 1384 | |
| 1385 Matcher<const ::std::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D"); | |
| 1386 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D")); | |
| 1387 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E")); | |
| 1388 | |
| 1389 ::std::wstring str(L"01204500800"); | |
| 1390 str[3] = L'\0'; | |
| 1391 Matcher<const ::std::wstring&> m4 = StrEq(str); | |
| 1392 EXPECT_TRUE(m4.Matches(str)); | |
| 1393 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0'; | |
| 1394 Matcher<const ::std::wstring&> m5 = StrEq(str); | |
| 1395 EXPECT_TRUE(m5.Matches(str)); | |
| 1396 } | |
| 1397 | |
| 1398 TEST(StdWideStrEqTest, CanDescribeSelf) { | |
| 1399 Matcher< ::std::wstring> m = StrEq(L"Hi-\'\"\?\\\a\b\f\n\r\t\v"); | |
| 1400 EXPECT_EQ("is equal to L\"Hi-\'\\\"\\?\\\\\\a\\b\\f\\n\\r\\t\\v\"", | |
| 1401 Describe(m)); | |
| 1402 | |
| 1403 Matcher< ::std::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D"); | |
| 1404 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"", | |
| 1405 Describe(m2)); | |
| 1406 | |
| 1407 ::std::wstring str(L"01204500800"); | |
| 1408 str[3] = L'\0'; | |
| 1409 Matcher<const ::std::wstring&> m4 = StrEq(str); | |
| 1410 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4)); | |
| 1411 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0'; | |
| 1412 Matcher<const ::std::wstring&> m5 = StrEq(str); | |
| 1413 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5)); | |
| 1414 } | |
| 1415 | |
| 1416 TEST(StdWideStrNeTest, MatchesUnequalString) { | |
| 1417 Matcher<const wchar_t*> m = StrNe(L"Hello"); | |
| 1418 EXPECT_TRUE(m.Matches(L"")); | |
| 1419 EXPECT_TRUE(m.Matches(NULL)); | |
| 1420 EXPECT_FALSE(m.Matches(L"Hello")); | |
| 1421 | |
| 1422 Matcher< ::std::wstring> m2 = StrNe(::std::wstring(L"Hello")); | |
| 1423 EXPECT_TRUE(m2.Matches(L"hello")); | |
| 1424 EXPECT_FALSE(m2.Matches(L"Hello")); | |
| 1425 } | |
| 1426 | |
| 1427 TEST(StdWideStrNeTest, CanDescribeSelf) { | |
| 1428 Matcher<const wchar_t*> m = StrNe(L"Hi"); | |
| 1429 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m)); | |
| 1430 } | |
| 1431 | |
| 1432 TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) { | |
| 1433 Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello")); | |
| 1434 EXPECT_TRUE(m.Matches(L"Hello")); | |
| 1435 EXPECT_TRUE(m.Matches(L"hello")); | |
| 1436 EXPECT_FALSE(m.Matches(L"Hi")); | |
| 1437 EXPECT_FALSE(m.Matches(NULL)); | |
| 1438 | |
| 1439 Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello"); | |
| 1440 EXPECT_TRUE(m2.Matches(L"hello")); | |
| 1441 EXPECT_FALSE(m2.Matches(L"Hi")); | |
| 1442 } | |
| 1443 | |
| 1444 TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) { | |
| 1445 ::std::wstring str1(L"oabocdooeoo"); | |
| 1446 ::std::wstring str2(L"OABOCDOOEOO"); | |
| 1447 Matcher<const ::std::wstring&> m0 = StrCaseEq(str1); | |
| 1448 EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L'\0'))); | |
| 1449 | |
| 1450 str1[3] = str2[3] = L'\0'; | |
| 1451 Matcher<const ::std::wstring&> m1 = StrCaseEq(str1); | |
| 1452 EXPECT_TRUE(m1.Matches(str2)); | |
| 1453 | |
| 1454 str1[0] = str1[6] = str1[7] = str1[10] = L'\0'; | |
| 1455 str2[0] = str2[6] = str2[7] = str2[10] = L'\0'; | |
| 1456 Matcher<const ::std::wstring&> m2 = StrCaseEq(str1); | |
| 1457 str1[9] = str2[9] = L'\0'; | |
| 1458 EXPECT_FALSE(m2.Matches(str2)); | |
| 1459 | |
| 1460 Matcher<const ::std::wstring&> m3 = StrCaseEq(str1); | |
| 1461 EXPECT_TRUE(m3.Matches(str2)); | |
| 1462 | |
| 1463 EXPECT_FALSE(m3.Matches(str2 + L"x")); | |
| 1464 str2.append(1, L'\0'); | |
| 1465 EXPECT_FALSE(m3.Matches(str2)); | |
| 1466 EXPECT_FALSE(m3.Matches(::std::wstring(str2, 0, 9))); | |
| 1467 } | |
| 1468 | |
| 1469 TEST(StdWideStrCaseEqTest, CanDescribeSelf) { | |
| 1470 Matcher< ::std::wstring> m = StrCaseEq(L"Hi"); | |
| 1471 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m)); | |
| 1472 } | |
| 1473 | |
| 1474 TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) { | |
| 1475 Matcher<const wchar_t*> m = StrCaseNe(L"Hello"); | |
| 1476 EXPECT_TRUE(m.Matches(L"Hi")); | |
| 1477 EXPECT_TRUE(m.Matches(NULL)); | |
| 1478 EXPECT_FALSE(m.Matches(L"Hello")); | |
| 1479 EXPECT_FALSE(m.Matches(L"hello")); | |
| 1480 | |
| 1481 Matcher< ::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello")); | |
| 1482 EXPECT_TRUE(m2.Matches(L"")); | |
| 1483 EXPECT_FALSE(m2.Matches(L"Hello")); | |
| 1484 } | |
| 1485 | |
| 1486 TEST(StdWideStrCaseNeTest, CanDescribeSelf) { | |
| 1487 Matcher<const wchar_t*> m = StrCaseNe(L"Hi"); | |
| 1488 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m)); | |
| 1489 } | |
| 1490 | |
| 1491 // Tests that HasSubstr() works for matching wstring-typed values. | |
| 1492 TEST(StdWideHasSubstrTest, WorksForStringClasses) { | |
| 1493 const Matcher< ::std::wstring> m1 = HasSubstr(L"foo"); | |
| 1494 EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food."))); | |
| 1495 EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo"))); | |
| 1496 | |
| 1497 const Matcher<const ::std::wstring&> m2 = HasSubstr(L"foo"); | |
| 1498 EXPECT_TRUE(m2.Matches(::std::wstring(L"I love food."))); | |
| 1499 EXPECT_FALSE(m2.Matches(::std::wstring(L"tofo"))); | |
| 1500 } | |
| 1501 | |
| 1502 // Tests that HasSubstr() works for matching C-wide-string-typed values. | |
| 1503 TEST(StdWideHasSubstrTest, WorksForCStrings) { | |
| 1504 const Matcher<wchar_t*> m1 = HasSubstr(L"foo"); | |
| 1505 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food."))); | |
| 1506 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo"))); | |
| 1507 EXPECT_FALSE(m1.Matches(NULL)); | |
| 1508 | |
| 1509 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo"); | |
| 1510 EXPECT_TRUE(m2.Matches(L"I love food.")); | |
| 1511 EXPECT_FALSE(m2.Matches(L"tofo")); | |
| 1512 EXPECT_FALSE(m2.Matches(NULL)); | |
| 1513 } | |
| 1514 | |
| 1515 // Tests that HasSubstr(s) describes itself properly. | |
| 1516 TEST(StdWideHasSubstrTest, CanDescribeSelf) { | |
| 1517 Matcher< ::std::wstring> m = HasSubstr(L"foo\n\""); | |
| 1518 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m)); | |
| 1519 } | |
| 1520 | |
| 1521 // Tests StartsWith(s). | |
| 1522 | |
| 1523 TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) { | |
| 1524 const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(L"")); | |
| 1525 EXPECT_TRUE(m1.Matches(L"Hi")); | |
| 1526 EXPECT_TRUE(m1.Matches(L"")); | |
| 1527 EXPECT_FALSE(m1.Matches(NULL)); | |
| 1528 | |
| 1529 const Matcher<const ::std::wstring&> m2 = StartsWith(L"Hi"); | |
| 1530 EXPECT_TRUE(m2.Matches(L"Hi")); | |
| 1531 EXPECT_TRUE(m2.Matches(L"Hi Hi!")); | |
| 1532 EXPECT_TRUE(m2.Matches(L"High")); | |
| 1533 EXPECT_FALSE(m2.Matches(L"H")); | |
| 1534 EXPECT_FALSE(m2.Matches(L" Hi")); | |
| 1535 } | |
| 1536 | |
| 1537 TEST(StdWideStartsWithTest, CanDescribeSelf) { | |
| 1538 Matcher<const ::std::wstring> m = StartsWith(L"Hi"); | |
| 1539 EXPECT_EQ("starts with L\"Hi\"", Describe(m)); | |
| 1540 } | |
| 1541 | |
| 1542 // Tests EndsWith(s). | |
| 1543 | |
| 1544 TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) { | |
| 1545 const Matcher<const wchar_t*> m1 = EndsWith(L""); | |
| 1546 EXPECT_TRUE(m1.Matches(L"Hi")); | |
| 1547 EXPECT_TRUE(m1.Matches(L"")); | |
| 1548 EXPECT_FALSE(m1.Matches(NULL)); | |
| 1549 | |
| 1550 const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L"Hi")); | |
| 1551 EXPECT_TRUE(m2.Matches(L"Hi")); | |
| 1552 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi")); | |
| 1553 EXPECT_TRUE(m2.Matches(L"Super Hi")); | |
| 1554 EXPECT_FALSE(m2.Matches(L"i")); | |
| 1555 EXPECT_FALSE(m2.Matches(L"Hi ")); | |
| 1556 } | |
| 1557 | |
| 1558 TEST(StdWideEndsWithTest, CanDescribeSelf) { | |
| 1559 Matcher<const ::std::wstring> m = EndsWith(L"Hi"); | |
| 1560 EXPECT_EQ("ends with L\"Hi\"", Describe(m)); | |
| 1561 } | |
| 1562 | |
| 1563 #endif // GTEST_HAS_STD_WSTRING | |
| 1564 | |
| 1565 #if GTEST_HAS_GLOBAL_WSTRING | |
| 1566 TEST(GlobalWideStrEqTest, MatchesEqual) { | |
| 1567 Matcher<const wchar_t*> m = StrEq(::wstring(L"Hello")); | |
| 1568 EXPECT_TRUE(m.Matches(L"Hello")); | |
| 1569 EXPECT_FALSE(m.Matches(L"hello")); | |
| 1570 EXPECT_FALSE(m.Matches(NULL)); | |
| 1571 | |
| 1572 Matcher<const ::wstring&> m2 = StrEq(L"Hello"); | |
| 1573 EXPECT_TRUE(m2.Matches(L"Hello")); | |
| 1574 EXPECT_FALSE(m2.Matches(L"Hi")); | |
| 1575 | |
| 1576 Matcher<const ::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D"); | |
| 1577 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D")); | |
| 1578 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E")); | |
| 1579 | |
| 1580 ::wstring str(L"01204500800"); | |
| 1581 str[3] = L'\0'; | |
| 1582 Matcher<const ::wstring&> m4 = StrEq(str); | |
| 1583 EXPECT_TRUE(m4.Matches(str)); | |
| 1584 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0'; | |
| 1585 Matcher<const ::wstring&> m5 = StrEq(str); | |
| 1586 EXPECT_TRUE(m5.Matches(str)); | |
| 1587 } | |
| 1588 | |
| 1589 TEST(GlobalWideStrEqTest, CanDescribeSelf) { | |
| 1590 Matcher< ::wstring> m = StrEq(L"Hi-\'\"\?\\\a\b\f\n\r\t\v"); | |
| 1591 EXPECT_EQ("is equal to L\"Hi-\'\\\"\\?\\\\\\a\\b\\f\\n\\r\\t\\v\"", | |
| 1592 Describe(m)); | |
| 1593 | |
| 1594 Matcher< ::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D"); | |
| 1595 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"", | |
| 1596 Describe(m2)); | |
| 1597 | |
| 1598 ::wstring str(L"01204500800"); | |
| 1599 str[3] = L'\0'; | |
| 1600 Matcher<const ::wstring&> m4 = StrEq(str); | |
| 1601 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4)); | |
| 1602 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0'; | |
| 1603 Matcher<const ::wstring&> m5 = StrEq(str); | |
| 1604 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5)); | |
| 1605 } | |
| 1606 | |
| 1607 TEST(GlobalWideStrNeTest, MatchesUnequalString) { | |
| 1608 Matcher<const wchar_t*> m = StrNe(L"Hello"); | |
| 1609 EXPECT_TRUE(m.Matches(L"")); | |
| 1610 EXPECT_TRUE(m.Matches(NULL)); | |
| 1611 EXPECT_FALSE(m.Matches(L"Hello")); | |
| 1612 | |
| 1613 Matcher< ::wstring> m2 = StrNe(::wstring(L"Hello")); | |
| 1614 EXPECT_TRUE(m2.Matches(L"hello")); | |
| 1615 EXPECT_FALSE(m2.Matches(L"Hello")); | |
| 1616 } | |
| 1617 | |
| 1618 TEST(GlobalWideStrNeTest, CanDescribeSelf) { | |
| 1619 Matcher<const wchar_t*> m = StrNe(L"Hi"); | |
| 1620 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m)); | |
| 1621 } | |
| 1622 | |
| 1623 TEST(GlobalWideStrCaseEqTest, MatchesEqualStringIgnoringCase) { | |
| 1624 Matcher<const wchar_t*> m = StrCaseEq(::wstring(L"Hello")); | |
| 1625 EXPECT_TRUE(m.Matches(L"Hello")); | |
| 1626 EXPECT_TRUE(m.Matches(L"hello")); | |
| 1627 EXPECT_FALSE(m.Matches(L"Hi")); | |
| 1628 EXPECT_FALSE(m.Matches(NULL)); | |
| 1629 | |
| 1630 Matcher<const ::wstring&> m2 = StrCaseEq(L"Hello"); | |
| 1631 EXPECT_TRUE(m2.Matches(L"hello")); | |
| 1632 EXPECT_FALSE(m2.Matches(L"Hi")); | |
| 1633 } | |
| 1634 | |
| 1635 TEST(GlobalWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) { | |
| 1636 ::wstring str1(L"oabocdooeoo"); | |
| 1637 ::wstring str2(L"OABOCDOOEOO"); | |
| 1638 Matcher<const ::wstring&> m0 = StrCaseEq(str1); | |
| 1639 EXPECT_FALSE(m0.Matches(str2 + ::wstring(1, L'\0'))); | |
| 1640 | |
| 1641 str1[3] = str2[3] = L'\0'; | |
| 1642 Matcher<const ::wstring&> m1 = StrCaseEq(str1); | |
| 1643 EXPECT_TRUE(m1.Matches(str2)); | |
| 1644 | |
| 1645 str1[0] = str1[6] = str1[7] = str1[10] = L'\0'; | |
| 1646 str2[0] = str2[6] = str2[7] = str2[10] = L'\0'; | |
| 1647 Matcher<const ::wstring&> m2 = StrCaseEq(str1); | |
| 1648 str1[9] = str2[9] = L'\0'; | |
| 1649 EXPECT_FALSE(m2.Matches(str2)); | |
| 1650 | |
| 1651 Matcher<const ::wstring&> m3 = StrCaseEq(str1); | |
| 1652 EXPECT_TRUE(m3.Matches(str2)); | |
| 1653 | |
| 1654 EXPECT_FALSE(m3.Matches(str2 + L"x")); | |
| 1655 str2.append(1, L'\0'); | |
| 1656 EXPECT_FALSE(m3.Matches(str2)); | |
| 1657 EXPECT_FALSE(m3.Matches(::wstring(str2, 0, 9))); | |
| 1658 } | |
| 1659 | |
| 1660 TEST(GlobalWideStrCaseEqTest, CanDescribeSelf) { | |
| 1661 Matcher< ::wstring> m = StrCaseEq(L"Hi"); | |
| 1662 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m)); | |
| 1663 } | |
| 1664 | |
| 1665 TEST(GlobalWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) { | |
| 1666 Matcher<const wchar_t*> m = StrCaseNe(L"Hello"); | |
| 1667 EXPECT_TRUE(m.Matches(L"Hi")); | |
| 1668 EXPECT_TRUE(m.Matches(NULL)); | |
| 1669 EXPECT_FALSE(m.Matches(L"Hello")); | |
| 1670 EXPECT_FALSE(m.Matches(L"hello")); | |
| 1671 | |
| 1672 Matcher< ::wstring> m2 = StrCaseNe(::wstring(L"Hello")); | |
| 1673 EXPECT_TRUE(m2.Matches(L"")); | |
| 1674 EXPECT_FALSE(m2.Matches(L"Hello")); | |
| 1675 } | |
| 1676 | |
| 1677 TEST(GlobalWideStrCaseNeTest, CanDescribeSelf) { | |
| 1678 Matcher<const wchar_t*> m = StrCaseNe(L"Hi"); | |
| 1679 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m)); | |
| 1680 } | |
| 1681 | |
| 1682 // Tests that HasSubstr() works for matching wstring-typed values. | |
| 1683 TEST(GlobalWideHasSubstrTest, WorksForStringClasses) { | |
| 1684 const Matcher< ::wstring> m1 = HasSubstr(L"foo"); | |
| 1685 EXPECT_TRUE(m1.Matches(::wstring(L"I love food."))); | |
| 1686 EXPECT_FALSE(m1.Matches(::wstring(L"tofo"))); | |
| 1687 | |
| 1688 const Matcher<const ::wstring&> m2 = HasSubstr(L"foo"); | |
| 1689 EXPECT_TRUE(m2.Matches(::wstring(L"I love food."))); | |
| 1690 EXPECT_FALSE(m2.Matches(::wstring(L"tofo"))); | |
| 1691 } | |
| 1692 | |
| 1693 // Tests that HasSubstr() works for matching C-wide-string-typed values. | |
| 1694 TEST(GlobalWideHasSubstrTest, WorksForCStrings) { | |
| 1695 const Matcher<wchar_t*> m1 = HasSubstr(L"foo"); | |
| 1696 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food."))); | |
| 1697 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo"))); | |
| 1698 EXPECT_FALSE(m1.Matches(NULL)); | |
| 1699 | |
| 1700 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo"); | |
| 1701 EXPECT_TRUE(m2.Matches(L"I love food.")); | |
| 1702 EXPECT_FALSE(m2.Matches(L"tofo")); | |
| 1703 EXPECT_FALSE(m2.Matches(NULL)); | |
| 1704 } | |
| 1705 | |
| 1706 // Tests that HasSubstr(s) describes itself properly. | |
| 1707 TEST(GlobalWideHasSubstrTest, CanDescribeSelf) { | |
| 1708 Matcher< ::wstring> m = HasSubstr(L"foo\n\""); | |
| 1709 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m)); | |
| 1710 } | |
| 1711 | |
| 1712 // Tests StartsWith(s). | |
| 1713 | |
| 1714 TEST(GlobalWideStartsWithTest, MatchesStringWithGivenPrefix) { | |
| 1715 const Matcher<const wchar_t*> m1 = StartsWith(::wstring(L"")); | |
| 1716 EXPECT_TRUE(m1.Matches(L"Hi")); | |
| 1717 EXPECT_TRUE(m1.Matches(L"")); | |
| 1718 EXPECT_FALSE(m1.Matches(NULL)); | |
| 1719 | |
| 1720 const Matcher<const ::wstring&> m2 = StartsWith(L"Hi"); | |
| 1721 EXPECT_TRUE(m2.Matches(L"Hi")); | |
| 1722 EXPECT_TRUE(m2.Matches(L"Hi Hi!")); | |
| 1723 EXPECT_TRUE(m2.Matches(L"High")); | |
| 1724 EXPECT_FALSE(m2.Matches(L"H")); | |
| 1725 EXPECT_FALSE(m2.Matches(L" Hi")); | |
| 1726 } | |
| 1727 | |
| 1728 TEST(GlobalWideStartsWithTest, CanDescribeSelf) { | |
| 1729 Matcher<const ::wstring> m = StartsWith(L"Hi"); | |
| 1730 EXPECT_EQ("starts with L\"Hi\"", Describe(m)); | |
| 1731 } | |
| 1732 | |
| 1733 // Tests EndsWith(s). | |
| 1734 | |
| 1735 TEST(GlobalWideEndsWithTest, MatchesStringWithGivenSuffix) { | |
| 1736 const Matcher<const wchar_t*> m1 = EndsWith(L""); | |
| 1737 EXPECT_TRUE(m1.Matches(L"Hi")); | |
| 1738 EXPECT_TRUE(m1.Matches(L"")); | |
| 1739 EXPECT_FALSE(m1.Matches(NULL)); | |
| 1740 | |
| 1741 const Matcher<const ::wstring&> m2 = EndsWith(::wstring(L"Hi")); | |
| 1742 EXPECT_TRUE(m2.Matches(L"Hi")); | |
| 1743 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi")); | |
| 1744 EXPECT_TRUE(m2.Matches(L"Super Hi")); | |
| 1745 EXPECT_FALSE(m2.Matches(L"i")); | |
| 1746 EXPECT_FALSE(m2.Matches(L"Hi ")); | |
| 1747 } | |
| 1748 | |
| 1749 TEST(GlobalWideEndsWithTest, CanDescribeSelf) { | |
| 1750 Matcher<const ::wstring> m = EndsWith(L"Hi"); | |
| 1751 EXPECT_EQ("ends with L\"Hi\"", Describe(m)); | |
| 1752 } | |
| 1753 | |
| 1754 #endif // GTEST_HAS_GLOBAL_WSTRING | |
| 1755 | |
| 1756 | |
| 1757 typedef ::std::tr1::tuple<long, int> Tuple2; // NOLINT | |
| 1758 | |
| 1759 // Tests that Eq() matches a 2-tuple where the first field == the | |
| 1760 // second field. | |
| 1761 TEST(Eq2Test, MatchesEqualArguments) { | |
| 1762 Matcher<const Tuple2&> m = Eq(); | |
| 1763 EXPECT_TRUE(m.Matches(Tuple2(5L, 5))); | |
| 1764 EXPECT_FALSE(m.Matches(Tuple2(5L, 6))); | |
| 1765 } | |
| 1766 | |
| 1767 // Tests that Eq() describes itself properly. | |
| 1768 TEST(Eq2Test, CanDescribeSelf) { | |
| 1769 Matcher<const Tuple2&> m = Eq(); | |
| 1770 EXPECT_EQ("are an equal pair", Describe(m)); | |
| 1771 } | |
| 1772 | |
| 1773 // Tests that Ge() matches a 2-tuple where the first field >= the | |
| 1774 // second field. | |
| 1775 TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) { | |
| 1776 Matcher<const Tuple2&> m = Ge(); | |
| 1777 EXPECT_TRUE(m.Matches(Tuple2(5L, 4))); | |
| 1778 EXPECT_TRUE(m.Matches(Tuple2(5L, 5))); | |
| 1779 EXPECT_FALSE(m.Matches(Tuple2(5L, 6))); | |
| 1780 } | |
| 1781 | |
| 1782 // Tests that Ge() describes itself properly. | |
| 1783 TEST(Ge2Test, CanDescribeSelf) { | |
| 1784 Matcher<const Tuple2&> m = Ge(); | |
| 1785 EXPECT_EQ("are a pair where the first >= the second", Describe(m)); | |
| 1786 } | |
| 1787 | |
| 1788 // Tests that Gt() matches a 2-tuple where the first field > the | |
| 1789 // second field. | |
| 1790 TEST(Gt2Test, MatchesGreaterThanArguments) { | |
| 1791 Matcher<const Tuple2&> m = Gt(); | |
| 1792 EXPECT_TRUE(m.Matches(Tuple2(5L, 4))); | |
| 1793 EXPECT_FALSE(m.Matches(Tuple2(5L, 5))); | |
| 1794 EXPECT_FALSE(m.Matches(Tuple2(5L, 6))); | |
| 1795 } | |
| 1796 | |
| 1797 // Tests that Gt() describes itself properly. | |
| 1798 TEST(Gt2Test, CanDescribeSelf) { | |
| 1799 Matcher<const Tuple2&> m = Gt(); | |
| 1800 EXPECT_EQ("are a pair where the first > the second", Describe(m)); | |
| 1801 } | |
| 1802 | |
| 1803 // Tests that Le() matches a 2-tuple where the first field <= the | |
| 1804 // second field. | |
| 1805 TEST(Le2Test, MatchesLessThanOrEqualArguments) { | |
| 1806 Matcher<const Tuple2&> m = Le(); | |
| 1807 EXPECT_TRUE(m.Matches(Tuple2(5L, 6))); | |
| 1808 EXPECT_TRUE(m.Matches(Tuple2(5L, 5))); | |
| 1809 EXPECT_FALSE(m.Matches(Tuple2(5L, 4))); | |
| 1810 } | |
| 1811 | |
| 1812 // Tests that Le() describes itself properly. | |
| 1813 TEST(Le2Test, CanDescribeSelf) { | |
| 1814 Matcher<const Tuple2&> m = Le(); | |
| 1815 EXPECT_EQ("are a pair where the first <= the second", Describe(m)); | |
| 1816 } | |
| 1817 | |
| 1818 // Tests that Lt() matches a 2-tuple where the first field < the | |
| 1819 // second field. | |
| 1820 TEST(Lt2Test, MatchesLessThanArguments) { | |
| 1821 Matcher<const Tuple2&> m = Lt(); | |
| 1822 EXPECT_TRUE(m.Matches(Tuple2(5L, 6))); | |
| 1823 EXPECT_FALSE(m.Matches(Tuple2(5L, 5))); | |
| 1824 EXPECT_FALSE(m.Matches(Tuple2(5L, 4))); | |
| 1825 } | |
| 1826 | |
| 1827 // Tests that Lt() describes itself properly. | |
| 1828 TEST(Lt2Test, CanDescribeSelf) { | |
| 1829 Matcher<const Tuple2&> m = Lt(); | |
| 1830 EXPECT_EQ("are a pair where the first < the second", Describe(m)); | |
| 1831 } | |
| 1832 | |
| 1833 // Tests that Ne() matches a 2-tuple where the first field != the | |
| 1834 // second field. | |
| 1835 TEST(Ne2Test, MatchesUnequalArguments) { | |
| 1836 Matcher<const Tuple2&> m = Ne(); | |
| 1837 EXPECT_TRUE(m.Matches(Tuple2(5L, 6))); | |
| 1838 EXPECT_TRUE(m.Matches(Tuple2(5L, 4))); | |
| 1839 EXPECT_FALSE(m.Matches(Tuple2(5L, 5))); | |
| 1840 } | |
| 1841 | |
| 1842 // Tests that Ne() describes itself properly. | |
| 1843 TEST(Ne2Test, CanDescribeSelf) { | |
| 1844 Matcher<const Tuple2&> m = Ne(); | |
| 1845 EXPECT_EQ("are an unequal pair", Describe(m)); | |
| 1846 } | |
| 1847 | |
| 1848 // Tests that Not(m) matches any value that doesn't match m. | |
| 1849 TEST(NotTest, NegatesMatcher) { | |
| 1850 Matcher<int> m; | |
| 1851 m = Not(Eq(2)); | |
| 1852 EXPECT_TRUE(m.Matches(3)); | |
| 1853 EXPECT_FALSE(m.Matches(2)); | |
| 1854 } | |
| 1855 | |
| 1856 // Tests that Not(m) describes itself properly. | |
| 1857 TEST(NotTest, CanDescribeSelf) { | |
| 1858 Matcher<int> m = Not(Eq(5)); | |
| 1859 EXPECT_EQ("isn't equal to 5", Describe(m)); | |
| 1860 } | |
| 1861 | |
| 1862 // Tests that monomorphic matchers are safely cast by the Not matcher. | |
| 1863 TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) { | |
| 1864 // greater_than_5 is a monomorphic matcher. | |
| 1865 Matcher<int> greater_than_5 = Gt(5); | |
| 1866 | |
| 1867 Matcher<const int&> m = Not(greater_than_5); | |
| 1868 Matcher<int&> m2 = Not(greater_than_5); | |
| 1869 Matcher<int&> m3 = Not(m); | |
| 1870 } | |
| 1871 | |
| 1872 // Tests that AllOf(m1, ..., mn) matches any value that matches all of | |
| 1873 // the given matchers. | |
| 1874 TEST(AllOfTest, MatchesWhenAllMatch) { | |
| 1875 Matcher<int> m; | |
| 1876 m = AllOf(Le(2), Ge(1)); | |
| 1877 EXPECT_TRUE(m.Matches(1)); | |
| 1878 EXPECT_TRUE(m.Matches(2)); | |
| 1879 EXPECT_FALSE(m.Matches(0)); | |
| 1880 EXPECT_FALSE(m.Matches(3)); | |
| 1881 | |
| 1882 m = AllOf(Gt(0), Ne(1), Ne(2)); | |
| 1883 EXPECT_TRUE(m.Matches(3)); | |
| 1884 EXPECT_FALSE(m.Matches(2)); | |
| 1885 EXPECT_FALSE(m.Matches(1)); | |
| 1886 EXPECT_FALSE(m.Matches(0)); | |
| 1887 | |
| 1888 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3)); | |
| 1889 EXPECT_TRUE(m.Matches(4)); | |
| 1890 EXPECT_FALSE(m.Matches(3)); | |
| 1891 EXPECT_FALSE(m.Matches(2)); | |
| 1892 EXPECT_FALSE(m.Matches(1)); | |
| 1893 EXPECT_FALSE(m.Matches(0)); | |
| 1894 | |
| 1895 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7)); | |
| 1896 EXPECT_TRUE(m.Matches(0)); | |
| 1897 EXPECT_TRUE(m.Matches(1)); | |
| 1898 EXPECT_FALSE(m.Matches(3)); | |
| 1899 } | |
| 1900 | |
| 1901 // Tests that AllOf(m1, ..., mn) describes itself properly. | |
| 1902 TEST(AllOfTest, CanDescribeSelf) { | |
| 1903 Matcher<int> m; | |
| 1904 m = AllOf(Le(2), Ge(1)); | |
| 1905 EXPECT_EQ("(is <= 2) and (is >= 1)", Describe(m)); | |
| 1906 | |
| 1907 m = AllOf(Gt(0), Ne(1), Ne(2)); | |
| 1908 EXPECT_EQ("(is > 0) and " | |
| 1909 "((isn't equal to 1) and " | |
| 1910 "(isn't equal to 2))", | |
| 1911 Describe(m)); | |
| 1912 | |
| 1913 | |
| 1914 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3)); | |
| 1915 EXPECT_EQ("(is > 0) and " | |
| 1916 "((isn't equal to 1) and " | |
| 1917 "((isn't equal to 2) and " | |
| 1918 "(isn't equal to 3)))", | |
| 1919 Describe(m)); | |
| 1920 | |
| 1921 | |
| 1922 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7)); | |
| 1923 EXPECT_EQ("(is >= 0) and " | |
| 1924 "((is < 10) and " | |
| 1925 "((isn't equal to 3) and " | |
| 1926 "((isn't equal to 5) and " | |
| 1927 "(isn't equal to 7))))", | |
| 1928 Describe(m)); | |
| 1929 } | |
| 1930 | |
| 1931 // Tests that AllOf(m1, ..., mn) describes its negation properly. | |
| 1932 TEST(AllOfTest, CanDescribeNegation) { | |
| 1933 Matcher<int> m; | |
| 1934 m = AllOf(Le(2), Ge(1)); | |
| 1935 EXPECT_EQ("(isn't <= 2) or " | |
| 1936 "(isn't >= 1)", | |
| 1937 DescribeNegation(m)); | |
| 1938 | |
| 1939 m = AllOf(Gt(0), Ne(1), Ne(2)); | |
| 1940 EXPECT_EQ("(isn't > 0) or " | |
| 1941 "((is equal to 1) or " | |
| 1942 "(is equal to 2))", | |
| 1943 DescribeNegation(m)); | |
| 1944 | |
| 1945 | |
| 1946 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3)); | |
| 1947 EXPECT_EQ("(isn't > 0) or " | |
| 1948 "((is equal to 1) or " | |
| 1949 "((is equal to 2) or " | |
| 1950 "(is equal to 3)))", | |
| 1951 DescribeNegation(m)); | |
| 1952 | |
| 1953 | |
| 1954 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7)); | |
| 1955 EXPECT_EQ("(isn't >= 0) or " | |
| 1956 "((isn't < 10) or " | |
| 1957 "((is equal to 3) or " | |
| 1958 "((is equal to 5) or " | |
| 1959 "(is equal to 7))))", | |
| 1960 DescribeNegation(m)); | |
| 1961 } | |
| 1962 | |
| 1963 // Tests that monomorphic matchers are safely cast by the AllOf matcher. | |
| 1964 TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) { | |
| 1965 // greater_than_5 and less_than_10 are monomorphic matchers. | |
| 1966 Matcher<int> greater_than_5 = Gt(5); | |
| 1967 Matcher<int> less_than_10 = Lt(10); | |
| 1968 | |
| 1969 Matcher<const int&> m = AllOf(greater_than_5, less_than_10); | |
| 1970 Matcher<int&> m2 = AllOf(greater_than_5, less_than_10); | |
| 1971 Matcher<int&> m3 = AllOf(greater_than_5, m2); | |
| 1972 | |
| 1973 // Tests that BothOf works when composing itself. | |
| 1974 Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10); | |
| 1975 Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10); | |
| 1976 } | |
| 1977 | |
| 1978 TEST(AllOfTest, ExplainsResult) { | |
| 1979 Matcher<int> m; | |
| 1980 | |
| 1981 // Successful match. Both matchers need to explain. The second | |
| 1982 // matcher doesn't give an explanation, so only the first matcher's | |
| 1983 // explanation is printed. | |
| 1984 m = AllOf(GreaterThan(10), Lt(30)); | |
| 1985 EXPECT_EQ("which is 15 more than 10", Explain(m, 25)); | |
| 1986 | |
| 1987 // Successful match. Both matchers need to explain. | |
| 1988 m = AllOf(GreaterThan(10), GreaterThan(20)); | |
| 1989 EXPECT_EQ("which is 20 more than 10, and which is 10 more than 20", | |
| 1990 Explain(m, 30)); | |
| 1991 | |
| 1992 // Successful match. All matchers need to explain. The second | |
| 1993 // matcher doesn't given an explanation. | |
| 1994 m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20)); | |
| 1995 EXPECT_EQ("which is 15 more than 10, and which is 5 more than 20", | |
| 1996 Explain(m, 25)); | |
| 1997 | |
| 1998 // Successful match. All matchers need to explain. | |
| 1999 m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30)); | |
| 2000 EXPECT_EQ("which is 30 more than 10, and which is 20 more than 20, " | |
| 2001 "and which is 10 more than 30", | |
| 2002 Explain(m, 40)); | |
| 2003 | |
| 2004 // Failed match. The first matcher, which failed, needs to | |
| 2005 // explain. | |
| 2006 m = AllOf(GreaterThan(10), GreaterThan(20)); | |
| 2007 EXPECT_EQ("which is 5 less than 10", Explain(m, 5)); | |
| 2008 | |
| 2009 // Failed match. The second matcher, which failed, needs to | |
| 2010 // explain. Since it doesn't given an explanation, nothing is | |
| 2011 // printed. | |
| 2012 m = AllOf(GreaterThan(10), Lt(30)); | |
| 2013 EXPECT_EQ("", Explain(m, 40)); | |
| 2014 | |
| 2015 // Failed match. The second matcher, which failed, needs to | |
| 2016 // explain. | |
| 2017 m = AllOf(GreaterThan(10), GreaterThan(20)); | |
| 2018 EXPECT_EQ("which is 5 less than 20", Explain(m, 15)); | |
| 2019 } | |
| 2020 | |
| 2021 // Tests that AnyOf(m1, ..., mn) matches any value that matches at | |
| 2022 // least one of the given matchers. | |
| 2023 TEST(AnyOfTest, MatchesWhenAnyMatches) { | |
| 2024 Matcher<int> m; | |
| 2025 m = AnyOf(Le(1), Ge(3)); | |
| 2026 EXPECT_TRUE(m.Matches(1)); | |
| 2027 EXPECT_TRUE(m.Matches(4)); | |
| 2028 EXPECT_FALSE(m.Matches(2)); | |
| 2029 | |
| 2030 m = AnyOf(Lt(0), Eq(1), Eq(2)); | |
| 2031 EXPECT_TRUE(m.Matches(-1)); | |
| 2032 EXPECT_TRUE(m.Matches(1)); | |
| 2033 EXPECT_TRUE(m.Matches(2)); | |
| 2034 EXPECT_FALSE(m.Matches(0)); | |
| 2035 | |
| 2036 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3)); | |
| 2037 EXPECT_TRUE(m.Matches(-1)); | |
| 2038 EXPECT_TRUE(m.Matches(1)); | |
| 2039 EXPECT_TRUE(m.Matches(2)); | |
| 2040 EXPECT_TRUE(m.Matches(3)); | |
| 2041 EXPECT_FALSE(m.Matches(0)); | |
| 2042 | |
| 2043 m = AnyOf(Le(0), Gt(10), 3, 5, 7); | |
| 2044 EXPECT_TRUE(m.Matches(0)); | |
| 2045 EXPECT_TRUE(m.Matches(11)); | |
| 2046 EXPECT_TRUE(m.Matches(3)); | |
| 2047 EXPECT_FALSE(m.Matches(2)); | |
| 2048 } | |
| 2049 | |
| 2050 // Tests that AnyOf(m1, ..., mn) describes itself properly. | |
| 2051 TEST(AnyOfTest, CanDescribeSelf) { | |
| 2052 Matcher<int> m; | |
| 2053 m = AnyOf(Le(1), Ge(3)); | |
| 2054 EXPECT_EQ("(is <= 1) or (is >= 3)", | |
| 2055 Describe(m)); | |
| 2056 | |
| 2057 m = AnyOf(Lt(0), Eq(1), Eq(2)); | |
| 2058 EXPECT_EQ("(is < 0) or " | |
| 2059 "((is equal to 1) or (is equal to 2))", | |
| 2060 Describe(m)); | |
| 2061 | |
| 2062 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3)); | |
| 2063 EXPECT_EQ("(is < 0) or " | |
| 2064 "((is equal to 1) or " | |
| 2065 "((is equal to 2) or " | |
| 2066 "(is equal to 3)))", | |
| 2067 Describe(m)); | |
| 2068 | |
| 2069 m = AnyOf(Le(0), Gt(10), 3, 5, 7); | |
| 2070 EXPECT_EQ("(is <= 0) or " | |
| 2071 "((is > 10) or " | |
| 2072 "((is equal to 3) or " | |
| 2073 "((is equal to 5) or " | |
| 2074 "(is equal to 7))))", | |
| 2075 Describe(m)); | |
| 2076 } | |
| 2077 | |
| 2078 // Tests that AnyOf(m1, ..., mn) describes its negation properly. | |
| 2079 TEST(AnyOfTest, CanDescribeNegation) { | |
| 2080 Matcher<int> m; | |
| 2081 m = AnyOf(Le(1), Ge(3)); | |
| 2082 EXPECT_EQ("(isn't <= 1) and (isn't >= 3)", | |
| 2083 DescribeNegation(m)); | |
| 2084 | |
| 2085 m = AnyOf(Lt(0), Eq(1), Eq(2)); | |
| 2086 EXPECT_EQ("(isn't < 0) and " | |
| 2087 "((isn't equal to 1) and (isn't equal to 2))", | |
| 2088 DescribeNegation(m)); | |
| 2089 | |
| 2090 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3)); | |
| 2091 EXPECT_EQ("(isn't < 0) and " | |
| 2092 "((isn't equal to 1) and " | |
| 2093 "((isn't equal to 2) and " | |
| 2094 "(isn't equal to 3)))", | |
| 2095 DescribeNegation(m)); | |
| 2096 | |
| 2097 m = AnyOf(Le(0), Gt(10), 3, 5, 7); | |
| 2098 EXPECT_EQ("(isn't <= 0) and " | |
| 2099 "((isn't > 10) and " | |
| 2100 "((isn't equal to 3) and " | |
| 2101 "((isn't equal to 5) and " | |
| 2102 "(isn't equal to 7))))", | |
| 2103 DescribeNegation(m)); | |
| 2104 } | |
| 2105 | |
| 2106 // Tests that monomorphic matchers are safely cast by the AnyOf matcher. | |
| 2107 TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) { | |
| 2108 // greater_than_5 and less_than_10 are monomorphic matchers. | |
| 2109 Matcher<int> greater_than_5 = Gt(5); | |
| 2110 Matcher<int> less_than_10 = Lt(10); | |
| 2111 | |
| 2112 Matcher<const int&> m = AnyOf(greater_than_5, less_than_10); | |
| 2113 Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10); | |
| 2114 Matcher<int&> m3 = AnyOf(greater_than_5, m2); | |
| 2115 | |
| 2116 // Tests that EitherOf works when composing itself. | |
| 2117 Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10); | |
| 2118 Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10); | |
| 2119 } | |
| 2120 | |
| 2121 TEST(AnyOfTest, ExplainsResult) { | |
| 2122 Matcher<int> m; | |
| 2123 | |
| 2124 // Failed match. Both matchers need to explain. The second | |
| 2125 // matcher doesn't give an explanation, so only the first matcher's | |
| 2126 // explanation is printed. | |
| 2127 m = AnyOf(GreaterThan(10), Lt(0)); | |
| 2128 EXPECT_EQ("which is 5 less than 10", Explain(m, 5)); | |
| 2129 | |
| 2130 // Failed match. Both matchers need to explain. | |
| 2131 m = AnyOf(GreaterThan(10), GreaterThan(20)); | |
| 2132 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20", | |
| 2133 Explain(m, 5)); | |
| 2134 | |
| 2135 // Failed match. All matchers need to explain. The second | |
| 2136 // matcher doesn't given an explanation. | |
| 2137 m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30)); | |
| 2138 EXPECT_EQ("which is 5 less than 10, and which is 25 less than 30", | |
| 2139 Explain(m, 5)); | |
| 2140 | |
| 2141 // Failed match. All matchers need to explain. | |
| 2142 m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30)); | |
| 2143 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20, " | |
| 2144 "and which is 25 less than 30", | |
| 2145 Explain(m, 5)); | |
| 2146 | |
| 2147 // Successful match. The first matcher, which succeeded, needs to | |
| 2148 // explain. | |
| 2149 m = AnyOf(GreaterThan(10), GreaterThan(20)); | |
| 2150 EXPECT_EQ("which is 5 more than 10", Explain(m, 15)); | |
| 2151 | |
| 2152 // Successful match. The second matcher, which succeeded, needs to | |
| 2153 // explain. Since it doesn't given an explanation, nothing is | |
| 2154 // printed. | |
| 2155 m = AnyOf(GreaterThan(10), Lt(30)); | |
| 2156 EXPECT_EQ("", Explain(m, 0)); | |
| 2157 | |
| 2158 // Successful match. The second matcher, which succeeded, needs to | |
| 2159 // explain. | |
| 2160 m = AnyOf(GreaterThan(30), GreaterThan(20)); | |
| 2161 EXPECT_EQ("which is 5 more than 20", Explain(m, 25)); | |
| 2162 } | |
| 2163 | |
| 2164 // The following predicate function and predicate functor are for | |
| 2165 // testing the Truly(predicate) matcher. | |
| 2166 | |
| 2167 // Returns non-zero if the input is positive. Note that the return | |
| 2168 // type of this function is not bool. It's OK as Truly() accepts any | |
| 2169 // unary function or functor whose return type can be implicitly | |
| 2170 // converted to bool. | |
| 2171 int IsPositive(double x) { | |
| 2172 return x > 0 ? 1 : 0; | |
| 2173 } | |
| 2174 | |
| 2175 // This functor returns true if the input is greater than the given | |
| 2176 // number. | |
| 2177 class IsGreaterThan { | |
| 2178 public: | |
| 2179 explicit IsGreaterThan(int threshold) : threshold_(threshold) {} | |
| 2180 | |
| 2181 bool operator()(int n) const { return n > threshold_; } | |
| 2182 | |
| 2183 private: | |
| 2184 int threshold_; | |
| 2185 }; | |
| 2186 | |
| 2187 // For testing Truly(). | |
| 2188 const int foo = 0; | |
| 2189 | |
| 2190 // This predicate returns true iff the argument references foo and has | |
| 2191 // a zero value. | |
| 2192 bool ReferencesFooAndIsZero(const int& n) { | |
| 2193 return (&n == &foo) && (n == 0); | |
| 2194 } | |
| 2195 | |
| 2196 // Tests that Truly(predicate) matches what satisfies the given | |
| 2197 // predicate. | |
| 2198 TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) { | |
| 2199 Matcher<double> m = Truly(IsPositive); | |
| 2200 EXPECT_TRUE(m.Matches(2.0)); | |
| 2201 EXPECT_FALSE(m.Matches(-1.5)); | |
| 2202 } | |
| 2203 | |
| 2204 // Tests that Truly(predicate_functor) works too. | |
| 2205 TEST(TrulyTest, CanBeUsedWithFunctor) { | |
| 2206 Matcher<int> m = Truly(IsGreaterThan(5)); | |
| 2207 EXPECT_TRUE(m.Matches(6)); | |
| 2208 EXPECT_FALSE(m.Matches(4)); | |
| 2209 } | |
| 2210 | |
| 2211 // Tests that Truly(predicate) can describe itself properly. | |
| 2212 TEST(TrulyTest, CanDescribeSelf) { | |
| 2213 Matcher<double> m = Truly(IsPositive); | |
| 2214 EXPECT_EQ("satisfies the given predicate", | |
| 2215 Describe(m)); | |
| 2216 } | |
| 2217 | |
| 2218 // Tests that Truly(predicate) works when the matcher takes its | |
| 2219 // argument by reference. | |
| 2220 TEST(TrulyTest, WorksForByRefArguments) { | |
| 2221 Matcher<const int&> m = Truly(ReferencesFooAndIsZero); | |
| 2222 EXPECT_TRUE(m.Matches(foo)); | |
| 2223 int n = 0; | |
| 2224 EXPECT_FALSE(m.Matches(n)); | |
| 2225 } | |
| 2226 | |
| 2227 // Tests that Matches(m) is a predicate satisfied by whatever that | |
| 2228 // matches matcher m. | |
| 2229 TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) { | |
| 2230 EXPECT_TRUE(Matches(Ge(0))(1)); | |
| 2231 EXPECT_FALSE(Matches(Eq('a'))('b')); | |
| 2232 } | |
| 2233 | |
| 2234 // Tests that Matches(m) works when the matcher takes its argument by | |
| 2235 // reference. | |
| 2236 TEST(MatchesTest, WorksOnByRefArguments) { | |
| 2237 int m = 0, n = 0; | |
| 2238 EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n)); | |
| 2239 EXPECT_FALSE(Matches(Ref(m))(n)); | |
| 2240 } | |
| 2241 | |
| 2242 // Tests that a Matcher on non-reference type can be used in | |
| 2243 // Matches(). | |
| 2244 TEST(MatchesTest, WorksWithMatcherOnNonRefType) { | |
| 2245 Matcher<int> eq5 = Eq(5); | |
| 2246 EXPECT_TRUE(Matches(eq5)(5)); | |
| 2247 EXPECT_FALSE(Matches(eq5)(2)); | |
| 2248 } | |
| 2249 | |
| 2250 // Tests Value(value, matcher). Since Value() is a simple wrapper for | |
| 2251 // Matches(), which has been tested already, we don't spend a lot of | |
| 2252 // effort on testing Value(). | |
| 2253 TEST(ValueTest, WorksWithPolymorphicMatcher) { | |
| 2254 EXPECT_TRUE(Value("hi", StartsWith("h"))); | |
| 2255 EXPECT_FALSE(Value(5, Gt(10))); | |
| 2256 } | |
| 2257 | |
| 2258 TEST(ValueTest, WorksWithMonomorphicMatcher) { | |
| 2259 const Matcher<int> is_zero = Eq(0); | |
| 2260 EXPECT_TRUE(Value(0, is_zero)); | |
| 2261 EXPECT_FALSE(Value('a', is_zero)); | |
| 2262 | |
| 2263 int n = 0; | |
| 2264 const Matcher<const int&> ref_n = Ref(n); | |
| 2265 EXPECT_TRUE(Value(n, ref_n)); | |
| 2266 EXPECT_FALSE(Value(1, ref_n)); | |
| 2267 } | |
| 2268 | |
| 2269 TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) { | |
| 2270 StringMatchResultListener listener1; | |
| 2271 EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1)); | |
| 2272 EXPECT_EQ("% 2 == 0", listener1.str()); | |
| 2273 | |
| 2274 StringMatchResultListener listener2; | |
| 2275 EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2)); | |
| 2276 EXPECT_EQ("", listener2.str()); | |
| 2277 } | |
| 2278 | |
| 2279 TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) { | |
| 2280 const Matcher<int> is_even = PolymorphicIsEven(); | |
| 2281 StringMatchResultListener listener1; | |
| 2282 EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1)); | |
| 2283 EXPECT_EQ("% 2 == 0", listener1.str()); | |
| 2284 | |
| 2285 const Matcher<const double&> is_zero = Eq(0); | |
| 2286 StringMatchResultListener listener2; | |
| 2287 EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2)); | |
| 2288 EXPECT_EQ("", listener2.str()); | |
| 2289 } | |
| 2290 | |
| 2291 MATCHER_P(Really, inner_matcher, "") { | |
| 2292 return ExplainMatchResult(inner_matcher, arg, result_listener); | |
| 2293 } | |
| 2294 | |
| 2295 TEST(ExplainMatchResultTest, WorksInsideMATCHER) { | |
| 2296 EXPECT_THAT(0, Really(Eq(0))); | |
| 2297 } | |
| 2298 | |
| 2299 TEST(AllArgsTest, WorksForTuple) { | |
| 2300 EXPECT_THAT(make_tuple(1, 2L), AllArgs(Lt())); | |
| 2301 EXPECT_THAT(make_tuple(2L, 1), Not(AllArgs(Lt()))); | |
| 2302 } | |
| 2303 | |
| 2304 TEST(AllArgsTest, WorksForNonTuple) { | |
| 2305 EXPECT_THAT(42, AllArgs(Gt(0))); | |
| 2306 EXPECT_THAT('a', Not(AllArgs(Eq('b')))); | |
| 2307 } | |
| 2308 | |
| 2309 class AllArgsHelper { | |
| 2310 public: | |
| 2311 AllArgsHelper() {} | |
| 2312 | |
| 2313 MOCK_METHOD2(Helper, int(char x, int y)); | |
| 2314 | |
| 2315 private: | |
| 2316 GTEST_DISALLOW_COPY_AND_ASSIGN_(AllArgsHelper); | |
| 2317 }; | |
| 2318 | |
| 2319 TEST(AllArgsTest, WorksInWithClause) { | |
| 2320 AllArgsHelper helper; | |
| 2321 ON_CALL(helper, Helper(_, _)) | |
| 2322 .With(AllArgs(Lt())) | |
| 2323 .WillByDefault(Return(1)); | |
| 2324 EXPECT_CALL(helper, Helper(_, _)); | |
| 2325 EXPECT_CALL(helper, Helper(_, _)) | |
| 2326 .With(AllArgs(Gt())) | |
| 2327 .WillOnce(Return(2)); | |
| 2328 | |
| 2329 EXPECT_EQ(1, helper.Helper('\1', 2)); | |
| 2330 EXPECT_EQ(2, helper.Helper('a', 1)); | |
| 2331 } | |
| 2332 | |
| 2333 // Tests that ASSERT_THAT() and EXPECT_THAT() work when the value | |
| 2334 // matches the matcher. | |
| 2335 TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) { | |
| 2336 ASSERT_THAT(5, Ge(2)) << "This should succeed."; | |
| 2337 ASSERT_THAT("Foo", EndsWith("oo")); | |
| 2338 EXPECT_THAT(2, AllOf(Le(7), Ge(0))) << "This should succeed too."; | |
| 2339 EXPECT_THAT("Hello", StartsWith("Hell")); | |
| 2340 } | |
| 2341 | |
| 2342 // Tests that ASSERT_THAT() and EXPECT_THAT() work when the value | |
| 2343 // doesn't match the matcher. | |
| 2344 TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) { | |
| 2345 // 'n' must be static as it is used in an EXPECT_FATAL_FAILURE(), | |
| 2346 // which cannot reference auto variables. | |
| 2347 static int n; | |
| 2348 n = 5; | |
| 2349 | |
| 2350 // VC++ prior to version 8.0 SP1 has a bug where it will not see any | |
| 2351 // functions declared in the namespace scope from within nested classes. | |
| 2352 // EXPECT/ASSERT_(NON)FATAL_FAILURE macros use nested classes so that all | |
| 2353 // namespace-level functions invoked inside them need to be explicitly | |
| 2354 // resolved. | |
| 2355 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Gt(10)), | |
| 2356 "Value of: n\n" | |
| 2357 "Expected: is > 10\n" | |
| 2358 " Actual: 5"); | |
| 2359 n = 0; | |
| 2360 EXPECT_NONFATAL_FAILURE( | |
| 2361 EXPECT_THAT(n, ::testing::AllOf(::testing::Le(7), ::testing::Ge(5))), | |
| 2362 "Value of: n\n" | |
| 2363 "Expected: (is <= 7) and (is >= 5)\n" | |
| 2364 " Actual: 0"); | |
| 2365 } | |
| 2366 | |
| 2367 // Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument | |
| 2368 // has a reference type. | |
| 2369 TEST(MatcherAssertionTest, WorksForByRefArguments) { | |
| 2370 // We use a static variable here as EXPECT_FATAL_FAILURE() cannot | |
| 2371 // reference auto variables. | |
| 2372 static int n; | |
| 2373 n = 0; | |
| 2374 EXPECT_THAT(n, AllOf(Le(7), Ref(n))); | |
| 2375 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))), | |
| 2376 "Value of: n\n" | |
| 2377 "Expected: does not reference the variable @"); | |
| 2378 // Tests the "Actual" part. | |
| 2379 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))), | |
| 2380 "Actual: 0, which is located @"); | |
| 2381 } | |
| 2382 | |
| 2383 #if !GTEST_OS_SYMBIAN | |
| 2384 // Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is | |
| 2385 // monomorphic. | |
| 2386 | |
| 2387 // ASSERT_THAT("hello", starts_with_he) fails to compile with Nokia's | |
| 2388 // Symbian compiler: it tries to compile | |
| 2389 // template<T, U> class MatcherCastImpl { ... | |
| 2390 // virtual bool MatchAndExplain(T x, ...) const { | |
| 2391 // return source_matcher_.MatchAndExplain(static_cast<U>(x), ...); | |
| 2392 // with U == string and T == const char* | |
| 2393 // With ASSERT_THAT("hello"...) changed to ASSERT_THAT(string("hello") ... ) | |
| 2394 // the compiler silently crashes with no output. | |
| 2395 // If MatcherCastImpl is changed to use U(x) instead of static_cast<U>(x) | |
| 2396 // the code compiles but the converted string is bogus. | |
| 2397 TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) { | |
| 2398 Matcher<const char*> starts_with_he = StartsWith("he"); | |
| 2399 ASSERT_THAT("hello", starts_with_he); | |
| 2400 | |
| 2401 Matcher<const string&> ends_with_ok = EndsWith("ok"); | |
| 2402 ASSERT_THAT("book", ends_with_ok); | |
| 2403 | |
| 2404 Matcher<int> is_greater_than_5 = Gt(5); | |
| 2405 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5), | |
| 2406 "Value of: 5\n" | |
| 2407 "Expected: is > 5\n" | |
| 2408 " Actual: 5"); | |
| 2409 } | |
| 2410 #endif // !GTEST_OS_SYMBIAN | |
| 2411 | |
| 2412 // Tests floating-point matchers. | |
| 2413 template <typename RawType> | |
| 2414 class FloatingPointTest : public testing::Test { | |
| 2415 protected: | |
| 2416 typedef typename testing::internal::FloatingPoint<RawType> Floating; | |
| 2417 typedef typename Floating::Bits Bits; | |
| 2418 | |
| 2419 virtual void SetUp() { | |
| 2420 const size_t max_ulps = Floating::kMaxUlps; | |
| 2421 | |
| 2422 // The bits that represent 0.0. | |
| 2423 const Bits zero_bits = Floating(0).bits(); | |
| 2424 | |
| 2425 // Makes some numbers close to 0.0. | |
| 2426 close_to_positive_zero_ = Floating::ReinterpretBits(zero_bits + max_ulps/2); | |
| 2427 close_to_negative_zero_ = -Floating::ReinterpretBits( | |
| 2428 zero_bits + max_ulps - max_ulps/2); | |
| 2429 further_from_negative_zero_ = -Floating::ReinterpretBits( | |
| 2430 zero_bits + max_ulps + 1 - max_ulps/2); | |
| 2431 | |
| 2432 // The bits that represent 1.0. | |
| 2433 const Bits one_bits = Floating(1).bits(); | |
| 2434 | |
| 2435 // Makes some numbers close to 1.0. | |
| 2436 close_to_one_ = Floating::ReinterpretBits(one_bits + max_ulps); | |
| 2437 further_from_one_ = Floating::ReinterpretBits(one_bits + max_ulps + 1); | |
| 2438 | |
| 2439 // +infinity. | |
| 2440 infinity_ = Floating::Infinity(); | |
| 2441 | |
| 2442 // The bits that represent +infinity. | |
| 2443 const Bits infinity_bits = Floating(infinity_).bits(); | |
| 2444 | |
| 2445 // Makes some numbers close to infinity. | |
| 2446 close_to_infinity_ = Floating::ReinterpretBits(infinity_bits - max_ulps); | |
| 2447 further_from_infinity_ = Floating::ReinterpretBits( | |
| 2448 infinity_bits - max_ulps - 1); | |
| 2449 | |
| 2450 // Makes some NAN's. | |
| 2451 nan1_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 1); | |
| 2452 nan2_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 200); | |
| 2453 } | |
| 2454 | |
| 2455 void TestSize() { | |
| 2456 EXPECT_EQ(sizeof(RawType), sizeof(Bits)); | |
| 2457 } | |
| 2458 | |
| 2459 // A battery of tests for FloatingEqMatcher::Matches. | |
| 2460 // matcher_maker is a pointer to a function which creates a FloatingEqMatcher. | |
| 2461 void TestMatches( | |
| 2462 testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) { | |
| 2463 Matcher<RawType> m1 = matcher_maker(0.0); | |
| 2464 EXPECT_TRUE(m1.Matches(-0.0)); | |
| 2465 EXPECT_TRUE(m1.Matches(close_to_positive_zero_)); | |
| 2466 EXPECT_TRUE(m1.Matches(close_to_negative_zero_)); | |
| 2467 EXPECT_FALSE(m1.Matches(1.0)); | |
| 2468 | |
| 2469 Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_); | |
| 2470 EXPECT_FALSE(m2.Matches(further_from_negative_zero_)); | |
| 2471 | |
| 2472 Matcher<RawType> m3 = matcher_maker(1.0); | |
| 2473 EXPECT_TRUE(m3.Matches(close_to_one_)); | |
| 2474 EXPECT_FALSE(m3.Matches(further_from_one_)); | |
| 2475 | |
| 2476 // Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above. | |
| 2477 EXPECT_FALSE(m3.Matches(0.0)); | |
| 2478 | |
| 2479 Matcher<RawType> m4 = matcher_maker(-infinity_); | |
| 2480 EXPECT_TRUE(m4.Matches(-close_to_infinity_)); | |
| 2481 | |
| 2482 Matcher<RawType> m5 = matcher_maker(infinity_); | |
| 2483 EXPECT_TRUE(m5.Matches(close_to_infinity_)); | |
| 2484 | |
| 2485 // This is interesting as the representations of infinity_ and nan1_ | |
| 2486 // are only 1 DLP apart. | |
| 2487 EXPECT_FALSE(m5.Matches(nan1_)); | |
| 2488 | |
| 2489 // matcher_maker can produce a Matcher<const RawType&>, which is needed in | |
| 2490 // some cases. | |
| 2491 Matcher<const RawType&> m6 = matcher_maker(0.0); | |
| 2492 EXPECT_TRUE(m6.Matches(-0.0)); | |
| 2493 EXPECT_TRUE(m6.Matches(close_to_positive_zero_)); | |
| 2494 EXPECT_FALSE(m6.Matches(1.0)); | |
| 2495 | |
| 2496 // matcher_maker can produce a Matcher<RawType&>, which is needed in some | |
| 2497 // cases. | |
| 2498 Matcher<RawType&> m7 = matcher_maker(0.0); | |
| 2499 RawType x = 0.0; | |
| 2500 EXPECT_TRUE(m7.Matches(x)); | |
| 2501 x = 0.01f; | |
| 2502 EXPECT_FALSE(m7.Matches(x)); | |
| 2503 } | |
| 2504 | |
| 2505 // Pre-calculated numbers to be used by the tests. | |
| 2506 | |
| 2507 static RawType close_to_positive_zero_; | |
| 2508 static RawType close_to_negative_zero_; | |
| 2509 static RawType further_from_negative_zero_; | |
| 2510 | |
| 2511 static RawType close_to_one_; | |
| 2512 static RawType further_from_one_; | |
| 2513 | |
| 2514 static RawType infinity_; | |
| 2515 static RawType close_to_infinity_; | |
| 2516 static RawType further_from_infinity_; | |
| 2517 | |
| 2518 static RawType nan1_; | |
| 2519 static RawType nan2_; | |
| 2520 }; | |
| 2521 | |
| 2522 template <typename RawType> | |
| 2523 RawType FloatingPointTest<RawType>::close_to_positive_zero_; | |
| 2524 | |
| 2525 template <typename RawType> | |
| 2526 RawType FloatingPointTest<RawType>::close_to_negative_zero_; | |
| 2527 | |
| 2528 template <typename RawType> | |
| 2529 RawType FloatingPointTest<RawType>::further_from_negative_zero_; | |
| 2530 | |
| 2531 template <typename RawType> | |
| 2532 RawType FloatingPointTest<RawType>::close_to_one_; | |
| 2533 | |
| 2534 template <typename RawType> | |
| 2535 RawType FloatingPointTest<RawType>::further_from_one_; | |
| 2536 | |
| 2537 template <typename RawType> | |
| 2538 RawType FloatingPointTest<RawType>::infinity_; | |
| 2539 | |
| 2540 template <typename RawType> | |
| 2541 RawType FloatingPointTest<RawType>::close_to_infinity_; | |
| 2542 | |
| 2543 template <typename RawType> | |
| 2544 RawType FloatingPointTest<RawType>::further_from_infinity_; | |
| 2545 | |
| 2546 template <typename RawType> | |
| 2547 RawType FloatingPointTest<RawType>::nan1_; | |
| 2548 | |
| 2549 template <typename RawType> | |
| 2550 RawType FloatingPointTest<RawType>::nan2_; | |
| 2551 | |
| 2552 // Instantiate FloatingPointTest for testing floats. | |
| 2553 typedef FloatingPointTest<float> FloatTest; | |
| 2554 | |
| 2555 TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) { | |
| 2556 TestMatches(&FloatEq); | |
| 2557 } | |
| 2558 | |
| 2559 TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) { | |
| 2560 TestMatches(&NanSensitiveFloatEq); | |
| 2561 } | |
| 2562 | |
| 2563 TEST_F(FloatTest, FloatEqCannotMatchNaN) { | |
| 2564 // FloatEq never matches NaN. | |
| 2565 Matcher<float> m = FloatEq(nan1_); | |
| 2566 EXPECT_FALSE(m.Matches(nan1_)); | |
| 2567 EXPECT_FALSE(m.Matches(nan2_)); | |
| 2568 EXPECT_FALSE(m.Matches(1.0)); | |
| 2569 } | |
| 2570 | |
| 2571 TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) { | |
| 2572 // NanSensitiveFloatEq will match NaN. | |
| 2573 Matcher<float> m = NanSensitiveFloatEq(nan1_); | |
| 2574 EXPECT_TRUE(m.Matches(nan1_)); | |
| 2575 EXPECT_TRUE(m.Matches(nan2_)); | |
| 2576 EXPECT_FALSE(m.Matches(1.0)); | |
| 2577 } | |
| 2578 | |
| 2579 TEST_F(FloatTest, FloatEqCanDescribeSelf) { | |
| 2580 Matcher<float> m1 = FloatEq(2.0f); | |
| 2581 EXPECT_EQ("is approximately 2", Describe(m1)); | |
| 2582 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1)); | |
| 2583 | |
| 2584 Matcher<float> m2 = FloatEq(0.5f); | |
| 2585 EXPECT_EQ("is approximately 0.5", Describe(m2)); | |
| 2586 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2)); | |
| 2587 | |
| 2588 Matcher<float> m3 = FloatEq(nan1_); | |
| 2589 EXPECT_EQ("never matches", Describe(m3)); | |
| 2590 EXPECT_EQ("is anything", DescribeNegation(m3)); | |
| 2591 } | |
| 2592 | |
| 2593 TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) { | |
| 2594 Matcher<float> m1 = NanSensitiveFloatEq(2.0f); | |
| 2595 EXPECT_EQ("is approximately 2", Describe(m1)); | |
| 2596 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1)); | |
| 2597 | |
| 2598 Matcher<float> m2 = NanSensitiveFloatEq(0.5f); | |
| 2599 EXPECT_EQ("is approximately 0.5", Describe(m2)); | |
| 2600 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2)); | |
| 2601 | |
| 2602 Matcher<float> m3 = NanSensitiveFloatEq(nan1_); | |
| 2603 EXPECT_EQ("is NaN", Describe(m3)); | |
| 2604 EXPECT_EQ("isn't NaN", DescribeNegation(m3)); | |
| 2605 } | |
| 2606 | |
| 2607 // Instantiate FloatingPointTest for testing doubles. | |
| 2608 typedef FloatingPointTest<double> DoubleTest; | |
| 2609 | |
| 2610 TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) { | |
| 2611 TestMatches(&DoubleEq); | |
| 2612 } | |
| 2613 | |
| 2614 TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) { | |
| 2615 TestMatches(&NanSensitiveDoubleEq); | |
| 2616 } | |
| 2617 | |
| 2618 TEST_F(DoubleTest, DoubleEqCannotMatchNaN) { | |
| 2619 // DoubleEq never matches NaN. | |
| 2620 Matcher<double> m = DoubleEq(nan1_); | |
| 2621 EXPECT_FALSE(m.Matches(nan1_)); | |
| 2622 EXPECT_FALSE(m.Matches(nan2_)); | |
| 2623 EXPECT_FALSE(m.Matches(1.0)); | |
| 2624 } | |
| 2625 | |
| 2626 TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) { | |
| 2627 // NanSensitiveDoubleEq will match NaN. | |
| 2628 Matcher<double> m = NanSensitiveDoubleEq(nan1_); | |
| 2629 EXPECT_TRUE(m.Matches(nan1_)); | |
| 2630 EXPECT_TRUE(m.Matches(nan2_)); | |
| 2631 EXPECT_FALSE(m.Matches(1.0)); | |
| 2632 } | |
| 2633 | |
| 2634 TEST_F(DoubleTest, DoubleEqCanDescribeSelf) { | |
| 2635 Matcher<double> m1 = DoubleEq(2.0); | |
| 2636 EXPECT_EQ("is approximately 2", Describe(m1)); | |
| 2637 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1)); | |
| 2638 | |
| 2639 Matcher<double> m2 = DoubleEq(0.5); | |
| 2640 EXPECT_EQ("is approximately 0.5", Describe(m2)); | |
| 2641 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2)); | |
| 2642 | |
| 2643 Matcher<double> m3 = DoubleEq(nan1_); | |
| 2644 EXPECT_EQ("never matches", Describe(m3)); | |
| 2645 EXPECT_EQ("is anything", DescribeNegation(m3)); | |
| 2646 } | |
| 2647 | |
| 2648 TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) { | |
| 2649 Matcher<double> m1 = NanSensitiveDoubleEq(2.0); | |
| 2650 EXPECT_EQ("is approximately 2", Describe(m1)); | |
| 2651 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1)); | |
| 2652 | |
| 2653 Matcher<double> m2 = NanSensitiveDoubleEq(0.5); | |
| 2654 EXPECT_EQ("is approximately 0.5", Describe(m2)); | |
| 2655 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2)); | |
| 2656 | |
| 2657 Matcher<double> m3 = NanSensitiveDoubleEq(nan1_); | |
| 2658 EXPECT_EQ("is NaN", Describe(m3)); | |
| 2659 EXPECT_EQ("isn't NaN", DescribeNegation(m3)); | |
| 2660 } | |
| 2661 | |
| 2662 TEST(PointeeTest, RawPointer) { | |
| 2663 const Matcher<int*> m = Pointee(Ge(0)); | |
| 2664 | |
| 2665 int n = 1; | |
| 2666 EXPECT_TRUE(m.Matches(&n)); | |
| 2667 n = -1; | |
| 2668 EXPECT_FALSE(m.Matches(&n)); | |
| 2669 EXPECT_FALSE(m.Matches(NULL)); | |
| 2670 } | |
| 2671 | |
| 2672 TEST(PointeeTest, RawPointerToConst) { | |
| 2673 const Matcher<const double*> m = Pointee(Ge(0)); | |
| 2674 | |
| 2675 double x = 1; | |
| 2676 EXPECT_TRUE(m.Matches(&x)); | |
| 2677 x = -1; | |
| 2678 EXPECT_FALSE(m.Matches(&x)); | |
| 2679 EXPECT_FALSE(m.Matches(NULL)); | |
| 2680 } | |
| 2681 | |
| 2682 TEST(PointeeTest, ReferenceToConstRawPointer) { | |
| 2683 const Matcher<int* const &> m = Pointee(Ge(0)); | |
| 2684 | |
| 2685 int n = 1; | |
| 2686 EXPECT_TRUE(m.Matches(&n)); | |
| 2687 n = -1; | |
| 2688 EXPECT_FALSE(m.Matches(&n)); | |
| 2689 EXPECT_FALSE(m.Matches(NULL)); | |
| 2690 } | |
| 2691 | |
| 2692 TEST(PointeeTest, ReferenceToNonConstRawPointer) { | |
| 2693 const Matcher<double* &> m = Pointee(Ge(0)); | |
| 2694 | |
| 2695 double x = 1.0; | |
| 2696 double* p = &x; | |
| 2697 EXPECT_TRUE(m.Matches(p)); | |
| 2698 x = -1; | |
| 2699 EXPECT_FALSE(m.Matches(p)); | |
| 2700 p = NULL; | |
| 2701 EXPECT_FALSE(m.Matches(p)); | |
| 2702 } | |
| 2703 | |
| 2704 TEST(PointeeTest, NeverMatchesNull) { | |
| 2705 const Matcher<const char*> m = Pointee(_); | |
| 2706 EXPECT_FALSE(m.Matches(NULL)); | |
| 2707 } | |
| 2708 | |
| 2709 // Tests that we can write Pointee(value) instead of Pointee(Eq(value)). | |
| 2710 TEST(PointeeTest, MatchesAgainstAValue) { | |
| 2711 const Matcher<int*> m = Pointee(5); | |
| 2712 | |
| 2713 int n = 5; | |
| 2714 EXPECT_TRUE(m.Matches(&n)); | |
| 2715 n = -1; | |
| 2716 EXPECT_FALSE(m.Matches(&n)); | |
| 2717 EXPECT_FALSE(m.Matches(NULL)); | |
| 2718 } | |
| 2719 | |
| 2720 TEST(PointeeTest, CanDescribeSelf) { | |
| 2721 const Matcher<int*> m = Pointee(Gt(3)); | |
| 2722 EXPECT_EQ("points to a value that is > 3", Describe(m)); | |
| 2723 EXPECT_EQ("does not point to a value that is > 3", | |
| 2724 DescribeNegation(m)); | |
| 2725 } | |
| 2726 | |
| 2727 TEST(PointeeTest, CanExplainMatchResult) { | |
| 2728 const Matcher<const string*> m = Pointee(StartsWith("Hi")); | |
| 2729 | |
| 2730 EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL))); | |
| 2731 | |
| 2732 const Matcher<int*> m2 = Pointee(GreaterThan(1)); | |
| 2733 int n = 3; | |
| 2734 EXPECT_EQ("which points to 3, which is 2 more than 1", | |
| 2735 Explain(m2, &n)); | |
| 2736 } | |
| 2737 | |
| 2738 TEST(PointeeTest, AlwaysExplainsPointee) { | |
| 2739 const Matcher<int*> m = Pointee(0); | |
| 2740 int n = 42; | |
| 2741 EXPECT_EQ("which points to 42", Explain(m, &n)); | |
| 2742 } | |
| 2743 | |
| 2744 // An uncopyable class. | |
| 2745 class Uncopyable { | |
| 2746 public: | |
| 2747 explicit Uncopyable(int a_value) : value_(a_value) {} | |
| 2748 | |
| 2749 int value() const { return value_; } | |
| 2750 private: | |
| 2751 const int value_; | |
| 2752 GTEST_DISALLOW_COPY_AND_ASSIGN_(Uncopyable); | |
| 2753 }; | |
| 2754 | |
| 2755 // Returns true iff x.value() is positive. | |
| 2756 bool ValueIsPositive(const Uncopyable& x) { return x.value() > 0; } | |
| 2757 | |
| 2758 // A user-defined struct for testing Field(). | |
| 2759 struct AStruct { | |
| 2760 AStruct() : x(0), y(1.0), z(5), p(NULL) {} | |
| 2761 AStruct(const AStruct& rhs) | |
| 2762 : x(rhs.x), y(rhs.y), z(rhs.z.value()), p(rhs.p) {} | |
| 2763 | |
| 2764 int x; // A non-const field. | |
| 2765 const double y; // A const field. | |
| 2766 Uncopyable z; // An uncopyable field. | |
| 2767 const char* p; // A pointer field. | |
| 2768 | |
| 2769 private: | |
| 2770 GTEST_DISALLOW_ASSIGN_(AStruct); | |
| 2771 }; | |
| 2772 | |
| 2773 // A derived struct for testing Field(). | |
| 2774 struct DerivedStruct : public AStruct { | |
| 2775 char ch; | |
| 2776 | |
| 2777 private: | |
| 2778 GTEST_DISALLOW_ASSIGN_(DerivedStruct); | |
| 2779 }; | |
| 2780 | |
| 2781 // Tests that Field(&Foo::field, ...) works when field is non-const. | |
| 2782 TEST(FieldTest, WorksForNonConstField) { | |
| 2783 Matcher<AStruct> m = Field(&AStruct::x, Ge(0)); | |
| 2784 | |
| 2785 AStruct a; | |
| 2786 EXPECT_TRUE(m.Matches(a)); | |
| 2787 a.x = -1; | |
| 2788 EXPECT_FALSE(m.Matches(a)); | |
| 2789 } | |
| 2790 | |
| 2791 // Tests that Field(&Foo::field, ...) works when field is const. | |
| 2792 TEST(FieldTest, WorksForConstField) { | |
| 2793 AStruct a; | |
| 2794 | |
| 2795 Matcher<AStruct> m = Field(&AStruct::y, Ge(0.0)); | |
| 2796 EXPECT_TRUE(m.Matches(a)); | |
| 2797 m = Field(&AStruct::y, Le(0.0)); | |
| 2798 EXPECT_FALSE(m.Matches(a)); | |
| 2799 } | |
| 2800 | |
| 2801 // Tests that Field(&Foo::field, ...) works when field is not copyable. | |
| 2802 TEST(FieldTest, WorksForUncopyableField) { | |
| 2803 AStruct a; | |
| 2804 | |
| 2805 Matcher<AStruct> m = Field(&AStruct::z, Truly(ValueIsPositive)); | |
| 2806 EXPECT_TRUE(m.Matches(a)); | |
| 2807 m = Field(&AStruct::z, Not(Truly(ValueIsPositive))); | |
| 2808 EXPECT_FALSE(m.Matches(a)); | |
| 2809 } | |
| 2810 | |
| 2811 // Tests that Field(&Foo::field, ...) works when field is a pointer. | |
| 2812 TEST(FieldTest, WorksForPointerField) { | |
| 2813 // Matching against NULL. | |
| 2814 Matcher<AStruct> m = Field(&AStruct::p, static_cast<const char*>(NULL)); | |
| 2815 AStruct a; | |
| 2816 EXPECT_TRUE(m.Matches(a)); | |
| 2817 a.p = "hi"; | |
| 2818 EXPECT_FALSE(m.Matches(a)); | |
| 2819 | |
| 2820 // Matching a pointer that is not NULL. | |
| 2821 m = Field(&AStruct::p, StartsWith("hi")); | |
| 2822 a.p = "hill"; | |
| 2823 EXPECT_TRUE(m.Matches(a)); | |
| 2824 a.p = "hole"; | |
| 2825 EXPECT_FALSE(m.Matches(a)); | |
| 2826 } | |
| 2827 | |
| 2828 // Tests that Field() works when the object is passed by reference. | |
| 2829 TEST(FieldTest, WorksForByRefArgument) { | |
| 2830 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0)); | |
| 2831 | |
| 2832 AStruct a; | |
| 2833 EXPECT_TRUE(m.Matches(a)); | |
| 2834 a.x = -1; | |
| 2835 EXPECT_FALSE(m.Matches(a)); | |
| 2836 } | |
| 2837 | |
| 2838 // Tests that Field(&Foo::field, ...) works when the argument's type | |
| 2839 // is a sub-type of Foo. | |
| 2840 TEST(FieldTest, WorksForArgumentOfSubType) { | |
| 2841 // Note that the matcher expects DerivedStruct but we say AStruct | |
| 2842 // inside Field(). | |
| 2843 Matcher<const DerivedStruct&> m = Field(&AStruct::x, Ge(0)); | |
| 2844 | |
| 2845 DerivedStruct d; | |
| 2846 EXPECT_TRUE(m.Matches(d)); | |
| 2847 d.x = -1; | |
| 2848 EXPECT_FALSE(m.Matches(d)); | |
| 2849 } | |
| 2850 | |
| 2851 // Tests that Field(&Foo::field, m) works when field's type and m's | |
| 2852 // argument type are compatible but not the same. | |
| 2853 TEST(FieldTest, WorksForCompatibleMatcherType) { | |
| 2854 // The field is an int, but the inner matcher expects a signed char. | |
| 2855 Matcher<const AStruct&> m = Field(&AStruct::x, | |
| 2856 Matcher<signed char>(Ge(0))); | |
| 2857 | |
| 2858 AStruct a; | |
| 2859 EXPECT_TRUE(m.Matches(a)); | |
| 2860 a.x = -1; | |
| 2861 EXPECT_FALSE(m.Matches(a)); | |
| 2862 } | |
| 2863 | |
| 2864 // Tests that Field() can describe itself. | |
| 2865 TEST(FieldTest, CanDescribeSelf) { | |
| 2866 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0)); | |
| 2867 | |
| 2868 EXPECT_EQ("is an object whose given field is >= 0", Describe(m)); | |
| 2869 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m)); | |
| 2870 } | |
| 2871 | |
| 2872 // Tests that Field() can explain the match result. | |
| 2873 TEST(FieldTest, CanExplainMatchResult) { | |
| 2874 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0)); | |
| 2875 | |
| 2876 AStruct a; | |
| 2877 a.x = 1; | |
| 2878 EXPECT_EQ("whose given field is 1", Explain(m, a)); | |
| 2879 | |
| 2880 m = Field(&AStruct::x, GreaterThan(0)); | |
| 2881 EXPECT_EQ("whose given field is 1, which is 1 more than 0", Explain(m, a)); | |
| 2882 } | |
| 2883 | |
| 2884 // Tests that Field() works when the argument is a pointer to const. | |
| 2885 TEST(FieldForPointerTest, WorksForPointerToConst) { | |
| 2886 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0)); | |
| 2887 | |
| 2888 AStruct a; | |
| 2889 EXPECT_TRUE(m.Matches(&a)); | |
| 2890 a.x = -1; | |
| 2891 EXPECT_FALSE(m.Matches(&a)); | |
| 2892 } | |
| 2893 | |
| 2894 // Tests that Field() works when the argument is a pointer to non-const. | |
| 2895 TEST(FieldForPointerTest, WorksForPointerToNonConst) { | |
| 2896 Matcher<AStruct*> m = Field(&AStruct::x, Ge(0)); | |
| 2897 | |
| 2898 AStruct a; | |
| 2899 EXPECT_TRUE(m.Matches(&a)); | |
| 2900 a.x = -1; | |
| 2901 EXPECT_FALSE(m.Matches(&a)); | |
| 2902 } | |
| 2903 | |
| 2904 // Tests that Field() works when the argument is a reference to a const pointer. | |
| 2905 TEST(FieldForPointerTest, WorksForReferenceToConstPointer) { | |
| 2906 Matcher<AStruct* const&> m = Field(&AStruct::x, Ge(0)); | |
| 2907 | |
| 2908 AStruct a; | |
| 2909 EXPECT_TRUE(m.Matches(&a)); | |
| 2910 a.x = -1; | |
| 2911 EXPECT_FALSE(m.Matches(&a)); | |
| 2912 } | |
| 2913 | |
| 2914 // Tests that Field() does not match the NULL pointer. | |
| 2915 TEST(FieldForPointerTest, DoesNotMatchNull) { | |
| 2916 Matcher<const AStruct*> m = Field(&AStruct::x, _); | |
| 2917 EXPECT_FALSE(m.Matches(NULL)); | |
| 2918 } | |
| 2919 | |
| 2920 // Tests that Field(&Foo::field, ...) works when the argument's type | |
| 2921 // is a sub-type of const Foo*. | |
| 2922 TEST(FieldForPointerTest, WorksForArgumentOfSubType) { | |
| 2923 // Note that the matcher expects DerivedStruct but we say AStruct | |
| 2924 // inside Field(). | |
| 2925 Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0)); | |
| 2926 | |
| 2927 DerivedStruct d; | |
| 2928 EXPECT_TRUE(m.Matches(&d)); | |
| 2929 d.x = -1; | |
| 2930 EXPECT_FALSE(m.Matches(&d)); | |
| 2931 } | |
| 2932 | |
| 2933 // Tests that Field() can describe itself when used to match a pointer. | |
| 2934 TEST(FieldForPointerTest, CanDescribeSelf) { | |
| 2935 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0)); | |
| 2936 | |
| 2937 EXPECT_EQ("is an object whose given field is >= 0", Describe(m)); | |
| 2938 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m)); | |
| 2939 } | |
| 2940 | |
| 2941 // Tests that Field() can explain the result of matching a pointer. | |
| 2942 TEST(FieldForPointerTest, CanExplainMatchResult) { | |
| 2943 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0)); | |
| 2944 | |
| 2945 AStruct a; | |
| 2946 a.x = 1; | |
| 2947 EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(NULL))); | |
| 2948 EXPECT_EQ("which points to an object whose given field is 1", Explain(m, &a)); | |
| 2949 | |
| 2950 m = Field(&AStruct::x, GreaterThan(0)); | |
| 2951 EXPECT_EQ("which points to an object whose given field is 1, " | |
| 2952 "which is 1 more than 0", Explain(m, &a)); | |
| 2953 } | |
| 2954 | |
| 2955 // A user-defined class for testing Property(). | |
| 2956 class AClass { | |
| 2957 public: | |
| 2958 AClass() : n_(0) {} | |
| 2959 | |
| 2960 // A getter that returns a non-reference. | |
| 2961 int n() const { return n_; } | |
| 2962 | |
| 2963 void set_n(int new_n) { n_ = new_n; } | |
| 2964 | |
| 2965 // A getter that returns a reference to const. | |
| 2966 const string& s() const { return s_; } | |
| 2967 | |
| 2968 void set_s(const string& new_s) { s_ = new_s; } | |
| 2969 | |
| 2970 // A getter that returns a reference to non-const. | |
| 2971 double& x() const { return x_; } | |
| 2972 private: | |
| 2973 int n_; | |
| 2974 string s_; | |
| 2975 | |
| 2976 static double x_; | |
| 2977 }; | |
| 2978 | |
| 2979 double AClass::x_ = 0.0; | |
| 2980 | |
| 2981 // A derived class for testing Property(). | |
| 2982 class DerivedClass : public AClass { | |
| 2983 private: | |
| 2984 int k_; | |
| 2985 }; | |
| 2986 | |
| 2987 // Tests that Property(&Foo::property, ...) works when property() | |
| 2988 // returns a non-reference. | |
| 2989 TEST(PropertyTest, WorksForNonReferenceProperty) { | |
| 2990 Matcher<const AClass&> m = Property(&AClass::n, Ge(0)); | |
| 2991 | |
| 2992 AClass a; | |
| 2993 a.set_n(1); | |
| 2994 EXPECT_TRUE(m.Matches(a)); | |
| 2995 | |
| 2996 a.set_n(-1); | |
| 2997 EXPECT_FALSE(m.Matches(a)); | |
| 2998 } | |
| 2999 | |
| 3000 // Tests that Property(&Foo::property, ...) works when property() | |
| 3001 // returns a reference to const. | |
| 3002 TEST(PropertyTest, WorksForReferenceToConstProperty) { | |
| 3003 Matcher<const AClass&> m = Property(&AClass::s, StartsWith("hi")); | |
| 3004 | |
| 3005 AClass a; | |
| 3006 a.set_s("hill"); | |
| 3007 EXPECT_TRUE(m.Matches(a)); | |
| 3008 | |
| 3009 a.set_s("hole"); | |
| 3010 EXPECT_FALSE(m.Matches(a)); | |
| 3011 } | |
| 3012 | |
| 3013 // Tests that Property(&Foo::property, ...) works when property() | |
| 3014 // returns a reference to non-const. | |
| 3015 TEST(PropertyTest, WorksForReferenceToNonConstProperty) { | |
| 3016 double x = 0.0; | |
| 3017 AClass a; | |
| 3018 | |
| 3019 Matcher<const AClass&> m = Property(&AClass::x, Ref(x)); | |
| 3020 EXPECT_FALSE(m.Matches(a)); | |
| 3021 | |
| 3022 m = Property(&AClass::x, Not(Ref(x))); | |
| 3023 EXPECT_TRUE(m.Matches(a)); | |
| 3024 } | |
| 3025 | |
| 3026 // Tests that Property(&Foo::property, ...) works when the argument is | |
| 3027 // passed by value. | |
| 3028 TEST(PropertyTest, WorksForByValueArgument) { | |
| 3029 Matcher<AClass> m = Property(&AClass::s, StartsWith("hi")); | |
| 3030 | |
| 3031 AClass a; | |
| 3032 a.set_s("hill"); | |
| 3033 EXPECT_TRUE(m.Matches(a)); | |
| 3034 | |
| 3035 a.set_s("hole"); | |
| 3036 EXPECT_FALSE(m.Matches(a)); | |
| 3037 } | |
| 3038 | |
| 3039 // Tests that Property(&Foo::property, ...) works when the argument's | |
| 3040 // type is a sub-type of Foo. | |
| 3041 TEST(PropertyTest, WorksForArgumentOfSubType) { | |
| 3042 // The matcher expects a DerivedClass, but inside the Property() we | |
| 3043 // say AClass. | |
| 3044 Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0)); | |
| 3045 | |
| 3046 DerivedClass d; | |
| 3047 d.set_n(1); | |
| 3048 EXPECT_TRUE(m.Matches(d)); | |
| 3049 | |
| 3050 d.set_n(-1); | |
| 3051 EXPECT_FALSE(m.Matches(d)); | |
| 3052 } | |
| 3053 | |
| 3054 // Tests that Property(&Foo::property, m) works when property()'s type | |
| 3055 // and m's argument type are compatible but different. | |
| 3056 TEST(PropertyTest, WorksForCompatibleMatcherType) { | |
| 3057 // n() returns an int but the inner matcher expects a signed char. | |
| 3058 Matcher<const AClass&> m = Property(&AClass::n, | |
| 3059 Matcher<signed char>(Ge(0))); | |
| 3060 | |
| 3061 AClass a; | |
| 3062 EXPECT_TRUE(m.Matches(a)); | |
| 3063 a.set_n(-1); | |
| 3064 EXPECT_FALSE(m.Matches(a)); | |
| 3065 } | |
| 3066 | |
| 3067 // Tests that Property() can describe itself. | |
| 3068 TEST(PropertyTest, CanDescribeSelf) { | |
| 3069 Matcher<const AClass&> m = Property(&AClass::n, Ge(0)); | |
| 3070 | |
| 3071 EXPECT_EQ("is an object whose given property is >= 0", Describe(m)); | |
| 3072 EXPECT_EQ("is an object whose given property isn't >= 0", | |
| 3073 DescribeNegation(m)); | |
| 3074 } | |
| 3075 | |
| 3076 // Tests that Property() can explain the match result. | |
| 3077 TEST(PropertyTest, CanExplainMatchResult) { | |
| 3078 Matcher<const AClass&> m = Property(&AClass::n, Ge(0)); | |
| 3079 | |
| 3080 AClass a; | |
| 3081 a.set_n(1); | |
| 3082 EXPECT_EQ("whose given property is 1", Explain(m, a)); | |
| 3083 | |
| 3084 m = Property(&AClass::n, GreaterThan(0)); | |
| 3085 EXPECT_EQ("whose given property is 1, which is 1 more than 0", Explain(m, a)); | |
| 3086 } | |
| 3087 | |
| 3088 // Tests that Property() works when the argument is a pointer to const. | |
| 3089 TEST(PropertyForPointerTest, WorksForPointerToConst) { | |
| 3090 Matcher<const AClass*> m = Property(&AClass::n, Ge(0)); | |
| 3091 | |
| 3092 AClass a; | |
| 3093 a.set_n(1); | |
| 3094 EXPECT_TRUE(m.Matches(&a)); | |
| 3095 | |
| 3096 a.set_n(-1); | |
| 3097 EXPECT_FALSE(m.Matches(&a)); | |
| 3098 } | |
| 3099 | |
| 3100 // Tests that Property() works when the argument is a pointer to non-const. | |
| 3101 TEST(PropertyForPointerTest, WorksForPointerToNonConst) { | |
| 3102 Matcher<AClass*> m = Property(&AClass::s, StartsWith("hi")); | |
| 3103 | |
| 3104 AClass a; | |
| 3105 a.set_s("hill"); | |
| 3106 EXPECT_TRUE(m.Matches(&a)); | |
| 3107 | |
| 3108 a.set_s("hole"); | |
| 3109 EXPECT_FALSE(m.Matches(&a)); | |
| 3110 } | |
| 3111 | |
| 3112 // Tests that Property() works when the argument is a reference to a | |
| 3113 // const pointer. | |
| 3114 TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) { | |
| 3115 Matcher<AClass* const&> m = Property(&AClass::s, StartsWith("hi")); | |
| 3116 | |
| 3117 AClass a; | |
| 3118 a.set_s("hill"); | |
| 3119 EXPECT_TRUE(m.Matches(&a)); | |
| 3120 | |
| 3121 a.set_s("hole"); | |
| 3122 EXPECT_FALSE(m.Matches(&a)); | |
| 3123 } | |
| 3124 | |
| 3125 // Tests that Property() does not match the NULL pointer. | |
| 3126 TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) { | |
| 3127 Matcher<const AClass*> m = Property(&AClass::x, _); | |
| 3128 EXPECT_FALSE(m.Matches(NULL)); | |
| 3129 } | |
| 3130 | |
| 3131 // Tests that Property(&Foo::property, ...) works when the argument's | |
| 3132 // type is a sub-type of const Foo*. | |
| 3133 TEST(PropertyForPointerTest, WorksForArgumentOfSubType) { | |
| 3134 // The matcher expects a DerivedClass, but inside the Property() we | |
| 3135 // say AClass. | |
| 3136 Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0)); | |
| 3137 | |
| 3138 DerivedClass d; | |
| 3139 d.set_n(1); | |
| 3140 EXPECT_TRUE(m.Matches(&d)); | |
| 3141 | |
| 3142 d.set_n(-1); | |
| 3143 EXPECT_FALSE(m.Matches(&d)); | |
| 3144 } | |
| 3145 | |
| 3146 // Tests that Property() can describe itself when used to match a pointer. | |
| 3147 TEST(PropertyForPointerTest, CanDescribeSelf) { | |
| 3148 Matcher<const AClass*> m = Property(&AClass::n, Ge(0)); | |
| 3149 | |
| 3150 EXPECT_EQ("is an object whose given property is >= 0", Describe(m)); | |
| 3151 EXPECT_EQ("is an object whose given property isn't >= 0", | |
| 3152 DescribeNegation(m)); | |
| 3153 } | |
| 3154 | |
| 3155 // Tests that Property() can explain the result of matching a pointer. | |
| 3156 TEST(PropertyForPointerTest, CanExplainMatchResult) { | |
| 3157 Matcher<const AClass*> m = Property(&AClass::n, Ge(0)); | |
| 3158 | |
| 3159 AClass a; | |
| 3160 a.set_n(1); | |
| 3161 EXPECT_EQ("", Explain(m, static_cast<const AClass*>(NULL))); | |
| 3162 EXPECT_EQ("which points to an object whose given property is 1", | |
| 3163 Explain(m, &a)); | |
| 3164 | |
| 3165 m = Property(&AClass::n, GreaterThan(0)); | |
| 3166 EXPECT_EQ("which points to an object whose given property is 1, " | |
| 3167 "which is 1 more than 0", Explain(m, &a)); | |
| 3168 } | |
| 3169 | |
| 3170 // Tests ResultOf. | |
| 3171 | |
| 3172 // Tests that ResultOf(f, ...) compiles and works as expected when f is a | |
| 3173 // function pointer. | |
| 3174 string IntToStringFunction(int input) { return input == 1 ? "foo" : "bar"; } | |
| 3175 | |
| 3176 TEST(ResultOfTest, WorksForFunctionPointers) { | |
| 3177 Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(string("foo"))); | |
| 3178 | |
| 3179 EXPECT_TRUE(matcher.Matches(1)); | |
| 3180 EXPECT_FALSE(matcher.Matches(2)); | |
| 3181 } | |
| 3182 | |
| 3183 // Tests that ResultOf() can describe itself. | |
| 3184 TEST(ResultOfTest, CanDescribeItself) { | |
| 3185 Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo")); | |
| 3186 | |
| 3187 EXPECT_EQ("is mapped by the given callable to a value that " | |
| 3188 "is equal to \"foo\"", Describe(matcher)); | |
| 3189 EXPECT_EQ("is mapped by the given callable to a value that " | |
| 3190 "isn't equal to \"foo\"", DescribeNegation(matcher)); | |
| 3191 } | |
| 3192 | |
| 3193 // Tests that ResultOf() can explain the match result. | |
| 3194 int IntFunction(int input) { return input == 42 ? 80 : 90; } | |
| 3195 | |
| 3196 TEST(ResultOfTest, CanExplainMatchResult) { | |
| 3197 Matcher<int> matcher = ResultOf(&IntFunction, Ge(85)); | |
| 3198 EXPECT_EQ("which is mapped by the given callable to 90", | |
| 3199 Explain(matcher, 36)); | |
| 3200 | |
| 3201 matcher = ResultOf(&IntFunction, GreaterThan(85)); | |
| 3202 EXPECT_EQ("which is mapped by the given callable to 90, " | |
| 3203 "which is 5 more than 85", Explain(matcher, 36)); | |
| 3204 } | |
| 3205 | |
| 3206 // Tests that ResultOf(f, ...) compiles and works as expected when f(x) | |
| 3207 // returns a non-reference. | |
| 3208 TEST(ResultOfTest, WorksForNonReferenceResults) { | |
| 3209 Matcher<int> matcher = ResultOf(&IntFunction, Eq(80)); | |
| 3210 | |
| 3211 EXPECT_TRUE(matcher.Matches(42)); | |
| 3212 EXPECT_FALSE(matcher.Matches(36)); | |
| 3213 } | |
| 3214 | |
| 3215 // Tests that ResultOf(f, ...) compiles and works as expected when f(x) | |
| 3216 // returns a reference to non-const. | |
| 3217 double& DoubleFunction(double& input) { return input; } | |
| 3218 | |
| 3219 Uncopyable& RefUncopyableFunction(Uncopyable& obj) { | |
| 3220 return obj; | |
| 3221 } | |
| 3222 | |
| 3223 TEST(ResultOfTest, WorksForReferenceToNonConstResults) { | |
| 3224 double x = 3.14; | |
| 3225 double x2 = x; | |
| 3226 Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(x)); | |
| 3227 | |
| 3228 EXPECT_TRUE(matcher.Matches(x)); | |
| 3229 EXPECT_FALSE(matcher.Matches(x2)); | |
| 3230 | |
| 3231 // Test that ResultOf works with uncopyable objects | |
| 3232 Uncopyable obj(0); | |
| 3233 Uncopyable obj2(0); | |
| 3234 Matcher<Uncopyable&> matcher2 = | |
| 3235 ResultOf(&RefUncopyableFunction, Ref(obj)); | |
| 3236 | |
| 3237 EXPECT_TRUE(matcher2.Matches(obj)); | |
| 3238 EXPECT_FALSE(matcher2.Matches(obj2)); | |
| 3239 } | |
| 3240 | |
| 3241 // Tests that ResultOf(f, ...) compiles and works as expected when f(x) | |
| 3242 // returns a reference to const. | |
| 3243 const string& StringFunction(const string& input) { return input; } | |
| 3244 | |
| 3245 TEST(ResultOfTest, WorksForReferenceToConstResults) { | |
| 3246 string s = "foo"; | |
| 3247 string s2 = s; | |
| 3248 Matcher<const string&> matcher = ResultOf(&StringFunction, Ref(s)); | |
| 3249 | |
| 3250 EXPECT_TRUE(matcher.Matches(s)); | |
| 3251 EXPECT_FALSE(matcher.Matches(s2)); | |
| 3252 } | |
| 3253 | |
| 3254 // Tests that ResultOf(f, m) works when f(x) and m's | |
| 3255 // argument types are compatible but different. | |
| 3256 TEST(ResultOfTest, WorksForCompatibleMatcherTypes) { | |
| 3257 // IntFunction() returns int but the inner matcher expects a signed char. | |
| 3258 Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85))); | |
| 3259 | |
| 3260 EXPECT_TRUE(matcher.Matches(36)); | |
| 3261 EXPECT_FALSE(matcher.Matches(42)); | |
| 3262 } | |
| 3263 | |
| 3264 // Tests that the program aborts when ResultOf is passed | |
| 3265 // a NULL function pointer. | |
| 3266 TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) { | |
| 3267 EXPECT_DEATH_IF_SUPPORTED( | |
| 3268 ResultOf(static_cast<string(*)(int)>(NULL), Eq(string("foo"))), | |
| 3269 "NULL function pointer is passed into ResultOf\\(\\)\\."); | |
| 3270 } | |
| 3271 | |
| 3272 // Tests that ResultOf(f, ...) compiles and works as expected when f is a | |
| 3273 // function reference. | |
| 3274 TEST(ResultOfTest, WorksForFunctionReferences) { | |
| 3275 Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq("foo")); | |
| 3276 EXPECT_TRUE(matcher.Matches(1)); | |
| 3277 EXPECT_FALSE(matcher.Matches(2)); | |
| 3278 } | |
| 3279 | |
| 3280 // Tests that ResultOf(f, ...) compiles and works as expected when f is a | |
| 3281 // function object. | |
| 3282 struct Functor : public ::std::unary_function<int, string> { | |
| 3283 result_type operator()(argument_type input) const { | |
| 3284 return IntToStringFunction(input); | |
| 3285 } | |
| 3286 }; | |
| 3287 | |
| 3288 TEST(ResultOfTest, WorksForFunctors) { | |
| 3289 Matcher<int> matcher = ResultOf(Functor(), Eq(string("foo"))); | |
| 3290 | |
| 3291 EXPECT_TRUE(matcher.Matches(1)); | |
| 3292 EXPECT_FALSE(matcher.Matches(2)); | |
| 3293 } | |
| 3294 | |
| 3295 // Tests that ResultOf(f, ...) compiles and works as expected when f is a | |
| 3296 // functor with more then one operator() defined. ResultOf() must work | |
| 3297 // for each defined operator(). | |
| 3298 struct PolymorphicFunctor { | |
| 3299 typedef int result_type; | |
| 3300 int operator()(int n) { return n; } | |
| 3301 int operator()(const char* s) { return static_cast<int>(strlen(s)); } | |
| 3302 }; | |
| 3303 | |
| 3304 TEST(ResultOfTest, WorksForPolymorphicFunctors) { | |
| 3305 Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5)); | |
| 3306 | |
| 3307 EXPECT_TRUE(matcher_int.Matches(10)); | |
| 3308 EXPECT_FALSE(matcher_int.Matches(2)); | |
| 3309 | |
| 3310 Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5)); | |
| 3311 | |
| 3312 EXPECT_TRUE(matcher_string.Matches("long string")); | |
| 3313 EXPECT_FALSE(matcher_string.Matches("shrt")); | |
| 3314 } | |
| 3315 | |
| 3316 const int* ReferencingFunction(const int& n) { return &n; } | |
| 3317 | |
| 3318 struct ReferencingFunctor { | |
| 3319 typedef const int* result_type; | |
| 3320 result_type operator()(const int& n) { return &n; } | |
| 3321 }; | |
| 3322 | |
| 3323 TEST(ResultOfTest, WorksForReferencingCallables) { | |
| 3324 const int n = 1; | |
| 3325 const int n2 = 1; | |
| 3326 Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n)); | |
| 3327 EXPECT_TRUE(matcher2.Matches(n)); | |
| 3328 EXPECT_FALSE(matcher2.Matches(n2)); | |
| 3329 | |
| 3330 Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n)); | |
| 3331 EXPECT_TRUE(matcher3.Matches(n)); | |
| 3332 EXPECT_FALSE(matcher3.Matches(n2)); | |
| 3333 } | |
| 3334 | |
| 3335 class DivisibleByImpl { | |
| 3336 public: | |
| 3337 explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {} | |
| 3338 | |
| 3339 // For testing using ExplainMatchResultTo() with polymorphic matchers. | |
| 3340 template <typename T> | |
| 3341 bool MatchAndExplain(const T& n, MatchResultListener* listener) const { | |
| 3342 *listener << "which is " << (n % divider_) << " modulo " | |
| 3343 << divider_; | |
| 3344 return (n % divider_) == 0; | |
| 3345 } | |
| 3346 | |
| 3347 void DescribeTo(ostream* os) const { | |
| 3348 *os << "is divisible by " << divider_; | |
| 3349 } | |
| 3350 | |
| 3351 void DescribeNegationTo(ostream* os) const { | |
| 3352 *os << "is not divisible by " << divider_; | |
| 3353 } | |
| 3354 | |
| 3355 void set_divider(int a_divider) { divider_ = a_divider; } | |
| 3356 int divider() const { return divider_; } | |
| 3357 | |
| 3358 private: | |
| 3359 int divider_; | |
| 3360 }; | |
| 3361 | |
| 3362 PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) { | |
| 3363 return MakePolymorphicMatcher(DivisibleByImpl(n)); | |
| 3364 } | |
| 3365 | |
| 3366 // Tests that when AllOf() fails, only the first failing matcher is | |
| 3367 // asked to explain why. | |
| 3368 TEST(ExplainMatchResultTest, AllOf_False_False) { | |
| 3369 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3)); | |
| 3370 EXPECT_EQ("which is 1 modulo 4", Explain(m, 5)); | |
| 3371 } | |
| 3372 | |
| 3373 // Tests that when AllOf() fails, only the first failing matcher is | |
| 3374 // asked to explain why. | |
| 3375 TEST(ExplainMatchResultTest, AllOf_False_True) { | |
| 3376 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3)); | |
| 3377 EXPECT_EQ("which is 2 modulo 4", Explain(m, 6)); | |
| 3378 } | |
| 3379 | |
| 3380 // Tests that when AllOf() fails, only the first failing matcher is | |
| 3381 // asked to explain why. | |
| 3382 TEST(ExplainMatchResultTest, AllOf_True_False) { | |
| 3383 const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3)); | |
| 3384 EXPECT_EQ("which is 2 modulo 3", Explain(m, 5)); | |
| 3385 } | |
| 3386 | |
| 3387 // Tests that when AllOf() succeeds, all matchers are asked to explain | |
| 3388 // why. | |
| 3389 TEST(ExplainMatchResultTest, AllOf_True_True) { | |
| 3390 const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3)); | |
| 3391 EXPECT_EQ("which is 0 modulo 2, and which is 0 modulo 3", Explain(m, 6)); | |
| 3392 } | |
| 3393 | |
| 3394 TEST(ExplainMatchResultTest, AllOf_True_True_2) { | |
| 3395 const Matcher<int> m = AllOf(Ge(2), Le(3)); | |
| 3396 EXPECT_EQ("", Explain(m, 2)); | |
| 3397 } | |
| 3398 | |
| 3399 TEST(ExplainmatcherResultTest, MonomorphicMatcher) { | |
| 3400 const Matcher<int> m = GreaterThan(5); | |
| 3401 EXPECT_EQ("which is 1 more than 5", Explain(m, 6)); | |
| 3402 } | |
| 3403 | |
| 3404 // The following two tests verify that values without a public copy | |
| 3405 // ctor can be used as arguments to matchers like Eq(), Ge(), and etc | |
| 3406 // with the help of ByRef(). | |
| 3407 | |
| 3408 class NotCopyable { | |
| 3409 public: | |
| 3410 explicit NotCopyable(int a_value) : value_(a_value) {} | |
| 3411 | |
| 3412 int value() const { return value_; } | |
| 3413 | |
| 3414 bool operator==(const NotCopyable& rhs) const { | |
| 3415 return value() == rhs.value(); | |
| 3416 } | |
| 3417 | |
| 3418 bool operator>=(const NotCopyable& rhs) const { | |
| 3419 return value() >= rhs.value(); | |
| 3420 } | |
| 3421 private: | |
| 3422 int value_; | |
| 3423 | |
| 3424 GTEST_DISALLOW_COPY_AND_ASSIGN_(NotCopyable); | |
| 3425 }; | |
| 3426 | |
| 3427 TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) { | |
| 3428 const NotCopyable const_value1(1); | |
| 3429 const Matcher<const NotCopyable&> m = Eq(ByRef(const_value1)); | |
| 3430 | |
| 3431 const NotCopyable n1(1), n2(2); | |
| 3432 EXPECT_TRUE(m.Matches(n1)); | |
| 3433 EXPECT_FALSE(m.Matches(n2)); | |
| 3434 } | |
| 3435 | |
| 3436 TEST(ByRefTest, AllowsNotCopyableValueInMatchers) { | |
| 3437 NotCopyable value2(2); | |
| 3438 const Matcher<NotCopyable&> m = Ge(ByRef(value2)); | |
| 3439 | |
| 3440 NotCopyable n1(1), n2(2); | |
| 3441 EXPECT_FALSE(m.Matches(n1)); | |
| 3442 EXPECT_TRUE(m.Matches(n2)); | |
| 3443 } | |
| 3444 | |
| 3445 #if GTEST_HAS_TYPED_TEST | |
| 3446 // Tests ContainerEq with different container types, and | |
| 3447 // different element types. | |
| 3448 | |
| 3449 template <typename T> | |
| 3450 class ContainerEqTest : public testing::Test {}; | |
| 3451 | |
| 3452 typedef testing::Types< | |
| 3453 set<int>, | |
| 3454 vector<size_t>, | |
| 3455 multiset<size_t>, | |
| 3456 list<int> > | |
| 3457 ContainerEqTestTypes; | |
| 3458 | |
| 3459 TYPED_TEST_CASE(ContainerEqTest, ContainerEqTestTypes); | |
| 3460 | |
| 3461 // Tests that the filled container is equal to itself. | |
| 3462 TYPED_TEST(ContainerEqTest, EqualsSelf) { | |
| 3463 static const int vals[] = {1, 1, 2, 3, 5, 8}; | |
| 3464 TypeParam my_set(vals, vals + 6); | |
| 3465 const Matcher<TypeParam> m = ContainerEq(my_set); | |
| 3466 EXPECT_TRUE(m.Matches(my_set)); | |
| 3467 EXPECT_EQ("", Explain(m, my_set)); | |
| 3468 } | |
| 3469 | |
| 3470 // Tests that missing values are reported. | |
| 3471 TYPED_TEST(ContainerEqTest, ValueMissing) { | |
| 3472 static const int vals[] = {1, 1, 2, 3, 5, 8}; | |
| 3473 static const int test_vals[] = {2, 1, 8, 5}; | |
| 3474 TypeParam my_set(vals, vals + 6); | |
| 3475 TypeParam test_set(test_vals, test_vals + 4); | |
| 3476 const Matcher<TypeParam> m = ContainerEq(my_set); | |
| 3477 EXPECT_FALSE(m.Matches(test_set)); | |
| 3478 EXPECT_EQ("which doesn't have these expected elements: 3", | |
| 3479 Explain(m, test_set)); | |
| 3480 } | |
| 3481 | |
| 3482 // Tests that added values are reported. | |
| 3483 TYPED_TEST(ContainerEqTest, ValueAdded) { | |
| 3484 static const int vals[] = {1, 1, 2, 3, 5, 8}; | |
| 3485 static const int test_vals[] = {1, 2, 3, 5, 8, 46}; | |
| 3486 TypeParam my_set(vals, vals + 6); | |
| 3487 TypeParam test_set(test_vals, test_vals + 6); | |
| 3488 const Matcher<const TypeParam&> m = ContainerEq(my_set); | |
| 3489 EXPECT_FALSE(m.Matches(test_set)); | |
| 3490 EXPECT_EQ("which has these unexpected elements: 46", Explain(m, test_set)); | |
| 3491 } | |
| 3492 | |
| 3493 // Tests that added and missing values are reported together. | |
| 3494 TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) { | |
| 3495 static const int vals[] = {1, 1, 2, 3, 5, 8}; | |
| 3496 static const int test_vals[] = {1, 2, 3, 8, 46}; | |
| 3497 TypeParam my_set(vals, vals + 6); | |
| 3498 TypeParam test_set(test_vals, test_vals + 5); | |
| 3499 const Matcher<TypeParam> m = ContainerEq(my_set); | |
| 3500 EXPECT_FALSE(m.Matches(test_set)); | |
| 3501 EXPECT_EQ("which has these unexpected elements: 46,\n" | |
| 3502 "and doesn't have these expected elements: 5", | |
| 3503 Explain(m, test_set)); | |
| 3504 } | |
| 3505 | |
| 3506 // Tests duplicated value -- expect no explanation. | |
| 3507 TYPED_TEST(ContainerEqTest, DuplicateDifference) { | |
| 3508 static const int vals[] = {1, 1, 2, 3, 5, 8}; | |
| 3509 static const int test_vals[] = {1, 2, 3, 5, 8}; | |
| 3510 TypeParam my_set(vals, vals + 6); | |
| 3511 TypeParam test_set(test_vals, test_vals + 5); | |
| 3512 const Matcher<const TypeParam&> m = ContainerEq(my_set); | |
| 3513 // Depending on the container, match may be true or false | |
| 3514 // But in any case there should be no explanation. | |
| 3515 EXPECT_EQ("", Explain(m, test_set)); | |
| 3516 } | |
| 3517 #endif // GTEST_HAS_TYPED_TEST | |
| 3518 | |
| 3519 // Tests that mutliple missing values are reported. | |
| 3520 // Using just vector here, so order is predicatble. | |
| 3521 TEST(ContainerEqExtraTest, MultipleValuesMissing) { | |
| 3522 static const int vals[] = {1, 1, 2, 3, 5, 8}; | |
| 3523 static const int test_vals[] = {2, 1, 5}; | |
| 3524 vector<int> my_set(vals, vals + 6); | |
| 3525 vector<int> test_set(test_vals, test_vals + 3); | |
| 3526 const Matcher<vector<int> > m = ContainerEq(my_set); | |
| 3527 EXPECT_FALSE(m.Matches(test_set)); | |
| 3528 EXPECT_EQ("which doesn't have these expected elements: 3, 8", | |
| 3529 Explain(m, test_set)); | |
| 3530 } | |
| 3531 | |
| 3532 // Tests that added values are reported. | |
| 3533 // Using just vector here, so order is predicatble. | |
| 3534 TEST(ContainerEqExtraTest, MultipleValuesAdded) { | |
| 3535 static const int vals[] = {1, 1, 2, 3, 5, 8}; | |
| 3536 static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46}; | |
| 3537 list<size_t> my_set(vals, vals + 6); | |
| 3538 list<size_t> test_set(test_vals, test_vals + 7); | |
| 3539 const Matcher<const list<size_t>&> m = ContainerEq(my_set); | |
| 3540 EXPECT_FALSE(m.Matches(test_set)); | |
| 3541 EXPECT_EQ("which has these unexpected elements: 92, 46", | |
| 3542 Explain(m, test_set)); | |
| 3543 } | |
| 3544 | |
| 3545 // Tests that added and missing values are reported together. | |
| 3546 TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) { | |
| 3547 static const int vals[] = {1, 1, 2, 3, 5, 8}; | |
| 3548 static const int test_vals[] = {1, 2, 3, 92, 46}; | |
| 3549 list<size_t> my_set(vals, vals + 6); | |
| 3550 list<size_t> test_set(test_vals, test_vals + 5); | |
| 3551 const Matcher<const list<size_t> > m = ContainerEq(my_set); | |
| 3552 EXPECT_FALSE(m.Matches(test_set)); | |
| 3553 EXPECT_EQ("which has these unexpected elements: 92, 46,\n" | |
| 3554 "and doesn't have these expected elements: 5, 8", | |
| 3555 Explain(m, test_set)); | |
| 3556 } | |
| 3557 | |
| 3558 // Tests to see that duplicate elements are detected, | |
| 3559 // but (as above) not reported in the explanation. | |
| 3560 TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) { | |
| 3561 static const int vals[] = {1, 1, 2, 3, 5, 8}; | |
| 3562 static const int test_vals[] = {1, 2, 3, 5, 8}; | |
| 3563 vector<int> my_set(vals, vals + 6); | |
| 3564 vector<int> test_set(test_vals, test_vals + 5); | |
| 3565 const Matcher<vector<int> > m = ContainerEq(my_set); | |
| 3566 EXPECT_TRUE(m.Matches(my_set)); | |
| 3567 EXPECT_FALSE(m.Matches(test_set)); | |
| 3568 // There is nothing to report when both sets contain all the same values. | |
| 3569 EXPECT_EQ("", Explain(m, test_set)); | |
| 3570 } | |
| 3571 | |
| 3572 // Tests that ContainerEq works for non-trivial associative containers, | |
| 3573 // like maps. | |
| 3574 TEST(ContainerEqExtraTest, WorksForMaps) { | |
| 3575 map<int, std::string> my_map; | |
| 3576 my_map[0] = "a"; | |
| 3577 my_map[1] = "b"; | |
| 3578 | |
| 3579 map<int, std::string> test_map; | |
| 3580 test_map[0] = "aa"; | |
| 3581 test_map[1] = "b"; | |
| 3582 | |
| 3583 const Matcher<const map<int, std::string>&> m = ContainerEq(my_map); | |
| 3584 EXPECT_TRUE(m.Matches(my_map)); | |
| 3585 EXPECT_FALSE(m.Matches(test_map)); | |
| 3586 | |
| 3587 EXPECT_EQ("which has these unexpected elements: (0, \"aa\"),\n" | |
| 3588 "and doesn't have these expected elements: (0, \"a\")", | |
| 3589 Explain(m, test_map)); | |
| 3590 } | |
| 3591 | |
| 3592 TEST(ContainerEqExtraTest, WorksForNativeArray) { | |
| 3593 int a1[] = { 1, 2, 3 }; | |
| 3594 int a2[] = { 1, 2, 3 }; | |
| 3595 int b[] = { 1, 2, 4 }; | |
| 3596 | |
| 3597 EXPECT_THAT(a1, ContainerEq(a2)); | |
| 3598 EXPECT_THAT(a1, Not(ContainerEq(b))); | |
| 3599 } | |
| 3600 | |
| 3601 TEST(ContainerEqExtraTest, WorksForTwoDimensionalNativeArray) { | |
| 3602 const char a1[][3] = { "hi", "lo" }; | |
| 3603 const char a2[][3] = { "hi", "lo" }; | |
| 3604 const char b[][3] = { "lo", "hi" }; | |
| 3605 | |
| 3606 // Tests using ContainerEq() in the first dimension. | |
| 3607 EXPECT_THAT(a1, ContainerEq(a2)); | |
| 3608 EXPECT_THAT(a1, Not(ContainerEq(b))); | |
| 3609 | |
| 3610 // Tests using ContainerEq() in the second dimension. | |
| 3611 EXPECT_THAT(a1, ElementsAre(ContainerEq(a2[0]), ContainerEq(a2[1]))); | |
| 3612 EXPECT_THAT(a1, ElementsAre(Not(ContainerEq(b[0])), ContainerEq(a2[1]))); | |
| 3613 } | |
| 3614 | |
| 3615 TEST(ContainerEqExtraTest, WorksForNativeArrayAsTuple) { | |
| 3616 const int a1[] = { 1, 2, 3 }; | |
| 3617 const int a2[] = { 1, 2, 3 }; | |
| 3618 const int b[] = { 1, 2, 3, 4 }; | |
| 3619 | |
| 3620 const int* const p1 = a1; | |
| 3621 EXPECT_THAT(make_tuple(p1, 3), ContainerEq(a2)); | |
| 3622 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(b))); | |
| 3623 | |
| 3624 const int c[] = { 1, 3, 2 }; | |
| 3625 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(c))); | |
| 3626 } | |
| 3627 | |
| 3628 TEST(ContainerEqExtraTest, CopiesNativeArrayParameter) { | |
| 3629 std::string a1[][3] = { | |
| 3630 { "hi", "hello", "ciao" }, | |
| 3631 { "bye", "see you", "ciao" } | |
| 3632 }; | |
| 3633 | |
| 3634 std::string a2[][3] = { | |
| 3635 { "hi", "hello", "ciao" }, | |
| 3636 { "bye", "see you", "ciao" } | |
| 3637 }; | |
| 3638 | |
| 3639 const Matcher<const std::string(&)[2][3]> m = ContainerEq(a2); | |
| 3640 EXPECT_THAT(a1, m); | |
| 3641 | |
| 3642 a2[0][0] = "ha"; | |
| 3643 EXPECT_THAT(a1, m); | |
| 3644 } | |
| 3645 | |
| 3646 // Tests GetParamIndex(). | |
| 3647 | |
| 3648 TEST(GetParamIndexTest, WorksForEmptyParamList) { | |
| 3649 const char* params[] = { NULL }; | |
| 3650 EXPECT_EQ(kTupleInterpolation, GetParamIndex(params, "*")); | |
| 3651 EXPECT_EQ(kInvalidInterpolation, GetParamIndex(params, "a")); | |
| 3652 } | |
| 3653 | |
| 3654 TEST(GetParamIndexTest, RecognizesStar) { | |
| 3655 const char* params[] = { "a", "b", NULL }; | |
| 3656 EXPECT_EQ(kTupleInterpolation, GetParamIndex(params, "*")); | |
| 3657 } | |
| 3658 | |
| 3659 TEST(GetParamIndexTest, RecognizesKnownParam) { | |
| 3660 const char* params[] = { "foo", "bar", NULL }; | |
| 3661 EXPECT_EQ(0, GetParamIndex(params, "foo")); | |
| 3662 EXPECT_EQ(1, GetParamIndex(params, "bar")); | |
| 3663 } | |
| 3664 | |
| 3665 TEST(GetParamIndexTest, RejectsUnknownParam) { | |
| 3666 const char* params[] = { "foo", "bar", NULL }; | |
| 3667 EXPECT_EQ(kInvalidInterpolation, GetParamIndex(params, "foobar")); | |
| 3668 } | |
| 3669 | |
| 3670 // Tests SkipPrefix(). | |
| 3671 | |
| 3672 TEST(SkipPrefixTest, SkipsWhenPrefixMatches) { | |
| 3673 const char* const str = "hello"; | |
| 3674 | |
| 3675 const char* p = str; | |
| 3676 EXPECT_TRUE(SkipPrefix("", &p)); | |
| 3677 EXPECT_EQ(str, p); | |
| 3678 | |
| 3679 p = str; | |
| 3680 EXPECT_TRUE(SkipPrefix("hell", &p)); | |
| 3681 EXPECT_EQ(str + 4, p); | |
| 3682 } | |
| 3683 | |
| 3684 TEST(SkipPrefixTest, DoesNotSkipWhenPrefixDoesNotMatch) { | |
| 3685 const char* const str = "world"; | |
| 3686 | |
| 3687 const char* p = str; | |
| 3688 EXPECT_FALSE(SkipPrefix("W", &p)); | |
| 3689 EXPECT_EQ(str, p); | |
| 3690 | |
| 3691 p = str; | |
| 3692 EXPECT_FALSE(SkipPrefix("world!", &p)); | |
| 3693 EXPECT_EQ(str, p); | |
| 3694 } | |
| 3695 | |
| 3696 // Tests FormatMatcherDescriptionSyntaxError(). | |
| 3697 TEST(FormatMatcherDescriptionSyntaxErrorTest, FormatsCorrectly) { | |
| 3698 const char* const description = "hello%world"; | |
| 3699 EXPECT_EQ("Syntax error at index 5 in matcher description \"hello%world\": ", | |
| 3700 FormatMatcherDescriptionSyntaxError(description, description + 5)); | |
| 3701 } | |
| 3702 | |
| 3703 // Tests ValidateMatcherDescription(). | |
| 3704 | |
| 3705 TEST(ValidateMatcherDescriptionTest, AcceptsEmptyDescription) { | |
| 3706 const char* params[] = { "foo", "bar", NULL }; | |
| 3707 EXPECT_THAT(ValidateMatcherDescription(params, ""), | |
| 3708 ElementsAre()); | |
| 3709 } | |
| 3710 | |
| 3711 TEST(ValidateMatcherDescriptionTest, | |
| 3712 AcceptsNonEmptyDescriptionWithNoInterpolation) { | |
| 3713 const char* params[] = { "foo", "bar", NULL }; | |
| 3714 EXPECT_THAT(ValidateMatcherDescription(params, "a simple description"), | |
| 3715 ElementsAre()); | |
| 3716 } | |
| 3717 | |
| 3718 // The MATCHER*() macros trigger warning C4100 (unreferenced formal | |
| 3719 // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in | |
| 3720 // the macro definition, as the warnings are generated when the macro | |
| 3721 // is expanded and macro expansion cannot contain #pragma. Therefore | |
| 3722 // we suppress them here. | |
| 3723 #ifdef _MSC_VER | |
| 3724 #pragma warning(push) | |
| 3725 #pragma warning(disable:4100) | |
| 3726 #endif | |
| 3727 | |
| 3728 // We use MATCHER_P3() to define a matcher for testing | |
| 3729 // ValidateMatcherDescription(); otherwise we'll end up with much | |
| 3730 // plumbing code. This is not circular as | |
| 3731 // ValidateMatcherDescription() doesn't affect whether the matcher | |
| 3732 // matches a value or not. | |
| 3733 MATCHER_P3(EqInterpolation, start, end, index, "equals Interpolation%(*)s") { | |
| 3734 return arg.start_pos == start && arg.end_pos == end && | |
| 3735 arg.param_index == index; | |
| 3736 } | |
| 3737 | |
| 3738 #ifdef _MSC_VER | |
| 3739 #pragma warning(pop) | |
| 3740 #endif | |
| 3741 | |
| 3742 TEST(ValidateMatcherDescriptionTest, AcceptsPercentInterpolation) { | |
| 3743 const char* params[] = { "foo", NULL }; | |
| 3744 const char* const desc = "one %%"; | |
| 3745 EXPECT_THAT(ValidateMatcherDescription(params, desc), | |
| 3746 ElementsAre(EqInterpolation(desc + 4, desc + 6, | |
| 3747 kPercentInterpolation))); | |
| 3748 } | |
| 3749 | |
| 3750 TEST(ValidateMatcherDescriptionTest, AcceptsTupleInterpolation) { | |
| 3751 const char* params[] = { "foo", "bar", "baz", NULL }; | |
| 3752 const char* const desc = "%(*)s after"; | |
| 3753 EXPECT_THAT(ValidateMatcherDescription(params, desc), | |
| 3754 ElementsAre(EqInterpolation(desc, desc + 5, | |
| 3755 kTupleInterpolation))); | |
| 3756 } | |
| 3757 | |
| 3758 TEST(ValidateMatcherDescriptionTest, AcceptsParamInterpolation) { | |
| 3759 const char* params[] = { "foo", "bar", "baz", NULL }; | |
| 3760 const char* const desc = "a %(bar)s."; | |
| 3761 EXPECT_THAT(ValidateMatcherDescription(params, desc), | |
| 3762 ElementsAre(EqInterpolation(desc + 2, desc + 9, 1))); | |
| 3763 } | |
| 3764 | |
| 3765 TEST(ValidateMatcherDescriptionTest, AcceptsMultiplenterpolations) { | |
| 3766 const char* params[] = { "foo", "bar", "baz", NULL }; | |
| 3767 const char* const desc = "%(baz)s %(foo)s %(bar)s"; | |
| 3768 EXPECT_THAT(ValidateMatcherDescription(params, desc), | |
| 3769 ElementsAre(EqInterpolation(desc, desc + 7, 2), | |
| 3770 EqInterpolation(desc + 8, desc + 15, 0), | |
| 3771 EqInterpolation(desc + 16, desc + 23, 1))); | |
| 3772 } | |
| 3773 | |
| 3774 TEST(ValidateMatcherDescriptionTest, AcceptsRepeatedParams) { | |
| 3775 const char* params[] = { "foo", "bar", NULL }; | |
| 3776 const char* const desc = "%(foo)s and %(foo)s"; | |
| 3777 EXPECT_THAT(ValidateMatcherDescription(params, desc), | |
| 3778 ElementsAre(EqInterpolation(desc, desc + 7, 0), | |
| 3779 EqInterpolation(desc + 12, desc + 19, 0))); | |
| 3780 } | |
| 3781 | |
| 3782 TEST(ValidateMatcherDescriptionTest, RejectsUnknownParam) { | |
| 3783 const char* params[] = { "a", "bar", NULL }; | |
| 3784 EXPECT_NONFATAL_FAILURE({ | |
| 3785 EXPECT_THAT(ValidateMatcherDescription(params, "%(foo)s"), | |
| 3786 ElementsAre()); | |
| 3787 }, "Syntax error at index 2 in matcher description \"%(foo)s\": " | |
| 3788 "\"foo\" is an invalid parameter name."); | |
| 3789 } | |
| 3790 | |
| 3791 TEST(ValidateMatcherDescriptionTest, RejectsUnfinishedParam) { | |
| 3792 const char* params[] = { "a", "bar", NULL }; | |
| 3793 EXPECT_NONFATAL_FAILURE({ | |
| 3794 EXPECT_THAT(ValidateMatcherDescription(params, "%(foo)"), | |
| 3795 ElementsAre()); | |
| 3796 }, "Syntax error at index 0 in matcher description \"%(foo)\": " | |
| 3797 "an interpolation must end with \")s\", but \"%(foo)\" does not."); | |
| 3798 | |
| 3799 EXPECT_NONFATAL_FAILURE({ | |
| 3800 EXPECT_THAT(ValidateMatcherDescription(params, "x%(a"), | |
| 3801 ElementsAre()); | |
| 3802 }, "Syntax error at index 1 in matcher description \"x%(a\": " | |
| 3803 "an interpolation must end with \")s\", but \"%(a\" does not."); | |
| 3804 } | |
| 3805 | |
| 3806 TEST(ValidateMatcherDescriptionTest, RejectsSinglePercent) { | |
| 3807 const char* params[] = { "a", NULL }; | |
| 3808 EXPECT_NONFATAL_FAILURE({ | |
| 3809 EXPECT_THAT(ValidateMatcherDescription(params, "a %."), | |
| 3810 ElementsAre()); | |
| 3811 }, "Syntax error at index 2 in matcher description \"a %.\": " | |
| 3812 "use \"%%\" instead of \"%\" to print \"%\"."); | |
| 3813 | |
| 3814 } | |
| 3815 | |
| 3816 // Tests JoinAsTuple(). | |
| 3817 | |
| 3818 TEST(JoinAsTupleTest, JoinsEmptyTuple) { | |
| 3819 EXPECT_EQ("", JoinAsTuple(Strings())); | |
| 3820 } | |
| 3821 | |
| 3822 TEST(JoinAsTupleTest, JoinsOneTuple) { | |
| 3823 const char* fields[] = { "1" }; | |
| 3824 EXPECT_EQ("1", JoinAsTuple(Strings(fields, fields + 1))); | |
| 3825 } | |
| 3826 | |
| 3827 TEST(JoinAsTupleTest, JoinsTwoTuple) { | |
| 3828 const char* fields[] = { "1", "a" }; | |
| 3829 EXPECT_EQ("(1, a)", JoinAsTuple(Strings(fields, fields + 2))); | |
| 3830 } | |
| 3831 | |
| 3832 TEST(JoinAsTupleTest, JoinsTenTuple) { | |
| 3833 const char* fields[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }; | |
| 3834 EXPECT_EQ("(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)", | |
| 3835 JoinAsTuple(Strings(fields, fields + 10))); | |
| 3836 } | |
| 3837 | |
| 3838 // Tests FormatMatcherDescription(). | |
| 3839 | |
| 3840 TEST(FormatMatcherDescriptionTest, WorksForEmptyDescription) { | |
| 3841 EXPECT_EQ("is even", | |
| 3842 FormatMatcherDescription("IsEven", "", Interpolations(), | |
| 3843 Strings())); | |
| 3844 | |
| 3845 const char* params[] = { "5" }; | |
| 3846 EXPECT_EQ("equals 5", | |
| 3847 FormatMatcherDescription("Equals", "", Interpolations(), | |
| 3848 Strings(params, params + 1))); | |
| 3849 | |
| 3850 const char* params2[] = { "5", "8" }; | |
| 3851 EXPECT_EQ("is in range (5, 8)", | |
| 3852 FormatMatcherDescription("IsInRange", "", Interpolations(), | |
| 3853 Strings(params2, params2 + 2))); | |
| 3854 } | |
| 3855 | |
| 3856 TEST(FormatMatcherDescriptionTest, WorksForDescriptionWithNoInterpolation) { | |
| 3857 EXPECT_EQ("is positive", | |
| 3858 FormatMatcherDescription("Gt0", "is positive", Interpolations(), | |
| 3859 Strings())); | |
| 3860 | |
| 3861 const char* params[] = { "5", "6" }; | |
| 3862 EXPECT_EQ("is negative", | |
| 3863 FormatMatcherDescription("Lt0", "is negative", Interpolations(), | |
| 3864 Strings(params, params + 2))); | |
| 3865 } | |
| 3866 | |
| 3867 TEST(FormatMatcherDescriptionTest, | |
| 3868 WorksWhenDescriptionStartsWithInterpolation) { | |
| 3869 const char* params[] = { "5" }; | |
| 3870 const char* const desc = "%(num)s times bigger"; | |
| 3871 const Interpolation interp[] = { Interpolation(desc, desc + 7, 0) }; | |
| 3872 EXPECT_EQ("5 times bigger", | |
| 3873 FormatMatcherDescription("Foo", desc, | |
| 3874 Interpolations(interp, interp + 1), | |
| 3875 Strings(params, params + 1))); | |
| 3876 } | |
| 3877 | |
| 3878 TEST(FormatMatcherDescriptionTest, | |
| 3879 WorksWhenDescriptionEndsWithInterpolation) { | |
| 3880 const char* params[] = { "5", "6" }; | |
| 3881 const char* const desc = "is bigger than %(y)s"; | |
| 3882 const Interpolation interp[] = { Interpolation(desc + 15, desc + 20, 1) }; | |
| 3883 EXPECT_EQ("is bigger than 6", | |
| 3884 FormatMatcherDescription("Foo", desc, | |
| 3885 Interpolations(interp, interp + 1), | |
| 3886 Strings(params, params + 2))); | |
| 3887 } | |
| 3888 | |
| 3889 TEST(FormatMatcherDescriptionTest, | |
| 3890 WorksWhenDescriptionStartsAndEndsWithInterpolation) { | |
| 3891 const char* params[] = { "5", "6" }; | |
| 3892 const char* const desc = "%(x)s <= arg <= %(y)s"; | |
| 3893 const Interpolation interp[] = { | |
| 3894 Interpolation(desc, desc + 5, 0), | |
| 3895 Interpolation(desc + 16, desc + 21, 1) | |
| 3896 }; | |
| 3897 EXPECT_EQ("5 <= arg <= 6", | |
| 3898 FormatMatcherDescription("Foo", desc, | |
| 3899 Interpolations(interp, interp + 2), | |
| 3900 Strings(params, params + 2))); | |
| 3901 } | |
| 3902 | |
| 3903 TEST(FormatMatcherDescriptionTest, | |
| 3904 WorksWhenDescriptionDoesNotStartOrEndWithInterpolation) { | |
| 3905 const char* params[] = { "5.2" }; | |
| 3906 const char* const desc = "has %(x)s cents"; | |
| 3907 const Interpolation interp[] = { Interpolation(desc + 4, desc + 9, 0) }; | |
| 3908 EXPECT_EQ("has 5.2 cents", | |
| 3909 FormatMatcherDescription("Foo", desc, | |
| 3910 Interpolations(interp, interp + 1), | |
| 3911 Strings(params, params + 1))); | |
| 3912 } | |
| 3913 | |
| 3914 TEST(FormatMatcherDescriptionTest, | |
| 3915 WorksWhenDescriptionContainsMultipleInterpolations) { | |
| 3916 const char* params[] = { "5", "6" }; | |
| 3917 const char* const desc = "in %(*)s or [%(x)s, %(y)s]"; | |
| 3918 const Interpolation interp[] = { | |
| 3919 Interpolation(desc + 3, desc + 8, kTupleInterpolation), | |
| 3920 Interpolation(desc + 13, desc + 18, 0), | |
| 3921 Interpolation(desc + 20, desc + 25, 1) | |
| 3922 }; | |
| 3923 EXPECT_EQ("in (5, 6) or [5, 6]", | |
| 3924 FormatMatcherDescription("Foo", desc, | |
| 3925 Interpolations(interp, interp + 3), | |
| 3926 Strings(params, params + 2))); | |
| 3927 } | |
| 3928 | |
| 3929 TEST(FormatMatcherDescriptionTest, | |
| 3930 WorksWhenDescriptionContainsRepeatedParams) { | |
| 3931 const char* params[] = { "9" }; | |
| 3932 const char* const desc = "in [-%(x)s, %(x)s]"; | |
| 3933 const Interpolation interp[] = { | |
| 3934 Interpolation(desc + 5, desc + 10, 0), | |
| 3935 Interpolation(desc + 12, desc + 17, 0) | |
| 3936 }; | |
| 3937 EXPECT_EQ("in [-9, 9]", | |
| 3938 FormatMatcherDescription("Foo", desc, | |
| 3939 Interpolations(interp, interp + 2), | |
| 3940 Strings(params, params + 1))); | |
| 3941 } | |
| 3942 | |
| 3943 TEST(FormatMatcherDescriptionTest, | |
| 3944 WorksForDescriptionWithInvalidInterpolation) { | |
| 3945 const char* params[] = { "9" }; | |
| 3946 const char* const desc = "> %(x)s %(x)"; | |
| 3947 const Interpolation interp[] = { Interpolation(desc + 2, desc + 7, 0) }; | |
| 3948 EXPECT_EQ("> 9 %(x)", | |
| 3949 FormatMatcherDescription("Foo", desc, | |
| 3950 Interpolations(interp, interp + 1), | |
| 3951 Strings(params, params + 1))); | |
| 3952 } | |
| 3953 | |
| 3954 // Tests PolymorphicMatcher::mutable_impl(). | |
| 3955 TEST(PolymorphicMatcherTest, CanAccessMutableImpl) { | |
| 3956 PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42)); | |
| 3957 DivisibleByImpl& impl = m.mutable_impl(); | |
| 3958 EXPECT_EQ(42, impl.divider()); | |
| 3959 | |
| 3960 impl.set_divider(0); | |
| 3961 EXPECT_EQ(0, m.mutable_impl().divider()); | |
| 3962 } | |
| 3963 | |
| 3964 // Tests PolymorphicMatcher::impl(). | |
| 3965 TEST(PolymorphicMatcherTest, CanAccessImpl) { | |
| 3966 const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42)); | |
| 3967 const DivisibleByImpl& impl = m.impl(); | |
| 3968 EXPECT_EQ(42, impl.divider()); | |
| 3969 } | |
| 3970 | |
| 3971 TEST(MatcherTupleTest, ExplainsMatchFailure) { | |
| 3972 stringstream ss1; | |
| 3973 ExplainMatchFailureTupleTo(make_tuple(Matcher<char>(Eq('a')), GreaterThan(5)), | |
| 3974 make_tuple('a', 10), &ss1); | |
| 3975 EXPECT_EQ("", ss1.str()); // Successful match. | |
| 3976 | |
| 3977 stringstream ss2; | |
| 3978 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))), | |
| 3979 make_tuple(2, 'b'), &ss2); | |
| 3980 EXPECT_EQ(" Expected arg #0: is > 5\n" | |
| 3981 " Actual: 2, which is 3 less than 5\n" | |
| 3982 " Expected arg #1: is equal to 'a' (97)\n" | |
| 3983 " Actual: 'b' (98)\n", | |
| 3984 ss2.str()); // Failed match where both arguments need explanation. | |
| 3985 | |
| 3986 stringstream ss3; | |
| 3987 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))), | |
| 3988 make_tuple(2, 'a'), &ss3); | |
| 3989 EXPECT_EQ(" Expected arg #0: is > 5\n" | |
| 3990 " Actual: 2, which is 3 less than 5\n", | |
| 3991 ss3.str()); // Failed match where only one argument needs | |
| 3992 // explanation. | |
| 3993 } | |
| 3994 | |
| 3995 // Tests Each(). | |
| 3996 | |
| 3997 TEST(EachTest, ExplainsMatchResultCorrectly) { | |
| 3998 set<int> a; // empty | |
| 3999 | |
| 4000 Matcher<set<int> > m = Each(2); | |
| 4001 EXPECT_EQ("", Explain(m, a)); | |
| 4002 | |
| 4003 Matcher<const int(&)[1]> n = Each(1); | |
| 4004 | |
| 4005 const int b[1] = { 1 }; | |
| 4006 EXPECT_EQ("", Explain(n, b)); | |
| 4007 | |
| 4008 n = Each(3); | |
| 4009 EXPECT_EQ("whose element #0 doesn't match", Explain(n, b)); | |
| 4010 | |
| 4011 a.insert(1); | |
| 4012 a.insert(2); | |
| 4013 a.insert(3); | |
| 4014 m = Each(GreaterThan(0)); | |
| 4015 EXPECT_EQ("", Explain(m, a)); | |
| 4016 | |
| 4017 m = Each(GreaterThan(10)); | |
| 4018 EXPECT_EQ("whose element #0 doesn't match, which is 9 less than 10", | |
| 4019 Explain(m, a)); | |
| 4020 } | |
| 4021 | |
| 4022 TEST(EachTest, DescribesItselfCorrectly) { | |
| 4023 Matcher<vector<int> > m = Each(1); | |
| 4024 EXPECT_EQ("only contains elements that is equal to 1", Describe(m)); | |
| 4025 | |
| 4026 Matcher<vector<int> > m2 = Not(m); | |
| 4027 EXPECT_EQ("contains some element that isn't equal to 1", Describe(m2)); | |
| 4028 } | |
| 4029 | |
| 4030 TEST(EachTest, MatchesVectorWhenAllElementsMatch) { | |
| 4031 vector<int> some_vector; | |
| 4032 EXPECT_THAT(some_vector, Each(1)); | |
| 4033 some_vector.push_back(3); | |
| 4034 EXPECT_THAT(some_vector, Not(Each(1))); | |
| 4035 EXPECT_THAT(some_vector, Each(3)); | |
| 4036 some_vector.push_back(1); | |
| 4037 some_vector.push_back(2); | |
| 4038 EXPECT_THAT(some_vector, Not(Each(3))); | |
| 4039 EXPECT_THAT(some_vector, Each(Lt(3.5))); | |
| 4040 | |
| 4041 vector<string> another_vector; | |
| 4042 another_vector.push_back("fee"); | |
| 4043 EXPECT_THAT(another_vector, Each(string("fee"))); | |
| 4044 another_vector.push_back("fie"); | |
| 4045 another_vector.push_back("foe"); | |
| 4046 another_vector.push_back("fum"); | |
| 4047 EXPECT_THAT(another_vector, Not(Each(string("fee")))); | |
| 4048 } | |
| 4049 | |
| 4050 TEST(EachTest, MatchesMapWhenAllElementsMatch) { | |
| 4051 map<const char*, int> my_map; | |
| 4052 const char* bar = "a string"; | |
| 4053 my_map[bar] = 2; | |
| 4054 EXPECT_THAT(my_map, Each(make_pair(bar, 2))); | |
| 4055 | |
| 4056 map<string, int> another_map; | |
| 4057 EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1))); | |
| 4058 another_map["fee"] = 1; | |
| 4059 EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1))); | |
| 4060 another_map["fie"] = 2; | |
| 4061 another_map["foe"] = 3; | |
| 4062 another_map["fum"] = 4; | |
| 4063 EXPECT_THAT(another_map, Not(Each(make_pair(string("fee"), 1)))); | |
| 4064 EXPECT_THAT(another_map, Not(Each(make_pair(string("fum"), 1)))); | |
| 4065 EXPECT_THAT(another_map, Each(Pair(_, Gt(0)))); | |
| 4066 } | |
| 4067 | |
| 4068 TEST(EachTest, AcceptsMatcher) { | |
| 4069 const int a[] = { 1, 2, 3 }; | |
| 4070 EXPECT_THAT(a, Each(Gt(0))); | |
| 4071 EXPECT_THAT(a, Not(Each(Gt(1)))); | |
| 4072 } | |
| 4073 | |
| 4074 TEST(EachTest, WorksForNativeArrayAsTuple) { | |
| 4075 const int a[] = { 1, 2 }; | |
| 4076 const int* const pointer = a; | |
| 4077 EXPECT_THAT(make_tuple(pointer, 2), Each(Gt(0))); | |
| 4078 EXPECT_THAT(make_tuple(pointer, 2), Not(Each(Gt(1)))); | |
| 4079 } | |
| 4080 | |
| 4081 // For testing Pointwise(). | |
| 4082 class IsHalfOfMatcher { | |
| 4083 public: | |
| 4084 template <typename T1, typename T2> | |
| 4085 bool MatchAndExplain(const tuple<T1, T2>& a_pair, | |
| 4086 MatchResultListener* listener) const { | |
| 4087 if (get<0>(a_pair) == get<1>(a_pair)/2) { | |
| 4088 *listener << "where the second is " << get<1>(a_pair); | |
| 4089 return true; | |
| 4090 } else { | |
| 4091 *listener << "where the second/2 is " << get<1>(a_pair)/2; | |
| 4092 return false; | |
| 4093 } | |
| 4094 } | |
| 4095 | |
| 4096 void DescribeTo(ostream* os) const { | |
| 4097 *os << "are a pair where the first is half of the second"; | |
| 4098 } | |
| 4099 | |
| 4100 void DescribeNegationTo(ostream* os) const { | |
| 4101 *os << "are a pair where the first isn't half of the second"; | |
| 4102 } | |
| 4103 }; | |
| 4104 | |
| 4105 PolymorphicMatcher<IsHalfOfMatcher> IsHalfOf() { | |
| 4106 return MakePolymorphicMatcher(IsHalfOfMatcher()); | |
| 4107 } | |
| 4108 | |
| 4109 TEST(PointwiseTest, DescribesSelf) { | |
| 4110 vector<int> rhs; | |
| 4111 rhs.push_back(1); | |
| 4112 rhs.push_back(2); | |
| 4113 rhs.push_back(3); | |
| 4114 const Matcher<const vector<int>&> m = Pointwise(IsHalfOf(), rhs); | |
| 4115 EXPECT_EQ("contains 3 values, where each value and its corresponding value " | |
| 4116 "in { 1, 2, 3 } are a pair where the first is half of the second", | |
| 4117 Describe(m)); | |
| 4118 EXPECT_EQ("doesn't contain exactly 3 values, or contains a value x at some " | |
| 4119 "index i where x and the i-th value of { 1, 2, 3 } are a pair " | |
| 4120 "where the first isn't half of the second", | |
| 4121 DescribeNegation(m)); | |
| 4122 } | |
| 4123 | |
| 4124 TEST(PointwiseTest, MakesCopyOfRhs) { | |
| 4125 list<signed char> rhs; | |
| 4126 rhs.push_back(2); | |
| 4127 rhs.push_back(4); | |
| 4128 | |
| 4129 int lhs[] = { 1, 2 }; | |
| 4130 const Matcher<const int (&)[2]> m = Pointwise(IsHalfOf(), rhs); | |
| 4131 EXPECT_THAT(lhs, m); | |
| 4132 | |
| 4133 // Changing rhs now shouldn't affect m, which made a copy of rhs. | |
| 4134 rhs.push_back(6); | |
| 4135 EXPECT_THAT(lhs, m); | |
| 4136 } | |
| 4137 | |
| 4138 TEST(PointwiseTest, WorksForLhsNativeArray) { | |
| 4139 const int lhs[] = { 1, 2, 3 }; | |
| 4140 vector<int> rhs; | |
| 4141 rhs.push_back(2); | |
| 4142 rhs.push_back(4); | |
| 4143 rhs.push_back(6); | |
| 4144 EXPECT_THAT(lhs, Pointwise(Lt(), rhs)); | |
| 4145 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs))); | |
| 4146 } | |
| 4147 | |
| 4148 TEST(PointwiseTest, WorksForRhsNativeArray) { | |
| 4149 const int rhs[] = { 1, 2, 3 }; | |
| 4150 vector<int> lhs; | |
| 4151 lhs.push_back(2); | |
| 4152 lhs.push_back(4); | |
| 4153 lhs.push_back(6); | |
| 4154 EXPECT_THAT(lhs, Pointwise(Gt(), rhs)); | |
| 4155 EXPECT_THAT(lhs, Not(Pointwise(Lt(), rhs))); | |
| 4156 } | |
| 4157 | |
| 4158 TEST(PointwiseTest, RejectsWrongSize) { | |
| 4159 const double lhs[2] = { 1, 2 }; | |
| 4160 const int rhs[1] = { 0 }; | |
| 4161 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs))); | |
| 4162 EXPECT_EQ("which contains 2 values", | |
| 4163 Explain(Pointwise(Gt(), rhs), lhs)); | |
| 4164 | |
| 4165 const int rhs2[3] = { 0, 1, 2 }; | |
| 4166 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs2))); | |
| 4167 } | |
| 4168 | |
| 4169 TEST(PointwiseTest, RejectsWrongContent) { | |
| 4170 const double lhs[3] = { 1, 2, 3 }; | |
| 4171 const int rhs[3] = { 2, 6, 4 }; | |
| 4172 EXPECT_THAT(lhs, Not(Pointwise(IsHalfOf(), rhs))); | |
| 4173 EXPECT_EQ("where the value pair (2, 6) at index #1 don't match, " | |
| 4174 "where the second/2 is 3", | |
| 4175 Explain(Pointwise(IsHalfOf(), rhs), lhs)); | |
| 4176 } | |
| 4177 | |
| 4178 TEST(PointwiseTest, AcceptsCorrectContent) { | |
| 4179 const double lhs[3] = { 1, 2, 3 }; | |
| 4180 const int rhs[3] = { 2, 4, 6 }; | |
| 4181 EXPECT_THAT(lhs, Pointwise(IsHalfOf(), rhs)); | |
| 4182 EXPECT_EQ("", Explain(Pointwise(IsHalfOf(), rhs), lhs)); | |
| 4183 } | |
| 4184 | |
| 4185 TEST(PointwiseTest, AllowsMonomorphicInnerMatcher) { | |
| 4186 const double lhs[3] = { 1, 2, 3 }; | |
| 4187 const int rhs[3] = { 2, 4, 6 }; | |
| 4188 const Matcher<tuple<const double&, const int&> > m1 = IsHalfOf(); | |
| 4189 EXPECT_THAT(lhs, Pointwise(m1, rhs)); | |
| 4190 EXPECT_EQ("", Explain(Pointwise(m1, rhs), lhs)); | |
| 4191 | |
| 4192 // This type works as a tuple<const double&, const int&> can be | |
| 4193 // implicitly cast to tuple<double, int>. | |
| 4194 const Matcher<tuple<double, int> > m2 = IsHalfOf(); | |
| 4195 EXPECT_THAT(lhs, Pointwise(m2, rhs)); | |
| 4196 EXPECT_EQ("", Explain(Pointwise(m2, rhs), lhs)); | |
| 4197 } | |
| 4198 | |
| 4199 } // namespace gmock_matchers_test | |
| 4200 } // namespace testing | |
| OLD | NEW |