gmock-generated-matchers_test.cc 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  1. // Copyright 2008, 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 the built-in matchers generated by a script.
  32. // Silence warning C4244: 'initializing': conversion from 'int' to 'short',
  33. // possible loss of data and C4100, unreferenced local parameter
  34. #ifdef _MSC_VER
  35. # pragma warning(push)
  36. # pragma warning(disable:4244)
  37. # pragma warning(disable:4100)
  38. #endif
  39. #include "gmock/gmock-generated-matchers.h"
  40. #include <list>
  41. #include <map>
  42. #include <memory>
  43. #include <set>
  44. #include <sstream>
  45. #include <string>
  46. #include <utility>
  47. #include <vector>
  48. #include "gmock/gmock.h"
  49. #include "gtest/gtest.h"
  50. #include "gtest/gtest-spi.h"
  51. namespace {
  52. using std::list;
  53. using std::map;
  54. using std::pair;
  55. using std::set;
  56. using std::stringstream;
  57. using std::vector;
  58. using testing::_;
  59. using testing::AllOf;
  60. using testing::AllOfArray;
  61. using testing::AnyOf;
  62. using testing::AnyOfArray;
  63. using testing::Args;
  64. using testing::Contains;
  65. using testing::ElementsAre;
  66. using testing::ElementsAreArray;
  67. using testing::Eq;
  68. using testing::Ge;
  69. using testing::Gt;
  70. using testing::Le;
  71. using testing::Lt;
  72. using testing::MakeMatcher;
  73. using testing::Matcher;
  74. using testing::MatcherInterface;
  75. using testing::MatchResultListener;
  76. using testing::Ne;
  77. using testing::Not;
  78. using testing::Pointee;
  79. using testing::PrintToString;
  80. using testing::Ref;
  81. using testing::StaticAssertTypeEq;
  82. using testing::StrEq;
  83. using testing::Value;
  84. using testing::internal::ElementsAreArrayMatcher;
  85. // Returns the description of the given matcher.
  86. template <typename T>
  87. std::string Describe(const Matcher<T>& m) {
  88. stringstream ss;
  89. m.DescribeTo(&ss);
  90. return ss.str();
  91. }
  92. // Returns the description of the negation of the given matcher.
  93. template <typename T>
  94. std::string DescribeNegation(const Matcher<T>& m) {
  95. stringstream ss;
  96. m.DescribeNegationTo(&ss);
  97. return ss.str();
  98. }
  99. // Returns the reason why x matches, or doesn't match, m.
  100. template <typename MatcherType, typename Value>
  101. std::string Explain(const MatcherType& m, const Value& x) {
  102. stringstream ss;
  103. m.ExplainMatchResultTo(x, &ss);
  104. return ss.str();
  105. }
  106. // For testing ExplainMatchResultTo().
  107. class GreaterThanMatcher : public MatcherInterface<int> {
  108. public:
  109. explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
  110. void DescribeTo(::std::ostream* os) const override {
  111. *os << "is greater than " << rhs_;
  112. }
  113. bool MatchAndExplain(int lhs, MatchResultListener* listener) const override {
  114. const int diff = lhs - rhs_;
  115. if (diff > 0) {
  116. *listener << "which is " << diff << " more than " << rhs_;
  117. } else if (diff == 0) {
  118. *listener << "which is the same as " << rhs_;
  119. } else {
  120. *listener << "which is " << -diff << " less than " << rhs_;
  121. }
  122. return lhs > rhs_;
  123. }
  124. private:
  125. int rhs_;
  126. };
  127. Matcher<int> GreaterThan(int n) {
  128. return MakeMatcher(new GreaterThanMatcher(n));
  129. }
  130. // Tests for ElementsAre().
  131. TEST(ElementsAreTest, CanDescribeExpectingNoElement) {
  132. Matcher<const vector<int>&> m = ElementsAre();
  133. EXPECT_EQ("is empty", Describe(m));
  134. }
  135. TEST(ElementsAreTest, CanDescribeExpectingOneElement) {
  136. Matcher<vector<int> > m = ElementsAre(Gt(5));
  137. EXPECT_EQ("has 1 element that is > 5", Describe(m));
  138. }
  139. TEST(ElementsAreTest, CanDescribeExpectingManyElements) {
  140. Matcher<list<std::string> > m = ElementsAre(StrEq("one"), "two");
  141. EXPECT_EQ("has 2 elements where\n"
  142. "element #0 is equal to \"one\",\n"
  143. "element #1 is equal to \"two\"", Describe(m));
  144. }
  145. TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) {
  146. Matcher<vector<int> > m = ElementsAre();
  147. EXPECT_EQ("isn't empty", DescribeNegation(m));
  148. }
  149. TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElment) {
  150. Matcher<const list<int>& > m = ElementsAre(Gt(5));
  151. EXPECT_EQ("doesn't have 1 element, or\n"
  152. "element #0 isn't > 5", DescribeNegation(m));
  153. }
  154. TEST(ElementsAreTest, CanDescribeNegationOfExpectingManyElements) {
  155. Matcher<const list<std::string>&> m = ElementsAre("one", "two");
  156. EXPECT_EQ("doesn't have 2 elements, or\n"
  157. "element #0 isn't equal to \"one\", or\n"
  158. "element #1 isn't equal to \"two\"", DescribeNegation(m));
  159. }
  160. TEST(ElementsAreTest, DoesNotExplainTrivialMatch) {
  161. Matcher<const list<int>& > m = ElementsAre(1, Ne(2));
  162. list<int> test_list;
  163. test_list.push_back(1);
  164. test_list.push_back(3);
  165. EXPECT_EQ("", Explain(m, test_list)); // No need to explain anything.
  166. }
  167. TEST(ElementsAreTest, ExplainsNonTrivialMatch) {
  168. Matcher<const vector<int>& > m =
  169. ElementsAre(GreaterThan(1), 0, GreaterThan(2));
  170. const int a[] = { 10, 0, 100 };
  171. vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a));
  172. EXPECT_EQ("whose element #0 matches, which is 9 more than 1,\n"
  173. "and whose element #2 matches, which is 98 more than 2",
  174. Explain(m, test_vector));
  175. }
  176. TEST(ElementsAreTest, CanExplainMismatchWrongSize) {
  177. Matcher<const list<int>& > m = ElementsAre(1, 3);
  178. list<int> test_list;
  179. // No need to explain when the container is empty.
  180. EXPECT_EQ("", Explain(m, test_list));
  181. test_list.push_back(1);
  182. EXPECT_EQ("which has 1 element", Explain(m, test_list));
  183. }
  184. TEST(ElementsAreTest, CanExplainMismatchRightSize) {
  185. Matcher<const vector<int>& > m = ElementsAre(1, GreaterThan(5));
  186. vector<int> v;
  187. v.push_back(2);
  188. v.push_back(1);
  189. EXPECT_EQ("whose element #0 doesn't match", Explain(m, v));
  190. v[0] = 1;
  191. EXPECT_EQ("whose element #1 doesn't match, which is 4 less than 5",
  192. Explain(m, v));
  193. }
  194. TEST(ElementsAreTest, MatchesOneElementVector) {
  195. vector<std::string> test_vector;
  196. test_vector.push_back("test string");
  197. EXPECT_THAT(test_vector, ElementsAre(StrEq("test string")));
  198. }
  199. TEST(ElementsAreTest, MatchesOneElementList) {
  200. list<std::string> test_list;
  201. test_list.push_back("test string");
  202. EXPECT_THAT(test_list, ElementsAre("test string"));
  203. }
  204. TEST(ElementsAreTest, MatchesThreeElementVector) {
  205. vector<std::string> test_vector;
  206. test_vector.push_back("one");
  207. test_vector.push_back("two");
  208. test_vector.push_back("three");
  209. EXPECT_THAT(test_vector, ElementsAre("one", StrEq("two"), _));
  210. }
  211. TEST(ElementsAreTest, MatchesOneElementEqMatcher) {
  212. vector<int> test_vector;
  213. test_vector.push_back(4);
  214. EXPECT_THAT(test_vector, ElementsAre(Eq(4)));
  215. }
  216. TEST(ElementsAreTest, MatchesOneElementAnyMatcher) {
  217. vector<int> test_vector;
  218. test_vector.push_back(4);
  219. EXPECT_THAT(test_vector, ElementsAre(_));
  220. }
  221. TEST(ElementsAreTest, MatchesOneElementValue) {
  222. vector<int> test_vector;
  223. test_vector.push_back(4);
  224. EXPECT_THAT(test_vector, ElementsAre(4));
  225. }
  226. TEST(ElementsAreTest, MatchesThreeElementsMixedMatchers) {
  227. vector<int> test_vector;
  228. test_vector.push_back(1);
  229. test_vector.push_back(2);
  230. test_vector.push_back(3);
  231. EXPECT_THAT(test_vector, ElementsAre(1, Eq(2), _));
  232. }
  233. TEST(ElementsAreTest, MatchesTenElementVector) {
  234. const int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  235. vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a));
  236. EXPECT_THAT(test_vector,
  237. // The element list can contain values and/or matchers
  238. // of different types.
  239. ElementsAre(0, Ge(0), _, 3, 4, Ne(2), Eq(6), 7, 8, _));
  240. }
  241. TEST(ElementsAreTest, DoesNotMatchWrongSize) {
  242. vector<std::string> test_vector;
  243. test_vector.push_back("test string");
  244. test_vector.push_back("test string");
  245. Matcher<vector<std::string> > m = ElementsAre(StrEq("test string"));
  246. EXPECT_FALSE(m.Matches(test_vector));
  247. }
  248. TEST(ElementsAreTest, DoesNotMatchWrongValue) {
  249. vector<std::string> test_vector;
  250. test_vector.push_back("other string");
  251. Matcher<vector<std::string> > m = ElementsAre(StrEq("test string"));
  252. EXPECT_FALSE(m.Matches(test_vector));
  253. }
  254. TEST(ElementsAreTest, DoesNotMatchWrongOrder) {
  255. vector<std::string> test_vector;
  256. test_vector.push_back("one");
  257. test_vector.push_back("three");
  258. test_vector.push_back("two");
  259. Matcher<vector<std::string> > m =
  260. ElementsAre(StrEq("one"), StrEq("two"), StrEq("three"));
  261. EXPECT_FALSE(m.Matches(test_vector));
  262. }
  263. TEST(ElementsAreTest, WorksForNestedContainer) {
  264. const char* strings[] = {
  265. "Hi",
  266. "world"
  267. };
  268. vector<list<char> > nested;
  269. for (size_t i = 0; i < GTEST_ARRAY_SIZE_(strings); i++) {
  270. nested.push_back(list<char>(strings[i], strings[i] + strlen(strings[i])));
  271. }
  272. EXPECT_THAT(nested, ElementsAre(ElementsAre('H', Ne('e')),
  273. ElementsAre('w', 'o', _, _, 'd')));
  274. EXPECT_THAT(nested, Not(ElementsAre(ElementsAre('H', 'e'),
  275. ElementsAre('w', 'o', _, _, 'd'))));
  276. }
  277. TEST(ElementsAreTest, WorksWithByRefElementMatchers) {
  278. int a[] = { 0, 1, 2 };
  279. vector<int> v(a, a + GTEST_ARRAY_SIZE_(a));
  280. EXPECT_THAT(v, ElementsAre(Ref(v[0]), Ref(v[1]), Ref(v[2])));
  281. EXPECT_THAT(v, Not(ElementsAre(Ref(v[0]), Ref(v[1]), Ref(a[2]))));
  282. }
  283. TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) {
  284. int a[] = { 0, 1, 2 };
  285. vector<int> v(a, a + GTEST_ARRAY_SIZE_(a));
  286. EXPECT_THAT(&v, Pointee(ElementsAre(0, 1, _)));
  287. EXPECT_THAT(&v, Not(Pointee(ElementsAre(0, _, 3))));
  288. }
  289. TEST(ElementsAreTest, WorksWithNativeArrayPassedByReference) {
  290. int array[] = { 0, 1, 2 };
  291. EXPECT_THAT(array, ElementsAre(0, 1, _));
  292. EXPECT_THAT(array, Not(ElementsAre(1, _, _)));
  293. EXPECT_THAT(array, Not(ElementsAre(0, _)));
  294. }
  295. class NativeArrayPassedAsPointerAndSize {
  296. public:
  297. NativeArrayPassedAsPointerAndSize() {}
  298. MOCK_METHOD2(Helper, void(int* array, int size));
  299. private:
  300. GTEST_DISALLOW_COPY_AND_ASSIGN_(NativeArrayPassedAsPointerAndSize);
  301. };
  302. TEST(ElementsAreTest, WorksWithNativeArrayPassedAsPointerAndSize) {
  303. int array[] = { 0, 1 };
  304. ::std::tuple<int*, size_t> array_as_tuple(array, 2);
  305. EXPECT_THAT(array_as_tuple, ElementsAre(0, 1));
  306. EXPECT_THAT(array_as_tuple, Not(ElementsAre(0)));
  307. NativeArrayPassedAsPointerAndSize helper;
  308. EXPECT_CALL(helper, Helper(_, _))
  309. .With(ElementsAre(0, 1));
  310. helper.Helper(array, 2);
  311. }
  312. TEST(ElementsAreTest, WorksWithTwoDimensionalNativeArray) {
  313. const char a2[][3] = { "hi", "lo" };
  314. EXPECT_THAT(a2, ElementsAre(ElementsAre('h', 'i', '\0'),
  315. ElementsAre('l', 'o', '\0')));
  316. EXPECT_THAT(a2, ElementsAre(StrEq("hi"), StrEq("lo")));
  317. EXPECT_THAT(a2, ElementsAre(Not(ElementsAre('h', 'o', '\0')),
  318. ElementsAre('l', 'o', '\0')));
  319. }
  320. TEST(ElementsAreTest, AcceptsStringLiteral) {
  321. std::string array[] = {"hi", "one", "two"};
  322. EXPECT_THAT(array, ElementsAre("hi", "one", "two"));
  323. EXPECT_THAT(array, Not(ElementsAre("hi", "one", "too")));
  324. }
  325. #ifndef _MSC_VER
  326. // The following test passes a value of type const char[] to a
  327. // function template that expects const T&. Some versions of MSVC
  328. // generates a compiler error C2665 for that. We believe it's a bug
  329. // in MSVC. Therefore this test is #if-ed out for MSVC.
  330. // Declared here with the size unknown. Defined AFTER the following test.
  331. extern const char kHi[];
  332. TEST(ElementsAreTest, AcceptsArrayWithUnknownSize) {
  333. // The size of kHi is not known in this test, but ElementsAre() should
  334. // still accept it.
  335. std::string array1[] = {"hi"};
  336. EXPECT_THAT(array1, ElementsAre(kHi));
  337. std::string array2[] = {"ho"};
  338. EXPECT_THAT(array2, Not(ElementsAre(kHi)));
  339. }
  340. const char kHi[] = "hi";
  341. #endif // _MSC_VER
  342. TEST(ElementsAreTest, MakesCopyOfArguments) {
  343. int x = 1;
  344. int y = 2;
  345. // This should make a copy of x and y.
  346. ::testing::internal::ElementsAreMatcher<std::tuple<int, int> >
  347. polymorphic_matcher = ElementsAre(x, y);
  348. // Changing x and y now shouldn't affect the meaning of the above matcher.
  349. x = y = 0;
  350. const int array1[] = { 1, 2 };
  351. EXPECT_THAT(array1, polymorphic_matcher);
  352. const int array2[] = { 0, 0 };
  353. EXPECT_THAT(array2, Not(polymorphic_matcher));
  354. }
  355. // Tests for ElementsAreArray(). Since ElementsAreArray() shares most
  356. // of the implementation with ElementsAre(), we don't test it as
  357. // thoroughly here.
  358. TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) {
  359. const int a[] = { 1, 2, 3 };
  360. vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a));
  361. EXPECT_THAT(test_vector, ElementsAreArray(a));
  362. test_vector[2] = 0;
  363. EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
  364. }
  365. TEST(ElementsAreArrayTest, CanBeCreatedWithArraySize) {
  366. const char* a[] = { "one", "two", "three" };
  367. vector<std::string> test_vector(a, a + GTEST_ARRAY_SIZE_(a));
  368. EXPECT_THAT(test_vector, ElementsAreArray(a, GTEST_ARRAY_SIZE_(a)));
  369. const char** p = a;
  370. test_vector[0] = "1";
  371. EXPECT_THAT(test_vector, Not(ElementsAreArray(p, GTEST_ARRAY_SIZE_(a))));
  372. }
  373. TEST(ElementsAreArrayTest, CanBeCreatedWithoutArraySize) {
  374. const char* a[] = { "one", "two", "three" };
  375. vector<std::string> test_vector(a, a + GTEST_ARRAY_SIZE_(a));
  376. EXPECT_THAT(test_vector, ElementsAreArray(a));
  377. test_vector[0] = "1";
  378. EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
  379. }
  380. TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) {
  381. const Matcher<std::string> kMatcherArray[] = {StrEq("one"), StrEq("two"),
  382. StrEq("three")};
  383. vector<std::string> test_vector;
  384. test_vector.push_back("one");
  385. test_vector.push_back("two");
  386. test_vector.push_back("three");
  387. EXPECT_THAT(test_vector, ElementsAreArray(kMatcherArray));
  388. test_vector.push_back("three");
  389. EXPECT_THAT(test_vector, Not(ElementsAreArray(kMatcherArray)));
  390. }
  391. TEST(ElementsAreArrayTest, CanBeCreatedWithVector) {
  392. const int a[] = { 1, 2, 3 };
  393. vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a));
  394. const vector<int> expected(a, a + GTEST_ARRAY_SIZE_(a));
  395. EXPECT_THAT(test_vector, ElementsAreArray(expected));
  396. test_vector.push_back(4);
  397. EXPECT_THAT(test_vector, Not(ElementsAreArray(expected)));
  398. }
  399. TEST(ElementsAreArrayTest, TakesInitializerList) {
  400. const int a[5] = { 1, 2, 3, 4, 5 };
  401. EXPECT_THAT(a, ElementsAreArray({ 1, 2, 3, 4, 5 }));
  402. EXPECT_THAT(a, Not(ElementsAreArray({ 1, 2, 3, 5, 4 })));
  403. EXPECT_THAT(a, Not(ElementsAreArray({ 1, 2, 3, 4, 6 })));
  404. }
  405. TEST(ElementsAreArrayTest, TakesInitializerListOfCStrings) {
  406. const std::string a[5] = {"a", "b", "c", "d", "e"};
  407. EXPECT_THAT(a, ElementsAreArray({ "a", "b", "c", "d", "e" }));
  408. EXPECT_THAT(a, Not(ElementsAreArray({ "a", "b", "c", "e", "d" })));
  409. EXPECT_THAT(a, Not(ElementsAreArray({ "a", "b", "c", "d", "ef" })));
  410. }
  411. TEST(ElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) {
  412. const int a[5] = { 1, 2, 3, 4, 5 };
  413. EXPECT_THAT(a, ElementsAreArray(
  414. { Eq(1), Eq(2), Eq(3), Eq(4), Eq(5) }));
  415. EXPECT_THAT(a, Not(ElementsAreArray(
  416. { Eq(1), Eq(2), Eq(3), Eq(4), Eq(6) })));
  417. }
  418. TEST(ElementsAreArrayTest,
  419. TakesInitializerListOfDifferentTypedMatchers) {
  420. const int a[5] = { 1, 2, 3, 4, 5 };
  421. // The compiler cannot infer the type of the initializer list if its
  422. // elements have different types. We must explicitly specify the
  423. // unified element type in this case.
  424. EXPECT_THAT(a, ElementsAreArray<Matcher<int> >(
  425. { Eq(1), Ne(-2), Ge(3), Le(4), Eq(5) }));
  426. EXPECT_THAT(a, Not(ElementsAreArray<Matcher<int> >(
  427. { Eq(1), Ne(-2), Ge(3), Le(4), Eq(6) })));
  428. }
  429. TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherVector) {
  430. const int a[] = { 1, 2, 3 };
  431. const Matcher<int> kMatchers[] = { Eq(1), Eq(2), Eq(3) };
  432. vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a));
  433. const vector<Matcher<int> > expected(
  434. kMatchers, kMatchers + GTEST_ARRAY_SIZE_(kMatchers));
  435. EXPECT_THAT(test_vector, ElementsAreArray(expected));
  436. test_vector.push_back(4);
  437. EXPECT_THAT(test_vector, Not(ElementsAreArray(expected)));
  438. }
  439. TEST(ElementsAreArrayTest, CanBeCreatedWithIteratorRange) {
  440. const int a[] = { 1, 2, 3 };
  441. const vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a));
  442. const vector<int> expected(a, a + GTEST_ARRAY_SIZE_(a));
  443. EXPECT_THAT(test_vector, ElementsAreArray(expected.begin(), expected.end()));
  444. // Pointers are iterators, too.
  445. EXPECT_THAT(test_vector, ElementsAreArray(a, a + GTEST_ARRAY_SIZE_(a)));
  446. // The empty range of NULL pointers should also be okay.
  447. int* const null_int = nullptr;
  448. EXPECT_THAT(test_vector, Not(ElementsAreArray(null_int, null_int)));
  449. EXPECT_THAT((vector<int>()), ElementsAreArray(null_int, null_int));
  450. }
  451. // Since ElementsAre() and ElementsAreArray() share much of the
  452. // implementation, we only do a sanity test for native arrays here.
  453. TEST(ElementsAreArrayTest, WorksWithNativeArray) {
  454. ::std::string a[] = { "hi", "ho" };
  455. ::std::string b[] = { "hi", "ho" };
  456. EXPECT_THAT(a, ElementsAreArray(b));
  457. EXPECT_THAT(a, ElementsAreArray(b, 2));
  458. EXPECT_THAT(a, Not(ElementsAreArray(b, 1)));
  459. }
  460. TEST(ElementsAreArrayTest, SourceLifeSpan) {
  461. const int a[] = { 1, 2, 3 };
  462. vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a));
  463. vector<int> expect(a, a + GTEST_ARRAY_SIZE_(a));
  464. ElementsAreArrayMatcher<int> matcher_maker =
  465. ElementsAreArray(expect.begin(), expect.end());
  466. EXPECT_THAT(test_vector, matcher_maker);
  467. // Changing in place the values that initialized matcher_maker should not
  468. // affect matcher_maker anymore. It should have made its own copy of them.
  469. typedef vector<int>::iterator Iter;
  470. for (Iter it = expect.begin(); it != expect.end(); ++it) { *it += 10; }
  471. EXPECT_THAT(test_vector, matcher_maker);
  472. test_vector.push_back(3);
  473. EXPECT_THAT(test_vector, Not(matcher_maker));
  474. }
  475. // Tests for the MATCHER*() macro family.
  476. // Tests that a simple MATCHER() definition works.
  477. MATCHER(IsEven, "") { return (arg % 2) == 0; }
  478. TEST(MatcherMacroTest, Works) {
  479. const Matcher<int> m = IsEven();
  480. EXPECT_TRUE(m.Matches(6));
  481. EXPECT_FALSE(m.Matches(7));
  482. EXPECT_EQ("is even", Describe(m));
  483. EXPECT_EQ("not (is even)", DescribeNegation(m));
  484. EXPECT_EQ("", Explain(m, 6));
  485. EXPECT_EQ("", Explain(m, 7));
  486. }
  487. // This also tests that the description string can reference 'negation'.
  488. MATCHER(IsEven2, negation ? "is odd" : "is even") {
  489. if ((arg % 2) == 0) {
  490. // Verifies that we can stream to result_listener, a listener
  491. // supplied by the MATCHER macro implicitly.
  492. *result_listener << "OK";
  493. return true;
  494. } else {
  495. *result_listener << "% 2 == " << (arg % 2);
  496. return false;
  497. }
  498. }
  499. // This also tests that the description string can reference matcher
  500. // parameters.
  501. MATCHER_P2(EqSumOf, x, y, std::string(negation ? "doesn't equal" : "equals") +
  502. " the sum of " + PrintToString(x) + " and " +
  503. PrintToString(y)) {
  504. if (arg == (x + y)) {
  505. *result_listener << "OK";
  506. return true;
  507. } else {
  508. // Verifies that we can stream to the underlying stream of
  509. // result_listener.
  510. if (result_listener->stream() != nullptr) {
  511. *result_listener->stream() << "diff == " << (x + y - arg);
  512. }
  513. return false;
  514. }
  515. }
  516. // Tests that the matcher description can reference 'negation' and the
  517. // matcher parameters.
  518. TEST(MatcherMacroTest, DescriptionCanReferenceNegationAndParameters) {
  519. const Matcher<int> m1 = IsEven2();
  520. EXPECT_EQ("is even", Describe(m1));
  521. EXPECT_EQ("is odd", DescribeNegation(m1));
  522. const Matcher<int> m2 = EqSumOf(5, 9);
  523. EXPECT_EQ("equals the sum of 5 and 9", Describe(m2));
  524. EXPECT_EQ("doesn't equal the sum of 5 and 9", DescribeNegation(m2));
  525. }
  526. // Tests explaining match result in a MATCHER* macro.
  527. TEST(MatcherMacroTest, CanExplainMatchResult) {
  528. const Matcher<int> m1 = IsEven2();
  529. EXPECT_EQ("OK", Explain(m1, 4));
  530. EXPECT_EQ("% 2 == 1", Explain(m1, 5));
  531. const Matcher<int> m2 = EqSumOf(1, 2);
  532. EXPECT_EQ("OK", Explain(m2, 3));
  533. EXPECT_EQ("diff == -1", Explain(m2, 4));
  534. }
  535. // Tests that the body of MATCHER() can reference the type of the
  536. // value being matched.
  537. MATCHER(IsEmptyString, "") {
  538. StaticAssertTypeEq< ::std::string, arg_type>();
  539. return arg == "";
  540. }
  541. MATCHER(IsEmptyStringByRef, "") {
  542. StaticAssertTypeEq<const ::std::string&, arg_type>();
  543. return arg == "";
  544. }
  545. TEST(MatcherMacroTest, CanReferenceArgType) {
  546. const Matcher< ::std::string> m1 = IsEmptyString();
  547. EXPECT_TRUE(m1.Matches(""));
  548. const Matcher<const ::std::string&> m2 = IsEmptyStringByRef();
  549. EXPECT_TRUE(m2.Matches(""));
  550. }
  551. // Tests that MATCHER() can be used in a namespace.
  552. namespace matcher_test {
  553. MATCHER(IsOdd, "") { return (arg % 2) != 0; }
  554. } // namespace matcher_test
  555. TEST(MatcherMacroTest, WorksInNamespace) {
  556. Matcher<int> m = matcher_test::IsOdd();
  557. EXPECT_FALSE(m.Matches(4));
  558. EXPECT_TRUE(m.Matches(5));
  559. }
  560. // Tests that Value() can be used to compose matchers.
  561. MATCHER(IsPositiveOdd, "") {
  562. return Value(arg, matcher_test::IsOdd()) && arg > 0;
  563. }
  564. TEST(MatcherMacroTest, CanBeComposedUsingValue) {
  565. EXPECT_THAT(3, IsPositiveOdd());
  566. EXPECT_THAT(4, Not(IsPositiveOdd()));
  567. EXPECT_THAT(-1, Not(IsPositiveOdd()));
  568. }
  569. // Tests that a simple MATCHER_P() definition works.
  570. MATCHER_P(IsGreaterThan32And, n, "") { return arg > 32 && arg > n; }
  571. TEST(MatcherPMacroTest, Works) {
  572. const Matcher<int> m = IsGreaterThan32And(5);
  573. EXPECT_TRUE(m.Matches(36));
  574. EXPECT_FALSE(m.Matches(5));
  575. EXPECT_EQ("is greater than 32 and 5", Describe(m));
  576. EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m));
  577. EXPECT_EQ("", Explain(m, 36));
  578. EXPECT_EQ("", Explain(m, 5));
  579. }
  580. // Tests that the description is calculated correctly from the matcher name.
  581. MATCHER_P(_is_Greater_Than32and_, n, "") { return arg > 32 && arg > n; }
  582. TEST(MatcherPMacroTest, GeneratesCorrectDescription) {
  583. const Matcher<int> m = _is_Greater_Than32and_(5);
  584. EXPECT_EQ("is greater than 32 and 5", Describe(m));
  585. EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m));
  586. EXPECT_EQ("", Explain(m, 36));
  587. EXPECT_EQ("", Explain(m, 5));
  588. }
  589. // Tests that a MATCHER_P matcher can be explicitly instantiated with
  590. // a reference parameter type.
  591. class UncopyableFoo {
  592. public:
  593. explicit UncopyableFoo(char value) : value_(value) {}
  594. private:
  595. UncopyableFoo(const UncopyableFoo&);
  596. void operator=(const UncopyableFoo&);
  597. char value_;
  598. };
  599. MATCHER_P(ReferencesUncopyable, variable, "") { return &arg == &variable; }
  600. TEST(MatcherPMacroTest, WorksWhenExplicitlyInstantiatedWithReference) {
  601. UncopyableFoo foo1('1'), foo2('2');
  602. const Matcher<const UncopyableFoo&> m =
  603. ReferencesUncopyable<const UncopyableFoo&>(foo1);
  604. EXPECT_TRUE(m.Matches(foo1));
  605. EXPECT_FALSE(m.Matches(foo2));
  606. // We don't want the address of the parameter printed, as most
  607. // likely it will just annoy the user. If the address is
  608. // interesting, the user should consider passing the parameter by
  609. // pointer instead.
  610. EXPECT_EQ("references uncopyable 1-byte object <31>", Describe(m));
  611. }
  612. // Tests that the body of MATCHER_Pn() can reference the parameter
  613. // types.
  614. MATCHER_P3(ParamTypesAreIntLongAndChar, foo, bar, baz, "") {
  615. StaticAssertTypeEq<int, foo_type>();
  616. StaticAssertTypeEq<long, bar_type>(); // NOLINT
  617. StaticAssertTypeEq<char, baz_type>();
  618. return arg == 0;
  619. }
  620. TEST(MatcherPnMacroTest, CanReferenceParamTypes) {
  621. EXPECT_THAT(0, ParamTypesAreIntLongAndChar(10, 20L, 'a'));
  622. }
  623. // Tests that a MATCHER_Pn matcher can be explicitly instantiated with
  624. // reference parameter types.
  625. MATCHER_P2(ReferencesAnyOf, variable1, variable2, "") {
  626. return &arg == &variable1 || &arg == &variable2;
  627. }
  628. TEST(MatcherPnMacroTest, WorksWhenExplicitlyInstantiatedWithReferences) {
  629. UncopyableFoo foo1('1'), foo2('2'), foo3('3');
  630. const Matcher<const UncopyableFoo&> m =
  631. ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
  632. EXPECT_TRUE(m.Matches(foo1));
  633. EXPECT_TRUE(m.Matches(foo2));
  634. EXPECT_FALSE(m.Matches(foo3));
  635. }
  636. TEST(MatcherPnMacroTest,
  637. GeneratesCorretDescriptionWhenExplicitlyInstantiatedWithReferences) {
  638. UncopyableFoo foo1('1'), foo2('2');
  639. const Matcher<const UncopyableFoo&> m =
  640. ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
  641. // We don't want the addresses of the parameters printed, as most
  642. // likely they will just annoy the user. If the addresses are
  643. // interesting, the user should consider passing the parameters by
  644. // pointers instead.
  645. EXPECT_EQ("references any of (1-byte object <31>, 1-byte object <32>)",
  646. Describe(m));
  647. }
  648. // Tests that a simple MATCHER_P2() definition works.
  649. MATCHER_P2(IsNotInClosedRange, low, hi, "") { return arg < low || arg > hi; }
  650. TEST(MatcherPnMacroTest, Works) {
  651. const Matcher<const long&> m = IsNotInClosedRange(10, 20); // NOLINT
  652. EXPECT_TRUE(m.Matches(36L));
  653. EXPECT_FALSE(m.Matches(15L));
  654. EXPECT_EQ("is not in closed range (10, 20)", Describe(m));
  655. EXPECT_EQ("not (is not in closed range (10, 20))", DescribeNegation(m));
  656. EXPECT_EQ("", Explain(m, 36L));
  657. EXPECT_EQ("", Explain(m, 15L));
  658. }
  659. // Tests that MATCHER*() definitions can be overloaded on the number
  660. // of parameters; also tests MATCHER_Pn() where n >= 3.
  661. MATCHER(EqualsSumOf, "") { return arg == 0; }
  662. MATCHER_P(EqualsSumOf, a, "") { return arg == a; }
  663. MATCHER_P2(EqualsSumOf, a, b, "") { return arg == a + b; }
  664. MATCHER_P3(EqualsSumOf, a, b, c, "") { return arg == a + b + c; }
  665. MATCHER_P4(EqualsSumOf, a, b, c, d, "") { return arg == a + b + c + d; }
  666. MATCHER_P5(EqualsSumOf, a, b, c, d, e, "") { return arg == a + b + c + d + e; }
  667. MATCHER_P6(EqualsSumOf, a, b, c, d, e, f, "") {
  668. return arg == a + b + c + d + e + f;
  669. }
  670. MATCHER_P7(EqualsSumOf, a, b, c, d, e, f, g, "") {
  671. return arg == a + b + c + d + e + f + g;
  672. }
  673. MATCHER_P8(EqualsSumOf, a, b, c, d, e, f, g, h, "") {
  674. return arg == a + b + c + d + e + f + g + h;
  675. }
  676. MATCHER_P9(EqualsSumOf, a, b, c, d, e, f, g, h, i, "") {
  677. return arg == a + b + c + d + e + f + g + h + i;
  678. }
  679. MATCHER_P10(EqualsSumOf, a, b, c, d, e, f, g, h, i, j, "") {
  680. return arg == a + b + c + d + e + f + g + h + i + j;
  681. }
  682. TEST(MatcherPnMacroTest, CanBeOverloadedOnNumberOfParameters) {
  683. EXPECT_THAT(0, EqualsSumOf());
  684. EXPECT_THAT(1, EqualsSumOf(1));
  685. EXPECT_THAT(12, EqualsSumOf(10, 2));
  686. EXPECT_THAT(123, EqualsSumOf(100, 20, 3));
  687. EXPECT_THAT(1234, EqualsSumOf(1000, 200, 30, 4));
  688. EXPECT_THAT(12345, EqualsSumOf(10000, 2000, 300, 40, 5));
  689. EXPECT_THAT("abcdef",
  690. EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f'));
  691. EXPECT_THAT("abcdefg",
  692. EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g'));
  693. EXPECT_THAT("abcdefgh",
  694. EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
  695. "h"));
  696. EXPECT_THAT("abcdefghi",
  697. EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
  698. "h", 'i'));
  699. EXPECT_THAT("abcdefghij",
  700. EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
  701. "h", 'i', ::std::string("j")));
  702. EXPECT_THAT(1, Not(EqualsSumOf()));
  703. EXPECT_THAT(-1, Not(EqualsSumOf(1)));
  704. EXPECT_THAT(-12, Not(EqualsSumOf(10, 2)));
  705. EXPECT_THAT(-123, Not(EqualsSumOf(100, 20, 3)));
  706. EXPECT_THAT(-1234, Not(EqualsSumOf(1000, 200, 30, 4)));
  707. EXPECT_THAT(-12345, Not(EqualsSumOf(10000, 2000, 300, 40, 5)));
  708. EXPECT_THAT("abcdef ",
  709. Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f')));
  710. EXPECT_THAT("abcdefg ",
  711. Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f',
  712. 'g')));
  713. EXPECT_THAT("abcdefgh ",
  714. Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
  715. "h")));
  716. EXPECT_THAT("abcdefghi ",
  717. Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
  718. "h", 'i')));
  719. EXPECT_THAT("abcdefghij ",
  720. Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
  721. "h", 'i', ::std::string("j"))));
  722. }
  723. // Tests that a MATCHER_Pn() definition can be instantiated with any
  724. // compatible parameter types.
  725. TEST(MatcherPnMacroTest, WorksForDifferentParameterTypes) {
  726. EXPECT_THAT(123, EqualsSumOf(100L, 20, static_cast<char>(3)));
  727. EXPECT_THAT("abcd", EqualsSumOf(::std::string("a"), "b", 'c', "d"));
  728. EXPECT_THAT(124, Not(EqualsSumOf(100L, 20, static_cast<char>(3))));
  729. EXPECT_THAT("abcde", Not(EqualsSumOf(::std::string("a"), "b", 'c', "d")));
  730. }
  731. // Tests that the matcher body can promote the parameter types.
  732. MATCHER_P2(EqConcat, prefix, suffix, "") {
  733. // The following lines promote the two parameters to desired types.
  734. std::string prefix_str(prefix);
  735. char suffix_char = static_cast<char>(suffix);
  736. return arg == prefix_str + suffix_char;
  737. }
  738. TEST(MatcherPnMacroTest, SimpleTypePromotion) {
  739. Matcher<std::string> no_promo =
  740. EqConcat(std::string("foo"), 't');
  741. Matcher<const std::string&> promo =
  742. EqConcat("foo", static_cast<int>('t'));
  743. EXPECT_FALSE(no_promo.Matches("fool"));
  744. EXPECT_FALSE(promo.Matches("fool"));
  745. EXPECT_TRUE(no_promo.Matches("foot"));
  746. EXPECT_TRUE(promo.Matches("foot"));
  747. }
  748. // Verifies the type of a MATCHER*.
  749. TEST(MatcherPnMacroTest, TypesAreCorrect) {
  750. // EqualsSumOf() must be assignable to a EqualsSumOfMatcher variable.
  751. EqualsSumOfMatcher a0 = EqualsSumOf();
  752. // EqualsSumOf(1) must be assignable to a EqualsSumOfMatcherP variable.
  753. EqualsSumOfMatcherP<int> a1 = EqualsSumOf(1);
  754. // EqualsSumOf(p1, ..., pk) must be assignable to a EqualsSumOfMatcherPk
  755. // variable, and so on.
  756. EqualsSumOfMatcherP2<int, char> a2 = EqualsSumOf(1, '2');
  757. EqualsSumOfMatcherP3<int, int, char> a3 = EqualsSumOf(1, 2, '3');
  758. EqualsSumOfMatcherP4<int, int, int, char> a4 = EqualsSumOf(1, 2, 3, '4');
  759. EqualsSumOfMatcherP5<int, int, int, int, char> a5 =
  760. EqualsSumOf(1, 2, 3, 4, '5');
  761. EqualsSumOfMatcherP6<int, int, int, int, int, char> a6 =
  762. EqualsSumOf(1, 2, 3, 4, 5, '6');
  763. EqualsSumOfMatcherP7<int, int, int, int, int, int, char> a7 =
  764. EqualsSumOf(1, 2, 3, 4, 5, 6, '7');
  765. EqualsSumOfMatcherP8<int, int, int, int, int, int, int, char> a8 =
  766. EqualsSumOf(1, 2, 3, 4, 5, 6, 7, '8');
  767. EqualsSumOfMatcherP9<int, int, int, int, int, int, int, int, char> a9 =
  768. EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, '9');
  769. EqualsSumOfMatcherP10<int, int, int, int, int, int, int, int, int, char> a10 =
  770. EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, 9, '0');
  771. // Avoid "unused variable" warnings.
  772. (void)a0;
  773. (void)a1;
  774. (void)a2;
  775. (void)a3;
  776. (void)a4;
  777. (void)a5;
  778. (void)a6;
  779. (void)a7;
  780. (void)a8;
  781. (void)a9;
  782. (void)a10;
  783. }
  784. // Tests that matcher-typed parameters can be used in Value() inside a
  785. // MATCHER_Pn definition.
  786. // Succeeds if arg matches exactly 2 of the 3 matchers.
  787. MATCHER_P3(TwoOf, m1, m2, m3, "") {
  788. const int count = static_cast<int>(Value(arg, m1))
  789. + static_cast<int>(Value(arg, m2)) + static_cast<int>(Value(arg, m3));
  790. return count == 2;
  791. }
  792. TEST(MatcherPnMacroTest, CanUseMatcherTypedParameterInValue) {
  793. EXPECT_THAT(42, TwoOf(Gt(0), Lt(50), Eq(10)));
  794. EXPECT_THAT(0, Not(TwoOf(Gt(-1), Lt(1), Eq(0))));
  795. }
  796. // Tests Contains().
  797. TEST(ContainsTest, ListMatchesWhenElementIsInContainer) {
  798. list<int> some_list;
  799. some_list.push_back(3);
  800. some_list.push_back(1);
  801. some_list.push_back(2);
  802. EXPECT_THAT(some_list, Contains(1));
  803. EXPECT_THAT(some_list, Contains(Gt(2.5)));
  804. EXPECT_THAT(some_list, Contains(Eq(2.0f)));
  805. list<std::string> another_list;
  806. another_list.push_back("fee");
  807. another_list.push_back("fie");
  808. another_list.push_back("foe");
  809. another_list.push_back("fum");
  810. EXPECT_THAT(another_list, Contains(std::string("fee")));
  811. }
  812. TEST(ContainsTest, ListDoesNotMatchWhenElementIsNotInContainer) {
  813. list<int> some_list;
  814. some_list.push_back(3);
  815. some_list.push_back(1);
  816. EXPECT_THAT(some_list, Not(Contains(4)));
  817. }
  818. TEST(ContainsTest, SetMatchesWhenElementIsInContainer) {
  819. set<int> some_set;
  820. some_set.insert(3);
  821. some_set.insert(1);
  822. some_set.insert(2);
  823. EXPECT_THAT(some_set, Contains(Eq(1.0)));
  824. EXPECT_THAT(some_set, Contains(Eq(3.0f)));
  825. EXPECT_THAT(some_set, Contains(2));
  826. set<const char*> another_set;
  827. another_set.insert("fee");
  828. another_set.insert("fie");
  829. another_set.insert("foe");
  830. another_set.insert("fum");
  831. EXPECT_THAT(another_set, Contains(Eq(std::string("fum"))));
  832. }
  833. TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) {
  834. set<int> some_set;
  835. some_set.insert(3);
  836. some_set.insert(1);
  837. EXPECT_THAT(some_set, Not(Contains(4)));
  838. set<const char*> c_string_set;
  839. c_string_set.insert("hello");
  840. EXPECT_THAT(c_string_set, Not(Contains(std::string("hello").c_str())));
  841. }
  842. TEST(ContainsTest, ExplainsMatchResultCorrectly) {
  843. const int a[2] = { 1, 2 };
  844. Matcher<const int (&)[2]> m = Contains(2);
  845. EXPECT_EQ("whose element #1 matches", Explain(m, a));
  846. m = Contains(3);
  847. EXPECT_EQ("", Explain(m, a));
  848. m = Contains(GreaterThan(0));
  849. EXPECT_EQ("whose element #0 matches, which is 1 more than 0", Explain(m, a));
  850. m = Contains(GreaterThan(10));
  851. EXPECT_EQ("", Explain(m, a));
  852. }
  853. TEST(ContainsTest, DescribesItselfCorrectly) {
  854. Matcher<vector<int> > m = Contains(1);
  855. EXPECT_EQ("contains at least one element that is equal to 1", Describe(m));
  856. Matcher<vector<int> > m2 = Not(m);
  857. EXPECT_EQ("doesn't contain any element that is equal to 1", Describe(m2));
  858. }
  859. TEST(ContainsTest, MapMatchesWhenElementIsInContainer) {
  860. map<const char*, int> my_map;
  861. const char* bar = "a string";
  862. my_map[bar] = 2;
  863. EXPECT_THAT(my_map, Contains(pair<const char* const, int>(bar, 2)));
  864. map<std::string, int> another_map;
  865. another_map["fee"] = 1;
  866. another_map["fie"] = 2;
  867. another_map["foe"] = 3;
  868. another_map["fum"] = 4;
  869. EXPECT_THAT(another_map,
  870. Contains(pair<const std::string, int>(std::string("fee"), 1)));
  871. EXPECT_THAT(another_map, Contains(pair<const std::string, int>("fie", 2)));
  872. }
  873. TEST(ContainsTest, MapDoesNotMatchWhenElementIsNotInContainer) {
  874. map<int, int> some_map;
  875. some_map[1] = 11;
  876. some_map[2] = 22;
  877. EXPECT_THAT(some_map, Not(Contains(pair<const int, int>(2, 23))));
  878. }
  879. TEST(ContainsTest, ArrayMatchesWhenElementIsInContainer) {
  880. const char* string_array[] = { "fee", "fie", "foe", "fum" };
  881. EXPECT_THAT(string_array, Contains(Eq(std::string("fum"))));
  882. }
  883. TEST(ContainsTest, ArrayDoesNotMatchWhenElementIsNotInContainer) {
  884. int int_array[] = { 1, 2, 3, 4 };
  885. EXPECT_THAT(int_array, Not(Contains(5)));
  886. }
  887. TEST(ContainsTest, AcceptsMatcher) {
  888. const int a[] = { 1, 2, 3 };
  889. EXPECT_THAT(a, Contains(Gt(2)));
  890. EXPECT_THAT(a, Not(Contains(Gt(4))));
  891. }
  892. TEST(ContainsTest, WorksForNativeArrayAsTuple) {
  893. const int a[] = { 1, 2 };
  894. const int* const pointer = a;
  895. EXPECT_THAT(std::make_tuple(pointer, 2), Contains(1));
  896. EXPECT_THAT(std::make_tuple(pointer, 2), Not(Contains(Gt(3))));
  897. }
  898. TEST(ContainsTest, WorksForTwoDimensionalNativeArray) {
  899. int a[][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
  900. EXPECT_THAT(a, Contains(ElementsAre(4, 5, 6)));
  901. EXPECT_THAT(a, Contains(Contains(5)));
  902. EXPECT_THAT(a, Not(Contains(ElementsAre(3, 4, 5))));
  903. EXPECT_THAT(a, Contains(Not(Contains(5))));
  904. }
  905. TEST(AllOfArrayTest, BasicForms) {
  906. // Iterator
  907. std::vector<int> v0{};
  908. std::vector<int> v1{1};
  909. std::vector<int> v2{2, 3};
  910. std::vector<int> v3{4, 4, 4};
  911. EXPECT_THAT(0, AllOfArray(v0.begin(), v0.end()));
  912. EXPECT_THAT(1, AllOfArray(v1.begin(), v1.end()));
  913. EXPECT_THAT(2, Not(AllOfArray(v1.begin(), v1.end())));
  914. EXPECT_THAT(3, Not(AllOfArray(v2.begin(), v2.end())));
  915. EXPECT_THAT(4, AllOfArray(v3.begin(), v3.end()));
  916. // Pointer + size
  917. int ar[6] = {1, 2, 3, 4, 4, 4};
  918. EXPECT_THAT(0, AllOfArray(ar, 0));
  919. EXPECT_THAT(1, AllOfArray(ar, 1));
  920. EXPECT_THAT(2, Not(AllOfArray(ar, 1)));
  921. EXPECT_THAT(3, Not(AllOfArray(ar + 1, 3)));
  922. EXPECT_THAT(4, AllOfArray(ar + 3, 3));
  923. // Array
  924. // int ar0[0]; Not usable
  925. int ar1[1] = {1};
  926. int ar2[2] = {2, 3};
  927. int ar3[3] = {4, 4, 4};
  928. // EXPECT_THAT(0, Not(AllOfArray(ar0))); // Cannot work
  929. EXPECT_THAT(1, AllOfArray(ar1));
  930. EXPECT_THAT(2, Not(AllOfArray(ar1)));
  931. EXPECT_THAT(3, Not(AllOfArray(ar2)));
  932. EXPECT_THAT(4, AllOfArray(ar3));
  933. // Container
  934. EXPECT_THAT(0, AllOfArray(v0));
  935. EXPECT_THAT(1, AllOfArray(v1));
  936. EXPECT_THAT(2, Not(AllOfArray(v1)));
  937. EXPECT_THAT(3, Not(AllOfArray(v2)));
  938. EXPECT_THAT(4, AllOfArray(v3));
  939. // Initializer
  940. EXPECT_THAT(0, AllOfArray<int>({})); // Requires template arg.
  941. EXPECT_THAT(1, AllOfArray({1}));
  942. EXPECT_THAT(2, Not(AllOfArray({1})));
  943. EXPECT_THAT(3, Not(AllOfArray({2, 3})));
  944. EXPECT_THAT(4, AllOfArray({4, 4, 4}));
  945. }
  946. TEST(AllOfArrayTest, Matchers) {
  947. // vector
  948. std::vector<Matcher<int>> matchers{Ge(1), Lt(2)};
  949. EXPECT_THAT(0, Not(AllOfArray(matchers)));
  950. EXPECT_THAT(1, AllOfArray(matchers));
  951. EXPECT_THAT(2, Not(AllOfArray(matchers)));
  952. // initializer_list
  953. EXPECT_THAT(0, Not(AllOfArray({Ge(0), Ge(1)})));
  954. EXPECT_THAT(1, AllOfArray({Ge(0), Ge(1)}));
  955. }
  956. TEST(AnyOfArrayTest, BasicForms) {
  957. // Iterator
  958. std::vector<int> v0{};
  959. std::vector<int> v1{1};
  960. std::vector<int> v2{2, 3};
  961. EXPECT_THAT(0, Not(AnyOfArray(v0.begin(), v0.end())));
  962. EXPECT_THAT(1, AnyOfArray(v1.begin(), v1.end()));
  963. EXPECT_THAT(2, Not(AnyOfArray(v1.begin(), v1.end())));
  964. EXPECT_THAT(3, AnyOfArray(v2.begin(), v2.end()));
  965. EXPECT_THAT(4, Not(AnyOfArray(v2.begin(), v2.end())));
  966. // Pointer + size
  967. int ar[3] = {1, 2, 3};
  968. EXPECT_THAT(0, Not(AnyOfArray(ar, 0)));
  969. EXPECT_THAT(1, AnyOfArray(ar, 1));
  970. EXPECT_THAT(2, Not(AnyOfArray(ar, 1)));
  971. EXPECT_THAT(3, AnyOfArray(ar + 1, 2));
  972. EXPECT_THAT(4, Not(AnyOfArray(ar + 1, 2)));
  973. // Array
  974. // int ar0[0]; Not usable
  975. int ar1[1] = {1};
  976. int ar2[2] = {2, 3};
  977. // EXPECT_THAT(0, Not(AnyOfArray(ar0))); // Cannot work
  978. EXPECT_THAT(1, AnyOfArray(ar1));
  979. EXPECT_THAT(2, Not(AnyOfArray(ar1)));
  980. EXPECT_THAT(3, AnyOfArray(ar2));
  981. EXPECT_THAT(4, Not(AnyOfArray(ar2)));
  982. // Container
  983. EXPECT_THAT(0, Not(AnyOfArray(v0)));
  984. EXPECT_THAT(1, AnyOfArray(v1));
  985. EXPECT_THAT(2, Not(AnyOfArray(v1)));
  986. EXPECT_THAT(3, AnyOfArray(v2));
  987. EXPECT_THAT(4, Not(AnyOfArray(v2)));
  988. // Initializer
  989. EXPECT_THAT(0, Not(AnyOfArray<int>({}))); // Requires template arg.
  990. EXPECT_THAT(1, AnyOfArray({1}));
  991. EXPECT_THAT(2, Not(AnyOfArray({1})));
  992. EXPECT_THAT(3, AnyOfArray({2, 3}));
  993. EXPECT_THAT(4, Not(AnyOfArray({2, 3})));
  994. }
  995. TEST(AnyOfArrayTest, Matchers) {
  996. // We negate test AllOfArrayTest.Matchers.
  997. // vector
  998. std::vector<Matcher<int>> matchers{Lt(1), Ge(2)};
  999. EXPECT_THAT(0, AnyOfArray(matchers));
  1000. EXPECT_THAT(1, Not(AnyOfArray(matchers)));
  1001. EXPECT_THAT(2, AnyOfArray(matchers));
  1002. // initializer_list
  1003. EXPECT_THAT(0, AnyOfArray({Lt(0), Lt(1)}));
  1004. EXPECT_THAT(1, Not(AllOfArray({Lt(0), Lt(1)})));
  1005. }
  1006. TEST(AnyOfArrayTest, ExplainsMatchResultCorrectly) {
  1007. // AnyOfArray and AllOfArry use the same underlying template-template,
  1008. // thus it is sufficient to test one here.
  1009. const std::vector<int> v0{};
  1010. const std::vector<int> v1{1};
  1011. const std::vector<int> v2{2, 3};
  1012. const Matcher<int> m0 = AnyOfArray(v0);
  1013. const Matcher<int> m1 = AnyOfArray(v1);
  1014. const Matcher<int> m2 = AnyOfArray(v2);
  1015. EXPECT_EQ("", Explain(m0, 0));
  1016. EXPECT_EQ("", Explain(m1, 1));
  1017. EXPECT_EQ("", Explain(m1, 2));
  1018. EXPECT_EQ("", Explain(m2, 3));
  1019. EXPECT_EQ("", Explain(m2, 4));
  1020. EXPECT_EQ("()", Describe(m0));
  1021. EXPECT_EQ("(is equal to 1)", Describe(m1));
  1022. EXPECT_EQ("(is equal to 2) or (is equal to 3)", Describe(m2));
  1023. EXPECT_EQ("()", DescribeNegation(m0));
  1024. EXPECT_EQ("(isn't equal to 1)", DescribeNegation(m1));
  1025. EXPECT_EQ("(isn't equal to 2) and (isn't equal to 3)", DescribeNegation(m2));
  1026. // Explain with matchers
  1027. const Matcher<int> g1 = AnyOfArray({GreaterThan(1)});
  1028. const Matcher<int> g2 = AnyOfArray({GreaterThan(1), GreaterThan(2)});
  1029. // Explains the first positiv match and all prior negative matches...
  1030. EXPECT_EQ("which is 1 less than 1", Explain(g1, 0));
  1031. EXPECT_EQ("which is the same as 1", Explain(g1, 1));
  1032. EXPECT_EQ("which is 1 more than 1", Explain(g1, 2));
  1033. EXPECT_EQ("which is 1 less than 1, and which is 2 less than 2",
  1034. Explain(g2, 0));
  1035. EXPECT_EQ("which is the same as 1, and which is 1 less than 2",
  1036. Explain(g2, 1));
  1037. EXPECT_EQ("which is 1 more than 1", // Only the first
  1038. Explain(g2, 2));
  1039. }
  1040. TEST(AllOfTest, HugeMatcher) {
  1041. // Verify that using AllOf with many arguments doesn't cause
  1042. // the compiler to exceed template instantiation depth limit.
  1043. EXPECT_THAT(0, testing::AllOf(_, _, _, _, _, _, _, _, _,
  1044. testing::AllOf(_, _, _, _, _, _, _, _, _, _)));
  1045. }
  1046. TEST(AnyOfTest, HugeMatcher) {
  1047. // Verify that using AnyOf with many arguments doesn't cause
  1048. // the compiler to exceed template instantiation depth limit.
  1049. EXPECT_THAT(0, testing::AnyOf(_, _, _, _, _, _, _, _, _,
  1050. testing::AnyOf(_, _, _, _, _, _, _, _, _, _)));
  1051. }
  1052. namespace adl_test {
  1053. // Verifies that the implementation of ::testing::AllOf and ::testing::AnyOf
  1054. // don't issue unqualified recursive calls. If they do, the argument dependent
  1055. // name lookup will cause AllOf/AnyOf in the 'adl_test' namespace to be found
  1056. // as a candidate and the compilation will break due to an ambiguous overload.
  1057. // The matcher must be in the same namespace as AllOf/AnyOf to make argument
  1058. // dependent lookup find those.
  1059. MATCHER(M, "") { return true; }
  1060. template <typename T1, typename T2>
  1061. bool AllOf(const T1& /*t1*/, const T2& /*t2*/) { return true; }
  1062. TEST(AllOfTest, DoesNotCallAllOfUnqualified) {
  1063. EXPECT_THAT(42, testing::AllOf(
  1064. M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
  1065. }
  1066. template <typename T1, typename T2> bool
  1067. AnyOf(const T1& t1, const T2& t2) { return true; }
  1068. TEST(AnyOfTest, DoesNotCallAnyOfUnqualified) {
  1069. EXPECT_THAT(42, testing::AnyOf(
  1070. M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
  1071. }
  1072. } // namespace adl_test
  1073. TEST(AllOfTest, WorksOnMoveOnlyType) {
  1074. std::unique_ptr<int> p(new int(3));
  1075. EXPECT_THAT(p, AllOf(Pointee(Eq(3)), Pointee(Gt(0)), Pointee(Lt(5))));
  1076. EXPECT_THAT(p, Not(AllOf(Pointee(Eq(3)), Pointee(Gt(0)), Pointee(Lt(3)))));
  1077. }
  1078. TEST(AnyOfTest, WorksOnMoveOnlyType) {
  1079. std::unique_ptr<int> p(new int(3));
  1080. EXPECT_THAT(p, AnyOf(Pointee(Eq(5)), Pointee(Lt(0)), Pointee(Lt(5))));
  1081. EXPECT_THAT(p, Not(AnyOf(Pointee(Eq(5)), Pointee(Lt(0)), Pointee(Gt(5)))));
  1082. }
  1083. MATCHER(IsNotNull, "") {
  1084. return arg != nullptr;
  1085. }
  1086. // Verifies that a matcher defined using MATCHER() can work on
  1087. // move-only types.
  1088. TEST(MatcherMacroTest, WorksOnMoveOnlyType) {
  1089. std::unique_ptr<int> p(new int(3));
  1090. EXPECT_THAT(p, IsNotNull());
  1091. EXPECT_THAT(std::unique_ptr<int>(), Not(IsNotNull()));
  1092. }
  1093. MATCHER_P(UniquePointee, pointee, "") {
  1094. return *arg == pointee;
  1095. }
  1096. // Verifies that a matcher defined using MATCHER_P*() can work on
  1097. // move-only types.
  1098. TEST(MatcherPMacroTest, WorksOnMoveOnlyType) {
  1099. std::unique_ptr<int> p(new int(3));
  1100. EXPECT_THAT(p, UniquePointee(3));
  1101. EXPECT_THAT(p, Not(UniquePointee(2)));
  1102. }
  1103. } // namespace
  1104. #ifdef _MSC_VER
  1105. # pragma warning(pop)
  1106. #endif