gmock_link_test.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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. // Google Mock - a framework for writing C++ mock classes.
  30. //
  31. // This file tests that:
  32. // a. A header file defining a mock class can be included in multiple
  33. // translation units without causing a link error.
  34. // b. Actions and matchers can be instantiated with identical template
  35. // arguments in different translation units without causing link
  36. // errors.
  37. // The following constructs are currently tested:
  38. // Actions:
  39. // Return()
  40. // Return(value)
  41. // ReturnNull
  42. // ReturnRef
  43. // Assign
  44. // SetArgPointee
  45. // SetArrayArgument
  46. // SetErrnoAndReturn
  47. // Invoke(function)
  48. // Invoke(object, method)
  49. // InvokeWithoutArgs(function)
  50. // InvokeWithoutArgs(object, method)
  51. // InvokeArgument
  52. // WithArg
  53. // WithArgs
  54. // WithoutArgs
  55. // DoAll
  56. // DoDefault
  57. // IgnoreResult
  58. // Throw
  59. // ACTION()-generated
  60. // ACTION_P()-generated
  61. // ACTION_P2()-generated
  62. // Matchers:
  63. // _
  64. // A
  65. // An
  66. // Eq
  67. // Gt, Lt, Ge, Le, Ne
  68. // NotNull
  69. // Ref
  70. // TypedEq
  71. // DoubleEq
  72. // FloatEq
  73. // NanSensitiveDoubleEq
  74. // NanSensitiveFloatEq
  75. // ContainsRegex
  76. // MatchesRegex
  77. // EndsWith
  78. // HasSubstr
  79. // StartsWith
  80. // StrCaseEq
  81. // StrCaseNe
  82. // StrEq
  83. // StrNe
  84. // ElementsAre
  85. // ElementsAreArray
  86. // ContainerEq
  87. // Field
  88. // Property
  89. // ResultOf(function)
  90. // ResultOf(callback)
  91. // Pointee
  92. // Truly(predicate)
  93. // AddressSatisfies
  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 GOOGLEMOCK_TEST_GMOCK_LINK_TEST_H_
  113. #define GOOGLEMOCK_TEST_GMOCK_LINK_TEST_H_
  114. #include "gmock/gmock.h"
  115. #ifndef GTEST_OS_WINDOWS_MOBILE
  116. #include <errno.h>
  117. #endif
  118. #include <iostream>
  119. #include <vector>
  120. #include "gtest/gtest.h"
  121. #include "gtest/internal/gtest-port.h"
  122. using testing::_;
  123. using testing::A;
  124. using testing::Action;
  125. using testing::AllOf;
  126. using testing::AnyOf;
  127. using testing::Assign;
  128. using testing::ContainerEq;
  129. using testing::DoAll;
  130. using testing::DoDefault;
  131. using testing::DoubleEq;
  132. using testing::ElementsAre;
  133. using testing::ElementsAreArray;
  134. using testing::EndsWith;
  135. using testing::Eq;
  136. using testing::Field;
  137. using testing::FloatEq;
  138. using testing::Ge;
  139. using testing::Gt;
  140. using testing::HasSubstr;
  141. using testing::IgnoreResult;
  142. using testing::Invoke;
  143. using testing::InvokeArgument;
  144. using testing::InvokeWithoutArgs;
  145. using testing::IsNull;
  146. using testing::IsSubsetOf;
  147. using testing::IsSupersetOf;
  148. using testing::Le;
  149. using testing::Lt;
  150. using testing::Matcher;
  151. using testing::MatcherCast;
  152. using testing::NanSensitiveDoubleEq;
  153. using testing::NanSensitiveFloatEq;
  154. using testing::Ne;
  155. using testing::Not;
  156. using testing::NotNull;
  157. using testing::Pointee;
  158. using testing::Property;
  159. using testing::Ref;
  160. using testing::ResultOf;
  161. using testing::Return;
  162. using testing::ReturnNull;
  163. using testing::ReturnRef;
  164. using testing::SetArgPointee;
  165. using testing::SetArrayArgument;
  166. using testing::StartsWith;
  167. using testing::StrCaseEq;
  168. using testing::StrCaseNe;
  169. using testing::StrEq;
  170. using testing::StrNe;
  171. using testing::Truly;
  172. using testing::TypedEq;
  173. using testing::WithArg;
  174. using testing::WithArgs;
  175. using testing::WithoutArgs;
  176. #ifndef GTEST_OS_WINDOWS_MOBILE
  177. using testing::SetErrnoAndReturn;
  178. #endif
  179. #if GTEST_HAS_EXCEPTIONS
  180. using testing::Throw;
  181. #endif
  182. using testing::ContainsRegex;
  183. using testing::MatchesRegex;
  184. class Interface {
  185. public:
  186. virtual ~Interface() = default;
  187. virtual void VoidFromString(char* str) = 0;
  188. virtual char* StringFromString(char* str) = 0;
  189. virtual int IntFromString(char* str) = 0;
  190. virtual int& IntRefFromString(char* str) = 0;
  191. virtual void VoidFromFunc(void (*func)(char* str)) = 0;
  192. virtual void VoidFromIntRef(int& n) = 0; // NOLINT
  193. virtual void VoidFromFloat(float n) = 0;
  194. virtual void VoidFromDouble(double n) = 0;
  195. virtual void VoidFromVector(const std::vector<int>& v) = 0;
  196. };
  197. class Mock : public Interface {
  198. public:
  199. Mock() = default;
  200. MOCK_METHOD1(VoidFromString, void(char* str));
  201. MOCK_METHOD1(StringFromString, char*(char* str));
  202. MOCK_METHOD1(IntFromString, int(char* str));
  203. MOCK_METHOD1(IntRefFromString, int&(char* str));
  204. MOCK_METHOD1(VoidFromFunc, void(void (*func)(char* str)));
  205. MOCK_METHOD1(VoidFromIntRef, void(int& n)); // NOLINT
  206. MOCK_METHOD1(VoidFromFloat, void(float n));
  207. MOCK_METHOD1(VoidFromDouble, void(double n));
  208. MOCK_METHOD1(VoidFromVector, void(const std::vector<int>& v));
  209. private:
  210. Mock(const Mock&) = delete;
  211. Mock& operator=(const Mock&) = delete;
  212. };
  213. class InvokeHelper {
  214. public:
  215. static void StaticVoidFromVoid() {}
  216. void VoidFromVoid() {}
  217. static void StaticVoidFromString(char* /* str */) {}
  218. void VoidFromString(char* /* str */) {}
  219. static int StaticIntFromString(char* /* str */) { return 1; }
  220. static bool StaticBoolFromString(const char* /* str */) { return true; }
  221. };
  222. class FieldHelper {
  223. public:
  224. explicit FieldHelper(int a_field) : field_(a_field) {}
  225. int field() const { return field_; }
  226. int field_; // NOLINT -- need external access to field_ to test
  227. // the Field matcher.
  228. };
  229. // Tests the linkage of the ReturnVoid action.
  230. TEST(LinkTest, TestReturnVoid) {
  231. Mock mock;
  232. EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Return());
  233. mock.VoidFromString(nullptr);
  234. }
  235. // Tests the linkage of the Return action.
  236. TEST(LinkTest, TestReturn) {
  237. Mock mock;
  238. char ch = 'x';
  239. EXPECT_CALL(mock, StringFromString(_)).WillOnce(Return(&ch));
  240. mock.StringFromString(nullptr);
  241. }
  242. // Tests the linkage of the ReturnNull action.
  243. TEST(LinkTest, TestReturnNull) {
  244. Mock mock;
  245. EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Return());
  246. mock.VoidFromString(nullptr);
  247. }
  248. // Tests the linkage of the ReturnRef action.
  249. TEST(LinkTest, TestReturnRef) {
  250. Mock mock;
  251. int n = 42;
  252. EXPECT_CALL(mock, IntRefFromString(_)).WillOnce(ReturnRef(n));
  253. mock.IntRefFromString(nullptr);
  254. }
  255. // Tests the linkage of the Assign action.
  256. TEST(LinkTest, TestAssign) {
  257. Mock mock;
  258. char ch = 'x';
  259. EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Assign(&ch, 'y'));
  260. mock.VoidFromString(nullptr);
  261. }
  262. // Tests the linkage of the SetArgPointee action.
  263. TEST(LinkTest, TestSetArgPointee) {
  264. Mock mock;
  265. char ch = 'x';
  266. EXPECT_CALL(mock, VoidFromString(_)).WillOnce(SetArgPointee<0>('y'));
  267. mock.VoidFromString(&ch);
  268. }
  269. // Tests the linkage of the SetArrayArgument action.
  270. TEST(LinkTest, TestSetArrayArgument) {
  271. Mock mock;
  272. char ch = 'x';
  273. char ch2 = 'y';
  274. EXPECT_CALL(mock, VoidFromString(_))
  275. .WillOnce(SetArrayArgument<0>(&ch2, &ch2 + 1));
  276. mock.VoidFromString(&ch);
  277. }
  278. #ifndef GTEST_OS_WINDOWS_MOBILE
  279. // Tests the linkage of the SetErrnoAndReturn action.
  280. TEST(LinkTest, TestSetErrnoAndReturn) {
  281. Mock mock;
  282. int saved_errno = errno;
  283. EXPECT_CALL(mock, IntFromString(_)).WillOnce(SetErrnoAndReturn(1, -1));
  284. mock.IntFromString(nullptr);
  285. errno = saved_errno;
  286. }
  287. #endif // !GTEST_OS_WINDOWS_MOBILE
  288. // Tests the linkage of the Invoke(function) and Invoke(object, method) actions.
  289. TEST(LinkTest, TestInvoke) {
  290. Mock mock;
  291. InvokeHelper test_invoke_helper;
  292. EXPECT_CALL(mock, VoidFromString(_))
  293. .WillOnce(Invoke(&InvokeHelper::StaticVoidFromString))
  294. .WillOnce(Invoke(&test_invoke_helper, &InvokeHelper::VoidFromString));
  295. mock.VoidFromString(nullptr);
  296. mock.VoidFromString(nullptr);
  297. }
  298. // Tests the linkage of the InvokeWithoutArgs action.
  299. TEST(LinkTest, TestInvokeWithoutArgs) {
  300. Mock mock;
  301. InvokeHelper test_invoke_helper;
  302. EXPECT_CALL(mock, VoidFromString(_))
  303. .WillOnce(InvokeWithoutArgs(&InvokeHelper::StaticVoidFromVoid))
  304. .WillOnce(
  305. InvokeWithoutArgs(&test_invoke_helper, &InvokeHelper::VoidFromVoid));
  306. mock.VoidFromString(nullptr);
  307. mock.VoidFromString(nullptr);
  308. }
  309. // Tests the linkage of the InvokeArgument action.
  310. TEST(LinkTest, TestInvokeArgument) {
  311. Mock mock;
  312. char ch = 'x';
  313. EXPECT_CALL(mock, VoidFromFunc(_)).WillOnce(InvokeArgument<0>(&ch));
  314. mock.VoidFromFunc(InvokeHelper::StaticVoidFromString);
  315. }
  316. // Tests the linkage of the WithArg action.
  317. TEST(LinkTest, TestWithArg) {
  318. Mock mock;
  319. EXPECT_CALL(mock, VoidFromString(_))
  320. .WillOnce(WithArg<0>(Invoke(&InvokeHelper::StaticVoidFromString)));
  321. mock.VoidFromString(nullptr);
  322. }
  323. // Tests the linkage of the WithArgs action.
  324. TEST(LinkTest, TestWithArgs) {
  325. Mock mock;
  326. EXPECT_CALL(mock, VoidFromString(_))
  327. .WillOnce(WithArgs<0>(Invoke(&InvokeHelper::StaticVoidFromString)));
  328. mock.VoidFromString(nullptr);
  329. }
  330. // Tests the linkage of the WithoutArgs action.
  331. TEST(LinkTest, TestWithoutArgs) {
  332. Mock mock;
  333. EXPECT_CALL(mock, VoidFromString(_)).WillOnce(WithoutArgs(Return()));
  334. mock.VoidFromString(nullptr);
  335. }
  336. // Tests the linkage of the DoAll action.
  337. TEST(LinkTest, TestDoAll) {
  338. Mock mock;
  339. char ch = 'x';
  340. EXPECT_CALL(mock, VoidFromString(_))
  341. .WillOnce(DoAll(SetArgPointee<0>('y'), Return()));
  342. mock.VoidFromString(&ch);
  343. }
  344. // Tests the linkage of the DoDefault action.
  345. TEST(LinkTest, TestDoDefault) {
  346. Mock mock;
  347. char ch = 'x';
  348. ON_CALL(mock, VoidFromString(_)).WillByDefault(Return());
  349. EXPECT_CALL(mock, VoidFromString(_)).WillOnce(DoDefault());
  350. mock.VoidFromString(&ch);
  351. }
  352. // Tests the linkage of the IgnoreResult action.
  353. TEST(LinkTest, TestIgnoreResult) {
  354. Mock mock;
  355. EXPECT_CALL(mock, VoidFromString(_)).WillOnce(IgnoreResult(Return(42)));
  356. mock.VoidFromString(nullptr);
  357. }
  358. #if GTEST_HAS_EXCEPTIONS
  359. // Tests the linkage of the Throw action.
  360. TEST(LinkTest, TestThrow) {
  361. Mock mock;
  362. EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Throw(42));
  363. EXPECT_THROW(mock.VoidFromString(nullptr), int);
  364. }
  365. #endif // GTEST_HAS_EXCEPTIONS
  366. // The ACTION*() macros trigger warning C4100 (unreferenced formal
  367. // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
  368. // the macro definition, as the warnings are generated when the macro
  369. // is expanded and macro expansion cannot contain #pragma. Therefore
  370. // we suppress them here.
  371. GTEST_DISABLE_MSC_WARNINGS_PUSH_(4100)
  372. // Tests the linkage of actions created using ACTION macro.
  373. namespace {
  374. ACTION(Return1) { return 1; }
  375. } // namespace
  376. TEST(LinkTest, TestActionMacro) {
  377. Mock mock;
  378. EXPECT_CALL(mock, IntFromString(_)).WillOnce(Return1());
  379. mock.IntFromString(nullptr);
  380. }
  381. // Tests the linkage of actions created using ACTION_P macro.
  382. namespace {
  383. ACTION_P(ReturnArgument, ret_value) { return ret_value; }
  384. } // namespace
  385. TEST(LinkTest, TestActionPMacro) {
  386. Mock mock;
  387. EXPECT_CALL(mock, IntFromString(_)).WillOnce(ReturnArgument(42));
  388. mock.IntFromString(nullptr);
  389. }
  390. // Tests the linkage of actions created using ACTION_P2 macro.
  391. namespace {
  392. ACTION_P2(ReturnEqualsEitherOf, first, second) {
  393. return arg0 == first || arg0 == second;
  394. }
  395. } // namespace
  396. GTEST_DISABLE_MSC_WARNINGS_POP_() // 4100
  397. TEST(LinkTest, TestActionP2Macro) {
  398. Mock mock;
  399. char ch = 'x';
  400. EXPECT_CALL(mock, IntFromString(_))
  401. .WillOnce(ReturnEqualsEitherOf("one", "two"));
  402. mock.IntFromString(&ch);
  403. }
  404. // Tests the linkage of the "_" matcher.
  405. TEST(LinkTest, TestMatcherAnything) {
  406. Mock mock;
  407. ON_CALL(mock, VoidFromString(_)).WillByDefault(Return());
  408. }
  409. // Tests the linkage of the A matcher.
  410. TEST(LinkTest, TestMatcherA) {
  411. Mock mock;
  412. ON_CALL(mock, VoidFromString(A<char*>())).WillByDefault(Return());
  413. }
  414. // Tests the linkage of the Eq and the "bare value" matcher.
  415. TEST(LinkTest, TestMatchersEq) {
  416. Mock mock;
  417. const char* p = "x";
  418. ON_CALL(mock, VoidFromString(Eq(p))).WillByDefault(Return());
  419. ON_CALL(mock, VoidFromString(const_cast<char*>("y"))).WillByDefault(Return());
  420. }
  421. // Tests the linkage of the Lt, Gt, Le, Ge, and Ne matchers.
  422. TEST(LinkTest, TestMatchersRelations) {
  423. Mock mock;
  424. ON_CALL(mock, VoidFromFloat(Lt(1.0f))).WillByDefault(Return());
  425. ON_CALL(mock, VoidFromFloat(Gt(1.0f))).WillByDefault(Return());
  426. ON_CALL(mock, VoidFromFloat(Le(1.0f))).WillByDefault(Return());
  427. ON_CALL(mock, VoidFromFloat(Ge(1.0f))).WillByDefault(Return());
  428. ON_CALL(mock, VoidFromFloat(Ne(1.0f))).WillByDefault(Return());
  429. }
  430. // Tests the linkage of the NotNull matcher.
  431. TEST(LinkTest, TestMatcherNotNull) {
  432. Mock mock;
  433. ON_CALL(mock, VoidFromString(NotNull())).WillByDefault(Return());
  434. }
  435. // Tests the linkage of the IsNull matcher.
  436. TEST(LinkTest, TestMatcherIsNull) {
  437. Mock mock;
  438. ON_CALL(mock, VoidFromString(IsNull())).WillByDefault(Return());
  439. }
  440. // Tests the linkage of the Ref matcher.
  441. TEST(LinkTest, TestMatcherRef) {
  442. Mock mock;
  443. int a = 0;
  444. ON_CALL(mock, VoidFromIntRef(Ref(a))).WillByDefault(Return());
  445. }
  446. // Tests the linkage of the TypedEq matcher.
  447. TEST(LinkTest, TestMatcherTypedEq) {
  448. Mock mock;
  449. long a = 0;
  450. ON_CALL(mock, VoidFromIntRef(TypedEq<int&>(a))).WillByDefault(Return());
  451. }
  452. // Tests the linkage of the FloatEq, DoubleEq, NanSensitiveFloatEq and
  453. // NanSensitiveDoubleEq matchers.
  454. TEST(LinkTest, TestMatchersFloatingPoint) {
  455. Mock mock;
  456. float a = 0;
  457. ON_CALL(mock, VoidFromFloat(FloatEq(a))).WillByDefault(Return());
  458. ON_CALL(mock, VoidFromDouble(DoubleEq(a))).WillByDefault(Return());
  459. ON_CALL(mock, VoidFromFloat(NanSensitiveFloatEq(a))).WillByDefault(Return());
  460. ON_CALL(mock, VoidFromDouble(NanSensitiveDoubleEq(a)))
  461. .WillByDefault(Return());
  462. }
  463. // Tests the linkage of the ContainsRegex matcher.
  464. TEST(LinkTest, TestMatcherContainsRegex) {
  465. Mock mock;
  466. ON_CALL(mock, VoidFromString(ContainsRegex(".*"))).WillByDefault(Return());
  467. }
  468. // Tests the linkage of the MatchesRegex matcher.
  469. TEST(LinkTest, TestMatcherMatchesRegex) {
  470. Mock mock;
  471. ON_CALL(mock, VoidFromString(MatchesRegex(".*"))).WillByDefault(Return());
  472. }
  473. // Tests the linkage of the StartsWith, EndsWith, and HasSubstr matchers.
  474. TEST(LinkTest, TestMatchersSubstrings) {
  475. Mock mock;
  476. ON_CALL(mock, VoidFromString(StartsWith("a"))).WillByDefault(Return());
  477. ON_CALL(mock, VoidFromString(EndsWith("c"))).WillByDefault(Return());
  478. ON_CALL(mock, VoidFromString(HasSubstr("b"))).WillByDefault(Return());
  479. }
  480. // Tests the linkage of the StrEq, StrNe, StrCaseEq, and StrCaseNe matchers.
  481. TEST(LinkTest, TestMatchersStringEquality) {
  482. Mock mock;
  483. ON_CALL(mock, VoidFromString(StrEq("a"))).WillByDefault(Return());
  484. ON_CALL(mock, VoidFromString(StrNe("a"))).WillByDefault(Return());
  485. ON_CALL(mock, VoidFromString(StrCaseEq("a"))).WillByDefault(Return());
  486. ON_CALL(mock, VoidFromString(StrCaseNe("a"))).WillByDefault(Return());
  487. }
  488. // Tests the linkage of the ElementsAre matcher.
  489. TEST(LinkTest, TestMatcherElementsAre) {
  490. Mock mock;
  491. ON_CALL(mock, VoidFromVector(ElementsAre('a', _))).WillByDefault(Return());
  492. }
  493. // Tests the linkage of the ElementsAreArray matcher.
  494. TEST(LinkTest, TestMatcherElementsAreArray) {
  495. Mock mock;
  496. char arr[] = {'a', 'b'};
  497. ON_CALL(mock, VoidFromVector(ElementsAreArray(arr))).WillByDefault(Return());
  498. }
  499. // Tests the linkage of the IsSubsetOf matcher.
  500. TEST(LinkTest, TestMatcherIsSubsetOf) {
  501. Mock mock;
  502. char arr[] = {'a', 'b'};
  503. ON_CALL(mock, VoidFromVector(IsSubsetOf(arr))).WillByDefault(Return());
  504. }
  505. // Tests the linkage of the IsSupersetOf matcher.
  506. TEST(LinkTest, TestMatcherIsSupersetOf) {
  507. Mock mock;
  508. char arr[] = {'a', 'b'};
  509. ON_CALL(mock, VoidFromVector(IsSupersetOf(arr))).WillByDefault(Return());
  510. }
  511. // Tests the linkage of the ContainerEq matcher.
  512. TEST(LinkTest, TestMatcherContainerEq) {
  513. Mock mock;
  514. std::vector<int> v;
  515. ON_CALL(mock, VoidFromVector(ContainerEq(v))).WillByDefault(Return());
  516. }
  517. // Tests the linkage of the Field matcher.
  518. TEST(LinkTest, TestMatcherField) {
  519. FieldHelper helper(0);
  520. Matcher<const FieldHelper&> m = Field(&FieldHelper::field_, Eq(0));
  521. EXPECT_TRUE(m.Matches(helper));
  522. Matcher<const FieldHelper*> m2 = Field(&FieldHelper::field_, Eq(0));
  523. EXPECT_TRUE(m2.Matches(&helper));
  524. }
  525. // Tests the linkage of the Property matcher.
  526. TEST(LinkTest, TestMatcherProperty) {
  527. FieldHelper helper(0);
  528. Matcher<const FieldHelper&> m = Property(&FieldHelper::field, Eq(0));
  529. EXPECT_TRUE(m.Matches(helper));
  530. Matcher<const FieldHelper*> m2 = Property(&FieldHelper::field, Eq(0));
  531. EXPECT_TRUE(m2.Matches(&helper));
  532. }
  533. // Tests the linkage of the ResultOf matcher.
  534. TEST(LinkTest, TestMatcherResultOf) {
  535. Matcher<char*> m = ResultOf(&InvokeHelper::StaticIntFromString, Eq(1));
  536. EXPECT_TRUE(m.Matches(nullptr));
  537. }
  538. // Tests the linkage of the ResultOf matcher.
  539. TEST(LinkTest, TestMatcherPointee) {
  540. int n = 1;
  541. Matcher<int*> m = Pointee(Eq(1));
  542. EXPECT_TRUE(m.Matches(&n));
  543. }
  544. // Tests the linkage of the Truly matcher.
  545. TEST(LinkTest, TestMatcherTruly) {
  546. Matcher<const char*> m = Truly(&InvokeHelper::StaticBoolFromString);
  547. EXPECT_TRUE(m.Matches(nullptr));
  548. }
  549. // Tests the linkage of the AllOf matcher.
  550. TEST(LinkTest, TestMatcherAllOf) {
  551. Matcher<int> m = AllOf(_, Eq(1));
  552. EXPECT_TRUE(m.Matches(1));
  553. }
  554. // Tests the linkage of the AnyOf matcher.
  555. TEST(LinkTest, TestMatcherAnyOf) {
  556. Matcher<int> m = AnyOf(_, Eq(1));
  557. EXPECT_TRUE(m.Matches(1));
  558. }
  559. // Tests the linkage of the Not matcher.
  560. TEST(LinkTest, TestMatcherNot) {
  561. Matcher<int> m = Not(_);
  562. EXPECT_FALSE(m.Matches(1));
  563. }
  564. // Tests the linkage of the MatcherCast<T>() function.
  565. TEST(LinkTest, TestMatcherCast) {
  566. Matcher<const char*> m = MatcherCast<const char*>(_);
  567. EXPECT_TRUE(m.Matches(nullptr));
  568. }
  569. #endif // GOOGLEMOCK_TEST_GMOCK_LINK_TEST_H_