gmock-generated-matchers_test.cc 43 KB

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