David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [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" |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 37 | #include "gmock/gmock-more-matchers.h" |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 38 | |
| 39 | #include <string.h> |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 40 | #include <time.h> |
| 41 | #include <deque> |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 42 | #include <functional> |
| 43 | #include <iostream> |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 44 | #include <iterator> |
| 45 | #include <limits> |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 46 | #include <list> |
| 47 | #include <map> |
| 48 | #include <set> |
| 49 | #include <sstream> |
| 50 | #include <string> |
| 51 | #include <utility> |
| 52 | #include <vector> |
| 53 | #include "gmock/gmock.h" |
| 54 | #include "gtest/gtest.h" |
| 55 | #include "gtest/gtest-spi.h" |
| 56 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 57 | #if GTEST_HAS_STD_FORWARD_LIST_ |
| 58 | # include <forward_list> // NOLINT |
| 59 | #endif |
| 60 | |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 61 | namespace testing { |
| 62 | |
| 63 | namespace internal { |
| 64 | GTEST_API_ string JoinAsTuple(const Strings& fields); |
| 65 | } // namespace internal |
| 66 | |
| 67 | namespace gmock_matchers_test { |
| 68 | |
| 69 | using std::greater; |
| 70 | using std::less; |
| 71 | using std::list; |
| 72 | using std::make_pair; |
| 73 | using std::map; |
| 74 | using std::multimap; |
| 75 | using std::multiset; |
| 76 | using std::ostream; |
| 77 | using std::pair; |
| 78 | using std::set; |
| 79 | using std::stringstream; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 80 | using std::vector; |
| 81 | using testing::A; |
| 82 | using testing::AllArgs; |
| 83 | using testing::AllOf; |
| 84 | using testing::An; |
| 85 | using testing::AnyOf; |
| 86 | using testing::ByRef; |
| 87 | using testing::ContainsRegex; |
| 88 | using testing::DoubleEq; |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 89 | using testing::DoubleNear; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 90 | using testing::EndsWith; |
| 91 | using testing::Eq; |
| 92 | using testing::ExplainMatchResult; |
| 93 | using testing::Field; |
| 94 | using testing::FloatEq; |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 95 | using testing::FloatNear; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 96 | using testing::Ge; |
| 97 | using testing::Gt; |
| 98 | using testing::HasSubstr; |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 99 | using testing::IsEmpty; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 100 | using testing::IsNull; |
| 101 | using testing::Key; |
| 102 | using testing::Le; |
| 103 | using testing::Lt; |
| 104 | using testing::MakeMatcher; |
| 105 | using testing::MakePolymorphicMatcher; |
| 106 | using testing::MatchResultListener; |
| 107 | using testing::Matcher; |
| 108 | using testing::MatcherCast; |
| 109 | using testing::MatcherInterface; |
| 110 | using testing::Matches; |
| 111 | using testing::MatchesRegex; |
| 112 | using testing::NanSensitiveDoubleEq; |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 113 | using testing::NanSensitiveDoubleNear; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 114 | using testing::NanSensitiveFloatEq; |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 115 | using testing::NanSensitiveFloatNear; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 116 | using testing::Ne; |
| 117 | using testing::Not; |
| 118 | using testing::NotNull; |
| 119 | using testing::Pair; |
| 120 | using testing::Pointee; |
| 121 | using testing::Pointwise; |
| 122 | using testing::PolymorphicMatcher; |
| 123 | using testing::Property; |
| 124 | using testing::Ref; |
| 125 | using testing::ResultOf; |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 126 | using testing::SizeIs; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 127 | using testing::StartsWith; |
| 128 | using testing::StrCaseEq; |
| 129 | using testing::StrCaseNe; |
| 130 | using testing::StrEq; |
| 131 | using testing::StrNe; |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 132 | using testing::StringMatchResultListener; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 133 | using testing::Truly; |
| 134 | using testing::TypedEq; |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 135 | using testing::UnorderedPointwise; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 136 | using testing::Value; |
| 137 | using testing::WhenSorted; |
| 138 | using testing::WhenSortedBy; |
| 139 | using testing::_; |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 140 | using testing::get; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 141 | using testing::internal::DummyMatchResultListener; |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 142 | using testing::internal::ElementMatcherPair; |
| 143 | using testing::internal::ElementMatcherPairs; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 144 | using testing::internal::ExplainMatchFailureTupleTo; |
| 145 | using testing::internal::FloatingEqMatcher; |
| 146 | using testing::internal::FormatMatcherDescription; |
| 147 | using testing::internal::IsReadableTypeName; |
| 148 | using testing::internal::JoinAsTuple; |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 149 | using testing::internal::linked_ptr; |
| 150 | using testing::internal::MatchMatrix; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 151 | using testing::internal::RE; |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 152 | using testing::internal::scoped_ptr; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 153 | using testing::internal::StreamMatchResultListener; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 154 | using testing::internal::Strings; |
| 155 | using testing::internal::linked_ptr; |
| 156 | using testing::internal::scoped_ptr; |
| 157 | using testing::internal::string; |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 158 | using testing::make_tuple; |
| 159 | using testing::tuple; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 160 | |
| 161 | // For testing ExplainMatchResultTo(). |
| 162 | class GreaterThanMatcher : public MatcherInterface<int> { |
| 163 | public: |
| 164 | explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {} |
| 165 | |
| 166 | virtual void DescribeTo(ostream* os) const { |
| 167 | *os << "is > " << rhs_; |
| 168 | } |
| 169 | |
| 170 | virtual bool MatchAndExplain(int lhs, |
| 171 | MatchResultListener* listener) const { |
| 172 | const int diff = lhs - rhs_; |
| 173 | if (diff > 0) { |
| 174 | *listener << "which is " << diff << " more than " << rhs_; |
| 175 | } else if (diff == 0) { |
| 176 | *listener << "which is the same as " << rhs_; |
| 177 | } else { |
| 178 | *listener << "which is " << -diff << " less than " << rhs_; |
| 179 | } |
| 180 | |
| 181 | return lhs > rhs_; |
| 182 | } |
| 183 | |
| 184 | private: |
| 185 | int rhs_; |
| 186 | }; |
| 187 | |
| 188 | Matcher<int> GreaterThan(int n) { |
| 189 | return MakeMatcher(new GreaterThanMatcher(n)); |
| 190 | } |
| 191 | |
| 192 | string OfType(const string& type_name) { |
| 193 | #if GTEST_HAS_RTTI |
| 194 | return " (of type " + type_name + ")"; |
| 195 | #else |
| 196 | return ""; |
| 197 | #endif |
| 198 | } |
| 199 | |
| 200 | // Returns the description of the given matcher. |
| 201 | template <typename T> |
| 202 | string Describe(const Matcher<T>& m) { |
| 203 | stringstream ss; |
| 204 | m.DescribeTo(&ss); |
| 205 | return ss.str(); |
| 206 | } |
| 207 | |
| 208 | // Returns the description of the negation of the given matcher. |
| 209 | template <typename T> |
| 210 | string DescribeNegation(const Matcher<T>& m) { |
| 211 | stringstream ss; |
| 212 | m.DescribeNegationTo(&ss); |
| 213 | return ss.str(); |
| 214 | } |
| 215 | |
| 216 | // Returns the reason why x matches, or doesn't match, m. |
| 217 | template <typename MatcherType, typename Value> |
| 218 | string Explain(const MatcherType& m, const Value& x) { |
| 219 | StringMatchResultListener listener; |
| 220 | ExplainMatchResult(m, x, &listener); |
| 221 | return listener.str(); |
| 222 | } |
| 223 | |
| 224 | TEST(MatchResultListenerTest, StreamingWorks) { |
| 225 | StringMatchResultListener listener; |
| 226 | listener << "hi" << 5; |
| 227 | EXPECT_EQ("hi5", listener.str()); |
| 228 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 229 | listener.Clear(); |
| 230 | EXPECT_EQ("", listener.str()); |
| 231 | |
| 232 | listener << 42; |
| 233 | EXPECT_EQ("42", listener.str()); |
| 234 | |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 235 | // Streaming shouldn't crash when the underlying ostream is NULL. |
| 236 | DummyMatchResultListener dummy; |
| 237 | dummy << "hi" << 5; |
| 238 | } |
| 239 | |
| 240 | TEST(MatchResultListenerTest, CanAccessUnderlyingStream) { |
| 241 | EXPECT_TRUE(DummyMatchResultListener().stream() == NULL); |
| 242 | EXPECT_TRUE(StreamMatchResultListener(NULL).stream() == NULL); |
| 243 | |
| 244 | EXPECT_EQ(&std::cout, StreamMatchResultListener(&std::cout).stream()); |
| 245 | } |
| 246 | |
| 247 | TEST(MatchResultListenerTest, IsInterestedWorks) { |
| 248 | EXPECT_TRUE(StringMatchResultListener().IsInterested()); |
| 249 | EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested()); |
| 250 | |
| 251 | EXPECT_FALSE(DummyMatchResultListener().IsInterested()); |
| 252 | EXPECT_FALSE(StreamMatchResultListener(NULL).IsInterested()); |
| 253 | } |
| 254 | |
| 255 | // Makes sure that the MatcherInterface<T> interface doesn't |
| 256 | // change. |
| 257 | class EvenMatcherImpl : public MatcherInterface<int> { |
| 258 | public: |
| 259 | virtual bool MatchAndExplain(int x, |
| 260 | MatchResultListener* /* listener */) const { |
| 261 | return x % 2 == 0; |
| 262 | } |
| 263 | |
| 264 | virtual void DescribeTo(ostream* os) const { |
| 265 | *os << "is an even number"; |
| 266 | } |
| 267 | |
| 268 | // We deliberately don't define DescribeNegationTo() and |
| 269 | // ExplainMatchResultTo() here, to make sure the definition of these |
| 270 | // two methods is optional. |
| 271 | }; |
| 272 | |
| 273 | // Makes sure that the MatcherInterface API doesn't change. |
| 274 | TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) { |
| 275 | EvenMatcherImpl m; |
| 276 | } |
| 277 | |
| 278 | // Tests implementing a monomorphic matcher using MatchAndExplain(). |
| 279 | |
| 280 | class NewEvenMatcherImpl : public MatcherInterface<int> { |
| 281 | public: |
| 282 | virtual bool MatchAndExplain(int x, MatchResultListener* listener) const { |
| 283 | const bool match = x % 2 == 0; |
| 284 | // Verifies that we can stream to a listener directly. |
| 285 | *listener << "value % " << 2; |
| 286 | if (listener->stream() != NULL) { |
| 287 | // Verifies that we can stream to a listener's underlying stream |
| 288 | // too. |
| 289 | *listener->stream() << " == " << (x % 2); |
| 290 | } |
| 291 | return match; |
| 292 | } |
| 293 | |
| 294 | virtual void DescribeTo(ostream* os) const { |
| 295 | *os << "is an even number"; |
| 296 | } |
| 297 | }; |
| 298 | |
| 299 | TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) { |
| 300 | Matcher<int> m = MakeMatcher(new NewEvenMatcherImpl); |
| 301 | EXPECT_TRUE(m.Matches(2)); |
| 302 | EXPECT_FALSE(m.Matches(3)); |
| 303 | EXPECT_EQ("value % 2 == 0", Explain(m, 2)); |
| 304 | EXPECT_EQ("value % 2 == 1", Explain(m, 3)); |
| 305 | } |
| 306 | |
| 307 | // Tests default-constructing a matcher. |
| 308 | TEST(MatcherTest, CanBeDefaultConstructed) { |
| 309 | Matcher<double> m; |
| 310 | } |
| 311 | |
| 312 | // Tests that Matcher<T> can be constructed from a MatcherInterface<T>*. |
| 313 | TEST(MatcherTest, CanBeConstructedFromMatcherInterface) { |
| 314 | const MatcherInterface<int>* impl = new EvenMatcherImpl; |
| 315 | Matcher<int> m(impl); |
| 316 | EXPECT_TRUE(m.Matches(4)); |
| 317 | EXPECT_FALSE(m.Matches(5)); |
| 318 | } |
| 319 | |
| 320 | // Tests that value can be used in place of Eq(value). |
| 321 | TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) { |
| 322 | Matcher<int> m1 = 5; |
| 323 | EXPECT_TRUE(m1.Matches(5)); |
| 324 | EXPECT_FALSE(m1.Matches(6)); |
| 325 | } |
| 326 | |
| 327 | // Tests that NULL can be used in place of Eq(NULL). |
| 328 | TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) { |
| 329 | Matcher<int*> m1 = NULL; |
| 330 | EXPECT_TRUE(m1.Matches(NULL)); |
| 331 | int n = 0; |
| 332 | EXPECT_FALSE(m1.Matches(&n)); |
| 333 | } |
| 334 | |
| 335 | // Tests that matchers are copyable. |
| 336 | TEST(MatcherTest, IsCopyable) { |
| 337 | // Tests the copy constructor. |
| 338 | Matcher<bool> m1 = Eq(false); |
| 339 | EXPECT_TRUE(m1.Matches(false)); |
| 340 | EXPECT_FALSE(m1.Matches(true)); |
| 341 | |
| 342 | // Tests the assignment operator. |
| 343 | m1 = Eq(true); |
| 344 | EXPECT_TRUE(m1.Matches(true)); |
| 345 | EXPECT_FALSE(m1.Matches(false)); |
| 346 | } |
| 347 | |
| 348 | // Tests that Matcher<T>::DescribeTo() calls |
| 349 | // MatcherInterface<T>::DescribeTo(). |
| 350 | TEST(MatcherTest, CanDescribeItself) { |
| 351 | EXPECT_EQ("is an even number", |
| 352 | Describe(Matcher<int>(new EvenMatcherImpl))); |
| 353 | } |
| 354 | |
| 355 | // Tests Matcher<T>::MatchAndExplain(). |
| 356 | TEST(MatcherTest, MatchAndExplain) { |
| 357 | Matcher<int> m = GreaterThan(0); |
| 358 | StringMatchResultListener listener1; |
| 359 | EXPECT_TRUE(m.MatchAndExplain(42, &listener1)); |
| 360 | EXPECT_EQ("which is 42 more than 0", listener1.str()); |
| 361 | |
| 362 | StringMatchResultListener listener2; |
| 363 | EXPECT_FALSE(m.MatchAndExplain(-9, &listener2)); |
| 364 | EXPECT_EQ("which is 9 less than 0", listener2.str()); |
| 365 | } |
| 366 | |
| 367 | // Tests that a C-string literal can be implicitly converted to a |
| 368 | // Matcher<string> or Matcher<const string&>. |
| 369 | TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) { |
| 370 | Matcher<string> m1 = "hi"; |
| 371 | EXPECT_TRUE(m1.Matches("hi")); |
| 372 | EXPECT_FALSE(m1.Matches("hello")); |
| 373 | |
| 374 | Matcher<const string&> m2 = "hi"; |
| 375 | EXPECT_TRUE(m2.Matches("hi")); |
| 376 | EXPECT_FALSE(m2.Matches("hello")); |
| 377 | } |
| 378 | |
| 379 | // Tests that a string object can be implicitly converted to a |
| 380 | // Matcher<string> or Matcher<const string&>. |
| 381 | TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) { |
| 382 | Matcher<string> m1 = string("hi"); |
| 383 | EXPECT_TRUE(m1.Matches("hi")); |
| 384 | EXPECT_FALSE(m1.Matches("hello")); |
| 385 | |
| 386 | Matcher<const string&> m2 = string("hi"); |
| 387 | EXPECT_TRUE(m2.Matches("hi")); |
| 388 | EXPECT_FALSE(m2.Matches("hello")); |
| 389 | } |
| 390 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 391 | #if GTEST_HAS_STRING_PIECE_ |
| 392 | // Tests that a C-string literal can be implicitly converted to a |
| 393 | // Matcher<StringPiece> or Matcher<const StringPiece&>. |
| 394 | TEST(StringPieceMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) { |
| 395 | Matcher<StringPiece> m1 = "cats"; |
| 396 | EXPECT_TRUE(m1.Matches("cats")); |
| 397 | EXPECT_FALSE(m1.Matches("dogs")); |
| 398 | |
| 399 | Matcher<const StringPiece&> m2 = "cats"; |
| 400 | EXPECT_TRUE(m2.Matches("cats")); |
| 401 | EXPECT_FALSE(m2.Matches("dogs")); |
| 402 | } |
| 403 | |
| 404 | // Tests that a string object can be implicitly converted to a |
| 405 | // Matcher<StringPiece> or Matcher<const StringPiece&>. |
| 406 | TEST(StringPieceMatcherTest, CanBeImplicitlyConstructedFromString) { |
| 407 | Matcher<StringPiece> m1 = string("cats"); |
| 408 | EXPECT_TRUE(m1.Matches("cats")); |
| 409 | EXPECT_FALSE(m1.Matches("dogs")); |
| 410 | |
| 411 | Matcher<const StringPiece&> m2 = string("cats"); |
| 412 | EXPECT_TRUE(m2.Matches("cats")); |
| 413 | EXPECT_FALSE(m2.Matches("dogs")); |
| 414 | } |
| 415 | |
| 416 | // Tests that a StringPiece object can be implicitly converted to a |
| 417 | // Matcher<StringPiece> or Matcher<const StringPiece&>. |
| 418 | TEST(StringPieceMatcherTest, CanBeImplicitlyConstructedFromStringPiece) { |
| 419 | Matcher<StringPiece> m1 = StringPiece("cats"); |
| 420 | EXPECT_TRUE(m1.Matches("cats")); |
| 421 | EXPECT_FALSE(m1.Matches("dogs")); |
| 422 | |
| 423 | Matcher<const StringPiece&> m2 = StringPiece("cats"); |
| 424 | EXPECT_TRUE(m2.Matches("cats")); |
| 425 | EXPECT_FALSE(m2.Matches("dogs")); |
| 426 | } |
| 427 | #endif // GTEST_HAS_STRING_PIECE_ |
| 428 | |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 429 | // Tests that MakeMatcher() constructs a Matcher<T> from a |
| 430 | // MatcherInterface* without requiring the user to explicitly |
| 431 | // write the type. |
| 432 | TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) { |
| 433 | const MatcherInterface<int>* dummy_impl = NULL; |
| 434 | Matcher<int> m = MakeMatcher(dummy_impl); |
| 435 | } |
| 436 | |
| 437 | // Tests that MakePolymorphicMatcher() can construct a polymorphic |
| 438 | // matcher from its implementation using the old API. |
| 439 | const int g_bar = 1; |
| 440 | class ReferencesBarOrIsZeroImpl { |
| 441 | public: |
| 442 | template <typename T> |
| 443 | bool MatchAndExplain(const T& x, |
| 444 | MatchResultListener* /* listener */) const { |
| 445 | const void* p = &x; |
| 446 | return p == &g_bar || x == 0; |
| 447 | } |
| 448 | |
| 449 | void DescribeTo(ostream* os) const { *os << "g_bar or zero"; } |
| 450 | |
| 451 | void DescribeNegationTo(ostream* os) const { |
| 452 | *os << "doesn't reference g_bar and is not zero"; |
| 453 | } |
| 454 | }; |
| 455 | |
| 456 | // This function verifies that MakePolymorphicMatcher() returns a |
| 457 | // PolymorphicMatcher<T> where T is the argument's type. |
| 458 | PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() { |
| 459 | return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl()); |
| 460 | } |
| 461 | |
| 462 | TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) { |
| 463 | // Using a polymorphic matcher to match a reference type. |
| 464 | Matcher<const int&> m1 = ReferencesBarOrIsZero(); |
| 465 | EXPECT_TRUE(m1.Matches(0)); |
| 466 | // Verifies that the identity of a by-reference argument is preserved. |
| 467 | EXPECT_TRUE(m1.Matches(g_bar)); |
| 468 | EXPECT_FALSE(m1.Matches(1)); |
| 469 | EXPECT_EQ("g_bar or zero", Describe(m1)); |
| 470 | |
| 471 | // Using a polymorphic matcher to match a value type. |
| 472 | Matcher<double> m2 = ReferencesBarOrIsZero(); |
| 473 | EXPECT_TRUE(m2.Matches(0.0)); |
| 474 | EXPECT_FALSE(m2.Matches(0.1)); |
| 475 | EXPECT_EQ("g_bar or zero", Describe(m2)); |
| 476 | } |
| 477 | |
| 478 | // Tests implementing a polymorphic matcher using MatchAndExplain(). |
| 479 | |
| 480 | class PolymorphicIsEvenImpl { |
| 481 | public: |
| 482 | void DescribeTo(ostream* os) const { *os << "is even"; } |
| 483 | |
| 484 | void DescribeNegationTo(ostream* os) const { |
| 485 | *os << "is odd"; |
| 486 | } |
| 487 | |
| 488 | template <typename T> |
| 489 | bool MatchAndExplain(const T& x, MatchResultListener* listener) const { |
| 490 | // Verifies that we can stream to the listener directly. |
| 491 | *listener << "% " << 2; |
| 492 | if (listener->stream() != NULL) { |
| 493 | // Verifies that we can stream to the listener's underlying stream |
| 494 | // too. |
| 495 | *listener->stream() << " == " << (x % 2); |
| 496 | } |
| 497 | return (x % 2) == 0; |
| 498 | } |
| 499 | }; |
| 500 | |
| 501 | PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() { |
| 502 | return MakePolymorphicMatcher(PolymorphicIsEvenImpl()); |
| 503 | } |
| 504 | |
| 505 | TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) { |
| 506 | // Using PolymorphicIsEven() as a Matcher<int>. |
| 507 | const Matcher<int> m1 = PolymorphicIsEven(); |
| 508 | EXPECT_TRUE(m1.Matches(42)); |
| 509 | EXPECT_FALSE(m1.Matches(43)); |
| 510 | EXPECT_EQ("is even", Describe(m1)); |
| 511 | |
| 512 | const Matcher<int> not_m1 = Not(m1); |
| 513 | EXPECT_EQ("is odd", Describe(not_m1)); |
| 514 | |
| 515 | EXPECT_EQ("% 2 == 0", Explain(m1, 42)); |
| 516 | |
| 517 | // Using PolymorphicIsEven() as a Matcher<char>. |
| 518 | const Matcher<char> m2 = PolymorphicIsEven(); |
| 519 | EXPECT_TRUE(m2.Matches('\x42')); |
| 520 | EXPECT_FALSE(m2.Matches('\x43')); |
| 521 | EXPECT_EQ("is even", Describe(m2)); |
| 522 | |
| 523 | const Matcher<char> not_m2 = Not(m2); |
| 524 | EXPECT_EQ("is odd", Describe(not_m2)); |
| 525 | |
| 526 | EXPECT_EQ("% 2 == 0", Explain(m2, '\x42')); |
| 527 | } |
| 528 | |
| 529 | // Tests that MatcherCast<T>(m) works when m is a polymorphic matcher. |
| 530 | TEST(MatcherCastTest, FromPolymorphicMatcher) { |
| 531 | Matcher<int> m = MatcherCast<int>(Eq(5)); |
| 532 | EXPECT_TRUE(m.Matches(5)); |
| 533 | EXPECT_FALSE(m.Matches(6)); |
| 534 | } |
| 535 | |
| 536 | // For testing casting matchers between compatible types. |
| 537 | class IntValue { |
| 538 | public: |
| 539 | // An int can be statically (although not implicitly) cast to a |
| 540 | // IntValue. |
| 541 | explicit IntValue(int a_value) : value_(a_value) {} |
| 542 | |
| 543 | int value() const { return value_; } |
| 544 | private: |
| 545 | int value_; |
| 546 | }; |
| 547 | |
| 548 | // For testing casting matchers between compatible types. |
| 549 | bool IsPositiveIntValue(const IntValue& foo) { |
| 550 | return foo.value() > 0; |
| 551 | } |
| 552 | |
| 553 | // Tests that MatcherCast<T>(m) works when m is a Matcher<U> where T |
| 554 | // can be statically converted to U. |
| 555 | TEST(MatcherCastTest, FromCompatibleType) { |
| 556 | Matcher<double> m1 = Eq(2.0); |
| 557 | Matcher<int> m2 = MatcherCast<int>(m1); |
| 558 | EXPECT_TRUE(m2.Matches(2)); |
| 559 | EXPECT_FALSE(m2.Matches(3)); |
| 560 | |
| 561 | Matcher<IntValue> m3 = Truly(IsPositiveIntValue); |
| 562 | Matcher<int> m4 = MatcherCast<int>(m3); |
| 563 | // In the following, the arguments 1 and 0 are statically converted |
| 564 | // to IntValue objects, and then tested by the IsPositiveIntValue() |
| 565 | // predicate. |
| 566 | EXPECT_TRUE(m4.Matches(1)); |
| 567 | EXPECT_FALSE(m4.Matches(0)); |
| 568 | } |
| 569 | |
| 570 | // Tests that MatcherCast<T>(m) works when m is a Matcher<const T&>. |
| 571 | TEST(MatcherCastTest, FromConstReferenceToNonReference) { |
| 572 | Matcher<const int&> m1 = Eq(0); |
| 573 | Matcher<int> m2 = MatcherCast<int>(m1); |
| 574 | EXPECT_TRUE(m2.Matches(0)); |
| 575 | EXPECT_FALSE(m2.Matches(1)); |
| 576 | } |
| 577 | |
| 578 | // Tests that MatcherCast<T>(m) works when m is a Matcher<T&>. |
| 579 | TEST(MatcherCastTest, FromReferenceToNonReference) { |
| 580 | Matcher<int&> m1 = Eq(0); |
| 581 | Matcher<int> m2 = MatcherCast<int>(m1); |
| 582 | EXPECT_TRUE(m2.Matches(0)); |
| 583 | EXPECT_FALSE(m2.Matches(1)); |
| 584 | } |
| 585 | |
| 586 | // Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>. |
| 587 | TEST(MatcherCastTest, FromNonReferenceToConstReference) { |
| 588 | Matcher<int> m1 = Eq(0); |
| 589 | Matcher<const int&> m2 = MatcherCast<const int&>(m1); |
| 590 | EXPECT_TRUE(m2.Matches(0)); |
| 591 | EXPECT_FALSE(m2.Matches(1)); |
| 592 | } |
| 593 | |
| 594 | // Tests that MatcherCast<T&>(m) works when m is a Matcher<T>. |
| 595 | TEST(MatcherCastTest, FromNonReferenceToReference) { |
| 596 | Matcher<int> m1 = Eq(0); |
| 597 | Matcher<int&> m2 = MatcherCast<int&>(m1); |
| 598 | int n = 0; |
| 599 | EXPECT_TRUE(m2.Matches(n)); |
| 600 | n = 1; |
| 601 | EXPECT_FALSE(m2.Matches(n)); |
| 602 | } |
| 603 | |
| 604 | // Tests that MatcherCast<T>(m) works when m is a Matcher<T>. |
| 605 | TEST(MatcherCastTest, FromSameType) { |
| 606 | Matcher<int> m1 = Eq(0); |
| 607 | Matcher<int> m2 = MatcherCast<int>(m1); |
| 608 | EXPECT_TRUE(m2.Matches(0)); |
| 609 | EXPECT_FALSE(m2.Matches(1)); |
| 610 | } |
| 611 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 612 | // Implicitly convertible from any type. |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 613 | struct ConvertibleFromAny { |
| 614 | ConvertibleFromAny(int a_value) : value(a_value) {} |
| 615 | template <typename T> |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 616 | ConvertibleFromAny(const T& /*a_value*/) : value(-1) { |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 617 | ADD_FAILURE() << "Conversion constructor called"; |
| 618 | } |
| 619 | int value; |
| 620 | }; |
| 621 | |
| 622 | bool operator==(const ConvertibleFromAny& a, const ConvertibleFromAny& b) { |
| 623 | return a.value == b.value; |
| 624 | } |
| 625 | |
| 626 | ostream& operator<<(ostream& os, const ConvertibleFromAny& a) { |
| 627 | return os << a.value; |
| 628 | } |
| 629 | |
| 630 | TEST(MatcherCastTest, ConversionConstructorIsUsed) { |
| 631 | Matcher<ConvertibleFromAny> m = MatcherCast<ConvertibleFromAny>(1); |
| 632 | EXPECT_TRUE(m.Matches(ConvertibleFromAny(1))); |
| 633 | EXPECT_FALSE(m.Matches(ConvertibleFromAny(2))); |
| 634 | } |
| 635 | |
| 636 | TEST(MatcherCastTest, FromConvertibleFromAny) { |
| 637 | Matcher<ConvertibleFromAny> m = |
| 638 | MatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1))); |
| 639 | EXPECT_TRUE(m.Matches(ConvertibleFromAny(1))); |
| 640 | EXPECT_FALSE(m.Matches(ConvertibleFromAny(2))); |
| 641 | } |
| 642 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 643 | struct IntReferenceWrapper { |
| 644 | IntReferenceWrapper(const int& a_value) : value(&a_value) {} |
| 645 | const int* value; |
| 646 | }; |
| 647 | |
| 648 | bool operator==(const IntReferenceWrapper& a, const IntReferenceWrapper& b) { |
| 649 | return a.value == b.value; |
| 650 | } |
| 651 | |
| 652 | TEST(MatcherCastTest, ValueIsNotCopied) { |
| 653 | int n = 42; |
| 654 | Matcher<IntReferenceWrapper> m = MatcherCast<IntReferenceWrapper>(n); |
| 655 | // Verify that the matcher holds a reference to n, not to its temporary copy. |
| 656 | EXPECT_TRUE(m.Matches(n)); |
| 657 | } |
| 658 | |
| 659 | class Base { |
| 660 | public: |
| 661 | virtual ~Base() {} |
| 662 | Base() {} |
| 663 | private: |
| 664 | GTEST_DISALLOW_COPY_AND_ASSIGN_(Base); |
| 665 | }; |
| 666 | |
| 667 | class Derived : public Base { |
| 668 | public: |
| 669 | Derived() : Base() {} |
| 670 | int i; |
| 671 | }; |
| 672 | |
| 673 | class OtherDerived : public Base {}; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 674 | |
| 675 | // Tests that SafeMatcherCast<T>(m) works when m is a polymorphic matcher. |
| 676 | TEST(SafeMatcherCastTest, FromPolymorphicMatcher) { |
| 677 | Matcher<char> m2 = SafeMatcherCast<char>(Eq(32)); |
| 678 | EXPECT_TRUE(m2.Matches(' ')); |
| 679 | EXPECT_FALSE(m2.Matches('\n')); |
| 680 | } |
| 681 | |
| 682 | // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where |
| 683 | // T and U are arithmetic types and T can be losslessly converted to |
| 684 | // U. |
| 685 | TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) { |
| 686 | Matcher<double> m1 = DoubleEq(1.0); |
| 687 | Matcher<float> m2 = SafeMatcherCast<float>(m1); |
| 688 | EXPECT_TRUE(m2.Matches(1.0f)); |
| 689 | EXPECT_FALSE(m2.Matches(2.0f)); |
| 690 | |
| 691 | Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>('a')); |
| 692 | EXPECT_TRUE(m3.Matches('a')); |
| 693 | EXPECT_FALSE(m3.Matches('b')); |
| 694 | } |
| 695 | |
| 696 | // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where T and U |
| 697 | // are pointers or references to a derived and a base class, correspondingly. |
| 698 | TEST(SafeMatcherCastTest, FromBaseClass) { |
| 699 | Derived d, d2; |
| 700 | Matcher<Base*> m1 = Eq(&d); |
| 701 | Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1); |
| 702 | EXPECT_TRUE(m2.Matches(&d)); |
| 703 | EXPECT_FALSE(m2.Matches(&d2)); |
| 704 | |
| 705 | Matcher<Base&> m3 = Ref(d); |
| 706 | Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3); |
| 707 | EXPECT_TRUE(m4.Matches(d)); |
| 708 | EXPECT_FALSE(m4.Matches(d2)); |
| 709 | } |
| 710 | |
| 711 | // Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<const T&>. |
| 712 | TEST(SafeMatcherCastTest, FromConstReferenceToReference) { |
| 713 | int n = 0; |
| 714 | Matcher<const int&> m1 = Ref(n); |
| 715 | Matcher<int&> m2 = SafeMatcherCast<int&>(m1); |
| 716 | int n1 = 0; |
| 717 | EXPECT_TRUE(m2.Matches(n)); |
| 718 | EXPECT_FALSE(m2.Matches(n1)); |
| 719 | } |
| 720 | |
| 721 | // Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>. |
| 722 | TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) { |
| 723 | Matcher<int> m1 = Eq(0); |
| 724 | Matcher<const int&> m2 = SafeMatcherCast<const int&>(m1); |
| 725 | EXPECT_TRUE(m2.Matches(0)); |
| 726 | EXPECT_FALSE(m2.Matches(1)); |
| 727 | } |
| 728 | |
| 729 | // Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<T>. |
| 730 | TEST(SafeMatcherCastTest, FromNonReferenceToReference) { |
| 731 | Matcher<int> m1 = Eq(0); |
| 732 | Matcher<int&> m2 = SafeMatcherCast<int&>(m1); |
| 733 | int n = 0; |
| 734 | EXPECT_TRUE(m2.Matches(n)); |
| 735 | n = 1; |
| 736 | EXPECT_FALSE(m2.Matches(n)); |
| 737 | } |
| 738 | |
| 739 | // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<T>. |
| 740 | TEST(SafeMatcherCastTest, FromSameType) { |
| 741 | Matcher<int> m1 = Eq(0); |
| 742 | Matcher<int> m2 = SafeMatcherCast<int>(m1); |
| 743 | EXPECT_TRUE(m2.Matches(0)); |
| 744 | EXPECT_FALSE(m2.Matches(1)); |
| 745 | } |
| 746 | |
| 747 | TEST(SafeMatcherCastTest, ConversionConstructorIsUsed) { |
| 748 | Matcher<ConvertibleFromAny> m = SafeMatcherCast<ConvertibleFromAny>(1); |
| 749 | EXPECT_TRUE(m.Matches(ConvertibleFromAny(1))); |
| 750 | EXPECT_FALSE(m.Matches(ConvertibleFromAny(2))); |
| 751 | } |
| 752 | |
| 753 | TEST(SafeMatcherCastTest, FromConvertibleFromAny) { |
| 754 | Matcher<ConvertibleFromAny> m = |
| 755 | SafeMatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1))); |
| 756 | EXPECT_TRUE(m.Matches(ConvertibleFromAny(1))); |
| 757 | EXPECT_FALSE(m.Matches(ConvertibleFromAny(2))); |
| 758 | } |
| 759 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 760 | TEST(SafeMatcherCastTest, ValueIsNotCopied) { |
| 761 | int n = 42; |
| 762 | Matcher<IntReferenceWrapper> m = SafeMatcherCast<IntReferenceWrapper>(n); |
| 763 | // Verify that the matcher holds a reference to n, not to its temporary copy. |
| 764 | EXPECT_TRUE(m.Matches(n)); |
| 765 | } |
| 766 | |
| 767 | TEST(ExpectThat, TakesLiterals) { |
| 768 | EXPECT_THAT(1, 1); |
| 769 | EXPECT_THAT(1.0, 1.0); |
| 770 | EXPECT_THAT(string(), ""); |
| 771 | } |
| 772 | |
| 773 | TEST(ExpectThat, TakesFunctions) { |
| 774 | struct Helper { |
| 775 | static void Func() {} |
| 776 | }; |
| 777 | void (*func)() = Helper::Func; |
| 778 | EXPECT_THAT(func, Helper::Func); |
| 779 | EXPECT_THAT(func, &Helper::Func); |
| 780 | } |
| 781 | |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 782 | // Tests that A<T>() matches any value of type T. |
| 783 | TEST(ATest, MatchesAnyValue) { |
| 784 | // Tests a matcher for a value type. |
| 785 | Matcher<double> m1 = A<double>(); |
| 786 | EXPECT_TRUE(m1.Matches(91.43)); |
| 787 | EXPECT_TRUE(m1.Matches(-15.32)); |
| 788 | |
| 789 | // Tests a matcher for a reference type. |
| 790 | int a = 2; |
| 791 | int b = -6; |
| 792 | Matcher<int&> m2 = A<int&>(); |
| 793 | EXPECT_TRUE(m2.Matches(a)); |
| 794 | EXPECT_TRUE(m2.Matches(b)); |
| 795 | } |
| 796 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 797 | TEST(ATest, WorksForDerivedClass) { |
| 798 | Base base; |
| 799 | Derived derived; |
| 800 | EXPECT_THAT(&base, A<Base*>()); |
| 801 | // This shouldn't compile: EXPECT_THAT(&base, A<Derived*>()); |
| 802 | EXPECT_THAT(&derived, A<Base*>()); |
| 803 | EXPECT_THAT(&derived, A<Derived*>()); |
| 804 | } |
| 805 | |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 806 | // Tests that A<T>() describes itself properly. |
| 807 | TEST(ATest, CanDescribeSelf) { |
| 808 | EXPECT_EQ("is anything", Describe(A<bool>())); |
| 809 | } |
| 810 | |
| 811 | // Tests that An<T>() matches any value of type T. |
| 812 | TEST(AnTest, MatchesAnyValue) { |
| 813 | // Tests a matcher for a value type. |
| 814 | Matcher<int> m1 = An<int>(); |
| 815 | EXPECT_TRUE(m1.Matches(9143)); |
| 816 | EXPECT_TRUE(m1.Matches(-1532)); |
| 817 | |
| 818 | // Tests a matcher for a reference type. |
| 819 | int a = 2; |
| 820 | int b = -6; |
| 821 | Matcher<int&> m2 = An<int&>(); |
| 822 | EXPECT_TRUE(m2.Matches(a)); |
| 823 | EXPECT_TRUE(m2.Matches(b)); |
| 824 | } |
| 825 | |
| 826 | // Tests that An<T>() describes itself properly. |
| 827 | TEST(AnTest, CanDescribeSelf) { |
| 828 | EXPECT_EQ("is anything", Describe(An<int>())); |
| 829 | } |
| 830 | |
| 831 | // Tests that _ can be used as a matcher for any type and matches any |
| 832 | // value of that type. |
| 833 | TEST(UnderscoreTest, MatchesAnyValue) { |
| 834 | // Uses _ as a matcher for a value type. |
| 835 | Matcher<int> m1 = _; |
| 836 | EXPECT_TRUE(m1.Matches(123)); |
| 837 | EXPECT_TRUE(m1.Matches(-242)); |
| 838 | |
| 839 | // Uses _ as a matcher for a reference type. |
| 840 | bool a = false; |
| 841 | const bool b = true; |
| 842 | Matcher<const bool&> m2 = _; |
| 843 | EXPECT_TRUE(m2.Matches(a)); |
| 844 | EXPECT_TRUE(m2.Matches(b)); |
| 845 | } |
| 846 | |
| 847 | // Tests that _ describes itself properly. |
| 848 | TEST(UnderscoreTest, CanDescribeSelf) { |
| 849 | Matcher<int> m = _; |
| 850 | EXPECT_EQ("is anything", Describe(m)); |
| 851 | } |
| 852 | |
| 853 | // Tests that Eq(x) matches any value equal to x. |
| 854 | TEST(EqTest, MatchesEqualValue) { |
| 855 | // 2 C-strings with same content but different addresses. |
| 856 | const char a1[] = "hi"; |
| 857 | const char a2[] = "hi"; |
| 858 | |
| 859 | Matcher<const char*> m1 = Eq(a1); |
| 860 | EXPECT_TRUE(m1.Matches(a1)); |
| 861 | EXPECT_FALSE(m1.Matches(a2)); |
| 862 | } |
| 863 | |
| 864 | // Tests that Eq(v) describes itself properly. |
| 865 | |
| 866 | class Unprintable { |
| 867 | public: |
| 868 | Unprintable() : c_('a') {} |
| 869 | |
| 870 | bool operator==(const Unprintable& /* rhs */) { return true; } |
| 871 | private: |
| 872 | char c_; |
| 873 | }; |
| 874 | |
| 875 | TEST(EqTest, CanDescribeSelf) { |
| 876 | Matcher<Unprintable> m = Eq(Unprintable()); |
| 877 | EXPECT_EQ("is equal to 1-byte object <61>", Describe(m)); |
| 878 | } |
| 879 | |
| 880 | // Tests that Eq(v) can be used to match any type that supports |
| 881 | // comparing with type T, where T is v's type. |
| 882 | TEST(EqTest, IsPolymorphic) { |
| 883 | Matcher<int> m1 = Eq(1); |
| 884 | EXPECT_TRUE(m1.Matches(1)); |
| 885 | EXPECT_FALSE(m1.Matches(2)); |
| 886 | |
| 887 | Matcher<char> m2 = Eq(1); |
| 888 | EXPECT_TRUE(m2.Matches('\1')); |
| 889 | EXPECT_FALSE(m2.Matches('a')); |
| 890 | } |
| 891 | |
| 892 | // Tests that TypedEq<T>(v) matches values of type T that's equal to v. |
| 893 | TEST(TypedEqTest, ChecksEqualityForGivenType) { |
| 894 | Matcher<char> m1 = TypedEq<char>('a'); |
| 895 | EXPECT_TRUE(m1.Matches('a')); |
| 896 | EXPECT_FALSE(m1.Matches('b')); |
| 897 | |
| 898 | Matcher<int> m2 = TypedEq<int>(6); |
| 899 | EXPECT_TRUE(m2.Matches(6)); |
| 900 | EXPECT_FALSE(m2.Matches(7)); |
| 901 | } |
| 902 | |
| 903 | // Tests that TypedEq(v) describes itself properly. |
| 904 | TEST(TypedEqTest, CanDescribeSelf) { |
| 905 | EXPECT_EQ("is equal to 2", Describe(TypedEq<int>(2))); |
| 906 | } |
| 907 | |
| 908 | // Tests that TypedEq<T>(v) has type Matcher<T>. |
| 909 | |
| 910 | // Type<T>::IsTypeOf(v) compiles iff the type of value v is T, where T |
| 911 | // is a "bare" type (i.e. not in the form of const U or U&). If v's |
| 912 | // type is not T, the compiler will generate a message about |
| 913 | // "undefined referece". |
| 914 | template <typename T> |
| 915 | struct Type { |
| 916 | static bool IsTypeOf(const T& /* v */) { return true; } |
| 917 | |
| 918 | template <typename T2> |
| 919 | static void IsTypeOf(T2 v); |
| 920 | }; |
| 921 | |
| 922 | TEST(TypedEqTest, HasSpecifiedType) { |
| 923 | // Verfies that the type of TypedEq<T>(v) is Matcher<T>. |
| 924 | Type<Matcher<int> >::IsTypeOf(TypedEq<int>(5)); |
| 925 | Type<Matcher<double> >::IsTypeOf(TypedEq<double>(5)); |
| 926 | } |
| 927 | |
| 928 | // Tests that Ge(v) matches anything >= v. |
| 929 | TEST(GeTest, ImplementsGreaterThanOrEqual) { |
| 930 | Matcher<int> m1 = Ge(0); |
| 931 | EXPECT_TRUE(m1.Matches(1)); |
| 932 | EXPECT_TRUE(m1.Matches(0)); |
| 933 | EXPECT_FALSE(m1.Matches(-1)); |
| 934 | } |
| 935 | |
| 936 | // Tests that Ge(v) describes itself properly. |
| 937 | TEST(GeTest, CanDescribeSelf) { |
| 938 | Matcher<int> m = Ge(5); |
| 939 | EXPECT_EQ("is >= 5", Describe(m)); |
| 940 | } |
| 941 | |
| 942 | // Tests that Gt(v) matches anything > v. |
| 943 | TEST(GtTest, ImplementsGreaterThan) { |
| 944 | Matcher<double> m1 = Gt(0); |
| 945 | EXPECT_TRUE(m1.Matches(1.0)); |
| 946 | EXPECT_FALSE(m1.Matches(0.0)); |
| 947 | EXPECT_FALSE(m1.Matches(-1.0)); |
| 948 | } |
| 949 | |
| 950 | // Tests that Gt(v) describes itself properly. |
| 951 | TEST(GtTest, CanDescribeSelf) { |
| 952 | Matcher<int> m = Gt(5); |
| 953 | EXPECT_EQ("is > 5", Describe(m)); |
| 954 | } |
| 955 | |
| 956 | // Tests that Le(v) matches anything <= v. |
| 957 | TEST(LeTest, ImplementsLessThanOrEqual) { |
| 958 | Matcher<char> m1 = Le('b'); |
| 959 | EXPECT_TRUE(m1.Matches('a')); |
| 960 | EXPECT_TRUE(m1.Matches('b')); |
| 961 | EXPECT_FALSE(m1.Matches('c')); |
| 962 | } |
| 963 | |
| 964 | // Tests that Le(v) describes itself properly. |
| 965 | TEST(LeTest, CanDescribeSelf) { |
| 966 | Matcher<int> m = Le(5); |
| 967 | EXPECT_EQ("is <= 5", Describe(m)); |
| 968 | } |
| 969 | |
| 970 | // Tests that Lt(v) matches anything < v. |
| 971 | TEST(LtTest, ImplementsLessThan) { |
| 972 | Matcher<const string&> m1 = Lt("Hello"); |
| 973 | EXPECT_TRUE(m1.Matches("Abc")); |
| 974 | EXPECT_FALSE(m1.Matches("Hello")); |
| 975 | EXPECT_FALSE(m1.Matches("Hello, world!")); |
| 976 | } |
| 977 | |
| 978 | // Tests that Lt(v) describes itself properly. |
| 979 | TEST(LtTest, CanDescribeSelf) { |
| 980 | Matcher<int> m = Lt(5); |
| 981 | EXPECT_EQ("is < 5", Describe(m)); |
| 982 | } |
| 983 | |
| 984 | // Tests that Ne(v) matches anything != v. |
| 985 | TEST(NeTest, ImplementsNotEqual) { |
| 986 | Matcher<int> m1 = Ne(0); |
| 987 | EXPECT_TRUE(m1.Matches(1)); |
| 988 | EXPECT_TRUE(m1.Matches(-1)); |
| 989 | EXPECT_FALSE(m1.Matches(0)); |
| 990 | } |
| 991 | |
| 992 | // Tests that Ne(v) describes itself properly. |
| 993 | TEST(NeTest, CanDescribeSelf) { |
| 994 | Matcher<int> m = Ne(5); |
| 995 | EXPECT_EQ("isn't equal to 5", Describe(m)); |
| 996 | } |
| 997 | |
| 998 | // Tests that IsNull() matches any NULL pointer of any type. |
| 999 | TEST(IsNullTest, MatchesNullPointer) { |
| 1000 | Matcher<int*> m1 = IsNull(); |
| 1001 | int* p1 = NULL; |
| 1002 | int n = 0; |
| 1003 | EXPECT_TRUE(m1.Matches(p1)); |
| 1004 | EXPECT_FALSE(m1.Matches(&n)); |
| 1005 | |
| 1006 | Matcher<const char*> m2 = IsNull(); |
| 1007 | const char* p2 = NULL; |
| 1008 | EXPECT_TRUE(m2.Matches(p2)); |
| 1009 | EXPECT_FALSE(m2.Matches("hi")); |
| 1010 | |
| 1011 | #if !GTEST_OS_SYMBIAN |
| 1012 | // Nokia's Symbian compiler generates: |
| 1013 | // gmock-matchers.h: ambiguous access to overloaded function |
| 1014 | // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(void *)' |
| 1015 | // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(const testing:: |
| 1016 | // MatcherInterface<void *> *)' |
| 1017 | // gmock-matchers.h: (point of instantiation: 'testing:: |
| 1018 | // gmock_matchers_test::IsNullTest_MatchesNullPointer_Test::TestBody()') |
| 1019 | // gmock-matchers.h: (instantiating: 'testing::PolymorphicMatc |
| 1020 | Matcher<void*> m3 = IsNull(); |
| 1021 | void* p3 = NULL; |
| 1022 | EXPECT_TRUE(m3.Matches(p3)); |
| 1023 | EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef))); |
| 1024 | #endif |
| 1025 | } |
| 1026 | |
| 1027 | TEST(IsNullTest, LinkedPtr) { |
| 1028 | const Matcher<linked_ptr<int> > m = IsNull(); |
| 1029 | const linked_ptr<int> null_p; |
| 1030 | const linked_ptr<int> non_null_p(new int); |
| 1031 | |
| 1032 | EXPECT_TRUE(m.Matches(null_p)); |
| 1033 | EXPECT_FALSE(m.Matches(non_null_p)); |
| 1034 | } |
| 1035 | |
| 1036 | TEST(IsNullTest, ReferenceToConstLinkedPtr) { |
| 1037 | const Matcher<const linked_ptr<double>&> m = IsNull(); |
| 1038 | const linked_ptr<double> null_p; |
| 1039 | const linked_ptr<double> non_null_p(new double); |
| 1040 | |
| 1041 | EXPECT_TRUE(m.Matches(null_p)); |
| 1042 | EXPECT_FALSE(m.Matches(non_null_p)); |
| 1043 | } |
| 1044 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 1045 | #if GTEST_LANG_CXX11 |
| 1046 | TEST(IsNullTest, StdFunction) { |
| 1047 | const Matcher<std::function<void()>> m = IsNull(); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 1048 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 1049 | EXPECT_TRUE(m.Matches(std::function<void()>())); |
| 1050 | EXPECT_FALSE(m.Matches([]{})); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 1051 | } |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 1052 | #endif // GTEST_LANG_CXX11 |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 1053 | |
| 1054 | // Tests that IsNull() describes itself properly. |
| 1055 | TEST(IsNullTest, CanDescribeSelf) { |
| 1056 | Matcher<int*> m = IsNull(); |
| 1057 | EXPECT_EQ("is NULL", Describe(m)); |
| 1058 | EXPECT_EQ("isn't NULL", DescribeNegation(m)); |
| 1059 | } |
| 1060 | |
| 1061 | // Tests that NotNull() matches any non-NULL pointer of any type. |
| 1062 | TEST(NotNullTest, MatchesNonNullPointer) { |
| 1063 | Matcher<int*> m1 = NotNull(); |
| 1064 | int* p1 = NULL; |
| 1065 | int n = 0; |
| 1066 | EXPECT_FALSE(m1.Matches(p1)); |
| 1067 | EXPECT_TRUE(m1.Matches(&n)); |
| 1068 | |
| 1069 | Matcher<const char*> m2 = NotNull(); |
| 1070 | const char* p2 = NULL; |
| 1071 | EXPECT_FALSE(m2.Matches(p2)); |
| 1072 | EXPECT_TRUE(m2.Matches("hi")); |
| 1073 | } |
| 1074 | |
| 1075 | TEST(NotNullTest, LinkedPtr) { |
| 1076 | const Matcher<linked_ptr<int> > m = NotNull(); |
| 1077 | const linked_ptr<int> null_p; |
| 1078 | const linked_ptr<int> non_null_p(new int); |
| 1079 | |
| 1080 | EXPECT_FALSE(m.Matches(null_p)); |
| 1081 | EXPECT_TRUE(m.Matches(non_null_p)); |
| 1082 | } |
| 1083 | |
| 1084 | TEST(NotNullTest, ReferenceToConstLinkedPtr) { |
| 1085 | const Matcher<const linked_ptr<double>&> m = NotNull(); |
| 1086 | const linked_ptr<double> null_p; |
| 1087 | const linked_ptr<double> non_null_p(new double); |
| 1088 | |
| 1089 | EXPECT_FALSE(m.Matches(null_p)); |
| 1090 | EXPECT_TRUE(m.Matches(non_null_p)); |
| 1091 | } |
| 1092 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 1093 | #if GTEST_LANG_CXX11 |
| 1094 | TEST(NotNullTest, StdFunction) { |
| 1095 | const Matcher<std::function<void()>> m = NotNull(); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 1096 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 1097 | EXPECT_TRUE(m.Matches([]{})); |
| 1098 | EXPECT_FALSE(m.Matches(std::function<void()>())); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 1099 | } |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 1100 | #endif // GTEST_LANG_CXX11 |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 1101 | |
| 1102 | // Tests that NotNull() describes itself properly. |
| 1103 | TEST(NotNullTest, CanDescribeSelf) { |
| 1104 | Matcher<int*> m = NotNull(); |
| 1105 | EXPECT_EQ("isn't NULL", Describe(m)); |
| 1106 | } |
| 1107 | |
| 1108 | // Tests that Ref(variable) matches an argument that references |
| 1109 | // 'variable'. |
| 1110 | TEST(RefTest, MatchesSameVariable) { |
| 1111 | int a = 0; |
| 1112 | int b = 0; |
| 1113 | Matcher<int&> m = Ref(a); |
| 1114 | EXPECT_TRUE(m.Matches(a)); |
| 1115 | EXPECT_FALSE(m.Matches(b)); |
| 1116 | } |
| 1117 | |
| 1118 | // Tests that Ref(variable) describes itself properly. |
| 1119 | TEST(RefTest, CanDescribeSelf) { |
| 1120 | int n = 5; |
| 1121 | Matcher<int&> m = Ref(n); |
| 1122 | stringstream ss; |
| 1123 | ss << "references the variable @" << &n << " 5"; |
| 1124 | EXPECT_EQ(string(ss.str()), Describe(m)); |
| 1125 | } |
| 1126 | |
| 1127 | // Test that Ref(non_const_varialbe) can be used as a matcher for a |
| 1128 | // const reference. |
| 1129 | TEST(RefTest, CanBeUsedAsMatcherForConstReference) { |
| 1130 | int a = 0; |
| 1131 | int b = 0; |
| 1132 | Matcher<const int&> m = Ref(a); |
| 1133 | EXPECT_TRUE(m.Matches(a)); |
| 1134 | EXPECT_FALSE(m.Matches(b)); |
| 1135 | } |
| 1136 | |
| 1137 | // Tests that Ref(variable) is covariant, i.e. Ref(derived) can be |
| 1138 | // used wherever Ref(base) can be used (Ref(derived) is a sub-type |
| 1139 | // of Ref(base), but not vice versa. |
| 1140 | |
| 1141 | TEST(RefTest, IsCovariant) { |
| 1142 | Base base, base2; |
| 1143 | Derived derived; |
| 1144 | Matcher<const Base&> m1 = Ref(base); |
| 1145 | EXPECT_TRUE(m1.Matches(base)); |
| 1146 | EXPECT_FALSE(m1.Matches(base2)); |
| 1147 | EXPECT_FALSE(m1.Matches(derived)); |
| 1148 | |
| 1149 | m1 = Ref(derived); |
| 1150 | EXPECT_TRUE(m1.Matches(derived)); |
| 1151 | EXPECT_FALSE(m1.Matches(base)); |
| 1152 | EXPECT_FALSE(m1.Matches(base2)); |
| 1153 | } |
| 1154 | |
| 1155 | TEST(RefTest, ExplainsResult) { |
| 1156 | int n = 0; |
| 1157 | EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), n), |
| 1158 | StartsWith("which is located @")); |
| 1159 | |
| 1160 | int m = 0; |
| 1161 | EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), m), |
| 1162 | StartsWith("which is located @")); |
| 1163 | } |
| 1164 | |
| 1165 | // Tests string comparison matchers. |
| 1166 | |
| 1167 | TEST(StrEqTest, MatchesEqualString) { |
| 1168 | Matcher<const char*> m = StrEq(string("Hello")); |
| 1169 | EXPECT_TRUE(m.Matches("Hello")); |
| 1170 | EXPECT_FALSE(m.Matches("hello")); |
| 1171 | EXPECT_FALSE(m.Matches(NULL)); |
| 1172 | |
| 1173 | Matcher<const string&> m2 = StrEq("Hello"); |
| 1174 | EXPECT_TRUE(m2.Matches("Hello")); |
| 1175 | EXPECT_FALSE(m2.Matches("Hi")); |
| 1176 | } |
| 1177 | |
| 1178 | TEST(StrEqTest, CanDescribeSelf) { |
| 1179 | Matcher<string> m = StrEq("Hi-\'\"?\\\a\b\f\n\r\t\v\xD3"); |
| 1180 | EXPECT_EQ("is equal to \"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"", |
| 1181 | Describe(m)); |
| 1182 | |
| 1183 | string str("01204500800"); |
| 1184 | str[3] = '\0'; |
| 1185 | Matcher<string> m2 = StrEq(str); |
| 1186 | EXPECT_EQ("is equal to \"012\\04500800\"", Describe(m2)); |
| 1187 | str[0] = str[6] = str[7] = str[9] = str[10] = '\0'; |
| 1188 | Matcher<string> m3 = StrEq(str); |
| 1189 | EXPECT_EQ("is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3)); |
| 1190 | } |
| 1191 | |
| 1192 | TEST(StrNeTest, MatchesUnequalString) { |
| 1193 | Matcher<const char*> m = StrNe("Hello"); |
| 1194 | EXPECT_TRUE(m.Matches("")); |
| 1195 | EXPECT_TRUE(m.Matches(NULL)); |
| 1196 | EXPECT_FALSE(m.Matches("Hello")); |
| 1197 | |
| 1198 | Matcher<string> m2 = StrNe(string("Hello")); |
| 1199 | EXPECT_TRUE(m2.Matches("hello")); |
| 1200 | EXPECT_FALSE(m2.Matches("Hello")); |
| 1201 | } |
| 1202 | |
| 1203 | TEST(StrNeTest, CanDescribeSelf) { |
| 1204 | Matcher<const char*> m = StrNe("Hi"); |
| 1205 | EXPECT_EQ("isn't equal to \"Hi\"", Describe(m)); |
| 1206 | } |
| 1207 | |
| 1208 | TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) { |
| 1209 | Matcher<const char*> m = StrCaseEq(string("Hello")); |
| 1210 | EXPECT_TRUE(m.Matches("Hello")); |
| 1211 | EXPECT_TRUE(m.Matches("hello")); |
| 1212 | EXPECT_FALSE(m.Matches("Hi")); |
| 1213 | EXPECT_FALSE(m.Matches(NULL)); |
| 1214 | |
| 1215 | Matcher<const string&> m2 = StrCaseEq("Hello"); |
| 1216 | EXPECT_TRUE(m2.Matches("hello")); |
| 1217 | EXPECT_FALSE(m2.Matches("Hi")); |
| 1218 | } |
| 1219 | |
| 1220 | TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) { |
| 1221 | string str1("oabocdooeoo"); |
| 1222 | string str2("OABOCDOOEOO"); |
| 1223 | Matcher<const string&> m0 = StrCaseEq(str1); |
| 1224 | EXPECT_FALSE(m0.Matches(str2 + string(1, '\0'))); |
| 1225 | |
| 1226 | str1[3] = str2[3] = '\0'; |
| 1227 | Matcher<const string&> m1 = StrCaseEq(str1); |
| 1228 | EXPECT_TRUE(m1.Matches(str2)); |
| 1229 | |
| 1230 | str1[0] = str1[6] = str1[7] = str1[10] = '\0'; |
| 1231 | str2[0] = str2[6] = str2[7] = str2[10] = '\0'; |
| 1232 | Matcher<const string&> m2 = StrCaseEq(str1); |
| 1233 | str1[9] = str2[9] = '\0'; |
| 1234 | EXPECT_FALSE(m2.Matches(str2)); |
| 1235 | |
| 1236 | Matcher<const string&> m3 = StrCaseEq(str1); |
| 1237 | EXPECT_TRUE(m3.Matches(str2)); |
| 1238 | |
| 1239 | EXPECT_FALSE(m3.Matches(str2 + "x")); |
| 1240 | str2.append(1, '\0'); |
| 1241 | EXPECT_FALSE(m3.Matches(str2)); |
| 1242 | EXPECT_FALSE(m3.Matches(string(str2, 0, 9))); |
| 1243 | } |
| 1244 | |
| 1245 | TEST(StrCaseEqTest, CanDescribeSelf) { |
| 1246 | Matcher<string> m = StrCaseEq("Hi"); |
| 1247 | EXPECT_EQ("is equal to (ignoring case) \"Hi\"", Describe(m)); |
| 1248 | } |
| 1249 | |
| 1250 | TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) { |
| 1251 | Matcher<const char*> m = StrCaseNe("Hello"); |
| 1252 | EXPECT_TRUE(m.Matches("Hi")); |
| 1253 | EXPECT_TRUE(m.Matches(NULL)); |
| 1254 | EXPECT_FALSE(m.Matches("Hello")); |
| 1255 | EXPECT_FALSE(m.Matches("hello")); |
| 1256 | |
| 1257 | Matcher<string> m2 = StrCaseNe(string("Hello")); |
| 1258 | EXPECT_TRUE(m2.Matches("")); |
| 1259 | EXPECT_FALSE(m2.Matches("Hello")); |
| 1260 | } |
| 1261 | |
| 1262 | TEST(StrCaseNeTest, CanDescribeSelf) { |
| 1263 | Matcher<const char*> m = StrCaseNe("Hi"); |
| 1264 | EXPECT_EQ("isn't equal to (ignoring case) \"Hi\"", Describe(m)); |
| 1265 | } |
| 1266 | |
| 1267 | // Tests that HasSubstr() works for matching string-typed values. |
| 1268 | TEST(HasSubstrTest, WorksForStringClasses) { |
| 1269 | const Matcher<string> m1 = HasSubstr("foo"); |
| 1270 | EXPECT_TRUE(m1.Matches(string("I love food."))); |
| 1271 | EXPECT_FALSE(m1.Matches(string("tofo"))); |
| 1272 | |
| 1273 | const Matcher<const std::string&> m2 = HasSubstr("foo"); |
| 1274 | EXPECT_TRUE(m2.Matches(std::string("I love food."))); |
| 1275 | EXPECT_FALSE(m2.Matches(std::string("tofo"))); |
| 1276 | } |
| 1277 | |
| 1278 | // Tests that HasSubstr() works for matching C-string-typed values. |
| 1279 | TEST(HasSubstrTest, WorksForCStrings) { |
| 1280 | const Matcher<char*> m1 = HasSubstr("foo"); |
| 1281 | EXPECT_TRUE(m1.Matches(const_cast<char*>("I love food."))); |
| 1282 | EXPECT_FALSE(m1.Matches(const_cast<char*>("tofo"))); |
| 1283 | EXPECT_FALSE(m1.Matches(NULL)); |
| 1284 | |
| 1285 | const Matcher<const char*> m2 = HasSubstr("foo"); |
| 1286 | EXPECT_TRUE(m2.Matches("I love food.")); |
| 1287 | EXPECT_FALSE(m2.Matches("tofo")); |
| 1288 | EXPECT_FALSE(m2.Matches(NULL)); |
| 1289 | } |
| 1290 | |
| 1291 | // Tests that HasSubstr(s) describes itself properly. |
| 1292 | TEST(HasSubstrTest, CanDescribeSelf) { |
| 1293 | Matcher<string> m = HasSubstr("foo\n\""); |
| 1294 | EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m)); |
| 1295 | } |
| 1296 | |
| 1297 | TEST(KeyTest, CanDescribeSelf) { |
| 1298 | Matcher<const pair<std::string, int>&> m = Key("foo"); |
| 1299 | EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m)); |
| 1300 | EXPECT_EQ("doesn't have a key that is equal to \"foo\"", DescribeNegation(m)); |
| 1301 | } |
| 1302 | |
| 1303 | TEST(KeyTest, ExplainsResult) { |
| 1304 | Matcher<pair<int, bool> > m = Key(GreaterThan(10)); |
| 1305 | EXPECT_EQ("whose first field is a value which is 5 less than 10", |
| 1306 | Explain(m, make_pair(5, true))); |
| 1307 | EXPECT_EQ("whose first field is a value which is 5 more than 10", |
| 1308 | Explain(m, make_pair(15, true))); |
| 1309 | } |
| 1310 | |
| 1311 | TEST(KeyTest, MatchesCorrectly) { |
| 1312 | pair<int, std::string> p(25, "foo"); |
| 1313 | EXPECT_THAT(p, Key(25)); |
| 1314 | EXPECT_THAT(p, Not(Key(42))); |
| 1315 | EXPECT_THAT(p, Key(Ge(20))); |
| 1316 | EXPECT_THAT(p, Not(Key(Lt(25)))); |
| 1317 | } |
| 1318 | |
| 1319 | TEST(KeyTest, SafelyCastsInnerMatcher) { |
| 1320 | Matcher<int> is_positive = Gt(0); |
| 1321 | Matcher<int> is_negative = Lt(0); |
| 1322 | pair<char, bool> p('a', true); |
| 1323 | EXPECT_THAT(p, Key(is_positive)); |
| 1324 | EXPECT_THAT(p, Not(Key(is_negative))); |
| 1325 | } |
| 1326 | |
| 1327 | TEST(KeyTest, InsideContainsUsingMap) { |
| 1328 | map<int, char> container; |
| 1329 | container.insert(make_pair(1, 'a')); |
| 1330 | container.insert(make_pair(2, 'b')); |
| 1331 | container.insert(make_pair(4, 'c')); |
| 1332 | EXPECT_THAT(container, Contains(Key(1))); |
| 1333 | EXPECT_THAT(container, Not(Contains(Key(3)))); |
| 1334 | } |
| 1335 | |
| 1336 | TEST(KeyTest, InsideContainsUsingMultimap) { |
| 1337 | multimap<int, char> container; |
| 1338 | container.insert(make_pair(1, 'a')); |
| 1339 | container.insert(make_pair(2, 'b')); |
| 1340 | container.insert(make_pair(4, 'c')); |
| 1341 | |
| 1342 | EXPECT_THAT(container, Not(Contains(Key(25)))); |
| 1343 | container.insert(make_pair(25, 'd')); |
| 1344 | EXPECT_THAT(container, Contains(Key(25))); |
| 1345 | container.insert(make_pair(25, 'e')); |
| 1346 | EXPECT_THAT(container, Contains(Key(25))); |
| 1347 | |
| 1348 | EXPECT_THAT(container, Contains(Key(1))); |
| 1349 | EXPECT_THAT(container, Not(Contains(Key(3)))); |
| 1350 | } |
| 1351 | |
| 1352 | TEST(PairTest, Typing) { |
| 1353 | // Test verifies the following type conversions can be compiled. |
| 1354 | Matcher<const pair<const char*, int>&> m1 = Pair("foo", 42); |
| 1355 | Matcher<const pair<const char*, int> > m2 = Pair("foo", 42); |
| 1356 | Matcher<pair<const char*, int> > m3 = Pair("foo", 42); |
| 1357 | |
| 1358 | Matcher<pair<int, const std::string> > m4 = Pair(25, "42"); |
| 1359 | Matcher<pair<const std::string, int> > m5 = Pair("25", 42); |
| 1360 | } |
| 1361 | |
| 1362 | TEST(PairTest, CanDescribeSelf) { |
| 1363 | Matcher<const pair<std::string, int>&> m1 = Pair("foo", 42); |
| 1364 | EXPECT_EQ("has a first field that is equal to \"foo\"" |
| 1365 | ", and has a second field that is equal to 42", |
| 1366 | Describe(m1)); |
| 1367 | EXPECT_EQ("has a first field that isn't equal to \"foo\"" |
| 1368 | ", or has a second field that isn't equal to 42", |
| 1369 | DescribeNegation(m1)); |
| 1370 | // Double and triple negation (1 or 2 times not and description of negation). |
| 1371 | Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42)); |
| 1372 | EXPECT_EQ("has a first field that isn't equal to 13" |
| 1373 | ", and has a second field that is equal to 42", |
| 1374 | DescribeNegation(m2)); |
| 1375 | } |
| 1376 | |
| 1377 | TEST(PairTest, CanExplainMatchResultTo) { |
| 1378 | // If neither field matches, Pair() should explain about the first |
| 1379 | // field. |
| 1380 | const Matcher<pair<int, int> > m = Pair(GreaterThan(0), GreaterThan(0)); |
| 1381 | EXPECT_EQ("whose first field does not match, which is 1 less than 0", |
| 1382 | Explain(m, make_pair(-1, -2))); |
| 1383 | |
| 1384 | // If the first field matches but the second doesn't, Pair() should |
| 1385 | // explain about the second field. |
| 1386 | EXPECT_EQ("whose second field does not match, which is 2 less than 0", |
| 1387 | Explain(m, make_pair(1, -2))); |
| 1388 | |
| 1389 | // If the first field doesn't match but the second does, Pair() |
| 1390 | // should explain about the first field. |
| 1391 | EXPECT_EQ("whose first field does not match, which is 1 less than 0", |
| 1392 | Explain(m, make_pair(-1, 2))); |
| 1393 | |
| 1394 | // If both fields match, Pair() should explain about them both. |
| 1395 | EXPECT_EQ("whose both fields match, where the first field is a value " |
| 1396 | "which is 1 more than 0, and the second field is a value " |
| 1397 | "which is 2 more than 0", |
| 1398 | Explain(m, make_pair(1, 2))); |
| 1399 | |
| 1400 | // If only the first match has an explanation, only this explanation should |
| 1401 | // be printed. |
| 1402 | const Matcher<pair<int, int> > explain_first = Pair(GreaterThan(0), 0); |
| 1403 | EXPECT_EQ("whose both fields match, where the first field is a value " |
| 1404 | "which is 1 more than 0", |
| 1405 | Explain(explain_first, make_pair(1, 0))); |
| 1406 | |
| 1407 | // If only the second match has an explanation, only this explanation should |
| 1408 | // be printed. |
| 1409 | const Matcher<pair<int, int> > explain_second = Pair(0, GreaterThan(0)); |
| 1410 | EXPECT_EQ("whose both fields match, where the second field is a value " |
| 1411 | "which is 1 more than 0", |
| 1412 | Explain(explain_second, make_pair(0, 1))); |
| 1413 | } |
| 1414 | |
| 1415 | TEST(PairTest, MatchesCorrectly) { |
| 1416 | pair<int, std::string> p(25, "foo"); |
| 1417 | |
| 1418 | // Both fields match. |
| 1419 | EXPECT_THAT(p, Pair(25, "foo")); |
| 1420 | EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o"))); |
| 1421 | |
| 1422 | // 'first' doesnt' match, but 'second' matches. |
| 1423 | EXPECT_THAT(p, Not(Pair(42, "foo"))); |
| 1424 | EXPECT_THAT(p, Not(Pair(Lt(25), "foo"))); |
| 1425 | |
| 1426 | // 'first' matches, but 'second' doesn't match. |
| 1427 | EXPECT_THAT(p, Not(Pair(25, "bar"))); |
| 1428 | EXPECT_THAT(p, Not(Pair(25, Not("foo")))); |
| 1429 | |
| 1430 | // Neither field matches. |
| 1431 | EXPECT_THAT(p, Not(Pair(13, "bar"))); |
| 1432 | EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a")))); |
| 1433 | } |
| 1434 | |
| 1435 | TEST(PairTest, SafelyCastsInnerMatchers) { |
| 1436 | Matcher<int> is_positive = Gt(0); |
| 1437 | Matcher<int> is_negative = Lt(0); |
| 1438 | pair<char, bool> p('a', true); |
| 1439 | EXPECT_THAT(p, Pair(is_positive, _)); |
| 1440 | EXPECT_THAT(p, Not(Pair(is_negative, _))); |
| 1441 | EXPECT_THAT(p, Pair(_, is_positive)); |
| 1442 | EXPECT_THAT(p, Not(Pair(_, is_negative))); |
| 1443 | } |
| 1444 | |
| 1445 | TEST(PairTest, InsideContainsUsingMap) { |
| 1446 | map<int, char> container; |
| 1447 | container.insert(make_pair(1, 'a')); |
| 1448 | container.insert(make_pair(2, 'b')); |
| 1449 | container.insert(make_pair(4, 'c')); |
| 1450 | EXPECT_THAT(container, Contains(Pair(1, 'a'))); |
| 1451 | EXPECT_THAT(container, Contains(Pair(1, _))); |
| 1452 | EXPECT_THAT(container, Contains(Pair(_, 'a'))); |
| 1453 | EXPECT_THAT(container, Not(Contains(Pair(3, _)))); |
| 1454 | } |
| 1455 | |
| 1456 | // Tests StartsWith(s). |
| 1457 | |
| 1458 | TEST(StartsWithTest, MatchesStringWithGivenPrefix) { |
| 1459 | const Matcher<const char*> m1 = StartsWith(string("")); |
| 1460 | EXPECT_TRUE(m1.Matches("Hi")); |
| 1461 | EXPECT_TRUE(m1.Matches("")); |
| 1462 | EXPECT_FALSE(m1.Matches(NULL)); |
| 1463 | |
| 1464 | const Matcher<const string&> m2 = StartsWith("Hi"); |
| 1465 | EXPECT_TRUE(m2.Matches("Hi")); |
| 1466 | EXPECT_TRUE(m2.Matches("Hi Hi!")); |
| 1467 | EXPECT_TRUE(m2.Matches("High")); |
| 1468 | EXPECT_FALSE(m2.Matches("H")); |
| 1469 | EXPECT_FALSE(m2.Matches(" Hi")); |
| 1470 | } |
| 1471 | |
| 1472 | TEST(StartsWithTest, CanDescribeSelf) { |
| 1473 | Matcher<const std::string> m = StartsWith("Hi"); |
| 1474 | EXPECT_EQ("starts with \"Hi\"", Describe(m)); |
| 1475 | } |
| 1476 | |
| 1477 | // Tests EndsWith(s). |
| 1478 | |
| 1479 | TEST(EndsWithTest, MatchesStringWithGivenSuffix) { |
| 1480 | const Matcher<const char*> m1 = EndsWith(""); |
| 1481 | EXPECT_TRUE(m1.Matches("Hi")); |
| 1482 | EXPECT_TRUE(m1.Matches("")); |
| 1483 | EXPECT_FALSE(m1.Matches(NULL)); |
| 1484 | |
| 1485 | const Matcher<const string&> m2 = EndsWith(string("Hi")); |
| 1486 | EXPECT_TRUE(m2.Matches("Hi")); |
| 1487 | EXPECT_TRUE(m2.Matches("Wow Hi Hi")); |
| 1488 | EXPECT_TRUE(m2.Matches("Super Hi")); |
| 1489 | EXPECT_FALSE(m2.Matches("i")); |
| 1490 | EXPECT_FALSE(m2.Matches("Hi ")); |
| 1491 | } |
| 1492 | |
| 1493 | TEST(EndsWithTest, CanDescribeSelf) { |
| 1494 | Matcher<const std::string> m = EndsWith("Hi"); |
| 1495 | EXPECT_EQ("ends with \"Hi\"", Describe(m)); |
| 1496 | } |
| 1497 | |
| 1498 | // Tests MatchesRegex(). |
| 1499 | |
| 1500 | TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) { |
| 1501 | const Matcher<const char*> m1 = MatchesRegex("a.*z"); |
| 1502 | EXPECT_TRUE(m1.Matches("az")); |
| 1503 | EXPECT_TRUE(m1.Matches("abcz")); |
| 1504 | EXPECT_FALSE(m1.Matches(NULL)); |
| 1505 | |
| 1506 | const Matcher<const string&> m2 = MatchesRegex(new RE("a.*z")); |
| 1507 | EXPECT_TRUE(m2.Matches("azbz")); |
| 1508 | EXPECT_FALSE(m2.Matches("az1")); |
| 1509 | EXPECT_FALSE(m2.Matches("1az")); |
| 1510 | } |
| 1511 | |
| 1512 | TEST(MatchesRegexTest, CanDescribeSelf) { |
| 1513 | Matcher<const std::string> m1 = MatchesRegex(string("Hi.*")); |
| 1514 | EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1)); |
| 1515 | |
| 1516 | Matcher<const char*> m2 = MatchesRegex(new RE("a.*")); |
| 1517 | EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2)); |
| 1518 | } |
| 1519 | |
| 1520 | // Tests ContainsRegex(). |
| 1521 | |
| 1522 | TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) { |
| 1523 | const Matcher<const char*> m1 = ContainsRegex(string("a.*z")); |
| 1524 | EXPECT_TRUE(m1.Matches("az")); |
| 1525 | EXPECT_TRUE(m1.Matches("0abcz1")); |
| 1526 | EXPECT_FALSE(m1.Matches(NULL)); |
| 1527 | |
| 1528 | const Matcher<const string&> m2 = ContainsRegex(new RE("a.*z")); |
| 1529 | EXPECT_TRUE(m2.Matches("azbz")); |
| 1530 | EXPECT_TRUE(m2.Matches("az1")); |
| 1531 | EXPECT_FALSE(m2.Matches("1a")); |
| 1532 | } |
| 1533 | |
| 1534 | TEST(ContainsRegexTest, CanDescribeSelf) { |
| 1535 | Matcher<const std::string> m1 = ContainsRegex("Hi.*"); |
| 1536 | EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1)); |
| 1537 | |
| 1538 | Matcher<const char*> m2 = ContainsRegex(new RE("a.*")); |
| 1539 | EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2)); |
| 1540 | } |
| 1541 | |
| 1542 | // Tests for wide strings. |
| 1543 | #if GTEST_HAS_STD_WSTRING |
| 1544 | TEST(StdWideStrEqTest, MatchesEqual) { |
| 1545 | Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello")); |
| 1546 | EXPECT_TRUE(m.Matches(L"Hello")); |
| 1547 | EXPECT_FALSE(m.Matches(L"hello")); |
| 1548 | EXPECT_FALSE(m.Matches(NULL)); |
| 1549 | |
| 1550 | Matcher<const ::std::wstring&> m2 = StrEq(L"Hello"); |
| 1551 | EXPECT_TRUE(m2.Matches(L"Hello")); |
| 1552 | EXPECT_FALSE(m2.Matches(L"Hi")); |
| 1553 | |
| 1554 | Matcher<const ::std::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D"); |
| 1555 | EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D")); |
| 1556 | EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E")); |
| 1557 | |
| 1558 | ::std::wstring str(L"01204500800"); |
| 1559 | str[3] = L'\0'; |
| 1560 | Matcher<const ::std::wstring&> m4 = StrEq(str); |
| 1561 | EXPECT_TRUE(m4.Matches(str)); |
| 1562 | str[0] = str[6] = str[7] = str[9] = str[10] = L'\0'; |
| 1563 | Matcher<const ::std::wstring&> m5 = StrEq(str); |
| 1564 | EXPECT_TRUE(m5.Matches(str)); |
| 1565 | } |
| 1566 | |
| 1567 | TEST(StdWideStrEqTest, CanDescribeSelf) { |
| 1568 | Matcher< ::std::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v"); |
| 1569 | EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"", |
| 1570 | Describe(m)); |
| 1571 | |
| 1572 | Matcher< ::std::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D"); |
| 1573 | EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"", |
| 1574 | Describe(m2)); |
| 1575 | |
| 1576 | ::std::wstring str(L"01204500800"); |
| 1577 | str[3] = L'\0'; |
| 1578 | Matcher<const ::std::wstring&> m4 = StrEq(str); |
| 1579 | EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4)); |
| 1580 | str[0] = str[6] = str[7] = str[9] = str[10] = L'\0'; |
| 1581 | Matcher<const ::std::wstring&> m5 = StrEq(str); |
| 1582 | EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5)); |
| 1583 | } |
| 1584 | |
| 1585 | TEST(StdWideStrNeTest, MatchesUnequalString) { |
| 1586 | Matcher<const wchar_t*> m = StrNe(L"Hello"); |
| 1587 | EXPECT_TRUE(m.Matches(L"")); |
| 1588 | EXPECT_TRUE(m.Matches(NULL)); |
| 1589 | EXPECT_FALSE(m.Matches(L"Hello")); |
| 1590 | |
| 1591 | Matcher< ::std::wstring> m2 = StrNe(::std::wstring(L"Hello")); |
| 1592 | EXPECT_TRUE(m2.Matches(L"hello")); |
| 1593 | EXPECT_FALSE(m2.Matches(L"Hello")); |
| 1594 | } |
| 1595 | |
| 1596 | TEST(StdWideStrNeTest, CanDescribeSelf) { |
| 1597 | Matcher<const wchar_t*> m = StrNe(L"Hi"); |
| 1598 | EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m)); |
| 1599 | } |
| 1600 | |
| 1601 | TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) { |
| 1602 | Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello")); |
| 1603 | EXPECT_TRUE(m.Matches(L"Hello")); |
| 1604 | EXPECT_TRUE(m.Matches(L"hello")); |
| 1605 | EXPECT_FALSE(m.Matches(L"Hi")); |
| 1606 | EXPECT_FALSE(m.Matches(NULL)); |
| 1607 | |
| 1608 | Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello"); |
| 1609 | EXPECT_TRUE(m2.Matches(L"hello")); |
| 1610 | EXPECT_FALSE(m2.Matches(L"Hi")); |
| 1611 | } |
| 1612 | |
| 1613 | TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) { |
| 1614 | ::std::wstring str1(L"oabocdooeoo"); |
| 1615 | ::std::wstring str2(L"OABOCDOOEOO"); |
| 1616 | Matcher<const ::std::wstring&> m0 = StrCaseEq(str1); |
| 1617 | EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L'\0'))); |
| 1618 | |
| 1619 | str1[3] = str2[3] = L'\0'; |
| 1620 | Matcher<const ::std::wstring&> m1 = StrCaseEq(str1); |
| 1621 | EXPECT_TRUE(m1.Matches(str2)); |
| 1622 | |
| 1623 | str1[0] = str1[6] = str1[7] = str1[10] = L'\0'; |
| 1624 | str2[0] = str2[6] = str2[7] = str2[10] = L'\0'; |
| 1625 | Matcher<const ::std::wstring&> m2 = StrCaseEq(str1); |
| 1626 | str1[9] = str2[9] = L'\0'; |
| 1627 | EXPECT_FALSE(m2.Matches(str2)); |
| 1628 | |
| 1629 | Matcher<const ::std::wstring&> m3 = StrCaseEq(str1); |
| 1630 | EXPECT_TRUE(m3.Matches(str2)); |
| 1631 | |
| 1632 | EXPECT_FALSE(m3.Matches(str2 + L"x")); |
| 1633 | str2.append(1, L'\0'); |
| 1634 | EXPECT_FALSE(m3.Matches(str2)); |
| 1635 | EXPECT_FALSE(m3.Matches(::std::wstring(str2, 0, 9))); |
| 1636 | } |
| 1637 | |
| 1638 | TEST(StdWideStrCaseEqTest, CanDescribeSelf) { |
| 1639 | Matcher< ::std::wstring> m = StrCaseEq(L"Hi"); |
| 1640 | EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m)); |
| 1641 | } |
| 1642 | |
| 1643 | TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) { |
| 1644 | Matcher<const wchar_t*> m = StrCaseNe(L"Hello"); |
| 1645 | EXPECT_TRUE(m.Matches(L"Hi")); |
| 1646 | EXPECT_TRUE(m.Matches(NULL)); |
| 1647 | EXPECT_FALSE(m.Matches(L"Hello")); |
| 1648 | EXPECT_FALSE(m.Matches(L"hello")); |
| 1649 | |
| 1650 | Matcher< ::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello")); |
| 1651 | EXPECT_TRUE(m2.Matches(L"")); |
| 1652 | EXPECT_FALSE(m2.Matches(L"Hello")); |
| 1653 | } |
| 1654 | |
| 1655 | TEST(StdWideStrCaseNeTest, CanDescribeSelf) { |
| 1656 | Matcher<const wchar_t*> m = StrCaseNe(L"Hi"); |
| 1657 | EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m)); |
| 1658 | } |
| 1659 | |
| 1660 | // Tests that HasSubstr() works for matching wstring-typed values. |
| 1661 | TEST(StdWideHasSubstrTest, WorksForStringClasses) { |
| 1662 | const Matcher< ::std::wstring> m1 = HasSubstr(L"foo"); |
| 1663 | EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food."))); |
| 1664 | EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo"))); |
| 1665 | |
| 1666 | const Matcher<const ::std::wstring&> m2 = HasSubstr(L"foo"); |
| 1667 | EXPECT_TRUE(m2.Matches(::std::wstring(L"I love food."))); |
| 1668 | EXPECT_FALSE(m2.Matches(::std::wstring(L"tofo"))); |
| 1669 | } |
| 1670 | |
| 1671 | // Tests that HasSubstr() works for matching C-wide-string-typed values. |
| 1672 | TEST(StdWideHasSubstrTest, WorksForCStrings) { |
| 1673 | const Matcher<wchar_t*> m1 = HasSubstr(L"foo"); |
| 1674 | EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food."))); |
| 1675 | EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo"))); |
| 1676 | EXPECT_FALSE(m1.Matches(NULL)); |
| 1677 | |
| 1678 | const Matcher<const wchar_t*> m2 = HasSubstr(L"foo"); |
| 1679 | EXPECT_TRUE(m2.Matches(L"I love food.")); |
| 1680 | EXPECT_FALSE(m2.Matches(L"tofo")); |
| 1681 | EXPECT_FALSE(m2.Matches(NULL)); |
| 1682 | } |
| 1683 | |
| 1684 | // Tests that HasSubstr(s) describes itself properly. |
| 1685 | TEST(StdWideHasSubstrTest, CanDescribeSelf) { |
| 1686 | Matcher< ::std::wstring> m = HasSubstr(L"foo\n\""); |
| 1687 | EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m)); |
| 1688 | } |
| 1689 | |
| 1690 | // Tests StartsWith(s). |
| 1691 | |
| 1692 | TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) { |
| 1693 | const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(L"")); |
| 1694 | EXPECT_TRUE(m1.Matches(L"Hi")); |
| 1695 | EXPECT_TRUE(m1.Matches(L"")); |
| 1696 | EXPECT_FALSE(m1.Matches(NULL)); |
| 1697 | |
| 1698 | const Matcher<const ::std::wstring&> m2 = StartsWith(L"Hi"); |
| 1699 | EXPECT_TRUE(m2.Matches(L"Hi")); |
| 1700 | EXPECT_TRUE(m2.Matches(L"Hi Hi!")); |
| 1701 | EXPECT_TRUE(m2.Matches(L"High")); |
| 1702 | EXPECT_FALSE(m2.Matches(L"H")); |
| 1703 | EXPECT_FALSE(m2.Matches(L" Hi")); |
| 1704 | } |
| 1705 | |
| 1706 | TEST(StdWideStartsWithTest, CanDescribeSelf) { |
| 1707 | Matcher<const ::std::wstring> m = StartsWith(L"Hi"); |
| 1708 | EXPECT_EQ("starts with L\"Hi\"", Describe(m)); |
| 1709 | } |
| 1710 | |
| 1711 | // Tests EndsWith(s). |
| 1712 | |
| 1713 | TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) { |
| 1714 | const Matcher<const wchar_t*> m1 = EndsWith(L""); |
| 1715 | EXPECT_TRUE(m1.Matches(L"Hi")); |
| 1716 | EXPECT_TRUE(m1.Matches(L"")); |
| 1717 | EXPECT_FALSE(m1.Matches(NULL)); |
| 1718 | |
| 1719 | const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L"Hi")); |
| 1720 | EXPECT_TRUE(m2.Matches(L"Hi")); |
| 1721 | EXPECT_TRUE(m2.Matches(L"Wow Hi Hi")); |
| 1722 | EXPECT_TRUE(m2.Matches(L"Super Hi")); |
| 1723 | EXPECT_FALSE(m2.Matches(L"i")); |
| 1724 | EXPECT_FALSE(m2.Matches(L"Hi ")); |
| 1725 | } |
| 1726 | |
| 1727 | TEST(StdWideEndsWithTest, CanDescribeSelf) { |
| 1728 | Matcher<const ::std::wstring> m = EndsWith(L"Hi"); |
| 1729 | EXPECT_EQ("ends with L\"Hi\"", Describe(m)); |
| 1730 | } |
| 1731 | |
| 1732 | #endif // GTEST_HAS_STD_WSTRING |
| 1733 | |
| 1734 | #if GTEST_HAS_GLOBAL_WSTRING |
| 1735 | TEST(GlobalWideStrEqTest, MatchesEqual) { |
| 1736 | Matcher<const wchar_t*> m = StrEq(::wstring(L"Hello")); |
| 1737 | EXPECT_TRUE(m.Matches(L"Hello")); |
| 1738 | EXPECT_FALSE(m.Matches(L"hello")); |
| 1739 | EXPECT_FALSE(m.Matches(NULL)); |
| 1740 | |
| 1741 | Matcher<const ::wstring&> m2 = StrEq(L"Hello"); |
| 1742 | EXPECT_TRUE(m2.Matches(L"Hello")); |
| 1743 | EXPECT_FALSE(m2.Matches(L"Hi")); |
| 1744 | |
| 1745 | Matcher<const ::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D"); |
| 1746 | EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D")); |
| 1747 | EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E")); |
| 1748 | |
| 1749 | ::wstring str(L"01204500800"); |
| 1750 | str[3] = L'\0'; |
| 1751 | Matcher<const ::wstring&> m4 = StrEq(str); |
| 1752 | EXPECT_TRUE(m4.Matches(str)); |
| 1753 | str[0] = str[6] = str[7] = str[9] = str[10] = L'\0'; |
| 1754 | Matcher<const ::wstring&> m5 = StrEq(str); |
| 1755 | EXPECT_TRUE(m5.Matches(str)); |
| 1756 | } |
| 1757 | |
| 1758 | TEST(GlobalWideStrEqTest, CanDescribeSelf) { |
| 1759 | Matcher< ::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v"); |
| 1760 | EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"", |
| 1761 | Describe(m)); |
| 1762 | |
| 1763 | Matcher< ::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D"); |
| 1764 | EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"", |
| 1765 | Describe(m2)); |
| 1766 | |
| 1767 | ::wstring str(L"01204500800"); |
| 1768 | str[3] = L'\0'; |
| 1769 | Matcher<const ::wstring&> m4 = StrEq(str); |
| 1770 | EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4)); |
| 1771 | str[0] = str[6] = str[7] = str[9] = str[10] = L'\0'; |
| 1772 | Matcher<const ::wstring&> m5 = StrEq(str); |
| 1773 | EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5)); |
| 1774 | } |
| 1775 | |
| 1776 | TEST(GlobalWideStrNeTest, MatchesUnequalString) { |
| 1777 | Matcher<const wchar_t*> m = StrNe(L"Hello"); |
| 1778 | EXPECT_TRUE(m.Matches(L"")); |
| 1779 | EXPECT_TRUE(m.Matches(NULL)); |
| 1780 | EXPECT_FALSE(m.Matches(L"Hello")); |
| 1781 | |
| 1782 | Matcher< ::wstring> m2 = StrNe(::wstring(L"Hello")); |
| 1783 | EXPECT_TRUE(m2.Matches(L"hello")); |
| 1784 | EXPECT_FALSE(m2.Matches(L"Hello")); |
| 1785 | } |
| 1786 | |
| 1787 | TEST(GlobalWideStrNeTest, CanDescribeSelf) { |
| 1788 | Matcher<const wchar_t*> m = StrNe(L"Hi"); |
| 1789 | EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m)); |
| 1790 | } |
| 1791 | |
| 1792 | TEST(GlobalWideStrCaseEqTest, MatchesEqualStringIgnoringCase) { |
| 1793 | Matcher<const wchar_t*> m = StrCaseEq(::wstring(L"Hello")); |
| 1794 | EXPECT_TRUE(m.Matches(L"Hello")); |
| 1795 | EXPECT_TRUE(m.Matches(L"hello")); |
| 1796 | EXPECT_FALSE(m.Matches(L"Hi")); |
| 1797 | EXPECT_FALSE(m.Matches(NULL)); |
| 1798 | |
| 1799 | Matcher<const ::wstring&> m2 = StrCaseEq(L"Hello"); |
| 1800 | EXPECT_TRUE(m2.Matches(L"hello")); |
| 1801 | EXPECT_FALSE(m2.Matches(L"Hi")); |
| 1802 | } |
| 1803 | |
| 1804 | TEST(GlobalWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) { |
| 1805 | ::wstring str1(L"oabocdooeoo"); |
| 1806 | ::wstring str2(L"OABOCDOOEOO"); |
| 1807 | Matcher<const ::wstring&> m0 = StrCaseEq(str1); |
| 1808 | EXPECT_FALSE(m0.Matches(str2 + ::wstring(1, L'\0'))); |
| 1809 | |
| 1810 | str1[3] = str2[3] = L'\0'; |
| 1811 | Matcher<const ::wstring&> m1 = StrCaseEq(str1); |
| 1812 | EXPECT_TRUE(m1.Matches(str2)); |
| 1813 | |
| 1814 | str1[0] = str1[6] = str1[7] = str1[10] = L'\0'; |
| 1815 | str2[0] = str2[6] = str2[7] = str2[10] = L'\0'; |
| 1816 | Matcher<const ::wstring&> m2 = StrCaseEq(str1); |
| 1817 | str1[9] = str2[9] = L'\0'; |
| 1818 | EXPECT_FALSE(m2.Matches(str2)); |
| 1819 | |
| 1820 | Matcher<const ::wstring&> m3 = StrCaseEq(str1); |
| 1821 | EXPECT_TRUE(m3.Matches(str2)); |
| 1822 | |
| 1823 | EXPECT_FALSE(m3.Matches(str2 + L"x")); |
| 1824 | str2.append(1, L'\0'); |
| 1825 | EXPECT_FALSE(m3.Matches(str2)); |
| 1826 | EXPECT_FALSE(m3.Matches(::wstring(str2, 0, 9))); |
| 1827 | } |
| 1828 | |
| 1829 | TEST(GlobalWideStrCaseEqTest, CanDescribeSelf) { |
| 1830 | Matcher< ::wstring> m = StrCaseEq(L"Hi"); |
| 1831 | EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m)); |
| 1832 | } |
| 1833 | |
| 1834 | TEST(GlobalWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) { |
| 1835 | Matcher<const wchar_t*> m = StrCaseNe(L"Hello"); |
| 1836 | EXPECT_TRUE(m.Matches(L"Hi")); |
| 1837 | EXPECT_TRUE(m.Matches(NULL)); |
| 1838 | EXPECT_FALSE(m.Matches(L"Hello")); |
| 1839 | EXPECT_FALSE(m.Matches(L"hello")); |
| 1840 | |
| 1841 | Matcher< ::wstring> m2 = StrCaseNe(::wstring(L"Hello")); |
| 1842 | EXPECT_TRUE(m2.Matches(L"")); |
| 1843 | EXPECT_FALSE(m2.Matches(L"Hello")); |
| 1844 | } |
| 1845 | |
| 1846 | TEST(GlobalWideStrCaseNeTest, CanDescribeSelf) { |
| 1847 | Matcher<const wchar_t*> m = StrCaseNe(L"Hi"); |
| 1848 | EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m)); |
| 1849 | } |
| 1850 | |
| 1851 | // Tests that HasSubstr() works for matching wstring-typed values. |
| 1852 | TEST(GlobalWideHasSubstrTest, WorksForStringClasses) { |
| 1853 | const Matcher< ::wstring> m1 = HasSubstr(L"foo"); |
| 1854 | EXPECT_TRUE(m1.Matches(::wstring(L"I love food."))); |
| 1855 | EXPECT_FALSE(m1.Matches(::wstring(L"tofo"))); |
| 1856 | |
| 1857 | const Matcher<const ::wstring&> m2 = HasSubstr(L"foo"); |
| 1858 | EXPECT_TRUE(m2.Matches(::wstring(L"I love food."))); |
| 1859 | EXPECT_FALSE(m2.Matches(::wstring(L"tofo"))); |
| 1860 | } |
| 1861 | |
| 1862 | // Tests that HasSubstr() works for matching C-wide-string-typed values. |
| 1863 | TEST(GlobalWideHasSubstrTest, WorksForCStrings) { |
| 1864 | const Matcher<wchar_t*> m1 = HasSubstr(L"foo"); |
| 1865 | EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food."))); |
| 1866 | EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo"))); |
| 1867 | EXPECT_FALSE(m1.Matches(NULL)); |
| 1868 | |
| 1869 | const Matcher<const wchar_t*> m2 = HasSubstr(L"foo"); |
| 1870 | EXPECT_TRUE(m2.Matches(L"I love food.")); |
| 1871 | EXPECT_FALSE(m2.Matches(L"tofo")); |
| 1872 | EXPECT_FALSE(m2.Matches(NULL)); |
| 1873 | } |
| 1874 | |
| 1875 | // Tests that HasSubstr(s) describes itself properly. |
| 1876 | TEST(GlobalWideHasSubstrTest, CanDescribeSelf) { |
| 1877 | Matcher< ::wstring> m = HasSubstr(L"foo\n\""); |
| 1878 | EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m)); |
| 1879 | } |
| 1880 | |
| 1881 | // Tests StartsWith(s). |
| 1882 | |
| 1883 | TEST(GlobalWideStartsWithTest, MatchesStringWithGivenPrefix) { |
| 1884 | const Matcher<const wchar_t*> m1 = StartsWith(::wstring(L"")); |
| 1885 | EXPECT_TRUE(m1.Matches(L"Hi")); |
| 1886 | EXPECT_TRUE(m1.Matches(L"")); |
| 1887 | EXPECT_FALSE(m1.Matches(NULL)); |
| 1888 | |
| 1889 | const Matcher<const ::wstring&> m2 = StartsWith(L"Hi"); |
| 1890 | EXPECT_TRUE(m2.Matches(L"Hi")); |
| 1891 | EXPECT_TRUE(m2.Matches(L"Hi Hi!")); |
| 1892 | EXPECT_TRUE(m2.Matches(L"High")); |
| 1893 | EXPECT_FALSE(m2.Matches(L"H")); |
| 1894 | EXPECT_FALSE(m2.Matches(L" Hi")); |
| 1895 | } |
| 1896 | |
| 1897 | TEST(GlobalWideStartsWithTest, CanDescribeSelf) { |
| 1898 | Matcher<const ::wstring> m = StartsWith(L"Hi"); |
| 1899 | EXPECT_EQ("starts with L\"Hi\"", Describe(m)); |
| 1900 | } |
| 1901 | |
| 1902 | // Tests EndsWith(s). |
| 1903 | |
| 1904 | TEST(GlobalWideEndsWithTest, MatchesStringWithGivenSuffix) { |
| 1905 | const Matcher<const wchar_t*> m1 = EndsWith(L""); |
| 1906 | EXPECT_TRUE(m1.Matches(L"Hi")); |
| 1907 | EXPECT_TRUE(m1.Matches(L"")); |
| 1908 | EXPECT_FALSE(m1.Matches(NULL)); |
| 1909 | |
| 1910 | const Matcher<const ::wstring&> m2 = EndsWith(::wstring(L"Hi")); |
| 1911 | EXPECT_TRUE(m2.Matches(L"Hi")); |
| 1912 | EXPECT_TRUE(m2.Matches(L"Wow Hi Hi")); |
| 1913 | EXPECT_TRUE(m2.Matches(L"Super Hi")); |
| 1914 | EXPECT_FALSE(m2.Matches(L"i")); |
| 1915 | EXPECT_FALSE(m2.Matches(L"Hi ")); |
| 1916 | } |
| 1917 | |
| 1918 | TEST(GlobalWideEndsWithTest, CanDescribeSelf) { |
| 1919 | Matcher<const ::wstring> m = EndsWith(L"Hi"); |
| 1920 | EXPECT_EQ("ends with L\"Hi\"", Describe(m)); |
| 1921 | } |
| 1922 | |
| 1923 | #endif // GTEST_HAS_GLOBAL_WSTRING |
| 1924 | |
| 1925 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 1926 | typedef ::testing::tuple<long, int> Tuple2; // NOLINT |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 1927 | |
| 1928 | // Tests that Eq() matches a 2-tuple where the first field == the |
| 1929 | // second field. |
| 1930 | TEST(Eq2Test, MatchesEqualArguments) { |
| 1931 | Matcher<const Tuple2&> m = Eq(); |
| 1932 | EXPECT_TRUE(m.Matches(Tuple2(5L, 5))); |
| 1933 | EXPECT_FALSE(m.Matches(Tuple2(5L, 6))); |
| 1934 | } |
| 1935 | |
| 1936 | // Tests that Eq() describes itself properly. |
| 1937 | TEST(Eq2Test, CanDescribeSelf) { |
| 1938 | Matcher<const Tuple2&> m = Eq(); |
| 1939 | EXPECT_EQ("are an equal pair", Describe(m)); |
| 1940 | } |
| 1941 | |
| 1942 | // Tests that Ge() matches a 2-tuple where the first field >= the |
| 1943 | // second field. |
| 1944 | TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) { |
| 1945 | Matcher<const Tuple2&> m = Ge(); |
| 1946 | EXPECT_TRUE(m.Matches(Tuple2(5L, 4))); |
| 1947 | EXPECT_TRUE(m.Matches(Tuple2(5L, 5))); |
| 1948 | EXPECT_FALSE(m.Matches(Tuple2(5L, 6))); |
| 1949 | } |
| 1950 | |
| 1951 | // Tests that Ge() describes itself properly. |
| 1952 | TEST(Ge2Test, CanDescribeSelf) { |
| 1953 | Matcher<const Tuple2&> m = Ge(); |
| 1954 | EXPECT_EQ("are a pair where the first >= the second", Describe(m)); |
| 1955 | } |
| 1956 | |
| 1957 | // Tests that Gt() matches a 2-tuple where the first field > the |
| 1958 | // second field. |
| 1959 | TEST(Gt2Test, MatchesGreaterThanArguments) { |
| 1960 | Matcher<const Tuple2&> m = Gt(); |
| 1961 | EXPECT_TRUE(m.Matches(Tuple2(5L, 4))); |
| 1962 | EXPECT_FALSE(m.Matches(Tuple2(5L, 5))); |
| 1963 | EXPECT_FALSE(m.Matches(Tuple2(5L, 6))); |
| 1964 | } |
| 1965 | |
| 1966 | // Tests that Gt() describes itself properly. |
| 1967 | TEST(Gt2Test, CanDescribeSelf) { |
| 1968 | Matcher<const Tuple2&> m = Gt(); |
| 1969 | EXPECT_EQ("are a pair where the first > the second", Describe(m)); |
| 1970 | } |
| 1971 | |
| 1972 | // Tests that Le() matches a 2-tuple where the first field <= the |
| 1973 | // second field. |
| 1974 | TEST(Le2Test, MatchesLessThanOrEqualArguments) { |
| 1975 | Matcher<const Tuple2&> m = Le(); |
| 1976 | EXPECT_TRUE(m.Matches(Tuple2(5L, 6))); |
| 1977 | EXPECT_TRUE(m.Matches(Tuple2(5L, 5))); |
| 1978 | EXPECT_FALSE(m.Matches(Tuple2(5L, 4))); |
| 1979 | } |
| 1980 | |
| 1981 | // Tests that Le() describes itself properly. |
| 1982 | TEST(Le2Test, CanDescribeSelf) { |
| 1983 | Matcher<const Tuple2&> m = Le(); |
| 1984 | EXPECT_EQ("are a pair where the first <= the second", Describe(m)); |
| 1985 | } |
| 1986 | |
| 1987 | // Tests that Lt() matches a 2-tuple where the first field < the |
| 1988 | // second field. |
| 1989 | TEST(Lt2Test, MatchesLessThanArguments) { |
| 1990 | Matcher<const Tuple2&> m = Lt(); |
| 1991 | EXPECT_TRUE(m.Matches(Tuple2(5L, 6))); |
| 1992 | EXPECT_FALSE(m.Matches(Tuple2(5L, 5))); |
| 1993 | EXPECT_FALSE(m.Matches(Tuple2(5L, 4))); |
| 1994 | } |
| 1995 | |
| 1996 | // Tests that Lt() describes itself properly. |
| 1997 | TEST(Lt2Test, CanDescribeSelf) { |
| 1998 | Matcher<const Tuple2&> m = Lt(); |
| 1999 | EXPECT_EQ("are a pair where the first < the second", Describe(m)); |
| 2000 | } |
| 2001 | |
| 2002 | // Tests that Ne() matches a 2-tuple where the first field != the |
| 2003 | // second field. |
| 2004 | TEST(Ne2Test, MatchesUnequalArguments) { |
| 2005 | Matcher<const Tuple2&> m = Ne(); |
| 2006 | EXPECT_TRUE(m.Matches(Tuple2(5L, 6))); |
| 2007 | EXPECT_TRUE(m.Matches(Tuple2(5L, 4))); |
| 2008 | EXPECT_FALSE(m.Matches(Tuple2(5L, 5))); |
| 2009 | } |
| 2010 | |
| 2011 | // Tests that Ne() describes itself properly. |
| 2012 | TEST(Ne2Test, CanDescribeSelf) { |
| 2013 | Matcher<const Tuple2&> m = Ne(); |
| 2014 | EXPECT_EQ("are an unequal pair", Describe(m)); |
| 2015 | } |
| 2016 | |
| 2017 | // Tests that Not(m) matches any value that doesn't match m. |
| 2018 | TEST(NotTest, NegatesMatcher) { |
| 2019 | Matcher<int> m; |
| 2020 | m = Not(Eq(2)); |
| 2021 | EXPECT_TRUE(m.Matches(3)); |
| 2022 | EXPECT_FALSE(m.Matches(2)); |
| 2023 | } |
| 2024 | |
| 2025 | // Tests that Not(m) describes itself properly. |
| 2026 | TEST(NotTest, CanDescribeSelf) { |
| 2027 | Matcher<int> m = Not(Eq(5)); |
| 2028 | EXPECT_EQ("isn't equal to 5", Describe(m)); |
| 2029 | } |
| 2030 | |
| 2031 | // Tests that monomorphic matchers are safely cast by the Not matcher. |
| 2032 | TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) { |
| 2033 | // greater_than_5 is a monomorphic matcher. |
| 2034 | Matcher<int> greater_than_5 = Gt(5); |
| 2035 | |
| 2036 | Matcher<const int&> m = Not(greater_than_5); |
| 2037 | Matcher<int&> m2 = Not(greater_than_5); |
| 2038 | Matcher<int&> m3 = Not(m); |
| 2039 | } |
| 2040 | |
| 2041 | // Helper to allow easy testing of AllOf matchers with num parameters. |
| 2042 | void AllOfMatches(int num, const Matcher<int>& m) { |
| 2043 | SCOPED_TRACE(Describe(m)); |
| 2044 | EXPECT_TRUE(m.Matches(0)); |
| 2045 | for (int i = 1; i <= num; ++i) { |
| 2046 | EXPECT_FALSE(m.Matches(i)); |
| 2047 | } |
| 2048 | EXPECT_TRUE(m.Matches(num + 1)); |
| 2049 | } |
| 2050 | |
| 2051 | // Tests that AllOf(m1, ..., mn) matches any value that matches all of |
| 2052 | // the given matchers. |
| 2053 | TEST(AllOfTest, MatchesWhenAllMatch) { |
| 2054 | Matcher<int> m; |
| 2055 | m = AllOf(Le(2), Ge(1)); |
| 2056 | EXPECT_TRUE(m.Matches(1)); |
| 2057 | EXPECT_TRUE(m.Matches(2)); |
| 2058 | EXPECT_FALSE(m.Matches(0)); |
| 2059 | EXPECT_FALSE(m.Matches(3)); |
| 2060 | |
| 2061 | m = AllOf(Gt(0), Ne(1), Ne(2)); |
| 2062 | EXPECT_TRUE(m.Matches(3)); |
| 2063 | EXPECT_FALSE(m.Matches(2)); |
| 2064 | EXPECT_FALSE(m.Matches(1)); |
| 2065 | EXPECT_FALSE(m.Matches(0)); |
| 2066 | |
| 2067 | m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3)); |
| 2068 | EXPECT_TRUE(m.Matches(4)); |
| 2069 | EXPECT_FALSE(m.Matches(3)); |
| 2070 | EXPECT_FALSE(m.Matches(2)); |
| 2071 | EXPECT_FALSE(m.Matches(1)); |
| 2072 | EXPECT_FALSE(m.Matches(0)); |
| 2073 | |
| 2074 | m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7)); |
| 2075 | EXPECT_TRUE(m.Matches(0)); |
| 2076 | EXPECT_TRUE(m.Matches(1)); |
| 2077 | EXPECT_FALSE(m.Matches(3)); |
| 2078 | |
| 2079 | // The following tests for varying number of sub-matchers. Due to the way |
| 2080 | // the sub-matchers are handled it is enough to test every sub-matcher once |
| 2081 | // with sub-matchers using the same matcher type. Varying matcher types are |
| 2082 | // checked for above. |
| 2083 | AllOfMatches(2, AllOf(Ne(1), Ne(2))); |
| 2084 | AllOfMatches(3, AllOf(Ne(1), Ne(2), Ne(3))); |
| 2085 | AllOfMatches(4, AllOf(Ne(1), Ne(2), Ne(3), Ne(4))); |
| 2086 | AllOfMatches(5, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5))); |
| 2087 | AllOfMatches(6, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6))); |
| 2088 | AllOfMatches(7, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7))); |
| 2089 | AllOfMatches(8, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), |
| 2090 | Ne(8))); |
| 2091 | AllOfMatches(9, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), |
| 2092 | Ne(8), Ne(9))); |
| 2093 | AllOfMatches(10, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), |
| 2094 | Ne(9), Ne(10))); |
| 2095 | } |
| 2096 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 2097 | #if GTEST_LANG_CXX11 |
| 2098 | // Tests the variadic version of the AllOfMatcher. |
| 2099 | TEST(AllOfTest, VariadicMatchesWhenAllMatch) { |
| 2100 | // Make sure AllOf is defined in the right namespace and does not depend on |
| 2101 | // ADL. |
| 2102 | ::testing::AllOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11); |
| 2103 | Matcher<int> m = AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), |
| 2104 | Ne(9), Ne(10), Ne(11)); |
| 2105 | EXPECT_THAT(Describe(m), EndsWith("and (isn't equal to 11))))))))))")); |
| 2106 | AllOfMatches(11, m); |
| 2107 | AllOfMatches(50, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), |
| 2108 | Ne(9), Ne(10), Ne(11), Ne(12), Ne(13), Ne(14), Ne(15), |
| 2109 | Ne(16), Ne(17), Ne(18), Ne(19), Ne(20), Ne(21), Ne(22), |
| 2110 | Ne(23), Ne(24), Ne(25), Ne(26), Ne(27), Ne(28), Ne(29), |
| 2111 | Ne(30), Ne(31), Ne(32), Ne(33), Ne(34), Ne(35), Ne(36), |
| 2112 | Ne(37), Ne(38), Ne(39), Ne(40), Ne(41), Ne(42), Ne(43), |
| 2113 | Ne(44), Ne(45), Ne(46), Ne(47), Ne(48), Ne(49), |
| 2114 | Ne(50))); |
| 2115 | } |
| 2116 | |
| 2117 | #endif // GTEST_LANG_CXX11 |
| 2118 | |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 2119 | // Tests that AllOf(m1, ..., mn) describes itself properly. |
| 2120 | TEST(AllOfTest, CanDescribeSelf) { |
| 2121 | Matcher<int> m; |
| 2122 | m = AllOf(Le(2), Ge(1)); |
| 2123 | EXPECT_EQ("(is <= 2) and (is >= 1)", Describe(m)); |
| 2124 | |
| 2125 | m = AllOf(Gt(0), Ne(1), Ne(2)); |
| 2126 | EXPECT_EQ("(is > 0) and " |
| 2127 | "((isn't equal to 1) and " |
| 2128 | "(isn't equal to 2))", |
| 2129 | Describe(m)); |
| 2130 | |
| 2131 | |
| 2132 | m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3)); |
| 2133 | EXPECT_EQ("((is > 0) and " |
| 2134 | "(isn't equal to 1)) and " |
| 2135 | "((isn't equal to 2) and " |
| 2136 | "(isn't equal to 3))", |
| 2137 | Describe(m)); |
| 2138 | |
| 2139 | |
| 2140 | m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7)); |
| 2141 | EXPECT_EQ("((is >= 0) and " |
| 2142 | "(is < 10)) and " |
| 2143 | "((isn't equal to 3) and " |
| 2144 | "((isn't equal to 5) and " |
| 2145 | "(isn't equal to 7)))", |
| 2146 | Describe(m)); |
| 2147 | } |
| 2148 | |
| 2149 | // Tests that AllOf(m1, ..., mn) describes its negation properly. |
| 2150 | TEST(AllOfTest, CanDescribeNegation) { |
| 2151 | Matcher<int> m; |
| 2152 | m = AllOf(Le(2), Ge(1)); |
| 2153 | EXPECT_EQ("(isn't <= 2) or " |
| 2154 | "(isn't >= 1)", |
| 2155 | DescribeNegation(m)); |
| 2156 | |
| 2157 | m = AllOf(Gt(0), Ne(1), Ne(2)); |
| 2158 | EXPECT_EQ("(isn't > 0) or " |
| 2159 | "((is equal to 1) or " |
| 2160 | "(is equal to 2))", |
| 2161 | DescribeNegation(m)); |
| 2162 | |
| 2163 | |
| 2164 | m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3)); |
| 2165 | EXPECT_EQ("((isn't > 0) or " |
| 2166 | "(is equal to 1)) or " |
| 2167 | "((is equal to 2) or " |
| 2168 | "(is equal to 3))", |
| 2169 | DescribeNegation(m)); |
| 2170 | |
| 2171 | |
| 2172 | m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7)); |
| 2173 | EXPECT_EQ("((isn't >= 0) or " |
| 2174 | "(isn't < 10)) or " |
| 2175 | "((is equal to 3) or " |
| 2176 | "((is equal to 5) or " |
| 2177 | "(is equal to 7)))", |
| 2178 | DescribeNegation(m)); |
| 2179 | } |
| 2180 | |
| 2181 | // Tests that monomorphic matchers are safely cast by the AllOf matcher. |
| 2182 | TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) { |
| 2183 | // greater_than_5 and less_than_10 are monomorphic matchers. |
| 2184 | Matcher<int> greater_than_5 = Gt(5); |
| 2185 | Matcher<int> less_than_10 = Lt(10); |
| 2186 | |
| 2187 | Matcher<const int&> m = AllOf(greater_than_5, less_than_10); |
| 2188 | Matcher<int&> m2 = AllOf(greater_than_5, less_than_10); |
| 2189 | Matcher<int&> m3 = AllOf(greater_than_5, m2); |
| 2190 | |
| 2191 | // Tests that BothOf works when composing itself. |
| 2192 | Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10); |
| 2193 | Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10); |
| 2194 | } |
| 2195 | |
| 2196 | TEST(AllOfTest, ExplainsResult) { |
| 2197 | Matcher<int> m; |
| 2198 | |
| 2199 | // Successful match. Both matchers need to explain. The second |
| 2200 | // matcher doesn't give an explanation, so only the first matcher's |
| 2201 | // explanation is printed. |
| 2202 | m = AllOf(GreaterThan(10), Lt(30)); |
| 2203 | EXPECT_EQ("which is 15 more than 10", Explain(m, 25)); |
| 2204 | |
| 2205 | // Successful match. Both matchers need to explain. |
| 2206 | m = AllOf(GreaterThan(10), GreaterThan(20)); |
| 2207 | EXPECT_EQ("which is 20 more than 10, and which is 10 more than 20", |
| 2208 | Explain(m, 30)); |
| 2209 | |
| 2210 | // Successful match. All matchers need to explain. The second |
| 2211 | // matcher doesn't given an explanation. |
| 2212 | m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20)); |
| 2213 | EXPECT_EQ("which is 15 more than 10, and which is 5 more than 20", |
| 2214 | Explain(m, 25)); |
| 2215 | |
| 2216 | // Successful match. All matchers need to explain. |
| 2217 | m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30)); |
| 2218 | EXPECT_EQ("which is 30 more than 10, and which is 20 more than 20, " |
| 2219 | "and which is 10 more than 30", |
| 2220 | Explain(m, 40)); |
| 2221 | |
| 2222 | // Failed match. The first matcher, which failed, needs to |
| 2223 | // explain. |
| 2224 | m = AllOf(GreaterThan(10), GreaterThan(20)); |
| 2225 | EXPECT_EQ("which is 5 less than 10", Explain(m, 5)); |
| 2226 | |
| 2227 | // Failed match. The second matcher, which failed, needs to |
| 2228 | // explain. Since it doesn't given an explanation, nothing is |
| 2229 | // printed. |
| 2230 | m = AllOf(GreaterThan(10), Lt(30)); |
| 2231 | EXPECT_EQ("", Explain(m, 40)); |
| 2232 | |
| 2233 | // Failed match. The second matcher, which failed, needs to |
| 2234 | // explain. |
| 2235 | m = AllOf(GreaterThan(10), GreaterThan(20)); |
| 2236 | EXPECT_EQ("which is 5 less than 20", Explain(m, 15)); |
| 2237 | } |
| 2238 | |
| 2239 | // Helper to allow easy testing of AnyOf matchers with num parameters. |
| 2240 | void AnyOfMatches(int num, const Matcher<int>& m) { |
| 2241 | SCOPED_TRACE(Describe(m)); |
| 2242 | EXPECT_FALSE(m.Matches(0)); |
| 2243 | for (int i = 1; i <= num; ++i) { |
| 2244 | EXPECT_TRUE(m.Matches(i)); |
| 2245 | } |
| 2246 | EXPECT_FALSE(m.Matches(num + 1)); |
| 2247 | } |
| 2248 | |
| 2249 | // Tests that AnyOf(m1, ..., mn) matches any value that matches at |
| 2250 | // least one of the given matchers. |
| 2251 | TEST(AnyOfTest, MatchesWhenAnyMatches) { |
| 2252 | Matcher<int> m; |
| 2253 | m = AnyOf(Le(1), Ge(3)); |
| 2254 | EXPECT_TRUE(m.Matches(1)); |
| 2255 | EXPECT_TRUE(m.Matches(4)); |
| 2256 | EXPECT_FALSE(m.Matches(2)); |
| 2257 | |
| 2258 | m = AnyOf(Lt(0), Eq(1), Eq(2)); |
| 2259 | EXPECT_TRUE(m.Matches(-1)); |
| 2260 | EXPECT_TRUE(m.Matches(1)); |
| 2261 | EXPECT_TRUE(m.Matches(2)); |
| 2262 | EXPECT_FALSE(m.Matches(0)); |
| 2263 | |
| 2264 | m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3)); |
| 2265 | EXPECT_TRUE(m.Matches(-1)); |
| 2266 | EXPECT_TRUE(m.Matches(1)); |
| 2267 | EXPECT_TRUE(m.Matches(2)); |
| 2268 | EXPECT_TRUE(m.Matches(3)); |
| 2269 | EXPECT_FALSE(m.Matches(0)); |
| 2270 | |
| 2271 | m = AnyOf(Le(0), Gt(10), 3, 5, 7); |
| 2272 | EXPECT_TRUE(m.Matches(0)); |
| 2273 | EXPECT_TRUE(m.Matches(11)); |
| 2274 | EXPECT_TRUE(m.Matches(3)); |
| 2275 | EXPECT_FALSE(m.Matches(2)); |
| 2276 | |
| 2277 | // The following tests for varying number of sub-matchers. Due to the way |
| 2278 | // the sub-matchers are handled it is enough to test every sub-matcher once |
| 2279 | // with sub-matchers using the same matcher type. Varying matcher types are |
| 2280 | // checked for above. |
| 2281 | AnyOfMatches(2, AnyOf(1, 2)); |
| 2282 | AnyOfMatches(3, AnyOf(1, 2, 3)); |
| 2283 | AnyOfMatches(4, AnyOf(1, 2, 3, 4)); |
| 2284 | AnyOfMatches(5, AnyOf(1, 2, 3, 4, 5)); |
| 2285 | AnyOfMatches(6, AnyOf(1, 2, 3, 4, 5, 6)); |
| 2286 | AnyOfMatches(7, AnyOf(1, 2, 3, 4, 5, 6, 7)); |
| 2287 | AnyOfMatches(8, AnyOf(1, 2, 3, 4, 5, 6, 7, 8)); |
| 2288 | AnyOfMatches(9, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9)); |
| 2289 | AnyOfMatches(10, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); |
| 2290 | } |
| 2291 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 2292 | #if GTEST_LANG_CXX11 |
| 2293 | // Tests the variadic version of the AnyOfMatcher. |
| 2294 | TEST(AnyOfTest, VariadicMatchesWhenAnyMatches) { |
| 2295 | // Also make sure AnyOf is defined in the right namespace and does not depend |
| 2296 | // on ADL. |
| 2297 | Matcher<int> m = ::testing::AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11); |
| 2298 | |
| 2299 | EXPECT_THAT(Describe(m), EndsWith("or (is equal to 11))))))))))")); |
| 2300 | AnyOfMatches(11, m); |
| 2301 | AnyOfMatches(50, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, |
| 2302 | 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, |
| 2303 | 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 2304 | 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, |
| 2305 | 41, 42, 43, 44, 45, 46, 47, 48, 49, 50)); |
| 2306 | } |
| 2307 | |
| 2308 | #endif // GTEST_LANG_CXX11 |
| 2309 | |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 2310 | // Tests that AnyOf(m1, ..., mn) describes itself properly. |
| 2311 | TEST(AnyOfTest, CanDescribeSelf) { |
| 2312 | Matcher<int> m; |
| 2313 | m = AnyOf(Le(1), Ge(3)); |
| 2314 | EXPECT_EQ("(is <= 1) or (is >= 3)", |
| 2315 | Describe(m)); |
| 2316 | |
| 2317 | m = AnyOf(Lt(0), Eq(1), Eq(2)); |
| 2318 | EXPECT_EQ("(is < 0) or " |
| 2319 | "((is equal to 1) or (is equal to 2))", |
| 2320 | Describe(m)); |
| 2321 | |
| 2322 | m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3)); |
| 2323 | EXPECT_EQ("((is < 0) or " |
| 2324 | "(is equal to 1)) or " |
| 2325 | "((is equal to 2) or " |
| 2326 | "(is equal to 3))", |
| 2327 | Describe(m)); |
| 2328 | |
| 2329 | m = AnyOf(Le(0), Gt(10), 3, 5, 7); |
| 2330 | EXPECT_EQ("((is <= 0) or " |
| 2331 | "(is > 10)) or " |
| 2332 | "((is equal to 3) or " |
| 2333 | "((is equal to 5) or " |
| 2334 | "(is equal to 7)))", |
| 2335 | Describe(m)); |
| 2336 | } |
| 2337 | |
| 2338 | // Tests that AnyOf(m1, ..., mn) describes its negation properly. |
| 2339 | TEST(AnyOfTest, CanDescribeNegation) { |
| 2340 | Matcher<int> m; |
| 2341 | m = AnyOf(Le(1), Ge(3)); |
| 2342 | EXPECT_EQ("(isn't <= 1) and (isn't >= 3)", |
| 2343 | DescribeNegation(m)); |
| 2344 | |
| 2345 | m = AnyOf(Lt(0), Eq(1), Eq(2)); |
| 2346 | EXPECT_EQ("(isn't < 0) and " |
| 2347 | "((isn't equal to 1) and (isn't equal to 2))", |
| 2348 | DescribeNegation(m)); |
| 2349 | |
| 2350 | m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3)); |
| 2351 | EXPECT_EQ("((isn't < 0) and " |
| 2352 | "(isn't equal to 1)) and " |
| 2353 | "((isn't equal to 2) and " |
| 2354 | "(isn't equal to 3))", |
| 2355 | DescribeNegation(m)); |
| 2356 | |
| 2357 | m = AnyOf(Le(0), Gt(10), 3, 5, 7); |
| 2358 | EXPECT_EQ("((isn't <= 0) and " |
| 2359 | "(isn't > 10)) and " |
| 2360 | "((isn't equal to 3) and " |
| 2361 | "((isn't equal to 5) and " |
| 2362 | "(isn't equal to 7)))", |
| 2363 | DescribeNegation(m)); |
| 2364 | } |
| 2365 | |
| 2366 | // Tests that monomorphic matchers are safely cast by the AnyOf matcher. |
| 2367 | TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) { |
| 2368 | // greater_than_5 and less_than_10 are monomorphic matchers. |
| 2369 | Matcher<int> greater_than_5 = Gt(5); |
| 2370 | Matcher<int> less_than_10 = Lt(10); |
| 2371 | |
| 2372 | Matcher<const int&> m = AnyOf(greater_than_5, less_than_10); |
| 2373 | Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10); |
| 2374 | Matcher<int&> m3 = AnyOf(greater_than_5, m2); |
| 2375 | |
| 2376 | // Tests that EitherOf works when composing itself. |
| 2377 | Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10); |
| 2378 | Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10); |
| 2379 | } |
| 2380 | |
| 2381 | TEST(AnyOfTest, ExplainsResult) { |
| 2382 | Matcher<int> m; |
| 2383 | |
| 2384 | // Failed match. Both matchers need to explain. The second |
| 2385 | // matcher doesn't give an explanation, so only the first matcher's |
| 2386 | // explanation is printed. |
| 2387 | m = AnyOf(GreaterThan(10), Lt(0)); |
| 2388 | EXPECT_EQ("which is 5 less than 10", Explain(m, 5)); |
| 2389 | |
| 2390 | // Failed match. Both matchers need to explain. |
| 2391 | m = AnyOf(GreaterThan(10), GreaterThan(20)); |
| 2392 | EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20", |
| 2393 | Explain(m, 5)); |
| 2394 | |
| 2395 | // Failed match. All matchers need to explain. The second |
| 2396 | // matcher doesn't given an explanation. |
| 2397 | m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30)); |
| 2398 | EXPECT_EQ("which is 5 less than 10, and which is 25 less than 30", |
| 2399 | Explain(m, 5)); |
| 2400 | |
| 2401 | // Failed match. All matchers need to explain. |
| 2402 | m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30)); |
| 2403 | EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20, " |
| 2404 | "and which is 25 less than 30", |
| 2405 | Explain(m, 5)); |
| 2406 | |
| 2407 | // Successful match. The first matcher, which succeeded, needs to |
| 2408 | // explain. |
| 2409 | m = AnyOf(GreaterThan(10), GreaterThan(20)); |
| 2410 | EXPECT_EQ("which is 5 more than 10", Explain(m, 15)); |
| 2411 | |
| 2412 | // Successful match. The second matcher, which succeeded, needs to |
| 2413 | // explain. Since it doesn't given an explanation, nothing is |
| 2414 | // printed. |
| 2415 | m = AnyOf(GreaterThan(10), Lt(30)); |
| 2416 | EXPECT_EQ("", Explain(m, 0)); |
| 2417 | |
| 2418 | // Successful match. The second matcher, which succeeded, needs to |
| 2419 | // explain. |
| 2420 | m = AnyOf(GreaterThan(30), GreaterThan(20)); |
| 2421 | EXPECT_EQ("which is 5 more than 20", Explain(m, 25)); |
| 2422 | } |
| 2423 | |
| 2424 | // The following predicate function and predicate functor are for |
| 2425 | // testing the Truly(predicate) matcher. |
| 2426 | |
| 2427 | // Returns non-zero if the input is positive. Note that the return |
| 2428 | // type of this function is not bool. It's OK as Truly() accepts any |
| 2429 | // unary function or functor whose return type can be implicitly |
| 2430 | // converted to bool. |
| 2431 | int IsPositive(double x) { |
| 2432 | return x > 0 ? 1 : 0; |
| 2433 | } |
| 2434 | |
| 2435 | // This functor returns true if the input is greater than the given |
| 2436 | // number. |
| 2437 | class IsGreaterThan { |
| 2438 | public: |
| 2439 | explicit IsGreaterThan(int threshold) : threshold_(threshold) {} |
| 2440 | |
| 2441 | bool operator()(int n) const { return n > threshold_; } |
| 2442 | |
| 2443 | private: |
| 2444 | int threshold_; |
| 2445 | }; |
| 2446 | |
| 2447 | // For testing Truly(). |
| 2448 | const int foo = 0; |
| 2449 | |
| 2450 | // This predicate returns true iff the argument references foo and has |
| 2451 | // a zero value. |
| 2452 | bool ReferencesFooAndIsZero(const int& n) { |
| 2453 | return (&n == &foo) && (n == 0); |
| 2454 | } |
| 2455 | |
| 2456 | // Tests that Truly(predicate) matches what satisfies the given |
| 2457 | // predicate. |
| 2458 | TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) { |
| 2459 | Matcher<double> m = Truly(IsPositive); |
| 2460 | EXPECT_TRUE(m.Matches(2.0)); |
| 2461 | EXPECT_FALSE(m.Matches(-1.5)); |
| 2462 | } |
| 2463 | |
| 2464 | // Tests that Truly(predicate_functor) works too. |
| 2465 | TEST(TrulyTest, CanBeUsedWithFunctor) { |
| 2466 | Matcher<int> m = Truly(IsGreaterThan(5)); |
| 2467 | EXPECT_TRUE(m.Matches(6)); |
| 2468 | EXPECT_FALSE(m.Matches(4)); |
| 2469 | } |
| 2470 | |
| 2471 | // A class that can be implicitly converted to bool. |
| 2472 | class ConvertibleToBool { |
| 2473 | public: |
| 2474 | explicit ConvertibleToBool(int number) : number_(number) {} |
| 2475 | operator bool() const { return number_ != 0; } |
| 2476 | |
| 2477 | private: |
| 2478 | int number_; |
| 2479 | }; |
| 2480 | |
| 2481 | ConvertibleToBool IsNotZero(int number) { |
| 2482 | return ConvertibleToBool(number); |
| 2483 | } |
| 2484 | |
| 2485 | // Tests that the predicate used in Truly() may return a class that's |
| 2486 | // implicitly convertible to bool, even when the class has no |
| 2487 | // operator!(). |
| 2488 | TEST(TrulyTest, PredicateCanReturnAClassConvertibleToBool) { |
| 2489 | Matcher<int> m = Truly(IsNotZero); |
| 2490 | EXPECT_TRUE(m.Matches(1)); |
| 2491 | EXPECT_FALSE(m.Matches(0)); |
| 2492 | } |
| 2493 | |
| 2494 | // Tests that Truly(predicate) can describe itself properly. |
| 2495 | TEST(TrulyTest, CanDescribeSelf) { |
| 2496 | Matcher<double> m = Truly(IsPositive); |
| 2497 | EXPECT_EQ("satisfies the given predicate", |
| 2498 | Describe(m)); |
| 2499 | } |
| 2500 | |
| 2501 | // Tests that Truly(predicate) works when the matcher takes its |
| 2502 | // argument by reference. |
| 2503 | TEST(TrulyTest, WorksForByRefArguments) { |
| 2504 | Matcher<const int&> m = Truly(ReferencesFooAndIsZero); |
| 2505 | EXPECT_TRUE(m.Matches(foo)); |
| 2506 | int n = 0; |
| 2507 | EXPECT_FALSE(m.Matches(n)); |
| 2508 | } |
| 2509 | |
| 2510 | // Tests that Matches(m) is a predicate satisfied by whatever that |
| 2511 | // matches matcher m. |
| 2512 | TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) { |
| 2513 | EXPECT_TRUE(Matches(Ge(0))(1)); |
| 2514 | EXPECT_FALSE(Matches(Eq('a'))('b')); |
| 2515 | } |
| 2516 | |
| 2517 | // Tests that Matches(m) works when the matcher takes its argument by |
| 2518 | // reference. |
| 2519 | TEST(MatchesTest, WorksOnByRefArguments) { |
| 2520 | int m = 0, n = 0; |
| 2521 | EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n)); |
| 2522 | EXPECT_FALSE(Matches(Ref(m))(n)); |
| 2523 | } |
| 2524 | |
| 2525 | // Tests that a Matcher on non-reference type can be used in |
| 2526 | // Matches(). |
| 2527 | TEST(MatchesTest, WorksWithMatcherOnNonRefType) { |
| 2528 | Matcher<int> eq5 = Eq(5); |
| 2529 | EXPECT_TRUE(Matches(eq5)(5)); |
| 2530 | EXPECT_FALSE(Matches(eq5)(2)); |
| 2531 | } |
| 2532 | |
| 2533 | // Tests Value(value, matcher). Since Value() is a simple wrapper for |
| 2534 | // Matches(), which has been tested already, we don't spend a lot of |
| 2535 | // effort on testing Value(). |
| 2536 | TEST(ValueTest, WorksWithPolymorphicMatcher) { |
| 2537 | EXPECT_TRUE(Value("hi", StartsWith("h"))); |
| 2538 | EXPECT_FALSE(Value(5, Gt(10))); |
| 2539 | } |
| 2540 | |
| 2541 | TEST(ValueTest, WorksWithMonomorphicMatcher) { |
| 2542 | const Matcher<int> is_zero = Eq(0); |
| 2543 | EXPECT_TRUE(Value(0, is_zero)); |
| 2544 | EXPECT_FALSE(Value('a', is_zero)); |
| 2545 | |
| 2546 | int n = 0; |
| 2547 | const Matcher<const int&> ref_n = Ref(n); |
| 2548 | EXPECT_TRUE(Value(n, ref_n)); |
| 2549 | EXPECT_FALSE(Value(1, ref_n)); |
| 2550 | } |
| 2551 | |
| 2552 | TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) { |
| 2553 | StringMatchResultListener listener1; |
| 2554 | EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1)); |
| 2555 | EXPECT_EQ("% 2 == 0", listener1.str()); |
| 2556 | |
| 2557 | StringMatchResultListener listener2; |
| 2558 | EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2)); |
| 2559 | EXPECT_EQ("", listener2.str()); |
| 2560 | } |
| 2561 | |
| 2562 | TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) { |
| 2563 | const Matcher<int> is_even = PolymorphicIsEven(); |
| 2564 | StringMatchResultListener listener1; |
| 2565 | EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1)); |
| 2566 | EXPECT_EQ("% 2 == 0", listener1.str()); |
| 2567 | |
| 2568 | const Matcher<const double&> is_zero = Eq(0); |
| 2569 | StringMatchResultListener listener2; |
| 2570 | EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2)); |
| 2571 | EXPECT_EQ("", listener2.str()); |
| 2572 | } |
| 2573 | |
| 2574 | MATCHER_P(Really, inner_matcher, "") { |
| 2575 | return ExplainMatchResult(inner_matcher, arg, result_listener); |
| 2576 | } |
| 2577 | |
| 2578 | TEST(ExplainMatchResultTest, WorksInsideMATCHER) { |
| 2579 | EXPECT_THAT(0, Really(Eq(0))); |
| 2580 | } |
| 2581 | |
| 2582 | TEST(AllArgsTest, WorksForTuple) { |
| 2583 | EXPECT_THAT(make_tuple(1, 2L), AllArgs(Lt())); |
| 2584 | EXPECT_THAT(make_tuple(2L, 1), Not(AllArgs(Lt()))); |
| 2585 | } |
| 2586 | |
| 2587 | TEST(AllArgsTest, WorksForNonTuple) { |
| 2588 | EXPECT_THAT(42, AllArgs(Gt(0))); |
| 2589 | EXPECT_THAT('a', Not(AllArgs(Eq('b')))); |
| 2590 | } |
| 2591 | |
| 2592 | class AllArgsHelper { |
| 2593 | public: |
| 2594 | AllArgsHelper() {} |
| 2595 | |
| 2596 | MOCK_METHOD2(Helper, int(char x, int y)); |
| 2597 | |
| 2598 | private: |
| 2599 | GTEST_DISALLOW_COPY_AND_ASSIGN_(AllArgsHelper); |
| 2600 | }; |
| 2601 | |
| 2602 | TEST(AllArgsTest, WorksInWithClause) { |
| 2603 | AllArgsHelper helper; |
| 2604 | ON_CALL(helper, Helper(_, _)) |
| 2605 | .With(AllArgs(Lt())) |
| 2606 | .WillByDefault(Return(1)); |
| 2607 | EXPECT_CALL(helper, Helper(_, _)); |
| 2608 | EXPECT_CALL(helper, Helper(_, _)) |
| 2609 | .With(AllArgs(Gt())) |
| 2610 | .WillOnce(Return(2)); |
| 2611 | |
| 2612 | EXPECT_EQ(1, helper.Helper('\1', 2)); |
| 2613 | EXPECT_EQ(2, helper.Helper('a', 1)); |
| 2614 | } |
| 2615 | |
| 2616 | // Tests that ASSERT_THAT() and EXPECT_THAT() work when the value |
| 2617 | // matches the matcher. |
| 2618 | TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) { |
| 2619 | ASSERT_THAT(5, Ge(2)) << "This should succeed."; |
| 2620 | ASSERT_THAT("Foo", EndsWith("oo")); |
| 2621 | EXPECT_THAT(2, AllOf(Le(7), Ge(0))) << "This should succeed too."; |
| 2622 | EXPECT_THAT("Hello", StartsWith("Hell")); |
| 2623 | } |
| 2624 | |
| 2625 | // Tests that ASSERT_THAT() and EXPECT_THAT() work when the value |
| 2626 | // doesn't match the matcher. |
| 2627 | TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) { |
| 2628 | // 'n' must be static as it is used in an EXPECT_FATAL_FAILURE(), |
| 2629 | // which cannot reference auto variables. |
| 2630 | static unsigned short n; // NOLINT |
| 2631 | n = 5; |
| 2632 | |
| 2633 | // VC++ prior to version 8.0 SP1 has a bug where it will not see any |
| 2634 | // functions declared in the namespace scope from within nested classes. |
| 2635 | // EXPECT/ASSERT_(NON)FATAL_FAILURE macros use nested classes so that all |
| 2636 | // namespace-level functions invoked inside them need to be explicitly |
| 2637 | // resolved. |
| 2638 | EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Gt(10)), |
| 2639 | "Value of: n\n" |
| 2640 | "Expected: is > 10\n" |
| 2641 | " Actual: 5" + OfType("unsigned short")); |
| 2642 | n = 0; |
| 2643 | EXPECT_NONFATAL_FAILURE( |
| 2644 | EXPECT_THAT(n, ::testing::AllOf(::testing::Le(7), ::testing::Ge(5))), |
| 2645 | "Value of: n\n" |
| 2646 | "Expected: (is <= 7) and (is >= 5)\n" |
| 2647 | " Actual: 0" + OfType("unsigned short")); |
| 2648 | } |
| 2649 | |
| 2650 | // Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument |
| 2651 | // has a reference type. |
| 2652 | TEST(MatcherAssertionTest, WorksForByRefArguments) { |
| 2653 | // We use a static variable here as EXPECT_FATAL_FAILURE() cannot |
| 2654 | // reference auto variables. |
| 2655 | static int n; |
| 2656 | n = 0; |
| 2657 | EXPECT_THAT(n, AllOf(Le(7), Ref(n))); |
| 2658 | EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))), |
| 2659 | "Value of: n\n" |
| 2660 | "Expected: does not reference the variable @"); |
| 2661 | // Tests the "Actual" part. |
| 2662 | EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))), |
| 2663 | "Actual: 0" + OfType("int") + ", which is located @"); |
| 2664 | } |
| 2665 | |
| 2666 | #if !GTEST_OS_SYMBIAN |
| 2667 | // Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is |
| 2668 | // monomorphic. |
| 2669 | |
| 2670 | // ASSERT_THAT("hello", starts_with_he) fails to compile with Nokia's |
| 2671 | // Symbian compiler: it tries to compile |
| 2672 | // template<T, U> class MatcherCastImpl { ... |
| 2673 | // virtual bool MatchAndExplain(T x, ...) const { |
| 2674 | // return source_matcher_.MatchAndExplain(static_cast<U>(x), ...); |
| 2675 | // with U == string and T == const char* |
| 2676 | // With ASSERT_THAT("hello"...) changed to ASSERT_THAT(string("hello") ... ) |
| 2677 | // the compiler silently crashes with no output. |
| 2678 | // If MatcherCastImpl is changed to use U(x) instead of static_cast<U>(x) |
| 2679 | // the code compiles but the converted string is bogus. |
| 2680 | TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) { |
| 2681 | Matcher<const char*> starts_with_he = StartsWith("he"); |
| 2682 | ASSERT_THAT("hello", starts_with_he); |
| 2683 | |
| 2684 | Matcher<const string&> ends_with_ok = EndsWith("ok"); |
| 2685 | ASSERT_THAT("book", ends_with_ok); |
| 2686 | const string bad = "bad"; |
| 2687 | EXPECT_NONFATAL_FAILURE(EXPECT_THAT(bad, ends_with_ok), |
| 2688 | "Value of: bad\n" |
| 2689 | "Expected: ends with \"ok\"\n" |
| 2690 | " Actual: \"bad\""); |
| 2691 | Matcher<int> is_greater_than_5 = Gt(5); |
| 2692 | EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5), |
| 2693 | "Value of: 5\n" |
| 2694 | "Expected: is > 5\n" |
| 2695 | " Actual: 5" + OfType("int")); |
| 2696 | } |
| 2697 | #endif // !GTEST_OS_SYMBIAN |
| 2698 | |
| 2699 | // Tests floating-point matchers. |
| 2700 | template <typename RawType> |
| 2701 | class FloatingPointTest : public testing::Test { |
| 2702 | protected: |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 2703 | typedef testing::internal::FloatingPoint<RawType> Floating; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 2704 | typedef typename Floating::Bits Bits; |
| 2705 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 2706 | FloatingPointTest() |
| 2707 | : max_ulps_(Floating::kMaxUlps), |
| 2708 | zero_bits_(Floating(0).bits()), |
| 2709 | one_bits_(Floating(1).bits()), |
| 2710 | infinity_bits_(Floating(Floating::Infinity()).bits()), |
| 2711 | close_to_positive_zero_( |
| 2712 | Floating::ReinterpretBits(zero_bits_ + max_ulps_/2)), |
| 2713 | close_to_negative_zero_( |
| 2714 | -Floating::ReinterpretBits(zero_bits_ + max_ulps_ - max_ulps_/2)), |
| 2715 | further_from_negative_zero_(-Floating::ReinterpretBits( |
| 2716 | zero_bits_ + max_ulps_ + 1 - max_ulps_/2)), |
| 2717 | close_to_one_(Floating::ReinterpretBits(one_bits_ + max_ulps_)), |
| 2718 | further_from_one_(Floating::ReinterpretBits(one_bits_ + max_ulps_ + 1)), |
| 2719 | infinity_(Floating::Infinity()), |
| 2720 | close_to_infinity_( |
| 2721 | Floating::ReinterpretBits(infinity_bits_ - max_ulps_)), |
| 2722 | further_from_infinity_( |
| 2723 | Floating::ReinterpretBits(infinity_bits_ - max_ulps_ - 1)), |
| 2724 | max_(Floating::Max()), |
| 2725 | nan1_(Floating::ReinterpretBits(Floating::kExponentBitMask | 1)), |
| 2726 | nan2_(Floating::ReinterpretBits(Floating::kExponentBitMask | 200)) { |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 2727 | } |
| 2728 | |
| 2729 | void TestSize() { |
| 2730 | EXPECT_EQ(sizeof(RawType), sizeof(Bits)); |
| 2731 | } |
| 2732 | |
| 2733 | // A battery of tests for FloatingEqMatcher::Matches. |
| 2734 | // matcher_maker is a pointer to a function which creates a FloatingEqMatcher. |
| 2735 | void TestMatches( |
| 2736 | testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) { |
| 2737 | Matcher<RawType> m1 = matcher_maker(0.0); |
| 2738 | EXPECT_TRUE(m1.Matches(-0.0)); |
| 2739 | EXPECT_TRUE(m1.Matches(close_to_positive_zero_)); |
| 2740 | EXPECT_TRUE(m1.Matches(close_to_negative_zero_)); |
| 2741 | EXPECT_FALSE(m1.Matches(1.0)); |
| 2742 | |
| 2743 | Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_); |
| 2744 | EXPECT_FALSE(m2.Matches(further_from_negative_zero_)); |
| 2745 | |
| 2746 | Matcher<RawType> m3 = matcher_maker(1.0); |
| 2747 | EXPECT_TRUE(m3.Matches(close_to_one_)); |
| 2748 | EXPECT_FALSE(m3.Matches(further_from_one_)); |
| 2749 | |
| 2750 | // Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above. |
| 2751 | EXPECT_FALSE(m3.Matches(0.0)); |
| 2752 | |
| 2753 | Matcher<RawType> m4 = matcher_maker(-infinity_); |
| 2754 | EXPECT_TRUE(m4.Matches(-close_to_infinity_)); |
| 2755 | |
| 2756 | Matcher<RawType> m5 = matcher_maker(infinity_); |
| 2757 | EXPECT_TRUE(m5.Matches(close_to_infinity_)); |
| 2758 | |
| 2759 | // This is interesting as the representations of infinity_ and nan1_ |
| 2760 | // are only 1 DLP apart. |
| 2761 | EXPECT_FALSE(m5.Matches(nan1_)); |
| 2762 | |
| 2763 | // matcher_maker can produce a Matcher<const RawType&>, which is needed in |
| 2764 | // some cases. |
| 2765 | Matcher<const RawType&> m6 = matcher_maker(0.0); |
| 2766 | EXPECT_TRUE(m6.Matches(-0.0)); |
| 2767 | EXPECT_TRUE(m6.Matches(close_to_positive_zero_)); |
| 2768 | EXPECT_FALSE(m6.Matches(1.0)); |
| 2769 | |
| 2770 | // matcher_maker can produce a Matcher<RawType&>, which is needed in some |
| 2771 | // cases. |
| 2772 | Matcher<RawType&> m7 = matcher_maker(0.0); |
| 2773 | RawType x = 0.0; |
| 2774 | EXPECT_TRUE(m7.Matches(x)); |
| 2775 | x = 0.01f; |
| 2776 | EXPECT_FALSE(m7.Matches(x)); |
| 2777 | } |
| 2778 | |
| 2779 | // Pre-calculated numbers to be used by the tests. |
| 2780 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 2781 | const size_t max_ulps_; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 2782 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 2783 | const Bits zero_bits_; // The bits that represent 0.0. |
| 2784 | const Bits one_bits_; // The bits that represent 1.0. |
| 2785 | const Bits infinity_bits_; // The bits that represent +infinity. |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 2786 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 2787 | // Some numbers close to 0.0. |
| 2788 | const RawType close_to_positive_zero_; |
| 2789 | const RawType close_to_negative_zero_; |
| 2790 | const RawType further_from_negative_zero_; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 2791 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 2792 | // Some numbers close to 1.0. |
| 2793 | const RawType close_to_one_; |
| 2794 | const RawType further_from_one_; |
| 2795 | |
| 2796 | // Some numbers close to +infinity. |
| 2797 | const RawType infinity_; |
| 2798 | const RawType close_to_infinity_; |
| 2799 | const RawType further_from_infinity_; |
| 2800 | |
| 2801 | // Maximum representable value that's not infinity. |
| 2802 | const RawType max_; |
| 2803 | |
| 2804 | // Some NaNs. |
| 2805 | const RawType nan1_; |
| 2806 | const RawType nan2_; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 2807 | }; |
| 2808 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 2809 | // Tests floating-point matchers with fixed epsilons. |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 2810 | template <typename RawType> |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 2811 | class FloatingPointNearTest : public FloatingPointTest<RawType> { |
| 2812 | protected: |
| 2813 | typedef FloatingPointTest<RawType> ParentType; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 2814 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 2815 | // A battery of tests for FloatingEqMatcher::Matches with a fixed epsilon. |
| 2816 | // matcher_maker is a pointer to a function which creates a FloatingEqMatcher. |
| 2817 | void TestNearMatches( |
| 2818 | testing::internal::FloatingEqMatcher<RawType> |
| 2819 | (*matcher_maker)(RawType, RawType)) { |
| 2820 | Matcher<RawType> m1 = matcher_maker(0.0, 0.0); |
| 2821 | EXPECT_TRUE(m1.Matches(0.0)); |
| 2822 | EXPECT_TRUE(m1.Matches(-0.0)); |
| 2823 | EXPECT_FALSE(m1.Matches(ParentType::close_to_positive_zero_)); |
| 2824 | EXPECT_FALSE(m1.Matches(ParentType::close_to_negative_zero_)); |
| 2825 | EXPECT_FALSE(m1.Matches(1.0)); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 2826 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 2827 | Matcher<RawType> m2 = matcher_maker(0.0, 1.0); |
| 2828 | EXPECT_TRUE(m2.Matches(0.0)); |
| 2829 | EXPECT_TRUE(m2.Matches(-0.0)); |
| 2830 | EXPECT_TRUE(m2.Matches(1.0)); |
| 2831 | EXPECT_TRUE(m2.Matches(-1.0)); |
| 2832 | EXPECT_FALSE(m2.Matches(ParentType::close_to_one_)); |
| 2833 | EXPECT_FALSE(m2.Matches(-ParentType::close_to_one_)); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 2834 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 2835 | // Check that inf matches inf, regardless of the of the specified max |
| 2836 | // absolute error. |
| 2837 | Matcher<RawType> m3 = matcher_maker(ParentType::infinity_, 0.0); |
| 2838 | EXPECT_TRUE(m3.Matches(ParentType::infinity_)); |
| 2839 | EXPECT_FALSE(m3.Matches(ParentType::close_to_infinity_)); |
| 2840 | EXPECT_FALSE(m3.Matches(-ParentType::infinity_)); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 2841 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 2842 | Matcher<RawType> m4 = matcher_maker(-ParentType::infinity_, 0.0); |
| 2843 | EXPECT_TRUE(m4.Matches(-ParentType::infinity_)); |
| 2844 | EXPECT_FALSE(m4.Matches(-ParentType::close_to_infinity_)); |
| 2845 | EXPECT_FALSE(m4.Matches(ParentType::infinity_)); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 2846 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 2847 | // Test various overflow scenarios. |
| 2848 | Matcher<RawType> m5 = matcher_maker(ParentType::max_, ParentType::max_); |
| 2849 | EXPECT_TRUE(m5.Matches(ParentType::max_)); |
| 2850 | EXPECT_FALSE(m5.Matches(-ParentType::max_)); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 2851 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 2852 | Matcher<RawType> m6 = matcher_maker(-ParentType::max_, ParentType::max_); |
| 2853 | EXPECT_FALSE(m6.Matches(ParentType::max_)); |
| 2854 | EXPECT_TRUE(m6.Matches(-ParentType::max_)); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 2855 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 2856 | Matcher<RawType> m7 = matcher_maker(ParentType::max_, 0); |
| 2857 | EXPECT_TRUE(m7.Matches(ParentType::max_)); |
| 2858 | EXPECT_FALSE(m7.Matches(-ParentType::max_)); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 2859 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 2860 | Matcher<RawType> m8 = matcher_maker(-ParentType::max_, 0); |
| 2861 | EXPECT_FALSE(m8.Matches(ParentType::max_)); |
| 2862 | EXPECT_TRUE(m8.Matches(-ParentType::max_)); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 2863 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 2864 | // The difference between max() and -max() normally overflows to infinity, |
| 2865 | // but it should still match if the max_abs_error is also infinity. |
| 2866 | Matcher<RawType> m9 = matcher_maker( |
| 2867 | ParentType::max_, ParentType::infinity_); |
| 2868 | EXPECT_TRUE(m8.Matches(-ParentType::max_)); |
| 2869 | |
| 2870 | // matcher_maker can produce a Matcher<const RawType&>, which is needed in |
| 2871 | // some cases. |
| 2872 | Matcher<const RawType&> m10 = matcher_maker(0.0, 1.0); |
| 2873 | EXPECT_TRUE(m10.Matches(-0.0)); |
| 2874 | EXPECT_TRUE(m10.Matches(ParentType::close_to_positive_zero_)); |
| 2875 | EXPECT_FALSE(m10.Matches(ParentType::close_to_one_)); |
| 2876 | |
| 2877 | // matcher_maker can produce a Matcher<RawType&>, which is needed in some |
| 2878 | // cases. |
| 2879 | Matcher<RawType&> m11 = matcher_maker(0.0, 1.0); |
| 2880 | RawType x = 0.0; |
| 2881 | EXPECT_TRUE(m11.Matches(x)); |
| 2882 | x = 1.0f; |
| 2883 | EXPECT_TRUE(m11.Matches(x)); |
| 2884 | x = -1.0f; |
| 2885 | EXPECT_TRUE(m11.Matches(x)); |
| 2886 | x = 1.1f; |
| 2887 | EXPECT_FALSE(m11.Matches(x)); |
| 2888 | x = -1.1f; |
| 2889 | EXPECT_FALSE(m11.Matches(x)); |
| 2890 | } |
| 2891 | }; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 2892 | |
| 2893 | // Instantiate FloatingPointTest for testing floats. |
| 2894 | typedef FloatingPointTest<float> FloatTest; |
| 2895 | |
| 2896 | TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) { |
| 2897 | TestMatches(&FloatEq); |
| 2898 | } |
| 2899 | |
| 2900 | TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) { |
| 2901 | TestMatches(&NanSensitiveFloatEq); |
| 2902 | } |
| 2903 | |
| 2904 | TEST_F(FloatTest, FloatEqCannotMatchNaN) { |
| 2905 | // FloatEq never matches NaN. |
| 2906 | Matcher<float> m = FloatEq(nan1_); |
| 2907 | EXPECT_FALSE(m.Matches(nan1_)); |
| 2908 | EXPECT_FALSE(m.Matches(nan2_)); |
| 2909 | EXPECT_FALSE(m.Matches(1.0)); |
| 2910 | } |
| 2911 | |
| 2912 | TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) { |
| 2913 | // NanSensitiveFloatEq will match NaN. |
| 2914 | Matcher<float> m = NanSensitiveFloatEq(nan1_); |
| 2915 | EXPECT_TRUE(m.Matches(nan1_)); |
| 2916 | EXPECT_TRUE(m.Matches(nan2_)); |
| 2917 | EXPECT_FALSE(m.Matches(1.0)); |
| 2918 | } |
| 2919 | |
| 2920 | TEST_F(FloatTest, FloatEqCanDescribeSelf) { |
| 2921 | Matcher<float> m1 = FloatEq(2.0f); |
| 2922 | EXPECT_EQ("is approximately 2", Describe(m1)); |
| 2923 | EXPECT_EQ("isn't approximately 2", DescribeNegation(m1)); |
| 2924 | |
| 2925 | Matcher<float> m2 = FloatEq(0.5f); |
| 2926 | EXPECT_EQ("is approximately 0.5", Describe(m2)); |
| 2927 | EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2)); |
| 2928 | |
| 2929 | Matcher<float> m3 = FloatEq(nan1_); |
| 2930 | EXPECT_EQ("never matches", Describe(m3)); |
| 2931 | EXPECT_EQ("is anything", DescribeNegation(m3)); |
| 2932 | } |
| 2933 | |
| 2934 | TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) { |
| 2935 | Matcher<float> m1 = NanSensitiveFloatEq(2.0f); |
| 2936 | EXPECT_EQ("is approximately 2", Describe(m1)); |
| 2937 | EXPECT_EQ("isn't approximately 2", DescribeNegation(m1)); |
| 2938 | |
| 2939 | Matcher<float> m2 = NanSensitiveFloatEq(0.5f); |
| 2940 | EXPECT_EQ("is approximately 0.5", Describe(m2)); |
| 2941 | EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2)); |
| 2942 | |
| 2943 | Matcher<float> m3 = NanSensitiveFloatEq(nan1_); |
| 2944 | EXPECT_EQ("is NaN", Describe(m3)); |
| 2945 | EXPECT_EQ("isn't NaN", DescribeNegation(m3)); |
| 2946 | } |
| 2947 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 2948 | // Instantiate FloatingPointTest for testing floats with a user-specified |
| 2949 | // max absolute error. |
| 2950 | typedef FloatingPointNearTest<float> FloatNearTest; |
| 2951 | |
| 2952 | TEST_F(FloatNearTest, FloatNearMatches) { |
| 2953 | TestNearMatches(&FloatNear); |
| 2954 | } |
| 2955 | |
| 2956 | TEST_F(FloatNearTest, NanSensitiveFloatNearApproximatelyMatchesFloats) { |
| 2957 | TestNearMatches(&NanSensitiveFloatNear); |
| 2958 | } |
| 2959 | |
| 2960 | TEST_F(FloatNearTest, FloatNearCanDescribeSelf) { |
| 2961 | Matcher<float> m1 = FloatNear(2.0f, 0.5f); |
| 2962 | EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1)); |
| 2963 | EXPECT_EQ( |
| 2964 | "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1)); |
| 2965 | |
| 2966 | Matcher<float> m2 = FloatNear(0.5f, 0.5f); |
| 2967 | EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2)); |
| 2968 | EXPECT_EQ( |
| 2969 | "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2)); |
| 2970 | |
| 2971 | Matcher<float> m3 = FloatNear(nan1_, 0.0); |
| 2972 | EXPECT_EQ("never matches", Describe(m3)); |
| 2973 | EXPECT_EQ("is anything", DescribeNegation(m3)); |
| 2974 | } |
| 2975 | |
| 2976 | TEST_F(FloatNearTest, NanSensitiveFloatNearCanDescribeSelf) { |
| 2977 | Matcher<float> m1 = NanSensitiveFloatNear(2.0f, 0.5f); |
| 2978 | EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1)); |
| 2979 | EXPECT_EQ( |
| 2980 | "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1)); |
| 2981 | |
| 2982 | Matcher<float> m2 = NanSensitiveFloatNear(0.5f, 0.5f); |
| 2983 | EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2)); |
| 2984 | EXPECT_EQ( |
| 2985 | "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2)); |
| 2986 | |
| 2987 | Matcher<float> m3 = NanSensitiveFloatNear(nan1_, 0.1f); |
| 2988 | EXPECT_EQ("is NaN", Describe(m3)); |
| 2989 | EXPECT_EQ("isn't NaN", DescribeNegation(m3)); |
| 2990 | } |
| 2991 | |
| 2992 | TEST_F(FloatNearTest, FloatNearCannotMatchNaN) { |
| 2993 | // FloatNear never matches NaN. |
| 2994 | Matcher<float> m = FloatNear(ParentType::nan1_, 0.1f); |
| 2995 | EXPECT_FALSE(m.Matches(nan1_)); |
| 2996 | EXPECT_FALSE(m.Matches(nan2_)); |
| 2997 | EXPECT_FALSE(m.Matches(1.0)); |
| 2998 | } |
| 2999 | |
| 3000 | TEST_F(FloatNearTest, NanSensitiveFloatNearCanMatchNaN) { |
| 3001 | // NanSensitiveFloatNear will match NaN. |
| 3002 | Matcher<float> m = NanSensitiveFloatNear(nan1_, 0.1f); |
| 3003 | EXPECT_TRUE(m.Matches(nan1_)); |
| 3004 | EXPECT_TRUE(m.Matches(nan2_)); |
| 3005 | EXPECT_FALSE(m.Matches(1.0)); |
| 3006 | } |
| 3007 | |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 3008 | // Instantiate FloatingPointTest for testing doubles. |
| 3009 | typedef FloatingPointTest<double> DoubleTest; |
| 3010 | |
| 3011 | TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) { |
| 3012 | TestMatches(&DoubleEq); |
| 3013 | } |
| 3014 | |
| 3015 | TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) { |
| 3016 | TestMatches(&NanSensitiveDoubleEq); |
| 3017 | } |
| 3018 | |
| 3019 | TEST_F(DoubleTest, DoubleEqCannotMatchNaN) { |
| 3020 | // DoubleEq never matches NaN. |
| 3021 | Matcher<double> m = DoubleEq(nan1_); |
| 3022 | EXPECT_FALSE(m.Matches(nan1_)); |
| 3023 | EXPECT_FALSE(m.Matches(nan2_)); |
| 3024 | EXPECT_FALSE(m.Matches(1.0)); |
| 3025 | } |
| 3026 | |
| 3027 | TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) { |
| 3028 | // NanSensitiveDoubleEq will match NaN. |
| 3029 | Matcher<double> m = NanSensitiveDoubleEq(nan1_); |
| 3030 | EXPECT_TRUE(m.Matches(nan1_)); |
| 3031 | EXPECT_TRUE(m.Matches(nan2_)); |
| 3032 | EXPECT_FALSE(m.Matches(1.0)); |
| 3033 | } |
| 3034 | |
| 3035 | TEST_F(DoubleTest, DoubleEqCanDescribeSelf) { |
| 3036 | Matcher<double> m1 = DoubleEq(2.0); |
| 3037 | EXPECT_EQ("is approximately 2", Describe(m1)); |
| 3038 | EXPECT_EQ("isn't approximately 2", DescribeNegation(m1)); |
| 3039 | |
| 3040 | Matcher<double> m2 = DoubleEq(0.5); |
| 3041 | EXPECT_EQ("is approximately 0.5", Describe(m2)); |
| 3042 | EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2)); |
| 3043 | |
| 3044 | Matcher<double> m3 = DoubleEq(nan1_); |
| 3045 | EXPECT_EQ("never matches", Describe(m3)); |
| 3046 | EXPECT_EQ("is anything", DescribeNegation(m3)); |
| 3047 | } |
| 3048 | |
| 3049 | TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) { |
| 3050 | Matcher<double> m1 = NanSensitiveDoubleEq(2.0); |
| 3051 | EXPECT_EQ("is approximately 2", Describe(m1)); |
| 3052 | EXPECT_EQ("isn't approximately 2", DescribeNegation(m1)); |
| 3053 | |
| 3054 | Matcher<double> m2 = NanSensitiveDoubleEq(0.5); |
| 3055 | EXPECT_EQ("is approximately 0.5", Describe(m2)); |
| 3056 | EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2)); |
| 3057 | |
| 3058 | Matcher<double> m3 = NanSensitiveDoubleEq(nan1_); |
| 3059 | EXPECT_EQ("is NaN", Describe(m3)); |
| 3060 | EXPECT_EQ("isn't NaN", DescribeNegation(m3)); |
| 3061 | } |
| 3062 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 3063 | // Instantiate FloatingPointTest for testing floats with a user-specified |
| 3064 | // max absolute error. |
| 3065 | typedef FloatingPointNearTest<double> DoubleNearTest; |
| 3066 | |
| 3067 | TEST_F(DoubleNearTest, DoubleNearMatches) { |
| 3068 | TestNearMatches(&DoubleNear); |
| 3069 | } |
| 3070 | |
| 3071 | TEST_F(DoubleNearTest, NanSensitiveDoubleNearApproximatelyMatchesDoubles) { |
| 3072 | TestNearMatches(&NanSensitiveDoubleNear); |
| 3073 | } |
| 3074 | |
| 3075 | TEST_F(DoubleNearTest, DoubleNearCanDescribeSelf) { |
| 3076 | Matcher<double> m1 = DoubleNear(2.0, 0.5); |
| 3077 | EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1)); |
| 3078 | EXPECT_EQ( |
| 3079 | "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1)); |
| 3080 | |
| 3081 | Matcher<double> m2 = DoubleNear(0.5, 0.5); |
| 3082 | EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2)); |
| 3083 | EXPECT_EQ( |
| 3084 | "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2)); |
| 3085 | |
| 3086 | Matcher<double> m3 = DoubleNear(nan1_, 0.0); |
| 3087 | EXPECT_EQ("never matches", Describe(m3)); |
| 3088 | EXPECT_EQ("is anything", DescribeNegation(m3)); |
| 3089 | } |
| 3090 | |
| 3091 | TEST_F(DoubleNearTest, ExplainsResultWhenMatchFails) { |
| 3092 | EXPECT_EQ("", Explain(DoubleNear(2.0, 0.1), 2.05)); |
| 3093 | EXPECT_EQ("which is 0.2 from 2", Explain(DoubleNear(2.0, 0.1), 2.2)); |
| 3094 | EXPECT_EQ("which is -0.3 from 2", Explain(DoubleNear(2.0, 0.1), 1.7)); |
| 3095 | |
| 3096 | const string explanation = Explain(DoubleNear(2.1, 1e-10), 2.1 + 1.2e-10); |
| 3097 | // Different C++ implementations may print floating-point numbers |
| 3098 | // slightly differently. |
| 3099 | EXPECT_TRUE(explanation == "which is 1.2e-10 from 2.1" || // GCC |
| 3100 | explanation == "which is 1.2e-010 from 2.1") // MSVC |
| 3101 | << " where explanation is \"" << explanation << "\"."; |
| 3102 | } |
| 3103 | |
| 3104 | TEST_F(DoubleNearTest, NanSensitiveDoubleNearCanDescribeSelf) { |
| 3105 | Matcher<double> m1 = NanSensitiveDoubleNear(2.0, 0.5); |
| 3106 | EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1)); |
| 3107 | EXPECT_EQ( |
| 3108 | "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1)); |
| 3109 | |
| 3110 | Matcher<double> m2 = NanSensitiveDoubleNear(0.5, 0.5); |
| 3111 | EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2)); |
| 3112 | EXPECT_EQ( |
| 3113 | "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2)); |
| 3114 | |
| 3115 | Matcher<double> m3 = NanSensitiveDoubleNear(nan1_, 0.1); |
| 3116 | EXPECT_EQ("is NaN", Describe(m3)); |
| 3117 | EXPECT_EQ("isn't NaN", DescribeNegation(m3)); |
| 3118 | } |
| 3119 | |
| 3120 | TEST_F(DoubleNearTest, DoubleNearCannotMatchNaN) { |
| 3121 | // DoubleNear never matches NaN. |
| 3122 | Matcher<double> m = DoubleNear(ParentType::nan1_, 0.1); |
| 3123 | EXPECT_FALSE(m.Matches(nan1_)); |
| 3124 | EXPECT_FALSE(m.Matches(nan2_)); |
| 3125 | EXPECT_FALSE(m.Matches(1.0)); |
| 3126 | } |
| 3127 | |
| 3128 | TEST_F(DoubleNearTest, NanSensitiveDoubleNearCanMatchNaN) { |
| 3129 | // NanSensitiveDoubleNear will match NaN. |
| 3130 | Matcher<double> m = NanSensitiveDoubleNear(nan1_, 0.1); |
| 3131 | EXPECT_TRUE(m.Matches(nan1_)); |
| 3132 | EXPECT_TRUE(m.Matches(nan2_)); |
| 3133 | EXPECT_FALSE(m.Matches(1.0)); |
| 3134 | } |
| 3135 | |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 3136 | TEST(PointeeTest, RawPointer) { |
| 3137 | const Matcher<int*> m = Pointee(Ge(0)); |
| 3138 | |
| 3139 | int n = 1; |
| 3140 | EXPECT_TRUE(m.Matches(&n)); |
| 3141 | n = -1; |
| 3142 | EXPECT_FALSE(m.Matches(&n)); |
| 3143 | EXPECT_FALSE(m.Matches(NULL)); |
| 3144 | } |
| 3145 | |
| 3146 | TEST(PointeeTest, RawPointerToConst) { |
| 3147 | const Matcher<const double*> m = Pointee(Ge(0)); |
| 3148 | |
| 3149 | double x = 1; |
| 3150 | EXPECT_TRUE(m.Matches(&x)); |
| 3151 | x = -1; |
| 3152 | EXPECT_FALSE(m.Matches(&x)); |
| 3153 | EXPECT_FALSE(m.Matches(NULL)); |
| 3154 | } |
| 3155 | |
| 3156 | TEST(PointeeTest, ReferenceToConstRawPointer) { |
| 3157 | const Matcher<int* const &> m = Pointee(Ge(0)); |
| 3158 | |
| 3159 | int n = 1; |
| 3160 | EXPECT_TRUE(m.Matches(&n)); |
| 3161 | n = -1; |
| 3162 | EXPECT_FALSE(m.Matches(&n)); |
| 3163 | EXPECT_FALSE(m.Matches(NULL)); |
| 3164 | } |
| 3165 | |
| 3166 | TEST(PointeeTest, ReferenceToNonConstRawPointer) { |
| 3167 | const Matcher<double* &> m = Pointee(Ge(0)); |
| 3168 | |
| 3169 | double x = 1.0; |
| 3170 | double* p = &x; |
| 3171 | EXPECT_TRUE(m.Matches(p)); |
| 3172 | x = -1; |
| 3173 | EXPECT_FALSE(m.Matches(p)); |
| 3174 | p = NULL; |
| 3175 | EXPECT_FALSE(m.Matches(p)); |
| 3176 | } |
| 3177 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 3178 | MATCHER_P(FieldIIs, inner_matcher, "") { |
| 3179 | return ExplainMatchResult(inner_matcher, arg.i, result_listener); |
| 3180 | } |
| 3181 | |
| 3182 | TEST(WhenDynamicCastToTest, SameType) { |
| 3183 | Derived derived; |
| 3184 | derived.i = 4; |
| 3185 | |
| 3186 | // Right type. A pointer is passed down. |
| 3187 | Base* as_base_ptr = &derived; |
| 3188 | EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(Not(IsNull()))); |
| 3189 | EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(Pointee(FieldIIs(4)))); |
| 3190 | EXPECT_THAT(as_base_ptr, |
| 3191 | Not(WhenDynamicCastTo<Derived*>(Pointee(FieldIIs(5))))); |
| 3192 | } |
| 3193 | |
| 3194 | TEST(WhenDynamicCastToTest, WrongTypes) { |
| 3195 | Base base; |
| 3196 | Derived derived; |
| 3197 | OtherDerived other_derived; |
| 3198 | |
| 3199 | // Wrong types. NULL is passed. |
| 3200 | EXPECT_THAT(&base, Not(WhenDynamicCastTo<Derived*>(Pointee(_)))); |
| 3201 | EXPECT_THAT(&base, WhenDynamicCastTo<Derived*>(IsNull())); |
| 3202 | Base* as_base_ptr = &derived; |
| 3203 | EXPECT_THAT(as_base_ptr, Not(WhenDynamicCastTo<OtherDerived*>(Pointee(_)))); |
| 3204 | EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<OtherDerived*>(IsNull())); |
| 3205 | as_base_ptr = &other_derived; |
| 3206 | EXPECT_THAT(as_base_ptr, Not(WhenDynamicCastTo<Derived*>(Pointee(_)))); |
| 3207 | EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(IsNull())); |
| 3208 | } |
| 3209 | |
| 3210 | TEST(WhenDynamicCastToTest, AlreadyNull) { |
| 3211 | // Already NULL. |
| 3212 | Base* as_base_ptr = NULL; |
| 3213 | EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(IsNull())); |
| 3214 | } |
| 3215 | |
| 3216 | struct AmbiguousCastTypes { |
| 3217 | class VirtualDerived : public virtual Base {}; |
| 3218 | class DerivedSub1 : public VirtualDerived {}; |
| 3219 | class DerivedSub2 : public VirtualDerived {}; |
| 3220 | class ManyDerivedInHierarchy : public DerivedSub1, public DerivedSub2 {}; |
| 3221 | }; |
| 3222 | |
| 3223 | TEST(WhenDynamicCastToTest, AmbiguousCast) { |
| 3224 | AmbiguousCastTypes::DerivedSub1 sub1; |
| 3225 | AmbiguousCastTypes::ManyDerivedInHierarchy many_derived; |
| 3226 | // Multiply derived from Base. dynamic_cast<> returns NULL. |
| 3227 | Base* as_base_ptr = |
| 3228 | static_cast<AmbiguousCastTypes::DerivedSub1*>(&many_derived); |
| 3229 | EXPECT_THAT(as_base_ptr, |
| 3230 | WhenDynamicCastTo<AmbiguousCastTypes::VirtualDerived*>(IsNull())); |
| 3231 | as_base_ptr = &sub1; |
| 3232 | EXPECT_THAT( |
| 3233 | as_base_ptr, |
| 3234 | WhenDynamicCastTo<AmbiguousCastTypes::VirtualDerived*>(Not(IsNull()))); |
| 3235 | } |
| 3236 | |
| 3237 | TEST(WhenDynamicCastToTest, Describe) { |
| 3238 | Matcher<Base*> matcher = WhenDynamicCastTo<Derived*>(Pointee(_)); |
| 3239 | #if GTEST_HAS_RTTI |
| 3240 | const string prefix = |
| 3241 | "when dynamic_cast to " + internal::GetTypeName<Derived*>() + ", "; |
| 3242 | #else // GTEST_HAS_RTTI |
| 3243 | const string prefix = "when dynamic_cast, "; |
| 3244 | #endif // GTEST_HAS_RTTI |
| 3245 | EXPECT_EQ(prefix + "points to a value that is anything", Describe(matcher)); |
| 3246 | EXPECT_EQ(prefix + "does not point to a value that is anything", |
| 3247 | DescribeNegation(matcher)); |
| 3248 | } |
| 3249 | |
| 3250 | TEST(WhenDynamicCastToTest, Explain) { |
| 3251 | Matcher<Base*> matcher = WhenDynamicCastTo<Derived*>(Pointee(_)); |
| 3252 | Base* null = NULL; |
| 3253 | EXPECT_THAT(Explain(matcher, null), HasSubstr("NULL")); |
| 3254 | Derived derived; |
| 3255 | EXPECT_TRUE(matcher.Matches(&derived)); |
| 3256 | EXPECT_THAT(Explain(matcher, &derived), HasSubstr("which points to ")); |
| 3257 | |
| 3258 | // With references, the matcher itself can fail. Test for that one. |
| 3259 | Matcher<const Base&> ref_matcher = WhenDynamicCastTo<const OtherDerived&>(_); |
| 3260 | EXPECT_THAT(Explain(ref_matcher, derived), |
| 3261 | HasSubstr("which cannot be dynamic_cast")); |
| 3262 | } |
| 3263 | |
| 3264 | TEST(WhenDynamicCastToTest, GoodReference) { |
| 3265 | Derived derived; |
| 3266 | derived.i = 4; |
| 3267 | Base& as_base_ref = derived; |
| 3268 | EXPECT_THAT(as_base_ref, WhenDynamicCastTo<const Derived&>(FieldIIs(4))); |
| 3269 | EXPECT_THAT(as_base_ref, WhenDynamicCastTo<const Derived&>(Not(FieldIIs(5)))); |
| 3270 | } |
| 3271 | |
| 3272 | TEST(WhenDynamicCastToTest, BadReference) { |
| 3273 | Derived derived; |
| 3274 | Base& as_base_ref = derived; |
| 3275 | EXPECT_THAT(as_base_ref, Not(WhenDynamicCastTo<const OtherDerived&>(_))); |
| 3276 | } |
| 3277 | |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 3278 | // Minimal const-propagating pointer. |
| 3279 | template <typename T> |
| 3280 | class ConstPropagatingPtr { |
| 3281 | public: |
| 3282 | typedef T element_type; |
| 3283 | |
| 3284 | ConstPropagatingPtr() : val_() {} |
| 3285 | explicit ConstPropagatingPtr(T* t) : val_(t) {} |
| 3286 | ConstPropagatingPtr(const ConstPropagatingPtr& other) : val_(other.val_) {} |
| 3287 | |
| 3288 | T* get() { return val_; } |
| 3289 | T& operator*() { return *val_; } |
| 3290 | // Most smart pointers return non-const T* and T& from the next methods. |
| 3291 | const T* get() const { return val_; } |
| 3292 | const T& operator*() const { return *val_; } |
| 3293 | |
| 3294 | private: |
| 3295 | T* val_; |
| 3296 | }; |
| 3297 | |
| 3298 | TEST(PointeeTest, WorksWithConstPropagatingPointers) { |
| 3299 | const Matcher< ConstPropagatingPtr<int> > m = Pointee(Lt(5)); |
| 3300 | int three = 3; |
| 3301 | const ConstPropagatingPtr<int> co(&three); |
| 3302 | ConstPropagatingPtr<int> o(&three); |
| 3303 | EXPECT_TRUE(m.Matches(o)); |
| 3304 | EXPECT_TRUE(m.Matches(co)); |
| 3305 | *o = 6; |
| 3306 | EXPECT_FALSE(m.Matches(o)); |
| 3307 | EXPECT_FALSE(m.Matches(ConstPropagatingPtr<int>())); |
| 3308 | } |
| 3309 | |
| 3310 | TEST(PointeeTest, NeverMatchesNull) { |
| 3311 | const Matcher<const char*> m = Pointee(_); |
| 3312 | EXPECT_FALSE(m.Matches(NULL)); |
| 3313 | } |
| 3314 | |
| 3315 | // Tests that we can write Pointee(value) instead of Pointee(Eq(value)). |
| 3316 | TEST(PointeeTest, MatchesAgainstAValue) { |
| 3317 | const Matcher<int*> m = Pointee(5); |
| 3318 | |
| 3319 | int n = 5; |
| 3320 | EXPECT_TRUE(m.Matches(&n)); |
| 3321 | n = -1; |
| 3322 | EXPECT_FALSE(m.Matches(&n)); |
| 3323 | EXPECT_FALSE(m.Matches(NULL)); |
| 3324 | } |
| 3325 | |
| 3326 | TEST(PointeeTest, CanDescribeSelf) { |
| 3327 | const Matcher<int*> m = Pointee(Gt(3)); |
| 3328 | EXPECT_EQ("points to a value that is > 3", Describe(m)); |
| 3329 | EXPECT_EQ("does not point to a value that is > 3", |
| 3330 | DescribeNegation(m)); |
| 3331 | } |
| 3332 | |
| 3333 | TEST(PointeeTest, CanExplainMatchResult) { |
| 3334 | const Matcher<const string*> m = Pointee(StartsWith("Hi")); |
| 3335 | |
| 3336 | EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL))); |
| 3337 | |
| 3338 | const Matcher<long*> m2 = Pointee(GreaterThan(1)); // NOLINT |
| 3339 | long n = 3; // NOLINT |
| 3340 | EXPECT_EQ("which points to 3" + OfType("long") + ", which is 2 more than 1", |
| 3341 | Explain(m2, &n)); |
| 3342 | } |
| 3343 | |
| 3344 | TEST(PointeeTest, AlwaysExplainsPointee) { |
| 3345 | const Matcher<int*> m = Pointee(0); |
| 3346 | int n = 42; |
| 3347 | EXPECT_EQ("which points to 42" + OfType("int"), Explain(m, &n)); |
| 3348 | } |
| 3349 | |
| 3350 | // An uncopyable class. |
| 3351 | class Uncopyable { |
| 3352 | public: |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 3353 | Uncopyable() : value_(-1) {} |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 3354 | explicit Uncopyable(int a_value) : value_(a_value) {} |
| 3355 | |
| 3356 | int value() const { return value_; } |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 3357 | void set_value(int i) { value_ = i; } |
| 3358 | |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 3359 | private: |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 3360 | int value_; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 3361 | GTEST_DISALLOW_COPY_AND_ASSIGN_(Uncopyable); |
| 3362 | }; |
| 3363 | |
| 3364 | // Returns true iff x.value() is positive. |
| 3365 | bool ValueIsPositive(const Uncopyable& x) { return x.value() > 0; } |
| 3366 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 3367 | MATCHER_P(UncopyableIs, inner_matcher, "") { |
| 3368 | return ExplainMatchResult(inner_matcher, arg.value(), result_listener); |
| 3369 | } |
| 3370 | |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 3371 | // A user-defined struct for testing Field(). |
| 3372 | struct AStruct { |
| 3373 | AStruct() : x(0), y(1.0), z(5), p(NULL) {} |
| 3374 | AStruct(const AStruct& rhs) |
| 3375 | : x(rhs.x), y(rhs.y), z(rhs.z.value()), p(rhs.p) {} |
| 3376 | |
| 3377 | int x; // A non-const field. |
| 3378 | const double y; // A const field. |
| 3379 | Uncopyable z; // An uncopyable field. |
| 3380 | const char* p; // A pointer field. |
| 3381 | |
| 3382 | private: |
| 3383 | GTEST_DISALLOW_ASSIGN_(AStruct); |
| 3384 | }; |
| 3385 | |
| 3386 | // A derived struct for testing Field(). |
| 3387 | struct DerivedStruct : public AStruct { |
| 3388 | char ch; |
| 3389 | |
| 3390 | private: |
| 3391 | GTEST_DISALLOW_ASSIGN_(DerivedStruct); |
| 3392 | }; |
| 3393 | |
| 3394 | // Tests that Field(&Foo::field, ...) works when field is non-const. |
| 3395 | TEST(FieldTest, WorksForNonConstField) { |
| 3396 | Matcher<AStruct> m = Field(&AStruct::x, Ge(0)); |
| 3397 | |
| 3398 | AStruct a; |
| 3399 | EXPECT_TRUE(m.Matches(a)); |
| 3400 | a.x = -1; |
| 3401 | EXPECT_FALSE(m.Matches(a)); |
| 3402 | } |
| 3403 | |
| 3404 | // Tests that Field(&Foo::field, ...) works when field is const. |
| 3405 | TEST(FieldTest, WorksForConstField) { |
| 3406 | AStruct a; |
| 3407 | |
| 3408 | Matcher<AStruct> m = Field(&AStruct::y, Ge(0.0)); |
| 3409 | EXPECT_TRUE(m.Matches(a)); |
| 3410 | m = Field(&AStruct::y, Le(0.0)); |
| 3411 | EXPECT_FALSE(m.Matches(a)); |
| 3412 | } |
| 3413 | |
| 3414 | // Tests that Field(&Foo::field, ...) works when field is not copyable. |
| 3415 | TEST(FieldTest, WorksForUncopyableField) { |
| 3416 | AStruct a; |
| 3417 | |
| 3418 | Matcher<AStruct> m = Field(&AStruct::z, Truly(ValueIsPositive)); |
| 3419 | EXPECT_TRUE(m.Matches(a)); |
| 3420 | m = Field(&AStruct::z, Not(Truly(ValueIsPositive))); |
| 3421 | EXPECT_FALSE(m.Matches(a)); |
| 3422 | } |
| 3423 | |
| 3424 | // Tests that Field(&Foo::field, ...) works when field is a pointer. |
| 3425 | TEST(FieldTest, WorksForPointerField) { |
| 3426 | // Matching against NULL. |
| 3427 | Matcher<AStruct> m = Field(&AStruct::p, static_cast<const char*>(NULL)); |
| 3428 | AStruct a; |
| 3429 | EXPECT_TRUE(m.Matches(a)); |
| 3430 | a.p = "hi"; |
| 3431 | EXPECT_FALSE(m.Matches(a)); |
| 3432 | |
| 3433 | // Matching a pointer that is not NULL. |
| 3434 | m = Field(&AStruct::p, StartsWith("hi")); |
| 3435 | a.p = "hill"; |
| 3436 | EXPECT_TRUE(m.Matches(a)); |
| 3437 | a.p = "hole"; |
| 3438 | EXPECT_FALSE(m.Matches(a)); |
| 3439 | } |
| 3440 | |
| 3441 | // Tests that Field() works when the object is passed by reference. |
| 3442 | TEST(FieldTest, WorksForByRefArgument) { |
| 3443 | Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0)); |
| 3444 | |
| 3445 | AStruct a; |
| 3446 | EXPECT_TRUE(m.Matches(a)); |
| 3447 | a.x = -1; |
| 3448 | EXPECT_FALSE(m.Matches(a)); |
| 3449 | } |
| 3450 | |
| 3451 | // Tests that Field(&Foo::field, ...) works when the argument's type |
| 3452 | // is a sub-type of Foo. |
| 3453 | TEST(FieldTest, WorksForArgumentOfSubType) { |
| 3454 | // Note that the matcher expects DerivedStruct but we say AStruct |
| 3455 | // inside Field(). |
| 3456 | Matcher<const DerivedStruct&> m = Field(&AStruct::x, Ge(0)); |
| 3457 | |
| 3458 | DerivedStruct d; |
| 3459 | EXPECT_TRUE(m.Matches(d)); |
| 3460 | d.x = -1; |
| 3461 | EXPECT_FALSE(m.Matches(d)); |
| 3462 | } |
| 3463 | |
| 3464 | // Tests that Field(&Foo::field, m) works when field's type and m's |
| 3465 | // argument type are compatible but not the same. |
| 3466 | TEST(FieldTest, WorksForCompatibleMatcherType) { |
| 3467 | // The field is an int, but the inner matcher expects a signed char. |
| 3468 | Matcher<const AStruct&> m = Field(&AStruct::x, |
| 3469 | Matcher<signed char>(Ge(0))); |
| 3470 | |
| 3471 | AStruct a; |
| 3472 | EXPECT_TRUE(m.Matches(a)); |
| 3473 | a.x = -1; |
| 3474 | EXPECT_FALSE(m.Matches(a)); |
| 3475 | } |
| 3476 | |
| 3477 | // Tests that Field() can describe itself. |
| 3478 | TEST(FieldTest, CanDescribeSelf) { |
| 3479 | Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0)); |
| 3480 | |
| 3481 | EXPECT_EQ("is an object whose given field is >= 0", Describe(m)); |
| 3482 | EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m)); |
| 3483 | } |
| 3484 | |
| 3485 | // Tests that Field() can explain the match result. |
| 3486 | TEST(FieldTest, CanExplainMatchResult) { |
| 3487 | Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0)); |
| 3488 | |
| 3489 | AStruct a; |
| 3490 | a.x = 1; |
| 3491 | EXPECT_EQ("whose given field is 1" + OfType("int"), Explain(m, a)); |
| 3492 | |
| 3493 | m = Field(&AStruct::x, GreaterThan(0)); |
| 3494 | EXPECT_EQ( |
| 3495 | "whose given field is 1" + OfType("int") + ", which is 1 more than 0", |
| 3496 | Explain(m, a)); |
| 3497 | } |
| 3498 | |
| 3499 | // Tests that Field() works when the argument is a pointer to const. |
| 3500 | TEST(FieldForPointerTest, WorksForPointerToConst) { |
| 3501 | Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0)); |
| 3502 | |
| 3503 | AStruct a; |
| 3504 | EXPECT_TRUE(m.Matches(&a)); |
| 3505 | a.x = -1; |
| 3506 | EXPECT_FALSE(m.Matches(&a)); |
| 3507 | } |
| 3508 | |
| 3509 | // Tests that Field() works when the argument is a pointer to non-const. |
| 3510 | TEST(FieldForPointerTest, WorksForPointerToNonConst) { |
| 3511 | Matcher<AStruct*> m = Field(&AStruct::x, Ge(0)); |
| 3512 | |
| 3513 | AStruct a; |
| 3514 | EXPECT_TRUE(m.Matches(&a)); |
| 3515 | a.x = -1; |
| 3516 | EXPECT_FALSE(m.Matches(&a)); |
| 3517 | } |
| 3518 | |
| 3519 | // Tests that Field() works when the argument is a reference to a const pointer. |
| 3520 | TEST(FieldForPointerTest, WorksForReferenceToConstPointer) { |
| 3521 | Matcher<AStruct* const&> m = Field(&AStruct::x, Ge(0)); |
| 3522 | |
| 3523 | AStruct a; |
| 3524 | EXPECT_TRUE(m.Matches(&a)); |
| 3525 | a.x = -1; |
| 3526 | EXPECT_FALSE(m.Matches(&a)); |
| 3527 | } |
| 3528 | |
| 3529 | // Tests that Field() does not match the NULL pointer. |
| 3530 | TEST(FieldForPointerTest, DoesNotMatchNull) { |
| 3531 | Matcher<const AStruct*> m = Field(&AStruct::x, _); |
| 3532 | EXPECT_FALSE(m.Matches(NULL)); |
| 3533 | } |
| 3534 | |
| 3535 | // Tests that Field(&Foo::field, ...) works when the argument's type |
| 3536 | // is a sub-type of const Foo*. |
| 3537 | TEST(FieldForPointerTest, WorksForArgumentOfSubType) { |
| 3538 | // Note that the matcher expects DerivedStruct but we say AStruct |
| 3539 | // inside Field(). |
| 3540 | Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0)); |
| 3541 | |
| 3542 | DerivedStruct d; |
| 3543 | EXPECT_TRUE(m.Matches(&d)); |
| 3544 | d.x = -1; |
| 3545 | EXPECT_FALSE(m.Matches(&d)); |
| 3546 | } |
| 3547 | |
| 3548 | // Tests that Field() can describe itself when used to match a pointer. |
| 3549 | TEST(FieldForPointerTest, CanDescribeSelf) { |
| 3550 | Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0)); |
| 3551 | |
| 3552 | EXPECT_EQ("is an object whose given field is >= 0", Describe(m)); |
| 3553 | EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m)); |
| 3554 | } |
| 3555 | |
| 3556 | // Tests that Field() can explain the result of matching a pointer. |
| 3557 | TEST(FieldForPointerTest, CanExplainMatchResult) { |
| 3558 | Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0)); |
| 3559 | |
| 3560 | AStruct a; |
| 3561 | a.x = 1; |
| 3562 | EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(NULL))); |
| 3563 | EXPECT_EQ("which points to an object whose given field is 1" + OfType("int"), |
| 3564 | Explain(m, &a)); |
| 3565 | |
| 3566 | m = Field(&AStruct::x, GreaterThan(0)); |
| 3567 | EXPECT_EQ("which points to an object whose given field is 1" + OfType("int") + |
| 3568 | ", which is 1 more than 0", Explain(m, &a)); |
| 3569 | } |
| 3570 | |
| 3571 | // A user-defined class for testing Property(). |
| 3572 | class AClass { |
| 3573 | public: |
| 3574 | AClass() : n_(0) {} |
| 3575 | |
| 3576 | // A getter that returns a non-reference. |
| 3577 | int n() const { return n_; } |
| 3578 | |
| 3579 | void set_n(int new_n) { n_ = new_n; } |
| 3580 | |
| 3581 | // A getter that returns a reference to const. |
| 3582 | const string& s() const { return s_; } |
| 3583 | |
| 3584 | void set_s(const string& new_s) { s_ = new_s; } |
| 3585 | |
| 3586 | // A getter that returns a reference to non-const. |
| 3587 | double& x() const { return x_; } |
| 3588 | private: |
| 3589 | int n_; |
| 3590 | string s_; |
| 3591 | |
| 3592 | static double x_; |
| 3593 | }; |
| 3594 | |
| 3595 | double AClass::x_ = 0.0; |
| 3596 | |
| 3597 | // A derived class for testing Property(). |
| 3598 | class DerivedClass : public AClass { |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 3599 | public: |
| 3600 | int k() const { return k_; } |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 3601 | private: |
| 3602 | int k_; |
| 3603 | }; |
| 3604 | |
| 3605 | // Tests that Property(&Foo::property, ...) works when property() |
| 3606 | // returns a non-reference. |
| 3607 | TEST(PropertyTest, WorksForNonReferenceProperty) { |
| 3608 | Matcher<const AClass&> m = Property(&AClass::n, Ge(0)); |
| 3609 | |
| 3610 | AClass a; |
| 3611 | a.set_n(1); |
| 3612 | EXPECT_TRUE(m.Matches(a)); |
| 3613 | |
| 3614 | a.set_n(-1); |
| 3615 | EXPECT_FALSE(m.Matches(a)); |
| 3616 | } |
| 3617 | |
| 3618 | // Tests that Property(&Foo::property, ...) works when property() |
| 3619 | // returns a reference to const. |
| 3620 | TEST(PropertyTest, WorksForReferenceToConstProperty) { |
| 3621 | Matcher<const AClass&> m = Property(&AClass::s, StartsWith("hi")); |
| 3622 | |
| 3623 | AClass a; |
| 3624 | a.set_s("hill"); |
| 3625 | EXPECT_TRUE(m.Matches(a)); |
| 3626 | |
| 3627 | a.set_s("hole"); |
| 3628 | EXPECT_FALSE(m.Matches(a)); |
| 3629 | } |
| 3630 | |
| 3631 | // Tests that Property(&Foo::property, ...) works when property() |
| 3632 | // returns a reference to non-const. |
| 3633 | TEST(PropertyTest, WorksForReferenceToNonConstProperty) { |
| 3634 | double x = 0.0; |
| 3635 | AClass a; |
| 3636 | |
| 3637 | Matcher<const AClass&> m = Property(&AClass::x, Ref(x)); |
| 3638 | EXPECT_FALSE(m.Matches(a)); |
| 3639 | |
| 3640 | m = Property(&AClass::x, Not(Ref(x))); |
| 3641 | EXPECT_TRUE(m.Matches(a)); |
| 3642 | } |
| 3643 | |
| 3644 | // Tests that Property(&Foo::property, ...) works when the argument is |
| 3645 | // passed by value. |
| 3646 | TEST(PropertyTest, WorksForByValueArgument) { |
| 3647 | Matcher<AClass> m = Property(&AClass::s, StartsWith("hi")); |
| 3648 | |
| 3649 | AClass a; |
| 3650 | a.set_s("hill"); |
| 3651 | EXPECT_TRUE(m.Matches(a)); |
| 3652 | |
| 3653 | a.set_s("hole"); |
| 3654 | EXPECT_FALSE(m.Matches(a)); |
| 3655 | } |
| 3656 | |
| 3657 | // Tests that Property(&Foo::property, ...) works when the argument's |
| 3658 | // type is a sub-type of Foo. |
| 3659 | TEST(PropertyTest, WorksForArgumentOfSubType) { |
| 3660 | // The matcher expects a DerivedClass, but inside the Property() we |
| 3661 | // say AClass. |
| 3662 | Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0)); |
| 3663 | |
| 3664 | DerivedClass d; |
| 3665 | d.set_n(1); |
| 3666 | EXPECT_TRUE(m.Matches(d)); |
| 3667 | |
| 3668 | d.set_n(-1); |
| 3669 | EXPECT_FALSE(m.Matches(d)); |
| 3670 | } |
| 3671 | |
| 3672 | // Tests that Property(&Foo::property, m) works when property()'s type |
| 3673 | // and m's argument type are compatible but different. |
| 3674 | TEST(PropertyTest, WorksForCompatibleMatcherType) { |
| 3675 | // n() returns an int but the inner matcher expects a signed char. |
| 3676 | Matcher<const AClass&> m = Property(&AClass::n, |
| 3677 | Matcher<signed char>(Ge(0))); |
| 3678 | |
| 3679 | AClass a; |
| 3680 | EXPECT_TRUE(m.Matches(a)); |
| 3681 | a.set_n(-1); |
| 3682 | EXPECT_FALSE(m.Matches(a)); |
| 3683 | } |
| 3684 | |
| 3685 | // Tests that Property() can describe itself. |
| 3686 | TEST(PropertyTest, CanDescribeSelf) { |
| 3687 | Matcher<const AClass&> m = Property(&AClass::n, Ge(0)); |
| 3688 | |
| 3689 | EXPECT_EQ("is an object whose given property is >= 0", Describe(m)); |
| 3690 | EXPECT_EQ("is an object whose given property isn't >= 0", |
| 3691 | DescribeNegation(m)); |
| 3692 | } |
| 3693 | |
| 3694 | // Tests that Property() can explain the match result. |
| 3695 | TEST(PropertyTest, CanExplainMatchResult) { |
| 3696 | Matcher<const AClass&> m = Property(&AClass::n, Ge(0)); |
| 3697 | |
| 3698 | AClass a; |
| 3699 | a.set_n(1); |
| 3700 | EXPECT_EQ("whose given property is 1" + OfType("int"), Explain(m, a)); |
| 3701 | |
| 3702 | m = Property(&AClass::n, GreaterThan(0)); |
| 3703 | EXPECT_EQ( |
| 3704 | "whose given property is 1" + OfType("int") + ", which is 1 more than 0", |
| 3705 | Explain(m, a)); |
| 3706 | } |
| 3707 | |
| 3708 | // Tests that Property() works when the argument is a pointer to const. |
| 3709 | TEST(PropertyForPointerTest, WorksForPointerToConst) { |
| 3710 | Matcher<const AClass*> m = Property(&AClass::n, Ge(0)); |
| 3711 | |
| 3712 | AClass a; |
| 3713 | a.set_n(1); |
| 3714 | EXPECT_TRUE(m.Matches(&a)); |
| 3715 | |
| 3716 | a.set_n(-1); |
| 3717 | EXPECT_FALSE(m.Matches(&a)); |
| 3718 | } |
| 3719 | |
| 3720 | // Tests that Property() works when the argument is a pointer to non-const. |
| 3721 | TEST(PropertyForPointerTest, WorksForPointerToNonConst) { |
| 3722 | Matcher<AClass*> m = Property(&AClass::s, StartsWith("hi")); |
| 3723 | |
| 3724 | AClass a; |
| 3725 | a.set_s("hill"); |
| 3726 | EXPECT_TRUE(m.Matches(&a)); |
| 3727 | |
| 3728 | a.set_s("hole"); |
| 3729 | EXPECT_FALSE(m.Matches(&a)); |
| 3730 | } |
| 3731 | |
| 3732 | // Tests that Property() works when the argument is a reference to a |
| 3733 | // const pointer. |
| 3734 | TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) { |
| 3735 | Matcher<AClass* const&> m = Property(&AClass::s, StartsWith("hi")); |
| 3736 | |
| 3737 | AClass a; |
| 3738 | a.set_s("hill"); |
| 3739 | EXPECT_TRUE(m.Matches(&a)); |
| 3740 | |
| 3741 | a.set_s("hole"); |
| 3742 | EXPECT_FALSE(m.Matches(&a)); |
| 3743 | } |
| 3744 | |
| 3745 | // Tests that Property() does not match the NULL pointer. |
| 3746 | TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) { |
| 3747 | Matcher<const AClass*> m = Property(&AClass::x, _); |
| 3748 | EXPECT_FALSE(m.Matches(NULL)); |
| 3749 | } |
| 3750 | |
| 3751 | // Tests that Property(&Foo::property, ...) works when the argument's |
| 3752 | // type is a sub-type of const Foo*. |
| 3753 | TEST(PropertyForPointerTest, WorksForArgumentOfSubType) { |
| 3754 | // The matcher expects a DerivedClass, but inside the Property() we |
| 3755 | // say AClass. |
| 3756 | Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0)); |
| 3757 | |
| 3758 | DerivedClass d; |
| 3759 | d.set_n(1); |
| 3760 | EXPECT_TRUE(m.Matches(&d)); |
| 3761 | |
| 3762 | d.set_n(-1); |
| 3763 | EXPECT_FALSE(m.Matches(&d)); |
| 3764 | } |
| 3765 | |
| 3766 | // Tests that Property() can describe itself when used to match a pointer. |
| 3767 | TEST(PropertyForPointerTest, CanDescribeSelf) { |
| 3768 | Matcher<const AClass*> m = Property(&AClass::n, Ge(0)); |
| 3769 | |
| 3770 | EXPECT_EQ("is an object whose given property is >= 0", Describe(m)); |
| 3771 | EXPECT_EQ("is an object whose given property isn't >= 0", |
| 3772 | DescribeNegation(m)); |
| 3773 | } |
| 3774 | |
| 3775 | // Tests that Property() can explain the result of matching a pointer. |
| 3776 | TEST(PropertyForPointerTest, CanExplainMatchResult) { |
| 3777 | Matcher<const AClass*> m = Property(&AClass::n, Ge(0)); |
| 3778 | |
| 3779 | AClass a; |
| 3780 | a.set_n(1); |
| 3781 | EXPECT_EQ("", Explain(m, static_cast<const AClass*>(NULL))); |
| 3782 | EXPECT_EQ( |
| 3783 | "which points to an object whose given property is 1" + OfType("int"), |
| 3784 | Explain(m, &a)); |
| 3785 | |
| 3786 | m = Property(&AClass::n, GreaterThan(0)); |
| 3787 | EXPECT_EQ("which points to an object whose given property is 1" + |
| 3788 | OfType("int") + ", which is 1 more than 0", |
| 3789 | Explain(m, &a)); |
| 3790 | } |
| 3791 | |
| 3792 | // Tests ResultOf. |
| 3793 | |
| 3794 | // Tests that ResultOf(f, ...) compiles and works as expected when f is a |
| 3795 | // function pointer. |
| 3796 | string IntToStringFunction(int input) { return input == 1 ? "foo" : "bar"; } |
| 3797 | |
| 3798 | TEST(ResultOfTest, WorksForFunctionPointers) { |
| 3799 | Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(string("foo"))); |
| 3800 | |
| 3801 | EXPECT_TRUE(matcher.Matches(1)); |
| 3802 | EXPECT_FALSE(matcher.Matches(2)); |
| 3803 | } |
| 3804 | |
| 3805 | // Tests that ResultOf() can describe itself. |
| 3806 | TEST(ResultOfTest, CanDescribeItself) { |
| 3807 | Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo")); |
| 3808 | |
| 3809 | EXPECT_EQ("is mapped by the given callable to a value that " |
| 3810 | "is equal to \"foo\"", Describe(matcher)); |
| 3811 | EXPECT_EQ("is mapped by the given callable to a value that " |
| 3812 | "isn't equal to \"foo\"", DescribeNegation(matcher)); |
| 3813 | } |
| 3814 | |
| 3815 | // Tests that ResultOf() can explain the match result. |
| 3816 | int IntFunction(int input) { return input == 42 ? 80 : 90; } |
| 3817 | |
| 3818 | TEST(ResultOfTest, CanExplainMatchResult) { |
| 3819 | Matcher<int> matcher = ResultOf(&IntFunction, Ge(85)); |
| 3820 | EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int"), |
| 3821 | Explain(matcher, 36)); |
| 3822 | |
| 3823 | matcher = ResultOf(&IntFunction, GreaterThan(85)); |
| 3824 | EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int") + |
| 3825 | ", which is 5 more than 85", Explain(matcher, 36)); |
| 3826 | } |
| 3827 | |
| 3828 | // Tests that ResultOf(f, ...) compiles and works as expected when f(x) |
| 3829 | // returns a non-reference. |
| 3830 | TEST(ResultOfTest, WorksForNonReferenceResults) { |
| 3831 | Matcher<int> matcher = ResultOf(&IntFunction, Eq(80)); |
| 3832 | |
| 3833 | EXPECT_TRUE(matcher.Matches(42)); |
| 3834 | EXPECT_FALSE(matcher.Matches(36)); |
| 3835 | } |
| 3836 | |
| 3837 | // Tests that ResultOf(f, ...) compiles and works as expected when f(x) |
| 3838 | // returns a reference to non-const. |
| 3839 | double& DoubleFunction(double& input) { return input; } // NOLINT |
| 3840 | |
| 3841 | Uncopyable& RefUncopyableFunction(Uncopyable& obj) { // NOLINT |
| 3842 | return obj; |
| 3843 | } |
| 3844 | |
| 3845 | TEST(ResultOfTest, WorksForReferenceToNonConstResults) { |
| 3846 | double x = 3.14; |
| 3847 | double x2 = x; |
| 3848 | Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(x)); |
| 3849 | |
| 3850 | EXPECT_TRUE(matcher.Matches(x)); |
| 3851 | EXPECT_FALSE(matcher.Matches(x2)); |
| 3852 | |
| 3853 | // Test that ResultOf works with uncopyable objects |
| 3854 | Uncopyable obj(0); |
| 3855 | Uncopyable obj2(0); |
| 3856 | Matcher<Uncopyable&> matcher2 = |
| 3857 | ResultOf(&RefUncopyableFunction, Ref(obj)); |
| 3858 | |
| 3859 | EXPECT_TRUE(matcher2.Matches(obj)); |
| 3860 | EXPECT_FALSE(matcher2.Matches(obj2)); |
| 3861 | } |
| 3862 | |
| 3863 | // Tests that ResultOf(f, ...) compiles and works as expected when f(x) |
| 3864 | // returns a reference to const. |
| 3865 | const string& StringFunction(const string& input) { return input; } |
| 3866 | |
| 3867 | TEST(ResultOfTest, WorksForReferenceToConstResults) { |
| 3868 | string s = "foo"; |
| 3869 | string s2 = s; |
| 3870 | Matcher<const string&> matcher = ResultOf(&StringFunction, Ref(s)); |
| 3871 | |
| 3872 | EXPECT_TRUE(matcher.Matches(s)); |
| 3873 | EXPECT_FALSE(matcher.Matches(s2)); |
| 3874 | } |
| 3875 | |
| 3876 | // Tests that ResultOf(f, m) works when f(x) and m's |
| 3877 | // argument types are compatible but different. |
| 3878 | TEST(ResultOfTest, WorksForCompatibleMatcherTypes) { |
| 3879 | // IntFunction() returns int but the inner matcher expects a signed char. |
| 3880 | Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85))); |
| 3881 | |
| 3882 | EXPECT_TRUE(matcher.Matches(36)); |
| 3883 | EXPECT_FALSE(matcher.Matches(42)); |
| 3884 | } |
| 3885 | |
| 3886 | // Tests that the program aborts when ResultOf is passed |
| 3887 | // a NULL function pointer. |
| 3888 | TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) { |
| 3889 | EXPECT_DEATH_IF_SUPPORTED( |
| 3890 | ResultOf(static_cast<string(*)(int dummy)>(NULL), Eq(string("foo"))), |
| 3891 | "NULL function pointer is passed into ResultOf\\(\\)\\."); |
| 3892 | } |
| 3893 | |
| 3894 | // Tests that ResultOf(f, ...) compiles and works as expected when f is a |
| 3895 | // function reference. |
| 3896 | TEST(ResultOfTest, WorksForFunctionReferences) { |
| 3897 | Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq("foo")); |
| 3898 | EXPECT_TRUE(matcher.Matches(1)); |
| 3899 | EXPECT_FALSE(matcher.Matches(2)); |
| 3900 | } |
| 3901 | |
| 3902 | // Tests that ResultOf(f, ...) compiles and works as expected when f is a |
| 3903 | // function object. |
| 3904 | struct Functor : public ::std::unary_function<int, string> { |
| 3905 | result_type operator()(argument_type input) const { |
| 3906 | return IntToStringFunction(input); |
| 3907 | } |
| 3908 | }; |
| 3909 | |
| 3910 | TEST(ResultOfTest, WorksForFunctors) { |
| 3911 | Matcher<int> matcher = ResultOf(Functor(), Eq(string("foo"))); |
| 3912 | |
| 3913 | EXPECT_TRUE(matcher.Matches(1)); |
| 3914 | EXPECT_FALSE(matcher.Matches(2)); |
| 3915 | } |
| 3916 | |
| 3917 | // Tests that ResultOf(f, ...) compiles and works as expected when f is a |
| 3918 | // functor with more then one operator() defined. ResultOf() must work |
| 3919 | // for each defined operator(). |
| 3920 | struct PolymorphicFunctor { |
| 3921 | typedef int result_type; |
| 3922 | int operator()(int n) { return n; } |
| 3923 | int operator()(const char* s) { return static_cast<int>(strlen(s)); } |
| 3924 | }; |
| 3925 | |
| 3926 | TEST(ResultOfTest, WorksForPolymorphicFunctors) { |
| 3927 | Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5)); |
| 3928 | |
| 3929 | EXPECT_TRUE(matcher_int.Matches(10)); |
| 3930 | EXPECT_FALSE(matcher_int.Matches(2)); |
| 3931 | |
| 3932 | Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5)); |
| 3933 | |
| 3934 | EXPECT_TRUE(matcher_string.Matches("long string")); |
| 3935 | EXPECT_FALSE(matcher_string.Matches("shrt")); |
| 3936 | } |
| 3937 | |
| 3938 | const int* ReferencingFunction(const int& n) { return &n; } |
| 3939 | |
| 3940 | struct ReferencingFunctor { |
| 3941 | typedef const int* result_type; |
| 3942 | result_type operator()(const int& n) { return &n; } |
| 3943 | }; |
| 3944 | |
| 3945 | TEST(ResultOfTest, WorksForReferencingCallables) { |
| 3946 | const int n = 1; |
| 3947 | const int n2 = 1; |
| 3948 | Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n)); |
| 3949 | EXPECT_TRUE(matcher2.Matches(n)); |
| 3950 | EXPECT_FALSE(matcher2.Matches(n2)); |
| 3951 | |
| 3952 | Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n)); |
| 3953 | EXPECT_TRUE(matcher3.Matches(n)); |
| 3954 | EXPECT_FALSE(matcher3.Matches(n2)); |
| 3955 | } |
| 3956 | |
| 3957 | class DivisibleByImpl { |
| 3958 | public: |
| 3959 | explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {} |
| 3960 | |
| 3961 | // For testing using ExplainMatchResultTo() with polymorphic matchers. |
| 3962 | template <typename T> |
| 3963 | bool MatchAndExplain(const T& n, MatchResultListener* listener) const { |
| 3964 | *listener << "which is " << (n % divider_) << " modulo " |
| 3965 | << divider_; |
| 3966 | return (n % divider_) == 0; |
| 3967 | } |
| 3968 | |
| 3969 | void DescribeTo(ostream* os) const { |
| 3970 | *os << "is divisible by " << divider_; |
| 3971 | } |
| 3972 | |
| 3973 | void DescribeNegationTo(ostream* os) const { |
| 3974 | *os << "is not divisible by " << divider_; |
| 3975 | } |
| 3976 | |
| 3977 | void set_divider(int a_divider) { divider_ = a_divider; } |
| 3978 | int divider() const { return divider_; } |
| 3979 | |
| 3980 | private: |
| 3981 | int divider_; |
| 3982 | }; |
| 3983 | |
| 3984 | PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) { |
| 3985 | return MakePolymorphicMatcher(DivisibleByImpl(n)); |
| 3986 | } |
| 3987 | |
| 3988 | // Tests that when AllOf() fails, only the first failing matcher is |
| 3989 | // asked to explain why. |
| 3990 | TEST(ExplainMatchResultTest, AllOf_False_False) { |
| 3991 | const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3)); |
| 3992 | EXPECT_EQ("which is 1 modulo 4", Explain(m, 5)); |
| 3993 | } |
| 3994 | |
| 3995 | // Tests that when AllOf() fails, only the first failing matcher is |
| 3996 | // asked to explain why. |
| 3997 | TEST(ExplainMatchResultTest, AllOf_False_True) { |
| 3998 | const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3)); |
| 3999 | EXPECT_EQ("which is 2 modulo 4", Explain(m, 6)); |
| 4000 | } |
| 4001 | |
| 4002 | // Tests that when AllOf() fails, only the first failing matcher is |
| 4003 | // asked to explain why. |
| 4004 | TEST(ExplainMatchResultTest, AllOf_True_False) { |
| 4005 | const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3)); |
| 4006 | EXPECT_EQ("which is 2 modulo 3", Explain(m, 5)); |
| 4007 | } |
| 4008 | |
| 4009 | // Tests that when AllOf() succeeds, all matchers are asked to explain |
| 4010 | // why. |
| 4011 | TEST(ExplainMatchResultTest, AllOf_True_True) { |
| 4012 | const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3)); |
| 4013 | EXPECT_EQ("which is 0 modulo 2, and which is 0 modulo 3", Explain(m, 6)); |
| 4014 | } |
| 4015 | |
| 4016 | TEST(ExplainMatchResultTest, AllOf_True_True_2) { |
| 4017 | const Matcher<int> m = AllOf(Ge(2), Le(3)); |
| 4018 | EXPECT_EQ("", Explain(m, 2)); |
| 4019 | } |
| 4020 | |
| 4021 | TEST(ExplainmatcherResultTest, MonomorphicMatcher) { |
| 4022 | const Matcher<int> m = GreaterThan(5); |
| 4023 | EXPECT_EQ("which is 1 more than 5", Explain(m, 6)); |
| 4024 | } |
| 4025 | |
| 4026 | // The following two tests verify that values without a public copy |
| 4027 | // ctor can be used as arguments to matchers like Eq(), Ge(), and etc |
| 4028 | // with the help of ByRef(). |
| 4029 | |
| 4030 | class NotCopyable { |
| 4031 | public: |
| 4032 | explicit NotCopyable(int a_value) : value_(a_value) {} |
| 4033 | |
| 4034 | int value() const { return value_; } |
| 4035 | |
| 4036 | bool operator==(const NotCopyable& rhs) const { |
| 4037 | return value() == rhs.value(); |
| 4038 | } |
| 4039 | |
| 4040 | bool operator>=(const NotCopyable& rhs) const { |
| 4041 | return value() >= rhs.value(); |
| 4042 | } |
| 4043 | private: |
| 4044 | int value_; |
| 4045 | |
| 4046 | GTEST_DISALLOW_COPY_AND_ASSIGN_(NotCopyable); |
| 4047 | }; |
| 4048 | |
| 4049 | TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) { |
| 4050 | const NotCopyable const_value1(1); |
| 4051 | const Matcher<const NotCopyable&> m = Eq(ByRef(const_value1)); |
| 4052 | |
| 4053 | const NotCopyable n1(1), n2(2); |
| 4054 | EXPECT_TRUE(m.Matches(n1)); |
| 4055 | EXPECT_FALSE(m.Matches(n2)); |
| 4056 | } |
| 4057 | |
| 4058 | TEST(ByRefTest, AllowsNotCopyableValueInMatchers) { |
| 4059 | NotCopyable value2(2); |
| 4060 | const Matcher<NotCopyable&> m = Ge(ByRef(value2)); |
| 4061 | |
| 4062 | NotCopyable n1(1), n2(2); |
| 4063 | EXPECT_FALSE(m.Matches(n1)); |
| 4064 | EXPECT_TRUE(m.Matches(n2)); |
| 4065 | } |
| 4066 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 4067 | TEST(IsEmptyTest, ImplementsIsEmpty) { |
| 4068 | vector<int> container; |
| 4069 | EXPECT_THAT(container, IsEmpty()); |
| 4070 | container.push_back(0); |
| 4071 | EXPECT_THAT(container, Not(IsEmpty())); |
| 4072 | container.push_back(1); |
| 4073 | EXPECT_THAT(container, Not(IsEmpty())); |
| 4074 | } |
| 4075 | |
| 4076 | TEST(IsEmptyTest, WorksWithString) { |
| 4077 | string text; |
| 4078 | EXPECT_THAT(text, IsEmpty()); |
| 4079 | text = "foo"; |
| 4080 | EXPECT_THAT(text, Not(IsEmpty())); |
| 4081 | text = string("\0", 1); |
| 4082 | EXPECT_THAT(text, Not(IsEmpty())); |
| 4083 | } |
| 4084 | |
| 4085 | TEST(IsEmptyTest, CanDescribeSelf) { |
| 4086 | Matcher<vector<int> > m = IsEmpty(); |
| 4087 | EXPECT_EQ("is empty", Describe(m)); |
| 4088 | EXPECT_EQ("isn't empty", DescribeNegation(m)); |
| 4089 | } |
| 4090 | |
| 4091 | TEST(IsEmptyTest, ExplainsResult) { |
| 4092 | Matcher<vector<int> > m = IsEmpty(); |
| 4093 | vector<int> container; |
| 4094 | EXPECT_EQ("", Explain(m, container)); |
| 4095 | container.push_back(0); |
| 4096 | EXPECT_EQ("whose size is 1", Explain(m, container)); |
| 4097 | } |
| 4098 | |
| 4099 | TEST(SizeIsTest, ImplementsSizeIs) { |
| 4100 | vector<int> container; |
| 4101 | EXPECT_THAT(container, SizeIs(0)); |
| 4102 | EXPECT_THAT(container, Not(SizeIs(1))); |
| 4103 | container.push_back(0); |
| 4104 | EXPECT_THAT(container, Not(SizeIs(0))); |
| 4105 | EXPECT_THAT(container, SizeIs(1)); |
| 4106 | container.push_back(0); |
| 4107 | EXPECT_THAT(container, Not(SizeIs(0))); |
| 4108 | EXPECT_THAT(container, SizeIs(2)); |
| 4109 | } |
| 4110 | |
| 4111 | TEST(SizeIsTest, WorksWithMap) { |
| 4112 | map<string, int> container; |
| 4113 | EXPECT_THAT(container, SizeIs(0)); |
| 4114 | EXPECT_THAT(container, Not(SizeIs(1))); |
| 4115 | container.insert(make_pair("foo", 1)); |
| 4116 | EXPECT_THAT(container, Not(SizeIs(0))); |
| 4117 | EXPECT_THAT(container, SizeIs(1)); |
| 4118 | container.insert(make_pair("bar", 2)); |
| 4119 | EXPECT_THAT(container, Not(SizeIs(0))); |
| 4120 | EXPECT_THAT(container, SizeIs(2)); |
| 4121 | } |
| 4122 | |
| 4123 | TEST(SizeIsTest, WorksWithReferences) { |
| 4124 | vector<int> container; |
| 4125 | Matcher<const vector<int>&> m = SizeIs(1); |
| 4126 | EXPECT_THAT(container, Not(m)); |
| 4127 | container.push_back(0); |
| 4128 | EXPECT_THAT(container, m); |
| 4129 | } |
| 4130 | |
| 4131 | TEST(SizeIsTest, CanDescribeSelf) { |
| 4132 | Matcher<vector<int> > m = SizeIs(2); |
| 4133 | EXPECT_EQ("size is equal to 2", Describe(m)); |
| 4134 | EXPECT_EQ("size isn't equal to 2", DescribeNegation(m)); |
| 4135 | } |
| 4136 | |
| 4137 | TEST(SizeIsTest, ExplainsResult) { |
| 4138 | Matcher<vector<int> > m1 = SizeIs(2); |
| 4139 | Matcher<vector<int> > m2 = SizeIs(Lt(2u)); |
| 4140 | Matcher<vector<int> > m3 = SizeIs(AnyOf(0, 3)); |
| 4141 | Matcher<vector<int> > m4 = SizeIs(GreaterThan(1)); |
| 4142 | vector<int> container; |
| 4143 | EXPECT_EQ("whose size 0 doesn't match", Explain(m1, container)); |
| 4144 | EXPECT_EQ("whose size 0 matches", Explain(m2, container)); |
| 4145 | EXPECT_EQ("whose size 0 matches", Explain(m3, container)); |
| 4146 | EXPECT_EQ("whose size 0 doesn't match, which is 1 less than 1", |
| 4147 | Explain(m4, container)); |
| 4148 | container.push_back(0); |
| 4149 | container.push_back(0); |
| 4150 | EXPECT_EQ("whose size 2 matches", Explain(m1, container)); |
| 4151 | EXPECT_EQ("whose size 2 doesn't match", Explain(m2, container)); |
| 4152 | EXPECT_EQ("whose size 2 doesn't match", Explain(m3, container)); |
| 4153 | EXPECT_EQ("whose size 2 matches, which is 1 more than 1", |
| 4154 | Explain(m4, container)); |
| 4155 | } |
| 4156 | |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 4157 | #if GTEST_HAS_TYPED_TEST |
| 4158 | // Tests ContainerEq with different container types, and |
| 4159 | // different element types. |
| 4160 | |
| 4161 | template <typename T> |
| 4162 | class ContainerEqTest : public testing::Test {}; |
| 4163 | |
| 4164 | typedef testing::Types< |
| 4165 | set<int>, |
| 4166 | vector<size_t>, |
| 4167 | multiset<size_t>, |
| 4168 | list<int> > |
| 4169 | ContainerEqTestTypes; |
| 4170 | |
| 4171 | TYPED_TEST_CASE(ContainerEqTest, ContainerEqTestTypes); |
| 4172 | |
| 4173 | // Tests that the filled container is equal to itself. |
| 4174 | TYPED_TEST(ContainerEqTest, EqualsSelf) { |
| 4175 | static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 4176 | TypeParam my_set(vals, vals + 6); |
| 4177 | const Matcher<TypeParam> m = ContainerEq(my_set); |
| 4178 | EXPECT_TRUE(m.Matches(my_set)); |
| 4179 | EXPECT_EQ("", Explain(m, my_set)); |
| 4180 | } |
| 4181 | |
| 4182 | // Tests that missing values are reported. |
| 4183 | TYPED_TEST(ContainerEqTest, ValueMissing) { |
| 4184 | static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 4185 | static const int test_vals[] = {2, 1, 8, 5}; |
| 4186 | TypeParam my_set(vals, vals + 6); |
| 4187 | TypeParam test_set(test_vals, test_vals + 4); |
| 4188 | const Matcher<TypeParam> m = ContainerEq(my_set); |
| 4189 | EXPECT_FALSE(m.Matches(test_set)); |
| 4190 | EXPECT_EQ("which doesn't have these expected elements: 3", |
| 4191 | Explain(m, test_set)); |
| 4192 | } |
| 4193 | |
| 4194 | // Tests that added values are reported. |
| 4195 | TYPED_TEST(ContainerEqTest, ValueAdded) { |
| 4196 | static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 4197 | static const int test_vals[] = {1, 2, 3, 5, 8, 46}; |
| 4198 | TypeParam my_set(vals, vals + 6); |
| 4199 | TypeParam test_set(test_vals, test_vals + 6); |
| 4200 | const Matcher<const TypeParam&> m = ContainerEq(my_set); |
| 4201 | EXPECT_FALSE(m.Matches(test_set)); |
| 4202 | EXPECT_EQ("which has these unexpected elements: 46", Explain(m, test_set)); |
| 4203 | } |
| 4204 | |
| 4205 | // Tests that added and missing values are reported together. |
| 4206 | TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) { |
| 4207 | static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 4208 | static const int test_vals[] = {1, 2, 3, 8, 46}; |
| 4209 | TypeParam my_set(vals, vals + 6); |
| 4210 | TypeParam test_set(test_vals, test_vals + 5); |
| 4211 | const Matcher<TypeParam> m = ContainerEq(my_set); |
| 4212 | EXPECT_FALSE(m.Matches(test_set)); |
| 4213 | EXPECT_EQ("which has these unexpected elements: 46,\n" |
| 4214 | "and doesn't have these expected elements: 5", |
| 4215 | Explain(m, test_set)); |
| 4216 | } |
| 4217 | |
| 4218 | // Tests duplicated value -- expect no explanation. |
| 4219 | TYPED_TEST(ContainerEqTest, DuplicateDifference) { |
| 4220 | static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 4221 | static const int test_vals[] = {1, 2, 3, 5, 8}; |
| 4222 | TypeParam my_set(vals, vals + 6); |
| 4223 | TypeParam test_set(test_vals, test_vals + 5); |
| 4224 | const Matcher<const TypeParam&> m = ContainerEq(my_set); |
| 4225 | // Depending on the container, match may be true or false |
| 4226 | // But in any case there should be no explanation. |
| 4227 | EXPECT_EQ("", Explain(m, test_set)); |
| 4228 | } |
| 4229 | #endif // GTEST_HAS_TYPED_TEST |
| 4230 | |
| 4231 | // Tests that mutliple missing values are reported. |
| 4232 | // Using just vector here, so order is predicatble. |
| 4233 | TEST(ContainerEqExtraTest, MultipleValuesMissing) { |
| 4234 | static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 4235 | static const int test_vals[] = {2, 1, 5}; |
| 4236 | vector<int> my_set(vals, vals + 6); |
| 4237 | vector<int> test_set(test_vals, test_vals + 3); |
| 4238 | const Matcher<vector<int> > m = ContainerEq(my_set); |
| 4239 | EXPECT_FALSE(m.Matches(test_set)); |
| 4240 | EXPECT_EQ("which doesn't have these expected elements: 3, 8", |
| 4241 | Explain(m, test_set)); |
| 4242 | } |
| 4243 | |
| 4244 | // Tests that added values are reported. |
| 4245 | // Using just vector here, so order is predicatble. |
| 4246 | TEST(ContainerEqExtraTest, MultipleValuesAdded) { |
| 4247 | static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 4248 | static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46}; |
| 4249 | list<size_t> my_set(vals, vals + 6); |
| 4250 | list<size_t> test_set(test_vals, test_vals + 7); |
| 4251 | const Matcher<const list<size_t>&> m = ContainerEq(my_set); |
| 4252 | EXPECT_FALSE(m.Matches(test_set)); |
| 4253 | EXPECT_EQ("which has these unexpected elements: 92, 46", |
| 4254 | Explain(m, test_set)); |
| 4255 | } |
| 4256 | |
| 4257 | // Tests that added and missing values are reported together. |
| 4258 | TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) { |
| 4259 | static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 4260 | static const int test_vals[] = {1, 2, 3, 92, 46}; |
| 4261 | list<size_t> my_set(vals, vals + 6); |
| 4262 | list<size_t> test_set(test_vals, test_vals + 5); |
| 4263 | const Matcher<const list<size_t> > m = ContainerEq(my_set); |
| 4264 | EXPECT_FALSE(m.Matches(test_set)); |
| 4265 | EXPECT_EQ("which has these unexpected elements: 92, 46,\n" |
| 4266 | "and doesn't have these expected elements: 5, 8", |
| 4267 | Explain(m, test_set)); |
| 4268 | } |
| 4269 | |
| 4270 | // Tests to see that duplicate elements are detected, |
| 4271 | // but (as above) not reported in the explanation. |
| 4272 | TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) { |
| 4273 | static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 4274 | static const int test_vals[] = {1, 2, 3, 5, 8}; |
| 4275 | vector<int> my_set(vals, vals + 6); |
| 4276 | vector<int> test_set(test_vals, test_vals + 5); |
| 4277 | const Matcher<vector<int> > m = ContainerEq(my_set); |
| 4278 | EXPECT_TRUE(m.Matches(my_set)); |
| 4279 | EXPECT_FALSE(m.Matches(test_set)); |
| 4280 | // There is nothing to report when both sets contain all the same values. |
| 4281 | EXPECT_EQ("", Explain(m, test_set)); |
| 4282 | } |
| 4283 | |
| 4284 | // Tests that ContainerEq works for non-trivial associative containers, |
| 4285 | // like maps. |
| 4286 | TEST(ContainerEqExtraTest, WorksForMaps) { |
| 4287 | map<int, std::string> my_map; |
| 4288 | my_map[0] = "a"; |
| 4289 | my_map[1] = "b"; |
| 4290 | |
| 4291 | map<int, std::string> test_map; |
| 4292 | test_map[0] = "aa"; |
| 4293 | test_map[1] = "b"; |
| 4294 | |
| 4295 | const Matcher<const map<int, std::string>&> m = ContainerEq(my_map); |
| 4296 | EXPECT_TRUE(m.Matches(my_map)); |
| 4297 | EXPECT_FALSE(m.Matches(test_map)); |
| 4298 | |
| 4299 | EXPECT_EQ("which has these unexpected elements: (0, \"aa\"),\n" |
| 4300 | "and doesn't have these expected elements: (0, \"a\")", |
| 4301 | Explain(m, test_map)); |
| 4302 | } |
| 4303 | |
| 4304 | TEST(ContainerEqExtraTest, WorksForNativeArray) { |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 4305 | int a1[] = {1, 2, 3}; |
| 4306 | int a2[] = {1, 2, 3}; |
| 4307 | int b[] = {1, 2, 4}; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 4308 | |
| 4309 | EXPECT_THAT(a1, ContainerEq(a2)); |
| 4310 | EXPECT_THAT(a1, Not(ContainerEq(b))); |
| 4311 | } |
| 4312 | |
| 4313 | TEST(ContainerEqExtraTest, WorksForTwoDimensionalNativeArray) { |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 4314 | const char a1[][3] = {"hi", "lo"}; |
| 4315 | const char a2[][3] = {"hi", "lo"}; |
| 4316 | const char b[][3] = {"lo", "hi"}; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 4317 | |
| 4318 | // Tests using ContainerEq() in the first dimension. |
| 4319 | EXPECT_THAT(a1, ContainerEq(a2)); |
| 4320 | EXPECT_THAT(a1, Not(ContainerEq(b))); |
| 4321 | |
| 4322 | // Tests using ContainerEq() in the second dimension. |
| 4323 | EXPECT_THAT(a1, ElementsAre(ContainerEq(a2[0]), ContainerEq(a2[1]))); |
| 4324 | EXPECT_THAT(a1, ElementsAre(Not(ContainerEq(b[0])), ContainerEq(a2[1]))); |
| 4325 | } |
| 4326 | |
| 4327 | TEST(ContainerEqExtraTest, WorksForNativeArrayAsTuple) { |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 4328 | const int a1[] = {1, 2, 3}; |
| 4329 | const int a2[] = {1, 2, 3}; |
| 4330 | const int b[] = {1, 2, 3, 4}; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 4331 | |
| 4332 | const int* const p1 = a1; |
| 4333 | EXPECT_THAT(make_tuple(p1, 3), ContainerEq(a2)); |
| 4334 | EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(b))); |
| 4335 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 4336 | const int c[] = {1, 3, 2}; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 4337 | EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(c))); |
| 4338 | } |
| 4339 | |
| 4340 | TEST(ContainerEqExtraTest, CopiesNativeArrayParameter) { |
| 4341 | std::string a1[][3] = { |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 4342 | {"hi", "hello", "ciao"}, |
| 4343 | {"bye", "see you", "ciao"} |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 4344 | }; |
| 4345 | |
| 4346 | std::string a2[][3] = { |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 4347 | {"hi", "hello", "ciao"}, |
| 4348 | {"bye", "see you", "ciao"} |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 4349 | }; |
| 4350 | |
| 4351 | const Matcher<const std::string(&)[2][3]> m = ContainerEq(a2); |
| 4352 | EXPECT_THAT(a1, m); |
| 4353 | |
| 4354 | a2[0][0] = "ha"; |
| 4355 | EXPECT_THAT(a1, m); |
| 4356 | } |
| 4357 | |
| 4358 | TEST(WhenSortedByTest, WorksForEmptyContainer) { |
| 4359 | const vector<int> numbers; |
| 4360 | EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre())); |
| 4361 | EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1)))); |
| 4362 | } |
| 4363 | |
| 4364 | TEST(WhenSortedByTest, WorksForNonEmptyContainer) { |
| 4365 | vector<unsigned> numbers; |
| 4366 | numbers.push_back(3); |
| 4367 | numbers.push_back(1); |
| 4368 | numbers.push_back(2); |
| 4369 | numbers.push_back(2); |
| 4370 | EXPECT_THAT(numbers, WhenSortedBy(greater<unsigned>(), |
| 4371 | ElementsAre(3, 2, 2, 1))); |
| 4372 | EXPECT_THAT(numbers, Not(WhenSortedBy(greater<unsigned>(), |
| 4373 | ElementsAre(1, 2, 2, 3)))); |
| 4374 | } |
| 4375 | |
| 4376 | TEST(WhenSortedByTest, WorksForNonVectorContainer) { |
| 4377 | list<string> words; |
| 4378 | words.push_back("say"); |
| 4379 | words.push_back("hello"); |
| 4380 | words.push_back("world"); |
| 4381 | EXPECT_THAT(words, WhenSortedBy(less<string>(), |
| 4382 | ElementsAre("hello", "say", "world"))); |
| 4383 | EXPECT_THAT(words, Not(WhenSortedBy(less<string>(), |
| 4384 | ElementsAre("say", "hello", "world")))); |
| 4385 | } |
| 4386 | |
| 4387 | TEST(WhenSortedByTest, WorksForNativeArray) { |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 4388 | const int numbers[] = {1, 3, 2, 4}; |
| 4389 | const int sorted_numbers[] = {1, 2, 3, 4}; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 4390 | EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre(1, 2, 3, 4))); |
| 4391 | EXPECT_THAT(numbers, WhenSortedBy(less<int>(), |
| 4392 | ElementsAreArray(sorted_numbers))); |
| 4393 | EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1, 3, 2, 4)))); |
| 4394 | } |
| 4395 | |
| 4396 | TEST(WhenSortedByTest, CanDescribeSelf) { |
| 4397 | const Matcher<vector<int> > m = WhenSortedBy(less<int>(), ElementsAre(1, 2)); |
| 4398 | EXPECT_EQ("(when sorted) has 2 elements where\n" |
| 4399 | "element #0 is equal to 1,\n" |
| 4400 | "element #1 is equal to 2", |
| 4401 | Describe(m)); |
| 4402 | EXPECT_EQ("(when sorted) doesn't have 2 elements, or\n" |
| 4403 | "element #0 isn't equal to 1, or\n" |
| 4404 | "element #1 isn't equal to 2", |
| 4405 | DescribeNegation(m)); |
| 4406 | } |
| 4407 | |
| 4408 | TEST(WhenSortedByTest, ExplainsMatchResult) { |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 4409 | const int a[] = {2, 1}; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 4410 | EXPECT_EQ("which is { 1, 2 } when sorted, whose element #0 doesn't match", |
| 4411 | Explain(WhenSortedBy(less<int>(), ElementsAre(2, 3)), a)); |
| 4412 | EXPECT_EQ("which is { 1, 2 } when sorted", |
| 4413 | Explain(WhenSortedBy(less<int>(), ElementsAre(1, 2)), a)); |
| 4414 | } |
| 4415 | |
| 4416 | // WhenSorted() is a simple wrapper on WhenSortedBy(). Hence we don't |
| 4417 | // need to test it as exhaustively as we test the latter. |
| 4418 | |
| 4419 | TEST(WhenSortedTest, WorksForEmptyContainer) { |
| 4420 | const vector<int> numbers; |
| 4421 | EXPECT_THAT(numbers, WhenSorted(ElementsAre())); |
| 4422 | EXPECT_THAT(numbers, Not(WhenSorted(ElementsAre(1)))); |
| 4423 | } |
| 4424 | |
| 4425 | TEST(WhenSortedTest, WorksForNonEmptyContainer) { |
| 4426 | list<string> words; |
| 4427 | words.push_back("3"); |
| 4428 | words.push_back("1"); |
| 4429 | words.push_back("2"); |
| 4430 | words.push_back("2"); |
| 4431 | EXPECT_THAT(words, WhenSorted(ElementsAre("1", "2", "2", "3"))); |
| 4432 | EXPECT_THAT(words, Not(WhenSorted(ElementsAre("3", "1", "2", "2")))); |
| 4433 | } |
| 4434 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 4435 | TEST(WhenSortedTest, WorksForMapTypes) { |
| 4436 | map<string, int> word_counts; |
| 4437 | word_counts["and"] = 1; |
| 4438 | word_counts["the"] = 1; |
| 4439 | word_counts["buffalo"] = 2; |
| 4440 | EXPECT_THAT(word_counts, WhenSorted(ElementsAre( |
| 4441 | Pair("and", 1), Pair("buffalo", 2), Pair("the", 1)))); |
| 4442 | EXPECT_THAT(word_counts, Not(WhenSorted(ElementsAre( |
| 4443 | Pair("and", 1), Pair("the", 1), Pair("buffalo", 2))))); |
| 4444 | } |
| 4445 | |
| 4446 | TEST(WhenSortedTest, WorksForMultiMapTypes) { |
| 4447 | multimap<int, int> ifib; |
| 4448 | ifib.insert(make_pair(8, 6)); |
| 4449 | ifib.insert(make_pair(2, 3)); |
| 4450 | ifib.insert(make_pair(1, 1)); |
| 4451 | ifib.insert(make_pair(3, 4)); |
| 4452 | ifib.insert(make_pair(1, 2)); |
| 4453 | ifib.insert(make_pair(5, 5)); |
| 4454 | EXPECT_THAT(ifib, WhenSorted(ElementsAre(Pair(1, 1), |
| 4455 | Pair(1, 2), |
| 4456 | Pair(2, 3), |
| 4457 | Pair(3, 4), |
| 4458 | Pair(5, 5), |
| 4459 | Pair(8, 6)))); |
| 4460 | EXPECT_THAT(ifib, Not(WhenSorted(ElementsAre(Pair(8, 6), |
| 4461 | Pair(2, 3), |
| 4462 | Pair(1, 1), |
| 4463 | Pair(3, 4), |
| 4464 | Pair(1, 2), |
| 4465 | Pair(5, 5))))); |
| 4466 | } |
| 4467 | |
| 4468 | TEST(WhenSortedTest, WorksForPolymorphicMatcher) { |
| 4469 | std::deque<int> d; |
| 4470 | d.push_back(2); |
| 4471 | d.push_back(1); |
| 4472 | EXPECT_THAT(d, WhenSorted(ElementsAre(1, 2))); |
| 4473 | EXPECT_THAT(d, Not(WhenSorted(ElementsAre(2, 1)))); |
| 4474 | } |
| 4475 | |
| 4476 | TEST(WhenSortedTest, WorksForVectorConstRefMatcher) { |
| 4477 | std::deque<int> d; |
| 4478 | d.push_back(2); |
| 4479 | d.push_back(1); |
| 4480 | Matcher<const std::vector<int>&> vector_match = ElementsAre(1, 2); |
| 4481 | EXPECT_THAT(d, WhenSorted(vector_match)); |
| 4482 | Matcher<const std::vector<int>&> not_vector_match = ElementsAre(2, 1); |
| 4483 | EXPECT_THAT(d, Not(WhenSorted(not_vector_match))); |
| 4484 | } |
| 4485 | |
| 4486 | // Deliberately bare pseudo-container. |
| 4487 | // Offers only begin() and end() accessors, yielding InputIterator. |
| 4488 | template <typename T> |
| 4489 | class Streamlike { |
| 4490 | private: |
| 4491 | class ConstIter; |
| 4492 | public: |
| 4493 | typedef ConstIter const_iterator; |
| 4494 | typedef T value_type; |
| 4495 | |
| 4496 | template <typename InIter> |
| 4497 | Streamlike(InIter first, InIter last) : remainder_(first, last) {} |
| 4498 | |
| 4499 | const_iterator begin() const { |
| 4500 | return const_iterator(this, remainder_.begin()); |
| 4501 | } |
| 4502 | const_iterator end() const { |
| 4503 | return const_iterator(this, remainder_.end()); |
| 4504 | } |
| 4505 | |
| 4506 | private: |
| 4507 | class ConstIter : public std::iterator<std::input_iterator_tag, |
| 4508 | value_type, |
| 4509 | ptrdiff_t, |
| 4510 | const value_type*, |
| 4511 | const value_type&> { |
| 4512 | public: |
| 4513 | ConstIter(const Streamlike* s, |
| 4514 | typename std::list<value_type>::iterator pos) |
| 4515 | : s_(s), pos_(pos) {} |
| 4516 | |
| 4517 | const value_type& operator*() const { return *pos_; } |
| 4518 | const value_type* operator->() const { return &*pos_; } |
| 4519 | ConstIter& operator++() { |
| 4520 | s_->remainder_.erase(pos_++); |
| 4521 | return *this; |
| 4522 | } |
| 4523 | |
| 4524 | // *iter++ is required to work (see std::istreambuf_iterator). |
| 4525 | // (void)iter++ is also required to work. |
| 4526 | class PostIncrProxy { |
| 4527 | public: |
| 4528 | explicit PostIncrProxy(const value_type& value) : value_(value) {} |
| 4529 | value_type operator*() const { return value_; } |
| 4530 | private: |
| 4531 | value_type value_; |
| 4532 | }; |
| 4533 | PostIncrProxy operator++(int) { |
| 4534 | PostIncrProxy proxy(**this); |
| 4535 | ++(*this); |
| 4536 | return proxy; |
| 4537 | } |
| 4538 | |
| 4539 | friend bool operator==(const ConstIter& a, const ConstIter& b) { |
| 4540 | return a.s_ == b.s_ && a.pos_ == b.pos_; |
| 4541 | } |
| 4542 | friend bool operator!=(const ConstIter& a, const ConstIter& b) { |
| 4543 | return !(a == b); |
| 4544 | } |
| 4545 | |
| 4546 | private: |
| 4547 | const Streamlike* s_; |
| 4548 | typename std::list<value_type>::iterator pos_; |
| 4549 | }; |
| 4550 | |
| 4551 | friend std::ostream& operator<<(std::ostream& os, const Streamlike& s) { |
| 4552 | os << "["; |
| 4553 | typedef typename std::list<value_type>::const_iterator Iter; |
| 4554 | const char* sep = ""; |
| 4555 | for (Iter it = s.remainder_.begin(); it != s.remainder_.end(); ++it) { |
| 4556 | os << sep << *it; |
| 4557 | sep = ","; |
| 4558 | } |
| 4559 | os << "]"; |
| 4560 | return os; |
| 4561 | } |
| 4562 | |
| 4563 | mutable std::list<value_type> remainder_; // modified by iteration |
| 4564 | }; |
| 4565 | |
| 4566 | TEST(StreamlikeTest, Iteration) { |
| 4567 | const int a[5] = {2, 1, 4, 5, 3}; |
| 4568 | Streamlike<int> s(a, a + 5); |
| 4569 | Streamlike<int>::const_iterator it = s.begin(); |
| 4570 | const int* ip = a; |
| 4571 | while (it != s.end()) { |
| 4572 | SCOPED_TRACE(ip - a); |
| 4573 | EXPECT_EQ(*ip++, *it++); |
| 4574 | } |
| 4575 | } |
| 4576 | |
| 4577 | #if GTEST_HAS_STD_FORWARD_LIST_ |
| 4578 | TEST(BeginEndDistanceIsTest, WorksWithForwardList) { |
| 4579 | std::forward_list<int> container; |
| 4580 | EXPECT_THAT(container, BeginEndDistanceIs(0)); |
| 4581 | EXPECT_THAT(container, Not(BeginEndDistanceIs(1))); |
| 4582 | container.push_front(0); |
| 4583 | EXPECT_THAT(container, Not(BeginEndDistanceIs(0))); |
| 4584 | EXPECT_THAT(container, BeginEndDistanceIs(1)); |
| 4585 | container.push_front(0); |
| 4586 | EXPECT_THAT(container, Not(BeginEndDistanceIs(0))); |
| 4587 | EXPECT_THAT(container, BeginEndDistanceIs(2)); |
| 4588 | } |
| 4589 | #endif // GTEST_HAS_STD_FORWARD_LIST_ |
| 4590 | |
| 4591 | TEST(BeginEndDistanceIsTest, WorksWithNonStdList) { |
| 4592 | const int a[5] = {1, 2, 3, 4, 5}; |
| 4593 | Streamlike<int> s(a, a + 5); |
| 4594 | EXPECT_THAT(s, BeginEndDistanceIs(5)); |
| 4595 | } |
| 4596 | |
| 4597 | TEST(BeginEndDistanceIsTest, CanDescribeSelf) { |
| 4598 | Matcher<vector<int> > m = BeginEndDistanceIs(2); |
| 4599 | EXPECT_EQ("distance between begin() and end() is equal to 2", Describe(m)); |
| 4600 | EXPECT_EQ("distance between begin() and end() isn't equal to 2", |
| 4601 | DescribeNegation(m)); |
| 4602 | } |
| 4603 | |
| 4604 | TEST(BeginEndDistanceIsTest, ExplainsResult) { |
| 4605 | Matcher<vector<int> > m1 = BeginEndDistanceIs(2); |
| 4606 | Matcher<vector<int> > m2 = BeginEndDistanceIs(Lt(2)); |
| 4607 | Matcher<vector<int> > m3 = BeginEndDistanceIs(AnyOf(0, 3)); |
| 4608 | Matcher<vector<int> > m4 = BeginEndDistanceIs(GreaterThan(1)); |
| 4609 | vector<int> container; |
| 4610 | EXPECT_EQ("whose distance between begin() and end() 0 doesn't match", |
| 4611 | Explain(m1, container)); |
| 4612 | EXPECT_EQ("whose distance between begin() and end() 0 matches", |
| 4613 | Explain(m2, container)); |
| 4614 | EXPECT_EQ("whose distance between begin() and end() 0 matches", |
| 4615 | Explain(m3, container)); |
| 4616 | EXPECT_EQ( |
| 4617 | "whose distance between begin() and end() 0 doesn't match, which is 1 " |
| 4618 | "less than 1", |
| 4619 | Explain(m4, container)); |
| 4620 | container.push_back(0); |
| 4621 | container.push_back(0); |
| 4622 | EXPECT_EQ("whose distance between begin() and end() 2 matches", |
| 4623 | Explain(m1, container)); |
| 4624 | EXPECT_EQ("whose distance between begin() and end() 2 doesn't match", |
| 4625 | Explain(m2, container)); |
| 4626 | EXPECT_EQ("whose distance between begin() and end() 2 doesn't match", |
| 4627 | Explain(m3, container)); |
| 4628 | EXPECT_EQ( |
| 4629 | "whose distance between begin() and end() 2 matches, which is 1 more " |
| 4630 | "than 1", |
| 4631 | Explain(m4, container)); |
| 4632 | } |
| 4633 | |
| 4634 | TEST(WhenSortedTest, WorksForStreamlike) { |
| 4635 | // Streamlike 'container' provides only minimal iterator support. |
| 4636 | // Its iterators are tagged with input_iterator_tag. |
| 4637 | const int a[5] = {2, 1, 4, 5, 3}; |
| 4638 | Streamlike<int> s(a, a + GTEST_ARRAY_SIZE_(a)); |
| 4639 | EXPECT_THAT(s, WhenSorted(ElementsAre(1, 2, 3, 4, 5))); |
| 4640 | EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3)))); |
| 4641 | } |
| 4642 | |
| 4643 | TEST(WhenSortedTest, WorksForVectorConstRefMatcherOnStreamlike) { |
| 4644 | const int a[] = {2, 1, 4, 5, 3}; |
| 4645 | Streamlike<int> s(a, a + GTEST_ARRAY_SIZE_(a)); |
| 4646 | Matcher<const std::vector<int>&> vector_match = ElementsAre(1, 2, 3, 4, 5); |
| 4647 | EXPECT_THAT(s, WhenSorted(vector_match)); |
| 4648 | EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3)))); |
| 4649 | } |
| 4650 | |
| 4651 | // Tests using ElementsAre() and ElementsAreArray() with stream-like |
| 4652 | // "containers". |
| 4653 | |
| 4654 | TEST(ElemensAreStreamTest, WorksForStreamlike) { |
| 4655 | const int a[5] = {1, 2, 3, 4, 5}; |
| 4656 | Streamlike<int> s(a, a + GTEST_ARRAY_SIZE_(a)); |
| 4657 | EXPECT_THAT(s, ElementsAre(1, 2, 3, 4, 5)); |
| 4658 | EXPECT_THAT(s, Not(ElementsAre(2, 1, 4, 5, 3))); |
| 4659 | } |
| 4660 | |
| 4661 | TEST(ElemensAreArrayStreamTest, WorksForStreamlike) { |
| 4662 | const int a[5] = {1, 2, 3, 4, 5}; |
| 4663 | Streamlike<int> s(a, a + GTEST_ARRAY_SIZE_(a)); |
| 4664 | |
| 4665 | vector<int> expected; |
| 4666 | expected.push_back(1); |
| 4667 | expected.push_back(2); |
| 4668 | expected.push_back(3); |
| 4669 | expected.push_back(4); |
| 4670 | expected.push_back(5); |
| 4671 | EXPECT_THAT(s, ElementsAreArray(expected)); |
| 4672 | |
| 4673 | expected[3] = 0; |
| 4674 | EXPECT_THAT(s, Not(ElementsAreArray(expected))); |
| 4675 | } |
| 4676 | |
| 4677 | TEST(ElementsAreTest, WorksWithUncopyable) { |
| 4678 | Uncopyable objs[2]; |
| 4679 | objs[0].set_value(-3); |
| 4680 | objs[1].set_value(1); |
| 4681 | EXPECT_THAT(objs, ElementsAre(UncopyableIs(-3), Truly(ValueIsPositive))); |
| 4682 | } |
| 4683 | |
| 4684 | TEST(ElementsAreTest, TakesStlContainer) { |
| 4685 | const int actual[] = {3, 1, 2}; |
| 4686 | |
| 4687 | ::std::list<int> expected; |
| 4688 | expected.push_back(3); |
| 4689 | expected.push_back(1); |
| 4690 | expected.push_back(2); |
| 4691 | EXPECT_THAT(actual, ElementsAreArray(expected)); |
| 4692 | |
| 4693 | expected.push_back(4); |
| 4694 | EXPECT_THAT(actual, Not(ElementsAreArray(expected))); |
| 4695 | } |
| 4696 | |
| 4697 | // Tests for UnorderedElementsAreArray() |
| 4698 | |
| 4699 | TEST(UnorderedElementsAreArrayTest, SucceedsWhenExpected) { |
| 4700 | const int a[] = {0, 1, 2, 3, 4}; |
| 4701 | std::vector<int> s(a, a + GTEST_ARRAY_SIZE_(a)); |
| 4702 | do { |
| 4703 | StringMatchResultListener listener; |
| 4704 | EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(a), |
| 4705 | s, &listener)) << listener.str(); |
| 4706 | } while (std::next_permutation(s.begin(), s.end())); |
| 4707 | } |
| 4708 | |
| 4709 | TEST(UnorderedElementsAreArrayTest, VectorBool) { |
| 4710 | const bool a[] = {0, 1, 0, 1, 1}; |
| 4711 | const bool b[] = {1, 0, 1, 1, 0}; |
| 4712 | std::vector<bool> expected(a, a + GTEST_ARRAY_SIZE_(a)); |
| 4713 | std::vector<bool> actual(b, b + GTEST_ARRAY_SIZE_(b)); |
| 4714 | StringMatchResultListener listener; |
| 4715 | EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(expected), |
| 4716 | actual, &listener)) << listener.str(); |
| 4717 | } |
| 4718 | |
| 4719 | TEST(UnorderedElementsAreArrayTest, WorksForStreamlike) { |
| 4720 | // Streamlike 'container' provides only minimal iterator support. |
| 4721 | // Its iterators are tagged with input_iterator_tag, and it has no |
| 4722 | // size() or empty() methods. |
| 4723 | const int a[5] = {2, 1, 4, 5, 3}; |
| 4724 | Streamlike<int> s(a, a + GTEST_ARRAY_SIZE_(a)); |
| 4725 | |
| 4726 | ::std::vector<int> expected; |
| 4727 | expected.push_back(1); |
| 4728 | expected.push_back(2); |
| 4729 | expected.push_back(3); |
| 4730 | expected.push_back(4); |
| 4731 | expected.push_back(5); |
| 4732 | EXPECT_THAT(s, UnorderedElementsAreArray(expected)); |
| 4733 | |
| 4734 | expected.push_back(6); |
| 4735 | EXPECT_THAT(s, Not(UnorderedElementsAreArray(expected))); |
| 4736 | } |
| 4737 | |
| 4738 | TEST(UnorderedElementsAreArrayTest, TakesStlContainer) { |
| 4739 | const int actual[] = {3, 1, 2}; |
| 4740 | |
| 4741 | ::std::list<int> expected; |
| 4742 | expected.push_back(1); |
| 4743 | expected.push_back(2); |
| 4744 | expected.push_back(3); |
| 4745 | EXPECT_THAT(actual, UnorderedElementsAreArray(expected)); |
| 4746 | |
| 4747 | expected.push_back(4); |
| 4748 | EXPECT_THAT(actual, Not(UnorderedElementsAreArray(expected))); |
| 4749 | } |
| 4750 | |
| 4751 | #if GTEST_HAS_STD_INITIALIZER_LIST_ |
| 4752 | |
| 4753 | TEST(UnorderedElementsAreArrayTest, TakesInitializerList) { |
| 4754 | const int a[5] = {2, 1, 4, 5, 3}; |
| 4755 | EXPECT_THAT(a, UnorderedElementsAreArray({1, 2, 3, 4, 5})); |
| 4756 | EXPECT_THAT(a, Not(UnorderedElementsAreArray({1, 2, 3, 4, 6}))); |
| 4757 | } |
| 4758 | |
| 4759 | TEST(UnorderedElementsAreArrayTest, TakesInitializerListOfCStrings) { |
| 4760 | const string a[5] = {"a", "b", "c", "d", "e"}; |
| 4761 | EXPECT_THAT(a, UnorderedElementsAreArray({"a", "b", "c", "d", "e"})); |
| 4762 | EXPECT_THAT(a, Not(UnorderedElementsAreArray({"a", "b", "c", "d", "ef"}))); |
| 4763 | } |
| 4764 | |
| 4765 | TEST(UnorderedElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) { |
| 4766 | const int a[5] = {2, 1, 4, 5, 3}; |
| 4767 | EXPECT_THAT(a, UnorderedElementsAreArray( |
| 4768 | {Eq(1), Eq(2), Eq(3), Eq(4), Eq(5)})); |
| 4769 | EXPECT_THAT(a, Not(UnorderedElementsAreArray( |
| 4770 | {Eq(1), Eq(2), Eq(3), Eq(4), Eq(6)}))); |
| 4771 | } |
| 4772 | |
| 4773 | TEST(UnorderedElementsAreArrayTest, |
| 4774 | TakesInitializerListOfDifferentTypedMatchers) { |
| 4775 | const int a[5] = {2, 1, 4, 5, 3}; |
| 4776 | // The compiler cannot infer the type of the initializer list if its |
| 4777 | // elements have different types. We must explicitly specify the |
| 4778 | // unified element type in this case. |
| 4779 | EXPECT_THAT(a, UnorderedElementsAreArray<Matcher<int> >( |
| 4780 | {Eq(1), Ne(-2), Ge(3), Le(4), Eq(5)})); |
| 4781 | EXPECT_THAT(a, Not(UnorderedElementsAreArray<Matcher<int> >( |
| 4782 | {Eq(1), Ne(-2), Ge(3), Le(4), Eq(6)}))); |
| 4783 | } |
| 4784 | |
| 4785 | #endif // GTEST_HAS_STD_INITIALIZER_LIST_ |
| 4786 | |
| 4787 | class UnorderedElementsAreTest : public testing::Test { |
| 4788 | protected: |
| 4789 | typedef std::vector<int> IntVec; |
| 4790 | }; |
| 4791 | |
| 4792 | TEST_F(UnorderedElementsAreTest, WorksWithUncopyable) { |
| 4793 | Uncopyable objs[2]; |
| 4794 | objs[0].set_value(-3); |
| 4795 | objs[1].set_value(1); |
| 4796 | EXPECT_THAT(objs, |
| 4797 | UnorderedElementsAre(Truly(ValueIsPositive), UncopyableIs(-3))); |
| 4798 | } |
| 4799 | |
| 4800 | TEST_F(UnorderedElementsAreTest, SucceedsWhenExpected) { |
| 4801 | const int a[] = {1, 2, 3}; |
| 4802 | std::vector<int> s(a, a + GTEST_ARRAY_SIZE_(a)); |
| 4803 | do { |
| 4804 | StringMatchResultListener listener; |
| 4805 | EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3), |
| 4806 | s, &listener)) << listener.str(); |
| 4807 | } while (std::next_permutation(s.begin(), s.end())); |
| 4808 | } |
| 4809 | |
| 4810 | TEST_F(UnorderedElementsAreTest, FailsWhenAnElementMatchesNoMatcher) { |
| 4811 | const int a[] = {1, 2, 3}; |
| 4812 | std::vector<int> s(a, a + GTEST_ARRAY_SIZE_(a)); |
| 4813 | std::vector<Matcher<int> > mv; |
| 4814 | mv.push_back(1); |
| 4815 | mv.push_back(2); |
| 4816 | mv.push_back(2); |
| 4817 | // The element with value '3' matches nothing: fail fast. |
| 4818 | StringMatchResultListener listener; |
| 4819 | EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAreArray(mv), |
| 4820 | s, &listener)) << listener.str(); |
| 4821 | } |
| 4822 | |
| 4823 | TEST_F(UnorderedElementsAreTest, WorksForStreamlike) { |
| 4824 | // Streamlike 'container' provides only minimal iterator support. |
| 4825 | // Its iterators are tagged with input_iterator_tag, and it has no |
| 4826 | // size() or empty() methods. |
| 4827 | const int a[5] = {2, 1, 4, 5, 3}; |
| 4828 | Streamlike<int> s(a, a + GTEST_ARRAY_SIZE_(a)); |
| 4829 | |
| 4830 | EXPECT_THAT(s, UnorderedElementsAre(1, 2, 3, 4, 5)); |
| 4831 | EXPECT_THAT(s, Not(UnorderedElementsAre(2, 2, 3, 4, 5))); |
| 4832 | } |
| 4833 | |
| 4834 | // One naive implementation of the matcher runs in O(N!) time, which is too |
| 4835 | // slow for many real-world inputs. This test shows that our matcher can match |
| 4836 | // 100 inputs very quickly (a few milliseconds). An O(100!) is 10^158 |
| 4837 | // iterations and obviously effectively incomputable. |
| 4838 | // [ RUN ] UnorderedElementsAreTest.Performance |
| 4839 | // [ OK ] UnorderedElementsAreTest.Performance (4 ms) |
| 4840 | TEST_F(UnorderedElementsAreTest, Performance) { |
| 4841 | std::vector<int> s; |
| 4842 | std::vector<Matcher<int> > mv; |
| 4843 | for (int i = 0; i < 100; ++i) { |
| 4844 | s.push_back(i); |
| 4845 | mv.push_back(_); |
| 4846 | } |
| 4847 | mv[50] = Eq(0); |
| 4848 | StringMatchResultListener listener; |
| 4849 | EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(mv), |
| 4850 | s, &listener)) << listener.str(); |
| 4851 | } |
| 4852 | |
| 4853 | // Another variant of 'Performance' with similar expectations. |
| 4854 | // [ RUN ] UnorderedElementsAreTest.PerformanceHalfStrict |
| 4855 | // [ OK ] UnorderedElementsAreTest.PerformanceHalfStrict (4 ms) |
| 4856 | TEST_F(UnorderedElementsAreTest, PerformanceHalfStrict) { |
| 4857 | std::vector<int> s; |
| 4858 | std::vector<Matcher<int> > mv; |
| 4859 | for (int i = 0; i < 100; ++i) { |
| 4860 | s.push_back(i); |
| 4861 | if (i & 1) { |
| 4862 | mv.push_back(_); |
| 4863 | } else { |
| 4864 | mv.push_back(i); |
| 4865 | } |
| 4866 | } |
| 4867 | StringMatchResultListener listener; |
| 4868 | EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(mv), |
| 4869 | s, &listener)) << listener.str(); |
| 4870 | } |
| 4871 | |
| 4872 | TEST_F(UnorderedElementsAreTest, FailMessageCountWrong) { |
| 4873 | std::vector<int> v; |
| 4874 | v.push_back(4); |
| 4875 | StringMatchResultListener listener; |
| 4876 | EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3), |
| 4877 | v, &listener)) << listener.str(); |
| 4878 | EXPECT_THAT(listener.str(), Eq("which has 1 element")); |
| 4879 | } |
| 4880 | |
| 4881 | TEST_F(UnorderedElementsAreTest, FailMessageCountWrongZero) { |
| 4882 | std::vector<int> v; |
| 4883 | StringMatchResultListener listener; |
| 4884 | EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3), |
| 4885 | v, &listener)) << listener.str(); |
| 4886 | EXPECT_THAT(listener.str(), Eq("")); |
| 4887 | } |
| 4888 | |
| 4889 | TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedMatchers) { |
| 4890 | std::vector<int> v; |
| 4891 | v.push_back(1); |
| 4892 | v.push_back(1); |
| 4893 | StringMatchResultListener listener; |
| 4894 | EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2), |
| 4895 | v, &listener)) << listener.str(); |
| 4896 | EXPECT_THAT( |
| 4897 | listener.str(), |
| 4898 | Eq("where the following matchers don't match any elements:\n" |
| 4899 | "matcher #1: is equal to 2")); |
| 4900 | } |
| 4901 | |
| 4902 | TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedElements) { |
| 4903 | std::vector<int> v; |
| 4904 | v.push_back(1); |
| 4905 | v.push_back(2); |
| 4906 | StringMatchResultListener listener; |
| 4907 | EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 1), |
| 4908 | v, &listener)) << listener.str(); |
| 4909 | EXPECT_THAT( |
| 4910 | listener.str(), |
| 4911 | Eq("where the following elements don't match any matchers:\n" |
| 4912 | "element #1: 2")); |
| 4913 | } |
| 4914 | |
| 4915 | TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedMatcherAndElement) { |
| 4916 | std::vector<int> v; |
| 4917 | v.push_back(2); |
| 4918 | v.push_back(3); |
| 4919 | StringMatchResultListener listener; |
| 4920 | EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2), |
| 4921 | v, &listener)) << listener.str(); |
| 4922 | EXPECT_THAT( |
| 4923 | listener.str(), |
| 4924 | Eq("where" |
| 4925 | " the following matchers don't match any elements:\n" |
| 4926 | "matcher #0: is equal to 1\n" |
| 4927 | "and" |
| 4928 | " where" |
| 4929 | " the following elements don't match any matchers:\n" |
| 4930 | "element #1: 3")); |
| 4931 | } |
| 4932 | |
| 4933 | // Test helper for formatting element, matcher index pairs in expectations. |
| 4934 | static string EMString(int element, int matcher) { |
| 4935 | stringstream ss; |
| 4936 | ss << "(element #" << element << ", matcher #" << matcher << ")"; |
| 4937 | return ss.str(); |
| 4938 | } |
| 4939 | |
| 4940 | TEST_F(UnorderedElementsAreTest, FailMessageImperfectMatchOnly) { |
| 4941 | // A situation where all elements and matchers have a match |
| 4942 | // associated with them, but the max matching is not perfect. |
| 4943 | std::vector<string> v; |
| 4944 | v.push_back("a"); |
| 4945 | v.push_back("b"); |
| 4946 | v.push_back("c"); |
| 4947 | StringMatchResultListener listener; |
| 4948 | EXPECT_FALSE(ExplainMatchResult( |
| 4949 | UnorderedElementsAre("a", "a", AnyOf("b", "c")), v, &listener)) |
| 4950 | << listener.str(); |
| 4951 | |
| 4952 | string prefix = |
| 4953 | "where no permutation of the elements can satisfy all matchers, " |
| 4954 | "and the closest match is 2 of 3 matchers with the " |
| 4955 | "pairings:\n"; |
| 4956 | |
| 4957 | // We have to be a bit loose here, because there are 4 valid max matches. |
| 4958 | EXPECT_THAT( |
| 4959 | listener.str(), |
| 4960 | AnyOf(prefix + "{\n " + EMString(0, 0) + |
| 4961 | ",\n " + EMString(1, 2) + "\n}", |
| 4962 | prefix + "{\n " + EMString(0, 1) + |
| 4963 | ",\n " + EMString(1, 2) + "\n}", |
| 4964 | prefix + "{\n " + EMString(0, 0) + |
| 4965 | ",\n " + EMString(2, 2) + "\n}", |
| 4966 | prefix + "{\n " + EMString(0, 1) + |
| 4967 | ",\n " + EMString(2, 2) + "\n}")); |
| 4968 | } |
| 4969 | |
| 4970 | TEST_F(UnorderedElementsAreTest, Describe) { |
| 4971 | EXPECT_THAT(Describe<IntVec>(UnorderedElementsAre()), |
| 4972 | Eq("is empty")); |
| 4973 | EXPECT_THAT( |
| 4974 | Describe<IntVec>(UnorderedElementsAre(345)), |
| 4975 | Eq("has 1 element and that element is equal to 345")); |
| 4976 | EXPECT_THAT( |
| 4977 | Describe<IntVec>(UnorderedElementsAre(111, 222, 333)), |
| 4978 | Eq("has 3 elements and there exists some permutation " |
| 4979 | "of elements such that:\n" |
| 4980 | " - element #0 is equal to 111, and\n" |
| 4981 | " - element #1 is equal to 222, and\n" |
| 4982 | " - element #2 is equal to 333")); |
| 4983 | } |
| 4984 | |
| 4985 | TEST_F(UnorderedElementsAreTest, DescribeNegation) { |
| 4986 | EXPECT_THAT(DescribeNegation<IntVec>(UnorderedElementsAre()), |
| 4987 | Eq("isn't empty")); |
| 4988 | EXPECT_THAT( |
| 4989 | DescribeNegation<IntVec>(UnorderedElementsAre(345)), |
| 4990 | Eq("doesn't have 1 element, or has 1 element that isn't equal to 345")); |
| 4991 | EXPECT_THAT( |
| 4992 | DescribeNegation<IntVec>(UnorderedElementsAre(123, 234, 345)), |
| 4993 | Eq("doesn't have 3 elements, or there exists no permutation " |
| 4994 | "of elements such that:\n" |
| 4995 | " - element #0 is equal to 123, and\n" |
| 4996 | " - element #1 is equal to 234, and\n" |
| 4997 | " - element #2 is equal to 345")); |
| 4998 | } |
| 4999 | |
| 5000 | namespace { |
| 5001 | |
| 5002 | // Used as a check on the more complex max flow method used in the |
| 5003 | // real testing::internal::FindMaxBipartiteMatching. This method is |
| 5004 | // compatible but runs in worst-case factorial time, so we only |
| 5005 | // use it in testing for small problem sizes. |
| 5006 | template <typename Graph> |
| 5007 | class BacktrackingMaxBPMState { |
| 5008 | public: |
| 5009 | // Does not take ownership of 'g'. |
| 5010 | explicit BacktrackingMaxBPMState(const Graph* g) : graph_(g) { } |
| 5011 | |
| 5012 | ElementMatcherPairs Compute() { |
| 5013 | if (graph_->LhsSize() == 0 || graph_->RhsSize() == 0) { |
| 5014 | return best_so_far_; |
| 5015 | } |
| 5016 | lhs_used_.assign(graph_->LhsSize(), kUnused); |
| 5017 | rhs_used_.assign(graph_->RhsSize(), kUnused); |
| 5018 | for (size_t irhs = 0; irhs < graph_->RhsSize(); ++irhs) { |
| 5019 | matches_.clear(); |
| 5020 | RecurseInto(irhs); |
| 5021 | if (best_so_far_.size() == graph_->RhsSize()) |
| 5022 | break; |
| 5023 | } |
| 5024 | return best_so_far_; |
| 5025 | } |
| 5026 | |
| 5027 | private: |
| 5028 | static const size_t kUnused = static_cast<size_t>(-1); |
| 5029 | |
| 5030 | void PushMatch(size_t lhs, size_t rhs) { |
| 5031 | matches_.push_back(ElementMatcherPair(lhs, rhs)); |
| 5032 | lhs_used_[lhs] = rhs; |
| 5033 | rhs_used_[rhs] = lhs; |
| 5034 | if (matches_.size() > best_so_far_.size()) { |
| 5035 | best_so_far_ = matches_; |
| 5036 | } |
| 5037 | } |
| 5038 | |
| 5039 | void PopMatch() { |
| 5040 | const ElementMatcherPair& back = matches_.back(); |
| 5041 | lhs_used_[back.first] = kUnused; |
| 5042 | rhs_used_[back.second] = kUnused; |
| 5043 | matches_.pop_back(); |
| 5044 | } |
| 5045 | |
| 5046 | bool RecurseInto(size_t irhs) { |
| 5047 | if (rhs_used_[irhs] != kUnused) { |
| 5048 | return true; |
| 5049 | } |
| 5050 | for (size_t ilhs = 0; ilhs < graph_->LhsSize(); ++ilhs) { |
| 5051 | if (lhs_used_[ilhs] != kUnused) { |
| 5052 | continue; |
| 5053 | } |
| 5054 | if (!graph_->HasEdge(ilhs, irhs)) { |
| 5055 | continue; |
| 5056 | } |
| 5057 | PushMatch(ilhs, irhs); |
| 5058 | if (best_so_far_.size() == graph_->RhsSize()) { |
| 5059 | return false; |
| 5060 | } |
| 5061 | for (size_t mi = irhs + 1; mi < graph_->RhsSize(); ++mi) { |
| 5062 | if (!RecurseInto(mi)) return false; |
| 5063 | } |
| 5064 | PopMatch(); |
| 5065 | } |
| 5066 | return true; |
| 5067 | } |
| 5068 | |
| 5069 | const Graph* graph_; // not owned |
| 5070 | std::vector<size_t> lhs_used_; |
| 5071 | std::vector<size_t> rhs_used_; |
| 5072 | ElementMatcherPairs matches_; |
| 5073 | ElementMatcherPairs best_so_far_; |
| 5074 | }; |
| 5075 | |
| 5076 | template <typename Graph> |
| 5077 | const size_t BacktrackingMaxBPMState<Graph>::kUnused; |
| 5078 | |
| 5079 | } // namespace |
| 5080 | |
| 5081 | // Implement a simple backtracking algorithm to determine if it is possible |
| 5082 | // to find one element per matcher, without reusing elements. |
| 5083 | template <typename Graph> |
| 5084 | ElementMatcherPairs |
| 5085 | FindBacktrackingMaxBPM(const Graph& g) { |
| 5086 | return BacktrackingMaxBPMState<Graph>(&g).Compute(); |
| 5087 | } |
| 5088 | |
| 5089 | class BacktrackingBPMTest : public ::testing::Test { }; |
| 5090 | |
| 5091 | // Tests the MaxBipartiteMatching algorithm with square matrices. |
| 5092 | // The single int param is the # of nodes on each of the left and right sides. |
| 5093 | class BipartiteTest : public ::testing::TestWithParam<int> { }; |
| 5094 | |
| 5095 | // Verify all match graphs up to some moderate number of edges. |
| 5096 | TEST_P(BipartiteTest, Exhaustive) { |
| 5097 | int nodes = GetParam(); |
| 5098 | MatchMatrix graph(nodes, nodes); |
| 5099 | do { |
| 5100 | ElementMatcherPairs matches = |
| 5101 | internal::FindMaxBipartiteMatching(graph); |
| 5102 | EXPECT_EQ(FindBacktrackingMaxBPM(graph).size(), matches.size()) |
| 5103 | << "graph: " << graph.DebugString(); |
| 5104 | // Check that all elements of matches are in the graph. |
| 5105 | // Check that elements of first and second are unique. |
| 5106 | std::vector<bool> seen_element(graph.LhsSize()); |
| 5107 | std::vector<bool> seen_matcher(graph.RhsSize()); |
| 5108 | SCOPED_TRACE(PrintToString(matches)); |
| 5109 | for (size_t i = 0; i < matches.size(); ++i) { |
| 5110 | size_t ilhs = matches[i].first; |
| 5111 | size_t irhs = matches[i].second; |
| 5112 | EXPECT_TRUE(graph.HasEdge(ilhs, irhs)); |
| 5113 | EXPECT_FALSE(seen_element[ilhs]); |
| 5114 | EXPECT_FALSE(seen_matcher[irhs]); |
| 5115 | seen_element[ilhs] = true; |
| 5116 | seen_matcher[irhs] = true; |
| 5117 | } |
| 5118 | } while (graph.NextGraph()); |
| 5119 | } |
| 5120 | |
| 5121 | INSTANTIATE_TEST_CASE_P(AllGraphs, BipartiteTest, |
| 5122 | ::testing::Range(0, 5)); |
| 5123 | |
| 5124 | // Parameterized by a pair interpreted as (LhsSize, RhsSize). |
| 5125 | class BipartiteNonSquareTest |
| 5126 | : public ::testing::TestWithParam<std::pair<size_t, size_t> > { |
| 5127 | }; |
| 5128 | |
| 5129 | TEST_F(BipartiteNonSquareTest, SimpleBacktracking) { |
| 5130 | // ....... |
| 5131 | // 0:-----\ : |
| 5132 | // 1:---\ | : |
| 5133 | // 2:---\ | : |
| 5134 | // 3:-\ | | : |
| 5135 | // :.......: |
| 5136 | // 0 1 2 |
| 5137 | MatchMatrix g(4, 3); |
| 5138 | static const int kEdges[][2] = {{0, 2}, {1, 1}, {2, 1}, {3, 0}}; |
| 5139 | for (size_t i = 0; i < GTEST_ARRAY_SIZE_(kEdges); ++i) { |
| 5140 | g.SetEdge(kEdges[i][0], kEdges[i][1], true); |
| 5141 | } |
| 5142 | EXPECT_THAT(FindBacktrackingMaxBPM(g), |
| 5143 | ElementsAre(Pair(3, 0), |
| 5144 | Pair(AnyOf(1, 2), 1), |
| 5145 | Pair(0, 2))) << g.DebugString(); |
| 5146 | } |
| 5147 | |
| 5148 | // Verify a few nonsquare matrices. |
| 5149 | TEST_P(BipartiteNonSquareTest, Exhaustive) { |
| 5150 | size_t nlhs = GetParam().first; |
| 5151 | size_t nrhs = GetParam().second; |
| 5152 | MatchMatrix graph(nlhs, nrhs); |
| 5153 | do { |
| 5154 | EXPECT_EQ(FindBacktrackingMaxBPM(graph).size(), |
| 5155 | internal::FindMaxBipartiteMatching(graph).size()) |
| 5156 | << "graph: " << graph.DebugString() |
| 5157 | << "\nbacktracking: " |
| 5158 | << PrintToString(FindBacktrackingMaxBPM(graph)) |
| 5159 | << "\nmax flow: " |
| 5160 | << PrintToString(internal::FindMaxBipartiteMatching(graph)); |
| 5161 | } while (graph.NextGraph()); |
| 5162 | } |
| 5163 | |
| 5164 | INSTANTIATE_TEST_CASE_P(AllGraphs, BipartiteNonSquareTest, |
| 5165 | testing::Values( |
| 5166 | std::make_pair(1, 2), |
| 5167 | std::make_pair(2, 1), |
| 5168 | std::make_pair(3, 2), |
| 5169 | std::make_pair(2, 3), |
| 5170 | std::make_pair(4, 1), |
| 5171 | std::make_pair(1, 4), |
| 5172 | std::make_pair(4, 3), |
| 5173 | std::make_pair(3, 4))); |
| 5174 | |
| 5175 | class BipartiteRandomTest |
| 5176 | : public ::testing::TestWithParam<std::pair<int, int> > { |
| 5177 | }; |
| 5178 | |
| 5179 | // Verifies a large sample of larger graphs. |
| 5180 | TEST_P(BipartiteRandomTest, LargerNets) { |
| 5181 | int nodes = GetParam().first; |
| 5182 | int iters = GetParam().second; |
| 5183 | MatchMatrix graph(nodes, nodes); |
| 5184 | |
| 5185 | testing::internal::Int32 seed = GTEST_FLAG(random_seed); |
| 5186 | if (seed == 0) { |
| 5187 | seed = static_cast<testing::internal::Int32>(time(NULL)); |
| 5188 | } |
| 5189 | |
| 5190 | for (; iters > 0; --iters, ++seed) { |
| 5191 | srand(static_cast<int>(seed)); |
| 5192 | graph.Randomize(); |
| 5193 | EXPECT_EQ(FindBacktrackingMaxBPM(graph).size(), |
| 5194 | internal::FindMaxBipartiteMatching(graph).size()) |
| 5195 | << " graph: " << graph.DebugString() |
| 5196 | << "\nTo reproduce the failure, rerun the test with the flag" |
| 5197 | " --" << GTEST_FLAG_PREFIX_ << "random_seed=" << seed; |
| 5198 | } |
| 5199 | } |
| 5200 | |
| 5201 | // Test argument is a std::pair<int, int> representing (nodes, iters). |
| 5202 | INSTANTIATE_TEST_CASE_P(Samples, BipartiteRandomTest, |
| 5203 | testing::Values( |
| 5204 | std::make_pair(5, 10000), |
| 5205 | std::make_pair(6, 5000), |
| 5206 | std::make_pair(7, 2000), |
| 5207 | std::make_pair(8, 500), |
| 5208 | std::make_pair(9, 100))); |
| 5209 | |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 5210 | // Tests IsReadableTypeName(). |
| 5211 | |
| 5212 | TEST(IsReadableTypeNameTest, ReturnsTrueForShortNames) { |
| 5213 | EXPECT_TRUE(IsReadableTypeName("int")); |
| 5214 | EXPECT_TRUE(IsReadableTypeName("const unsigned char*")); |
| 5215 | EXPECT_TRUE(IsReadableTypeName("MyMap<int, void*>")); |
| 5216 | EXPECT_TRUE(IsReadableTypeName("void (*)(int, bool)")); |
| 5217 | } |
| 5218 | |
| 5219 | TEST(IsReadableTypeNameTest, ReturnsTrueForLongNonTemplateNonFunctionNames) { |
| 5220 | EXPECT_TRUE(IsReadableTypeName("my_long_namespace::MyClassName")); |
| 5221 | EXPECT_TRUE(IsReadableTypeName("int [5][6][7][8][9][10][11]")); |
| 5222 | EXPECT_TRUE(IsReadableTypeName("my_namespace::MyOuterClass::MyInnerClass")); |
| 5223 | } |
| 5224 | |
| 5225 | TEST(IsReadableTypeNameTest, ReturnsFalseForLongTemplateNames) { |
| 5226 | EXPECT_FALSE( |
| 5227 | IsReadableTypeName("basic_string<char, std::char_traits<char> >")); |
| 5228 | EXPECT_FALSE(IsReadableTypeName("std::vector<int, std::alloc_traits<int> >")); |
| 5229 | } |
| 5230 | |
| 5231 | TEST(IsReadableTypeNameTest, ReturnsFalseForLongFunctionTypeNames) { |
| 5232 | EXPECT_FALSE(IsReadableTypeName("void (&)(int, bool, char, float)")); |
| 5233 | } |
| 5234 | |
| 5235 | // Tests JoinAsTuple(). |
| 5236 | |
| 5237 | TEST(JoinAsTupleTest, JoinsEmptyTuple) { |
| 5238 | EXPECT_EQ("", JoinAsTuple(Strings())); |
| 5239 | } |
| 5240 | |
| 5241 | TEST(JoinAsTupleTest, JoinsOneTuple) { |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 5242 | const char* fields[] = {"1"}; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 5243 | EXPECT_EQ("1", JoinAsTuple(Strings(fields, fields + 1))); |
| 5244 | } |
| 5245 | |
| 5246 | TEST(JoinAsTupleTest, JoinsTwoTuple) { |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 5247 | const char* fields[] = {"1", "a"}; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 5248 | EXPECT_EQ("(1, a)", JoinAsTuple(Strings(fields, fields + 2))); |
| 5249 | } |
| 5250 | |
| 5251 | TEST(JoinAsTupleTest, JoinsTenTuple) { |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 5252 | const char* fields[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 5253 | EXPECT_EQ("(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)", |
| 5254 | JoinAsTuple(Strings(fields, fields + 10))); |
| 5255 | } |
| 5256 | |
| 5257 | // Tests FormatMatcherDescription(). |
| 5258 | |
| 5259 | TEST(FormatMatcherDescriptionTest, WorksForEmptyDescription) { |
| 5260 | EXPECT_EQ("is even", |
| 5261 | FormatMatcherDescription(false, "IsEven", Strings())); |
| 5262 | EXPECT_EQ("not (is even)", |
| 5263 | FormatMatcherDescription(true, "IsEven", Strings())); |
| 5264 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 5265 | const char* params[] = {"5"}; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 5266 | EXPECT_EQ("equals 5", |
| 5267 | FormatMatcherDescription(false, "Equals", |
| 5268 | Strings(params, params + 1))); |
| 5269 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 5270 | const char* params2[] = {"5", "8"}; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 5271 | EXPECT_EQ("is in range (5, 8)", |
| 5272 | FormatMatcherDescription(false, "IsInRange", |
| 5273 | Strings(params2, params2 + 2))); |
| 5274 | } |
| 5275 | |
| 5276 | // Tests PolymorphicMatcher::mutable_impl(). |
| 5277 | TEST(PolymorphicMatcherTest, CanAccessMutableImpl) { |
| 5278 | PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42)); |
| 5279 | DivisibleByImpl& impl = m.mutable_impl(); |
| 5280 | EXPECT_EQ(42, impl.divider()); |
| 5281 | |
| 5282 | impl.set_divider(0); |
| 5283 | EXPECT_EQ(0, m.mutable_impl().divider()); |
| 5284 | } |
| 5285 | |
| 5286 | // Tests PolymorphicMatcher::impl(). |
| 5287 | TEST(PolymorphicMatcherTest, CanAccessImpl) { |
| 5288 | const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42)); |
| 5289 | const DivisibleByImpl& impl = m.impl(); |
| 5290 | EXPECT_EQ(42, impl.divider()); |
| 5291 | } |
| 5292 | |
| 5293 | TEST(MatcherTupleTest, ExplainsMatchFailure) { |
| 5294 | stringstream ss1; |
| 5295 | ExplainMatchFailureTupleTo(make_tuple(Matcher<char>(Eq('a')), GreaterThan(5)), |
| 5296 | make_tuple('a', 10), &ss1); |
| 5297 | EXPECT_EQ("", ss1.str()); // Successful match. |
| 5298 | |
| 5299 | stringstream ss2; |
| 5300 | ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))), |
| 5301 | make_tuple(2, 'b'), &ss2); |
| 5302 | EXPECT_EQ(" Expected arg #0: is > 5\n" |
| 5303 | " Actual: 2, which is 3 less than 5\n" |
| 5304 | " Expected arg #1: is equal to 'a' (97, 0x61)\n" |
| 5305 | " Actual: 'b' (98, 0x62)\n", |
| 5306 | ss2.str()); // Failed match where both arguments need explanation. |
| 5307 | |
| 5308 | stringstream ss3; |
| 5309 | ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))), |
| 5310 | make_tuple(2, 'a'), &ss3); |
| 5311 | EXPECT_EQ(" Expected arg #0: is > 5\n" |
| 5312 | " Actual: 2, which is 3 less than 5\n", |
| 5313 | ss3.str()); // Failed match where only one argument needs |
| 5314 | // explanation. |
| 5315 | } |
| 5316 | |
| 5317 | // Tests Each(). |
| 5318 | |
| 5319 | TEST(EachTest, ExplainsMatchResultCorrectly) { |
| 5320 | set<int> a; // empty |
| 5321 | |
| 5322 | Matcher<set<int> > m = Each(2); |
| 5323 | EXPECT_EQ("", Explain(m, a)); |
| 5324 | |
| 5325 | Matcher<const int(&)[1]> n = Each(1); // NOLINT |
| 5326 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 5327 | const int b[1] = {1}; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 5328 | EXPECT_EQ("", Explain(n, b)); |
| 5329 | |
| 5330 | n = Each(3); |
| 5331 | EXPECT_EQ("whose element #0 doesn't match", Explain(n, b)); |
| 5332 | |
| 5333 | a.insert(1); |
| 5334 | a.insert(2); |
| 5335 | a.insert(3); |
| 5336 | m = Each(GreaterThan(0)); |
| 5337 | EXPECT_EQ("", Explain(m, a)); |
| 5338 | |
| 5339 | m = Each(GreaterThan(10)); |
| 5340 | EXPECT_EQ("whose element #0 doesn't match, which is 9 less than 10", |
| 5341 | Explain(m, a)); |
| 5342 | } |
| 5343 | |
| 5344 | TEST(EachTest, DescribesItselfCorrectly) { |
| 5345 | Matcher<vector<int> > m = Each(1); |
| 5346 | EXPECT_EQ("only contains elements that is equal to 1", Describe(m)); |
| 5347 | |
| 5348 | Matcher<vector<int> > m2 = Not(m); |
| 5349 | EXPECT_EQ("contains some element that isn't equal to 1", Describe(m2)); |
| 5350 | } |
| 5351 | |
| 5352 | TEST(EachTest, MatchesVectorWhenAllElementsMatch) { |
| 5353 | vector<int> some_vector; |
| 5354 | EXPECT_THAT(some_vector, Each(1)); |
| 5355 | some_vector.push_back(3); |
| 5356 | EXPECT_THAT(some_vector, Not(Each(1))); |
| 5357 | EXPECT_THAT(some_vector, Each(3)); |
| 5358 | some_vector.push_back(1); |
| 5359 | some_vector.push_back(2); |
| 5360 | EXPECT_THAT(some_vector, Not(Each(3))); |
| 5361 | EXPECT_THAT(some_vector, Each(Lt(3.5))); |
| 5362 | |
| 5363 | vector<string> another_vector; |
| 5364 | another_vector.push_back("fee"); |
| 5365 | EXPECT_THAT(another_vector, Each(string("fee"))); |
| 5366 | another_vector.push_back("fie"); |
| 5367 | another_vector.push_back("foe"); |
| 5368 | another_vector.push_back("fum"); |
| 5369 | EXPECT_THAT(another_vector, Not(Each(string("fee")))); |
| 5370 | } |
| 5371 | |
| 5372 | TEST(EachTest, MatchesMapWhenAllElementsMatch) { |
| 5373 | map<const char*, int> my_map; |
| 5374 | const char* bar = "a string"; |
| 5375 | my_map[bar] = 2; |
| 5376 | EXPECT_THAT(my_map, Each(make_pair(bar, 2))); |
| 5377 | |
| 5378 | map<string, int> another_map; |
| 5379 | EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1))); |
| 5380 | another_map["fee"] = 1; |
| 5381 | EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1))); |
| 5382 | another_map["fie"] = 2; |
| 5383 | another_map["foe"] = 3; |
| 5384 | another_map["fum"] = 4; |
| 5385 | EXPECT_THAT(another_map, Not(Each(make_pair(string("fee"), 1)))); |
| 5386 | EXPECT_THAT(another_map, Not(Each(make_pair(string("fum"), 1)))); |
| 5387 | EXPECT_THAT(another_map, Each(Pair(_, Gt(0)))); |
| 5388 | } |
| 5389 | |
| 5390 | TEST(EachTest, AcceptsMatcher) { |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 5391 | const int a[] = {1, 2, 3}; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 5392 | EXPECT_THAT(a, Each(Gt(0))); |
| 5393 | EXPECT_THAT(a, Not(Each(Gt(1)))); |
| 5394 | } |
| 5395 | |
| 5396 | TEST(EachTest, WorksForNativeArrayAsTuple) { |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 5397 | const int a[] = {1, 2}; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 5398 | const int* const pointer = a; |
| 5399 | EXPECT_THAT(make_tuple(pointer, 2), Each(Gt(0))); |
| 5400 | EXPECT_THAT(make_tuple(pointer, 2), Not(Each(Gt(1)))); |
| 5401 | } |
| 5402 | |
| 5403 | // For testing Pointwise(). |
| 5404 | class IsHalfOfMatcher { |
| 5405 | public: |
| 5406 | template <typename T1, typename T2> |
| 5407 | bool MatchAndExplain(const tuple<T1, T2>& a_pair, |
| 5408 | MatchResultListener* listener) const { |
| 5409 | if (get<0>(a_pair) == get<1>(a_pair)/2) { |
| 5410 | *listener << "where the second is " << get<1>(a_pair); |
| 5411 | return true; |
| 5412 | } else { |
| 5413 | *listener << "where the second/2 is " << get<1>(a_pair)/2; |
| 5414 | return false; |
| 5415 | } |
| 5416 | } |
| 5417 | |
| 5418 | void DescribeTo(ostream* os) const { |
| 5419 | *os << "are a pair where the first is half of the second"; |
| 5420 | } |
| 5421 | |
| 5422 | void DescribeNegationTo(ostream* os) const { |
| 5423 | *os << "are a pair where the first isn't half of the second"; |
| 5424 | } |
| 5425 | }; |
| 5426 | |
| 5427 | PolymorphicMatcher<IsHalfOfMatcher> IsHalfOf() { |
| 5428 | return MakePolymorphicMatcher(IsHalfOfMatcher()); |
| 5429 | } |
| 5430 | |
| 5431 | TEST(PointwiseTest, DescribesSelf) { |
| 5432 | vector<int> rhs; |
| 5433 | rhs.push_back(1); |
| 5434 | rhs.push_back(2); |
| 5435 | rhs.push_back(3); |
| 5436 | const Matcher<const vector<int>&> m = Pointwise(IsHalfOf(), rhs); |
| 5437 | EXPECT_EQ("contains 3 values, where each value and its corresponding value " |
| 5438 | "in { 1, 2, 3 } are a pair where the first is half of the second", |
| 5439 | Describe(m)); |
| 5440 | EXPECT_EQ("doesn't contain exactly 3 values, or contains a value x at some " |
| 5441 | "index i where x and the i-th value of { 1, 2, 3 } are a pair " |
| 5442 | "where the first isn't half of the second", |
| 5443 | DescribeNegation(m)); |
| 5444 | } |
| 5445 | |
| 5446 | TEST(PointwiseTest, MakesCopyOfRhs) { |
| 5447 | list<signed char> rhs; |
| 5448 | rhs.push_back(2); |
| 5449 | rhs.push_back(4); |
| 5450 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 5451 | int lhs[] = {1, 2}; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 5452 | const Matcher<const int (&)[2]> m = Pointwise(IsHalfOf(), rhs); |
| 5453 | EXPECT_THAT(lhs, m); |
| 5454 | |
| 5455 | // Changing rhs now shouldn't affect m, which made a copy of rhs. |
| 5456 | rhs.push_back(6); |
| 5457 | EXPECT_THAT(lhs, m); |
| 5458 | } |
| 5459 | |
| 5460 | TEST(PointwiseTest, WorksForLhsNativeArray) { |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 5461 | const int lhs[] = {1, 2, 3}; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 5462 | vector<int> rhs; |
| 5463 | rhs.push_back(2); |
| 5464 | rhs.push_back(4); |
| 5465 | rhs.push_back(6); |
| 5466 | EXPECT_THAT(lhs, Pointwise(Lt(), rhs)); |
| 5467 | EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs))); |
| 5468 | } |
| 5469 | |
| 5470 | TEST(PointwiseTest, WorksForRhsNativeArray) { |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 5471 | const int rhs[] = {1, 2, 3}; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 5472 | vector<int> lhs; |
| 5473 | lhs.push_back(2); |
| 5474 | lhs.push_back(4); |
| 5475 | lhs.push_back(6); |
| 5476 | EXPECT_THAT(lhs, Pointwise(Gt(), rhs)); |
| 5477 | EXPECT_THAT(lhs, Not(Pointwise(Lt(), rhs))); |
| 5478 | } |
| 5479 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 5480 | #if GTEST_HAS_STD_INITIALIZER_LIST_ |
| 5481 | |
| 5482 | TEST(PointwiseTest, WorksForRhsInitializerList) { |
| 5483 | const vector<int> lhs{2, 4, 6}; |
| 5484 | EXPECT_THAT(lhs, Pointwise(Gt(), {1, 2, 3})); |
| 5485 | EXPECT_THAT(lhs, Not(Pointwise(Lt(), {3, 3, 7}))); |
| 5486 | } |
| 5487 | |
| 5488 | #endif // GTEST_HAS_STD_INITIALIZER_LIST_ |
| 5489 | |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 5490 | TEST(PointwiseTest, RejectsWrongSize) { |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 5491 | const double lhs[2] = {1, 2}; |
| 5492 | const int rhs[1] = {0}; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 5493 | EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs))); |
| 5494 | EXPECT_EQ("which contains 2 values", |
| 5495 | Explain(Pointwise(Gt(), rhs), lhs)); |
| 5496 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 5497 | const int rhs2[3] = {0, 1, 2}; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 5498 | EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs2))); |
| 5499 | } |
| 5500 | |
| 5501 | TEST(PointwiseTest, RejectsWrongContent) { |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 5502 | const double lhs[3] = {1, 2, 3}; |
| 5503 | const int rhs[3] = {2, 6, 4}; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 5504 | EXPECT_THAT(lhs, Not(Pointwise(IsHalfOf(), rhs))); |
| 5505 | EXPECT_EQ("where the value pair (2, 6) at index #1 don't match, " |
| 5506 | "where the second/2 is 3", |
| 5507 | Explain(Pointwise(IsHalfOf(), rhs), lhs)); |
| 5508 | } |
| 5509 | |
| 5510 | TEST(PointwiseTest, AcceptsCorrectContent) { |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 5511 | const double lhs[3] = {1, 2, 3}; |
| 5512 | const int rhs[3] = {2, 4, 6}; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 5513 | EXPECT_THAT(lhs, Pointwise(IsHalfOf(), rhs)); |
| 5514 | EXPECT_EQ("", Explain(Pointwise(IsHalfOf(), rhs), lhs)); |
| 5515 | } |
| 5516 | |
| 5517 | TEST(PointwiseTest, AllowsMonomorphicInnerMatcher) { |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 5518 | const double lhs[3] = {1, 2, 3}; |
| 5519 | const int rhs[3] = {2, 4, 6}; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 5520 | const Matcher<tuple<const double&, const int&> > m1 = IsHalfOf(); |
| 5521 | EXPECT_THAT(lhs, Pointwise(m1, rhs)); |
| 5522 | EXPECT_EQ("", Explain(Pointwise(m1, rhs), lhs)); |
| 5523 | |
| 5524 | // This type works as a tuple<const double&, const int&> can be |
| 5525 | // implicitly cast to tuple<double, int>. |
| 5526 | const Matcher<tuple<double, int> > m2 = IsHalfOf(); |
| 5527 | EXPECT_THAT(lhs, Pointwise(m2, rhs)); |
| 5528 | EXPECT_EQ("", Explain(Pointwise(m2, rhs), lhs)); |
| 5529 | } |
| 5530 | |
David Ghandehari | fd76734 | 2017-02-13 12:56:12 -0800 | [diff] [blame] | 5531 | TEST(UnorderedPointwiseTest, DescribesSelf) { |
| 5532 | vector<int> rhs; |
| 5533 | rhs.push_back(1); |
| 5534 | rhs.push_back(2); |
| 5535 | rhs.push_back(3); |
| 5536 | const Matcher<const vector<int>&> m = UnorderedPointwise(IsHalfOf(), rhs); |
| 5537 | EXPECT_EQ( |
| 5538 | "has 3 elements and there exists some permutation of elements such " |
| 5539 | "that:\n" |
| 5540 | " - element #0 and 1 are a pair where the first is half of the second, " |
| 5541 | "and\n" |
| 5542 | " - element #1 and 2 are a pair where the first is half of the second, " |
| 5543 | "and\n" |
| 5544 | " - element #2 and 3 are a pair where the first is half of the second", |
| 5545 | Describe(m)); |
| 5546 | EXPECT_EQ( |
| 5547 | "doesn't have 3 elements, or there exists no permutation of elements " |
| 5548 | "such that:\n" |
| 5549 | " - element #0 and 1 are a pair where the first is half of the second, " |
| 5550 | "and\n" |
| 5551 | " - element #1 and 2 are a pair where the first is half of the second, " |
| 5552 | "and\n" |
| 5553 | " - element #2 and 3 are a pair where the first is half of the second", |
| 5554 | DescribeNegation(m)); |
| 5555 | } |
| 5556 | |
| 5557 | TEST(UnorderedPointwiseTest, MakesCopyOfRhs) { |
| 5558 | list<signed char> rhs; |
| 5559 | rhs.push_back(2); |
| 5560 | rhs.push_back(4); |
| 5561 | |
| 5562 | int lhs[] = {2, 1}; |
| 5563 | const Matcher<const int (&)[2]> m = UnorderedPointwise(IsHalfOf(), rhs); |
| 5564 | EXPECT_THAT(lhs, m); |
| 5565 | |
| 5566 | // Changing rhs now shouldn't affect m, which made a copy of rhs. |
| 5567 | rhs.push_back(6); |
| 5568 | EXPECT_THAT(lhs, m); |
| 5569 | } |
| 5570 | |
| 5571 | TEST(UnorderedPointwiseTest, WorksForLhsNativeArray) { |
| 5572 | const int lhs[] = {1, 2, 3}; |
| 5573 | vector<int> rhs; |
| 5574 | rhs.push_back(4); |
| 5575 | rhs.push_back(6); |
| 5576 | rhs.push_back(2); |
| 5577 | EXPECT_THAT(lhs, UnorderedPointwise(Lt(), rhs)); |
| 5578 | EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs))); |
| 5579 | } |
| 5580 | |
| 5581 | TEST(UnorderedPointwiseTest, WorksForRhsNativeArray) { |
| 5582 | const int rhs[] = {1, 2, 3}; |
| 5583 | vector<int> lhs; |
| 5584 | lhs.push_back(4); |
| 5585 | lhs.push_back(2); |
| 5586 | lhs.push_back(6); |
| 5587 | EXPECT_THAT(lhs, UnorderedPointwise(Gt(), rhs)); |
| 5588 | EXPECT_THAT(lhs, Not(UnorderedPointwise(Lt(), rhs))); |
| 5589 | } |
| 5590 | |
| 5591 | #if GTEST_HAS_STD_INITIALIZER_LIST_ |
| 5592 | |
| 5593 | TEST(UnorderedPointwiseTest, WorksForRhsInitializerList) { |
| 5594 | const vector<int> lhs{2, 4, 6}; |
| 5595 | EXPECT_THAT(lhs, UnorderedPointwise(Gt(), {5, 1, 3})); |
| 5596 | EXPECT_THAT(lhs, Not(UnorderedPointwise(Lt(), {1, 1, 7}))); |
| 5597 | } |
| 5598 | |
| 5599 | #endif // GTEST_HAS_STD_INITIALIZER_LIST_ |
| 5600 | |
| 5601 | TEST(UnorderedPointwiseTest, RejectsWrongSize) { |
| 5602 | const double lhs[2] = {1, 2}; |
| 5603 | const int rhs[1] = {0}; |
| 5604 | EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs))); |
| 5605 | EXPECT_EQ("which has 2 elements", |
| 5606 | Explain(UnorderedPointwise(Gt(), rhs), lhs)); |
| 5607 | |
| 5608 | const int rhs2[3] = {0, 1, 2}; |
| 5609 | EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs2))); |
| 5610 | } |
| 5611 | |
| 5612 | TEST(UnorderedPointwiseTest, RejectsWrongContent) { |
| 5613 | const double lhs[3] = {1, 2, 3}; |
| 5614 | const int rhs[3] = {2, 6, 6}; |
| 5615 | EXPECT_THAT(lhs, Not(UnorderedPointwise(IsHalfOf(), rhs))); |
| 5616 | EXPECT_EQ("where the following elements don't match any matchers:\n" |
| 5617 | "element #1: 2", |
| 5618 | Explain(UnorderedPointwise(IsHalfOf(), rhs), lhs)); |
| 5619 | } |
| 5620 | |
| 5621 | TEST(UnorderedPointwiseTest, AcceptsCorrectContentInSameOrder) { |
| 5622 | const double lhs[3] = {1, 2, 3}; |
| 5623 | const int rhs[3] = {2, 4, 6}; |
| 5624 | EXPECT_THAT(lhs, UnorderedPointwise(IsHalfOf(), rhs)); |
| 5625 | } |
| 5626 | |
| 5627 | TEST(UnorderedPointwiseTest, AcceptsCorrectContentInDifferentOrder) { |
| 5628 | const double lhs[3] = {1, 2, 3}; |
| 5629 | const int rhs[3] = {6, 4, 2}; |
| 5630 | EXPECT_THAT(lhs, UnorderedPointwise(IsHalfOf(), rhs)); |
| 5631 | } |
| 5632 | |
| 5633 | TEST(UnorderedPointwiseTest, AllowsMonomorphicInnerMatcher) { |
| 5634 | const double lhs[3] = {1, 2, 3}; |
| 5635 | const int rhs[3] = {4, 6, 2}; |
| 5636 | const Matcher<tuple<const double&, const int&> > m1 = IsHalfOf(); |
| 5637 | EXPECT_THAT(lhs, UnorderedPointwise(m1, rhs)); |
| 5638 | |
| 5639 | // This type works as a tuple<const double&, const int&> can be |
| 5640 | // implicitly cast to tuple<double, int>. |
| 5641 | const Matcher<tuple<double, int> > m2 = IsHalfOf(); |
| 5642 | EXPECT_THAT(lhs, UnorderedPointwise(m2, rhs)); |
| 5643 | } |
| 5644 | |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 5645 | } // namespace gmock_matchers_test |
| 5646 | } // namespace testing |