gmock_link_test.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. // Copyright 2009, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. //
  30. // Author: vladl@google.com (Vlad Losev)
  31. // Google Mock - a framework for writing C++ mock classes.
  32. //
  33. // This file tests that:
  34. // a. A header file defining a mock class can be included in multiple
  35. // translation units without causing a link error.
  36. // b. Actions and matchers can be instantiated with identical template
  37. // arguments in different translation units without causing link
  38. // errors.
  39. // The following constructs are currently tested:
  40. // Actions:
  41. // Return()
  42. // Return(value)
  43. // ReturnNull
  44. // ReturnRef
  45. // Assign
  46. // SetArgPointee
  47. // SetArrayArgument
  48. // SetErrnoAndReturn
  49. // Invoke(function)
  50. // Invoke(object, method)
  51. // InvokeWithoutArgs(function)
  52. // InvokeWithoutArgs(object, method)
  53. // InvokeArgument
  54. // WithArg
  55. // WithArgs
  56. // WithoutArgs
  57. // DoAll
  58. // DoDefault
  59. // IgnoreResult
  60. // Throw
  61. // ACTION()-generated
  62. // ACTION_P()-generated
  63. // ACTION_P2()-generated
  64. // Matchers:
  65. // _
  66. // A
  67. // An
  68. // Eq
  69. // Gt, Lt, Ge, Le, Ne
  70. // NotNull
  71. // Ref
  72. // TypedEq
  73. // DoubleEq
  74. // FloatEq
  75. // NanSensitiveDoubleEq
  76. // NanSensitiveFloatEq
  77. // ContainsRegex
  78. // MatchesRegex
  79. // EndsWith
  80. // HasSubstr
  81. // StartsWith
  82. // StrCaseEq
  83. // StrCaseNe
  84. // StrEq
  85. // StrNe
  86. // ElementsAre
  87. // ElementsAreArray
  88. // ContainerEq
  89. // Field
  90. // Property
  91. // ResultOf(function)
  92. // Pointee
  93. // Truly(predicate)
  94. // AllOf
  95. // AnyOf
  96. // Not
  97. // MatcherCast<T>
  98. //
  99. // Please note: this test does not verify the functioning of these
  100. // constructs, only that the programs using them will link successfully.
  101. //
  102. // Implementation note:
  103. // This test requires identical definitions of Interface and Mock to be
  104. // included in different translation units. We achieve this by writing
  105. // them in this header and #including it in gmock_link_test.cc and
  106. // gmock_link2_test.cc. Because the symbols generated by the compiler for
  107. // those constructs must be identical in both translation units,
  108. // definitions of Interface and Mock tests MUST be kept in the SAME
  109. // NON-ANONYMOUS namespace in this file. The test fixture class LinkTest
  110. // is defined as LinkTest1 in gmock_link_test.cc and as LinkTest2 in
  111. // gmock_link2_test.cc to avoid producing linker errors.
  112. #ifndef GMOCK_TEST_GMOCK_LINK_TEST_H_
  113. #define GMOCK_TEST_GMOCK_LINK_TEST_H_
  114. #include "gmock/gmock.h"
  115. #if !GTEST_OS_WINDOWS_MOBILE
  116. # include <errno.h>
  117. #endif
  118. #include "gmock/internal/gmock-port.h"
  119. #include "gtest/gtest.h"
  120. #include <iostream>
  121. #include <vector>
  122. using testing::_;
  123. using testing::A;
  124. using testing::AllOf;
  125. using testing::AnyOf;
  126. using testing::Assign;
  127. using testing::ContainerEq;
  128. using testing::DoAll;
  129. using testing::DoDefault;
  130. using testing::DoubleEq;
  131. using testing::ElementsAre;
  132. using testing::ElementsAreArray;
  133. using testing::EndsWith;
  134. using testing::Eq;
  135. using testing::Field;
  136. using testing::FloatEq;
  137. using testing::Ge;
  138. using testing::Gt;
  139. using testing::HasSubstr;
  140. using testing::IgnoreResult;
  141. using testing::Invoke;
  142. using testing::InvokeArgument;
  143. using testing::InvokeWithoutArgs;
  144. using testing::IsNull;
  145. using testing::Le;
  146. using testing::Lt;
  147. using testing::Matcher;
  148. using testing::MatcherCast;
  149. using testing::NanSensitiveDoubleEq;
  150. using testing::NanSensitiveFloatEq;
  151. using testing::Ne;
  152. using testing::Not;
  153. using testing::NotNull;
  154. using testing::Pointee;
  155. using testing::Property;
  156. using testing::Ref;
  157. using testing::ResultOf;
  158. using testing::Return;
  159. using testing::ReturnNull;
  160. using testing::ReturnRef;
  161. using testing::SetArgPointee;
  162. using testing::SetArrayArgument;
  163. using testing::StartsWith;
  164. using testing::StrCaseEq;
  165. using testing::StrCaseNe;
  166. using testing::StrEq;
  167. using testing::StrNe;
  168. using testing::Truly;
  169. using testing::TypedEq;
  170. using testing::WithArg;
  171. using testing::WithArgs;
  172. using testing::WithoutArgs;
  173. #if !GTEST_OS_WINDOWS_MOBILE
  174. using testing::SetErrnoAndReturn;
  175. #endif
  176. #if GTEST_HAS_EXCEPTIONS
  177. using testing::Throw;
  178. #endif
  179. using testing::ContainsRegex;
  180. using testing::MatchesRegex;
  181. class Interface {
  182. public:
  183. virtual ~Interface() {}
  184. virtual void VoidFromString(char* str) = 0;
  185. virtual char* StringFromString(char* str) = 0;
  186. virtual int IntFromString(char* str) = 0;
  187. virtual int& IntRefFromString(char* str) = 0;
  188. virtual void VoidFromFunc(void(*func)(char* str)) = 0;
  189. virtual void VoidFromIntRef(int& n) = 0; // NOLINT
  190. virtual void VoidFromFloat(float n) = 0;
  191. virtual void VoidFromDouble(double n) = 0;
  192. virtual void VoidFromVector(const std::vector<int>& v) = 0;
  193. };
  194. class Mock: public Interface {
  195. public:
  196. Mock() {}
  197. MOCK_METHOD1(VoidFromString, void(char* str));
  198. MOCK_METHOD1(StringFromString, char*(char* str));
  199. MOCK_METHOD1(IntFromString, int(char* str));
  200. MOCK_METHOD1(IntRefFromString, int&(char* str));
  201. MOCK_METHOD1(VoidFromFunc, void(void(*func)(char* str)));
  202. MOCK_METHOD1(VoidFromIntRef, void(int& n)); // NOLINT
  203. MOCK_METHOD1(VoidFromFloat, void(float n));
  204. MOCK_METHOD1(VoidFromDouble, void(double n));
  205. MOCK_METHOD1(VoidFromVector, void(const std::vector<int>& v));
  206. private:
  207. GTEST_DISALLOW_COPY_AND_ASSIGN_(Mock);
  208. };
  209. class InvokeHelper {
  210. public:
  211. static void StaticVoidFromVoid() {}
  212. void VoidFromVoid() {}
  213. static void StaticVoidFromString(char* /* str */) {}
  214. void VoidFromString(char* /* str */) {}
  215. static int StaticIntFromString(char* /* str */) { return 1; }
  216. static bool StaticBoolFromString(const char* /* str */) { return true; }
  217. };
  218. class FieldHelper {
  219. public:
  220. explicit FieldHelper(int a_field) : field_(a_field) {}
  221. int field() const { return field_; }
  222. int field_; // NOLINT -- need external access to field_ to test
  223. // the Field matcher.
  224. };
  225. // Tests the linkage of the ReturnVoid action.
  226. TEST(LinkTest, TestReturnVoid) {
  227. Mock mock;
  228. EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Return());
  229. mock.VoidFromString(NULL);
  230. }
  231. // Tests the linkage of the Return action.
  232. TEST(LinkTest, TestReturn) {
  233. Mock mock;
  234. char ch = 'x';
  235. EXPECT_CALL(mock, StringFromString(_)).WillOnce(Return(&ch));
  236. mock.StringFromString(NULL);
  237. }
  238. // Tests the linkage of the ReturnNull action.
  239. TEST(LinkTest, TestReturnNull) {
  240. Mock mock;
  241. EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Return());
  242. mock.VoidFromString(NULL);
  243. }
  244. // Tests the linkage of the ReturnRef action.
  245. TEST(LinkTest, TestReturnRef) {
  246. Mock mock;
  247. int n = 42;
  248. EXPECT_CALL(mock, IntRefFromString(_)).WillOnce(ReturnRef(n));
  249. mock.IntRefFromString(NULL);
  250. }
  251. // Tests the linkage of the Assign action.
  252. TEST(LinkTest, TestAssign) {
  253. Mock mock;
  254. char ch = 'x';
  255. EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Assign(&ch, 'y'));
  256. mock.VoidFromString(NULL);
  257. }
  258. // Tests the linkage of the SetArgPointee action.
  259. TEST(LinkTest, TestSetArgPointee) {
  260. Mock mock;
  261. char ch = 'x';
  262. EXPECT_CALL(mock, VoidFromString(_)).WillOnce(SetArgPointee<0>('y'));
  263. mock.VoidFromString(&ch);
  264. }
  265. // Tests the linkage of the SetArrayArgument action.
  266. TEST(LinkTest, TestSetArrayArgument) {
  267. Mock mock;
  268. char ch = 'x';
  269. char ch2 = 'y';
  270. EXPECT_CALL(mock, VoidFromString(_)).WillOnce(SetArrayArgument<0>(&ch2,
  271. &ch2 + 1));
  272. mock.VoidFromString(&ch);
  273. }
  274. #if !GTEST_OS_WINDOWS_MOBILE
  275. // Tests the linkage of the SetErrnoAndReturn action.
  276. TEST(LinkTest, TestSetErrnoAndReturn) {
  277. Mock mock;
  278. int saved_errno = errno;
  279. EXPECT_CALL(mock, IntFromString(_)).WillOnce(SetErrnoAndReturn(1, -1));
  280. mock.IntFromString(NULL);
  281. errno = saved_errno;
  282. }
  283. #endif // !GTEST_OS_WINDOWS_MOBILE
  284. // Tests the linkage of the Invoke(function) and Invoke(object, method) actions.
  285. TEST(LinkTest, TestInvoke) {
  286. Mock mock;
  287. InvokeHelper test_invoke_helper;
  288. EXPECT_CALL(mock, VoidFromString(_))
  289. .WillOnce(Invoke(&InvokeHelper::StaticVoidFromString))
  290. .WillOnce(Invoke(&test_invoke_helper, &InvokeHelper::VoidFromString));
  291. mock.VoidFromString(NULL);
  292. mock.VoidFromString(NULL);
  293. }
  294. // Tests the linkage of the InvokeWithoutArgs action.
  295. TEST(LinkTest, TestInvokeWithoutArgs) {
  296. Mock mock;
  297. InvokeHelper test_invoke_helper;
  298. EXPECT_CALL(mock, VoidFromString(_))
  299. .WillOnce(InvokeWithoutArgs(&InvokeHelper::StaticVoidFromVoid))
  300. .WillOnce(InvokeWithoutArgs(&test_invoke_helper,
  301. &InvokeHelper::VoidFromVoid));
  302. mock.VoidFromString(NULL);
  303. mock.VoidFromString(NULL);
  304. }
  305. // Tests the linkage of the InvokeArgument action.
  306. TEST(LinkTest, TestInvokeArgument) {
  307. Mock mock;
  308. char ch = 'x';
  309. EXPECT_CALL(mock, VoidFromFunc(_)).WillOnce(InvokeArgument<0>(&ch));
  310. mock.VoidFromFunc(InvokeHelper::StaticVoidFromString);
  311. }
  312. // Tests the linkage of the WithArg action.
  313. TEST(LinkTest, TestWithArg) {
  314. Mock mock;
  315. EXPECT_CALL(mock, VoidFromString(_))
  316. .WillOnce(WithArg<0>(Invoke(&InvokeHelper::StaticVoidFromString)));
  317. mock.VoidFromString(NULL);
  318. }
  319. // Tests the linkage of the WithArgs action.
  320. TEST(LinkTest, TestWithArgs) {
  321. Mock mock;
  322. EXPECT_CALL(mock, VoidFromString(_))
  323. .WillOnce(WithArgs<0>(Invoke(&InvokeHelper::StaticVoidFromString)));
  324. mock.VoidFromString(NULL);
  325. }
  326. // Tests the linkage of the WithoutArgs action.
  327. TEST(LinkTest, TestWithoutArgs) {
  328. Mock mock;
  329. EXPECT_CALL(mock, VoidFromString(_)).WillOnce(WithoutArgs(Return()));
  330. mock.VoidFromString(NULL);
  331. }
  332. // Tests the linkage of the DoAll action.
  333. TEST(LinkTest, TestDoAll) {
  334. Mock mock;
  335. char ch = 'x';
  336. EXPECT_CALL(mock, VoidFromString(_))
  337. .WillOnce(DoAll(SetArgPointee<0>('y'), Return()));
  338. mock.VoidFromString(&ch);
  339. }
  340. // Tests the linkage of the DoDefault action.
  341. TEST(LinkTest, TestDoDefault) {
  342. Mock mock;
  343. char ch = 'x';
  344. ON_CALL(mock, VoidFromString(_)).WillByDefault(Return());
  345. EXPECT_CALL(mock, VoidFromString(_)).WillOnce(DoDefault());
  346. mock.VoidFromString(&ch);
  347. }
  348. // Tests the linkage of the IgnoreResult action.
  349. TEST(LinkTest, TestIgnoreResult) {
  350. Mock mock;
  351. EXPECT_CALL(mock, VoidFromString(_)).WillOnce(IgnoreResult(Return(42)));
  352. mock.VoidFromString(NULL);
  353. }
  354. #if GTEST_HAS_EXCEPTIONS
  355. // Tests the linkage of the Throw action.
  356. TEST(LinkTest, TestThrow) {
  357. Mock mock;
  358. EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Throw(42));
  359. EXPECT_THROW(mock.VoidFromString(NULL), int);
  360. }
  361. #endif // GTEST_HAS_EXCEPTIONS
  362. // The ACTION*() macros trigger warning C4100 (unreferenced formal
  363. // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
  364. // the macro definition, as the warnings are generated when the macro
  365. // is expanded and macro expansion cannot contain #pragma. Therefore
  366. // we suppress them here.
  367. #ifdef _MSC_VER
  368. # pragma warning(push)
  369. # pragma warning(disable:4100)
  370. #endif
  371. // Tests the linkage of actions created using ACTION macro.
  372. namespace {
  373. ACTION(Return1) { return 1; }
  374. }
  375. TEST(LinkTest, TestActionMacro) {
  376. Mock mock;
  377. EXPECT_CALL(mock, IntFromString(_)).WillOnce(Return1());
  378. mock.IntFromString(NULL);
  379. }
  380. // Tests the linkage of actions created using ACTION_P macro.
  381. namespace {
  382. ACTION_P(ReturnArgument, ret_value) { return ret_value; }
  383. }
  384. TEST(LinkTest, TestActionPMacro) {
  385. Mock mock;
  386. EXPECT_CALL(mock, IntFromString(_)).WillOnce(ReturnArgument(42));
  387. mock.IntFromString(NULL);
  388. }
  389. // Tests the linkage of actions created using ACTION_P2 macro.
  390. namespace {
  391. ACTION_P2(ReturnEqualsEitherOf, first, second) {
  392. return arg0 == first || arg0 == second;
  393. }
  394. }
  395. #ifdef _MSC_VER
  396. # pragma warning(pop)
  397. #endif
  398. TEST(LinkTest, TestActionP2Macro) {
  399. Mock mock;
  400. char ch = 'x';
  401. EXPECT_CALL(mock, IntFromString(_))
  402. .WillOnce(ReturnEqualsEitherOf("one", "two"));
  403. mock.IntFromString(&ch);
  404. }
  405. // Tests the linkage of the "_" matcher.
  406. TEST(LinkTest, TestMatcherAnything) {
  407. Mock mock;
  408. ON_CALL(mock, VoidFromString(_)).WillByDefault(Return());
  409. }
  410. // Tests the linkage of the A matcher.
  411. TEST(LinkTest, TestMatcherA) {
  412. Mock mock;
  413. ON_CALL(mock, VoidFromString(A<char*>())).WillByDefault(Return());
  414. }
  415. // Tests the linkage of the Eq and the "bare value" matcher.
  416. TEST(LinkTest, TestMatchersEq) {
  417. Mock mock;
  418. const char* p = "x";
  419. ON_CALL(mock, VoidFromString(Eq(p))).WillByDefault(Return());
  420. ON_CALL(mock, VoidFromString(const_cast<char*>("y")))
  421. .WillByDefault(Return());
  422. }
  423. // Tests the linkage of the Lt, Gt, Le, Ge, and Ne matchers.
  424. TEST(LinkTest, TestMatchersRelations) {
  425. Mock mock;
  426. ON_CALL(mock, VoidFromFloat(Lt(1.0f))).WillByDefault(Return());
  427. ON_CALL(mock, VoidFromFloat(Gt(1.0f))).WillByDefault(Return());
  428. ON_CALL(mock, VoidFromFloat(Le(1.0f))).WillByDefault(Return());
  429. ON_CALL(mock, VoidFromFloat(Ge(1.0f))).WillByDefault(Return());
  430. ON_CALL(mock, VoidFromFloat(Ne(1.0f))).WillByDefault(Return());
  431. }
  432. // Tests the linkage of the NotNull matcher.
  433. TEST(LinkTest, TestMatcherNotNull) {
  434. Mock mock;
  435. ON_CALL(mock, VoidFromString(NotNull())).WillByDefault(Return());
  436. }
  437. // Tests the linkage of the IsNull matcher.
  438. TEST(LinkTest, TestMatcherIsNull) {
  439. Mock mock;
  440. ON_CALL(mock, VoidFromString(IsNull())).WillByDefault(Return());
  441. }
  442. // Tests the linkage of the Ref matcher.
  443. TEST(LinkTest, TestMatcherRef) {
  444. Mock mock;
  445. int a = 0;
  446. ON_CALL(mock, VoidFromIntRef(Ref(a))).WillByDefault(Return());
  447. }
  448. // Tests the linkage of the TypedEq matcher.
  449. TEST(LinkTest, TestMatcherTypedEq) {
  450. Mock mock;
  451. long a = 0;
  452. ON_CALL(mock, VoidFromIntRef(TypedEq<int&>(a))).WillByDefault(Return());
  453. }
  454. // Tests the linkage of the FloatEq, DoubleEq, NanSensitiveFloatEq and
  455. // NanSensitiveDoubleEq matchers.
  456. TEST(LinkTest, TestMatchersFloatingPoint) {
  457. Mock mock;
  458. float a = 0;
  459. ON_CALL(mock, VoidFromFloat(FloatEq(a))).WillByDefault(Return());
  460. ON_CALL(mock, VoidFromDouble(DoubleEq(a))).WillByDefault(Return());
  461. ON_CALL(mock, VoidFromFloat(NanSensitiveFloatEq(a))).WillByDefault(Return());
  462. ON_CALL(mock, VoidFromDouble(NanSensitiveDoubleEq(a)))
  463. .WillByDefault(Return());
  464. }
  465. // Tests the linkage of the ContainsRegex matcher.
  466. TEST(LinkTest, TestMatcherContainsRegex) {
  467. Mock mock;
  468. ON_CALL(mock, VoidFromString(ContainsRegex(".*"))).WillByDefault(Return());
  469. }
  470. // Tests the linkage of the MatchesRegex matcher.
  471. TEST(LinkTest, TestMatcherMatchesRegex) {
  472. Mock mock;
  473. ON_CALL(mock, VoidFromString(MatchesRegex(".*"))).WillByDefault(Return());
  474. }
  475. // Tests the linkage of the StartsWith, EndsWith, and HasSubstr matchers.
  476. TEST(LinkTest, TestMatchersSubstrings) {
  477. Mock mock;
  478. ON_CALL(mock, VoidFromString(StartsWith("a"))).WillByDefault(Return());
  479. ON_CALL(mock, VoidFromString(EndsWith("c"))).WillByDefault(Return());
  480. ON_CALL(mock, VoidFromString(HasSubstr("b"))).WillByDefault(Return());
  481. }
  482. // Tests the linkage of the StrEq, StrNe, StrCaseEq, and StrCaseNe matchers.
  483. TEST(LinkTest, TestMatchersStringEquality) {
  484. Mock mock;
  485. ON_CALL(mock, VoidFromString(StrEq("a"))).WillByDefault(Return());
  486. ON_CALL(mock, VoidFromString(StrNe("a"))).WillByDefault(Return());
  487. ON_CALL(mock, VoidFromString(StrCaseEq("a"))).WillByDefault(Return());
  488. ON_CALL(mock, VoidFromString(StrCaseNe("a"))).WillByDefault(Return());
  489. }
  490. // Tests the linkage of the ElementsAre matcher.
  491. TEST(LinkTest, TestMatcherElementsAre) {
  492. Mock mock;
  493. ON_CALL(mock, VoidFromVector(ElementsAre('a', _))).WillByDefault(Return());
  494. }
  495. // Tests the linkage of the ElementsAreArray matcher.
  496. TEST(LinkTest, TestMatcherElementsAreArray) {
  497. Mock mock;
  498. char arr[] = { 'a', 'b' };
  499. ON_CALL(mock, VoidFromVector(ElementsAreArray(arr))).WillByDefault(Return());
  500. }
  501. // Tests the linkage of the ContainerEq matcher.
  502. TEST(LinkTest, TestMatcherContainerEq) {
  503. Mock mock;
  504. std::vector<int> v;
  505. ON_CALL(mock, VoidFromVector(ContainerEq(v))).WillByDefault(Return());
  506. }
  507. // Tests the linkage of the Field matcher.
  508. TEST(LinkTest, TestMatcherField) {
  509. FieldHelper helper(0);
  510. Matcher<const FieldHelper&> m = Field(&FieldHelper::field_, Eq(0));
  511. EXPECT_TRUE(m.Matches(helper));
  512. Matcher<const FieldHelper*> m2 = Field(&FieldHelper::field_, Eq(0));
  513. EXPECT_TRUE(m2.Matches(&helper));
  514. }
  515. // Tests the linkage of the Property matcher.
  516. TEST(LinkTest, TestMatcherProperty) {
  517. FieldHelper helper(0);
  518. Matcher<const FieldHelper&> m = Property(&FieldHelper::field, Eq(0));
  519. EXPECT_TRUE(m.Matches(helper));
  520. Matcher<const FieldHelper*> m2 = Property(&FieldHelper::field, Eq(0));
  521. EXPECT_TRUE(m2.Matches(&helper));
  522. }
  523. // Tests the linkage of the ResultOf matcher.
  524. TEST(LinkTest, TestMatcherResultOf) {
  525. Matcher<char*> m = ResultOf(&InvokeHelper::StaticIntFromString, Eq(1));
  526. EXPECT_TRUE(m.Matches(NULL));
  527. }
  528. // Tests the linkage of the ResultOf matcher.
  529. TEST(LinkTest, TestMatcherPointee) {
  530. int n = 1;
  531. Matcher<int*> m = Pointee(Eq(1));
  532. EXPECT_TRUE(m.Matches(&n));
  533. }
  534. // Tests the linkage of the Truly matcher.
  535. TEST(LinkTest, TestMatcherTruly) {
  536. Matcher<const char*> m = Truly(&InvokeHelper::StaticBoolFromString);
  537. EXPECT_TRUE(m.Matches(NULL));
  538. }
  539. // Tests the linkage of the AllOf matcher.
  540. TEST(LinkTest, TestMatcherAllOf) {
  541. Matcher<int> m = AllOf(_, Eq(1));
  542. EXPECT_TRUE(m.Matches(1));
  543. }
  544. // Tests the linkage of the AnyOf matcher.
  545. TEST(LinkTest, TestMatcherAnyOf) {
  546. Matcher<int> m = AnyOf(_, Eq(1));
  547. EXPECT_TRUE(m.Matches(1));
  548. }
  549. // Tests the linkage of the Not matcher.
  550. TEST(LinkTest, TestMatcherNot) {
  551. Matcher<int> m = Not(_);
  552. EXPECT_FALSE(m.Matches(1));
  553. }
  554. // Tests the linkage of the MatcherCast<T>() function.
  555. TEST(LinkTest, TestMatcherCast) {
  556. Matcher<const char*> m = MatcherCast<const char*>(_);
  557. EXPECT_TRUE(m.Matches(NULL));
  558. }
  559. #endif // GMOCK_TEST_GMOCK_LINK_TEST_H_