gmock-matchers-comparisons_test.cc 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427
  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. // Google Mock - a framework for writing C++ mock classes.
  30. //
  31. // This file tests some commonly used argument matchers.
  32. #include <functional>
  33. #include <memory>
  34. #include <string>
  35. #include <tuple>
  36. #include <vector>
  37. #include "gmock/gmock.h"
  38. #include "test/gmock-matchers_test.h"
  39. #include "gtest/gtest.h"
  40. // Silence warning C4244: 'initializing': conversion from 'int' to 'short',
  41. // possible loss of data and C4100, unreferenced local parameter
  42. GTEST_DISABLE_MSC_WARNINGS_PUSH_(4244 4100)
  43. namespace testing {
  44. namespace gmock_matchers_test {
  45. namespace {
  46. INSTANTIATE_GTEST_MATCHER_TEST_P(MonotonicMatcherTest);
  47. TEST_P(MonotonicMatcherTestP, IsPrintable) {
  48. stringstream ss;
  49. ss << GreaterThan(5);
  50. EXPECT_EQ("is > 5", ss.str());
  51. }
  52. TEST(MatchResultListenerTest, StreamingWorks) {
  53. StringMatchResultListener listener;
  54. listener << "hi" << 5;
  55. EXPECT_EQ("hi5", listener.str());
  56. listener.Clear();
  57. EXPECT_EQ("", listener.str());
  58. listener << 42;
  59. EXPECT_EQ("42", listener.str());
  60. // Streaming shouldn't crash when the underlying ostream is NULL.
  61. DummyMatchResultListener dummy;
  62. dummy << "hi" << 5;
  63. }
  64. TEST(MatchResultListenerTest, CanAccessUnderlyingStream) {
  65. EXPECT_TRUE(DummyMatchResultListener().stream() == nullptr);
  66. EXPECT_TRUE(StreamMatchResultListener(nullptr).stream() == nullptr);
  67. EXPECT_EQ(&std::cout, StreamMatchResultListener(&std::cout).stream());
  68. }
  69. TEST(MatchResultListenerTest, IsInterestedWorks) {
  70. EXPECT_TRUE(StringMatchResultListener().IsInterested());
  71. EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested());
  72. EXPECT_FALSE(DummyMatchResultListener().IsInterested());
  73. EXPECT_FALSE(StreamMatchResultListener(nullptr).IsInterested());
  74. }
  75. // Makes sure that the MatcherInterface<T> interface doesn't
  76. // change.
  77. class EvenMatcherImpl : public MatcherInterface<int> {
  78. public:
  79. bool MatchAndExplain(int x,
  80. MatchResultListener* /* listener */) const override {
  81. return x % 2 == 0;
  82. }
  83. void DescribeTo(ostream* os) const override { *os << "is an even number"; }
  84. // We deliberately don't define DescribeNegationTo() and
  85. // ExplainMatchResultTo() here, to make sure the definition of these
  86. // two methods is optional.
  87. };
  88. // Makes sure that the MatcherInterface API doesn't change.
  89. TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) {
  90. EvenMatcherImpl m;
  91. }
  92. // Tests implementing a monomorphic matcher using MatchAndExplain().
  93. class NewEvenMatcherImpl : public MatcherInterface<int> {
  94. public:
  95. bool MatchAndExplain(int x, MatchResultListener* listener) const override {
  96. const bool match = x % 2 == 0;
  97. // Verifies that we can stream to a listener directly.
  98. *listener << "value % " << 2;
  99. if (listener->stream() != nullptr) {
  100. // Verifies that we can stream to a listener's underlying stream
  101. // too.
  102. *listener->stream() << " == " << (x % 2);
  103. }
  104. return match;
  105. }
  106. void DescribeTo(ostream* os) const override { *os << "is an even number"; }
  107. };
  108. TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) {
  109. Matcher<int> m = MakeMatcher(new NewEvenMatcherImpl);
  110. EXPECT_TRUE(m.Matches(2));
  111. EXPECT_FALSE(m.Matches(3));
  112. EXPECT_EQ("value % 2 == 0", Explain(m, 2));
  113. EXPECT_EQ("value % 2 == 1", Explain(m, 3));
  114. }
  115. INSTANTIATE_GTEST_MATCHER_TEST_P(MatcherTest);
  116. // Tests default-constructing a matcher.
  117. TEST(MatcherTest, CanBeDefaultConstructed) { Matcher<double> m; }
  118. // Tests that Matcher<T> can be constructed from a MatcherInterface<T>*.
  119. TEST(MatcherTest, CanBeConstructedFromMatcherInterface) {
  120. const MatcherInterface<int>* impl = new EvenMatcherImpl;
  121. Matcher<int> m(impl);
  122. EXPECT_TRUE(m.Matches(4));
  123. EXPECT_FALSE(m.Matches(5));
  124. }
  125. // Tests that value can be used in place of Eq(value).
  126. TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) {
  127. Matcher<int> m1 = 5;
  128. EXPECT_TRUE(m1.Matches(5));
  129. EXPECT_FALSE(m1.Matches(6));
  130. }
  131. // Tests that NULL can be used in place of Eq(NULL).
  132. TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) {
  133. Matcher<int*> m1 = nullptr;
  134. EXPECT_TRUE(m1.Matches(nullptr));
  135. int n = 0;
  136. EXPECT_FALSE(m1.Matches(&n));
  137. }
  138. // Tests that matchers can be constructed from a variable that is not properly
  139. // defined. This should be illegal, but many users rely on this accidentally.
  140. struct Undefined {
  141. virtual ~Undefined() = 0;
  142. static const int kInt = 1;
  143. };
  144. TEST(MatcherTest, CanBeConstructedFromUndefinedVariable) {
  145. Matcher<int> m1 = Undefined::kInt;
  146. EXPECT_TRUE(m1.Matches(1));
  147. EXPECT_FALSE(m1.Matches(2));
  148. }
  149. // Test that a matcher parameterized with an abstract class compiles.
  150. TEST(MatcherTest, CanAcceptAbstractClass) { Matcher<const Undefined&> m = _; }
  151. // Tests that matchers are copyable.
  152. TEST(MatcherTest, IsCopyable) {
  153. // Tests the copy constructor.
  154. Matcher<bool> m1 = Eq(false);
  155. EXPECT_TRUE(m1.Matches(false));
  156. EXPECT_FALSE(m1.Matches(true));
  157. // Tests the assignment operator.
  158. m1 = Eq(true);
  159. EXPECT_TRUE(m1.Matches(true));
  160. EXPECT_FALSE(m1.Matches(false));
  161. }
  162. // Tests that Matcher<T>::DescribeTo() calls
  163. // MatcherInterface<T>::DescribeTo().
  164. TEST(MatcherTest, CanDescribeItself) {
  165. EXPECT_EQ("is an even number", Describe(Matcher<int>(new EvenMatcherImpl)));
  166. }
  167. // Tests Matcher<T>::MatchAndExplain().
  168. TEST_P(MatcherTestP, MatchAndExplain) {
  169. Matcher<int> m = GreaterThan(0);
  170. StringMatchResultListener listener1;
  171. EXPECT_TRUE(m.MatchAndExplain(42, &listener1));
  172. EXPECT_EQ("which is 42 more than 0", listener1.str());
  173. StringMatchResultListener listener2;
  174. EXPECT_FALSE(m.MatchAndExplain(-9, &listener2));
  175. EXPECT_EQ("which is 9 less than 0", listener2.str());
  176. }
  177. // Tests that a C-string literal can be implicitly converted to a
  178. // Matcher<std::string> or Matcher<const std::string&>.
  179. TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
  180. Matcher<std::string> m1 = "hi";
  181. EXPECT_TRUE(m1.Matches("hi"));
  182. EXPECT_FALSE(m1.Matches("hello"));
  183. Matcher<const std::string&> m2 = "hi";
  184. EXPECT_TRUE(m2.Matches("hi"));
  185. EXPECT_FALSE(m2.Matches("hello"));
  186. }
  187. // Tests that a string object can be implicitly converted to a
  188. // Matcher<std::string> or Matcher<const std::string&>.
  189. TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {
  190. Matcher<std::string> m1 = std::string("hi");
  191. EXPECT_TRUE(m1.Matches("hi"));
  192. EXPECT_FALSE(m1.Matches("hello"));
  193. Matcher<const std::string&> m2 = std::string("hi");
  194. EXPECT_TRUE(m2.Matches("hi"));
  195. EXPECT_FALSE(m2.Matches("hello"));
  196. }
  197. #if GTEST_INTERNAL_HAS_STRING_VIEW
  198. // Tests that a C-string literal can be implicitly converted to a
  199. // Matcher<StringView> or Matcher<const StringView&>.
  200. TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
  201. Matcher<internal::StringView> m1 = "cats";
  202. EXPECT_TRUE(m1.Matches("cats"));
  203. EXPECT_FALSE(m1.Matches("dogs"));
  204. Matcher<const internal::StringView&> m2 = "cats";
  205. EXPECT_TRUE(m2.Matches("cats"));
  206. EXPECT_FALSE(m2.Matches("dogs"));
  207. }
  208. // Tests that a std::string object can be implicitly converted to a
  209. // Matcher<StringView> or Matcher<const StringView&>.
  210. TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromString) {
  211. Matcher<internal::StringView> m1 = std::string("cats");
  212. EXPECT_TRUE(m1.Matches("cats"));
  213. EXPECT_FALSE(m1.Matches("dogs"));
  214. Matcher<const internal::StringView&> m2 = std::string("cats");
  215. EXPECT_TRUE(m2.Matches("cats"));
  216. EXPECT_FALSE(m2.Matches("dogs"));
  217. }
  218. // Tests that a StringView object can be implicitly converted to a
  219. // Matcher<StringView> or Matcher<const StringView&>.
  220. TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromStringView) {
  221. Matcher<internal::StringView> m1 = internal::StringView("cats");
  222. EXPECT_TRUE(m1.Matches("cats"));
  223. EXPECT_FALSE(m1.Matches("dogs"));
  224. Matcher<const internal::StringView&> m2 = internal::StringView("cats");
  225. EXPECT_TRUE(m2.Matches("cats"));
  226. EXPECT_FALSE(m2.Matches("dogs"));
  227. }
  228. #endif // GTEST_INTERNAL_HAS_STRING_VIEW
  229. // Tests that a std::reference_wrapper<std::string> object can be implicitly
  230. // converted to a Matcher<std::string> or Matcher<const std::string&> via Eq().
  231. TEST(StringMatcherTest,
  232. CanBeImplicitlyConstructedFromEqReferenceWrapperString) {
  233. std::string value = "cats";
  234. Matcher<std::string> m1 = Eq(std::ref(value));
  235. EXPECT_TRUE(m1.Matches("cats"));
  236. EXPECT_FALSE(m1.Matches("dogs"));
  237. Matcher<const std::string&> m2 = Eq(std::ref(value));
  238. EXPECT_TRUE(m2.Matches("cats"));
  239. EXPECT_FALSE(m2.Matches("dogs"));
  240. }
  241. // Tests that MakeMatcher() constructs a Matcher<T> from a
  242. // MatcherInterface* without requiring the user to explicitly
  243. // write the type.
  244. TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) {
  245. const MatcherInterface<int>* dummy_impl = new EvenMatcherImpl;
  246. Matcher<int> m = MakeMatcher(dummy_impl);
  247. }
  248. // Tests that MakePolymorphicMatcher() can construct a polymorphic
  249. // matcher from its implementation using the old API.
  250. const int g_bar = 1;
  251. class ReferencesBarOrIsZeroImpl {
  252. public:
  253. template <typename T>
  254. bool MatchAndExplain(const T& x, MatchResultListener* /* listener */) const {
  255. const void* p = &x;
  256. return p == &g_bar || x == 0;
  257. }
  258. void DescribeTo(ostream* os) const { *os << "g_bar or zero"; }
  259. void DescribeNegationTo(ostream* os) const {
  260. *os << "doesn't reference g_bar and is not zero";
  261. }
  262. };
  263. // This function verifies that MakePolymorphicMatcher() returns a
  264. // PolymorphicMatcher<T> where T is the argument's type.
  265. PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() {
  266. return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl());
  267. }
  268. TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) {
  269. // Using a polymorphic matcher to match a reference type.
  270. Matcher<const int&> m1 = ReferencesBarOrIsZero();
  271. EXPECT_TRUE(m1.Matches(0));
  272. // Verifies that the identity of a by-reference argument is preserved.
  273. EXPECT_TRUE(m1.Matches(g_bar));
  274. EXPECT_FALSE(m1.Matches(1));
  275. EXPECT_EQ("g_bar or zero", Describe(m1));
  276. // Using a polymorphic matcher to match a value type.
  277. Matcher<double> m2 = ReferencesBarOrIsZero();
  278. EXPECT_TRUE(m2.Matches(0.0));
  279. EXPECT_FALSE(m2.Matches(0.1));
  280. EXPECT_EQ("g_bar or zero", Describe(m2));
  281. }
  282. // Tests implementing a polymorphic matcher using MatchAndExplain().
  283. class PolymorphicIsEvenImpl {
  284. public:
  285. void DescribeTo(ostream* os) const { *os << "is even"; }
  286. void DescribeNegationTo(ostream* os) const { *os << "is odd"; }
  287. template <typename T>
  288. bool MatchAndExplain(const T& x, MatchResultListener* listener) const {
  289. // Verifies that we can stream to the listener directly.
  290. *listener << "% " << 2;
  291. if (listener->stream() != nullptr) {
  292. // Verifies that we can stream to the listener's underlying stream
  293. // too.
  294. *listener->stream() << " == " << (x % 2);
  295. }
  296. return (x % 2) == 0;
  297. }
  298. };
  299. PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() {
  300. return MakePolymorphicMatcher(PolymorphicIsEvenImpl());
  301. }
  302. TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) {
  303. // Using PolymorphicIsEven() as a Matcher<int>.
  304. const Matcher<int> m1 = PolymorphicIsEven();
  305. EXPECT_TRUE(m1.Matches(42));
  306. EXPECT_FALSE(m1.Matches(43));
  307. EXPECT_EQ("is even", Describe(m1));
  308. const Matcher<int> not_m1 = Not(m1);
  309. EXPECT_EQ("is odd", Describe(not_m1));
  310. EXPECT_EQ("% 2 == 0", Explain(m1, 42));
  311. // Using PolymorphicIsEven() as a Matcher<char>.
  312. const Matcher<char> m2 = PolymorphicIsEven();
  313. EXPECT_TRUE(m2.Matches('\x42'));
  314. EXPECT_FALSE(m2.Matches('\x43'));
  315. EXPECT_EQ("is even", Describe(m2));
  316. const Matcher<char> not_m2 = Not(m2);
  317. EXPECT_EQ("is odd", Describe(not_m2));
  318. EXPECT_EQ("% 2 == 0", Explain(m2, '\x42'));
  319. }
  320. INSTANTIATE_GTEST_MATCHER_TEST_P(MatcherCastTest);
  321. // Tests that MatcherCast<T>(m) works when m is a polymorphic matcher.
  322. TEST_P(MatcherCastTestP, FromPolymorphicMatcher) {
  323. Matcher<int16_t> m;
  324. if (use_gtest_matcher_) {
  325. m = MatcherCast<int16_t>(GtestGreaterThan(int64_t{5}));
  326. } else {
  327. m = MatcherCast<int16_t>(Gt(int64_t{5}));
  328. }
  329. EXPECT_TRUE(m.Matches(6));
  330. EXPECT_FALSE(m.Matches(4));
  331. }
  332. // For testing casting matchers between compatible types.
  333. class IntValue {
  334. public:
  335. // An int can be statically (although not implicitly) cast to a
  336. // IntValue.
  337. explicit IntValue(int a_value) : value_(a_value) {}
  338. int value() const { return value_; }
  339. private:
  340. int value_;
  341. };
  342. // For testing casting matchers between compatible types. This is similar to
  343. // IntValue, but takes a non-const reference to the value, showing MatcherCast
  344. // works with such types (and doesn't, for example, use a const ref internally).
  345. class MutableIntView {
  346. public:
  347. // An int& can be statically (although not implicitly) cast to a
  348. // MutableIntView.
  349. explicit MutableIntView(int& a_value) : value_(a_value) {}
  350. int& value() const { return value_; }
  351. private:
  352. int& value_;
  353. };
  354. // For testing casting matchers between compatible types.
  355. bool IsPositiveIntValue(const IntValue& foo) { return foo.value() > 0; }
  356. // For testing casting matchers between compatible types.
  357. bool IsPositiveMutableIntView(MutableIntView foo) { return foo.value() > 0; }
  358. // Tests that MatcherCast<T>(m) works when m is a Matcher<U> where T
  359. // can be statically converted to U.
  360. TEST(MatcherCastTest, FromCompatibleType) {
  361. Matcher<double> m1 = Eq(2.0);
  362. Matcher<int> m2 = MatcherCast<int>(m1);
  363. EXPECT_TRUE(m2.Matches(2));
  364. EXPECT_FALSE(m2.Matches(3));
  365. Matcher<IntValue> m3 = Truly(IsPositiveIntValue);
  366. Matcher<int> m4 = MatcherCast<int>(m3);
  367. // In the following, the arguments 1 and 0 are statically converted
  368. // to IntValue objects, and then tested by the IsPositiveIntValue()
  369. // predicate.
  370. EXPECT_TRUE(m4.Matches(1));
  371. EXPECT_FALSE(m4.Matches(0));
  372. Matcher<MutableIntView> m5 = Truly(IsPositiveMutableIntView);
  373. Matcher<int> m6 = MatcherCast<int>(m5);
  374. // In the following, the arguments 1 and 0 are statically converted to
  375. // MutableIntView objects, and then tested by the IsPositiveMutableIntView()
  376. // predicate.
  377. EXPECT_TRUE(m6.Matches(1));
  378. EXPECT_FALSE(m6.Matches(0));
  379. }
  380. // Tests that MatcherCast<T>(m) works when m is a Matcher<const T&>.
  381. TEST(MatcherCastTest, FromConstReferenceToNonReference) {
  382. int n = 0;
  383. Matcher<const int&> m1 = Ref(n);
  384. Matcher<int> m2 = MatcherCast<int>(m1);
  385. int n1 = 0;
  386. EXPECT_TRUE(m2.Matches(n));
  387. EXPECT_FALSE(m2.Matches(n1));
  388. }
  389. // Tests that MatcherCast<T&>(m) works when m is a Matcher<const T&>.
  390. TEST(MatcherCastTest, FromConstReferenceToReference) {
  391. int n = 0;
  392. Matcher<const int&> m1 = Ref(n);
  393. Matcher<int&> m2 = MatcherCast<int&>(m1);
  394. int n1 = 0;
  395. EXPECT_TRUE(m2.Matches(n));
  396. EXPECT_FALSE(m2.Matches(n1));
  397. }
  398. // Tests that MatcherCast<T>(m) works when m is a Matcher<T&>.
  399. TEST(MatcherCastTest, FromReferenceToNonReference) {
  400. Matcher<int&> m1 = Eq(0);
  401. Matcher<int> m2 = MatcherCast<int>(m1);
  402. EXPECT_TRUE(m2.Matches(0));
  403. EXPECT_FALSE(m2.Matches(1));
  404. // Of course, reference identity isn't preserved since a copy is required.
  405. int n = 0;
  406. Matcher<int&> m3 = Ref(n);
  407. Matcher<int> m4 = MatcherCast<int>(m3);
  408. EXPECT_FALSE(m4.Matches(n));
  409. }
  410. // Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
  411. TEST(MatcherCastTest, FromNonReferenceToConstReference) {
  412. Matcher<int> m1 = Eq(0);
  413. Matcher<const int&> m2 = MatcherCast<const int&>(m1);
  414. EXPECT_TRUE(m2.Matches(0));
  415. EXPECT_FALSE(m2.Matches(1));
  416. }
  417. // Tests that MatcherCast<T&>(m) works when m is a Matcher<T>.
  418. TEST(MatcherCastTest, FromNonReferenceToReference) {
  419. Matcher<int> m1 = Eq(0);
  420. Matcher<int&> m2 = MatcherCast<int&>(m1);
  421. int n = 0;
  422. EXPECT_TRUE(m2.Matches(n));
  423. n = 1;
  424. EXPECT_FALSE(m2.Matches(n));
  425. }
  426. // Tests that MatcherCast<T>(m) works when m is a Matcher<T>.
  427. TEST(MatcherCastTest, FromSameType) {
  428. Matcher<int> m1 = Eq(0);
  429. Matcher<int> m2 = MatcherCast<int>(m1);
  430. EXPECT_TRUE(m2.Matches(0));
  431. EXPECT_FALSE(m2.Matches(1));
  432. }
  433. // Tests that MatcherCast<T>(m) works when m is a value of the same type as the
  434. // value type of the Matcher.
  435. TEST(MatcherCastTest, FromAValue) {
  436. Matcher<int> m = MatcherCast<int>(42);
  437. EXPECT_TRUE(m.Matches(42));
  438. EXPECT_FALSE(m.Matches(239));
  439. }
  440. // Tests that MatcherCast<T>(m) works when m is a value of the type implicitly
  441. // convertible to the value type of the Matcher.
  442. TEST(MatcherCastTest, FromAnImplicitlyConvertibleValue) {
  443. const int kExpected = 'c';
  444. Matcher<int> m = MatcherCast<int>('c');
  445. EXPECT_TRUE(m.Matches(kExpected));
  446. EXPECT_FALSE(m.Matches(kExpected + 1));
  447. }
  448. struct NonImplicitlyConstructibleTypeWithOperatorEq {
  449. friend bool operator==(
  450. const NonImplicitlyConstructibleTypeWithOperatorEq& /* ignored */,
  451. int rhs) {
  452. return 42 == rhs;
  453. }
  454. friend bool operator==(
  455. int lhs,
  456. const NonImplicitlyConstructibleTypeWithOperatorEq& /* ignored */) {
  457. return lhs == 42;
  458. }
  459. };
  460. // Tests that MatcherCast<T>(m) works when m is a neither a matcher nor
  461. // implicitly convertible to the value type of the Matcher, but the value type
  462. // of the matcher has operator==() overload accepting m.
  463. TEST(MatcherCastTest, NonImplicitlyConstructibleTypeWithOperatorEq) {
  464. Matcher<NonImplicitlyConstructibleTypeWithOperatorEq> m1 =
  465. MatcherCast<NonImplicitlyConstructibleTypeWithOperatorEq>(42);
  466. EXPECT_TRUE(m1.Matches(NonImplicitlyConstructibleTypeWithOperatorEq()));
  467. Matcher<NonImplicitlyConstructibleTypeWithOperatorEq> m2 =
  468. MatcherCast<NonImplicitlyConstructibleTypeWithOperatorEq>(239);
  469. EXPECT_FALSE(m2.Matches(NonImplicitlyConstructibleTypeWithOperatorEq()));
  470. // When updating the following lines please also change the comment to
  471. // namespace convertible_from_any.
  472. Matcher<int> m3 =
  473. MatcherCast<int>(NonImplicitlyConstructibleTypeWithOperatorEq());
  474. EXPECT_TRUE(m3.Matches(42));
  475. EXPECT_FALSE(m3.Matches(239));
  476. }
  477. // ConvertibleFromAny does not work with MSVC. resulting in
  478. // error C2440: 'initializing': cannot convert from 'Eq' to 'M'
  479. // No constructor could take the source type, or constructor overload
  480. // resolution was ambiguous
  481. #if !defined _MSC_VER
  482. // The below ConvertibleFromAny struct is implicitly constructible from anything
  483. // and when in the same namespace can interact with other tests. In particular,
  484. // if it is in the same namespace as other tests and one removes
  485. // NonImplicitlyConstructibleTypeWithOperatorEq::operator==(int lhs, ...);
  486. // then the corresponding test still compiles (and it should not!) by implicitly
  487. // converting NonImplicitlyConstructibleTypeWithOperatorEq to ConvertibleFromAny
  488. // in m3.Matcher().
  489. namespace convertible_from_any {
  490. // Implicitly convertible from any type.
  491. struct ConvertibleFromAny {
  492. ConvertibleFromAny(int a_value) : value(a_value) {}
  493. template <typename T>
  494. ConvertibleFromAny(const T& /*a_value*/) : value(-1) {
  495. ADD_FAILURE() << "Conversion constructor called";
  496. }
  497. int value;
  498. };
  499. bool operator==(const ConvertibleFromAny& a, const ConvertibleFromAny& b) {
  500. return a.value == b.value;
  501. }
  502. ostream& operator<<(ostream& os, const ConvertibleFromAny& a) {
  503. return os << a.value;
  504. }
  505. TEST(MatcherCastTest, ConversionConstructorIsUsed) {
  506. Matcher<ConvertibleFromAny> m = MatcherCast<ConvertibleFromAny>(1);
  507. EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
  508. EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
  509. }
  510. TEST(MatcherCastTest, FromConvertibleFromAny) {
  511. Matcher<ConvertibleFromAny> m =
  512. MatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
  513. EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
  514. EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
  515. }
  516. } // namespace convertible_from_any
  517. #endif // !defined _MSC_VER
  518. struct IntReferenceWrapper {
  519. IntReferenceWrapper(const int& a_value) : value(&a_value) {}
  520. const int* value;
  521. };
  522. bool operator==(const IntReferenceWrapper& a, const IntReferenceWrapper& b) {
  523. return a.value == b.value;
  524. }
  525. TEST(MatcherCastTest, ValueIsNotCopied) {
  526. int n = 42;
  527. Matcher<IntReferenceWrapper> m = MatcherCast<IntReferenceWrapper>(n);
  528. // Verify that the matcher holds a reference to n, not to its temporary copy.
  529. EXPECT_TRUE(m.Matches(n));
  530. }
  531. class Base {
  532. public:
  533. virtual ~Base() = default;
  534. Base() = default;
  535. private:
  536. Base(const Base&) = delete;
  537. Base& operator=(const Base&) = delete;
  538. };
  539. class Derived : public Base {
  540. public:
  541. Derived() : Base() {}
  542. int i;
  543. };
  544. class OtherDerived : public Base {};
  545. INSTANTIATE_GTEST_MATCHER_TEST_P(SafeMatcherCastTest);
  546. // Tests that SafeMatcherCast<T>(m) works when m is a polymorphic matcher.
  547. TEST_P(SafeMatcherCastTestP, FromPolymorphicMatcher) {
  548. Matcher<char> m2;
  549. if (use_gtest_matcher_) {
  550. m2 = SafeMatcherCast<char>(GtestGreaterThan(32));
  551. } else {
  552. m2 = SafeMatcherCast<char>(Gt(32));
  553. }
  554. EXPECT_TRUE(m2.Matches('A'));
  555. EXPECT_FALSE(m2.Matches('\n'));
  556. }
  557. // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where
  558. // T and U are arithmetic types and T can be losslessly converted to
  559. // U.
  560. TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) {
  561. Matcher<double> m1 = DoubleEq(1.0);
  562. Matcher<float> m2 = SafeMatcherCast<float>(m1);
  563. EXPECT_TRUE(m2.Matches(1.0f));
  564. EXPECT_FALSE(m2.Matches(2.0f));
  565. Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>('a'));
  566. EXPECT_TRUE(m3.Matches('a'));
  567. EXPECT_FALSE(m3.Matches('b'));
  568. }
  569. // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where T and U
  570. // are pointers or references to a derived and a base class, correspondingly.
  571. TEST(SafeMatcherCastTest, FromBaseClass) {
  572. Derived d, d2;
  573. Matcher<Base*> m1 = Eq(&d);
  574. Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1);
  575. EXPECT_TRUE(m2.Matches(&d));
  576. EXPECT_FALSE(m2.Matches(&d2));
  577. Matcher<Base&> m3 = Ref(d);
  578. Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3);
  579. EXPECT_TRUE(m4.Matches(d));
  580. EXPECT_FALSE(m4.Matches(d2));
  581. }
  582. // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<const T&>.
  583. TEST(SafeMatcherCastTest, FromConstReferenceToNonReference) {
  584. int n = 0;
  585. Matcher<const int&> m1 = Ref(n);
  586. Matcher<int> m2 = SafeMatcherCast<int>(m1);
  587. int n1 = 0;
  588. EXPECT_TRUE(m2.Matches(n));
  589. EXPECT_FALSE(m2.Matches(n1));
  590. }
  591. // Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<const T&>.
  592. TEST(SafeMatcherCastTest, FromConstReferenceToReference) {
  593. int n = 0;
  594. Matcher<const int&> m1 = Ref(n);
  595. Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
  596. int n1 = 0;
  597. EXPECT_TRUE(m2.Matches(n));
  598. EXPECT_FALSE(m2.Matches(n1));
  599. }
  600. // Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
  601. TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) {
  602. Matcher<std::unique_ptr<int>> m1 = IsNull();
  603. Matcher<const std::unique_ptr<int>&> m2 =
  604. SafeMatcherCast<const std::unique_ptr<int>&>(m1);
  605. EXPECT_TRUE(m2.Matches(std::unique_ptr<int>()));
  606. EXPECT_FALSE(m2.Matches(std::unique_ptr<int>(new int)));
  607. }
  608. // Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<T>.
  609. TEST(SafeMatcherCastTest, FromNonReferenceToReference) {
  610. Matcher<int> m1 = Eq(0);
  611. Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
  612. int n = 0;
  613. EXPECT_TRUE(m2.Matches(n));
  614. n = 1;
  615. EXPECT_FALSE(m2.Matches(n));
  616. }
  617. // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<T>.
  618. TEST(SafeMatcherCastTest, FromSameType) {
  619. Matcher<int> m1 = Eq(0);
  620. Matcher<int> m2 = SafeMatcherCast<int>(m1);
  621. EXPECT_TRUE(m2.Matches(0));
  622. EXPECT_FALSE(m2.Matches(1));
  623. }
  624. #if !defined _MSC_VER
  625. namespace convertible_from_any {
  626. TEST(SafeMatcherCastTest, ConversionConstructorIsUsed) {
  627. Matcher<ConvertibleFromAny> m = SafeMatcherCast<ConvertibleFromAny>(1);
  628. EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
  629. EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
  630. }
  631. TEST(SafeMatcherCastTest, FromConvertibleFromAny) {
  632. Matcher<ConvertibleFromAny> m =
  633. SafeMatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
  634. EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
  635. EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
  636. }
  637. } // namespace convertible_from_any
  638. #endif // !defined _MSC_VER
  639. TEST(SafeMatcherCastTest, ValueIsNotCopied) {
  640. int n = 42;
  641. Matcher<IntReferenceWrapper> m = SafeMatcherCast<IntReferenceWrapper>(n);
  642. // Verify that the matcher holds a reference to n, not to its temporary copy.
  643. EXPECT_TRUE(m.Matches(n));
  644. }
  645. TEST(ExpectThat, TakesLiterals) {
  646. EXPECT_THAT(1, 1);
  647. EXPECT_THAT(1.0, 1.0);
  648. EXPECT_THAT(std::string(), "");
  649. }
  650. TEST(ExpectThat, TakesFunctions) {
  651. struct Helper {
  652. static void Func() {}
  653. };
  654. void (*func)() = Helper::Func;
  655. EXPECT_THAT(func, Helper::Func);
  656. EXPECT_THAT(func, &Helper::Func);
  657. }
  658. // Tests that A<T>() matches any value of type T.
  659. TEST(ATest, MatchesAnyValue) {
  660. // Tests a matcher for a value type.
  661. Matcher<double> m1 = A<double>();
  662. EXPECT_TRUE(m1.Matches(91.43));
  663. EXPECT_TRUE(m1.Matches(-15.32));
  664. // Tests a matcher for a reference type.
  665. int a = 2;
  666. int b = -6;
  667. Matcher<int&> m2 = A<int&>();
  668. EXPECT_TRUE(m2.Matches(a));
  669. EXPECT_TRUE(m2.Matches(b));
  670. }
  671. TEST(ATest, WorksForDerivedClass) {
  672. Base base;
  673. Derived derived;
  674. EXPECT_THAT(&base, A<Base*>());
  675. // This shouldn't compile: EXPECT_THAT(&base, A<Derived*>());
  676. EXPECT_THAT(&derived, A<Base*>());
  677. EXPECT_THAT(&derived, A<Derived*>());
  678. }
  679. // Tests that A<T>() describes itself properly.
  680. TEST(ATest, CanDescribeSelf) { EXPECT_EQ("is anything", Describe(A<bool>())); }
  681. // Tests that An<T>() matches any value of type T.
  682. TEST(AnTest, MatchesAnyValue) {
  683. // Tests a matcher for a value type.
  684. Matcher<int> m1 = An<int>();
  685. EXPECT_TRUE(m1.Matches(9143));
  686. EXPECT_TRUE(m1.Matches(-1532));
  687. // Tests a matcher for a reference type.
  688. int a = 2;
  689. int b = -6;
  690. Matcher<int&> m2 = An<int&>();
  691. EXPECT_TRUE(m2.Matches(a));
  692. EXPECT_TRUE(m2.Matches(b));
  693. }
  694. // Tests that An<T>() describes itself properly.
  695. TEST(AnTest, CanDescribeSelf) { EXPECT_EQ("is anything", Describe(An<int>())); }
  696. // Tests that _ can be used as a matcher for any type and matches any
  697. // value of that type.
  698. TEST(UnderscoreTest, MatchesAnyValue) {
  699. // Uses _ as a matcher for a value type.
  700. Matcher<int> m1 = _;
  701. EXPECT_TRUE(m1.Matches(123));
  702. EXPECT_TRUE(m1.Matches(-242));
  703. // Uses _ as a matcher for a reference type.
  704. bool a = false;
  705. const bool b = true;
  706. Matcher<const bool&> m2 = _;
  707. EXPECT_TRUE(m2.Matches(a));
  708. EXPECT_TRUE(m2.Matches(b));
  709. }
  710. // Tests that _ describes itself properly.
  711. TEST(UnderscoreTest, CanDescribeSelf) {
  712. Matcher<int> m = _;
  713. EXPECT_EQ("is anything", Describe(m));
  714. }
  715. // Tests that Eq(x) matches any value equal to x.
  716. TEST(EqTest, MatchesEqualValue) {
  717. // 2 C-strings with same content but different addresses.
  718. const char a1[] = "hi";
  719. const char a2[] = "hi";
  720. Matcher<const char*> m1 = Eq(a1);
  721. EXPECT_TRUE(m1.Matches(a1));
  722. EXPECT_FALSE(m1.Matches(a2));
  723. }
  724. // Tests that Eq(v) describes itself properly.
  725. class Unprintable {
  726. public:
  727. Unprintable() : c_('a') {}
  728. bool operator==(const Unprintable& /* rhs */) const { return true; }
  729. // -Wunused-private-field: dummy accessor for `c_`.
  730. char dummy_c() { return c_; }
  731. private:
  732. char c_;
  733. };
  734. TEST(EqTest, CanDescribeSelf) {
  735. Matcher<Unprintable> m = Eq(Unprintable());
  736. EXPECT_EQ("is equal to 1-byte object <61>", Describe(m));
  737. }
  738. // Tests that Eq(v) can be used to match any type that supports
  739. // comparing with type T, where T is v's type.
  740. TEST(EqTest, IsPolymorphic) {
  741. Matcher<int> m1 = Eq(1);
  742. EXPECT_TRUE(m1.Matches(1));
  743. EXPECT_FALSE(m1.Matches(2));
  744. Matcher<char> m2 = Eq(1);
  745. EXPECT_TRUE(m2.Matches('\1'));
  746. EXPECT_FALSE(m2.Matches('a'));
  747. }
  748. // Tests that TypedEq<T>(v) matches values of type T that's equal to v.
  749. TEST(TypedEqTest, ChecksEqualityForGivenType) {
  750. Matcher<char> m1 = TypedEq<char>('a');
  751. EXPECT_TRUE(m1.Matches('a'));
  752. EXPECT_FALSE(m1.Matches('b'));
  753. Matcher<int> m2 = TypedEq<int>(6);
  754. EXPECT_TRUE(m2.Matches(6));
  755. EXPECT_FALSE(m2.Matches(7));
  756. }
  757. // Tests that TypedEq(v) describes itself properly.
  758. TEST(TypedEqTest, CanDescribeSelf) {
  759. EXPECT_EQ("is equal to 2", Describe(TypedEq<int>(2)));
  760. }
  761. // Tests that TypedEq<T>(v) has type Matcher<T>.
  762. // Type<T>::IsTypeOf(v) compiles if and only if the type of value v is T, where
  763. // T is a "bare" type (i.e. not in the form of const U or U&). If v's type is
  764. // not T, the compiler will generate a message about "undefined reference".
  765. template <typename T>
  766. struct Type {
  767. static bool IsTypeOf(const T& /* v */) { return true; }
  768. template <typename T2>
  769. static void IsTypeOf(T2 v);
  770. };
  771. TEST(TypedEqTest, HasSpecifiedType) {
  772. // Verifies that the type of TypedEq<T>(v) is Matcher<T>.
  773. Type<Matcher<int>>::IsTypeOf(TypedEq<int>(5));
  774. Type<Matcher<double>>::IsTypeOf(TypedEq<double>(5));
  775. }
  776. // Tests that Ge(v) matches anything >= v.
  777. TEST(GeTest, ImplementsGreaterThanOrEqual) {
  778. Matcher<int> m1 = Ge(0);
  779. EXPECT_TRUE(m1.Matches(1));
  780. EXPECT_TRUE(m1.Matches(0));
  781. EXPECT_FALSE(m1.Matches(-1));
  782. }
  783. // Tests that Ge(v) describes itself properly.
  784. TEST(GeTest, CanDescribeSelf) {
  785. Matcher<int> m = Ge(5);
  786. EXPECT_EQ("is >= 5", Describe(m));
  787. }
  788. // Tests that Gt(v) matches anything > v.
  789. TEST(GtTest, ImplementsGreaterThan) {
  790. Matcher<double> m1 = Gt(0);
  791. EXPECT_TRUE(m1.Matches(1.0));
  792. EXPECT_FALSE(m1.Matches(0.0));
  793. EXPECT_FALSE(m1.Matches(-1.0));
  794. }
  795. // Tests that Gt(v) describes itself properly.
  796. TEST(GtTest, CanDescribeSelf) {
  797. Matcher<int> m = Gt(5);
  798. EXPECT_EQ("is > 5", Describe(m));
  799. }
  800. // Tests that Le(v) matches anything <= v.
  801. TEST(LeTest, ImplementsLessThanOrEqual) {
  802. Matcher<char> m1 = Le('b');
  803. EXPECT_TRUE(m1.Matches('a'));
  804. EXPECT_TRUE(m1.Matches('b'));
  805. EXPECT_FALSE(m1.Matches('c'));
  806. }
  807. // Tests that Le(v) describes itself properly.
  808. TEST(LeTest, CanDescribeSelf) {
  809. Matcher<int> m = Le(5);
  810. EXPECT_EQ("is <= 5", Describe(m));
  811. }
  812. // Tests that Lt(v) matches anything < v.
  813. TEST(LtTest, ImplementsLessThan) {
  814. Matcher<const std::string&> m1 = Lt("Hello");
  815. EXPECT_TRUE(m1.Matches("Abc"));
  816. EXPECT_FALSE(m1.Matches("Hello"));
  817. EXPECT_FALSE(m1.Matches("Hello, world!"));
  818. }
  819. // Tests that Lt(v) describes itself properly.
  820. TEST(LtTest, CanDescribeSelf) {
  821. Matcher<int> m = Lt(5);
  822. EXPECT_EQ("is < 5", Describe(m));
  823. }
  824. // Tests that Ne(v) matches anything != v.
  825. TEST(NeTest, ImplementsNotEqual) {
  826. Matcher<int> m1 = Ne(0);
  827. EXPECT_TRUE(m1.Matches(1));
  828. EXPECT_TRUE(m1.Matches(-1));
  829. EXPECT_FALSE(m1.Matches(0));
  830. }
  831. // Tests that Ne(v) describes itself properly.
  832. TEST(NeTest, CanDescribeSelf) {
  833. Matcher<int> m = Ne(5);
  834. EXPECT_EQ("isn't equal to 5", Describe(m));
  835. }
  836. class MoveOnly {
  837. public:
  838. explicit MoveOnly(int i) : i_(i) {}
  839. MoveOnly(const MoveOnly&) = delete;
  840. MoveOnly(MoveOnly&&) = default;
  841. MoveOnly& operator=(const MoveOnly&) = delete;
  842. MoveOnly& operator=(MoveOnly&&) = default;
  843. bool operator==(const MoveOnly& other) const { return i_ == other.i_; }
  844. bool operator!=(const MoveOnly& other) const { return i_ != other.i_; }
  845. bool operator<(const MoveOnly& other) const { return i_ < other.i_; }
  846. bool operator<=(const MoveOnly& other) const { return i_ <= other.i_; }
  847. bool operator>(const MoveOnly& other) const { return i_ > other.i_; }
  848. bool operator>=(const MoveOnly& other) const { return i_ >= other.i_; }
  849. private:
  850. int i_;
  851. };
  852. struct MoveHelper {
  853. MOCK_METHOD1(Call, void(MoveOnly));
  854. };
  855. // Disable this test in VS 2015 (version 14), where it fails when SEH is enabled
  856. #if defined(_MSC_VER) && (_MSC_VER < 1910)
  857. TEST(ComparisonBaseTest, DISABLED_WorksWithMoveOnly) {
  858. #else
  859. TEST(ComparisonBaseTest, WorksWithMoveOnly) {
  860. #endif
  861. MoveOnly m{0};
  862. MoveHelper helper;
  863. EXPECT_CALL(helper, Call(Eq(ByRef(m))));
  864. helper.Call(MoveOnly(0));
  865. EXPECT_CALL(helper, Call(Ne(ByRef(m))));
  866. helper.Call(MoveOnly(1));
  867. EXPECT_CALL(helper, Call(Le(ByRef(m))));
  868. helper.Call(MoveOnly(0));
  869. EXPECT_CALL(helper, Call(Lt(ByRef(m))));
  870. helper.Call(MoveOnly(-1));
  871. EXPECT_CALL(helper, Call(Ge(ByRef(m))));
  872. helper.Call(MoveOnly(0));
  873. EXPECT_CALL(helper, Call(Gt(ByRef(m))));
  874. helper.Call(MoveOnly(1));
  875. }
  876. TEST(IsEmptyTest, MatchesContainer) {
  877. const Matcher<std::vector<int>> m = IsEmpty();
  878. std::vector<int> a = {};
  879. std::vector<int> b = {1};
  880. EXPECT_TRUE(m.Matches(a));
  881. EXPECT_FALSE(m.Matches(b));
  882. }
  883. TEST(IsEmptyTest, MatchesStdString) {
  884. const Matcher<std::string> m = IsEmpty();
  885. std::string a = "z";
  886. std::string b = "";
  887. EXPECT_FALSE(m.Matches(a));
  888. EXPECT_TRUE(m.Matches(b));
  889. }
  890. TEST(IsEmptyTest, MatchesCString) {
  891. const Matcher<const char*> m = IsEmpty();
  892. const char a[] = "";
  893. const char b[] = "x";
  894. EXPECT_TRUE(m.Matches(a));
  895. EXPECT_FALSE(m.Matches(b));
  896. }
  897. // Tests that IsNull() matches any NULL pointer of any type.
  898. TEST(IsNullTest, MatchesNullPointer) {
  899. Matcher<int*> m1 = IsNull();
  900. int* p1 = nullptr;
  901. int n = 0;
  902. EXPECT_TRUE(m1.Matches(p1));
  903. EXPECT_FALSE(m1.Matches(&n));
  904. Matcher<const char*> m2 = IsNull();
  905. const char* p2 = nullptr;
  906. EXPECT_TRUE(m2.Matches(p2));
  907. EXPECT_FALSE(m2.Matches("hi"));
  908. Matcher<void*> m3 = IsNull();
  909. void* p3 = nullptr;
  910. EXPECT_TRUE(m3.Matches(p3));
  911. EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef)));
  912. }
  913. TEST(IsNullTest, StdFunction) {
  914. const Matcher<std::function<void()>> m = IsNull();
  915. EXPECT_TRUE(m.Matches(std::function<void()>()));
  916. EXPECT_FALSE(m.Matches([] {}));
  917. }
  918. // Tests that IsNull() describes itself properly.
  919. TEST(IsNullTest, CanDescribeSelf) {
  920. Matcher<int*> m = IsNull();
  921. EXPECT_EQ("is NULL", Describe(m));
  922. EXPECT_EQ("isn't NULL", DescribeNegation(m));
  923. }
  924. // Tests that NotNull() matches any non-NULL pointer of any type.
  925. TEST(NotNullTest, MatchesNonNullPointer) {
  926. Matcher<int*> m1 = NotNull();
  927. int* p1 = nullptr;
  928. int n = 0;
  929. EXPECT_FALSE(m1.Matches(p1));
  930. EXPECT_TRUE(m1.Matches(&n));
  931. Matcher<const char*> m2 = NotNull();
  932. const char* p2 = nullptr;
  933. EXPECT_FALSE(m2.Matches(p2));
  934. EXPECT_TRUE(m2.Matches("hi"));
  935. }
  936. TEST(NotNullTest, LinkedPtr) {
  937. const Matcher<std::shared_ptr<int>> m = NotNull();
  938. const std::shared_ptr<int> null_p;
  939. const std::shared_ptr<int> non_null_p(new int);
  940. EXPECT_FALSE(m.Matches(null_p));
  941. EXPECT_TRUE(m.Matches(non_null_p));
  942. }
  943. TEST(NotNullTest, ReferenceToConstLinkedPtr) {
  944. const Matcher<const std::shared_ptr<double>&> m = NotNull();
  945. const std::shared_ptr<double> null_p;
  946. const std::shared_ptr<double> non_null_p(new double);
  947. EXPECT_FALSE(m.Matches(null_p));
  948. EXPECT_TRUE(m.Matches(non_null_p));
  949. }
  950. TEST(NotNullTest, StdFunction) {
  951. const Matcher<std::function<void()>> m = NotNull();
  952. EXPECT_TRUE(m.Matches([] {}));
  953. EXPECT_FALSE(m.Matches(std::function<void()>()));
  954. }
  955. // Tests that NotNull() describes itself properly.
  956. TEST(NotNullTest, CanDescribeSelf) {
  957. Matcher<int*> m = NotNull();
  958. EXPECT_EQ("isn't NULL", Describe(m));
  959. }
  960. // Tests that Ref(variable) matches an argument that references
  961. // 'variable'.
  962. TEST(RefTest, MatchesSameVariable) {
  963. int a = 0;
  964. int b = 0;
  965. Matcher<int&> m = Ref(a);
  966. EXPECT_TRUE(m.Matches(a));
  967. EXPECT_FALSE(m.Matches(b));
  968. }
  969. // Tests that Ref(variable) describes itself properly.
  970. TEST(RefTest, CanDescribeSelf) {
  971. int n = 5;
  972. Matcher<int&> m = Ref(n);
  973. stringstream ss;
  974. ss << "references the variable @" << &n << " 5";
  975. EXPECT_EQ(ss.str(), Describe(m));
  976. }
  977. // Test that Ref(non_const_varialbe) can be used as a matcher for a
  978. // const reference.
  979. TEST(RefTest, CanBeUsedAsMatcherForConstReference) {
  980. int a = 0;
  981. int b = 0;
  982. Matcher<const int&> m = Ref(a);
  983. EXPECT_TRUE(m.Matches(a));
  984. EXPECT_FALSE(m.Matches(b));
  985. }
  986. // Tests that Ref(variable) is covariant, i.e. Ref(derived) can be
  987. // used wherever Ref(base) can be used (Ref(derived) is a sub-type
  988. // of Ref(base), but not vice versa.
  989. TEST(RefTest, IsCovariant) {
  990. Base base, base2;
  991. Derived derived;
  992. Matcher<const Base&> m1 = Ref(base);
  993. EXPECT_TRUE(m1.Matches(base));
  994. EXPECT_FALSE(m1.Matches(base2));
  995. EXPECT_FALSE(m1.Matches(derived));
  996. m1 = Ref(derived);
  997. EXPECT_TRUE(m1.Matches(derived));
  998. EXPECT_FALSE(m1.Matches(base));
  999. EXPECT_FALSE(m1.Matches(base2));
  1000. }
  1001. TEST(RefTest, ExplainsResult) {
  1002. int n = 0;
  1003. EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), n),
  1004. StartsWith("which is located @"));
  1005. int m = 0;
  1006. EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), m),
  1007. StartsWith("which is located @"));
  1008. }
  1009. // Tests string comparison matchers.
  1010. template <typename T = std::string>
  1011. std::string FromStringLike(internal::StringLike<T> str) {
  1012. return std::string(str);
  1013. }
  1014. TEST(StringLike, TestConversions) {
  1015. EXPECT_EQ("foo", FromStringLike("foo"));
  1016. EXPECT_EQ("foo", FromStringLike(std::string("foo")));
  1017. #if GTEST_INTERNAL_HAS_STRING_VIEW
  1018. EXPECT_EQ("foo", FromStringLike(internal::StringView("foo")));
  1019. #endif // GTEST_INTERNAL_HAS_STRING_VIEW
  1020. // Non deducible types.
  1021. EXPECT_EQ("", FromStringLike({}));
  1022. EXPECT_EQ("foo", FromStringLike({'f', 'o', 'o'}));
  1023. const char buf[] = "foo";
  1024. EXPECT_EQ("foo", FromStringLike({buf, buf + 3}));
  1025. }
  1026. TEST(StrEqTest, MatchesEqualString) {
  1027. Matcher<const char*> m = StrEq(std::string("Hello"));
  1028. EXPECT_TRUE(m.Matches("Hello"));
  1029. EXPECT_FALSE(m.Matches("hello"));
  1030. EXPECT_FALSE(m.Matches(nullptr));
  1031. Matcher<const std::string&> m2 = StrEq("Hello");
  1032. EXPECT_TRUE(m2.Matches("Hello"));
  1033. EXPECT_FALSE(m2.Matches("Hi"));
  1034. #if GTEST_INTERNAL_HAS_STRING_VIEW
  1035. Matcher<const internal::StringView&> m3 =
  1036. StrEq(internal::StringView("Hello"));
  1037. EXPECT_TRUE(m3.Matches(internal::StringView("Hello")));
  1038. EXPECT_FALSE(m3.Matches(internal::StringView("hello")));
  1039. EXPECT_FALSE(m3.Matches(internal::StringView()));
  1040. Matcher<const internal::StringView&> m_empty = StrEq("");
  1041. EXPECT_TRUE(m_empty.Matches(internal::StringView("")));
  1042. EXPECT_TRUE(m_empty.Matches(internal::StringView()));
  1043. EXPECT_FALSE(m_empty.Matches(internal::StringView("hello")));
  1044. #endif // GTEST_INTERNAL_HAS_STRING_VIEW
  1045. }
  1046. TEST(StrEqTest, CanDescribeSelf) {
  1047. Matcher<std::string> m = StrEq("Hi-\'\"?\\\a\b\f\n\r\t\v\xD3");
  1048. EXPECT_EQ("is equal to \"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"",
  1049. Describe(m));
  1050. std::string str("01204500800");
  1051. str[3] = '\0';
  1052. Matcher<std::string> m2 = StrEq(str);
  1053. EXPECT_EQ("is equal to \"012\\04500800\"", Describe(m2));
  1054. str[0] = str[6] = str[7] = str[9] = str[10] = '\0';
  1055. Matcher<std::string> m3 = StrEq(str);
  1056. EXPECT_EQ("is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3));
  1057. }
  1058. TEST(StrNeTest, MatchesUnequalString) {
  1059. Matcher<const char*> m = StrNe("Hello");
  1060. EXPECT_TRUE(m.Matches(""));
  1061. EXPECT_TRUE(m.Matches(nullptr));
  1062. EXPECT_FALSE(m.Matches("Hello"));
  1063. Matcher<std::string> m2 = StrNe(std::string("Hello"));
  1064. EXPECT_TRUE(m2.Matches("hello"));
  1065. EXPECT_FALSE(m2.Matches("Hello"));
  1066. #if GTEST_INTERNAL_HAS_STRING_VIEW
  1067. Matcher<const internal::StringView> m3 = StrNe(internal::StringView("Hello"));
  1068. EXPECT_TRUE(m3.Matches(internal::StringView("")));
  1069. EXPECT_TRUE(m3.Matches(internal::StringView()));
  1070. EXPECT_FALSE(m3.Matches(internal::StringView("Hello")));
  1071. #endif // GTEST_INTERNAL_HAS_STRING_VIEW
  1072. }
  1073. TEST(StrNeTest, CanDescribeSelf) {
  1074. Matcher<const char*> m = StrNe("Hi");
  1075. EXPECT_EQ("isn't equal to \"Hi\"", Describe(m));
  1076. }
  1077. TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
  1078. Matcher<const char*> m = StrCaseEq(std::string("Hello"));
  1079. EXPECT_TRUE(m.Matches("Hello"));
  1080. EXPECT_TRUE(m.Matches("hello"));
  1081. EXPECT_FALSE(m.Matches("Hi"));
  1082. EXPECT_FALSE(m.Matches(nullptr));
  1083. Matcher<const std::string&> m2 = StrCaseEq("Hello");
  1084. EXPECT_TRUE(m2.Matches("hello"));
  1085. EXPECT_FALSE(m2.Matches("Hi"));
  1086. #if GTEST_INTERNAL_HAS_STRING_VIEW
  1087. Matcher<const internal::StringView&> m3 =
  1088. StrCaseEq(internal::StringView("Hello"));
  1089. EXPECT_TRUE(m3.Matches(internal::StringView("Hello")));
  1090. EXPECT_TRUE(m3.Matches(internal::StringView("hello")));
  1091. EXPECT_FALSE(m3.Matches(internal::StringView("Hi")));
  1092. EXPECT_FALSE(m3.Matches(internal::StringView()));
  1093. #endif // GTEST_INTERNAL_HAS_STRING_VIEW
  1094. }
  1095. TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
  1096. std::string str1("oabocdooeoo");
  1097. std::string str2("OABOCDOOEOO");
  1098. Matcher<const std::string&> m0 = StrCaseEq(str1);
  1099. EXPECT_FALSE(m0.Matches(str2 + std::string(1, '\0')));
  1100. str1[3] = str2[3] = '\0';
  1101. Matcher<const std::string&> m1 = StrCaseEq(str1);
  1102. EXPECT_TRUE(m1.Matches(str2));
  1103. str1[0] = str1[6] = str1[7] = str1[10] = '\0';
  1104. str2[0] = str2[6] = str2[7] = str2[10] = '\0';
  1105. Matcher<const std::string&> m2 = StrCaseEq(str1);
  1106. str1[9] = str2[9] = '\0';
  1107. EXPECT_FALSE(m2.Matches(str2));
  1108. Matcher<const std::string&> m3 = StrCaseEq(str1);
  1109. EXPECT_TRUE(m3.Matches(str2));
  1110. EXPECT_FALSE(m3.Matches(str2 + "x"));
  1111. str2.append(1, '\0');
  1112. EXPECT_FALSE(m3.Matches(str2));
  1113. EXPECT_FALSE(m3.Matches(std::string(str2, 0, 9)));
  1114. }
  1115. TEST(StrCaseEqTest, CanDescribeSelf) {
  1116. Matcher<std::string> m = StrCaseEq("Hi");
  1117. EXPECT_EQ("is equal to (ignoring case) \"Hi\"", Describe(m));
  1118. }
  1119. TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
  1120. Matcher<const char*> m = StrCaseNe("Hello");
  1121. EXPECT_TRUE(m.Matches("Hi"));
  1122. EXPECT_TRUE(m.Matches(nullptr));
  1123. EXPECT_FALSE(m.Matches("Hello"));
  1124. EXPECT_FALSE(m.Matches("hello"));
  1125. Matcher<std::string> m2 = StrCaseNe(std::string("Hello"));
  1126. EXPECT_TRUE(m2.Matches(""));
  1127. EXPECT_FALSE(m2.Matches("Hello"));
  1128. #if GTEST_INTERNAL_HAS_STRING_VIEW
  1129. Matcher<const internal::StringView> m3 =
  1130. StrCaseNe(internal::StringView("Hello"));
  1131. EXPECT_TRUE(m3.Matches(internal::StringView("Hi")));
  1132. EXPECT_TRUE(m3.Matches(internal::StringView()));
  1133. EXPECT_FALSE(m3.Matches(internal::StringView("Hello")));
  1134. EXPECT_FALSE(m3.Matches(internal::StringView("hello")));
  1135. #endif // GTEST_INTERNAL_HAS_STRING_VIEW
  1136. }
  1137. TEST(StrCaseNeTest, CanDescribeSelf) {
  1138. Matcher<const char*> m = StrCaseNe("Hi");
  1139. EXPECT_EQ("isn't equal to (ignoring case) \"Hi\"", Describe(m));
  1140. }
  1141. // Tests that HasSubstr() works for matching string-typed values.
  1142. TEST(HasSubstrTest, WorksForStringClasses) {
  1143. const Matcher<std::string> m1 = HasSubstr("foo");
  1144. EXPECT_TRUE(m1.Matches(std::string("I love food.")));
  1145. EXPECT_FALSE(m1.Matches(std::string("tofo")));
  1146. const Matcher<const std::string&> m2 = HasSubstr("foo");
  1147. EXPECT_TRUE(m2.Matches(std::string("I love food.")));
  1148. EXPECT_FALSE(m2.Matches(std::string("tofo")));
  1149. const Matcher<std::string> m_empty = HasSubstr("");
  1150. EXPECT_TRUE(m_empty.Matches(std::string()));
  1151. EXPECT_TRUE(m_empty.Matches(std::string("not empty")));
  1152. }
  1153. // Tests that HasSubstr() works for matching C-string-typed values.
  1154. TEST(HasSubstrTest, WorksForCStrings) {
  1155. const Matcher<char*> m1 = HasSubstr("foo");
  1156. EXPECT_TRUE(m1.Matches(const_cast<char*>("I love food.")));
  1157. EXPECT_FALSE(m1.Matches(const_cast<char*>("tofo")));
  1158. EXPECT_FALSE(m1.Matches(nullptr));
  1159. const Matcher<const char*> m2 = HasSubstr("foo");
  1160. EXPECT_TRUE(m2.Matches("I love food."));
  1161. EXPECT_FALSE(m2.Matches("tofo"));
  1162. EXPECT_FALSE(m2.Matches(nullptr));
  1163. const Matcher<const char*> m_empty = HasSubstr("");
  1164. EXPECT_TRUE(m_empty.Matches("not empty"));
  1165. EXPECT_TRUE(m_empty.Matches(""));
  1166. EXPECT_FALSE(m_empty.Matches(nullptr));
  1167. }
  1168. #if GTEST_INTERNAL_HAS_STRING_VIEW
  1169. // Tests that HasSubstr() works for matching StringView-typed values.
  1170. TEST(HasSubstrTest, WorksForStringViewClasses) {
  1171. const Matcher<internal::StringView> m1 =
  1172. HasSubstr(internal::StringView("foo"));
  1173. EXPECT_TRUE(m1.Matches(internal::StringView("I love food.")));
  1174. EXPECT_FALSE(m1.Matches(internal::StringView("tofo")));
  1175. EXPECT_FALSE(m1.Matches(internal::StringView()));
  1176. const Matcher<const internal::StringView&> m2 = HasSubstr("foo");
  1177. EXPECT_TRUE(m2.Matches(internal::StringView("I love food.")));
  1178. EXPECT_FALSE(m2.Matches(internal::StringView("tofo")));
  1179. EXPECT_FALSE(m2.Matches(internal::StringView()));
  1180. const Matcher<const internal::StringView&> m3 = HasSubstr("");
  1181. EXPECT_TRUE(m3.Matches(internal::StringView("foo")));
  1182. EXPECT_TRUE(m3.Matches(internal::StringView("")));
  1183. EXPECT_TRUE(m3.Matches(internal::StringView()));
  1184. }
  1185. #endif // GTEST_INTERNAL_HAS_STRING_VIEW
  1186. // Tests that HasSubstr(s) describes itself properly.
  1187. TEST(HasSubstrTest, CanDescribeSelf) {
  1188. Matcher<std::string> m = HasSubstr("foo\n\"");
  1189. EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m));
  1190. }
  1191. INSTANTIATE_GTEST_MATCHER_TEST_P(KeyTest);
  1192. TEST(KeyTest, CanDescribeSelf) {
  1193. Matcher<const pair<std::string, int>&> m = Key("foo");
  1194. EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m));
  1195. EXPECT_EQ("doesn't have a key that is equal to \"foo\"", DescribeNegation(m));
  1196. }
  1197. TEST_P(KeyTestP, ExplainsResult) {
  1198. Matcher<pair<int, bool>> m = Key(GreaterThan(10));
  1199. EXPECT_EQ("whose first field is a value which is 5 less than 10",
  1200. Explain(m, make_pair(5, true)));
  1201. EXPECT_EQ("whose first field is a value which is 5 more than 10",
  1202. Explain(m, make_pair(15, true)));
  1203. }
  1204. TEST(KeyTest, MatchesCorrectly) {
  1205. pair<int, std::string> p(25, "foo");
  1206. EXPECT_THAT(p, Key(25));
  1207. EXPECT_THAT(p, Not(Key(42)));
  1208. EXPECT_THAT(p, Key(Ge(20)));
  1209. EXPECT_THAT(p, Not(Key(Lt(25))));
  1210. }
  1211. TEST(KeyTest, WorksWithMoveOnly) {
  1212. pair<std::unique_ptr<int>, std::unique_ptr<int>> p;
  1213. EXPECT_THAT(p, Key(Eq(nullptr)));
  1214. }
  1215. INSTANTIATE_GTEST_MATCHER_TEST_P(PairTest);
  1216. template <size_t I>
  1217. struct Tag {};
  1218. struct PairWithGet {
  1219. int member_1;
  1220. std::string member_2;
  1221. using first_type = int;
  1222. using second_type = std::string;
  1223. const int& GetImpl(Tag<0>) const { return member_1; }
  1224. const std::string& GetImpl(Tag<1>) const { return member_2; }
  1225. };
  1226. template <size_t I>
  1227. auto get(const PairWithGet& value) -> decltype(value.GetImpl(Tag<I>())) {
  1228. return value.GetImpl(Tag<I>());
  1229. }
  1230. TEST(PairTest, MatchesPairWithGetCorrectly) {
  1231. PairWithGet p{25, "foo"};
  1232. EXPECT_THAT(p, Key(25));
  1233. EXPECT_THAT(p, Not(Key(42)));
  1234. EXPECT_THAT(p, Key(Ge(20)));
  1235. EXPECT_THAT(p, Not(Key(Lt(25))));
  1236. std::vector<PairWithGet> v = {{11, "Foo"}, {29, "gMockIsBestMock"}};
  1237. EXPECT_THAT(v, Contains(Key(29)));
  1238. }
  1239. TEST(KeyTest, SafelyCastsInnerMatcher) {
  1240. Matcher<int> is_positive = Gt(0);
  1241. Matcher<int> is_negative = Lt(0);
  1242. pair<char, bool> p('a', true);
  1243. EXPECT_THAT(p, Key(is_positive));
  1244. EXPECT_THAT(p, Not(Key(is_negative)));
  1245. }
  1246. TEST(KeyTest, InsideContainsUsingMap) {
  1247. map<int, char> container;
  1248. container.insert(make_pair(1, 'a'));
  1249. container.insert(make_pair(2, 'b'));
  1250. container.insert(make_pair(4, 'c'));
  1251. EXPECT_THAT(container, Contains(Key(1)));
  1252. EXPECT_THAT(container, Not(Contains(Key(3))));
  1253. }
  1254. TEST(KeyTest, InsideContainsUsingMultimap) {
  1255. multimap<int, char> container;
  1256. container.insert(make_pair(1, 'a'));
  1257. container.insert(make_pair(2, 'b'));
  1258. container.insert(make_pair(4, 'c'));
  1259. EXPECT_THAT(container, Not(Contains(Key(25))));
  1260. container.insert(make_pair(25, 'd'));
  1261. EXPECT_THAT(container, Contains(Key(25)));
  1262. container.insert(make_pair(25, 'e'));
  1263. EXPECT_THAT(container, Contains(Key(25)));
  1264. EXPECT_THAT(container, Contains(Key(1)));
  1265. EXPECT_THAT(container, Not(Contains(Key(3))));
  1266. }
  1267. TEST(PairTest, Typing) {
  1268. // Test verifies the following type conversions can be compiled.
  1269. Matcher<const pair<const char*, int>&> m1 = Pair("foo", 42);
  1270. Matcher<const pair<const char*, int>> m2 = Pair("foo", 42);
  1271. Matcher<pair<const char*, int>> m3 = Pair("foo", 42);
  1272. Matcher<pair<int, const std::string>> m4 = Pair(25, "42");
  1273. Matcher<pair<const std::string, int>> m5 = Pair("25", 42);
  1274. }
  1275. TEST(PairTest, CanDescribeSelf) {
  1276. Matcher<const pair<std::string, int>&> m1 = Pair("foo", 42);
  1277. EXPECT_EQ(
  1278. "has a first field that is equal to \"foo\""
  1279. ", and has a second field that is equal to 42",
  1280. Describe(m1));
  1281. EXPECT_EQ(
  1282. "has a first field that isn't equal to \"foo\""
  1283. ", or has a second field that isn't equal to 42",
  1284. DescribeNegation(m1));
  1285. // Double and triple negation (1 or 2 times not and description of negation).
  1286. Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42));
  1287. EXPECT_EQ(
  1288. "has a first field that isn't equal to 13"
  1289. ", and has a second field that is equal to 42",
  1290. DescribeNegation(m2));
  1291. }
  1292. TEST_P(PairTestP, CanExplainMatchResultTo) {
  1293. // If neither field matches, Pair() should explain about the first
  1294. // field.
  1295. const Matcher<pair<int, int>> m = Pair(GreaterThan(0), GreaterThan(0));
  1296. EXPECT_EQ("whose first field does not match, which is 1 less than 0",
  1297. Explain(m, make_pair(-1, -2)));
  1298. // If the first field matches but the second doesn't, Pair() should
  1299. // explain about the second field.
  1300. EXPECT_EQ("whose second field does not match, which is 2 less than 0",
  1301. Explain(m, make_pair(1, -2)));
  1302. // If the first field doesn't match but the second does, Pair()
  1303. // should explain about the first field.
  1304. EXPECT_EQ("whose first field does not match, which is 1 less than 0",
  1305. Explain(m, make_pair(-1, 2)));
  1306. // If both fields match, Pair() should explain about them both.
  1307. EXPECT_EQ(
  1308. "whose both fields match, where the first field is a value "
  1309. "which is 1 more than 0, and the second field is a value "
  1310. "which is 2 more than 0",
  1311. Explain(m, make_pair(1, 2)));
  1312. // If only the first match has an explanation, only this explanation should
  1313. // be printed.
  1314. const Matcher<pair<int, int>> explain_first = Pair(GreaterThan(0), 0);
  1315. EXPECT_EQ(
  1316. "whose both fields match, where the first field is a value "
  1317. "which is 1 more than 0",
  1318. Explain(explain_first, make_pair(1, 0)));
  1319. // If only the second match has an explanation, only this explanation should
  1320. // be printed.
  1321. const Matcher<pair<int, int>> explain_second = Pair(0, GreaterThan(0));
  1322. EXPECT_EQ(
  1323. "whose both fields match, where the second field is a value "
  1324. "which is 1 more than 0",
  1325. Explain(explain_second, make_pair(0, 1)));
  1326. }
  1327. TEST(PairTest, MatchesCorrectly) {
  1328. pair<int, std::string> p(25, "foo");
  1329. // Both fields match.
  1330. EXPECT_THAT(p, Pair(25, "foo"));
  1331. EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o")));
  1332. // 'first' doesn't match, but 'second' matches.
  1333. EXPECT_THAT(p, Not(Pair(42, "foo")));
  1334. EXPECT_THAT(p, Not(Pair(Lt(25), "foo")));
  1335. // 'first' matches, but 'second' doesn't match.
  1336. EXPECT_THAT(p, Not(Pair(25, "bar")));
  1337. EXPECT_THAT(p, Not(Pair(25, Not("foo"))));
  1338. // Neither field matches.
  1339. EXPECT_THAT(p, Not(Pair(13, "bar")));
  1340. EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a"))));
  1341. }
  1342. TEST(PairTest, WorksWithMoveOnly) {
  1343. pair<std::unique_ptr<int>, std::unique_ptr<int>> p;
  1344. p.second = std::make_unique<int>(7);
  1345. EXPECT_THAT(p, Pair(Eq(nullptr), Ne(nullptr)));
  1346. }
  1347. TEST(PairTest, SafelyCastsInnerMatchers) {
  1348. Matcher<int> is_positive = Gt(0);
  1349. Matcher<int> is_negative = Lt(0);
  1350. pair<char, bool> p('a', true);
  1351. EXPECT_THAT(p, Pair(is_positive, _));
  1352. EXPECT_THAT(p, Not(Pair(is_negative, _)));
  1353. EXPECT_THAT(p, Pair(_, is_positive));
  1354. EXPECT_THAT(p, Not(Pair(_, is_negative)));
  1355. }
  1356. TEST(PairTest, InsideContainsUsingMap) {
  1357. map<int, char> container;
  1358. container.insert(make_pair(1, 'a'));
  1359. container.insert(make_pair(2, 'b'));
  1360. container.insert(make_pair(4, 'c'));
  1361. EXPECT_THAT(container, Contains(Pair(1, 'a')));
  1362. EXPECT_THAT(container, Contains(Pair(1, _)));
  1363. EXPECT_THAT(container, Contains(Pair(_, 'a')));
  1364. EXPECT_THAT(container, Not(Contains(Pair(3, _))));
  1365. }
  1366. INSTANTIATE_GTEST_MATCHER_TEST_P(FieldsAreTest);
  1367. TEST(FieldsAreTest, MatchesCorrectly) {
  1368. std::tuple<int, std::string, double> p(25, "foo", .5);
  1369. // All fields match.
  1370. EXPECT_THAT(p, FieldsAre(25, "foo", .5));
  1371. EXPECT_THAT(p, FieldsAre(Ge(20), HasSubstr("o"), DoubleEq(.5)));
  1372. // Some don't match.
  1373. EXPECT_THAT(p, Not(FieldsAre(26, "foo", .5)));
  1374. EXPECT_THAT(p, Not(FieldsAre(25, "fo", .5)));
  1375. EXPECT_THAT(p, Not(FieldsAre(25, "foo", .6)));
  1376. }
  1377. TEST(FieldsAreTest, CanDescribeSelf) {
  1378. Matcher<const pair<std::string, int>&> m1 = FieldsAre("foo", 42);
  1379. EXPECT_EQ(
  1380. "has field #0 that is equal to \"foo\""
  1381. ", and has field #1 that is equal to 42",
  1382. Describe(m1));
  1383. EXPECT_EQ(
  1384. "has field #0 that isn't equal to \"foo\""
  1385. ", or has field #1 that isn't equal to 42",
  1386. DescribeNegation(m1));
  1387. }
  1388. TEST_P(FieldsAreTestP, CanExplainMatchResultTo) {
  1389. // The first one that fails is the one that gives the error.
  1390. Matcher<std::tuple<int, int, int>> m =
  1391. FieldsAre(GreaterThan(0), GreaterThan(0), GreaterThan(0));
  1392. EXPECT_EQ("whose field #0 does not match, which is 1 less than 0",
  1393. Explain(m, std::make_tuple(-1, -2, -3)));
  1394. EXPECT_EQ("whose field #1 does not match, which is 2 less than 0",
  1395. Explain(m, std::make_tuple(1, -2, -3)));
  1396. EXPECT_EQ("whose field #2 does not match, which is 3 less than 0",
  1397. Explain(m, std::make_tuple(1, 2, -3)));
  1398. // If they all match, we get a long explanation of success.
  1399. EXPECT_EQ(
  1400. "whose all elements match, "
  1401. "where field #0 is a value which is 1 more than 0"
  1402. ", and field #1 is a value which is 2 more than 0"
  1403. ", and field #2 is a value which is 3 more than 0",
  1404. Explain(m, std::make_tuple(1, 2, 3)));
  1405. // Only print those that have an explanation.
  1406. m = FieldsAre(GreaterThan(0), 0, GreaterThan(0));
  1407. EXPECT_EQ(
  1408. "whose all elements match, "
  1409. "where field #0 is a value which is 1 more than 0"
  1410. ", and field #2 is a value which is 3 more than 0",
  1411. Explain(m, std::make_tuple(1, 0, 3)));
  1412. // If only one has an explanation, then print that one.
  1413. m = FieldsAre(0, GreaterThan(0), 0);
  1414. EXPECT_EQ(
  1415. "whose all elements match, "
  1416. "where field #1 is a value which is 1 more than 0",
  1417. Explain(m, std::make_tuple(0, 1, 0)));
  1418. }
  1419. #if defined(__cpp_structured_bindings) && __cpp_structured_bindings >= 201606
  1420. TEST(FieldsAreTest, StructuredBindings) {
  1421. // testing::FieldsAre can also match aggregates and such with C++17 and up.
  1422. struct MyType {
  1423. int i;
  1424. std::string str;
  1425. };
  1426. EXPECT_THAT((MyType{17, "foo"}), FieldsAre(Eq(17), HasSubstr("oo")));
  1427. // Test all the supported arities.
  1428. struct MyVarType1 {
  1429. int a;
  1430. };
  1431. EXPECT_THAT(MyVarType1{}, FieldsAre(0));
  1432. struct MyVarType2 {
  1433. int a, b;
  1434. };
  1435. EXPECT_THAT(MyVarType2{}, FieldsAre(0, 0));
  1436. struct MyVarType3 {
  1437. int a, b, c;
  1438. };
  1439. EXPECT_THAT(MyVarType3{}, FieldsAre(0, 0, 0));
  1440. struct MyVarType4 {
  1441. int a, b, c, d;
  1442. };
  1443. EXPECT_THAT(MyVarType4{}, FieldsAre(0, 0, 0, 0));
  1444. struct MyVarType5 {
  1445. int a, b, c, d, e;
  1446. };
  1447. EXPECT_THAT(MyVarType5{}, FieldsAre(0, 0, 0, 0, 0));
  1448. struct MyVarType6 {
  1449. int a, b, c, d, e, f;
  1450. };
  1451. EXPECT_THAT(MyVarType6{}, FieldsAre(0, 0, 0, 0, 0, 0));
  1452. struct MyVarType7 {
  1453. int a, b, c, d, e, f, g;
  1454. };
  1455. EXPECT_THAT(MyVarType7{}, FieldsAre(0, 0, 0, 0, 0, 0, 0));
  1456. struct MyVarType8 {
  1457. int a, b, c, d, e, f, g, h;
  1458. };
  1459. EXPECT_THAT(MyVarType8{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0));
  1460. struct MyVarType9 {
  1461. int a, b, c, d, e, f, g, h, i;
  1462. };
  1463. EXPECT_THAT(MyVarType9{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0));
  1464. struct MyVarType10 {
  1465. int a, b, c, d, e, f, g, h, i, j;
  1466. };
  1467. EXPECT_THAT(MyVarType10{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
  1468. struct MyVarType11 {
  1469. int a, b, c, d, e, f, g, h, i, j, k;
  1470. };
  1471. EXPECT_THAT(MyVarType11{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
  1472. struct MyVarType12 {
  1473. int a, b, c, d, e, f, g, h, i, j, k, l;
  1474. };
  1475. EXPECT_THAT(MyVarType12{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
  1476. struct MyVarType13 {
  1477. int a, b, c, d, e, f, g, h, i, j, k, l, m;
  1478. };
  1479. EXPECT_THAT(MyVarType13{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
  1480. struct MyVarType14 {
  1481. int a, b, c, d, e, f, g, h, i, j, k, l, m, n;
  1482. };
  1483. EXPECT_THAT(MyVarType14{},
  1484. FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
  1485. struct MyVarType15 {
  1486. int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o;
  1487. };
  1488. EXPECT_THAT(MyVarType15{},
  1489. FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
  1490. struct MyVarType16 {
  1491. int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p;
  1492. };
  1493. EXPECT_THAT(MyVarType16{},
  1494. FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
  1495. struct MyVarType17 {
  1496. int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q;
  1497. };
  1498. EXPECT_THAT(MyVarType17{},
  1499. FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
  1500. struct MyVarType18 {
  1501. int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r;
  1502. };
  1503. EXPECT_THAT(MyVarType18{},
  1504. FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
  1505. struct MyVarType19 {
  1506. int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s;
  1507. };
  1508. EXPECT_THAT(MyVarType19{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1509. 0, 0, 0, 0, 0));
  1510. }
  1511. #endif
  1512. TEST(PairTest, UseGetInsteadOfMembers) {
  1513. PairWithGet pair{7, "ABC"};
  1514. EXPECT_THAT(pair, Pair(7, "ABC"));
  1515. EXPECT_THAT(pair, Pair(Ge(7), HasSubstr("AB")));
  1516. EXPECT_THAT(pair, Not(Pair(Lt(7), "ABC")));
  1517. std::vector<PairWithGet> v = {{11, "Foo"}, {29, "gMockIsBestMock"}};
  1518. EXPECT_THAT(v,
  1519. ElementsAre(Pair(11, std::string("Foo")), Pair(Ge(10), Not(""))));
  1520. }
  1521. // Tests StartsWith(s).
  1522. TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
  1523. const Matcher<const char*> m1 = StartsWith(std::string(""));
  1524. EXPECT_TRUE(m1.Matches("Hi"));
  1525. EXPECT_TRUE(m1.Matches(""));
  1526. EXPECT_FALSE(m1.Matches(nullptr));
  1527. const Matcher<const std::string&> m2 = StartsWith("Hi");
  1528. EXPECT_TRUE(m2.Matches("Hi"));
  1529. EXPECT_TRUE(m2.Matches("Hi Hi!"));
  1530. EXPECT_TRUE(m2.Matches("High"));
  1531. EXPECT_FALSE(m2.Matches("H"));
  1532. EXPECT_FALSE(m2.Matches(" Hi"));
  1533. #if GTEST_INTERNAL_HAS_STRING_VIEW
  1534. const Matcher<internal::StringView> m_empty =
  1535. StartsWith(internal::StringView(""));
  1536. EXPECT_TRUE(m_empty.Matches(internal::StringView()));
  1537. EXPECT_TRUE(m_empty.Matches(internal::StringView("")));
  1538. EXPECT_TRUE(m_empty.Matches(internal::StringView("not empty")));
  1539. #endif // GTEST_INTERNAL_HAS_STRING_VIEW
  1540. }
  1541. TEST(StartsWithTest, CanDescribeSelf) {
  1542. Matcher<const std::string> m = StartsWith("Hi");
  1543. EXPECT_EQ("starts with \"Hi\"", Describe(m));
  1544. }
  1545. TEST(StartsWithTest, WorksWithStringMatcherOnStringViewMatchee) {
  1546. #if GTEST_INTERNAL_HAS_STRING_VIEW
  1547. EXPECT_THAT(internal::StringView("talk to me goose"),
  1548. StartsWith(std::string("talk")));
  1549. #else
  1550. GTEST_SKIP() << "Not applicable without internal::StringView.";
  1551. #endif // GTEST_INTERNAL_HAS_STRING_VIEW
  1552. }
  1553. // Tests EndsWith(s).
  1554. TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
  1555. const Matcher<const char*> m1 = EndsWith("");
  1556. EXPECT_TRUE(m1.Matches("Hi"));
  1557. EXPECT_TRUE(m1.Matches(""));
  1558. EXPECT_FALSE(m1.Matches(nullptr));
  1559. const Matcher<const std::string&> m2 = EndsWith(std::string("Hi"));
  1560. EXPECT_TRUE(m2.Matches("Hi"));
  1561. EXPECT_TRUE(m2.Matches("Wow Hi Hi"));
  1562. EXPECT_TRUE(m2.Matches("Super Hi"));
  1563. EXPECT_FALSE(m2.Matches("i"));
  1564. EXPECT_FALSE(m2.Matches("Hi "));
  1565. #if GTEST_INTERNAL_HAS_STRING_VIEW
  1566. const Matcher<const internal::StringView&> m4 =
  1567. EndsWith(internal::StringView(""));
  1568. EXPECT_TRUE(m4.Matches("Hi"));
  1569. EXPECT_TRUE(m4.Matches(""));
  1570. EXPECT_TRUE(m4.Matches(internal::StringView()));
  1571. EXPECT_TRUE(m4.Matches(internal::StringView("")));
  1572. #endif // GTEST_INTERNAL_HAS_STRING_VIEW
  1573. }
  1574. TEST(EndsWithTest, CanDescribeSelf) {
  1575. Matcher<const std::string> m = EndsWith("Hi");
  1576. EXPECT_EQ("ends with \"Hi\"", Describe(m));
  1577. }
  1578. // Tests WhenBase64Unescaped.
  1579. TEST(WhenBase64UnescapedTest, MatchesUnescapedBase64Strings) {
  1580. const Matcher<const char*> m1 = WhenBase64Unescaped(EndsWith("!"));
  1581. EXPECT_FALSE(m1.Matches("invalid base64"));
  1582. EXPECT_FALSE(m1.Matches("aGVsbG8gd29ybGQ=")); // hello world
  1583. EXPECT_TRUE(m1.Matches("aGVsbG8gd29ybGQh")); // hello world!
  1584. EXPECT_TRUE(m1.Matches("+/-_IQ")); // \xfb\xff\xbf!
  1585. const Matcher<const std::string&> m2 = WhenBase64Unescaped(EndsWith("!"));
  1586. EXPECT_FALSE(m2.Matches("invalid base64"));
  1587. EXPECT_FALSE(m2.Matches("aGVsbG8gd29ybGQ=")); // hello world
  1588. EXPECT_TRUE(m2.Matches("aGVsbG8gd29ybGQh")); // hello world!
  1589. EXPECT_TRUE(m2.Matches("+/-_IQ")); // \xfb\xff\xbf!
  1590. #if GTEST_INTERNAL_HAS_STRING_VIEW
  1591. const Matcher<const internal::StringView&> m3 =
  1592. WhenBase64Unescaped(EndsWith("!"));
  1593. EXPECT_FALSE(m3.Matches("invalid base64"));
  1594. EXPECT_FALSE(m3.Matches("aGVsbG8gd29ybGQ=")); // hello world
  1595. EXPECT_TRUE(m3.Matches("aGVsbG8gd29ybGQh")); // hello world!
  1596. EXPECT_TRUE(m3.Matches("+/-_IQ")); // \xfb\xff\xbf!
  1597. #endif // GTEST_INTERNAL_HAS_STRING_VIEW
  1598. }
  1599. TEST(WhenBase64UnescapedTest, CanDescribeSelf) {
  1600. const Matcher<const char*> m = WhenBase64Unescaped(EndsWith("!"));
  1601. EXPECT_EQ("matches after Base64Unescape ends with \"!\"", Describe(m));
  1602. }
  1603. // Tests MatchesRegex().
  1604. TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
  1605. const Matcher<const char*> m1 = MatchesRegex("a.*z");
  1606. EXPECT_TRUE(m1.Matches("az"));
  1607. EXPECT_TRUE(m1.Matches("abcz"));
  1608. EXPECT_FALSE(m1.Matches(nullptr));
  1609. const Matcher<const std::string&> m2 = MatchesRegex(new RE("a.*z"));
  1610. EXPECT_TRUE(m2.Matches("azbz"));
  1611. EXPECT_FALSE(m2.Matches("az1"));
  1612. EXPECT_FALSE(m2.Matches("1az"));
  1613. #if GTEST_INTERNAL_HAS_STRING_VIEW
  1614. const Matcher<const internal::StringView&> m3 = MatchesRegex("a.*z");
  1615. EXPECT_TRUE(m3.Matches(internal::StringView("az")));
  1616. EXPECT_TRUE(m3.Matches(internal::StringView("abcz")));
  1617. EXPECT_FALSE(m3.Matches(internal::StringView("1az")));
  1618. EXPECT_FALSE(m3.Matches(internal::StringView()));
  1619. const Matcher<const internal::StringView&> m4 =
  1620. MatchesRegex(internal::StringView(""));
  1621. EXPECT_TRUE(m4.Matches(internal::StringView("")));
  1622. EXPECT_TRUE(m4.Matches(internal::StringView()));
  1623. #endif // GTEST_INTERNAL_HAS_STRING_VIEW
  1624. }
  1625. TEST(MatchesRegexTest, CanDescribeSelf) {
  1626. Matcher<const std::string> m1 = MatchesRegex(std::string("Hi.*"));
  1627. EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1));
  1628. Matcher<const char*> m2 = MatchesRegex(new RE("a.*"));
  1629. EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2));
  1630. #if GTEST_INTERNAL_HAS_STRING_VIEW
  1631. Matcher<const internal::StringView> m3 = MatchesRegex(new RE("0.*"));
  1632. EXPECT_EQ("matches regular expression \"0.*\"", Describe(m3));
  1633. #endif // GTEST_INTERNAL_HAS_STRING_VIEW
  1634. }
  1635. // Tests ContainsRegex().
  1636. TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
  1637. const Matcher<const char*> m1 = ContainsRegex(std::string("a.*z"));
  1638. EXPECT_TRUE(m1.Matches("az"));
  1639. EXPECT_TRUE(m1.Matches("0abcz1"));
  1640. EXPECT_FALSE(m1.Matches(nullptr));
  1641. const Matcher<const std::string&> m2 = ContainsRegex(new RE("a.*z"));
  1642. EXPECT_TRUE(m2.Matches("azbz"));
  1643. EXPECT_TRUE(m2.Matches("az1"));
  1644. EXPECT_FALSE(m2.Matches("1a"));
  1645. #if GTEST_INTERNAL_HAS_STRING_VIEW
  1646. const Matcher<const internal::StringView&> m3 = ContainsRegex(new RE("a.*z"));
  1647. EXPECT_TRUE(m3.Matches(internal::StringView("azbz")));
  1648. EXPECT_TRUE(m3.Matches(internal::StringView("az1")));
  1649. EXPECT_FALSE(m3.Matches(internal::StringView("1a")));
  1650. EXPECT_FALSE(m3.Matches(internal::StringView()));
  1651. const Matcher<const internal::StringView&> m4 =
  1652. ContainsRegex(internal::StringView(""));
  1653. EXPECT_TRUE(m4.Matches(internal::StringView("")));
  1654. EXPECT_TRUE(m4.Matches(internal::StringView()));
  1655. #endif // GTEST_INTERNAL_HAS_STRING_VIEW
  1656. }
  1657. TEST(ContainsRegexTest, CanDescribeSelf) {
  1658. Matcher<const std::string> m1 = ContainsRegex("Hi.*");
  1659. EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1));
  1660. Matcher<const char*> m2 = ContainsRegex(new RE("a.*"));
  1661. EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2));
  1662. #if GTEST_INTERNAL_HAS_STRING_VIEW
  1663. Matcher<const internal::StringView> m3 = ContainsRegex(new RE("0.*"));
  1664. EXPECT_EQ("contains regular expression \"0.*\"", Describe(m3));
  1665. #endif // GTEST_INTERNAL_HAS_STRING_VIEW
  1666. }
  1667. // Tests for wide strings.
  1668. #if GTEST_HAS_STD_WSTRING
  1669. TEST(StdWideStrEqTest, MatchesEqual) {
  1670. Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello"));
  1671. EXPECT_TRUE(m.Matches(L"Hello"));
  1672. EXPECT_FALSE(m.Matches(L"hello"));
  1673. EXPECT_FALSE(m.Matches(nullptr));
  1674. Matcher<const ::std::wstring&> m2 = StrEq(L"Hello");
  1675. EXPECT_TRUE(m2.Matches(L"Hello"));
  1676. EXPECT_FALSE(m2.Matches(L"Hi"));
  1677. Matcher<const ::std::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
  1678. EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
  1679. EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
  1680. ::std::wstring str(L"01204500800");
  1681. str[3] = L'\0';
  1682. Matcher<const ::std::wstring&> m4 = StrEq(str);
  1683. EXPECT_TRUE(m4.Matches(str));
  1684. str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
  1685. Matcher<const ::std::wstring&> m5 = StrEq(str);
  1686. EXPECT_TRUE(m5.Matches(str));
  1687. }
  1688. TEST(StdWideStrEqTest, CanDescribeSelf) {
  1689. Matcher<::std::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v");
  1690. EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
  1691. Describe(m));
  1692. Matcher<::std::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
  1693. EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"", Describe(m2));
  1694. ::std::wstring str(L"01204500800");
  1695. str[3] = L'\0';
  1696. Matcher<const ::std::wstring&> m4 = StrEq(str);
  1697. EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
  1698. str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
  1699. Matcher<const ::std::wstring&> m5 = StrEq(str);
  1700. EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
  1701. }
  1702. TEST(StdWideStrNeTest, MatchesUnequalString) {
  1703. Matcher<const wchar_t*> m = StrNe(L"Hello");
  1704. EXPECT_TRUE(m.Matches(L""));
  1705. EXPECT_TRUE(m.Matches(nullptr));
  1706. EXPECT_FALSE(m.Matches(L"Hello"));
  1707. Matcher<::std::wstring> m2 = StrNe(::std::wstring(L"Hello"));
  1708. EXPECT_TRUE(m2.Matches(L"hello"));
  1709. EXPECT_FALSE(m2.Matches(L"Hello"));
  1710. }
  1711. TEST(StdWideStrNeTest, CanDescribeSelf) {
  1712. Matcher<const wchar_t*> m = StrNe(L"Hi");
  1713. EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
  1714. }
  1715. TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
  1716. Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello"));
  1717. EXPECT_TRUE(m.Matches(L"Hello"));
  1718. EXPECT_TRUE(m.Matches(L"hello"));
  1719. EXPECT_FALSE(m.Matches(L"Hi"));
  1720. EXPECT_FALSE(m.Matches(nullptr));
  1721. Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello");
  1722. EXPECT_TRUE(m2.Matches(L"hello"));
  1723. EXPECT_FALSE(m2.Matches(L"Hi"));
  1724. }
  1725. TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
  1726. ::std::wstring str1(L"oabocdooeoo");
  1727. ::std::wstring str2(L"OABOCDOOEOO");
  1728. Matcher<const ::std::wstring&> m0 = StrCaseEq(str1);
  1729. EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L'\0')));
  1730. str1[3] = str2[3] = L'\0';
  1731. Matcher<const ::std::wstring&> m1 = StrCaseEq(str1);
  1732. EXPECT_TRUE(m1.Matches(str2));
  1733. str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
  1734. str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
  1735. Matcher<const ::std::wstring&> m2 = StrCaseEq(str1);
  1736. str1[9] = str2[9] = L'\0';
  1737. EXPECT_FALSE(m2.Matches(str2));
  1738. Matcher<const ::std::wstring&> m3 = StrCaseEq(str1);
  1739. EXPECT_TRUE(m3.Matches(str2));
  1740. EXPECT_FALSE(m3.Matches(str2 + L"x"));
  1741. str2.append(1, L'\0');
  1742. EXPECT_FALSE(m3.Matches(str2));
  1743. EXPECT_FALSE(m3.Matches(::std::wstring(str2, 0, 9)));
  1744. }
  1745. TEST(StdWideStrCaseEqTest, CanDescribeSelf) {
  1746. Matcher<::std::wstring> m = StrCaseEq(L"Hi");
  1747. EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
  1748. }
  1749. TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
  1750. Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
  1751. EXPECT_TRUE(m.Matches(L"Hi"));
  1752. EXPECT_TRUE(m.Matches(nullptr));
  1753. EXPECT_FALSE(m.Matches(L"Hello"));
  1754. EXPECT_FALSE(m.Matches(L"hello"));
  1755. Matcher<::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello"));
  1756. EXPECT_TRUE(m2.Matches(L""));
  1757. EXPECT_FALSE(m2.Matches(L"Hello"));
  1758. }
  1759. TEST(StdWideStrCaseNeTest, CanDescribeSelf) {
  1760. Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
  1761. EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
  1762. }
  1763. // Tests that HasSubstr() works for matching wstring-typed values.
  1764. TEST(StdWideHasSubstrTest, WorksForStringClasses) {
  1765. const Matcher<::std::wstring> m1 = HasSubstr(L"foo");
  1766. EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food.")));
  1767. EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo")));
  1768. const Matcher<const ::std::wstring&> m2 = HasSubstr(L"foo");
  1769. EXPECT_TRUE(m2.Matches(::std::wstring(L"I love food.")));
  1770. EXPECT_FALSE(m2.Matches(::std::wstring(L"tofo")));
  1771. }
  1772. // Tests that HasSubstr() works for matching C-wide-string-typed values.
  1773. TEST(StdWideHasSubstrTest, WorksForCStrings) {
  1774. const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
  1775. EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
  1776. EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
  1777. EXPECT_FALSE(m1.Matches(nullptr));
  1778. const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
  1779. EXPECT_TRUE(m2.Matches(L"I love food."));
  1780. EXPECT_FALSE(m2.Matches(L"tofo"));
  1781. EXPECT_FALSE(m2.Matches(nullptr));
  1782. }
  1783. // Tests that HasSubstr(s) describes itself properly.
  1784. TEST(StdWideHasSubstrTest, CanDescribeSelf) {
  1785. Matcher<::std::wstring> m = HasSubstr(L"foo\n\"");
  1786. EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
  1787. }
  1788. // Tests StartsWith(s).
  1789. TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) {
  1790. const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(L""));
  1791. EXPECT_TRUE(m1.Matches(L"Hi"));
  1792. EXPECT_TRUE(m1.Matches(L""));
  1793. EXPECT_FALSE(m1.Matches(nullptr));
  1794. const Matcher<const ::std::wstring&> m2 = StartsWith(L"Hi");
  1795. EXPECT_TRUE(m2.Matches(L"Hi"));
  1796. EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
  1797. EXPECT_TRUE(m2.Matches(L"High"));
  1798. EXPECT_FALSE(m2.Matches(L"H"));
  1799. EXPECT_FALSE(m2.Matches(L" Hi"));
  1800. }
  1801. TEST(StdWideStartsWithTest, CanDescribeSelf) {
  1802. Matcher<const ::std::wstring> m = StartsWith(L"Hi");
  1803. EXPECT_EQ("starts with L\"Hi\"", Describe(m));
  1804. }
  1805. // Tests EndsWith(s).
  1806. TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) {
  1807. const Matcher<const wchar_t*> m1 = EndsWith(L"");
  1808. EXPECT_TRUE(m1.Matches(L"Hi"));
  1809. EXPECT_TRUE(m1.Matches(L""));
  1810. EXPECT_FALSE(m1.Matches(nullptr));
  1811. const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L"Hi"));
  1812. EXPECT_TRUE(m2.Matches(L"Hi"));
  1813. EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
  1814. EXPECT_TRUE(m2.Matches(L"Super Hi"));
  1815. EXPECT_FALSE(m2.Matches(L"i"));
  1816. EXPECT_FALSE(m2.Matches(L"Hi "));
  1817. }
  1818. TEST(StdWideEndsWithTest, CanDescribeSelf) {
  1819. Matcher<const ::std::wstring> m = EndsWith(L"Hi");
  1820. EXPECT_EQ("ends with L\"Hi\"", Describe(m));
  1821. }
  1822. #endif // GTEST_HAS_STD_WSTRING
  1823. TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) {
  1824. StringMatchResultListener listener1;
  1825. EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1));
  1826. EXPECT_EQ("% 2 == 0", listener1.str());
  1827. StringMatchResultListener listener2;
  1828. EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2));
  1829. EXPECT_EQ("", listener2.str());
  1830. }
  1831. TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) {
  1832. const Matcher<int> is_even = PolymorphicIsEven();
  1833. StringMatchResultListener listener1;
  1834. EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1));
  1835. EXPECT_EQ("% 2 == 0", listener1.str());
  1836. const Matcher<const double&> is_zero = Eq(0);
  1837. StringMatchResultListener listener2;
  1838. EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2));
  1839. EXPECT_EQ("", listener2.str());
  1840. }
  1841. MATCHER(ConstructNoArg, "") { return true; }
  1842. MATCHER_P(Construct1Arg, arg1, "") { return true; }
  1843. MATCHER_P2(Construct2Args, arg1, arg2, "") { return true; }
  1844. TEST(MatcherConstruct, ExplicitVsImplicit) {
  1845. {
  1846. // No arg constructor can be constructed with empty brace.
  1847. ConstructNoArgMatcher m = {};
  1848. (void)m;
  1849. // And with no args
  1850. ConstructNoArgMatcher m2;
  1851. (void)m2;
  1852. }
  1853. {
  1854. // The one arg constructor has an explicit constructor.
  1855. // This is to prevent the implicit conversion.
  1856. using M = Construct1ArgMatcherP<int>;
  1857. EXPECT_TRUE((std::is_constructible<M, int>::value));
  1858. EXPECT_FALSE((std::is_convertible<int, M>::value));
  1859. }
  1860. {
  1861. // Multiple arg matchers can be constructed with an implicit construction.
  1862. Construct2ArgsMatcherP2<int, double> m = {1, 2.2};
  1863. (void)m;
  1864. }
  1865. }
  1866. MATCHER_P(Really, inner_matcher, "") {
  1867. return ExplainMatchResult(inner_matcher, arg, result_listener);
  1868. }
  1869. TEST(ExplainMatchResultTest, WorksInsideMATCHER) {
  1870. EXPECT_THAT(0, Really(Eq(0)));
  1871. }
  1872. TEST(DescribeMatcherTest, WorksWithValue) {
  1873. EXPECT_EQ("is equal to 42", DescribeMatcher<int>(42));
  1874. EXPECT_EQ("isn't equal to 42", DescribeMatcher<int>(42, true));
  1875. }
  1876. TEST(DescribeMatcherTest, WorksWithMonomorphicMatcher) {
  1877. const Matcher<int> monomorphic = Le(0);
  1878. EXPECT_EQ("is <= 0", DescribeMatcher<int>(monomorphic));
  1879. EXPECT_EQ("isn't <= 0", DescribeMatcher<int>(monomorphic, true));
  1880. }
  1881. TEST(DescribeMatcherTest, WorksWithPolymorphicMatcher) {
  1882. EXPECT_EQ("is even", DescribeMatcher<int>(PolymorphicIsEven()));
  1883. EXPECT_EQ("is odd", DescribeMatcher<int>(PolymorphicIsEven(), true));
  1884. }
  1885. MATCHER_P(FieldIIs, inner_matcher, "") {
  1886. return ExplainMatchResult(inner_matcher, arg.i, result_listener);
  1887. }
  1888. #if GTEST_HAS_RTTI
  1889. TEST(WhenDynamicCastToTest, SameType) {
  1890. Derived derived;
  1891. derived.i = 4;
  1892. // Right type. A pointer is passed down.
  1893. Base* as_base_ptr = &derived;
  1894. EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(Not(IsNull())));
  1895. EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(Pointee(FieldIIs(4))));
  1896. EXPECT_THAT(as_base_ptr,
  1897. Not(WhenDynamicCastTo<Derived*>(Pointee(FieldIIs(5)))));
  1898. }
  1899. TEST(WhenDynamicCastToTest, WrongTypes) {
  1900. Base base;
  1901. Derived derived;
  1902. OtherDerived other_derived;
  1903. // Wrong types. NULL is passed.
  1904. EXPECT_THAT(&base, Not(WhenDynamicCastTo<Derived*>(Pointee(_))));
  1905. EXPECT_THAT(&base, WhenDynamicCastTo<Derived*>(IsNull()));
  1906. Base* as_base_ptr = &derived;
  1907. EXPECT_THAT(as_base_ptr, Not(WhenDynamicCastTo<OtherDerived*>(Pointee(_))));
  1908. EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<OtherDerived*>(IsNull()));
  1909. as_base_ptr = &other_derived;
  1910. EXPECT_THAT(as_base_ptr, Not(WhenDynamicCastTo<Derived*>(Pointee(_))));
  1911. EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(IsNull()));
  1912. }
  1913. TEST(WhenDynamicCastToTest, AlreadyNull) {
  1914. // Already NULL.
  1915. Base* as_base_ptr = nullptr;
  1916. EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(IsNull()));
  1917. }
  1918. struct AmbiguousCastTypes {
  1919. class VirtualDerived : public virtual Base {};
  1920. class DerivedSub1 : public VirtualDerived {};
  1921. class DerivedSub2 : public VirtualDerived {};
  1922. class ManyDerivedInHierarchy : public DerivedSub1, public DerivedSub2 {};
  1923. };
  1924. TEST(WhenDynamicCastToTest, AmbiguousCast) {
  1925. AmbiguousCastTypes::DerivedSub1 sub1;
  1926. AmbiguousCastTypes::ManyDerivedInHierarchy many_derived;
  1927. // Multiply derived from Base. dynamic_cast<> returns NULL.
  1928. Base* as_base_ptr =
  1929. static_cast<AmbiguousCastTypes::DerivedSub1*>(&many_derived);
  1930. EXPECT_THAT(as_base_ptr,
  1931. WhenDynamicCastTo<AmbiguousCastTypes::VirtualDerived*>(IsNull()));
  1932. as_base_ptr = &sub1;
  1933. EXPECT_THAT(
  1934. as_base_ptr,
  1935. WhenDynamicCastTo<AmbiguousCastTypes::VirtualDerived*>(Not(IsNull())));
  1936. }
  1937. TEST(WhenDynamicCastToTest, Describe) {
  1938. Matcher<Base*> matcher = WhenDynamicCastTo<Derived*>(Pointee(_));
  1939. const std::string prefix =
  1940. "when dynamic_cast to " + internal::GetTypeName<Derived*>() + ", ";
  1941. EXPECT_EQ(prefix + "points to a value that is anything", Describe(matcher));
  1942. EXPECT_EQ(prefix + "does not point to a value that is anything",
  1943. DescribeNegation(matcher));
  1944. }
  1945. TEST(WhenDynamicCastToTest, Explain) {
  1946. Matcher<Base*> matcher = WhenDynamicCastTo<Derived*>(Pointee(_));
  1947. Base* null = nullptr;
  1948. EXPECT_THAT(Explain(matcher, null), HasSubstr("NULL"));
  1949. Derived derived;
  1950. EXPECT_TRUE(matcher.Matches(&derived));
  1951. EXPECT_THAT(Explain(matcher, &derived), HasSubstr("which points to "));
  1952. // With references, the matcher itself can fail. Test for that one.
  1953. Matcher<const Base&> ref_matcher = WhenDynamicCastTo<const OtherDerived&>(_);
  1954. EXPECT_THAT(Explain(ref_matcher, derived),
  1955. HasSubstr("which cannot be dynamic_cast"));
  1956. }
  1957. TEST(WhenDynamicCastToTest, GoodReference) {
  1958. Derived derived;
  1959. derived.i = 4;
  1960. Base& as_base_ref = derived;
  1961. EXPECT_THAT(as_base_ref, WhenDynamicCastTo<const Derived&>(FieldIIs(4)));
  1962. EXPECT_THAT(as_base_ref, WhenDynamicCastTo<const Derived&>(Not(FieldIIs(5))));
  1963. }
  1964. TEST(WhenDynamicCastToTest, BadReference) {
  1965. Derived derived;
  1966. Base& as_base_ref = derived;
  1967. EXPECT_THAT(as_base_ref, Not(WhenDynamicCastTo<const OtherDerived&>(_)));
  1968. }
  1969. #endif // GTEST_HAS_RTTI
  1970. class DivisibleByImpl {
  1971. public:
  1972. explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {}
  1973. // For testing using ExplainMatchResultTo() with polymorphic matchers.
  1974. template <typename T>
  1975. bool MatchAndExplain(const T& n, MatchResultListener* listener) const {
  1976. *listener << "which is " << (n % divider_) << " modulo " << divider_;
  1977. return (n % divider_) == 0;
  1978. }
  1979. void DescribeTo(ostream* os) const { *os << "is divisible by " << divider_; }
  1980. void DescribeNegationTo(ostream* os) const {
  1981. *os << "is not divisible by " << divider_;
  1982. }
  1983. void set_divider(int a_divider) { divider_ = a_divider; }
  1984. int divider() const { return divider_; }
  1985. private:
  1986. int divider_;
  1987. };
  1988. PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) {
  1989. return MakePolymorphicMatcher(DivisibleByImpl(n));
  1990. }
  1991. // Tests that when AllOf() fails, only the first failing matcher is
  1992. // asked to explain why.
  1993. TEST(ExplainMatchResultTest, AllOf_False_False) {
  1994. const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
  1995. EXPECT_EQ("which is 1 modulo 4", Explain(m, 5));
  1996. }
  1997. // Tests that when AllOf() fails, only the first failing matcher is
  1998. // asked to explain why.
  1999. TEST(ExplainMatchResultTest, AllOf_False_True) {
  2000. const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
  2001. EXPECT_EQ("which is 2 modulo 4", Explain(m, 6));
  2002. }
  2003. // Tests that when AllOf() fails, only the first failing matcher is
  2004. // asked to explain why.
  2005. TEST(ExplainMatchResultTest, AllOf_True_False) {
  2006. const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));
  2007. EXPECT_EQ("which is 2 modulo 3", Explain(m, 5));
  2008. }
  2009. // Tests that when AllOf() succeeds, all matchers are asked to explain
  2010. // why.
  2011. TEST(ExplainMatchResultTest, AllOf_True_True) {
  2012. const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3));
  2013. EXPECT_EQ("which is 0 modulo 2, and which is 0 modulo 3", Explain(m, 6));
  2014. }
  2015. // Tests that when AllOf() succeeds, but matchers have no explanation,
  2016. // the matcher description is used.
  2017. TEST(ExplainMatchResultTest, AllOf_True_True_2) {
  2018. const Matcher<int> m = AllOf(Ge(2), Le(3));
  2019. EXPECT_EQ("is >= 2, and is <= 3", Explain(m, 2));
  2020. }
  2021. INSTANTIATE_GTEST_MATCHER_TEST_P(ExplainmatcherResultTest);
  2022. TEST_P(ExplainmatcherResultTestP, MonomorphicMatcher) {
  2023. const Matcher<int> m = GreaterThan(5);
  2024. EXPECT_EQ("which is 1 more than 5", Explain(m, 6));
  2025. }
  2026. // Tests PolymorphicMatcher::mutable_impl().
  2027. TEST(PolymorphicMatcherTest, CanAccessMutableImpl) {
  2028. PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
  2029. DivisibleByImpl& impl = m.mutable_impl();
  2030. EXPECT_EQ(42, impl.divider());
  2031. impl.set_divider(0);
  2032. EXPECT_EQ(0, m.mutable_impl().divider());
  2033. }
  2034. // Tests PolymorphicMatcher::impl().
  2035. TEST(PolymorphicMatcherTest, CanAccessImpl) {
  2036. const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
  2037. const DivisibleByImpl& impl = m.impl();
  2038. EXPECT_EQ(42, impl.divider());
  2039. }
  2040. } // namespace
  2041. } // namespace gmock_matchers_test
  2042. } // namespace testing
  2043. GTEST_DISABLE_MSC_WARNINGS_POP_() // 4244 4100