/third_party/gmock/test/gmock-matchers_test.cc

https://code.google.com/ · C++ · 5247 lines · 3835 code · 837 blank · 575 comment · 70 complexity · 66027d3769968bdc28b9a9671a8d1e39 MD5 · raw file

Large files are truncated click here to view the full file

  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. // Google Mock - a framework for writing C++ mock classes.
  32. //
  33. // This file tests some commonly used argument matchers.
  34. #include "gmock/gmock-matchers.h"
  35. #include "gmock/gmock-more-matchers.h"
  36. #include <string.h>
  37. #include <time.h>
  38. #include <deque>
  39. #include <functional>
  40. #include <iostream>
  41. #include <iterator>
  42. #include <limits>
  43. #include <list>
  44. #include <map>
  45. #include <set>
  46. #include <sstream>
  47. #include <string>
  48. #include <utility>
  49. #include <vector>
  50. #include "gmock/gmock.h"
  51. #include "gtest/gtest.h"
  52. #include "gtest/gtest-spi.h"
  53. namespace testing {
  54. namespace internal {
  55. GTEST_API_ string JoinAsTuple(const Strings& fields);
  56. } // namespace internal
  57. namespace gmock_matchers_test {
  58. using std::greater;
  59. using std::less;
  60. using std::list;
  61. using std::make_pair;
  62. using std::map;
  63. using std::multimap;
  64. using std::multiset;
  65. using std::ostream;
  66. using std::pair;
  67. using std::set;
  68. using std::stringstream;
  69. using std::tr1::get;
  70. using std::tr1::make_tuple;
  71. using std::tr1::tuple;
  72. using std::vector;
  73. using testing::A;
  74. using testing::AllArgs;
  75. using testing::AllOf;
  76. using testing::An;
  77. using testing::AnyOf;
  78. using testing::ByRef;
  79. using testing::ContainsRegex;
  80. using testing::DoubleEq;
  81. using testing::DoubleNear;
  82. using testing::EndsWith;
  83. using testing::Eq;
  84. using testing::ExplainMatchResult;
  85. using testing::Field;
  86. using testing::FloatEq;
  87. using testing::FloatNear;
  88. using testing::Ge;
  89. using testing::Gt;
  90. using testing::HasSubstr;
  91. using testing::IsEmpty;
  92. using testing::IsNull;
  93. using testing::Key;
  94. using testing::Le;
  95. using testing::Lt;
  96. using testing::MakeMatcher;
  97. using testing::MakePolymorphicMatcher;
  98. using testing::MatchResultListener;
  99. using testing::Matcher;
  100. using testing::MatcherCast;
  101. using testing::MatcherInterface;
  102. using testing::Matches;
  103. using testing::MatchesRegex;
  104. using testing::NanSensitiveDoubleEq;
  105. using testing::NanSensitiveDoubleNear;
  106. using testing::NanSensitiveFloatEq;
  107. using testing::NanSensitiveFloatNear;
  108. using testing::Ne;
  109. using testing::Not;
  110. using testing::NotNull;
  111. using testing::Pair;
  112. using testing::Pointee;
  113. using testing::Pointwise;
  114. using testing::PolymorphicMatcher;
  115. using testing::Property;
  116. using testing::Ref;
  117. using testing::ResultOf;
  118. using testing::SizeIs;
  119. using testing::StartsWith;
  120. using testing::StringMatchResultListener;
  121. using testing::StrCaseEq;
  122. using testing::StrCaseNe;
  123. using testing::StrEq;
  124. using testing::StrNe;
  125. using testing::Truly;
  126. using testing::TypedEq;
  127. using testing::Value;
  128. using testing::WhenSorted;
  129. using testing::WhenSortedBy;
  130. using testing::_;
  131. using testing::internal::DummyMatchResultListener;
  132. using testing::internal::ElementMatcherPair;
  133. using testing::internal::ElementMatcherPairs;
  134. using testing::internal::ExplainMatchFailureTupleTo;
  135. using testing::internal::FloatingEqMatcher;
  136. using testing::internal::FormatMatcherDescription;
  137. using testing::internal::IsReadableTypeName;
  138. using testing::internal::JoinAsTuple;
  139. using testing::internal::MatchMatrix;
  140. using testing::internal::RE;
  141. using testing::internal::StreamMatchResultListener;
  142. using testing::internal::Strings;
  143. using testing::internal::linked_ptr;
  144. using testing::internal::scoped_ptr;
  145. using testing::internal::string;
  146. // Evaluates to the number of elements in 'array'.
  147. #define GMOCK_ARRAY_SIZE_(array) (sizeof(array) / sizeof(array[0]))
  148. // For testing ExplainMatchResultTo().
  149. class GreaterThanMatcher : public MatcherInterface<int> {
  150. public:
  151. explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
  152. virtual void DescribeTo(ostream* os) const {
  153. *os << "is > " << rhs_;
  154. }
  155. virtual bool MatchAndExplain(int lhs,
  156. MatchResultListener* listener) const {
  157. const int diff = lhs - rhs_;
  158. if (diff > 0) {
  159. *listener << "which is " << diff << " more than " << rhs_;
  160. } else if (diff == 0) {
  161. *listener << "which is the same as " << rhs_;
  162. } else {
  163. *listener << "which is " << -diff << " less than " << rhs_;
  164. }
  165. return lhs > rhs_;
  166. }
  167. private:
  168. int rhs_;
  169. };
  170. Matcher<int> GreaterThan(int n) {
  171. return MakeMatcher(new GreaterThanMatcher(n));
  172. }
  173. string OfType(const string& type_name) {
  174. #if GTEST_HAS_RTTI
  175. return " (of type " + type_name + ")";
  176. #else
  177. return "";
  178. #endif
  179. }
  180. // Returns the description of the given matcher.
  181. template <typename T>
  182. string Describe(const Matcher<T>& m) {
  183. stringstream ss;
  184. m.DescribeTo(&ss);
  185. return ss.str();
  186. }
  187. // Returns the description of the negation of the given matcher.
  188. template <typename T>
  189. string DescribeNegation(const Matcher<T>& m) {
  190. stringstream ss;
  191. m.DescribeNegationTo(&ss);
  192. return ss.str();
  193. }
  194. // Returns the reason why x matches, or doesn't match, m.
  195. template <typename MatcherType, typename Value>
  196. string Explain(const MatcherType& m, const Value& x) {
  197. StringMatchResultListener listener;
  198. ExplainMatchResult(m, x, &listener);
  199. return listener.str();
  200. }
  201. TEST(MatchResultListenerTest, StreamingWorks) {
  202. StringMatchResultListener listener;
  203. listener << "hi" << 5;
  204. EXPECT_EQ("hi5", listener.str());
  205. listener.Clear();
  206. EXPECT_EQ("", listener.str());
  207. listener << 42;
  208. EXPECT_EQ("42", listener.str());
  209. // Streaming shouldn't crash when the underlying ostream is NULL.
  210. DummyMatchResultListener dummy;
  211. dummy << "hi" << 5;
  212. }
  213. TEST(MatchResultListenerTest, CanAccessUnderlyingStream) {
  214. EXPECT_TRUE(DummyMatchResultListener().stream() == NULL);
  215. EXPECT_TRUE(StreamMatchResultListener(NULL).stream() == NULL);
  216. EXPECT_EQ(&std::cout, StreamMatchResultListener(&std::cout).stream());
  217. }
  218. TEST(MatchResultListenerTest, IsInterestedWorks) {
  219. EXPECT_TRUE(StringMatchResultListener().IsInterested());
  220. EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested());
  221. EXPECT_FALSE(DummyMatchResultListener().IsInterested());
  222. EXPECT_FALSE(StreamMatchResultListener(NULL).IsInterested());
  223. }
  224. // Makes sure that the MatcherInterface<T> interface doesn't
  225. // change.
  226. class EvenMatcherImpl : public MatcherInterface<int> {
  227. public:
  228. virtual bool MatchAndExplain(int x,
  229. MatchResultListener* /* listener */) const {
  230. return x % 2 == 0;
  231. }
  232. virtual void DescribeTo(ostream* os) const {
  233. *os << "is an even number";
  234. }
  235. // We deliberately don't define DescribeNegationTo() and
  236. // ExplainMatchResultTo() here, to make sure the definition of these
  237. // two methods is optional.
  238. };
  239. // Makes sure that the MatcherInterface API doesn't change.
  240. TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) {
  241. EvenMatcherImpl m;
  242. }
  243. // Tests implementing a monomorphic matcher using MatchAndExplain().
  244. class NewEvenMatcherImpl : public MatcherInterface<int> {
  245. public:
  246. virtual bool MatchAndExplain(int x, MatchResultListener* listener) const {
  247. const bool match = x % 2 == 0;
  248. // Verifies that we can stream to a listener directly.
  249. *listener << "value % " << 2;
  250. if (listener->stream() != NULL) {
  251. // Verifies that we can stream to a listener's underlying stream
  252. // too.
  253. *listener->stream() << " == " << (x % 2);
  254. }
  255. return match;
  256. }
  257. virtual void DescribeTo(ostream* os) const {
  258. *os << "is an even number";
  259. }
  260. };
  261. TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) {
  262. Matcher<int> m = MakeMatcher(new NewEvenMatcherImpl);
  263. EXPECT_TRUE(m.Matches(2));
  264. EXPECT_FALSE(m.Matches(3));
  265. EXPECT_EQ("value % 2 == 0", Explain(m, 2));
  266. EXPECT_EQ("value % 2 == 1", Explain(m, 3));
  267. }
  268. // Tests default-constructing a matcher.
  269. TEST(MatcherTest, CanBeDefaultConstructed) {
  270. Matcher<double> m;
  271. }
  272. // Tests that Matcher<T> can be constructed from a MatcherInterface<T>*.
  273. TEST(MatcherTest, CanBeConstructedFromMatcherInterface) {
  274. const MatcherInterface<int>* impl = new EvenMatcherImpl;
  275. Matcher<int> m(impl);
  276. EXPECT_TRUE(m.Matches(4));
  277. EXPECT_FALSE(m.Matches(5));
  278. }
  279. // Tests that value can be used in place of Eq(value).
  280. TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) {
  281. Matcher<int> m1 = 5;
  282. EXPECT_TRUE(m1.Matches(5));
  283. EXPECT_FALSE(m1.Matches(6));
  284. }
  285. // Tests that NULL can be used in place of Eq(NULL).
  286. TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) {
  287. Matcher<int*> m1 = NULL;
  288. EXPECT_TRUE(m1.Matches(NULL));
  289. int n = 0;
  290. EXPECT_FALSE(m1.Matches(&n));
  291. }
  292. // Tests that matchers are copyable.
  293. TEST(MatcherTest, IsCopyable) {
  294. // Tests the copy constructor.
  295. Matcher<bool> m1 = Eq(false);
  296. EXPECT_TRUE(m1.Matches(false));
  297. EXPECT_FALSE(m1.Matches(true));
  298. // Tests the assignment operator.
  299. m1 = Eq(true);
  300. EXPECT_TRUE(m1.Matches(true));
  301. EXPECT_FALSE(m1.Matches(false));
  302. }
  303. // Tests that Matcher<T>::DescribeTo() calls
  304. // MatcherInterface<T>::DescribeTo().
  305. TEST(MatcherTest, CanDescribeItself) {
  306. EXPECT_EQ("is an even number",
  307. Describe(Matcher<int>(new EvenMatcherImpl)));
  308. }
  309. // Tests Matcher<T>::MatchAndExplain().
  310. TEST(MatcherTest, MatchAndExplain) {
  311. Matcher<int> m = GreaterThan(0);
  312. StringMatchResultListener listener1;
  313. EXPECT_TRUE(m.MatchAndExplain(42, &listener1));
  314. EXPECT_EQ("which is 42 more than 0", listener1.str());
  315. StringMatchResultListener listener2;
  316. EXPECT_FALSE(m.MatchAndExplain(-9, &listener2));
  317. EXPECT_EQ("which is 9 less than 0", listener2.str());
  318. }
  319. // Tests that a C-string literal can be implicitly converted to a
  320. // Matcher<string> or Matcher<const string&>.
  321. TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
  322. Matcher<string> m1 = "hi";
  323. EXPECT_TRUE(m1.Matches("hi"));
  324. EXPECT_FALSE(m1.Matches("hello"));
  325. Matcher<const string&> m2 = "hi";
  326. EXPECT_TRUE(m2.Matches("hi"));
  327. EXPECT_FALSE(m2.Matches("hello"));
  328. }
  329. // Tests that a string object can be implicitly converted to a
  330. // Matcher<string> or Matcher<const string&>.
  331. TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {
  332. Matcher<string> m1 = string("hi");
  333. EXPECT_TRUE(m1.Matches("hi"));
  334. EXPECT_FALSE(m1.Matches("hello"));
  335. Matcher<const string&> m2 = string("hi");
  336. EXPECT_TRUE(m2.Matches("hi"));
  337. EXPECT_FALSE(m2.Matches("hello"));
  338. }
  339. #if GTEST_HAS_STRING_PIECE_
  340. // Tests that a C-string literal can be implicitly converted to a
  341. // Matcher<StringPiece> or Matcher<const StringPiece&>.
  342. TEST(StringPieceMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
  343. Matcher<StringPiece> m1 = "cats";
  344. EXPECT_TRUE(m1.Matches("cats"));
  345. EXPECT_FALSE(m1.Matches("dogs"));
  346. Matcher<const StringPiece&> m2 = "cats";
  347. EXPECT_TRUE(m2.Matches("cats"));
  348. EXPECT_FALSE(m2.Matches("dogs"));
  349. }
  350. // Tests that a string object can be implicitly converted to a
  351. // Matcher<StringPiece> or Matcher<const StringPiece&>.
  352. TEST(StringPieceMatcherTest, CanBeImplicitlyConstructedFromString) {
  353. Matcher<StringPiece> m1 = string("cats");
  354. EXPECT_TRUE(m1.Matches("cats"));
  355. EXPECT_FALSE(m1.Matches("dogs"));
  356. Matcher<const StringPiece&> m2 = string("cats");
  357. EXPECT_TRUE(m2.Matches("cats"));
  358. EXPECT_FALSE(m2.Matches("dogs"));
  359. }
  360. // Tests that a StringPiece object can be implicitly converted to a
  361. // Matcher<StringPiece> or Matcher<const StringPiece&>.
  362. TEST(StringPieceMatcherTest, CanBeImplicitlyConstructedFromStringPiece) {
  363. Matcher<StringPiece> m1 = StringPiece("cats");
  364. EXPECT_TRUE(m1.Matches("cats"));
  365. EXPECT_FALSE(m1.Matches("dogs"));
  366. Matcher<const StringPiece&> m2 = StringPiece("cats");
  367. EXPECT_TRUE(m2.Matches("cats"));
  368. EXPECT_FALSE(m2.Matches("dogs"));
  369. }
  370. #endif // GTEST_HAS_STRING_PIECE_
  371. // Tests that MakeMatcher() constructs a Matcher<T> from a
  372. // MatcherInterface* without requiring the user to explicitly
  373. // write the type.
  374. TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) {
  375. const MatcherInterface<int>* dummy_impl = NULL;
  376. Matcher<int> m = MakeMatcher(dummy_impl);
  377. }
  378. // Tests that MakePolymorphicMatcher() can construct a polymorphic
  379. // matcher from its implementation using the old API.
  380. const int g_bar = 1;
  381. class ReferencesBarOrIsZeroImpl {
  382. public:
  383. template <typename T>
  384. bool MatchAndExplain(const T& x,
  385. MatchResultListener* /* listener */) const {
  386. const void* p = &x;
  387. return p == &g_bar || x == 0;
  388. }
  389. void DescribeTo(ostream* os) const { *os << "g_bar or zero"; }
  390. void DescribeNegationTo(ostream* os) const {
  391. *os << "doesn't reference g_bar and is not zero";
  392. }
  393. };
  394. // This function verifies that MakePolymorphicMatcher() returns a
  395. // PolymorphicMatcher<T> where T is the argument's type.
  396. PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() {
  397. return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl());
  398. }
  399. TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) {
  400. // Using a polymorphic matcher to match a reference type.
  401. Matcher<const int&> m1 = ReferencesBarOrIsZero();
  402. EXPECT_TRUE(m1.Matches(0));
  403. // Verifies that the identity of a by-reference argument is preserved.
  404. EXPECT_TRUE(m1.Matches(g_bar));
  405. EXPECT_FALSE(m1.Matches(1));
  406. EXPECT_EQ("g_bar or zero", Describe(m1));
  407. // Using a polymorphic matcher to match a value type.
  408. Matcher<double> m2 = ReferencesBarOrIsZero();
  409. EXPECT_TRUE(m2.Matches(0.0));
  410. EXPECT_FALSE(m2.Matches(0.1));
  411. EXPECT_EQ("g_bar or zero", Describe(m2));
  412. }
  413. // Tests implementing a polymorphic matcher using MatchAndExplain().
  414. class PolymorphicIsEvenImpl {
  415. public:
  416. void DescribeTo(ostream* os) const { *os << "is even"; }
  417. void DescribeNegationTo(ostream* os) const {
  418. *os << "is odd";
  419. }
  420. template <typename T>
  421. bool MatchAndExplain(const T& x, MatchResultListener* listener) const {
  422. // Verifies that we can stream to the listener directly.
  423. *listener << "% " << 2;
  424. if (listener->stream() != NULL) {
  425. // Verifies that we can stream to the listener's underlying stream
  426. // too.
  427. *listener->stream() << " == " << (x % 2);
  428. }
  429. return (x % 2) == 0;
  430. }
  431. };
  432. PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() {
  433. return MakePolymorphicMatcher(PolymorphicIsEvenImpl());
  434. }
  435. TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) {
  436. // Using PolymorphicIsEven() as a Matcher<int>.
  437. const Matcher<int> m1 = PolymorphicIsEven();
  438. EXPECT_TRUE(m1.Matches(42));
  439. EXPECT_FALSE(m1.Matches(43));
  440. EXPECT_EQ("is even", Describe(m1));
  441. const Matcher<int> not_m1 = Not(m1);
  442. EXPECT_EQ("is odd", Describe(not_m1));
  443. EXPECT_EQ("% 2 == 0", Explain(m1, 42));
  444. // Using PolymorphicIsEven() as a Matcher<char>.
  445. const Matcher<char> m2 = PolymorphicIsEven();
  446. EXPECT_TRUE(m2.Matches('\x42'));
  447. EXPECT_FALSE(m2.Matches('\x43'));
  448. EXPECT_EQ("is even", Describe(m2));
  449. const Matcher<char> not_m2 = Not(m2);
  450. EXPECT_EQ("is odd", Describe(not_m2));
  451. EXPECT_EQ("% 2 == 0", Explain(m2, '\x42'));
  452. }
  453. // Tests that MatcherCast<T>(m) works when m is a polymorphic matcher.
  454. TEST(MatcherCastTest, FromPolymorphicMatcher) {
  455. Matcher<int> m = MatcherCast<int>(Eq(5));
  456. EXPECT_TRUE(m.Matches(5));
  457. EXPECT_FALSE(m.Matches(6));
  458. }
  459. // For testing casting matchers between compatible types.
  460. class IntValue {
  461. public:
  462. // An int can be statically (although not implicitly) cast to a
  463. // IntValue.
  464. explicit IntValue(int a_value) : value_(a_value) {}
  465. int value() const { return value_; }
  466. private:
  467. int value_;
  468. };
  469. // For testing casting matchers between compatible types.
  470. bool IsPositiveIntValue(const IntValue& foo) {
  471. return foo.value() > 0;
  472. }
  473. // Tests that MatcherCast<T>(m) works when m is a Matcher<U> where T
  474. // can be statically converted to U.
  475. TEST(MatcherCastTest, FromCompatibleType) {
  476. Matcher<double> m1 = Eq(2.0);
  477. Matcher<int> m2 = MatcherCast<int>(m1);
  478. EXPECT_TRUE(m2.Matches(2));
  479. EXPECT_FALSE(m2.Matches(3));
  480. Matcher<IntValue> m3 = Truly(IsPositiveIntValue);
  481. Matcher<int> m4 = MatcherCast<int>(m3);
  482. // In the following, the arguments 1 and 0 are statically converted
  483. // to IntValue objects, and then tested by the IsPositiveIntValue()
  484. // predicate.
  485. EXPECT_TRUE(m4.Matches(1));
  486. EXPECT_FALSE(m4.Matches(0));
  487. }
  488. // Tests that MatcherCast<T>(m) works when m is a Matcher<const T&>.
  489. TEST(MatcherCastTest, FromConstReferenceToNonReference) {
  490. Matcher<const int&> m1 = Eq(0);
  491. Matcher<int> m2 = MatcherCast<int>(m1);
  492. EXPECT_TRUE(m2.Matches(0));
  493. EXPECT_FALSE(m2.Matches(1));
  494. }
  495. // Tests that MatcherCast<T>(m) works when m is a Matcher<T&>.
  496. TEST(MatcherCastTest, FromReferenceToNonReference) {
  497. Matcher<int&> m1 = Eq(0);
  498. Matcher<int> m2 = MatcherCast<int>(m1);
  499. EXPECT_TRUE(m2.Matches(0));
  500. EXPECT_FALSE(m2.Matches(1));
  501. }
  502. // Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
  503. TEST(MatcherCastTest, FromNonReferenceToConstReference) {
  504. Matcher<int> m1 = Eq(0);
  505. Matcher<const int&> m2 = MatcherCast<const int&>(m1);
  506. EXPECT_TRUE(m2.Matches(0));
  507. EXPECT_FALSE(m2.Matches(1));
  508. }
  509. // Tests that MatcherCast<T&>(m) works when m is a Matcher<T>.
  510. TEST(MatcherCastTest, FromNonReferenceToReference) {
  511. Matcher<int> m1 = Eq(0);
  512. Matcher<int&> m2 = MatcherCast<int&>(m1);
  513. int n = 0;
  514. EXPECT_TRUE(m2.Matches(n));
  515. n = 1;
  516. EXPECT_FALSE(m2.Matches(n));
  517. }
  518. // Tests that MatcherCast<T>(m) works when m is a Matcher<T>.
  519. TEST(MatcherCastTest, FromSameType) {
  520. Matcher<int> m1 = Eq(0);
  521. Matcher<int> m2 = MatcherCast<int>(m1);
  522. EXPECT_TRUE(m2.Matches(0));
  523. EXPECT_FALSE(m2.Matches(1));
  524. }
  525. // Implicitly convertible form any type.
  526. struct ConvertibleFromAny {
  527. ConvertibleFromAny(int a_value) : value(a_value) {}
  528. template <typename T>
  529. ConvertibleFromAny(const T& a_value) : value(-1) {
  530. ADD_FAILURE() << "Conversion constructor called";
  531. }
  532. int value;
  533. };
  534. bool operator==(const ConvertibleFromAny& a, const ConvertibleFromAny& b) {
  535. return a.value == b.value;
  536. }
  537. ostream& operator<<(ostream& os, const ConvertibleFromAny& a) {
  538. return os << a.value;
  539. }
  540. TEST(MatcherCastTest, ConversionConstructorIsUsed) {
  541. Matcher<ConvertibleFromAny> m = MatcherCast<ConvertibleFromAny>(1);
  542. EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
  543. EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
  544. }
  545. TEST(MatcherCastTest, FromConvertibleFromAny) {
  546. Matcher<ConvertibleFromAny> m =
  547. MatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
  548. EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
  549. EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
  550. }
  551. class Base {};
  552. class Derived : public Base {};
  553. // Tests that SafeMatcherCast<T>(m) works when m is a polymorphic matcher.
  554. TEST(SafeMatcherCastTest, FromPolymorphicMatcher) {
  555. Matcher<char> m2 = SafeMatcherCast<char>(Eq(32));
  556. EXPECT_TRUE(m2.Matches(' '));
  557. EXPECT_FALSE(m2.Matches('\n'));
  558. }
  559. // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where
  560. // T and U are arithmetic types and T can be losslessly converted to
  561. // U.
  562. TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) {
  563. Matcher<double> m1 = DoubleEq(1.0);
  564. Matcher<float> m2 = SafeMatcherCast<float>(m1);
  565. EXPECT_TRUE(m2.Matches(1.0f));
  566. EXPECT_FALSE(m2.Matches(2.0f));
  567. Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>('a'));
  568. EXPECT_TRUE(m3.Matches('a'));
  569. EXPECT_FALSE(m3.Matches('b'));
  570. }
  571. // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where T and U
  572. // are pointers or references to a derived and a base class, correspondingly.
  573. TEST(SafeMatcherCastTest, FromBaseClass) {
  574. Derived d, d2;
  575. Matcher<Base*> m1 = Eq(&d);
  576. Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1);
  577. EXPECT_TRUE(m2.Matches(&d));
  578. EXPECT_FALSE(m2.Matches(&d2));
  579. Matcher<Base&> m3 = Ref(d);
  580. Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3);
  581. EXPECT_TRUE(m4.Matches(d));
  582. EXPECT_FALSE(m4.Matches(d2));
  583. }
  584. // Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<const T&>.
  585. TEST(SafeMatcherCastTest, FromConstReferenceToReference) {
  586. int n = 0;
  587. Matcher<const int&> m1 = Ref(n);
  588. Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
  589. int n1 = 0;
  590. EXPECT_TRUE(m2.Matches(n));
  591. EXPECT_FALSE(m2.Matches(n1));
  592. }
  593. // Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
  594. TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) {
  595. Matcher<int> m1 = Eq(0);
  596. Matcher<const int&> m2 = SafeMatcherCast<const int&>(m1);
  597. EXPECT_TRUE(m2.Matches(0));
  598. EXPECT_FALSE(m2.Matches(1));
  599. }
  600. // Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<T>.
  601. TEST(SafeMatcherCastTest, FromNonReferenceToReference) {
  602. Matcher<int> m1 = Eq(0);
  603. Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
  604. int n = 0;
  605. EXPECT_TRUE(m2.Matches(n));
  606. n = 1;
  607. EXPECT_FALSE(m2.Matches(n));
  608. }
  609. // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<T>.
  610. TEST(SafeMatcherCastTest, FromSameType) {
  611. Matcher<int> m1 = Eq(0);
  612. Matcher<int> m2 = SafeMatcherCast<int>(m1);
  613. EXPECT_TRUE(m2.Matches(0));
  614. EXPECT_FALSE(m2.Matches(1));
  615. }
  616. TEST(SafeMatcherCastTest, ConversionConstructorIsUsed) {
  617. Matcher<ConvertibleFromAny> m = SafeMatcherCast<ConvertibleFromAny>(1);
  618. EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
  619. EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
  620. }
  621. TEST(SafeMatcherCastTest, FromConvertibleFromAny) {
  622. Matcher<ConvertibleFromAny> m =
  623. SafeMatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
  624. EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
  625. EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
  626. }
  627. // Tests that A<T>() matches any value of type T.
  628. TEST(ATest, MatchesAnyValue) {
  629. // Tests a matcher for a value type.
  630. Matcher<double> m1 = A<double>();
  631. EXPECT_TRUE(m1.Matches(91.43));
  632. EXPECT_TRUE(m1.Matches(-15.32));
  633. // Tests a matcher for a reference type.
  634. int a = 2;
  635. int b = -6;
  636. Matcher<int&> m2 = A<int&>();
  637. EXPECT_TRUE(m2.Matches(a));
  638. EXPECT_TRUE(m2.Matches(b));
  639. }
  640. TEST(ATest, WorksForDerivedClass) {
  641. Base base;
  642. Derived derived;
  643. EXPECT_THAT(&base, A<Base*>());
  644. // This shouldn't compile: EXPECT_THAT(&base, A<Derived*>());
  645. EXPECT_THAT(&derived, A<Base*>());
  646. EXPECT_THAT(&derived, A<Derived*>());
  647. }
  648. // Tests that A<T>() describes itself properly.
  649. TEST(ATest, CanDescribeSelf) {
  650. EXPECT_EQ("is anything", Describe(A<bool>()));
  651. }
  652. // Tests that An<T>() matches any value of type T.
  653. TEST(AnTest, MatchesAnyValue) {
  654. // Tests a matcher for a value type.
  655. Matcher<int> m1 = An<int>();
  656. EXPECT_TRUE(m1.Matches(9143));
  657. EXPECT_TRUE(m1.Matches(-1532));
  658. // Tests a matcher for a reference type.
  659. int a = 2;
  660. int b = -6;
  661. Matcher<int&> m2 = An<int&>();
  662. EXPECT_TRUE(m2.Matches(a));
  663. EXPECT_TRUE(m2.Matches(b));
  664. }
  665. // Tests that An<T>() describes itself properly.
  666. TEST(AnTest, CanDescribeSelf) {
  667. EXPECT_EQ("is anything", Describe(An<int>()));
  668. }
  669. // Tests that _ can be used as a matcher for any type and matches any
  670. // value of that type.
  671. TEST(UnderscoreTest, MatchesAnyValue) {
  672. // Uses _ as a matcher for a value type.
  673. Matcher<int> m1 = _;
  674. EXPECT_TRUE(m1.Matches(123));
  675. EXPECT_TRUE(m1.Matches(-242));
  676. // Uses _ as a matcher for a reference type.
  677. bool a = false;
  678. const bool b = true;
  679. Matcher<const bool&> m2 = _;
  680. EXPECT_TRUE(m2.Matches(a));
  681. EXPECT_TRUE(m2.Matches(b));
  682. }
  683. // Tests that _ describes itself properly.
  684. TEST(UnderscoreTest, CanDescribeSelf) {
  685. Matcher<int> m = _;
  686. EXPECT_EQ("is anything", Describe(m));
  687. }
  688. // Tests that Eq(x) matches any value equal to x.
  689. TEST(EqTest, MatchesEqualValue) {
  690. // 2 C-strings with same content but different addresses.
  691. const char a1[] = "hi";
  692. const char a2[] = "hi";
  693. Matcher<const char*> m1 = Eq(a1);
  694. EXPECT_TRUE(m1.Matches(a1));
  695. EXPECT_FALSE(m1.Matches(a2));
  696. }
  697. // Tests that Eq(v) describes itself properly.
  698. class Unprintable {
  699. public:
  700. Unprintable() : c_('a') {}
  701. bool operator==(const Unprintable& /* rhs */) { return true; }
  702. private:
  703. char c_;
  704. };
  705. TEST(EqTest, CanDescribeSelf) {
  706. Matcher<Unprintable> m = Eq(Unprintable());
  707. EXPECT_EQ("is equal to 1-byte object <61>", Describe(m));
  708. }
  709. // Tests that Eq(v) can be used to match any type that supports
  710. // comparing with type T, where T is v's type.
  711. TEST(EqTest, IsPolymorphic) {
  712. Matcher<int> m1 = Eq(1);
  713. EXPECT_TRUE(m1.Matches(1));
  714. EXPECT_FALSE(m1.Matches(2));
  715. Matcher<char> m2 = Eq(1);
  716. EXPECT_TRUE(m2.Matches('\1'));
  717. EXPECT_FALSE(m2.Matches('a'));
  718. }
  719. // Tests that TypedEq<T>(v) matches values of type T that's equal to v.
  720. TEST(TypedEqTest, ChecksEqualityForGivenType) {
  721. Matcher<char> m1 = TypedEq<char>('a');
  722. EXPECT_TRUE(m1.Matches('a'));
  723. EXPECT_FALSE(m1.Matches('b'));
  724. Matcher<int> m2 = TypedEq<int>(6);
  725. EXPECT_TRUE(m2.Matches(6));
  726. EXPECT_FALSE(m2.Matches(7));
  727. }
  728. // Tests that TypedEq(v) describes itself properly.
  729. TEST(TypedEqTest, CanDescribeSelf) {
  730. EXPECT_EQ("is equal to 2", Describe(TypedEq<int>(2)));
  731. }
  732. // Tests that TypedEq<T>(v) has type Matcher<T>.
  733. // Type<T>::IsTypeOf(v) compiles iff the type of value v is T, where T
  734. // is a "bare" type (i.e. not in the form of const U or U&). If v's
  735. // type is not T, the compiler will generate a message about
  736. // "undefined referece".
  737. template <typename T>
  738. struct Type {
  739. static bool IsTypeOf(const T& /* v */) { return true; }
  740. template <typename T2>
  741. static void IsTypeOf(T2 v);
  742. };
  743. TEST(TypedEqTest, HasSpecifiedType) {
  744. // Verfies that the type of TypedEq<T>(v) is Matcher<T>.
  745. Type<Matcher<int> >::IsTypeOf(TypedEq<int>(5));
  746. Type<Matcher<double> >::IsTypeOf(TypedEq<double>(5));
  747. }
  748. // Tests that Ge(v) matches anything >= v.
  749. TEST(GeTest, ImplementsGreaterThanOrEqual) {
  750. Matcher<int> m1 = Ge(0);
  751. EXPECT_TRUE(m1.Matches(1));
  752. EXPECT_TRUE(m1.Matches(0));
  753. EXPECT_FALSE(m1.Matches(-1));
  754. }
  755. // Tests that Ge(v) describes itself properly.
  756. TEST(GeTest, CanDescribeSelf) {
  757. Matcher<int> m = Ge(5);
  758. EXPECT_EQ("is >= 5", Describe(m));
  759. }
  760. // Tests that Gt(v) matches anything > v.
  761. TEST(GtTest, ImplementsGreaterThan) {
  762. Matcher<double> m1 = Gt(0);
  763. EXPECT_TRUE(m1.Matches(1.0));
  764. EXPECT_FALSE(m1.Matches(0.0));
  765. EXPECT_FALSE(m1.Matches(-1.0));
  766. }
  767. // Tests that Gt(v) describes itself properly.
  768. TEST(GtTest, CanDescribeSelf) {
  769. Matcher<int> m = Gt(5);
  770. EXPECT_EQ("is > 5", Describe(m));
  771. }
  772. // Tests that Le(v) matches anything <= v.
  773. TEST(LeTest, ImplementsLessThanOrEqual) {
  774. Matcher<char> m1 = Le('b');
  775. EXPECT_TRUE(m1.Matches('a'));
  776. EXPECT_TRUE(m1.Matches('b'));
  777. EXPECT_FALSE(m1.Matches('c'));
  778. }
  779. // Tests that Le(v) describes itself properly.
  780. TEST(LeTest, CanDescribeSelf) {
  781. Matcher<int> m = Le(5);
  782. EXPECT_EQ("is <= 5", Describe(m));
  783. }
  784. // Tests that Lt(v) matches anything < v.
  785. TEST(LtTest, ImplementsLessThan) {
  786. Matcher<const string&> m1 = Lt("Hello");
  787. EXPECT_TRUE(m1.Matches("Abc"));
  788. EXPECT_FALSE(m1.Matches("Hello"));
  789. EXPECT_FALSE(m1.Matches("Hello, world!"));
  790. }
  791. // Tests that Lt(v) describes itself properly.
  792. TEST(LtTest, CanDescribeSelf) {
  793. Matcher<int> m = Lt(5);
  794. EXPECT_EQ("is < 5", Describe(m));
  795. }
  796. // Tests that Ne(v) matches anything != v.
  797. TEST(NeTest, ImplementsNotEqual) {
  798. Matcher<int> m1 = Ne(0);
  799. EXPECT_TRUE(m1.Matches(1));
  800. EXPECT_TRUE(m1.Matches(-1));
  801. EXPECT_FALSE(m1.Matches(0));
  802. }
  803. // Tests that Ne(v) describes itself properly.
  804. TEST(NeTest, CanDescribeSelf) {
  805. Matcher<int> m = Ne(5);
  806. EXPECT_EQ("isn't equal to 5", Describe(m));
  807. }
  808. // Tests that IsNull() matches any NULL pointer of any type.
  809. TEST(IsNullTest, MatchesNullPointer) {
  810. Matcher<int*> m1 = IsNull();
  811. int* p1 = NULL;
  812. int n = 0;
  813. EXPECT_TRUE(m1.Matches(p1));
  814. EXPECT_FALSE(m1.Matches(&n));
  815. Matcher<const char*> m2 = IsNull();
  816. const char* p2 = NULL;
  817. EXPECT_TRUE(m2.Matches(p2));
  818. EXPECT_FALSE(m2.Matches("hi"));
  819. #if !GTEST_OS_SYMBIAN
  820. // Nokia's Symbian compiler generates:
  821. // gmock-matchers.h: ambiguous access to overloaded function
  822. // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(void *)'
  823. // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(const testing::
  824. // MatcherInterface<void *> *)'
  825. // gmock-matchers.h: (point of instantiation: 'testing::
  826. // gmock_matchers_test::IsNullTest_MatchesNullPointer_Test::TestBody()')
  827. // gmock-matchers.h: (instantiating: 'testing::PolymorphicMatc
  828. Matcher<void*> m3 = IsNull();
  829. void* p3 = NULL;
  830. EXPECT_TRUE(m3.Matches(p3));
  831. EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef)));
  832. #endif
  833. }
  834. TEST(IsNullTest, LinkedPtr) {
  835. const Matcher<linked_ptr<int> > m = IsNull();
  836. const linked_ptr<int> null_p;
  837. const linked_ptr<int> non_null_p(new int);
  838. EXPECT_TRUE(m.Matches(null_p));
  839. EXPECT_FALSE(m.Matches(non_null_p));
  840. }
  841. TEST(IsNullTest, ReferenceToConstLinkedPtr) {
  842. const Matcher<const linked_ptr<double>&> m = IsNull();
  843. const linked_ptr<double> null_p;
  844. const linked_ptr<double> non_null_p(new double);
  845. EXPECT_TRUE(m.Matches(null_p));
  846. EXPECT_FALSE(m.Matches(non_null_p));
  847. }
  848. TEST(IsNullTest, ReferenceToConstScopedPtr) {
  849. const Matcher<const scoped_ptr<double>&> m = IsNull();
  850. const scoped_ptr<double> null_p;
  851. const scoped_ptr<double> non_null_p(new double);
  852. EXPECT_TRUE(m.Matches(null_p));
  853. EXPECT_FALSE(m.Matches(non_null_p));
  854. }
  855. // Tests that IsNull() describes itself properly.
  856. TEST(IsNullTest, CanDescribeSelf) {
  857. Matcher<int*> m = IsNull();
  858. EXPECT_EQ("is NULL", Describe(m));
  859. EXPECT_EQ("isn't NULL", DescribeNegation(m));
  860. }
  861. // Tests that NotNull() matches any non-NULL pointer of any type.
  862. TEST(NotNullTest, MatchesNonNullPointer) {
  863. Matcher<int*> m1 = NotNull();
  864. int* p1 = NULL;
  865. int n = 0;
  866. EXPECT_FALSE(m1.Matches(p1));
  867. EXPECT_TRUE(m1.Matches(&n));
  868. Matcher<const char*> m2 = NotNull();
  869. const char* p2 = NULL;
  870. EXPECT_FALSE(m2.Matches(p2));
  871. EXPECT_TRUE(m2.Matches("hi"));
  872. }
  873. TEST(NotNullTest, LinkedPtr) {
  874. const Matcher<linked_ptr<int> > m = NotNull();
  875. const linked_ptr<int> null_p;
  876. const linked_ptr<int> non_null_p(new int);
  877. EXPECT_FALSE(m.Matches(null_p));
  878. EXPECT_TRUE(m.Matches(non_null_p));
  879. }
  880. TEST(NotNullTest, ReferenceToConstLinkedPtr) {
  881. const Matcher<const linked_ptr<double>&> m = NotNull();
  882. const linked_ptr<double> null_p;
  883. const linked_ptr<double> non_null_p(new double);
  884. EXPECT_FALSE(m.Matches(null_p));
  885. EXPECT_TRUE(m.Matches(non_null_p));
  886. }
  887. TEST(NotNullTest, ReferenceToConstScopedPtr) {
  888. const Matcher<const scoped_ptr<double>&> m = NotNull();
  889. const scoped_ptr<double> null_p;
  890. const scoped_ptr<double> non_null_p(new double);
  891. EXPECT_FALSE(m.Matches(null_p));
  892. EXPECT_TRUE(m.Matches(non_null_p));
  893. }
  894. // Tests that NotNull() describes itself properly.
  895. TEST(NotNullTest, CanDescribeSelf) {
  896. Matcher<int*> m = NotNull();
  897. EXPECT_EQ("isn't NULL", Describe(m));
  898. }
  899. // Tests that Ref(variable) matches an argument that references
  900. // 'variable'.
  901. TEST(RefTest, MatchesSameVariable) {
  902. int a = 0;
  903. int b = 0;
  904. Matcher<int&> m = Ref(a);
  905. EXPECT_TRUE(m.Matches(a));
  906. EXPECT_FALSE(m.Matches(b));
  907. }
  908. // Tests that Ref(variable) describes itself properly.
  909. TEST(RefTest, CanDescribeSelf) {
  910. int n = 5;
  911. Matcher<int&> m = Ref(n);
  912. stringstream ss;
  913. ss << "references the variable @" << &n << " 5";
  914. EXPECT_EQ(string(ss.str()), Describe(m));
  915. }
  916. // Test that Ref(non_const_varialbe) can be used as a matcher for a
  917. // const reference.
  918. TEST(RefTest, CanBeUsedAsMatcherForConstReference) {
  919. int a = 0;
  920. int b = 0;
  921. Matcher<const int&> m = Ref(a);
  922. EXPECT_TRUE(m.Matches(a));
  923. EXPECT_FALSE(m.Matches(b));
  924. }
  925. // Tests that Ref(variable) is covariant, i.e. Ref(derived) can be
  926. // used wherever Ref(base) can be used (Ref(derived) is a sub-type
  927. // of Ref(base), but not vice versa.
  928. TEST(RefTest, IsCovariant) {
  929. Base base, base2;
  930. Derived derived;
  931. Matcher<const Base&> m1 = Ref(base);
  932. EXPECT_TRUE(m1.Matches(base));
  933. EXPECT_FALSE(m1.Matches(base2));
  934. EXPECT_FALSE(m1.Matches(derived));
  935. m1 = Ref(derived);
  936. EXPECT_TRUE(m1.Matches(derived));
  937. EXPECT_FALSE(m1.Matches(base));
  938. EXPECT_FALSE(m1.Matches(base2));
  939. }
  940. TEST(RefTest, ExplainsResult) {
  941. int n = 0;
  942. EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), n),
  943. StartsWith("which is located @"));
  944. int m = 0;
  945. EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), m),
  946. StartsWith("which is located @"));
  947. }
  948. // Tests string comparison matchers.
  949. TEST(StrEqTest, MatchesEqualString) {
  950. Matcher<const char*> m = StrEq(string("Hello"));
  951. EXPECT_TRUE(m.Matches("Hello"));
  952. EXPECT_FALSE(m.Matches("hello"));
  953. EXPECT_FALSE(m.Matches(NULL));
  954. Matcher<const string&> m2 = StrEq("Hello");
  955. EXPECT_TRUE(m2.Matches("Hello"));
  956. EXPECT_FALSE(m2.Matches("Hi"));
  957. }
  958. TEST(StrEqTest, CanDescribeSelf) {
  959. Matcher<string> m = StrEq("Hi-\'\"?\\\a\b\f\n\r\t\v\xD3");
  960. EXPECT_EQ("is equal to \"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"",
  961. Describe(m));
  962. string str("01204500800");
  963. str[3] = '\0';
  964. Matcher<string> m2 = StrEq(str);
  965. EXPECT_EQ("is equal to \"012\\04500800\"", Describe(m2));
  966. str[0] = str[6] = str[7] = str[9] = str[10] = '\0';
  967. Matcher<string> m3 = StrEq(str);
  968. EXPECT_EQ("is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3));
  969. }
  970. TEST(StrNeTest, MatchesUnequalString) {
  971. Matcher<const char*> m = StrNe("Hello");
  972. EXPECT_TRUE(m.Matches(""));
  973. EXPECT_TRUE(m.Matches(NULL));
  974. EXPECT_FALSE(m.Matches("Hello"));
  975. Matcher<string> m2 = StrNe(string("Hello"));
  976. EXPECT_TRUE(m2.Matches("hello"));
  977. EXPECT_FALSE(m2.Matches("Hello"));
  978. }
  979. TEST(StrNeTest, CanDescribeSelf) {
  980. Matcher<const char*> m = StrNe("Hi");
  981. EXPECT_EQ("isn't equal to \"Hi\"", Describe(m));
  982. }
  983. TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
  984. Matcher<const char*> m = StrCaseEq(string("Hello"));
  985. EXPECT_TRUE(m.Matches("Hello"));
  986. EXPECT_TRUE(m.Matches("hello"));
  987. EXPECT_FALSE(m.Matches("Hi"));
  988. EXPECT_FALSE(m.Matches(NULL));
  989. Matcher<const string&> m2 = StrCaseEq("Hello");
  990. EXPECT_TRUE(m2.Matches("hello"));
  991. EXPECT_FALSE(m2.Matches("Hi"));
  992. }
  993. TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
  994. string str1("oabocdooeoo");
  995. string str2("OABOCDOOEOO");
  996. Matcher<const string&> m0 = StrCaseEq(str1);
  997. EXPECT_FALSE(m0.Matches(str2 + string(1, '\0')));
  998. str1[3] = str2[3] = '\0';
  999. Matcher<const string&> m1 = StrCaseEq(str1);
  1000. EXPECT_TRUE(m1.Matches(str2));
  1001. str1[0] = str1[6] = str1[7] = str1[10] = '\0';
  1002. str2[0] = str2[6] = str2[7] = str2[10] = '\0';
  1003. Matcher<const string&> m2 = StrCaseEq(str1);
  1004. str1[9] = str2[9] = '\0';
  1005. EXPECT_FALSE(m2.Matches(str2));
  1006. Matcher<const string&> m3 = StrCaseEq(str1);
  1007. EXPECT_TRUE(m3.Matches(str2));
  1008. EXPECT_FALSE(m3.Matches(str2 + "x"));
  1009. str2.append(1, '\0');
  1010. EXPECT_FALSE(m3.Matches(str2));
  1011. EXPECT_FALSE(m3.Matches(string(str2, 0, 9)));
  1012. }
  1013. TEST(StrCaseEqTest, CanDescribeSelf) {
  1014. Matcher<string> m = StrCaseEq("Hi");
  1015. EXPECT_EQ("is equal to (ignoring case) \"Hi\"", Describe(m));
  1016. }
  1017. TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
  1018. Matcher<const char*> m = StrCaseNe("Hello");
  1019. EXPECT_TRUE(m.Matches("Hi"));
  1020. EXPECT_TRUE(m.Matches(NULL));
  1021. EXPECT_FALSE(m.Matches("Hello"));
  1022. EXPECT_FALSE(m.Matches("hello"));
  1023. Matcher<string> m2 = StrCaseNe(string("Hello"));
  1024. EXPECT_TRUE(m2.Matches(""));
  1025. EXPECT_FALSE(m2.Matches("Hello"));
  1026. }
  1027. TEST(StrCaseNeTest, CanDescribeSelf) {
  1028. Matcher<const char*> m = StrCaseNe("Hi");
  1029. EXPECT_EQ("isn't equal to (ignoring case) \"Hi\"", Describe(m));
  1030. }
  1031. // Tests that HasSubstr() works for matching string-typed values.
  1032. TEST(HasSubstrTest, WorksForStringClasses) {
  1033. const Matcher<string> m1 = HasSubstr("foo");
  1034. EXPECT_TRUE(m1.Matches(string("I love food.")));
  1035. EXPECT_FALSE(m1.Matches(string("tofo")));
  1036. const Matcher<const std::string&> m2 = HasSubstr("foo");
  1037. EXPECT_TRUE(m2.Matches(std::string("I love food.")));
  1038. EXPECT_FALSE(m2.Matches(std::string("tofo")));
  1039. }
  1040. // Tests that HasSubstr() works for matching C-string-typed values.
  1041. TEST(HasSubstrTest, WorksForCStrings) {
  1042. const Matcher<char*> m1 = HasSubstr("foo");
  1043. EXPECT_TRUE(m1.Matches(const_cast<char*>("I love food.")));
  1044. EXPECT_FALSE(m1.Matches(const_cast<char*>("tofo")));
  1045. EXPECT_FALSE(m1.Matches(NULL));
  1046. const Matcher<const char*> m2 = HasSubstr("foo");
  1047. EXPECT_TRUE(m2.Matches("I love food."));
  1048. EXPECT_FALSE(m2.Matches("tofo"));
  1049. EXPECT_FALSE(m2.Matches(NULL));
  1050. }
  1051. // Tests that HasSubstr(s) describes itself properly.
  1052. TEST(HasSubstrTest, CanDescribeSelf) {
  1053. Matcher<string> m = HasSubstr("foo\n\"");
  1054. EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m));
  1055. }
  1056. TEST(KeyTest, CanDescribeSelf) {
  1057. Matcher<const pair<std::string, int>&> m = Key("foo");
  1058. EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m));
  1059. EXPECT_EQ("doesn't have a key that is equal to \"foo\"", DescribeNegation(m));
  1060. }
  1061. TEST(KeyTest, ExplainsResult) {
  1062. Matcher<pair<int, bool> > m = Key(GreaterThan(10));
  1063. EXPECT_EQ("whose first field is a value which is 5 less than 10",
  1064. Explain(m, make_pair(5, true)));
  1065. EXPECT_EQ("whose first field is a value which is 5 more than 10",
  1066. Explain(m, make_pair(15, true)));
  1067. }
  1068. TEST(KeyTest, MatchesCorrectly) {
  1069. pair<int, std::string> p(25, "foo");
  1070. EXPECT_THAT(p, Key(25));
  1071. EXPECT_THAT(p, Not(Key(42)));
  1072. EXPECT_THAT(p, Key(Ge(20)));
  1073. EXPECT_THAT(p, Not(Key(Lt(25))));
  1074. }
  1075. TEST(KeyTest, SafelyCastsInnerMatcher) {
  1076. Matcher<int> is_positive = Gt(0);
  1077. Matcher<int> is_negative = Lt(0);
  1078. pair<char, bool> p('a', true);
  1079. EXPECT_THAT(p, Key(is_positive));
  1080. EXPECT_THAT(p, Not(Key(is_negative)));
  1081. }
  1082. TEST(KeyTest, InsideContainsUsingMap) {
  1083. map<int, char> container;
  1084. container.insert(make_pair(1, 'a'));
  1085. container.insert(make_pair(2, 'b'));
  1086. container.insert(make_pair(4, 'c'));
  1087. EXPECT_THAT(container, Contains(Key(1)));
  1088. EXPECT_THAT(container, Not(Contains(Key(3))));
  1089. }
  1090. TEST(KeyTest, InsideContainsUsingMultimap) {
  1091. multimap<int, char> container;
  1092. container.insert(make_pair(1, 'a'));
  1093. container.insert(make_pair(2, 'b'));
  1094. container.insert(make_pair(4, 'c'));
  1095. EXPECT_THAT(container, Not(Contains(Key(25))));
  1096. container.insert(make_pair(25, 'd'));
  1097. EXPECT_THAT(container, Contains(Key(25)));
  1098. container.insert(make_pair(25, 'e'));
  1099. EXPECT_THAT(container, Contains(Key(25)));
  1100. EXPECT_THAT(container, Contains(Key(1)));
  1101. EXPECT_THAT(container, Not(Contains(Key(3))));
  1102. }
  1103. TEST(PairTest, Typing) {
  1104. // Test verifies the following type conversions can be compiled.
  1105. Matcher<const pair<const char*, int>&> m1 = Pair("foo", 42);
  1106. Matcher<const pair<const char*, int> > m2 = Pair("foo", 42);
  1107. Matcher<pair<const char*, int> > m3 = Pair("foo", 42);
  1108. Matcher<pair<int, const std::string> > m4 = Pair(25, "42");
  1109. Matcher<pair<const std::string, int> > m5 = Pair("25", 42);
  1110. }
  1111. TEST(PairTest, CanDescribeSelf) {
  1112. Matcher<const pair<std::string, int>&> m1 = Pair("foo", 42);
  1113. EXPECT_EQ("has a first field that is equal to \"foo\""
  1114. ", and has a second field that is equal to 42",
  1115. Describe(m1));
  1116. EXPECT_EQ("has a first field that isn't equal to \"foo\""
  1117. ", or has a second field that isn't equal to 42",
  1118. DescribeNegation(m1));
  1119. // Double and triple negation (1 or 2 times not and description of negation).
  1120. Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42));
  1121. EXPECT_EQ("has a first field that isn't equal to 13"
  1122. ", and has a second field that is equal to 42",
  1123. DescribeNegation(m2));
  1124. }
  1125. TEST(PairTest, CanExplainMatchResultTo) {
  1126. // If neither field matches, Pair() should explain about the first
  1127. // field.
  1128. const Matcher<pair<int, int> > m = Pair(GreaterThan(0), GreaterThan(0));
  1129. EXPECT_EQ("whose first field does not match, which is 1 less than 0",
  1130. Explain(m, make_pair(-1, -2)));
  1131. // If the first field matches but the second doesn't, Pair() should
  1132. // explain about the second field.
  1133. EXPECT_EQ("whose second field does not match, which is 2 less than 0",
  1134. Explain(m, make_pair(1, -2)));
  1135. // If the first field doesn't match but the second does, Pair()
  1136. // should explain about the first field.
  1137. EXPECT_EQ("whose first field does not match, which is 1 less than 0",
  1138. Explain(m, make_pair(-1, 2)));
  1139. // If both fields match, Pair() should explain about them both.
  1140. EXPECT_EQ("whose both fields match, where the first field is a value "
  1141. "which is 1 more than 0, and the second field is a value "
  1142. "which is 2 more than 0",
  1143. Explain(m, make_pair(1, 2)));
  1144. // If only the first match has an explanation, only this explanation should
  1145. // be printed.
  1146. const Matcher<pair<int, int> > explain_first = Pair(GreaterThan(0), 0);
  1147. EXPECT_EQ("whose both fields match, where the first field is a value "
  1148. "which is 1 more than 0",
  1149. Explain(explain_first, make_pair(1, 0)));
  1150. // If only the second match has an explanation, only this explanation should
  1151. // be printed.
  1152. const Matcher<pair<int, int> > explain_second = Pair(0, GreaterThan(0));
  1153. EXPECT_EQ("whose both fields match, where the second field is a value "
  1154. "which is 1 more than 0",
  1155. Explain(explain_second, make_pair(0, 1)));
  1156. }
  1157. TEST(PairTest, MatchesCorrectly) {
  1158. pair<int, std::string> p(25, "foo");
  1159. // Both fields match.
  1160. EXPECT_THAT(p, Pair(25, "foo"));
  1161. EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o")));
  1162. // 'first' doesnt' match, but 'second' matches.
  1163. EXPECT_THAT(p, Not(Pair(42, "foo")));
  1164. EXPECT_THAT(p, Not(Pair(Lt(25), "foo")));
  1165. // 'first' matches, but 'second' doesn't match.
  1166. EXPECT_THAT(p, Not(Pair(25, "bar")));
  1167. EXPECT_THAT(p, Not(Pair(25, Not("foo"))));
  1168. // Neither field matches.
  1169. EXPECT_THAT(p, Not(Pair(13, "bar")));
  1170. EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a"))));
  1171. }
  1172. TEST(PairTest, SafelyCastsInnerMatchers) {
  1173. Matcher<int> is_positive = Gt(0);
  1174. Matcher<int> is_negative = Lt(0);
  1175. pair<char, bool> p('a', true);
  1176. EXPECT_THAT(p, Pair(is_positive, _));
  1177. EXPECT_THAT(p, Not(Pair(is_negative, _)));
  1178. EXPECT_THAT(p, Pair(_, is_positive));
  1179. EXPECT_THAT(p, Not(Pair(_, is_negative)));
  1180. }
  1181. TEST(PairTest, InsideContainsUsingMap) {
  1182. map<int, char> container;
  1183. container.insert(make_pair(1, 'a'));
  1184. container.insert(make_pair(2, 'b'));
  1185. container.insert(make_pair(4, 'c'));
  1186. EXPECT_THAT(container, Contains(Pair(1, 'a')));
  1187. EXPECT_THAT(container, Contains(Pair(1, _)));
  1188. EXPECT_THAT(container, Contains(Pair(_, 'a')));
  1189. EXPECT_THAT(container, Not(Contains(Pair(3, _))));
  1190. }
  1191. // Tests StartsWith(s).
  1192. TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
  1193. const Matcher<const char*> m1 = StartsWith(string(""));
  1194. EXPECT_TRUE(m1.Matches("Hi"));
  1195. EXPECT_TRUE(m1.Matches(""));
  1196. EXPECT_FALSE(m1.Matches(NULL));
  1197. const Matcher<const string&> m2 = StartsWith("Hi");
  1198. EXPECT_TRUE(m2.Matches("Hi"));
  1199. EXPECT_TRUE(m2.Matches("Hi Hi!"));
  1200. EXPECT_TRUE(m2.Matches("High"));
  1201. EXPECT_FALSE(m2.Matches("H"));
  1202. EXPECT_FALSE(m2.Matches(" Hi"));
  1203. }
  1204. TEST(StartsWithTest, CanDescribeSelf) {
  1205. Matcher<const std::string> m = StartsWith("Hi");
  1206. EXPECT_EQ("starts with \"Hi\"", Describe(m));
  1207. }
  1208. // Tests EndsWith(s).
  1209. TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
  1210. const Matcher<const char*> m1 = EndsWith("");
  1211. EXPECT_TRUE(m1.Matches("Hi"));
  1212. EXPECT_TRUE(m1.Matches(""));
  1213. EXPECT_FALSE(m1.Matches(NULL));
  1214. const Matcher<const string&> m2 = EndsWith(string("Hi"));
  1215. EXPECT_TRUE(m2.Matches("Hi"));
  1216. EXPECT_TRUE(m2.Matches("Wow Hi Hi"));
  1217. EXPECT_TRUE(m2.Matches("Super Hi"));
  1218. EXPECT_FALSE(m2.Matches("i"));
  1219. EXPECT_FALSE(m2.Matches("Hi "));
  1220. }
  1221. TEST(EndsWithTest, CanDescribeSelf) {
  1222. Matcher<const std::string> m = EndsWith("Hi");
  1223. EXPECT_EQ("ends with \"Hi\"", Describe(m));
  1224. }
  1225. // Tests MatchesRegex().
  1226. TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
  1227. const Matcher<const char*> m1 = MatchesRegex("a.*z");
  1228. EXPECT_TRUE(m1.Matches("az"));
  1229. EXPECT_TRUE(m1.Matches("abcz"));
  1230. EXPECT_FALSE(m1.Matches(NULL));
  1231. const Matcher<const string&> m2 = MatchesRegex(new RE("a.*z"));
  1232. EXPECT_TRUE(m2.Matches("azbz"));
  1233. EXPECT_FALSE(m2.Matches("az1"));
  1234. EXPECT_FALSE(m2.Matches("1az"));
  1235. }
  1236. TEST(MatchesRegexTest, CanDescribeSelf) {
  1237. Matcher<const std::string> m1 = MatchesRegex(string("Hi.*"));
  1238. EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1));
  1239. Matcher<const char*> m2 = MatchesRegex(new RE("a.*"));
  1240. EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2));
  1241. }
  1242. // Tests ContainsRegex().
  1243. TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
  1244. const Matcher<const char*> m1 = ContainsRegex(string("a.*z"));
  1245. EXPECT_TRUE(m1.Matches("az"));
  1246. EXPECT_TRUE(m1.Matches("0abcz1"));
  1247. EXPECT_FALSE(m1.Matches(NULL));
  1248. const Matcher<const string&> m2 = ContainsRegex(new RE("a.*z"));
  1249. EXPECT_TRUE(m2.Matches("azbz"));
  1250. EXPECT_TRUE(m2.Matches("az1"));
  1251. EXPECT_FALSE(m2.Matches("1a"));
  1252. }
  1253. TEST(ContainsRegexTest, CanDescribeSelf) {
  1254. Matcher<const std::string> m1 = ContainsRegex("Hi.*");
  1255. EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1));
  1256. Matcher<const char*> m2 = ContainsRegex(new RE("a.*"));
  1257. EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2));
  1258. }
  1259. // Tests for wide strings.
  1260. #if GTEST_HAS_STD_WSTRING
  1261. TEST(StdWideStrEqTest, MatchesEqual) {
  1262. Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello"));
  1263. EXPECT_TRUE(m.Matches(L"Hello"));
  1264. EXPECT_FALSE(m.Matches(L"hello"));
  1265. EXPECT_FALSE(m.Matches(NULL));
  1266. Matcher<const ::std::wstring&> m2 = StrEq(L"Hello");
  1267. EXPECT_TRUE(m2.Matches(L"Hello"));
  1268. EXPECT_FALSE(m2.Matches(L"Hi"));
  1269. Matcher<const ::std::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
  1270. EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
  1271. EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
  1272. ::std::wstring str(L"01204500800");
  1273. str[3] = L'\0';
  1274. Matcher<const ::std::wstring&> m4 = StrEq(str);
  1275. EXPECT_TRUE(m4.Matches(str));
  1276. str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
  1277. Matcher<const ::std::wstring&> m5 = StrEq(str);
  1278. EXPECT_TRUE(m5.Matches(str));
  1279. }
  1280. TEST(StdWideStrEqTest, CanDescribeSelf) {
  1281. Matcher< ::std::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v");
  1282. EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
  1283. Describe(m));
  1284. Matcher< ::std::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
  1285. EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
  1286. Describe(m2));
  1287. ::std::wstring str(L"01204500800");
  1288. str[3] = L'\0';
  1289. Matcher<const ::std::wstring&> m4 = StrEq(str);
  1290. EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
  1291. str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
  1292. Matcher<const ::std::wstring&> m5 = StrEq(str);
  1293. EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
  1294. }
  1295. TEST(StdWideStrNeTest, MatchesUnequalString) {
  1296. Matcher<const wchar_t*> m = StrNe(L"Hello");
  1297. EXPECT_TRUE(m.Matches(L""));
  1298. EXPECT_TRUE(m.Matches(NULL));
  1299. EXPECT_FALSE(m.Matches(L"Hello"));
  1300. Matcher< ::std::wstring> m2 = StrNe(::std::wstring(L"Hello"));
  1301. EXPECT_TRUE(m2.Matches(L"hello"));
  1302. EXPECT_FALSE(m2.Matches(L"Hello"));
  1303. }
  1304. TEST(StdWideStrNeTest, CanDescribeSelf) {
  1305. Matcher<const wchar_t*> m = StrNe(L"Hi");
  1306. EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
  1307. }
  1308. TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
  1309. Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello"));
  1310. EXPECT_TRUE(m.Matches(L"Hello"));
  1311. EXPECT_TRUE(m.Matches(L"hello"));
  1312. EXPECT_FALSE(m.Matches(L"Hi"));
  1313. EXPECT_FALSE(m.Matches(NULL));
  1314. Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello");
  1315. EXPECT_TRUE(m2.Matches(L"hello"));
  1316. EXPECT_FALSE(m2.Matches(L"Hi"));
  1317. }
  1318. TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
  1319. ::std::wstring str1(L"oabocdooeoo");
  1320. ::std::wstring str2(L"OABOCDOOEOO");
  1321. Matcher<const ::std::wstring&> m0 = StrCaseEq(str1);
  1322. EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L'\0')));
  1323. str1[3] = str2[3] = L'\0';
  1324. Matcher<const ::std::wstring&> m1 = StrCaseEq(str1);
  1325. EXPECT_TRUE(m1.Matches(str2));
  1326. str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
  1327. str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
  1328. Matcher<const ::std::wstring&> m2 = StrCaseEq(str1);
  1329. str1[9] = str2[9] = L'\0';
  1330. EXPECT_FALSE(m2.Matches(str2));
  1331. Matcher<const ::std::wstring&> m3 = StrCaseEq(str1);
  1332. EXPECT_TRUE(m3.Matches(str2));
  1333. EXPECT_FALSE(m3.Matches(str2 + L"x"));
  1334. str2.append(1, L'\0');
  1335. EXPECT_FALSE(m3.Matches(str2));
  1336. EXPECT_FALSE(m3.Matches(::std::wstring(str2, 0, 9)));
  1337. }
  1338. TEST(StdWideStrCaseEqTest, CanDescribeSelf) {
  1339. Matcher< ::std::wstring> m = StrCaseEq(L"Hi");
  1340. EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
  1341. }
  1342. TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
  1343. Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
  1344. EXPECT_TRUE(m.Matches(L"Hi"));
  1345. EXPECT_TRUE(m.Matches(NULL));
  1346. EXPECT_FALSE(m.Matches(L"Hello"));
  1347. EXPECT_FALSE(m.Matches(L"hello"));
  1348. Matcher< ::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello"));
  1349. EXPECT_TRUE(m2.Matches(L""));
  1350. EXPECT_FALSE(m2.Matches(L"Hello"));
  1351. }
  1352. TEST(StdWideStrCaseNeTest, CanDescribeSelf) {
  1353. Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
  1354. EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
  1355. }
  1356. // Tests that HasSubstr() works for matching wstring-typed values.
  1357. TEST(StdWideHasSubstrTest, WorksForStringClasses) {
  1358. const Matcher< ::std::wstring> m1 = HasSubstr(L"foo");
  1359. EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food.")));
  1360. EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo")));
  1361. const