shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 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> |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame^] | 40 | #include <list> |
| 41 | #include <map> |
| 42 | #include <set> |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 43 | #include <sstream> |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame^] | 44 | #include <string> |
| 45 | #include <vector> |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 46 | #include <gmock/gmock.h> |
| 47 | #include <gtest/gtest.h> |
| 48 | #include <gtest/gtest-spi.h> |
| 49 | |
| 50 | namespace testing { |
| 51 | namespace gmock_matchers_test { |
| 52 | |
| 53 | using std::stringstream; |
| 54 | using testing::A; |
| 55 | using testing::AllOf; |
| 56 | using testing::An; |
| 57 | using testing::AnyOf; |
| 58 | using testing::ByRef; |
| 59 | using testing::DoubleEq; |
| 60 | using testing::EndsWith; |
| 61 | using testing::Eq; |
| 62 | using testing::Field; |
| 63 | using testing::FloatEq; |
| 64 | using testing::Ge; |
| 65 | using testing::Gt; |
| 66 | using testing::HasSubstr; |
| 67 | using testing::Le; |
| 68 | using testing::Lt; |
| 69 | using testing::MakeMatcher; |
| 70 | using testing::MakePolymorphicMatcher; |
| 71 | using testing::Matcher; |
| 72 | using testing::MatcherCast; |
| 73 | using testing::MatcherInterface; |
| 74 | using testing::Matches; |
| 75 | using testing::NanSensitiveDoubleEq; |
| 76 | using testing::NanSensitiveFloatEq; |
| 77 | using testing::Ne; |
| 78 | using testing::Not; |
| 79 | using testing::NotNull; |
| 80 | using testing::Pointee; |
| 81 | using testing::PolymorphicMatcher; |
| 82 | using testing::Property; |
| 83 | using testing::Ref; |
| 84 | using testing::ResultOf; |
| 85 | using testing::StartsWith; |
| 86 | using testing::StrCaseEq; |
| 87 | using testing::StrCaseNe; |
| 88 | using testing::StrEq; |
| 89 | using testing::StrNe; |
| 90 | using testing::Truly; |
| 91 | using testing::TypedEq; |
| 92 | using testing::_; |
| 93 | using testing::internal::FloatingEqMatcher; |
| 94 | using testing::internal::String; |
| 95 | using testing::internal::string; |
| 96 | |
| 97 | #ifdef GMOCK_HAS_REGEX |
| 98 | using testing::ContainsRegex; |
| 99 | using testing::MatchesRegex; |
| 100 | using testing::internal::RE; |
| 101 | #endif // GMOCK_HAS_REGEX |
| 102 | |
| 103 | // Returns the description of the given matcher. |
| 104 | template <typename T> |
| 105 | string Describe(const Matcher<T>& m) { |
| 106 | stringstream ss; |
| 107 | m.DescribeTo(&ss); |
| 108 | return ss.str(); |
| 109 | } |
| 110 | |
| 111 | // Returns the description of the negation of the given matcher. |
| 112 | template <typename T> |
| 113 | string DescribeNegation(const Matcher<T>& m) { |
| 114 | stringstream ss; |
| 115 | m.DescribeNegationTo(&ss); |
| 116 | return ss.str(); |
| 117 | } |
| 118 | |
| 119 | // Returns the reason why x matches, or doesn't match, m. |
| 120 | template <typename MatcherType, typename Value> |
| 121 | string Explain(const MatcherType& m, const Value& x) { |
| 122 | stringstream ss; |
| 123 | m.ExplainMatchResultTo(x, &ss); |
| 124 | return ss.str(); |
| 125 | } |
| 126 | |
| 127 | // Makes sure that the MatcherInterface<T> interface doesn't |
| 128 | // change. |
| 129 | class EvenMatcherImpl : public MatcherInterface<int> { |
| 130 | public: |
| 131 | virtual bool Matches(int x) const { return x % 2 == 0; } |
| 132 | |
| 133 | virtual void DescribeTo(::std::ostream* os) const { |
| 134 | *os << "is an even number"; |
| 135 | } |
| 136 | |
| 137 | // We deliberately don't define DescribeNegationTo() and |
| 138 | // ExplainMatchResultTo() here, to make sure the definition of these |
| 139 | // two methods is optional. |
| 140 | }; |
| 141 | |
| 142 | TEST(MatcherInterfaceTest, CanBeImplemented) { |
| 143 | EvenMatcherImpl m; |
| 144 | } |
| 145 | |
| 146 | // Tests default-constructing a matcher. |
| 147 | TEST(MatcherTest, CanBeDefaultConstructed) { |
| 148 | Matcher<double> m; |
| 149 | } |
| 150 | |
| 151 | // Tests that Matcher<T> can be constructed from a MatcherInterface<T>*. |
| 152 | TEST(MatcherTest, CanBeConstructedFromMatcherInterface) { |
| 153 | const MatcherInterface<int>* impl = new EvenMatcherImpl; |
| 154 | Matcher<int> m(impl); |
| 155 | EXPECT_TRUE(m.Matches(4)); |
| 156 | EXPECT_FALSE(m.Matches(5)); |
| 157 | } |
| 158 | |
| 159 | // Tests that value can be used in place of Eq(value). |
| 160 | TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) { |
| 161 | Matcher<int> m1 = 5; |
| 162 | EXPECT_TRUE(m1.Matches(5)); |
| 163 | EXPECT_FALSE(m1.Matches(6)); |
| 164 | } |
| 165 | |
| 166 | // Tests that NULL can be used in place of Eq(NULL). |
| 167 | TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) { |
| 168 | Matcher<int*> m1 = NULL; |
| 169 | EXPECT_TRUE(m1.Matches(NULL)); |
| 170 | int n = 0; |
| 171 | EXPECT_FALSE(m1.Matches(&n)); |
| 172 | } |
| 173 | |
| 174 | // Tests that matchers are copyable. |
| 175 | TEST(MatcherTest, IsCopyable) { |
| 176 | // Tests the copy constructor. |
| 177 | Matcher<bool> m1 = Eq(false); |
| 178 | EXPECT_TRUE(m1.Matches(false)); |
| 179 | EXPECT_FALSE(m1.Matches(true)); |
| 180 | |
| 181 | // Tests the assignment operator. |
| 182 | m1 = Eq(true); |
| 183 | EXPECT_TRUE(m1.Matches(true)); |
| 184 | EXPECT_FALSE(m1.Matches(false)); |
| 185 | } |
| 186 | |
| 187 | // Tests that Matcher<T>::DescribeTo() calls |
| 188 | // MatcherInterface<T>::DescribeTo(). |
| 189 | TEST(MatcherTest, CanDescribeItself) { |
| 190 | EXPECT_EQ("is an even number", |
| 191 | Describe(Matcher<int>(new EvenMatcherImpl))); |
| 192 | } |
| 193 | |
| 194 | // Tests that a C-string literal can be implicitly converted to a |
| 195 | // Matcher<string> or Matcher<const string&>. |
| 196 | TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) { |
| 197 | Matcher<string> m1 = "hi"; |
| 198 | EXPECT_TRUE(m1.Matches("hi")); |
| 199 | EXPECT_FALSE(m1.Matches("hello")); |
| 200 | |
| 201 | Matcher<const string&> m2 = "hi"; |
| 202 | EXPECT_TRUE(m2.Matches("hi")); |
| 203 | EXPECT_FALSE(m2.Matches("hello")); |
| 204 | } |
| 205 | |
| 206 | // Tests that a string object can be implicitly converted to a |
| 207 | // Matcher<string> or Matcher<const string&>. |
| 208 | TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) { |
| 209 | Matcher<string> m1 = string("hi"); |
| 210 | EXPECT_TRUE(m1.Matches("hi")); |
| 211 | EXPECT_FALSE(m1.Matches("hello")); |
| 212 | |
| 213 | Matcher<const string&> m2 = string("hi"); |
| 214 | EXPECT_TRUE(m2.Matches("hi")); |
| 215 | EXPECT_FALSE(m2.Matches("hello")); |
| 216 | } |
| 217 | |
| 218 | // Tests that MakeMatcher() constructs a Matcher<T> from a |
| 219 | // MatcherInterface* without requiring the user to explicitly |
| 220 | // write the type. |
| 221 | TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) { |
| 222 | const MatcherInterface<int>* dummy_impl = NULL; |
| 223 | Matcher<int> m = MakeMatcher(dummy_impl); |
| 224 | } |
| 225 | |
| 226 | // Tests that MakePolymorphicMatcher() constructs a polymorphic |
| 227 | // matcher from its implementation. |
| 228 | const int bar = 1; |
| 229 | class ReferencesBarOrIsZeroImpl { |
| 230 | public: |
| 231 | template <typename T> |
| 232 | bool Matches(const T& x) const { |
| 233 | const void* p = &x; |
| 234 | return p == &bar || x == 0; |
| 235 | } |
| 236 | |
| 237 | void DescribeTo(::std::ostream* os) const { *os << "bar or zero"; } |
| 238 | |
| 239 | void DescribeNegationTo(::std::ostream* os) const { |
| 240 | *os << "doesn't reference bar and is not zero"; |
| 241 | } |
| 242 | }; |
| 243 | |
| 244 | // This function verifies that MakePolymorphicMatcher() returns a |
| 245 | // PolymorphicMatcher<T> where T is the argument's type. |
| 246 | PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() { |
| 247 | return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl()); |
| 248 | } |
| 249 | |
| 250 | TEST(MakePolymorphicMatcherTest, ConstructsMatcherFromImpl) { |
| 251 | // Using a polymorphic matcher to match a reference type. |
| 252 | Matcher<const int&> m1 = ReferencesBarOrIsZero(); |
| 253 | EXPECT_TRUE(m1.Matches(0)); |
| 254 | // Verifies that the identity of a by-reference argument is preserved. |
| 255 | EXPECT_TRUE(m1.Matches(bar)); |
| 256 | EXPECT_FALSE(m1.Matches(1)); |
| 257 | EXPECT_EQ("bar or zero", Describe(m1)); |
| 258 | |
| 259 | // Using a polymorphic matcher to match a value type. |
| 260 | Matcher<double> m2 = ReferencesBarOrIsZero(); |
| 261 | EXPECT_TRUE(m2.Matches(0.0)); |
| 262 | EXPECT_FALSE(m2.Matches(0.1)); |
| 263 | EXPECT_EQ("bar or zero", Describe(m2)); |
| 264 | } |
| 265 | |
| 266 | // Tests that MatcherCast<T>(m) works when m is a polymorphic matcher. |
| 267 | TEST(MatcherCastTest, FromPolymorphicMatcher) { |
| 268 | Matcher<int> m = MatcherCast<int>(Eq(5)); |
| 269 | EXPECT_TRUE(m.Matches(5)); |
| 270 | EXPECT_FALSE(m.Matches(6)); |
| 271 | } |
| 272 | |
| 273 | // For testing casting matchers between compatible types. |
| 274 | class IntValue { |
| 275 | public: |
| 276 | // An int can be statically (although not implicitly) cast to a |
| 277 | // IntValue. |
| 278 | explicit IntValue(int value) : value_(value) {} |
| 279 | |
| 280 | int value() const { return value_; } |
| 281 | private: |
| 282 | int value_; |
| 283 | }; |
| 284 | |
| 285 | // For testing casting matchers between compatible types. |
| 286 | bool IsPositiveIntValue(const IntValue& foo) { |
| 287 | return foo.value() > 0; |
| 288 | } |
| 289 | |
| 290 | // Tests that MatcherCast<T>(m) works when m is a Matcher<U> where T |
| 291 | // can be statically converted to U. |
| 292 | TEST(MatcherCastTest, FromCompatibleType) { |
| 293 | Matcher<double> m1 = Eq(2.0); |
| 294 | Matcher<int> m2 = MatcherCast<int>(m1); |
| 295 | EXPECT_TRUE(m2.Matches(2)); |
| 296 | EXPECT_FALSE(m2.Matches(3)); |
| 297 | |
| 298 | Matcher<IntValue> m3 = Truly(IsPositiveIntValue); |
| 299 | Matcher<int> m4 = MatcherCast<int>(m3); |
| 300 | // In the following, the arguments 1 and 0 are statically converted |
| 301 | // to IntValue objects, and then tested by the IsPositiveIntValue() |
| 302 | // predicate. |
| 303 | EXPECT_TRUE(m4.Matches(1)); |
| 304 | EXPECT_FALSE(m4.Matches(0)); |
| 305 | } |
| 306 | |
| 307 | // Tests that MatcherCast<T>(m) works when m is a Matcher<const T&>. |
| 308 | TEST(MatcherCastTest, FromConstReferenceToNonReference) { |
| 309 | Matcher<const int&> m1 = Eq(0); |
| 310 | Matcher<int> m2 = MatcherCast<int>(m1); |
| 311 | EXPECT_TRUE(m2.Matches(0)); |
| 312 | EXPECT_FALSE(m2.Matches(1)); |
| 313 | } |
| 314 | |
| 315 | // Tests that MatcherCast<T>(m) works when m is a Matcher<T&>. |
| 316 | TEST(MatcherCastTest, FromReferenceToNonReference) { |
| 317 | Matcher<int&> m1 = Eq(0); |
| 318 | Matcher<int> m2 = MatcherCast<int>(m1); |
| 319 | EXPECT_TRUE(m2.Matches(0)); |
| 320 | EXPECT_FALSE(m2.Matches(1)); |
| 321 | } |
| 322 | |
| 323 | // Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>. |
| 324 | TEST(MatcherCastTest, FromNonReferenceToConstReference) { |
| 325 | Matcher<int> m1 = Eq(0); |
| 326 | Matcher<const int&> m2 = MatcherCast<const int&>(m1); |
| 327 | EXPECT_TRUE(m2.Matches(0)); |
| 328 | EXPECT_FALSE(m2.Matches(1)); |
| 329 | } |
| 330 | |
| 331 | // Tests that MatcherCast<T&>(m) works when m is a Matcher<T>. |
| 332 | TEST(MatcherCastTest, FromNonReferenceToReference) { |
| 333 | Matcher<int> m1 = Eq(0); |
| 334 | Matcher<int&> m2 = MatcherCast<int&>(m1); |
| 335 | int n = 0; |
| 336 | EXPECT_TRUE(m2.Matches(n)); |
| 337 | n = 1; |
| 338 | EXPECT_FALSE(m2.Matches(n)); |
| 339 | } |
| 340 | |
| 341 | // Tests that MatcherCast<T>(m) works when m is a Matcher<T>. |
| 342 | TEST(MatcherCastTest, FromSameType) { |
| 343 | Matcher<int> m1 = Eq(0); |
| 344 | Matcher<int> m2 = MatcherCast<int>(m1); |
| 345 | EXPECT_TRUE(m2.Matches(0)); |
| 346 | EXPECT_FALSE(m2.Matches(1)); |
| 347 | } |
| 348 | |
| 349 | // Tests that A<T>() matches any value of type T. |
| 350 | TEST(ATest, MatchesAnyValue) { |
| 351 | // Tests a matcher for a value type. |
| 352 | Matcher<double> m1 = A<double>(); |
| 353 | EXPECT_TRUE(m1.Matches(91.43)); |
| 354 | EXPECT_TRUE(m1.Matches(-15.32)); |
| 355 | |
| 356 | // Tests a matcher for a reference type. |
| 357 | int a = 2; |
| 358 | int b = -6; |
| 359 | Matcher<int&> m2 = A<int&>(); |
| 360 | EXPECT_TRUE(m2.Matches(a)); |
| 361 | EXPECT_TRUE(m2.Matches(b)); |
| 362 | } |
| 363 | |
| 364 | // Tests that A<T>() describes itself properly. |
| 365 | TEST(ATest, CanDescribeSelf) { |
| 366 | EXPECT_EQ("is anything", Describe(A<bool>())); |
| 367 | } |
| 368 | |
| 369 | // Tests that An<T>() matches any value of type T. |
| 370 | TEST(AnTest, MatchesAnyValue) { |
| 371 | // Tests a matcher for a value type. |
| 372 | Matcher<int> m1 = An<int>(); |
| 373 | EXPECT_TRUE(m1.Matches(9143)); |
| 374 | EXPECT_TRUE(m1.Matches(-1532)); |
| 375 | |
| 376 | // Tests a matcher for a reference type. |
| 377 | int a = 2; |
| 378 | int b = -6; |
| 379 | Matcher<int&> m2 = An<int&>(); |
| 380 | EXPECT_TRUE(m2.Matches(a)); |
| 381 | EXPECT_TRUE(m2.Matches(b)); |
| 382 | } |
| 383 | |
| 384 | // Tests that An<T>() describes itself properly. |
| 385 | TEST(AnTest, CanDescribeSelf) { |
| 386 | EXPECT_EQ("is anything", Describe(An<int>())); |
| 387 | } |
| 388 | |
| 389 | // Tests that _ can be used as a matcher for any type and matches any |
| 390 | // value of that type. |
| 391 | TEST(UnderscoreTest, MatchesAnyValue) { |
| 392 | // Uses _ as a matcher for a value type. |
| 393 | Matcher<int> m1 = _; |
| 394 | EXPECT_TRUE(m1.Matches(123)); |
| 395 | EXPECT_TRUE(m1.Matches(-242)); |
| 396 | |
| 397 | // Uses _ as a matcher for a reference type. |
| 398 | bool a = false; |
| 399 | const bool b = true; |
| 400 | Matcher<const bool&> m2 = _; |
| 401 | EXPECT_TRUE(m2.Matches(a)); |
| 402 | EXPECT_TRUE(m2.Matches(b)); |
| 403 | } |
| 404 | |
| 405 | // Tests that _ describes itself properly. |
| 406 | TEST(UnderscoreTest, CanDescribeSelf) { |
| 407 | Matcher<int> m = _; |
| 408 | EXPECT_EQ("is anything", Describe(m)); |
| 409 | } |
| 410 | |
| 411 | // Tests that Eq(x) matches any value equal to x. |
| 412 | TEST(EqTest, MatchesEqualValue) { |
| 413 | // 2 C-strings with same content but different addresses. |
| 414 | const char a1[] = "hi"; |
| 415 | const char a2[] = "hi"; |
| 416 | |
| 417 | Matcher<const char*> m1 = Eq(a1); |
| 418 | EXPECT_TRUE(m1.Matches(a1)); |
| 419 | EXPECT_FALSE(m1.Matches(a2)); |
| 420 | } |
| 421 | |
| 422 | // Tests that Eq(v) describes itself properly. |
| 423 | |
| 424 | class Unprintable { |
| 425 | public: |
| 426 | Unprintable() : c_('a') {} |
| 427 | |
| 428 | bool operator==(const Unprintable& rhs) { return true; } |
| 429 | private: |
| 430 | char c_; |
| 431 | }; |
| 432 | |
| 433 | TEST(EqTest, CanDescribeSelf) { |
| 434 | Matcher<Unprintable> m = Eq(Unprintable()); |
| 435 | EXPECT_EQ("is equal to 1-byte object <61>", Describe(m)); |
| 436 | } |
| 437 | |
| 438 | // Tests that Eq(v) can be used to match any type that supports |
| 439 | // comparing with type T, where T is v's type. |
| 440 | TEST(EqTest, IsPolymorphic) { |
| 441 | Matcher<int> m1 = Eq(1); |
| 442 | EXPECT_TRUE(m1.Matches(1)); |
| 443 | EXPECT_FALSE(m1.Matches(2)); |
| 444 | |
| 445 | Matcher<char> m2 = Eq(1); |
| 446 | EXPECT_TRUE(m2.Matches('\1')); |
| 447 | EXPECT_FALSE(m2.Matches('a')); |
| 448 | } |
| 449 | |
| 450 | // Tests that TypedEq<T>(v) matches values of type T that's equal to v. |
| 451 | TEST(TypedEqTest, ChecksEqualityForGivenType) { |
| 452 | Matcher<char> m1 = TypedEq<char>('a'); |
| 453 | EXPECT_TRUE(m1.Matches('a')); |
| 454 | EXPECT_FALSE(m1.Matches('b')); |
| 455 | |
| 456 | Matcher<int> m2 = TypedEq<int>(6); |
| 457 | EXPECT_TRUE(m2.Matches(6)); |
| 458 | EXPECT_FALSE(m2.Matches(7)); |
| 459 | } |
| 460 | |
| 461 | // Tests that TypedEq(v) describes itself properly. |
| 462 | TEST(TypedEqTest, CanDescribeSelf) { |
| 463 | EXPECT_EQ("is equal to 2", Describe(TypedEq<int>(2))); |
| 464 | } |
| 465 | |
| 466 | // Tests that TypedEq<T>(v) has type Matcher<T>. |
| 467 | |
| 468 | // Type<T>::IsTypeOf(v) compiles iff the type of value v is T, where T |
| 469 | // is a "bare" type (i.e. not in the form of const U or U&). If v's |
| 470 | // type is not T, the compiler will generate a message about |
| 471 | // "undefined referece". |
| 472 | template <typename T> |
| 473 | struct Type { |
| 474 | static bool IsTypeOf(const T& v) { return true; } |
| 475 | |
| 476 | template <typename T2> |
| 477 | static void IsTypeOf(T2 v); |
| 478 | }; |
| 479 | |
| 480 | TEST(TypedEqTest, HasSpecifiedType) { |
| 481 | // Verfies that the type of TypedEq<T>(v) is Matcher<T>. |
| 482 | Type<Matcher<int> >::IsTypeOf(TypedEq<int>(5)); |
| 483 | Type<Matcher<double> >::IsTypeOf(TypedEq<double>(5)); |
| 484 | } |
| 485 | |
| 486 | // Tests that Ge(v) matches anything >= v. |
| 487 | TEST(GeTest, ImplementsGreaterThanOrEqual) { |
| 488 | Matcher<int> m1 = Ge(0); |
| 489 | EXPECT_TRUE(m1.Matches(1)); |
| 490 | EXPECT_TRUE(m1.Matches(0)); |
| 491 | EXPECT_FALSE(m1.Matches(-1)); |
| 492 | } |
| 493 | |
| 494 | // Tests that Ge(v) describes itself properly. |
| 495 | TEST(GeTest, CanDescribeSelf) { |
| 496 | Matcher<int> m = Ge(5); |
| 497 | EXPECT_EQ("is greater than or equal to 5", Describe(m)); |
| 498 | } |
| 499 | |
| 500 | // Tests that Gt(v) matches anything > v. |
| 501 | TEST(GtTest, ImplementsGreaterThan) { |
| 502 | Matcher<double> m1 = Gt(0); |
| 503 | EXPECT_TRUE(m1.Matches(1.0)); |
| 504 | EXPECT_FALSE(m1.Matches(0.0)); |
| 505 | EXPECT_FALSE(m1.Matches(-1.0)); |
| 506 | } |
| 507 | |
| 508 | // Tests that Gt(v) describes itself properly. |
| 509 | TEST(GtTest, CanDescribeSelf) { |
| 510 | Matcher<int> m = Gt(5); |
| 511 | EXPECT_EQ("is greater than 5", Describe(m)); |
| 512 | } |
| 513 | |
| 514 | // Tests that Le(v) matches anything <= v. |
| 515 | TEST(LeTest, ImplementsLessThanOrEqual) { |
| 516 | Matcher<char> m1 = Le('b'); |
| 517 | EXPECT_TRUE(m1.Matches('a')); |
| 518 | EXPECT_TRUE(m1.Matches('b')); |
| 519 | EXPECT_FALSE(m1.Matches('c')); |
| 520 | } |
| 521 | |
| 522 | // Tests that Le(v) describes itself properly. |
| 523 | TEST(LeTest, CanDescribeSelf) { |
| 524 | Matcher<int> m = Le(5); |
| 525 | EXPECT_EQ("is less than or equal to 5", Describe(m)); |
| 526 | } |
| 527 | |
| 528 | // Tests that Lt(v) matches anything < v. |
| 529 | TEST(LtTest, ImplementsLessThan) { |
| 530 | Matcher<const string&> m1 = Lt("Hello"); |
| 531 | EXPECT_TRUE(m1.Matches("Abc")); |
| 532 | EXPECT_FALSE(m1.Matches("Hello")); |
| 533 | EXPECT_FALSE(m1.Matches("Hello, world!")); |
| 534 | } |
| 535 | |
| 536 | // Tests that Lt(v) describes itself properly. |
| 537 | TEST(LtTest, CanDescribeSelf) { |
| 538 | Matcher<int> m = Lt(5); |
| 539 | EXPECT_EQ("is less than 5", Describe(m)); |
| 540 | } |
| 541 | |
| 542 | // Tests that Ne(v) matches anything != v. |
| 543 | TEST(NeTest, ImplementsNotEqual) { |
| 544 | Matcher<int> m1 = Ne(0); |
| 545 | EXPECT_TRUE(m1.Matches(1)); |
| 546 | EXPECT_TRUE(m1.Matches(-1)); |
| 547 | EXPECT_FALSE(m1.Matches(0)); |
| 548 | } |
| 549 | |
| 550 | // Tests that Ne(v) describes itself properly. |
| 551 | TEST(NeTest, CanDescribeSelf) { |
| 552 | Matcher<int> m = Ne(5); |
| 553 | EXPECT_EQ("is not equal to 5", Describe(m)); |
| 554 | } |
| 555 | |
| 556 | // Tests that NotNull() matches any non-NULL pointer of any type. |
| 557 | TEST(NotNullTest, MatchesNonNullPointer) { |
| 558 | Matcher<int*> m1 = NotNull(); |
| 559 | int* p1 = NULL; |
| 560 | int n = 0; |
| 561 | EXPECT_FALSE(m1.Matches(p1)); |
| 562 | EXPECT_TRUE(m1.Matches(&n)); |
| 563 | |
| 564 | Matcher<const char*> m2 = NotNull(); |
| 565 | const char* p2 = NULL; |
| 566 | EXPECT_FALSE(m2.Matches(p2)); |
| 567 | EXPECT_TRUE(m2.Matches("hi")); |
| 568 | } |
| 569 | |
| 570 | // Tests that NotNull() describes itself properly. |
| 571 | TEST(NotNullTest, CanDescribeSelf) { |
| 572 | Matcher<int*> m = NotNull(); |
| 573 | EXPECT_EQ("is not NULL", Describe(m)); |
| 574 | } |
| 575 | |
| 576 | // Tests that Ref(variable) matches an argument that references |
| 577 | // 'variable'. |
| 578 | TEST(RefTest, MatchesSameVariable) { |
| 579 | int a = 0; |
| 580 | int b = 0; |
| 581 | Matcher<int&> m = Ref(a); |
| 582 | EXPECT_TRUE(m.Matches(a)); |
| 583 | EXPECT_FALSE(m.Matches(b)); |
| 584 | } |
| 585 | |
| 586 | // Tests that Ref(variable) describes itself properly. |
| 587 | TEST(RefTest, CanDescribeSelf) { |
| 588 | int n = 5; |
| 589 | Matcher<int&> m = Ref(n); |
| 590 | stringstream ss; |
| 591 | ss << "references the variable @" << &n << " 5"; |
| 592 | EXPECT_EQ(string(ss.str()), Describe(m)); |
| 593 | } |
| 594 | |
| 595 | // Test that Ref(non_const_varialbe) can be used as a matcher for a |
| 596 | // const reference. |
| 597 | TEST(RefTest, CanBeUsedAsMatcherForConstReference) { |
| 598 | int a = 0; |
| 599 | int b = 0; |
| 600 | Matcher<const int&> m = Ref(a); |
| 601 | EXPECT_TRUE(m.Matches(a)); |
| 602 | EXPECT_FALSE(m.Matches(b)); |
| 603 | } |
| 604 | |
| 605 | // Tests that Ref(variable) is covariant, i.e. Ref(derived) can be |
| 606 | // used wherever Ref(base) can be used (Ref(derived) is a sub-type |
| 607 | // of Ref(base), but not vice versa. |
| 608 | |
| 609 | class Base {}; |
| 610 | class Derived : public Base {}; |
| 611 | |
| 612 | TEST(RefTest, IsCovariant) { |
| 613 | Base base, base2; |
| 614 | Derived derived; |
| 615 | Matcher<const Base&> m1 = Ref(base); |
| 616 | EXPECT_TRUE(m1.Matches(base)); |
| 617 | EXPECT_FALSE(m1.Matches(base2)); |
| 618 | EXPECT_FALSE(m1.Matches(derived)); |
| 619 | |
| 620 | m1 = Ref(derived); |
| 621 | EXPECT_TRUE(m1.Matches(derived)); |
| 622 | EXPECT_FALSE(m1.Matches(base)); |
| 623 | EXPECT_FALSE(m1.Matches(base2)); |
| 624 | } |
| 625 | |
| 626 | // Tests string comparison matchers. |
| 627 | |
| 628 | TEST(StrEqTest, MatchesEqualString) { |
| 629 | Matcher<const char*> m = StrEq(string("Hello")); |
| 630 | EXPECT_TRUE(m.Matches("Hello")); |
| 631 | EXPECT_FALSE(m.Matches("hello")); |
| 632 | EXPECT_FALSE(m.Matches(NULL)); |
| 633 | |
| 634 | Matcher<const string&> m2 = StrEq("Hello"); |
| 635 | EXPECT_TRUE(m2.Matches("Hello")); |
| 636 | EXPECT_FALSE(m2.Matches("Hi")); |
| 637 | } |
| 638 | |
| 639 | TEST(StrEqTest, CanDescribeSelf) { |
| 640 | Matcher<string> m = StrEq("Hi-\'\"\?\\\a\b\f\n\r\t\v\xD3"); |
| 641 | EXPECT_EQ("is equal to \"Hi-\'\\\"\\?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"", |
| 642 | Describe(m)); |
| 643 | |
| 644 | string str("01204500800"); |
| 645 | str[3] = '\0'; |
| 646 | Matcher<string> m2 = StrEq(str); |
| 647 | EXPECT_EQ("is equal to \"012\\04500800\"", Describe(m2)); |
| 648 | str[0] = str[6] = str[7] = str[9] = str[10] = '\0'; |
| 649 | Matcher<string> m3 = StrEq(str); |
| 650 | EXPECT_EQ("is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3)); |
| 651 | } |
| 652 | |
| 653 | TEST(StrNeTest, MatchesUnequalString) { |
| 654 | Matcher<const char*> m = StrNe("Hello"); |
| 655 | EXPECT_TRUE(m.Matches("")); |
| 656 | EXPECT_TRUE(m.Matches(NULL)); |
| 657 | EXPECT_FALSE(m.Matches("Hello")); |
| 658 | |
| 659 | Matcher<string> m2 = StrNe(string("Hello")); |
| 660 | EXPECT_TRUE(m2.Matches("hello")); |
| 661 | EXPECT_FALSE(m2.Matches("Hello")); |
| 662 | } |
| 663 | |
| 664 | TEST(StrNeTest, CanDescribeSelf) { |
| 665 | Matcher<const char*> m = StrNe("Hi"); |
| 666 | EXPECT_EQ("is not equal to \"Hi\"", Describe(m)); |
| 667 | } |
| 668 | |
| 669 | TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) { |
| 670 | Matcher<const char*> m = StrCaseEq(string("Hello")); |
| 671 | EXPECT_TRUE(m.Matches("Hello")); |
| 672 | EXPECT_TRUE(m.Matches("hello")); |
| 673 | EXPECT_FALSE(m.Matches("Hi")); |
| 674 | EXPECT_FALSE(m.Matches(NULL)); |
| 675 | |
| 676 | Matcher<const string&> m2 = StrCaseEq("Hello"); |
| 677 | EXPECT_TRUE(m2.Matches("hello")); |
| 678 | EXPECT_FALSE(m2.Matches("Hi")); |
| 679 | } |
| 680 | |
| 681 | TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) { |
| 682 | string str1("oabocdooeoo"); |
| 683 | string str2("OABOCDOOEOO"); |
| 684 | Matcher<const string&> m0 = StrCaseEq(str1); |
| 685 | EXPECT_FALSE(m0.Matches(str2 + string(1, '\0'))); |
| 686 | |
| 687 | str1[3] = str2[3] = '\0'; |
| 688 | Matcher<const string&> m1 = StrCaseEq(str1); |
| 689 | EXPECT_TRUE(m1.Matches(str2)); |
| 690 | |
| 691 | str1[0] = str1[6] = str1[7] = str1[10] = '\0'; |
| 692 | str2[0] = str2[6] = str2[7] = str2[10] = '\0'; |
| 693 | Matcher<const string&> m2 = StrCaseEq(str1); |
| 694 | str1[9] = str2[9] = '\0'; |
| 695 | EXPECT_FALSE(m2.Matches(str2)); |
| 696 | |
| 697 | Matcher<const string&> m3 = StrCaseEq(str1); |
| 698 | EXPECT_TRUE(m3.Matches(str2)); |
| 699 | |
| 700 | EXPECT_FALSE(m3.Matches(str2 + "x")); |
| 701 | str2.append(1, '\0'); |
| 702 | EXPECT_FALSE(m3.Matches(str2)); |
| 703 | EXPECT_FALSE(m3.Matches(string(str2, 0, 9))); |
| 704 | } |
| 705 | |
| 706 | TEST(StrCaseEqTest, CanDescribeSelf) { |
| 707 | Matcher<string> m = StrCaseEq("Hi"); |
| 708 | EXPECT_EQ("is equal to (ignoring case) \"Hi\"", Describe(m)); |
| 709 | } |
| 710 | |
| 711 | TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) { |
| 712 | Matcher<const char*> m = StrCaseNe("Hello"); |
| 713 | EXPECT_TRUE(m.Matches("Hi")); |
| 714 | EXPECT_TRUE(m.Matches(NULL)); |
| 715 | EXPECT_FALSE(m.Matches("Hello")); |
| 716 | EXPECT_FALSE(m.Matches("hello")); |
| 717 | |
| 718 | Matcher<string> m2 = StrCaseNe(string("Hello")); |
| 719 | EXPECT_TRUE(m2.Matches("")); |
| 720 | EXPECT_FALSE(m2.Matches("Hello")); |
| 721 | } |
| 722 | |
| 723 | TEST(StrCaseNeTest, CanDescribeSelf) { |
| 724 | Matcher<const char*> m = StrCaseNe("Hi"); |
| 725 | EXPECT_EQ("is not equal to (ignoring case) \"Hi\"", Describe(m)); |
| 726 | } |
| 727 | |
| 728 | // Tests that HasSubstr() works for matching string-typed values. |
| 729 | TEST(HasSubstrTest, WorksForStringClasses) { |
| 730 | const Matcher<string> m1 = HasSubstr("foo"); |
| 731 | EXPECT_TRUE(m1.Matches(string("I love food."))); |
| 732 | EXPECT_FALSE(m1.Matches(string("tofo"))); |
| 733 | |
| 734 | const Matcher<const std::string&> m2 = HasSubstr("foo"); |
| 735 | EXPECT_TRUE(m2.Matches(std::string("I love food."))); |
| 736 | EXPECT_FALSE(m2.Matches(std::string("tofo"))); |
| 737 | } |
| 738 | |
| 739 | // Tests that HasSubstr() works for matching C-string-typed values. |
| 740 | TEST(HasSubstrTest, WorksForCStrings) { |
| 741 | const Matcher<char*> m1 = HasSubstr("foo"); |
| 742 | EXPECT_TRUE(m1.Matches(const_cast<char*>("I love food."))); |
| 743 | EXPECT_FALSE(m1.Matches(const_cast<char*>("tofo"))); |
| 744 | EXPECT_FALSE(m1.Matches(NULL)); |
| 745 | |
| 746 | const Matcher<const char*> m2 = HasSubstr("foo"); |
| 747 | EXPECT_TRUE(m2.Matches("I love food.")); |
| 748 | EXPECT_FALSE(m2.Matches("tofo")); |
| 749 | EXPECT_FALSE(m2.Matches(NULL)); |
| 750 | } |
| 751 | |
| 752 | // Tests that HasSubstr(s) describes itself properly. |
| 753 | TEST(HasSubstrTest, CanDescribeSelf) { |
| 754 | Matcher<string> m = HasSubstr("foo\n\""); |
| 755 | EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m)); |
| 756 | } |
| 757 | |
| 758 | // Tests StartsWith(s). |
| 759 | |
| 760 | TEST(StartsWithTest, MatchesStringWithGivenPrefix) { |
| 761 | const Matcher<const char*> m1 = StartsWith(string("")); |
| 762 | EXPECT_TRUE(m1.Matches("Hi")); |
| 763 | EXPECT_TRUE(m1.Matches("")); |
| 764 | EXPECT_FALSE(m1.Matches(NULL)); |
| 765 | |
| 766 | const Matcher<const string&> m2 = StartsWith("Hi"); |
| 767 | EXPECT_TRUE(m2.Matches("Hi")); |
| 768 | EXPECT_TRUE(m2.Matches("Hi Hi!")); |
| 769 | EXPECT_TRUE(m2.Matches("High")); |
| 770 | EXPECT_FALSE(m2.Matches("H")); |
| 771 | EXPECT_FALSE(m2.Matches(" Hi")); |
| 772 | } |
| 773 | |
| 774 | TEST(StartsWithTest, CanDescribeSelf) { |
| 775 | Matcher<const std::string> m = StartsWith("Hi"); |
| 776 | EXPECT_EQ("starts with \"Hi\"", Describe(m)); |
| 777 | } |
| 778 | |
| 779 | // Tests EndsWith(s). |
| 780 | |
| 781 | TEST(EndsWithTest, MatchesStringWithGivenSuffix) { |
| 782 | const Matcher<const char*> m1 = EndsWith(""); |
| 783 | EXPECT_TRUE(m1.Matches("Hi")); |
| 784 | EXPECT_TRUE(m1.Matches("")); |
| 785 | EXPECT_FALSE(m1.Matches(NULL)); |
| 786 | |
| 787 | const Matcher<const string&> m2 = EndsWith(string("Hi")); |
| 788 | EXPECT_TRUE(m2.Matches("Hi")); |
| 789 | EXPECT_TRUE(m2.Matches("Wow Hi Hi")); |
| 790 | EXPECT_TRUE(m2.Matches("Super Hi")); |
| 791 | EXPECT_FALSE(m2.Matches("i")); |
| 792 | EXPECT_FALSE(m2.Matches("Hi ")); |
| 793 | } |
| 794 | |
| 795 | TEST(EndsWithTest, CanDescribeSelf) { |
| 796 | Matcher<const std::string> m = EndsWith("Hi"); |
| 797 | EXPECT_EQ("ends with \"Hi\"", Describe(m)); |
| 798 | } |
| 799 | |
| 800 | #ifdef GMOCK_HAS_REGEX |
| 801 | |
| 802 | // Tests MatchesRegex(). |
| 803 | |
| 804 | TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) { |
| 805 | const Matcher<const char*> m1 = MatchesRegex("a.*z"); |
| 806 | EXPECT_TRUE(m1.Matches("az")); |
| 807 | EXPECT_TRUE(m1.Matches("abcz")); |
| 808 | EXPECT_FALSE(m1.Matches(NULL)); |
| 809 | |
| 810 | const Matcher<const string&> m2 = MatchesRegex(new RE("a.*z")); |
| 811 | EXPECT_TRUE(m2.Matches("azbz")); |
| 812 | EXPECT_FALSE(m2.Matches("az1")); |
| 813 | EXPECT_FALSE(m2.Matches("1az")); |
| 814 | } |
| 815 | |
| 816 | TEST(MatchesRegexTest, CanDescribeSelf) { |
| 817 | Matcher<const std::string> m1 = MatchesRegex(string("Hi.*")); |
| 818 | EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1)); |
| 819 | |
| 820 | Matcher<const char*> m2 = MatchesRegex(new RE("[a-z].*")); |
| 821 | EXPECT_EQ("matches regular expression \"[a-z].*\"", Describe(m2)); |
| 822 | } |
| 823 | |
| 824 | // Tests ContainsRegex(). |
| 825 | |
| 826 | TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) { |
| 827 | const Matcher<const char*> m1 = ContainsRegex(string("a.*z")); |
| 828 | EXPECT_TRUE(m1.Matches("az")); |
| 829 | EXPECT_TRUE(m1.Matches("0abcz1")); |
| 830 | EXPECT_FALSE(m1.Matches(NULL)); |
| 831 | |
| 832 | const Matcher<const string&> m2 = ContainsRegex(new RE("a.*z")); |
| 833 | EXPECT_TRUE(m2.Matches("azbz")); |
| 834 | EXPECT_TRUE(m2.Matches("az1")); |
| 835 | EXPECT_FALSE(m2.Matches("1a")); |
| 836 | } |
| 837 | |
| 838 | TEST(ContainsRegexTest, CanDescribeSelf) { |
| 839 | Matcher<const std::string> m1 = ContainsRegex("Hi.*"); |
| 840 | EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1)); |
| 841 | |
| 842 | Matcher<const char*> m2 = ContainsRegex(new RE("[a-z].*")); |
| 843 | EXPECT_EQ("contains regular expression \"[a-z].*\"", Describe(m2)); |
| 844 | } |
| 845 | #endif // GMOCK_HAS_REGEX |
| 846 | |
| 847 | // Tests for wide strings. |
| 848 | #if GTEST_HAS_STD_WSTRING |
| 849 | TEST(StdWideStrEqTest, MatchesEqual) { |
| 850 | Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello")); |
| 851 | EXPECT_TRUE(m.Matches(L"Hello")); |
| 852 | EXPECT_FALSE(m.Matches(L"hello")); |
| 853 | EXPECT_FALSE(m.Matches(NULL)); |
| 854 | |
| 855 | Matcher<const ::std::wstring&> m2 = StrEq(L"Hello"); |
| 856 | EXPECT_TRUE(m2.Matches(L"Hello")); |
| 857 | EXPECT_FALSE(m2.Matches(L"Hi")); |
| 858 | |
| 859 | Matcher<const ::std::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D"); |
| 860 | EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D")); |
| 861 | EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E")); |
| 862 | |
| 863 | ::std::wstring str(L"01204500800"); |
| 864 | str[3] = L'\0'; |
| 865 | Matcher<const ::std::wstring&> m4 = StrEq(str); |
| 866 | EXPECT_TRUE(m4.Matches(str)); |
| 867 | str[0] = str[6] = str[7] = str[9] = str[10] = L'\0'; |
| 868 | Matcher<const ::std::wstring&> m5 = StrEq(str); |
| 869 | EXPECT_TRUE(m5.Matches(str)); |
| 870 | } |
| 871 | |
| 872 | TEST(StdWideStrEqTest, CanDescribeSelf) { |
| 873 | Matcher< ::std::wstring> m = StrEq(L"Hi-\'\"\?\\\a\b\f\n\r\t\v"); |
| 874 | EXPECT_EQ("is equal to L\"Hi-\'\\\"\\?\\\\\\a\\b\\f\\n\\r\\t\\v\"", |
| 875 | Describe(m)); |
| 876 | |
| 877 | Matcher< ::std::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D"); |
| 878 | EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"", |
| 879 | Describe(m2)); |
| 880 | |
| 881 | ::std::wstring str(L"01204500800"); |
| 882 | str[3] = L'\0'; |
| 883 | Matcher<const ::std::wstring&> m4 = StrEq(str); |
| 884 | EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4)); |
| 885 | str[0] = str[6] = str[7] = str[9] = str[10] = L'\0'; |
| 886 | Matcher<const ::std::wstring&> m5 = StrEq(str); |
| 887 | EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5)); |
| 888 | } |
| 889 | |
| 890 | TEST(StdWideStrNeTest, MatchesUnequalString) { |
| 891 | Matcher<const wchar_t*> m = StrNe(L"Hello"); |
| 892 | EXPECT_TRUE(m.Matches(L"")); |
| 893 | EXPECT_TRUE(m.Matches(NULL)); |
| 894 | EXPECT_FALSE(m.Matches(L"Hello")); |
| 895 | |
| 896 | Matcher< ::std::wstring> m2 = StrNe(::std::wstring(L"Hello")); |
| 897 | EXPECT_TRUE(m2.Matches(L"hello")); |
| 898 | EXPECT_FALSE(m2.Matches(L"Hello")); |
| 899 | } |
| 900 | |
| 901 | TEST(StdWideStrNeTest, CanDescribeSelf) { |
| 902 | Matcher<const wchar_t*> m = StrNe(L"Hi"); |
| 903 | EXPECT_EQ("is not equal to L\"Hi\"", Describe(m)); |
| 904 | } |
| 905 | |
| 906 | TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) { |
| 907 | Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello")); |
| 908 | EXPECT_TRUE(m.Matches(L"Hello")); |
| 909 | EXPECT_TRUE(m.Matches(L"hello")); |
| 910 | EXPECT_FALSE(m.Matches(L"Hi")); |
| 911 | EXPECT_FALSE(m.Matches(NULL)); |
| 912 | |
| 913 | Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello"); |
| 914 | EXPECT_TRUE(m2.Matches(L"hello")); |
| 915 | EXPECT_FALSE(m2.Matches(L"Hi")); |
| 916 | } |
| 917 | |
| 918 | TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) { |
| 919 | ::std::wstring str1(L"oabocdooeoo"); |
| 920 | ::std::wstring str2(L"OABOCDOOEOO"); |
| 921 | Matcher<const ::std::wstring&> m0 = StrCaseEq(str1); |
| 922 | EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L'\0'))); |
| 923 | |
| 924 | str1[3] = str2[3] = L'\0'; |
| 925 | Matcher<const ::std::wstring&> m1 = StrCaseEq(str1); |
| 926 | EXPECT_TRUE(m1.Matches(str2)); |
| 927 | |
| 928 | str1[0] = str1[6] = str1[7] = str1[10] = L'\0'; |
| 929 | str2[0] = str2[6] = str2[7] = str2[10] = L'\0'; |
| 930 | Matcher<const ::std::wstring&> m2 = StrCaseEq(str1); |
| 931 | str1[9] = str2[9] = L'\0'; |
| 932 | EXPECT_FALSE(m2.Matches(str2)); |
| 933 | |
| 934 | Matcher<const ::std::wstring&> m3 = StrCaseEq(str1); |
| 935 | EXPECT_TRUE(m3.Matches(str2)); |
| 936 | |
| 937 | EXPECT_FALSE(m3.Matches(str2 + L"x")); |
| 938 | str2.append(1, L'\0'); |
| 939 | EXPECT_FALSE(m3.Matches(str2)); |
| 940 | EXPECT_FALSE(m3.Matches(::std::wstring(str2, 0, 9))); |
| 941 | } |
| 942 | |
| 943 | TEST(StdWideStrCaseEqTest, CanDescribeSelf) { |
| 944 | Matcher< ::std::wstring> m = StrCaseEq(L"Hi"); |
| 945 | EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m)); |
| 946 | } |
| 947 | |
| 948 | TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) { |
| 949 | Matcher<const wchar_t*> m = StrCaseNe(L"Hello"); |
| 950 | EXPECT_TRUE(m.Matches(L"Hi")); |
| 951 | EXPECT_TRUE(m.Matches(NULL)); |
| 952 | EXPECT_FALSE(m.Matches(L"Hello")); |
| 953 | EXPECT_FALSE(m.Matches(L"hello")); |
| 954 | |
| 955 | Matcher< ::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello")); |
| 956 | EXPECT_TRUE(m2.Matches(L"")); |
| 957 | EXPECT_FALSE(m2.Matches(L"Hello")); |
| 958 | } |
| 959 | |
| 960 | TEST(StdWideStrCaseNeTest, CanDescribeSelf) { |
| 961 | Matcher<const wchar_t*> m = StrCaseNe(L"Hi"); |
| 962 | EXPECT_EQ("is not equal to (ignoring case) L\"Hi\"", Describe(m)); |
| 963 | } |
| 964 | |
| 965 | // Tests that HasSubstr() works for matching wstring-typed values. |
| 966 | TEST(StdWideHasSubstrTest, WorksForStringClasses) { |
| 967 | const Matcher< ::std::wstring> m1 = HasSubstr(L"foo"); |
| 968 | EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food."))); |
| 969 | EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo"))); |
| 970 | |
| 971 | const Matcher<const ::std::wstring&> m2 = HasSubstr(L"foo"); |
| 972 | EXPECT_TRUE(m2.Matches(::std::wstring(L"I love food."))); |
| 973 | EXPECT_FALSE(m2.Matches(::std::wstring(L"tofo"))); |
| 974 | } |
| 975 | |
| 976 | // Tests that HasSubstr() works for matching C-wide-string-typed values. |
| 977 | TEST(StdWideHasSubstrTest, WorksForCStrings) { |
| 978 | const Matcher<wchar_t*> m1 = HasSubstr(L"foo"); |
| 979 | EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food."))); |
| 980 | EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo"))); |
| 981 | EXPECT_FALSE(m1.Matches(NULL)); |
| 982 | |
| 983 | const Matcher<const wchar_t*> m2 = HasSubstr(L"foo"); |
| 984 | EXPECT_TRUE(m2.Matches(L"I love food.")); |
| 985 | EXPECT_FALSE(m2.Matches(L"tofo")); |
| 986 | EXPECT_FALSE(m2.Matches(NULL)); |
| 987 | } |
| 988 | |
| 989 | // Tests that HasSubstr(s) describes itself properly. |
| 990 | TEST(StdWideHasSubstrTest, CanDescribeSelf) { |
| 991 | Matcher< ::std::wstring> m = HasSubstr(L"foo\n\""); |
| 992 | EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m)); |
| 993 | } |
| 994 | |
| 995 | // Tests StartsWith(s). |
| 996 | |
| 997 | TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) { |
| 998 | const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(L"")); |
| 999 | EXPECT_TRUE(m1.Matches(L"Hi")); |
| 1000 | EXPECT_TRUE(m1.Matches(L"")); |
| 1001 | EXPECT_FALSE(m1.Matches(NULL)); |
| 1002 | |
| 1003 | const Matcher<const ::std::wstring&> m2 = StartsWith(L"Hi"); |
| 1004 | EXPECT_TRUE(m2.Matches(L"Hi")); |
| 1005 | EXPECT_TRUE(m2.Matches(L"Hi Hi!")); |
| 1006 | EXPECT_TRUE(m2.Matches(L"High")); |
| 1007 | EXPECT_FALSE(m2.Matches(L"H")); |
| 1008 | EXPECT_FALSE(m2.Matches(L" Hi")); |
| 1009 | } |
| 1010 | |
| 1011 | TEST(StdWideStartsWithTest, CanDescribeSelf) { |
| 1012 | Matcher<const ::std::wstring> m = StartsWith(L"Hi"); |
| 1013 | EXPECT_EQ("starts with L\"Hi\"", Describe(m)); |
| 1014 | } |
| 1015 | |
| 1016 | // Tests EndsWith(s). |
| 1017 | |
| 1018 | TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) { |
| 1019 | const Matcher<const wchar_t*> m1 = EndsWith(L""); |
| 1020 | EXPECT_TRUE(m1.Matches(L"Hi")); |
| 1021 | EXPECT_TRUE(m1.Matches(L"")); |
| 1022 | EXPECT_FALSE(m1.Matches(NULL)); |
| 1023 | |
| 1024 | const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L"Hi")); |
| 1025 | EXPECT_TRUE(m2.Matches(L"Hi")); |
| 1026 | EXPECT_TRUE(m2.Matches(L"Wow Hi Hi")); |
| 1027 | EXPECT_TRUE(m2.Matches(L"Super Hi")); |
| 1028 | EXPECT_FALSE(m2.Matches(L"i")); |
| 1029 | EXPECT_FALSE(m2.Matches(L"Hi ")); |
| 1030 | } |
| 1031 | |
| 1032 | TEST(StdWideEndsWithTest, CanDescribeSelf) { |
| 1033 | Matcher<const ::std::wstring> m = EndsWith(L"Hi"); |
| 1034 | EXPECT_EQ("ends with L\"Hi\"", Describe(m)); |
| 1035 | } |
| 1036 | |
| 1037 | #endif // GTEST_HAS_STD_WSTRING |
| 1038 | |
| 1039 | #if GTEST_HAS_GLOBAL_WSTRING |
| 1040 | TEST(GlobalWideStrEqTest, MatchesEqual) { |
| 1041 | Matcher<const wchar_t*> m = StrEq(::wstring(L"Hello")); |
| 1042 | EXPECT_TRUE(m.Matches(L"Hello")); |
| 1043 | EXPECT_FALSE(m.Matches(L"hello")); |
| 1044 | EXPECT_FALSE(m.Matches(NULL)); |
| 1045 | |
| 1046 | Matcher<const ::wstring&> m2 = StrEq(L"Hello"); |
| 1047 | EXPECT_TRUE(m2.Matches(L"Hello")); |
| 1048 | EXPECT_FALSE(m2.Matches(L"Hi")); |
| 1049 | |
| 1050 | Matcher<const ::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D"); |
| 1051 | EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D")); |
| 1052 | EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E")); |
| 1053 | |
| 1054 | ::wstring str(L"01204500800"); |
| 1055 | str[3] = L'\0'; |
| 1056 | Matcher<const ::wstring&> m4 = StrEq(str); |
| 1057 | EXPECT_TRUE(m4.Matches(str)); |
| 1058 | str[0] = str[6] = str[7] = str[9] = str[10] = L'\0'; |
| 1059 | Matcher<const ::wstring&> m5 = StrEq(str); |
| 1060 | EXPECT_TRUE(m5.Matches(str)); |
| 1061 | } |
| 1062 | |
| 1063 | TEST(GlobalWideStrEqTest, CanDescribeSelf) { |
| 1064 | Matcher< ::wstring> m = StrEq(L"Hi-\'\"\?\\\a\b\f\n\r\t\v"); |
| 1065 | EXPECT_EQ("is equal to L\"Hi-\'\\\"\\?\\\\\\a\\b\\f\\n\\r\\t\\v\"", |
| 1066 | Describe(m)); |
| 1067 | |
| 1068 | Matcher< ::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D"); |
| 1069 | EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"", |
| 1070 | Describe(m2)); |
| 1071 | |
| 1072 | ::wstring str(L"01204500800"); |
| 1073 | str[3] = L'\0'; |
| 1074 | Matcher<const ::wstring&> m4 = StrEq(str); |
| 1075 | EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4)); |
| 1076 | str[0] = str[6] = str[7] = str[9] = str[10] = L'\0'; |
| 1077 | Matcher<const ::wstring&> m5 = StrEq(str); |
| 1078 | EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5)); |
| 1079 | } |
| 1080 | |
| 1081 | TEST(GlobalWideStrNeTest, MatchesUnequalString) { |
| 1082 | Matcher<const wchar_t*> m = StrNe(L"Hello"); |
| 1083 | EXPECT_TRUE(m.Matches(L"")); |
| 1084 | EXPECT_TRUE(m.Matches(NULL)); |
| 1085 | EXPECT_FALSE(m.Matches(L"Hello")); |
| 1086 | |
| 1087 | Matcher< ::wstring> m2 = StrNe(::wstring(L"Hello")); |
| 1088 | EXPECT_TRUE(m2.Matches(L"hello")); |
| 1089 | EXPECT_FALSE(m2.Matches(L"Hello")); |
| 1090 | } |
| 1091 | |
| 1092 | TEST(GlobalWideStrNeTest, CanDescribeSelf) { |
| 1093 | Matcher<const wchar_t*> m = StrNe(L"Hi"); |
| 1094 | EXPECT_EQ("is not equal to L\"Hi\"", Describe(m)); |
| 1095 | } |
| 1096 | |
| 1097 | TEST(GlobalWideStrCaseEqTest, MatchesEqualStringIgnoringCase) { |
| 1098 | Matcher<const wchar_t*> m = StrCaseEq(::wstring(L"Hello")); |
| 1099 | EXPECT_TRUE(m.Matches(L"Hello")); |
| 1100 | EXPECT_TRUE(m.Matches(L"hello")); |
| 1101 | EXPECT_FALSE(m.Matches(L"Hi")); |
| 1102 | EXPECT_FALSE(m.Matches(NULL)); |
| 1103 | |
| 1104 | Matcher<const ::wstring&> m2 = StrCaseEq(L"Hello"); |
| 1105 | EXPECT_TRUE(m2.Matches(L"hello")); |
| 1106 | EXPECT_FALSE(m2.Matches(L"Hi")); |
| 1107 | } |
| 1108 | |
| 1109 | TEST(GlobalWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) { |
| 1110 | ::wstring str1(L"oabocdooeoo"); |
| 1111 | ::wstring str2(L"OABOCDOOEOO"); |
| 1112 | Matcher<const ::wstring&> m0 = StrCaseEq(str1); |
| 1113 | EXPECT_FALSE(m0.Matches(str2 + ::wstring(1, L'\0'))); |
| 1114 | |
| 1115 | str1[3] = str2[3] = L'\0'; |
| 1116 | Matcher<const ::wstring&> m1 = StrCaseEq(str1); |
| 1117 | EXPECT_TRUE(m1.Matches(str2)); |
| 1118 | |
| 1119 | str1[0] = str1[6] = str1[7] = str1[10] = L'\0'; |
| 1120 | str2[0] = str2[6] = str2[7] = str2[10] = L'\0'; |
| 1121 | Matcher<const ::wstring&> m2 = StrCaseEq(str1); |
| 1122 | str1[9] = str2[9] = L'\0'; |
| 1123 | EXPECT_FALSE(m2.Matches(str2)); |
| 1124 | |
| 1125 | Matcher<const ::wstring&> m3 = StrCaseEq(str1); |
| 1126 | EXPECT_TRUE(m3.Matches(str2)); |
| 1127 | |
| 1128 | EXPECT_FALSE(m3.Matches(str2 + L"x")); |
| 1129 | str2.append(1, L'\0'); |
| 1130 | EXPECT_FALSE(m3.Matches(str2)); |
| 1131 | EXPECT_FALSE(m3.Matches(::wstring(str2, 0, 9))); |
| 1132 | } |
| 1133 | |
| 1134 | TEST(GlobalWideStrCaseEqTest, CanDescribeSelf) { |
| 1135 | Matcher< ::wstring> m = StrCaseEq(L"Hi"); |
| 1136 | EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m)); |
| 1137 | } |
| 1138 | |
| 1139 | TEST(GlobalWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) { |
| 1140 | Matcher<const wchar_t*> m = StrCaseNe(L"Hello"); |
| 1141 | EXPECT_TRUE(m.Matches(L"Hi")); |
| 1142 | EXPECT_TRUE(m.Matches(NULL)); |
| 1143 | EXPECT_FALSE(m.Matches(L"Hello")); |
| 1144 | EXPECT_FALSE(m.Matches(L"hello")); |
| 1145 | |
| 1146 | Matcher< ::wstring> m2 = StrCaseNe(::wstring(L"Hello")); |
| 1147 | EXPECT_TRUE(m2.Matches(L"")); |
| 1148 | EXPECT_FALSE(m2.Matches(L"Hello")); |
| 1149 | } |
| 1150 | |
| 1151 | TEST(GlobalWideStrCaseNeTest, CanDescribeSelf) { |
| 1152 | Matcher<const wchar_t*> m = StrCaseNe(L"Hi"); |
| 1153 | EXPECT_EQ("is not equal to (ignoring case) L\"Hi\"", Describe(m)); |
| 1154 | } |
| 1155 | |
| 1156 | // Tests that HasSubstr() works for matching wstring-typed values. |
| 1157 | TEST(GlobalWideHasSubstrTest, WorksForStringClasses) { |
| 1158 | const Matcher< ::wstring> m1 = HasSubstr(L"foo"); |
| 1159 | EXPECT_TRUE(m1.Matches(::wstring(L"I love food."))); |
| 1160 | EXPECT_FALSE(m1.Matches(::wstring(L"tofo"))); |
| 1161 | |
| 1162 | const Matcher<const ::wstring&> m2 = HasSubstr(L"foo"); |
| 1163 | EXPECT_TRUE(m2.Matches(::wstring(L"I love food."))); |
| 1164 | EXPECT_FALSE(m2.Matches(::wstring(L"tofo"))); |
| 1165 | } |
| 1166 | |
| 1167 | // Tests that HasSubstr() works for matching C-wide-string-typed values. |
| 1168 | TEST(GlobalWideHasSubstrTest, WorksForCStrings) { |
| 1169 | const Matcher<wchar_t*> m1 = HasSubstr(L"foo"); |
| 1170 | EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food."))); |
| 1171 | EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo"))); |
| 1172 | EXPECT_FALSE(m1.Matches(NULL)); |
| 1173 | |
| 1174 | const Matcher<const wchar_t*> m2 = HasSubstr(L"foo"); |
| 1175 | EXPECT_TRUE(m2.Matches(L"I love food.")); |
| 1176 | EXPECT_FALSE(m2.Matches(L"tofo")); |
| 1177 | EXPECT_FALSE(m2.Matches(NULL)); |
| 1178 | } |
| 1179 | |
| 1180 | // Tests that HasSubstr(s) describes itself properly. |
| 1181 | TEST(GlobalWideHasSubstrTest, CanDescribeSelf) { |
| 1182 | Matcher< ::wstring> m = HasSubstr(L"foo\n\""); |
| 1183 | EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m)); |
| 1184 | } |
| 1185 | |
| 1186 | // Tests StartsWith(s). |
| 1187 | |
| 1188 | TEST(GlobalWideStartsWithTest, MatchesStringWithGivenPrefix) { |
| 1189 | const Matcher<const wchar_t*> m1 = StartsWith(::wstring(L"")); |
| 1190 | EXPECT_TRUE(m1.Matches(L"Hi")); |
| 1191 | EXPECT_TRUE(m1.Matches(L"")); |
| 1192 | EXPECT_FALSE(m1.Matches(NULL)); |
| 1193 | |
| 1194 | const Matcher<const ::wstring&> m2 = StartsWith(L"Hi"); |
| 1195 | EXPECT_TRUE(m2.Matches(L"Hi")); |
| 1196 | EXPECT_TRUE(m2.Matches(L"Hi Hi!")); |
| 1197 | EXPECT_TRUE(m2.Matches(L"High")); |
| 1198 | EXPECT_FALSE(m2.Matches(L"H")); |
| 1199 | EXPECT_FALSE(m2.Matches(L" Hi")); |
| 1200 | } |
| 1201 | |
| 1202 | TEST(GlobalWideStartsWithTest, CanDescribeSelf) { |
| 1203 | Matcher<const ::wstring> m = StartsWith(L"Hi"); |
| 1204 | EXPECT_EQ("starts with L\"Hi\"", Describe(m)); |
| 1205 | } |
| 1206 | |
| 1207 | // Tests EndsWith(s). |
| 1208 | |
| 1209 | TEST(GlobalWideEndsWithTest, MatchesStringWithGivenSuffix) { |
| 1210 | const Matcher<const wchar_t*> m1 = EndsWith(L""); |
| 1211 | EXPECT_TRUE(m1.Matches(L"Hi")); |
| 1212 | EXPECT_TRUE(m1.Matches(L"")); |
| 1213 | EXPECT_FALSE(m1.Matches(NULL)); |
| 1214 | |
| 1215 | const Matcher<const ::wstring&> m2 = EndsWith(::wstring(L"Hi")); |
| 1216 | EXPECT_TRUE(m2.Matches(L"Hi")); |
| 1217 | EXPECT_TRUE(m2.Matches(L"Wow Hi Hi")); |
| 1218 | EXPECT_TRUE(m2.Matches(L"Super Hi")); |
| 1219 | EXPECT_FALSE(m2.Matches(L"i")); |
| 1220 | EXPECT_FALSE(m2.Matches(L"Hi ")); |
| 1221 | } |
| 1222 | |
| 1223 | TEST(GlobalWideEndsWithTest, CanDescribeSelf) { |
| 1224 | Matcher<const ::wstring> m = EndsWith(L"Hi"); |
| 1225 | EXPECT_EQ("ends with L\"Hi\"", Describe(m)); |
| 1226 | } |
| 1227 | |
| 1228 | #endif // GTEST_HAS_GLOBAL_WSTRING |
| 1229 | |
| 1230 | |
| 1231 | typedef ::std::tr1::tuple<long, int> Tuple2; // NOLINT |
| 1232 | |
| 1233 | // Tests that Eq() matches a 2-tuple where the first field == the |
| 1234 | // second field. |
| 1235 | TEST(Eq2Test, MatchesEqualArguments) { |
| 1236 | Matcher<const Tuple2&> m = Eq(); |
| 1237 | EXPECT_TRUE(m.Matches(Tuple2(5L, 5))); |
| 1238 | EXPECT_FALSE(m.Matches(Tuple2(5L, 6))); |
| 1239 | } |
| 1240 | |
| 1241 | // Tests that Eq() describes itself properly. |
| 1242 | TEST(Eq2Test, CanDescribeSelf) { |
| 1243 | Matcher<const Tuple2&> m = Eq(); |
| 1244 | EXPECT_EQ("argument #0 is equal to argument #1", Describe(m)); |
| 1245 | } |
| 1246 | |
| 1247 | // Tests that Ge() matches a 2-tuple where the first field >= the |
| 1248 | // second field. |
| 1249 | TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) { |
| 1250 | Matcher<const Tuple2&> m = Ge(); |
| 1251 | EXPECT_TRUE(m.Matches(Tuple2(5L, 4))); |
| 1252 | EXPECT_TRUE(m.Matches(Tuple2(5L, 5))); |
| 1253 | EXPECT_FALSE(m.Matches(Tuple2(5L, 6))); |
| 1254 | } |
| 1255 | |
| 1256 | // Tests that Ge() describes itself properly. |
| 1257 | TEST(Ge2Test, CanDescribeSelf) { |
| 1258 | Matcher<const Tuple2&> m = Ge(); |
| 1259 | EXPECT_EQ("argument #0 is greater than or equal to argument #1", |
| 1260 | Describe(m)); |
| 1261 | } |
| 1262 | |
| 1263 | // Tests that Gt() matches a 2-tuple where the first field > the |
| 1264 | // second field. |
| 1265 | TEST(Gt2Test, MatchesGreaterThanArguments) { |
| 1266 | Matcher<const Tuple2&> m = Gt(); |
| 1267 | EXPECT_TRUE(m.Matches(Tuple2(5L, 4))); |
| 1268 | EXPECT_FALSE(m.Matches(Tuple2(5L, 5))); |
| 1269 | EXPECT_FALSE(m.Matches(Tuple2(5L, 6))); |
| 1270 | } |
| 1271 | |
| 1272 | // Tests that Gt() describes itself properly. |
| 1273 | TEST(Gt2Test, CanDescribeSelf) { |
| 1274 | Matcher<const Tuple2&> m = Gt(); |
| 1275 | EXPECT_EQ("argument #0 is greater than argument #1", Describe(m)); |
| 1276 | } |
| 1277 | |
| 1278 | // Tests that Le() matches a 2-tuple where the first field <= the |
| 1279 | // second field. |
| 1280 | TEST(Le2Test, MatchesLessThanOrEqualArguments) { |
| 1281 | Matcher<const Tuple2&> m = Le(); |
| 1282 | EXPECT_TRUE(m.Matches(Tuple2(5L, 6))); |
| 1283 | EXPECT_TRUE(m.Matches(Tuple2(5L, 5))); |
| 1284 | EXPECT_FALSE(m.Matches(Tuple2(5L, 4))); |
| 1285 | } |
| 1286 | |
| 1287 | // Tests that Le() describes itself properly. |
| 1288 | TEST(Le2Test, CanDescribeSelf) { |
| 1289 | Matcher<const Tuple2&> m = Le(); |
| 1290 | EXPECT_EQ("argument #0 is less than or equal to argument #1", |
| 1291 | Describe(m)); |
| 1292 | } |
| 1293 | |
| 1294 | // Tests that Lt() matches a 2-tuple where the first field < the |
| 1295 | // second field. |
| 1296 | TEST(Lt2Test, MatchesLessThanArguments) { |
| 1297 | Matcher<const Tuple2&> m = Lt(); |
| 1298 | EXPECT_TRUE(m.Matches(Tuple2(5L, 6))); |
| 1299 | EXPECT_FALSE(m.Matches(Tuple2(5L, 5))); |
| 1300 | EXPECT_FALSE(m.Matches(Tuple2(5L, 4))); |
| 1301 | } |
| 1302 | |
| 1303 | // Tests that Lt() describes itself properly. |
| 1304 | TEST(Lt2Test, CanDescribeSelf) { |
| 1305 | Matcher<const Tuple2&> m = Lt(); |
| 1306 | EXPECT_EQ("argument #0 is less than argument #1", Describe(m)); |
| 1307 | } |
| 1308 | |
| 1309 | // Tests that Ne() matches a 2-tuple where the first field != the |
| 1310 | // second field. |
| 1311 | TEST(Ne2Test, MatchesUnequalArguments) { |
| 1312 | Matcher<const Tuple2&> m = Ne(); |
| 1313 | EXPECT_TRUE(m.Matches(Tuple2(5L, 6))); |
| 1314 | EXPECT_TRUE(m.Matches(Tuple2(5L, 4))); |
| 1315 | EXPECT_FALSE(m.Matches(Tuple2(5L, 5))); |
| 1316 | } |
| 1317 | |
| 1318 | // Tests that Ne() describes itself properly. |
| 1319 | TEST(Ne2Test, CanDescribeSelf) { |
| 1320 | Matcher<const Tuple2&> m = Ne(); |
| 1321 | EXPECT_EQ("argument #0 is not equal to argument #1", Describe(m)); |
| 1322 | } |
| 1323 | |
| 1324 | // Tests that Not(m) matches any value that doesn't match m. |
| 1325 | TEST(NotTest, NegatesMatcher) { |
| 1326 | Matcher<int> m; |
| 1327 | m = Not(Eq(2)); |
| 1328 | EXPECT_TRUE(m.Matches(3)); |
| 1329 | EXPECT_FALSE(m.Matches(2)); |
| 1330 | } |
| 1331 | |
| 1332 | // Tests that Not(m) describes itself properly. |
| 1333 | TEST(NotTest, CanDescribeSelf) { |
| 1334 | Matcher<int> m = Not(Eq(5)); |
| 1335 | EXPECT_EQ("is not equal to 5", Describe(m)); |
| 1336 | } |
| 1337 | |
| 1338 | // Tests that AllOf(m1, ..., mn) matches any value that matches all of |
| 1339 | // the given matchers. |
| 1340 | TEST(AllOfTest, MatchesWhenAllMatch) { |
| 1341 | Matcher<int> m; |
| 1342 | m = AllOf(Le(2), Ge(1)); |
| 1343 | EXPECT_TRUE(m.Matches(1)); |
| 1344 | EXPECT_TRUE(m.Matches(2)); |
| 1345 | EXPECT_FALSE(m.Matches(0)); |
| 1346 | EXPECT_FALSE(m.Matches(3)); |
| 1347 | |
| 1348 | m = AllOf(Gt(0), Ne(1), Ne(2)); |
| 1349 | EXPECT_TRUE(m.Matches(3)); |
| 1350 | EXPECT_FALSE(m.Matches(2)); |
| 1351 | EXPECT_FALSE(m.Matches(1)); |
| 1352 | EXPECT_FALSE(m.Matches(0)); |
| 1353 | |
| 1354 | m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3)); |
| 1355 | EXPECT_TRUE(m.Matches(4)); |
| 1356 | EXPECT_FALSE(m.Matches(3)); |
| 1357 | EXPECT_FALSE(m.Matches(2)); |
| 1358 | EXPECT_FALSE(m.Matches(1)); |
| 1359 | EXPECT_FALSE(m.Matches(0)); |
| 1360 | |
| 1361 | m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7)); |
| 1362 | EXPECT_TRUE(m.Matches(0)); |
| 1363 | EXPECT_TRUE(m.Matches(1)); |
| 1364 | EXPECT_FALSE(m.Matches(3)); |
| 1365 | } |
| 1366 | |
| 1367 | // Tests that AllOf(m1, ..., mn) describes itself properly. |
| 1368 | TEST(AllOfTest, CanDescribeSelf) { |
| 1369 | Matcher<int> m; |
| 1370 | m = AllOf(Le(2), Ge(1)); |
| 1371 | EXPECT_EQ("(is less than or equal to 2) and " |
| 1372 | "(is greater than or equal to 1)", |
| 1373 | Describe(m)); |
| 1374 | |
| 1375 | m = AllOf(Gt(0), Ne(1), Ne(2)); |
| 1376 | EXPECT_EQ("(is greater than 0) and " |
| 1377 | "((is not equal to 1) and " |
| 1378 | "(is not equal to 2))", |
| 1379 | Describe(m)); |
| 1380 | |
| 1381 | |
| 1382 | m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3)); |
| 1383 | EXPECT_EQ("(is greater than 0) and " |
| 1384 | "((is not equal to 1) and " |
| 1385 | "((is not equal to 2) and " |
| 1386 | "(is not equal to 3)))", |
| 1387 | Describe(m)); |
| 1388 | |
| 1389 | |
| 1390 | m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7)); |
| 1391 | EXPECT_EQ("(is greater than or equal to 0) and " |
| 1392 | "((is less than 10) and " |
| 1393 | "((is not equal to 3) and " |
| 1394 | "((is not equal to 5) and " |
| 1395 | "(is not equal to 7))))", Describe(m)); |
| 1396 | } |
| 1397 | |
| 1398 | // Tests that AnyOf(m1, ..., mn) matches any value that matches at |
| 1399 | // least one of the given matchers. |
| 1400 | TEST(AnyOfTest, MatchesWhenAnyMatches) { |
| 1401 | Matcher<int> m; |
| 1402 | m = AnyOf(Le(1), Ge(3)); |
| 1403 | EXPECT_TRUE(m.Matches(1)); |
| 1404 | EXPECT_TRUE(m.Matches(4)); |
| 1405 | EXPECT_FALSE(m.Matches(2)); |
| 1406 | |
| 1407 | m = AnyOf(Lt(0), Eq(1), Eq(2)); |
| 1408 | EXPECT_TRUE(m.Matches(-1)); |
| 1409 | EXPECT_TRUE(m.Matches(1)); |
| 1410 | EXPECT_TRUE(m.Matches(2)); |
| 1411 | EXPECT_FALSE(m.Matches(0)); |
| 1412 | |
| 1413 | m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3)); |
| 1414 | EXPECT_TRUE(m.Matches(-1)); |
| 1415 | EXPECT_TRUE(m.Matches(1)); |
| 1416 | EXPECT_TRUE(m.Matches(2)); |
| 1417 | EXPECT_TRUE(m.Matches(3)); |
| 1418 | EXPECT_FALSE(m.Matches(0)); |
| 1419 | |
| 1420 | m = AnyOf(Le(0), Gt(10), 3, 5, 7); |
| 1421 | EXPECT_TRUE(m.Matches(0)); |
| 1422 | EXPECT_TRUE(m.Matches(11)); |
| 1423 | EXPECT_TRUE(m.Matches(3)); |
| 1424 | EXPECT_FALSE(m.Matches(2)); |
| 1425 | } |
| 1426 | |
| 1427 | // Tests that AnyOf(m1, ..., mn) describes itself properly. |
| 1428 | TEST(AnyOfTest, CanDescribeSelf) { |
| 1429 | Matcher<int> m; |
| 1430 | m = AnyOf(Le(1), Ge(3)); |
| 1431 | EXPECT_EQ("(is less than or equal to 1) or " |
| 1432 | "(is greater than or equal to 3)", |
| 1433 | Describe(m)); |
| 1434 | |
| 1435 | m = AnyOf(Lt(0), Eq(1), Eq(2)); |
| 1436 | EXPECT_EQ("(is less than 0) or " |
| 1437 | "((is equal to 1) or (is equal to 2))", |
| 1438 | Describe(m)); |
| 1439 | |
| 1440 | m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3)); |
| 1441 | EXPECT_EQ("(is less than 0) or " |
| 1442 | "((is equal to 1) or " |
| 1443 | "((is equal to 2) or " |
| 1444 | "(is equal to 3)))", |
| 1445 | Describe(m)); |
| 1446 | |
| 1447 | m = AnyOf(Le(0), Gt(10), 3, 5, 7); |
| 1448 | EXPECT_EQ("(is less than or equal to 0) or " |
| 1449 | "((is greater than 10) or " |
| 1450 | "((is equal to 3) or " |
| 1451 | "((is equal to 5) or " |
| 1452 | "(is equal to 7))))", |
| 1453 | Describe(m)); |
| 1454 | } |
| 1455 | |
| 1456 | // The following predicate function and predicate functor are for |
| 1457 | // testing the Truly(predicate) matcher. |
| 1458 | |
| 1459 | // Returns non-zero if the input is positive. Note that the return |
| 1460 | // type of this function is not bool. It's OK as Truly() accepts any |
| 1461 | // unary function or functor whose return type can be implicitly |
| 1462 | // converted to bool. |
| 1463 | int IsPositive(double x) { |
| 1464 | return x > 0 ? 1 : 0; |
| 1465 | } |
| 1466 | |
| 1467 | // This functor returns true if the input is greater than the given |
| 1468 | // number. |
| 1469 | class IsGreaterThan { |
| 1470 | public: |
| 1471 | explicit IsGreaterThan(int threshold) : threshold_(threshold) {} |
| 1472 | |
| 1473 | bool operator()(int n) const { return n > threshold_; } |
| 1474 | private: |
| 1475 | const int threshold_; |
| 1476 | }; |
| 1477 | |
| 1478 | // For testing Truly(). |
| 1479 | const int foo = 0; |
| 1480 | |
| 1481 | // This predicate returns true iff the argument references foo and has |
| 1482 | // a zero value. |
| 1483 | bool ReferencesFooAndIsZero(const int& n) { |
| 1484 | return (&n == &foo) && (n == 0); |
| 1485 | } |
| 1486 | |
| 1487 | // Tests that Truly(predicate) matches what satisfies the given |
| 1488 | // predicate. |
| 1489 | TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) { |
| 1490 | Matcher<double> m = Truly(IsPositive); |
| 1491 | EXPECT_TRUE(m.Matches(2.0)); |
| 1492 | EXPECT_FALSE(m.Matches(-1.5)); |
| 1493 | } |
| 1494 | |
| 1495 | // Tests that Truly(predicate_functor) works too. |
| 1496 | TEST(TrulyTest, CanBeUsedWithFunctor) { |
| 1497 | Matcher<int> m = Truly(IsGreaterThan(5)); |
| 1498 | EXPECT_TRUE(m.Matches(6)); |
| 1499 | EXPECT_FALSE(m.Matches(4)); |
| 1500 | } |
| 1501 | |
| 1502 | // Tests that Truly(predicate) can describe itself properly. |
| 1503 | TEST(TrulyTest, CanDescribeSelf) { |
| 1504 | Matcher<double> m = Truly(IsPositive); |
| 1505 | EXPECT_EQ("satisfies the given predicate", |
| 1506 | Describe(m)); |
| 1507 | } |
| 1508 | |
| 1509 | // Tests that Truly(predicate) works when the matcher takes its |
| 1510 | // argument by reference. |
| 1511 | TEST(TrulyTest, WorksForByRefArguments) { |
| 1512 | Matcher<const int&> m = Truly(ReferencesFooAndIsZero); |
| 1513 | EXPECT_TRUE(m.Matches(foo)); |
| 1514 | int n = 0; |
| 1515 | EXPECT_FALSE(m.Matches(n)); |
| 1516 | } |
| 1517 | |
| 1518 | // Tests that Matches(m) is a predicate satisfied by whatever that |
| 1519 | // matches matcher m. |
| 1520 | TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) { |
| 1521 | EXPECT_TRUE(Matches(Ge(0))(1)); |
| 1522 | EXPECT_FALSE(Matches(Eq('a'))('b')); |
| 1523 | } |
| 1524 | |
| 1525 | // Tests that Matches(m) works when the matcher takes its argument by |
| 1526 | // reference. |
| 1527 | TEST(MatchesTest, WorksOnByRefArguments) { |
| 1528 | int m = 0, n = 0; |
| 1529 | EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n)); |
| 1530 | EXPECT_FALSE(Matches(Ref(m))(n)); |
| 1531 | } |
| 1532 | |
| 1533 | // Tests that a Matcher on non-reference type can be used in |
| 1534 | // Matches(). |
| 1535 | TEST(MatchesTest, WorksWithMatcherOnNonRefType) { |
| 1536 | Matcher<int> eq5 = Eq(5); |
| 1537 | EXPECT_TRUE(Matches(eq5)(5)); |
| 1538 | EXPECT_FALSE(Matches(eq5)(2)); |
| 1539 | } |
| 1540 | |
| 1541 | // Tests that ASSERT_THAT() and EXPECT_THAT() work when the value |
| 1542 | // matches the matcher. |
| 1543 | TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) { |
| 1544 | ASSERT_THAT(5, Ge(2)) << "This should succeed."; |
| 1545 | ASSERT_THAT("Foo", EndsWith("oo")); |
| 1546 | EXPECT_THAT(2, AllOf(Le(7), Ge(0))) << "This should succeed too."; |
| 1547 | EXPECT_THAT("Hello", StartsWith("Hell")); |
| 1548 | } |
| 1549 | |
| 1550 | // Tests that ASSERT_THAT() and EXPECT_THAT() work when the value |
| 1551 | // doesn't match the matcher. |
| 1552 | TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) { |
| 1553 | // 'n' must be static as it is used in an EXPECT_FATAL_FAILURE(), |
| 1554 | // which cannot reference auto variables. |
| 1555 | static int n; |
| 1556 | n = 5; |
| 1557 | EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Gt(10)) << "This should fail.", |
| 1558 | "Value of: n\n" |
| 1559 | "Expected: is greater than 10\n" |
| 1560 | " Actual: 5\n" |
| 1561 | "This should fail."); |
| 1562 | n = 0; |
| 1563 | EXPECT_NONFATAL_FAILURE(EXPECT_THAT(n, AllOf(Le(7), Ge(5))), |
| 1564 | "Value of: n\n" |
| 1565 | "Expected: (is less than or equal to 7) and " |
| 1566 | "(is greater than or equal to 5)\n" |
| 1567 | " Actual: 0"); |
| 1568 | } |
| 1569 | |
| 1570 | // Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument |
| 1571 | // has a reference type. |
| 1572 | TEST(MatcherAssertionTest, WorksForByRefArguments) { |
| 1573 | // We use a static variable here as EXPECT_FATAL_FAILURE() cannot |
| 1574 | // reference auto variables. |
| 1575 | static int n; |
| 1576 | n = 0; |
| 1577 | EXPECT_THAT(n, AllOf(Le(7), Ref(n))); |
| 1578 | EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Not(Ref(n))), |
| 1579 | "Value of: n\n" |
| 1580 | "Expected: does not reference the variable @"); |
| 1581 | // Tests the "Actual" part. |
| 1582 | EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Not(Ref(n))), |
| 1583 | "Actual: 0 (is located @"); |
| 1584 | } |
| 1585 | |
| 1586 | // Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is |
| 1587 | // monomorphic. |
| 1588 | TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) { |
| 1589 | Matcher<const char*> starts_with_he = StartsWith("he"); |
| 1590 | ASSERT_THAT("hello", starts_with_he); |
| 1591 | |
| 1592 | Matcher<const string&> ends_with_ok = EndsWith("ok"); |
| 1593 | ASSERT_THAT("book", ends_with_ok); |
| 1594 | |
| 1595 | Matcher<int> is_greater_than_5 = Gt(5); |
| 1596 | EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5), |
| 1597 | "Value of: 5\n" |
| 1598 | "Expected: is greater than 5\n" |
| 1599 | " Actual: 5"); |
| 1600 | } |
| 1601 | |
| 1602 | // Tests floating-point matchers. |
| 1603 | template <typename RawType> |
| 1604 | class FloatingPointTest : public testing::Test { |
| 1605 | protected: |
| 1606 | typedef typename testing::internal::FloatingPoint<RawType> Floating; |
| 1607 | typedef typename Floating::Bits Bits; |
| 1608 | |
| 1609 | virtual void SetUp() { |
| 1610 | const size_t max_ulps = Floating::kMaxUlps; |
| 1611 | |
| 1612 | // The bits that represent 0.0. |
| 1613 | const Bits zero_bits = Floating(0).bits(); |
| 1614 | |
| 1615 | // Makes some numbers close to 0.0. |
| 1616 | close_to_positive_zero_ = Floating::ReinterpretBits(zero_bits + max_ulps/2); |
| 1617 | close_to_negative_zero_ = -Floating::ReinterpretBits( |
| 1618 | zero_bits + max_ulps - max_ulps/2); |
| 1619 | further_from_negative_zero_ = -Floating::ReinterpretBits( |
| 1620 | zero_bits + max_ulps + 1 - max_ulps/2); |
| 1621 | |
| 1622 | // The bits that represent 1.0. |
| 1623 | const Bits one_bits = Floating(1).bits(); |
| 1624 | |
| 1625 | // Makes some numbers close to 1.0. |
| 1626 | close_to_one_ = Floating::ReinterpretBits(one_bits + max_ulps); |
| 1627 | further_from_one_ = Floating::ReinterpretBits(one_bits + max_ulps + 1); |
| 1628 | |
| 1629 | // +infinity. |
| 1630 | infinity_ = Floating::Infinity(); |
| 1631 | |
| 1632 | // The bits that represent +infinity. |
| 1633 | const Bits infinity_bits = Floating(infinity_).bits(); |
| 1634 | |
| 1635 | // Makes some numbers close to infinity. |
| 1636 | close_to_infinity_ = Floating::ReinterpretBits(infinity_bits - max_ulps); |
| 1637 | further_from_infinity_ = Floating::ReinterpretBits( |
| 1638 | infinity_bits - max_ulps - 1); |
| 1639 | |
| 1640 | // Makes some NAN's. |
| 1641 | nan1_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 1); |
| 1642 | nan2_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 200); |
| 1643 | } |
| 1644 | |
| 1645 | void TestSize() { |
| 1646 | EXPECT_EQ(sizeof(RawType), sizeof(Bits)); |
| 1647 | } |
| 1648 | |
| 1649 | // A battery of tests for FloatingEqMatcher::Matches. |
| 1650 | // matcher_maker is a pointer to a function which creates a FloatingEqMatcher. |
| 1651 | void TestMatches( |
| 1652 | testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) { |
| 1653 | Matcher<RawType> m1 = matcher_maker(0.0); |
| 1654 | EXPECT_TRUE(m1.Matches(-0.0)); |
| 1655 | EXPECT_TRUE(m1.Matches(close_to_positive_zero_)); |
| 1656 | EXPECT_TRUE(m1.Matches(close_to_negative_zero_)); |
| 1657 | EXPECT_FALSE(m1.Matches(1.0)); |
| 1658 | |
| 1659 | Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_); |
| 1660 | EXPECT_FALSE(m2.Matches(further_from_negative_zero_)); |
| 1661 | |
| 1662 | Matcher<RawType> m3 = matcher_maker(1.0); |
| 1663 | EXPECT_TRUE(m3.Matches(close_to_one_)); |
| 1664 | EXPECT_FALSE(m3.Matches(further_from_one_)); |
| 1665 | |
| 1666 | // Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above. |
| 1667 | EXPECT_FALSE(m3.Matches(0.0)); |
| 1668 | |
| 1669 | Matcher<RawType> m4 = matcher_maker(-infinity_); |
| 1670 | EXPECT_TRUE(m4.Matches(-close_to_infinity_)); |
| 1671 | |
| 1672 | Matcher<RawType> m5 = matcher_maker(infinity_); |
| 1673 | EXPECT_TRUE(m5.Matches(close_to_infinity_)); |
| 1674 | |
| 1675 | // This is interesting as the representations of infinity_ and nan1_ |
| 1676 | // are only 1 DLP apart. |
| 1677 | EXPECT_FALSE(m5.Matches(nan1_)); |
| 1678 | |
| 1679 | // matcher_maker can produce a Matcher<const RawType&>, which is needed in |
| 1680 | // some cases. |
| 1681 | Matcher<const RawType&> m6 = matcher_maker(0.0); |
| 1682 | EXPECT_TRUE(m6.Matches(-0.0)); |
| 1683 | EXPECT_TRUE(m6.Matches(close_to_positive_zero_)); |
| 1684 | EXPECT_FALSE(m6.Matches(1.0)); |
| 1685 | |
| 1686 | // matcher_maker can produce a Matcher<RawType&>, which is needed in some |
| 1687 | // cases. |
| 1688 | Matcher<RawType&> m7 = matcher_maker(0.0); |
| 1689 | RawType x = 0.0; |
| 1690 | EXPECT_TRUE(m7.Matches(x)); |
| 1691 | x = 0.01f; |
| 1692 | EXPECT_FALSE(m7.Matches(x)); |
| 1693 | } |
| 1694 | |
| 1695 | // Pre-calculated numbers to be used by the tests. |
| 1696 | |
| 1697 | static RawType close_to_positive_zero_; |
| 1698 | static RawType close_to_negative_zero_; |
| 1699 | static RawType further_from_negative_zero_; |
| 1700 | |
| 1701 | static RawType close_to_one_; |
| 1702 | static RawType further_from_one_; |
| 1703 | |
| 1704 | static RawType infinity_; |
| 1705 | static RawType close_to_infinity_; |
| 1706 | static RawType further_from_infinity_; |
| 1707 | |
| 1708 | static RawType nan1_; |
| 1709 | static RawType nan2_; |
| 1710 | }; |
| 1711 | |
| 1712 | template <typename RawType> |
| 1713 | RawType FloatingPointTest<RawType>::close_to_positive_zero_; |
| 1714 | |
| 1715 | template <typename RawType> |
| 1716 | RawType FloatingPointTest<RawType>::close_to_negative_zero_; |
| 1717 | |
| 1718 | template <typename RawType> |
| 1719 | RawType FloatingPointTest<RawType>::further_from_negative_zero_; |
| 1720 | |
| 1721 | template <typename RawType> |
| 1722 | RawType FloatingPointTest<RawType>::close_to_one_; |
| 1723 | |
| 1724 | template <typename RawType> |
| 1725 | RawType FloatingPointTest<RawType>::further_from_one_; |
| 1726 | |
| 1727 | template <typename RawType> |
| 1728 | RawType FloatingPointTest<RawType>::infinity_; |
| 1729 | |
| 1730 | template <typename RawType> |
| 1731 | RawType FloatingPointTest<RawType>::close_to_infinity_; |
| 1732 | |
| 1733 | template <typename RawType> |
| 1734 | RawType FloatingPointTest<RawType>::further_from_infinity_; |
| 1735 | |
| 1736 | template <typename RawType> |
| 1737 | RawType FloatingPointTest<RawType>::nan1_; |
| 1738 | |
| 1739 | template <typename RawType> |
| 1740 | RawType FloatingPointTest<RawType>::nan2_; |
| 1741 | |
| 1742 | // Instantiate FloatingPointTest for testing floats. |
| 1743 | typedef FloatingPointTest<float> FloatTest; |
| 1744 | |
| 1745 | TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) { |
| 1746 | TestMatches(&FloatEq); |
| 1747 | } |
| 1748 | |
| 1749 | TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) { |
| 1750 | TestMatches(&NanSensitiveFloatEq); |
| 1751 | } |
| 1752 | |
| 1753 | TEST_F(FloatTest, FloatEqCannotMatchNaN) { |
| 1754 | // FloatEq never matches NaN. |
| 1755 | Matcher<float> m = FloatEq(nan1_); |
| 1756 | EXPECT_FALSE(m.Matches(nan1_)); |
| 1757 | EXPECT_FALSE(m.Matches(nan2_)); |
| 1758 | EXPECT_FALSE(m.Matches(1.0)); |
| 1759 | } |
| 1760 | |
| 1761 | TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) { |
| 1762 | // NanSensitiveFloatEq will match NaN. |
| 1763 | Matcher<float> m = NanSensitiveFloatEq(nan1_); |
| 1764 | EXPECT_TRUE(m.Matches(nan1_)); |
| 1765 | EXPECT_TRUE(m.Matches(nan2_)); |
| 1766 | EXPECT_FALSE(m.Matches(1.0)); |
| 1767 | } |
| 1768 | |
| 1769 | TEST_F(FloatTest, FloatEqCanDescribeSelf) { |
| 1770 | Matcher<float> m1 = FloatEq(2.0f); |
| 1771 | EXPECT_EQ("is approximately 2", Describe(m1)); |
| 1772 | EXPECT_EQ("is not approximately 2", DescribeNegation(m1)); |
| 1773 | |
| 1774 | Matcher<float> m2 = FloatEq(0.5f); |
| 1775 | EXPECT_EQ("is approximately 0.5", Describe(m2)); |
| 1776 | EXPECT_EQ("is not approximately 0.5", DescribeNegation(m2)); |
| 1777 | |
| 1778 | Matcher<float> m3 = FloatEq(nan1_); |
| 1779 | EXPECT_EQ("never matches", Describe(m3)); |
| 1780 | EXPECT_EQ("is anything", DescribeNegation(m3)); |
| 1781 | } |
| 1782 | |
| 1783 | TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) { |
| 1784 | Matcher<float> m1 = NanSensitiveFloatEq(2.0f); |
| 1785 | EXPECT_EQ("is approximately 2", Describe(m1)); |
| 1786 | EXPECT_EQ("is not approximately 2", DescribeNegation(m1)); |
| 1787 | |
| 1788 | Matcher<float> m2 = NanSensitiveFloatEq(0.5f); |
| 1789 | EXPECT_EQ("is approximately 0.5", Describe(m2)); |
| 1790 | EXPECT_EQ("is not approximately 0.5", DescribeNegation(m2)); |
| 1791 | |
| 1792 | Matcher<float> m3 = NanSensitiveFloatEq(nan1_); |
| 1793 | EXPECT_EQ("is NaN", Describe(m3)); |
| 1794 | EXPECT_EQ("is not NaN", DescribeNegation(m3)); |
| 1795 | } |
| 1796 | |
| 1797 | // Instantiate FloatingPointTest for testing doubles. |
| 1798 | typedef FloatingPointTest<double> DoubleTest; |
| 1799 | |
| 1800 | TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) { |
| 1801 | TestMatches(&DoubleEq); |
| 1802 | } |
| 1803 | |
| 1804 | TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) { |
| 1805 | TestMatches(&NanSensitiveDoubleEq); |
| 1806 | } |
| 1807 | |
| 1808 | TEST_F(DoubleTest, DoubleEqCannotMatchNaN) { |
| 1809 | // DoubleEq never matches NaN. |
| 1810 | Matcher<double> m = DoubleEq(nan1_); |
| 1811 | EXPECT_FALSE(m.Matches(nan1_)); |
| 1812 | EXPECT_FALSE(m.Matches(nan2_)); |
| 1813 | EXPECT_FALSE(m.Matches(1.0)); |
| 1814 | } |
| 1815 | |
| 1816 | TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) { |
| 1817 | // NanSensitiveDoubleEq will match NaN. |
| 1818 | Matcher<double> m = NanSensitiveDoubleEq(nan1_); |
| 1819 | EXPECT_TRUE(m.Matches(nan1_)); |
| 1820 | EXPECT_TRUE(m.Matches(nan2_)); |
| 1821 | EXPECT_FALSE(m.Matches(1.0)); |
| 1822 | } |
| 1823 | |
| 1824 | TEST_F(DoubleTest, DoubleEqCanDescribeSelf) { |
| 1825 | Matcher<double> m1 = DoubleEq(2.0); |
| 1826 | EXPECT_EQ("is approximately 2", Describe(m1)); |
| 1827 | EXPECT_EQ("is not approximately 2", DescribeNegation(m1)); |
| 1828 | |
| 1829 | Matcher<double> m2 = DoubleEq(0.5); |
| 1830 | EXPECT_EQ("is approximately 0.5", Describe(m2)); |
| 1831 | EXPECT_EQ("is not approximately 0.5", DescribeNegation(m2)); |
| 1832 | |
| 1833 | Matcher<double> m3 = DoubleEq(nan1_); |
| 1834 | EXPECT_EQ("never matches", Describe(m3)); |
| 1835 | EXPECT_EQ("is anything", DescribeNegation(m3)); |
| 1836 | } |
| 1837 | |
| 1838 | TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) { |
| 1839 | Matcher<double> m1 = NanSensitiveDoubleEq(2.0); |
| 1840 | EXPECT_EQ("is approximately 2", Describe(m1)); |
| 1841 | EXPECT_EQ("is not approximately 2", DescribeNegation(m1)); |
| 1842 | |
| 1843 | Matcher<double> m2 = NanSensitiveDoubleEq(0.5); |
| 1844 | EXPECT_EQ("is approximately 0.5", Describe(m2)); |
| 1845 | EXPECT_EQ("is not approximately 0.5", DescribeNegation(m2)); |
| 1846 | |
| 1847 | Matcher<double> m3 = NanSensitiveDoubleEq(nan1_); |
| 1848 | EXPECT_EQ("is NaN", Describe(m3)); |
| 1849 | EXPECT_EQ("is not NaN", DescribeNegation(m3)); |
| 1850 | } |
| 1851 | |
| 1852 | TEST(PointeeTest, RawPointer) { |
| 1853 | const Matcher<int*> m = Pointee(Ge(0)); |
| 1854 | |
| 1855 | int n = 1; |
| 1856 | EXPECT_TRUE(m.Matches(&n)); |
| 1857 | n = -1; |
| 1858 | EXPECT_FALSE(m.Matches(&n)); |
| 1859 | EXPECT_FALSE(m.Matches(NULL)); |
| 1860 | } |
| 1861 | |
| 1862 | TEST(PointeeTest, RawPointerToConst) { |
| 1863 | const Matcher<const double*> m = Pointee(Ge(0)); |
| 1864 | |
| 1865 | double x = 1; |
| 1866 | EXPECT_TRUE(m.Matches(&x)); |
| 1867 | x = -1; |
| 1868 | EXPECT_FALSE(m.Matches(&x)); |
| 1869 | EXPECT_FALSE(m.Matches(NULL)); |
| 1870 | } |
| 1871 | |
| 1872 | TEST(PointeeTest, ReferenceToConstRawPointer) { |
| 1873 | const Matcher<int* const &> m = Pointee(Ge(0)); |
| 1874 | |
| 1875 | int n = 1; |
| 1876 | EXPECT_TRUE(m.Matches(&n)); |
| 1877 | n = -1; |
| 1878 | EXPECT_FALSE(m.Matches(&n)); |
| 1879 | EXPECT_FALSE(m.Matches(NULL)); |
| 1880 | } |
| 1881 | |
| 1882 | TEST(PointeeTest, ReferenceToNonConstRawPointer) { |
| 1883 | const Matcher<double* &> m = Pointee(Ge(0)); |
| 1884 | |
| 1885 | double x = 1.0; |
| 1886 | double* p = &x; |
| 1887 | EXPECT_TRUE(m.Matches(p)); |
| 1888 | x = -1; |
| 1889 | EXPECT_FALSE(m.Matches(p)); |
| 1890 | p = NULL; |
| 1891 | EXPECT_FALSE(m.Matches(p)); |
| 1892 | } |
| 1893 | |
| 1894 | TEST(PointeeTest, NeverMatchesNull) { |
| 1895 | const Matcher<const char*> m = Pointee(_); |
| 1896 | EXPECT_FALSE(m.Matches(NULL)); |
| 1897 | } |
| 1898 | |
| 1899 | // Tests that we can write Pointee(value) instead of Pointee(Eq(value)). |
| 1900 | TEST(PointeeTest, MatchesAgainstAValue) { |
| 1901 | const Matcher<int*> m = Pointee(5); |
| 1902 | |
| 1903 | int n = 5; |
| 1904 | EXPECT_TRUE(m.Matches(&n)); |
| 1905 | n = -1; |
| 1906 | EXPECT_FALSE(m.Matches(&n)); |
| 1907 | EXPECT_FALSE(m.Matches(NULL)); |
| 1908 | } |
| 1909 | |
| 1910 | TEST(PointeeTest, CanDescribeSelf) { |
| 1911 | const Matcher<int*> m = Pointee(Gt(3)); |
| 1912 | EXPECT_EQ("points to a value that is greater than 3", Describe(m)); |
| 1913 | EXPECT_EQ("does not point to a value that is greater than 3", |
| 1914 | DescribeNegation(m)); |
| 1915 | } |
| 1916 | |
| 1917 | // For testing ExplainMatchResultTo(). |
| 1918 | class GreaterThanMatcher : public MatcherInterface<int> { |
| 1919 | public: |
| 1920 | explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {} |
| 1921 | |
| 1922 | virtual bool Matches(int lhs) const { return lhs > rhs_; } |
| 1923 | |
| 1924 | virtual void DescribeTo(::std::ostream* os) const { |
| 1925 | *os << "is greater than " << rhs_; |
| 1926 | } |
| 1927 | |
| 1928 | virtual void ExplainMatchResultTo(int lhs, ::std::ostream* os) const { |
| 1929 | const int diff = lhs - rhs_; |
| 1930 | if (diff > 0) { |
| 1931 | *os << "is " << diff << " more than " << rhs_; |
| 1932 | } else if (diff == 0) { |
| 1933 | *os << "is the same as " << rhs_; |
| 1934 | } else { |
| 1935 | *os << "is " << -diff << " less than " << rhs_; |
| 1936 | } |
| 1937 | } |
| 1938 | private: |
| 1939 | const int rhs_; |
| 1940 | }; |
| 1941 | |
| 1942 | Matcher<int> GreaterThan(int n) { |
| 1943 | return MakeMatcher(new GreaterThanMatcher(n)); |
| 1944 | } |
| 1945 | |
| 1946 | TEST(PointeeTest, CanExplainMatchResult) { |
| 1947 | const Matcher<const string*> m = Pointee(StartsWith("Hi")); |
| 1948 | |
| 1949 | EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL))); |
| 1950 | |
| 1951 | const Matcher<int*> m2 = Pointee(GreaterThan(1)); |
| 1952 | int n = 3; |
| 1953 | EXPECT_EQ("points to a value that is 2 more than 1", Explain(m2, &n)); |
| 1954 | } |
| 1955 | |
| 1956 | // An uncopyable class. |
| 1957 | class Uncopyable { |
| 1958 | public: |
| 1959 | explicit Uncopyable(int value) : value_(value) {} |
| 1960 | |
| 1961 | int value() const { return value_; } |
| 1962 | private: |
| 1963 | const int value_; |
| 1964 | GTEST_DISALLOW_COPY_AND_ASSIGN_(Uncopyable); |
| 1965 | }; |
| 1966 | |
| 1967 | // Returns true iff x.value() is positive. |
| 1968 | bool ValueIsPositive(const Uncopyable& x) { return x.value() > 0; } |
| 1969 | |
| 1970 | // A user-defined struct for testing Field(). |
| 1971 | struct AStruct { |
| 1972 | AStruct() : x(0), y(1.0), z(5), p(NULL) {} |
| 1973 | AStruct(const AStruct& rhs) |
| 1974 | : x(rhs.x), y(rhs.y), z(rhs.z.value()), p(rhs.p) {} |
| 1975 | |
| 1976 | int x; // A non-const field. |
| 1977 | const double y; // A const field. |
| 1978 | Uncopyable z; // An uncopyable field. |
| 1979 | const char* p; // A pointer field. |
| 1980 | }; |
| 1981 | |
| 1982 | // A derived struct for testing Field(). |
| 1983 | struct DerivedStruct : public AStruct { |
| 1984 | char ch; |
| 1985 | }; |
| 1986 | |
| 1987 | // Tests that Field(&Foo::field, ...) works when field is non-const. |
| 1988 | TEST(FieldTest, WorksForNonConstField) { |
| 1989 | Matcher<AStruct> m = Field(&AStruct::x, Ge(0)); |
| 1990 | |
| 1991 | AStruct a; |
| 1992 | EXPECT_TRUE(m.Matches(a)); |
| 1993 | a.x = -1; |
| 1994 | EXPECT_FALSE(m.Matches(a)); |
| 1995 | } |
| 1996 | |
| 1997 | // Tests that Field(&Foo::field, ...) works when field is const. |
| 1998 | TEST(FieldTest, WorksForConstField) { |
| 1999 | AStruct a; |
| 2000 | |
| 2001 | Matcher<AStruct> m = Field(&AStruct::y, Ge(0.0)); |
| 2002 | EXPECT_TRUE(m.Matches(a)); |
| 2003 | m = Field(&AStruct::y, Le(0.0)); |
| 2004 | EXPECT_FALSE(m.Matches(a)); |
| 2005 | } |
| 2006 | |
| 2007 | // Tests that Field(&Foo::field, ...) works when field is not copyable. |
| 2008 | TEST(FieldTest, WorksForUncopyableField) { |
| 2009 | AStruct a; |
| 2010 | |
| 2011 | Matcher<AStruct> m = Field(&AStruct::z, Truly(ValueIsPositive)); |
| 2012 | EXPECT_TRUE(m.Matches(a)); |
| 2013 | m = Field(&AStruct::z, Not(Truly(ValueIsPositive))); |
| 2014 | EXPECT_FALSE(m.Matches(a)); |
| 2015 | } |
| 2016 | |
| 2017 | // Tests that Field(&Foo::field, ...) works when field is a pointer. |
| 2018 | TEST(FieldTest, WorksForPointerField) { |
| 2019 | // Matching against NULL. |
| 2020 | Matcher<AStruct> m = Field(&AStruct::p, static_cast<const char*>(NULL)); |
| 2021 | AStruct a; |
| 2022 | EXPECT_TRUE(m.Matches(a)); |
| 2023 | a.p = "hi"; |
| 2024 | EXPECT_FALSE(m.Matches(a)); |
| 2025 | |
| 2026 | // Matching a pointer that is not NULL. |
| 2027 | m = Field(&AStruct::p, StartsWith("hi")); |
| 2028 | a.p = "hill"; |
| 2029 | EXPECT_TRUE(m.Matches(a)); |
| 2030 | a.p = "hole"; |
| 2031 | EXPECT_FALSE(m.Matches(a)); |
| 2032 | } |
| 2033 | |
| 2034 | // Tests that Field() works when the object is passed by reference. |
| 2035 | TEST(FieldTest, WorksForByRefArgument) { |
| 2036 | Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0)); |
| 2037 | |
| 2038 | AStruct a; |
| 2039 | EXPECT_TRUE(m.Matches(a)); |
| 2040 | a.x = -1; |
| 2041 | EXPECT_FALSE(m.Matches(a)); |
| 2042 | } |
| 2043 | |
| 2044 | // Tests that Field(&Foo::field, ...) works when the argument's type |
| 2045 | // is a sub-type of Foo. |
| 2046 | TEST(FieldTest, WorksForArgumentOfSubType) { |
| 2047 | // Note that the matcher expects DerivedStruct but we say AStruct |
| 2048 | // inside Field(). |
| 2049 | Matcher<const DerivedStruct&> m = Field(&AStruct::x, Ge(0)); |
| 2050 | |
| 2051 | DerivedStruct d; |
| 2052 | EXPECT_TRUE(m.Matches(d)); |
| 2053 | d.x = -1; |
| 2054 | EXPECT_FALSE(m.Matches(d)); |
| 2055 | } |
| 2056 | |
| 2057 | // Tests that Field(&Foo::field, m) works when field's type and m's |
| 2058 | // argument type are compatible but not the same. |
| 2059 | TEST(FieldTest, WorksForCompatibleMatcherType) { |
| 2060 | // The field is an int, but the inner matcher expects a signed char. |
| 2061 | Matcher<const AStruct&> m = Field(&AStruct::x, |
| 2062 | Matcher<signed char>(Ge(0))); |
| 2063 | |
| 2064 | AStruct a; |
| 2065 | EXPECT_TRUE(m.Matches(a)); |
| 2066 | a.x = -1; |
| 2067 | EXPECT_FALSE(m.Matches(a)); |
| 2068 | } |
| 2069 | |
| 2070 | // Tests that Field() can describe itself. |
| 2071 | TEST(FieldTest, CanDescribeSelf) { |
| 2072 | Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0)); |
| 2073 | |
| 2074 | EXPECT_EQ("the given field is greater than or equal to 0", Describe(m)); |
| 2075 | EXPECT_EQ("the given field is not greater than or equal to 0", |
| 2076 | DescribeNegation(m)); |
| 2077 | } |
| 2078 | |
| 2079 | // Tests that Field() can explain the match result. |
| 2080 | TEST(FieldTest, CanExplainMatchResult) { |
| 2081 | Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0)); |
| 2082 | |
| 2083 | AStruct a; |
| 2084 | a.x = 1; |
| 2085 | EXPECT_EQ("", Explain(m, a)); |
| 2086 | |
| 2087 | m = Field(&AStruct::x, GreaterThan(0)); |
| 2088 | EXPECT_EQ("the given field is 1 more than 0", Explain(m, a)); |
| 2089 | } |
| 2090 | |
| 2091 | // Tests that Field() works when the argument is a pointer to const. |
| 2092 | TEST(FieldForPointerTest, WorksForPointerToConst) { |
| 2093 | Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0)); |
| 2094 | |
| 2095 | AStruct a; |
| 2096 | EXPECT_TRUE(m.Matches(&a)); |
| 2097 | a.x = -1; |
| 2098 | EXPECT_FALSE(m.Matches(&a)); |
| 2099 | } |
| 2100 | |
| 2101 | // Tests that Field() works when the argument is a pointer to non-const. |
| 2102 | TEST(FieldForPointerTest, WorksForPointerToNonConst) { |
| 2103 | Matcher<AStruct*> m = Field(&AStruct::x, Ge(0)); |
| 2104 | |
| 2105 | AStruct a; |
| 2106 | EXPECT_TRUE(m.Matches(&a)); |
| 2107 | a.x = -1; |
| 2108 | EXPECT_FALSE(m.Matches(&a)); |
| 2109 | } |
| 2110 | |
| 2111 | // Tests that Field() does not match the NULL pointer. |
| 2112 | TEST(FieldForPointerTest, DoesNotMatchNull) { |
| 2113 | Matcher<const AStruct*> m = Field(&AStruct::x, _); |
| 2114 | EXPECT_FALSE(m.Matches(NULL)); |
| 2115 | } |
| 2116 | |
| 2117 | // Tests that Field(&Foo::field, ...) works when the argument's type |
| 2118 | // is a sub-type of const Foo*. |
| 2119 | TEST(FieldForPointerTest, WorksForArgumentOfSubType) { |
| 2120 | // Note that the matcher expects DerivedStruct but we say AStruct |
| 2121 | // inside Field(). |
| 2122 | Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0)); |
| 2123 | |
| 2124 | DerivedStruct d; |
| 2125 | EXPECT_TRUE(m.Matches(&d)); |
| 2126 | d.x = -1; |
| 2127 | EXPECT_FALSE(m.Matches(&d)); |
| 2128 | } |
| 2129 | |
| 2130 | // Tests that Field() can describe itself when used to match a pointer. |
| 2131 | TEST(FieldForPointerTest, CanDescribeSelf) { |
| 2132 | Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0)); |
| 2133 | |
| 2134 | EXPECT_EQ("the given field is greater than or equal to 0", Describe(m)); |
| 2135 | EXPECT_EQ("the given field is not greater than or equal to 0", |
| 2136 | DescribeNegation(m)); |
| 2137 | } |
| 2138 | |
| 2139 | // Tests that Field() can explain the result of matching a pointer. |
| 2140 | TEST(FieldForPointerTest, CanExplainMatchResult) { |
| 2141 | Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0)); |
| 2142 | |
| 2143 | AStruct a; |
| 2144 | a.x = 1; |
| 2145 | EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(NULL))); |
| 2146 | EXPECT_EQ("", Explain(m, &a)); |
| 2147 | |
| 2148 | m = Field(&AStruct::x, GreaterThan(0)); |
| 2149 | EXPECT_EQ("the given field is 1 more than 0", Explain(m, &a)); |
| 2150 | } |
| 2151 | |
| 2152 | // A user-defined class for testing Property(). |
| 2153 | class AClass { |
| 2154 | public: |
| 2155 | AClass() : n_(0) {} |
| 2156 | |
| 2157 | // A getter that returns a non-reference. |
| 2158 | int n() const { return n_; } |
| 2159 | |
| 2160 | void set_n(int new_n) { n_ = new_n; } |
| 2161 | |
| 2162 | // A getter that returns a reference to const. |
| 2163 | const string& s() const { return s_; } |
| 2164 | |
| 2165 | void set_s(const string& new_s) { s_ = new_s; } |
| 2166 | |
| 2167 | // A getter that returns a reference to non-const. |
| 2168 | double& x() const { return x_; } |
| 2169 | private: |
| 2170 | int n_; |
| 2171 | string s_; |
| 2172 | |
| 2173 | static double x_; |
| 2174 | }; |
| 2175 | |
| 2176 | double AClass::x_ = 0.0; |
| 2177 | |
| 2178 | // A derived class for testing Property(). |
| 2179 | class DerivedClass : public AClass { |
| 2180 | private: |
| 2181 | int k_; |
| 2182 | }; |
| 2183 | |
| 2184 | // Tests that Property(&Foo::property, ...) works when property() |
| 2185 | // returns a non-reference. |
| 2186 | TEST(PropertyTest, WorksForNonReferenceProperty) { |
| 2187 | Matcher<const AClass&> m = Property(&AClass::n, Ge(0)); |
| 2188 | |
| 2189 | AClass a; |
| 2190 | a.set_n(1); |
| 2191 | EXPECT_TRUE(m.Matches(a)); |
| 2192 | |
| 2193 | a.set_n(-1); |
| 2194 | EXPECT_FALSE(m.Matches(a)); |
| 2195 | } |
| 2196 | |
| 2197 | // Tests that Property(&Foo::property, ...) works when property() |
| 2198 | // returns a reference to const. |
| 2199 | TEST(PropertyTest, WorksForReferenceToConstProperty) { |
| 2200 | Matcher<const AClass&> m = Property(&AClass::s, StartsWith("hi")); |
| 2201 | |
| 2202 | AClass a; |
| 2203 | a.set_s("hill"); |
| 2204 | EXPECT_TRUE(m.Matches(a)); |
| 2205 | |
| 2206 | a.set_s("hole"); |
| 2207 | EXPECT_FALSE(m.Matches(a)); |
| 2208 | } |
| 2209 | |
| 2210 | // Tests that Property(&Foo::property, ...) works when property() |
| 2211 | // returns a reference to non-const. |
| 2212 | TEST(PropertyTest, WorksForReferenceToNonConstProperty) { |
| 2213 | double x = 0.0; |
| 2214 | AClass a; |
| 2215 | |
| 2216 | Matcher<const AClass&> m = Property(&AClass::x, Ref(x)); |
| 2217 | EXPECT_FALSE(m.Matches(a)); |
| 2218 | |
| 2219 | m = Property(&AClass::x, Not(Ref(x))); |
| 2220 | EXPECT_TRUE(m.Matches(a)); |
| 2221 | } |
| 2222 | |
| 2223 | // Tests that Property(&Foo::property, ...) works when the argument is |
| 2224 | // passed by value. |
| 2225 | TEST(PropertyTest, WorksForByValueArgument) { |
| 2226 | Matcher<AClass> m = Property(&AClass::s, StartsWith("hi")); |
| 2227 | |
| 2228 | AClass a; |
| 2229 | a.set_s("hill"); |
| 2230 | EXPECT_TRUE(m.Matches(a)); |
| 2231 | |
| 2232 | a.set_s("hole"); |
| 2233 | EXPECT_FALSE(m.Matches(a)); |
| 2234 | } |
| 2235 | |
| 2236 | // Tests that Property(&Foo::property, ...) works when the argument's |
| 2237 | // type is a sub-type of Foo. |
| 2238 | TEST(PropertyTest, WorksForArgumentOfSubType) { |
| 2239 | // The matcher expects a DerivedClass, but inside the Property() we |
| 2240 | // say AClass. |
| 2241 | Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0)); |
| 2242 | |
| 2243 | DerivedClass d; |
| 2244 | d.set_n(1); |
| 2245 | EXPECT_TRUE(m.Matches(d)); |
| 2246 | |
| 2247 | d.set_n(-1); |
| 2248 | EXPECT_FALSE(m.Matches(d)); |
| 2249 | } |
| 2250 | |
| 2251 | // Tests that Property(&Foo::property, m) works when property()'s type |
| 2252 | // and m's argument type are compatible but different. |
| 2253 | TEST(PropertyTest, WorksForCompatibleMatcherType) { |
| 2254 | // n() returns an int but the inner matcher expects a signed char. |
| 2255 | Matcher<const AClass&> m = Property(&AClass::n, |
| 2256 | Matcher<signed char>(Ge(0))); |
| 2257 | |
| 2258 | AClass a; |
| 2259 | EXPECT_TRUE(m.Matches(a)); |
| 2260 | a.set_n(-1); |
| 2261 | EXPECT_FALSE(m.Matches(a)); |
| 2262 | } |
| 2263 | |
| 2264 | // Tests that Property() can describe itself. |
| 2265 | TEST(PropertyTest, CanDescribeSelf) { |
| 2266 | Matcher<const AClass&> m = Property(&AClass::n, Ge(0)); |
| 2267 | |
| 2268 | EXPECT_EQ("the given property is greater than or equal to 0", Describe(m)); |
| 2269 | EXPECT_EQ("the given property is not greater than or equal to 0", |
| 2270 | DescribeNegation(m)); |
| 2271 | } |
| 2272 | |
| 2273 | // Tests that Property() can explain the match result. |
| 2274 | TEST(PropertyTest, CanExplainMatchResult) { |
| 2275 | Matcher<const AClass&> m = Property(&AClass::n, Ge(0)); |
| 2276 | |
| 2277 | AClass a; |
| 2278 | a.set_n(1); |
| 2279 | EXPECT_EQ("", Explain(m, a)); |
| 2280 | |
| 2281 | m = Property(&AClass::n, GreaterThan(0)); |
| 2282 | EXPECT_EQ("the given property is 1 more than 0", Explain(m, a)); |
| 2283 | } |
| 2284 | |
| 2285 | // Tests that Property() works when the argument is a pointer to const. |
| 2286 | TEST(PropertyForPointerTest, WorksForPointerToConst) { |
| 2287 | Matcher<const AClass*> m = Property(&AClass::n, Ge(0)); |
| 2288 | |
| 2289 | AClass a; |
| 2290 | a.set_n(1); |
| 2291 | EXPECT_TRUE(m.Matches(&a)); |
| 2292 | |
| 2293 | a.set_n(-1); |
| 2294 | EXPECT_FALSE(m.Matches(&a)); |
| 2295 | } |
| 2296 | |
| 2297 | // Tests that Property() works when the argument is a pointer to non-const. |
| 2298 | TEST(PropertyForPointerTest, WorksForPointerToNonConst) { |
| 2299 | Matcher<AClass*> m = Property(&AClass::s, StartsWith("hi")); |
| 2300 | |
| 2301 | AClass a; |
| 2302 | a.set_s("hill"); |
| 2303 | EXPECT_TRUE(m.Matches(&a)); |
| 2304 | |
| 2305 | a.set_s("hole"); |
| 2306 | EXPECT_FALSE(m.Matches(&a)); |
| 2307 | } |
| 2308 | |
| 2309 | // Tests that Property() does not match the NULL pointer. |
| 2310 | TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) { |
| 2311 | Matcher<const AClass*> m = Property(&AClass::x, _); |
| 2312 | EXPECT_FALSE(m.Matches(NULL)); |
| 2313 | } |
| 2314 | |
| 2315 | // Tests that Property(&Foo::property, ...) works when the argument's |
| 2316 | // type is a sub-type of const Foo*. |
| 2317 | TEST(PropertyForPointerTest, WorksForArgumentOfSubType) { |
| 2318 | // The matcher expects a DerivedClass, but inside the Property() we |
| 2319 | // say AClass. |
| 2320 | Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0)); |
| 2321 | |
| 2322 | DerivedClass d; |
| 2323 | d.set_n(1); |
| 2324 | EXPECT_TRUE(m.Matches(&d)); |
| 2325 | |
| 2326 | d.set_n(-1); |
| 2327 | EXPECT_FALSE(m.Matches(&d)); |
| 2328 | } |
| 2329 | |
| 2330 | // Tests that Property() can describe itself when used to match a pointer. |
| 2331 | TEST(PropertyForPointerTest, CanDescribeSelf) { |
| 2332 | Matcher<const AClass*> m = Property(&AClass::n, Ge(0)); |
| 2333 | |
| 2334 | EXPECT_EQ("the given property is greater than or equal to 0", Describe(m)); |
| 2335 | EXPECT_EQ("the given property is not greater than or equal to 0", |
| 2336 | DescribeNegation(m)); |
| 2337 | } |
| 2338 | |
| 2339 | // Tests that Property() can explain the result of matching a pointer. |
| 2340 | TEST(PropertyForPointerTest, CanExplainMatchResult) { |
| 2341 | Matcher<const AClass*> m = Property(&AClass::n, Ge(0)); |
| 2342 | |
| 2343 | AClass a; |
| 2344 | a.set_n(1); |
| 2345 | EXPECT_EQ("", Explain(m, static_cast<const AClass*>(NULL))); |
| 2346 | EXPECT_EQ("", Explain(m, &a)); |
| 2347 | |
| 2348 | m = Property(&AClass::n, GreaterThan(0)); |
| 2349 | EXPECT_EQ("the given property is 1 more than 0", Explain(m, &a)); |
| 2350 | } |
| 2351 | |
| 2352 | // Tests ResultOf. |
| 2353 | |
| 2354 | // Tests that ResultOf(f, ...) compiles and works as expected when f is a |
| 2355 | // function pointer. |
| 2356 | string IntToStringFunction(int input) { return input == 1 ? "foo" : "bar"; } |
| 2357 | |
| 2358 | TEST(ResultOfTest, WorksForFunctionPointers) { |
| 2359 | Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(string("foo"))); |
| 2360 | |
| 2361 | EXPECT_TRUE(matcher.Matches(1)); |
| 2362 | EXPECT_FALSE(matcher.Matches(2)); |
| 2363 | } |
| 2364 | |
| 2365 | // Tests that ResultOf() can describe itself. |
| 2366 | TEST(ResultOfTest, CanDescribeItself) { |
| 2367 | Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo")); |
| 2368 | |
| 2369 | EXPECT_EQ("result of the given callable is equal to \"foo\"", |
| 2370 | Describe(matcher)); |
| 2371 | EXPECT_EQ("result of the given callable is not equal to \"foo\"", |
| 2372 | DescribeNegation(matcher)); |
| 2373 | } |
| 2374 | |
| 2375 | // Tests that ResultOf() can explain the match result. |
| 2376 | int IntFunction(int input) { return input == 42 ? 80 : 90; } |
| 2377 | |
| 2378 | TEST(ResultOfTest, CanExplainMatchResult) { |
| 2379 | Matcher<int> matcher = ResultOf(&IntFunction, Ge(85)); |
| 2380 | EXPECT_EQ("", Explain(matcher, 36)); |
| 2381 | |
| 2382 | matcher = ResultOf(&IntFunction, GreaterThan(85)); |
| 2383 | EXPECT_EQ("result of the given callable is 5 more than 85", |
| 2384 | Explain(matcher, 36)); |
| 2385 | } |
| 2386 | |
| 2387 | // Tests that ResultOf(f, ...) compiles and works as expected when f(x) |
| 2388 | // returns a non-reference. |
| 2389 | TEST(ResultOfTest, WorksForNonReferenceResults) { |
| 2390 | Matcher<int> matcher = ResultOf(&IntFunction, Eq(80)); |
| 2391 | |
| 2392 | EXPECT_TRUE(matcher.Matches(42)); |
| 2393 | EXPECT_FALSE(matcher.Matches(36)); |
| 2394 | } |
| 2395 | |
| 2396 | // Tests that ResultOf(f, ...) compiles and works as expected when f(x) |
| 2397 | // returns a reference to non-const. |
| 2398 | double& DoubleFunction(double& input) { return input; } |
| 2399 | |
| 2400 | Uncopyable& RefUncopyableFunction(Uncopyable& obj) { |
| 2401 | return obj; |
| 2402 | } |
| 2403 | |
| 2404 | TEST(ResultOfTest, WorksForReferenceToNonConstResults) { |
| 2405 | double x = 3.14; |
| 2406 | double x2 = x; |
| 2407 | Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(x)); |
| 2408 | |
| 2409 | EXPECT_TRUE(matcher.Matches(x)); |
| 2410 | EXPECT_FALSE(matcher.Matches(x2)); |
| 2411 | |
| 2412 | // Test that ResultOf works with uncopyable objects |
| 2413 | Uncopyable obj(0); |
| 2414 | Uncopyable obj2(0); |
| 2415 | Matcher<Uncopyable&> matcher2 = |
| 2416 | ResultOf(&RefUncopyableFunction, Ref(obj)); |
| 2417 | |
| 2418 | EXPECT_TRUE(matcher2.Matches(obj)); |
| 2419 | EXPECT_FALSE(matcher2.Matches(obj2)); |
| 2420 | } |
| 2421 | |
| 2422 | // Tests that ResultOf(f, ...) compiles and works as expected when f(x) |
| 2423 | // returns a reference to const. |
| 2424 | const string& StringFunction(const string& input) { return input; } |
| 2425 | |
| 2426 | TEST(ResultOfTest, WorksForReferenceToConstResults) { |
| 2427 | string s = "foo"; |
| 2428 | string s2 = s; |
| 2429 | Matcher<const string&> matcher = ResultOf(&StringFunction, Ref(s)); |
| 2430 | |
| 2431 | EXPECT_TRUE(matcher.Matches(s)); |
| 2432 | EXPECT_FALSE(matcher.Matches(s2)); |
| 2433 | } |
| 2434 | |
| 2435 | // Tests that ResultOf(f, m) works when f(x) and m's |
| 2436 | // argument types are compatible but different. |
| 2437 | TEST(ResultOfTest, WorksForCompatibleMatcherTypes) { |
| 2438 | // IntFunction() returns int but the inner matcher expects a signed char. |
| 2439 | Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85))); |
| 2440 | |
| 2441 | EXPECT_TRUE(matcher.Matches(36)); |
| 2442 | EXPECT_FALSE(matcher.Matches(42)); |
| 2443 | } |
| 2444 | |
| 2445 | #ifdef GTEST_HAS_DEATH_TEST |
| 2446 | // Tests that the program aborts when ResultOf is passed |
| 2447 | // a NULL function pointer. |
| 2448 | TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) { |
| 2449 | EXPECT_DEATH( |
| 2450 | ResultOf(static_cast<string(*)(int)>(NULL), Eq(string("foo"))), |
| 2451 | "NULL function pointer is passed into ResultOf\\(\\)\\."); |
| 2452 | } |
| 2453 | #endif // GTEST_HAS_DEATH_TEST |
| 2454 | |
| 2455 | // Tests that ResultOf(f, ...) compiles and works as expected when f is a |
| 2456 | // function reference. |
| 2457 | TEST(ResultOfTest, WorksForFunctionReferences) { |
| 2458 | Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq("foo")); |
| 2459 | EXPECT_TRUE(matcher.Matches(1)); |
| 2460 | EXPECT_FALSE(matcher.Matches(2)); |
| 2461 | } |
| 2462 | |
| 2463 | // Tests that ResultOf(f, ...) compiles and works as expected when f is a |
| 2464 | // function object. |
| 2465 | struct Functor : public ::std::unary_function<int, string> { |
| 2466 | result_type operator()(argument_type input) const { |
| 2467 | return IntToStringFunction(input); |
| 2468 | } |
| 2469 | }; |
| 2470 | |
| 2471 | TEST(ResultOfTest, WorksForFunctors) { |
| 2472 | Matcher<int> matcher = ResultOf(Functor(), Eq(string("foo"))); |
| 2473 | |
| 2474 | EXPECT_TRUE(matcher.Matches(1)); |
| 2475 | EXPECT_FALSE(matcher.Matches(2)); |
| 2476 | } |
| 2477 | |
| 2478 | // Tests that ResultOf(f, ...) compiles and works as expected when f is a |
| 2479 | // functor with more then one operator() defined. ResultOf() must work |
| 2480 | // for each defined operator(). |
| 2481 | struct PolymorphicFunctor { |
| 2482 | typedef int result_type; |
| 2483 | int operator()(int n) { return n; } |
| 2484 | int operator()(const char* s) { return static_cast<int>(strlen(s)); } |
| 2485 | }; |
| 2486 | |
| 2487 | TEST(ResultOfTest, WorksForPolymorphicFunctors) { |
| 2488 | Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5)); |
| 2489 | |
| 2490 | EXPECT_TRUE(matcher_int.Matches(10)); |
| 2491 | EXPECT_FALSE(matcher_int.Matches(2)); |
| 2492 | |
| 2493 | Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5)); |
| 2494 | |
| 2495 | EXPECT_TRUE(matcher_string.Matches("long string")); |
| 2496 | EXPECT_FALSE(matcher_string.Matches("shrt")); |
| 2497 | } |
| 2498 | |
| 2499 | const int* ReferencingFunction(const int& n) { return &n; } |
| 2500 | |
| 2501 | struct ReferencingFunctor { |
| 2502 | typedef const int* result_type; |
| 2503 | result_type operator()(const int& n) { return &n; } |
| 2504 | }; |
| 2505 | |
| 2506 | TEST(ResultOfTest, WorksForReferencingCallables) { |
| 2507 | const int n = 1; |
| 2508 | const int n2 = 1; |
| 2509 | Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n)); |
| 2510 | EXPECT_TRUE(matcher2.Matches(n)); |
| 2511 | EXPECT_FALSE(matcher2.Matches(n2)); |
| 2512 | |
| 2513 | Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n)); |
| 2514 | EXPECT_TRUE(matcher3.Matches(n)); |
| 2515 | EXPECT_FALSE(matcher3.Matches(n2)); |
| 2516 | } |
| 2517 | |
| 2518 | |
| 2519 | class DivisibleByImpl { |
| 2520 | public: |
| 2521 | explicit DivisibleByImpl(int divider) : divider_(divider) {} |
| 2522 | |
| 2523 | template <typename T> |
| 2524 | bool Matches(const T& n) const { |
| 2525 | return (n % divider_) == 0; |
| 2526 | } |
| 2527 | |
| 2528 | void DescribeTo(::std::ostream* os) const { |
| 2529 | *os << "is divisible by " << divider_; |
| 2530 | } |
| 2531 | |
| 2532 | void DescribeNegationTo(::std::ostream* os) const { |
| 2533 | *os << "is not divisible by " << divider_; |
| 2534 | } |
| 2535 | |
| 2536 | int divider() const { return divider_; } |
| 2537 | private: |
| 2538 | const int divider_; |
| 2539 | }; |
| 2540 | |
| 2541 | // For testing using ExplainMatchResultTo() with polymorphic matchers. |
| 2542 | template <typename T> |
| 2543 | void ExplainMatchResultTo(const DivisibleByImpl& impl, const T& n, |
| 2544 | ::std::ostream* os) { |
| 2545 | *os << "is " << (n % impl.divider()) << " modulo " |
| 2546 | << impl.divider(); |
| 2547 | } |
| 2548 | |
| 2549 | PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) { |
| 2550 | return MakePolymorphicMatcher(DivisibleByImpl(n)); |
| 2551 | } |
| 2552 | |
| 2553 | // Tests that when AllOf() fails, only the first failing matcher is |
| 2554 | // asked to explain why. |
| 2555 | TEST(ExplainMatchResultTest, AllOf_False_False) { |
| 2556 | const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3)); |
| 2557 | EXPECT_EQ("is 1 modulo 4", Explain(m, 5)); |
| 2558 | } |
| 2559 | |
| 2560 | // Tests that when AllOf() fails, only the first failing matcher is |
| 2561 | // asked to explain why. |
| 2562 | TEST(ExplainMatchResultTest, AllOf_False_True) { |
| 2563 | const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3)); |
| 2564 | EXPECT_EQ("is 2 modulo 4", Explain(m, 6)); |
| 2565 | } |
| 2566 | |
| 2567 | // Tests that when AllOf() fails, only the first failing matcher is |
| 2568 | // asked to explain why. |
| 2569 | TEST(ExplainMatchResultTest, AllOf_True_False) { |
| 2570 | const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3)); |
| 2571 | EXPECT_EQ("is 2 modulo 3", Explain(m, 5)); |
| 2572 | } |
| 2573 | |
| 2574 | // Tests that when AllOf() succeeds, all matchers are asked to explain |
| 2575 | // why. |
| 2576 | TEST(ExplainMatchResultTest, AllOf_True_True) { |
| 2577 | const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3)); |
| 2578 | EXPECT_EQ("is 0 modulo 2; is 0 modulo 3", Explain(m, 6)); |
| 2579 | } |
| 2580 | |
| 2581 | TEST(ExplainMatchResultTest, AllOf_True_True_2) { |
| 2582 | const Matcher<int> m = AllOf(Ge(2), Le(3)); |
| 2583 | EXPECT_EQ("", Explain(m, 2)); |
| 2584 | } |
| 2585 | |
| 2586 | TEST(ExplainmatcherResultTest, MonomorphicMatcher) { |
| 2587 | const Matcher<int> m = GreaterThan(5); |
| 2588 | EXPECT_EQ("is 1 more than 5", Explain(m, 6)); |
| 2589 | } |
| 2590 | |
| 2591 | // The following two tests verify that values without a public copy |
| 2592 | // ctor can be used as arguments to matchers like Eq(), Ge(), and etc |
| 2593 | // with the help of ByRef(). |
| 2594 | |
| 2595 | class NotCopyable { |
| 2596 | public: |
| 2597 | explicit NotCopyable(int value) : value_(value) {} |
| 2598 | |
| 2599 | int value() const { return value_; } |
| 2600 | |
| 2601 | bool operator==(const NotCopyable& rhs) const { |
| 2602 | return value() == rhs.value(); |
| 2603 | } |
| 2604 | |
| 2605 | bool operator>=(const NotCopyable& rhs) const { |
| 2606 | return value() >= rhs.value(); |
| 2607 | } |
| 2608 | private: |
| 2609 | int value_; |
| 2610 | |
| 2611 | GTEST_DISALLOW_COPY_AND_ASSIGN_(NotCopyable); |
| 2612 | }; |
| 2613 | |
| 2614 | TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) { |
| 2615 | const NotCopyable const_value1(1); |
| 2616 | const Matcher<const NotCopyable&> m = Eq(ByRef(const_value1)); |
| 2617 | |
| 2618 | const NotCopyable n1(1), n2(2); |
| 2619 | EXPECT_TRUE(m.Matches(n1)); |
| 2620 | EXPECT_FALSE(m.Matches(n2)); |
| 2621 | } |
| 2622 | |
| 2623 | TEST(ByRefTest, AllowsNotCopyableValueInMatchers) { |
| 2624 | NotCopyable value2(2); |
| 2625 | const Matcher<NotCopyable&> m = Ge(ByRef(value2)); |
| 2626 | |
| 2627 | NotCopyable n1(1), n2(2); |
| 2628 | EXPECT_FALSE(m.Matches(n1)); |
| 2629 | EXPECT_TRUE(m.Matches(n2)); |
| 2630 | } |
| 2631 | |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame^] | 2632 | // Tests ContainerEq with different container types, and |
| 2633 | // different element types. |
| 2634 | |
| 2635 | template <typename T> |
| 2636 | class ContainerEqTest : public testing::Test { |
| 2637 | public: |
| 2638 | }; |
| 2639 | |
| 2640 | typedef testing::Types< |
| 2641 | std::set<int>, |
| 2642 | std::vector<size_t>, |
| 2643 | std::multiset<size_t>, |
| 2644 | std::list<int> > |
| 2645 | ContainerEqTestTypes; |
| 2646 | |
| 2647 | TYPED_TEST_CASE(ContainerEqTest, ContainerEqTestTypes); |
| 2648 | |
| 2649 | // Tests that the filled container is equal to itself. |
| 2650 | TYPED_TEST(ContainerEqTest, EqualsSelf) { |
| 2651 | static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 2652 | TypeParam my_set(vals, vals + 6); |
| 2653 | const Matcher<TypeParam> m = ContainerEq(my_set); |
| 2654 | EXPECT_TRUE(m.Matches(my_set)); |
| 2655 | EXPECT_EQ("", Explain(m, my_set)); |
| 2656 | } |
| 2657 | |
| 2658 | // Tests that missing values are reported. |
| 2659 | TYPED_TEST(ContainerEqTest, ValueMissing) { |
| 2660 | static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 2661 | static const int test_vals[] = {2, 1, 8, 5}; |
| 2662 | TypeParam my_set(vals, vals + 6); |
| 2663 | TypeParam test_set(test_vals, test_vals + 4); |
| 2664 | const Matcher<TypeParam> m = ContainerEq(my_set); |
| 2665 | EXPECT_FALSE(m.Matches(test_set)); |
| 2666 | EXPECT_EQ("Not in actual: 3", Explain(m, test_set)); |
| 2667 | } |
| 2668 | |
| 2669 | // Tests that added values are reported. |
| 2670 | TYPED_TEST(ContainerEqTest, ValueAdded) { |
| 2671 | static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 2672 | static const int test_vals[] = {1, 2, 3, 5, 8, 46}; |
| 2673 | TypeParam my_set(vals, vals + 6); |
| 2674 | TypeParam test_set(test_vals, test_vals + 6); |
| 2675 | const Matcher<const TypeParam&> m = ContainerEq(my_set); |
| 2676 | EXPECT_FALSE(m.Matches(test_set)); |
| 2677 | EXPECT_EQ("Only in actual: 46", Explain(m, test_set)); |
| 2678 | } |
| 2679 | |
| 2680 | // Tests that added and missing values are reported together. |
| 2681 | TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) { |
| 2682 | static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 2683 | static const int test_vals[] = {1, 2, 3, 8, 46}; |
| 2684 | TypeParam my_set(vals, vals + 6); |
| 2685 | TypeParam test_set(test_vals, test_vals + 5); |
| 2686 | const Matcher<TypeParam> m = ContainerEq(my_set); |
| 2687 | EXPECT_FALSE(m.Matches(test_set)); |
| 2688 | EXPECT_EQ("Only in actual: 46; not in actual: 5", Explain(m, test_set)); |
| 2689 | } |
| 2690 | |
| 2691 | // Tests duplicated value -- expect no explanation. |
| 2692 | TYPED_TEST(ContainerEqTest, DuplicateDifference) { |
| 2693 | static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 2694 | static const int test_vals[] = {1, 2, 3, 5, 8}; |
| 2695 | TypeParam my_set(vals, vals + 6); |
| 2696 | TypeParam test_set(test_vals, test_vals + 5); |
| 2697 | const Matcher<const TypeParam&> m = ContainerEq(my_set); |
| 2698 | // Depending on the container, match may be true or false |
| 2699 | // But in any case there should be no explanation. |
| 2700 | EXPECT_EQ("", Explain(m, test_set)); |
| 2701 | } |
| 2702 | |
| 2703 | // Tests that mutliple missing values are reported. |
| 2704 | // Using just vector here, so order is predicatble. |
| 2705 | TEST(ContainerEqExtraTest, MultipleValuesMissing) { |
| 2706 | static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 2707 | static const int test_vals[] = {2, 1, 5}; |
| 2708 | std::vector<int> my_set(vals, vals + 6); |
| 2709 | std::vector<int> test_set(test_vals, test_vals + 3); |
| 2710 | const Matcher<std::vector<int> > m = ContainerEq(my_set); |
| 2711 | EXPECT_FALSE(m.Matches(test_set)); |
| 2712 | EXPECT_EQ("Not in actual: 3, 8", Explain(m, test_set)); |
| 2713 | } |
| 2714 | |
| 2715 | // Tests that added values are reported. |
| 2716 | // Using just vector here, so order is predicatble. |
| 2717 | TEST(ContainerEqExtraTest, MultipleValuesAdded) { |
| 2718 | static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 2719 | static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46}; |
| 2720 | std::list<size_t> my_set(vals, vals + 6); |
| 2721 | std::list<size_t> test_set(test_vals, test_vals + 7); |
| 2722 | const Matcher<const std::list<size_t>&> m = ContainerEq(my_set); |
| 2723 | EXPECT_FALSE(m.Matches(test_set)); |
| 2724 | EXPECT_EQ("Only in actual: 92, 46", Explain(m, test_set)); |
| 2725 | } |
| 2726 | |
| 2727 | // Tests that added and missing values are reported together. |
| 2728 | TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) { |
| 2729 | static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 2730 | static const int test_vals[] = {1, 2, 3, 92, 46}; |
| 2731 | std::list<size_t> my_set(vals, vals + 6); |
| 2732 | std::list<size_t> test_set(test_vals, test_vals + 5); |
| 2733 | const Matcher<const std::list<size_t> > m = ContainerEq(my_set); |
| 2734 | EXPECT_FALSE(m.Matches(test_set)); |
| 2735 | EXPECT_EQ("Only in actual: 92, 46; not in actual: 5, 8", |
| 2736 | Explain(m, test_set)); |
| 2737 | } |
| 2738 | |
| 2739 | // Tests to see that duplicate elements are detected, |
| 2740 | // but (as above) not reported in the explanation. |
| 2741 | TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) { |
| 2742 | static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 2743 | static const int test_vals[] = {1, 2, 3, 5, 8}; |
| 2744 | std::vector<int> my_set(vals, vals + 6); |
| 2745 | std::vector<int> test_set(test_vals, test_vals + 5); |
| 2746 | const Matcher<std::vector<int> > m = ContainerEq(my_set); |
| 2747 | EXPECT_TRUE(m.Matches(my_set)); |
| 2748 | EXPECT_FALSE(m.Matches(test_set)); |
| 2749 | // There is nothing to report when both sets contain all the same values. |
| 2750 | EXPECT_EQ("", Explain(m, test_set)); |
| 2751 | } |
| 2752 | |
| 2753 | // Tests that ContainerEq works for non-trivial associative containers, |
| 2754 | // like maps. |
| 2755 | TEST(ContainerEqExtraTest, WorksForMaps) { |
| 2756 | std::map<int, std::string> my_map; |
| 2757 | my_map[0] = "a"; |
| 2758 | my_map[1] = "b"; |
| 2759 | |
| 2760 | std::map<int, std::string> test_map; |
| 2761 | test_map[0] = "aa"; |
| 2762 | test_map[1] = "b"; |
| 2763 | |
| 2764 | const Matcher<const std::map<int, std::string>&> m = ContainerEq(my_map); |
| 2765 | EXPECT_TRUE(m.Matches(my_map)); |
| 2766 | EXPECT_FALSE(m.Matches(test_map)); |
| 2767 | |
| 2768 | EXPECT_EQ("Only in actual: (0, \"aa\"); not in actual: (0, \"a\")", |
| 2769 | Explain(m, test_map)); |
| 2770 | } |
| 2771 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2772 | } // namespace gmock_matchers_test |
| 2773 | } // namespace testing |