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