gmock_link_test.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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 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 <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. #if !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() {}
  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() {}
  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. GTEST_DISALLOW_COPY_AND_ASSIGN_(Mock);
  211. };
  212. class InvokeHelper {
  213. public:
  214. static void StaticVoidFromVoid() {}
  215. void VoidFromVoid() {}
  216. static void StaticVoidFromString(char* /* str */) {}
  217. void VoidFromString(char* /* str */) {}
  218. static int StaticIntFromString(char* /* str */) { return 1; }
  219. static bool StaticBoolFromString(const char* /* str */) { return true; }
  220. };
  221. class FieldHelper {
  222. public:
  223. explicit FieldHelper(int a_field) : field_(a_field) {}
  224. int field() const { return field_; }
  225. int field_; // NOLINT -- need external access to field_ to test
  226. // the Field matcher.
  227. };
  228. // Tests the linkage of the ReturnVoid action.
  229. TEST(LinkTest, TestReturnVoid) {
  230. Mock mock;
  231. EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Return());
  232. mock.VoidFromString(NULL);
  233. }
  234. // Tests the linkage of the Return action.
  235. TEST(LinkTest, TestReturn) {
  236. Mock mock;
  237. char ch = 'x';
  238. EXPECT_CALL(mock, StringFromString(_)).WillOnce(Return(&ch));
  239. mock.StringFromString(NULL);
  240. }
  241. // Tests the linkage of the ReturnNull action.
  242. TEST(LinkTest, TestReturnNull) {
  243. Mock mock;
  244. EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Return());
  245. mock.VoidFromString(NULL);
  246. }
  247. // Tests the linkage of the ReturnRef action.
  248. TEST(LinkTest, TestReturnRef) {
  249. Mock mock;
  250. int n = 42;
  251. EXPECT_CALL(mock, IntRefFromString(_)).WillOnce(ReturnRef(n));
  252. mock.IntRefFromString(NULL);
  253. }
  254. // Tests the linkage of the Assign action.
  255. TEST(LinkTest, TestAssign) {
  256. Mock mock;
  257. char ch = 'x';
  258. EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Assign(&ch, 'y'));
  259. mock.VoidFromString(NULL);
  260. }
  261. // Tests the linkage of the SetArgPointee action.
  262. TEST(LinkTest, TestSetArgPointee) {
  263. Mock mock;
  264. char ch = 'x';
  265. EXPECT_CALL(mock, VoidFromString(_)).WillOnce(SetArgPointee<0>('y'));
  266. mock.VoidFromString(&ch);
  267. }
  268. // Tests the linkage of the SetArrayArgument action.
  269. TEST(LinkTest, TestSetArrayArgument) {
  270. Mock mock;
  271. char ch = 'x';
  272. char ch2 = 'y';
  273. EXPECT_CALL(mock, VoidFromString(_)).WillOnce(SetArrayArgument<0>(&ch2,
  274. &ch2 + 1));
  275. mock.VoidFromString(&ch);
  276. }
  277. #if !GTEST_OS_WINDOWS_MOBILE
  278. // Tests the linkage of the SetErrnoAndReturn action.
  279. TEST(LinkTest, TestSetErrnoAndReturn) {
  280. Mock mock;
  281. int saved_errno = errno;
  282. EXPECT_CALL(mock, IntFromString(_)).WillOnce(SetErrnoAndReturn(1, -1));
  283. mock.IntFromString(NULL);
  284. errno = saved_errno;
  285. }
  286. #endif // !GTEST_OS_WINDOWS_MOBILE
  287. // Tests the linkage of the Invoke(function) and Invoke(object, method) actions.
  288. TEST(LinkTest, TestInvoke) {
  289. Mock mock;
  290. InvokeHelper test_invoke_helper;
  291. EXPECT_CALL(mock, VoidFromString(_))
  292. .WillOnce(Invoke(&InvokeHelper::StaticVoidFromString))
  293. .WillOnce(Invoke(&test_invoke_helper, &InvokeHelper::VoidFromString));
  294. mock.VoidFromString(NULL);
  295. mock.VoidFromString(NULL);
  296. }
  297. // Tests the linkage of the InvokeWithoutArgs action.
  298. TEST(LinkTest, TestInvokeWithoutArgs) {
  299. Mock mock;
  300. InvokeHelper test_invoke_helper;
  301. EXPECT_CALL(mock, VoidFromString(_))
  302. .WillOnce(InvokeWithoutArgs(&InvokeHelper::StaticVoidFromVoid))
  303. .WillOnce(InvokeWithoutArgs(&test_invoke_helper,
  304. &InvokeHelper::VoidFromVoid));
  305. mock.VoidFromString(NULL);
  306. mock.VoidFromString(NULL);
  307. }
  308. // Tests the linkage of the InvokeArgument action.
  309. TEST(LinkTest, TestInvokeArgument) {
  310. Mock mock;
  311. char ch = 'x';
  312. EXPECT_CALL(mock, VoidFromFunc(_)).WillOnce(InvokeArgument<0>(&ch));
  313. mock.VoidFromFunc(InvokeHelper::StaticVoidFromString);
  314. }
  315. // Tests the linkage of the WithArg action.
  316. TEST(LinkTest, TestWithArg) {
  317. Mock mock;
  318. EXPECT_CALL(mock, VoidFromString(_))
  319. .WillOnce(WithArg<0>(Invoke(&InvokeHelper::StaticVoidFromString)));
  320. mock.VoidFromString(NULL);
  321. }
  322. // Tests the linkage of the WithArgs action.
  323. TEST(LinkTest, TestWithArgs) {
  324. Mock mock;
  325. EXPECT_CALL(mock, VoidFromString(_))
  326. .WillOnce(WithArgs<0>(Invoke(&InvokeHelper::StaticVoidFromString)));
  327. mock.VoidFromString(NULL);
  328. }
  329. // Tests the linkage of the WithoutArgs action.
  330. TEST(LinkTest, TestWithoutArgs) {
  331. Mock mock;
  332. EXPECT_CALL(mock, VoidFromString(_)).WillOnce(WithoutArgs(Return()));
  333. mock.VoidFromString(NULL);
  334. }
  335. // Tests the linkage of the DoAll action.
  336. TEST(LinkTest, TestDoAll) {
  337. Mock mock;
  338. char ch = 'x';
  339. EXPECT_CALL(mock, VoidFromString(_))
  340. .WillOnce(DoAll(SetArgPointee<0>('y'), Return()));
  341. mock.VoidFromString(&ch);
  342. }
  343. // Tests the linkage of the DoDefault action.
  344. TEST(LinkTest, TestDoDefault) {
  345. Mock mock;
  346. char ch = 'x';
  347. ON_CALL(mock, VoidFromString(_)).WillByDefault(Return());
  348. EXPECT_CALL(mock, VoidFromString(_)).WillOnce(DoDefault());
  349. mock.VoidFromString(&ch);
  350. }
  351. // Tests the linkage of the IgnoreResult action.
  352. TEST(LinkTest, TestIgnoreResult) {
  353. Mock mock;
  354. EXPECT_CALL(mock, VoidFromString(_)).WillOnce(IgnoreResult(Return(42)));
  355. mock.VoidFromString(NULL);
  356. }
  357. #if GTEST_HAS_EXCEPTIONS
  358. // Tests the linkage of the Throw action.
  359. TEST(LinkTest, TestThrow) {
  360. Mock mock;
  361. EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Throw(42));
  362. EXPECT_THROW(mock.VoidFromString(NULL), int);
  363. }
  364. #endif // GTEST_HAS_EXCEPTIONS
  365. // The ACTION*() macros trigger warning C4100 (unreferenced formal
  366. // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
  367. // the macro definition, as the warnings are generated when the macro
  368. // is expanded and macro expansion cannot contain #pragma. Therefore
  369. // we suppress them here.
  370. #ifdef _MSC_VER
  371. # pragma warning(push)
  372. # pragma warning(disable:4100)
  373. #endif
  374. // Tests the linkage of actions created using ACTION macro.
  375. namespace {
  376. ACTION(Return1) { return 1; }
  377. }
  378. TEST(LinkTest, TestActionMacro) {
  379. Mock mock;
  380. EXPECT_CALL(mock, IntFromString(_)).WillOnce(Return1());
  381. mock.IntFromString(NULL);
  382. }
  383. // Tests the linkage of actions created using ACTION_P macro.
  384. namespace {
  385. ACTION_P(ReturnArgument, ret_value) { return ret_value; }
  386. }
  387. TEST(LinkTest, TestActionPMacro) {
  388. Mock mock;
  389. EXPECT_CALL(mock, IntFromString(_)).WillOnce(ReturnArgument(42));
  390. mock.IntFromString(NULL);
  391. }
  392. // Tests the linkage of actions created using ACTION_P2 macro.
  393. namespace {
  394. ACTION_P2(ReturnEqualsEitherOf, first, second) {
  395. return arg0 == first || arg0 == second;
  396. }
  397. }
  398. #ifdef _MSC_VER
  399. # pragma warning(pop)
  400. #endif
  401. TEST(LinkTest, TestActionP2Macro) {
  402. Mock mock;
  403. char ch = 'x';
  404. EXPECT_CALL(mock, IntFromString(_))
  405. .WillOnce(ReturnEqualsEitherOf("one", "two"));
  406. mock.IntFromString(&ch);
  407. }
  408. // Tests the linkage of the "_" matcher.
  409. TEST(LinkTest, TestMatcherAnything) {
  410. Mock mock;
  411. ON_CALL(mock, VoidFromString(_)).WillByDefault(Return());
  412. }
  413. // Tests the linkage of the A matcher.
  414. TEST(LinkTest, TestMatcherA) {
  415. Mock mock;
  416. ON_CALL(mock, VoidFromString(A<char*>())).WillByDefault(Return());
  417. }
  418. // Tests the linkage of the Eq and the "bare value" matcher.
  419. TEST(LinkTest, TestMatchersEq) {
  420. Mock mock;
  421. const char* p = "x";
  422. ON_CALL(mock, VoidFromString(Eq(p))).WillByDefault(Return());
  423. ON_CALL(mock, VoidFromString(const_cast<char*>("y")))
  424. .WillByDefault(Return());
  425. }
  426. // Tests the linkage of the Lt, Gt, Le, Ge, and Ne matchers.
  427. TEST(LinkTest, TestMatchersRelations) {
  428. Mock mock;
  429. ON_CALL(mock, VoidFromFloat(Lt(1.0f))).WillByDefault(Return());
  430. ON_CALL(mock, VoidFromFloat(Gt(1.0f))).WillByDefault(Return());
  431. ON_CALL(mock, VoidFromFloat(Le(1.0f))).WillByDefault(Return());
  432. ON_CALL(mock, VoidFromFloat(Ge(1.0f))).WillByDefault(Return());
  433. ON_CALL(mock, VoidFromFloat(Ne(1.0f))).WillByDefault(Return());
  434. }
  435. // Tests the linkage of the NotNull matcher.
  436. TEST(LinkTest, TestMatcherNotNull) {
  437. Mock mock;
  438. ON_CALL(mock, VoidFromString(NotNull())).WillByDefault(Return());
  439. }
  440. // Tests the linkage of the IsNull matcher.
  441. TEST(LinkTest, TestMatcherIsNull) {
  442. Mock mock;
  443. ON_CALL(mock, VoidFromString(IsNull())).WillByDefault(Return());
  444. }
  445. // Tests the linkage of the Ref matcher.
  446. TEST(LinkTest, TestMatcherRef) {
  447. Mock mock;
  448. int a = 0;
  449. ON_CALL(mock, VoidFromIntRef(Ref(a))).WillByDefault(Return());
  450. }
  451. // Tests the linkage of the TypedEq matcher.
  452. TEST(LinkTest, TestMatcherTypedEq) {
  453. Mock mock;
  454. long a = 0;
  455. ON_CALL(mock, VoidFromIntRef(TypedEq<int&>(a))).WillByDefault(Return());
  456. }
  457. // Tests the linkage of the FloatEq, DoubleEq, NanSensitiveFloatEq and
  458. // NanSensitiveDoubleEq matchers.
  459. TEST(LinkTest, TestMatchersFloatingPoint) {
  460. Mock mock;
  461. float a = 0;
  462. ON_CALL(mock, VoidFromFloat(FloatEq(a))).WillByDefault(Return());
  463. ON_CALL(mock, VoidFromDouble(DoubleEq(a))).WillByDefault(Return());
  464. ON_CALL(mock, VoidFromFloat(NanSensitiveFloatEq(a))).WillByDefault(Return());
  465. ON_CALL(mock, VoidFromDouble(NanSensitiveDoubleEq(a)))
  466. .WillByDefault(Return());
  467. }
  468. // Tests the linkage of the ContainsRegex matcher.
  469. TEST(LinkTest, TestMatcherContainsRegex) {
  470. Mock mock;
  471. ON_CALL(mock, VoidFromString(ContainsRegex(".*"))).WillByDefault(Return());
  472. }
  473. // Tests the linkage of the MatchesRegex matcher.
  474. TEST(LinkTest, TestMatcherMatchesRegex) {
  475. Mock mock;
  476. ON_CALL(mock, VoidFromString(MatchesRegex(".*"))).WillByDefault(Return());
  477. }
  478. // Tests the linkage of the StartsWith, EndsWith, and HasSubstr matchers.
  479. TEST(LinkTest, TestMatchersSubstrings) {
  480. Mock mock;
  481. ON_CALL(mock, VoidFromString(StartsWith("a"))).WillByDefault(Return());
  482. ON_CALL(mock, VoidFromString(EndsWith("c"))).WillByDefault(Return());
  483. ON_CALL(mock, VoidFromString(HasSubstr("b"))).WillByDefault(Return());
  484. }
  485. // Tests the linkage of the StrEq, StrNe, StrCaseEq, and StrCaseNe matchers.
  486. TEST(LinkTest, TestMatchersStringEquality) {
  487. Mock mock;
  488. ON_CALL(mock, VoidFromString(StrEq("a"))).WillByDefault(Return());
  489. ON_CALL(mock, VoidFromString(StrNe("a"))).WillByDefault(Return());
  490. ON_CALL(mock, VoidFromString(StrCaseEq("a"))).WillByDefault(Return());
  491. ON_CALL(mock, VoidFromString(StrCaseNe("a"))).WillByDefault(Return());
  492. }
  493. // Tests the linkage of the ElementsAre matcher.
  494. TEST(LinkTest, TestMatcherElementsAre) {
  495. Mock mock;
  496. ON_CALL(mock, VoidFromVector(ElementsAre('a', _))).WillByDefault(Return());
  497. }
  498. // Tests the linkage of the ElementsAreArray matcher.
  499. TEST(LinkTest, TestMatcherElementsAreArray) {
  500. Mock mock;
  501. char arr[] = { 'a', 'b' };
  502. ON_CALL(mock, VoidFromVector(ElementsAreArray(arr))).WillByDefault(Return());
  503. }
  504. // Tests the linkage of the IsSubsetOf matcher.
  505. TEST(LinkTest, TestMatcherIsSubsetOf) {
  506. Mock mock;
  507. char arr[] = {'a', 'b'};
  508. ON_CALL(mock, VoidFromVector(IsSubsetOf(arr))).WillByDefault(Return());
  509. }
  510. // Tests the linkage of the IsSupersetOf matcher.
  511. TEST(LinkTest, TestMatcherIsSupersetOf) {
  512. Mock mock;
  513. char arr[] = {'a', 'b'};
  514. ON_CALL(mock, VoidFromVector(IsSupersetOf(arr))).WillByDefault(Return());
  515. }
  516. // Tests the linkage of the ContainerEq matcher.
  517. TEST(LinkTest, TestMatcherContainerEq) {
  518. Mock mock;
  519. std::vector<int> v;
  520. ON_CALL(mock, VoidFromVector(ContainerEq(v))).WillByDefault(Return());
  521. }
  522. // Tests the linkage of the Field matcher.
  523. TEST(LinkTest, TestMatcherField) {
  524. FieldHelper helper(0);
  525. Matcher<const FieldHelper&> m = Field(&FieldHelper::field_, Eq(0));
  526. EXPECT_TRUE(m.Matches(helper));
  527. Matcher<const FieldHelper*> m2 = Field(&FieldHelper::field_, Eq(0));
  528. EXPECT_TRUE(m2.Matches(&helper));
  529. }
  530. // Tests the linkage of the Property matcher.
  531. TEST(LinkTest, TestMatcherProperty) {
  532. FieldHelper helper(0);
  533. Matcher<const FieldHelper&> m = Property(&FieldHelper::field, Eq(0));
  534. EXPECT_TRUE(m.Matches(helper));
  535. Matcher<const FieldHelper*> m2 = Property(&FieldHelper::field, Eq(0));
  536. EXPECT_TRUE(m2.Matches(&helper));
  537. }
  538. // Tests the linkage of the ResultOf matcher.
  539. TEST(LinkTest, TestMatcherResultOf) {
  540. Matcher<char*> m = ResultOf(&InvokeHelper::StaticIntFromString, Eq(1));
  541. EXPECT_TRUE(m.Matches(NULL));
  542. }
  543. // Tests the linkage of the ResultOf matcher.
  544. TEST(LinkTest, TestMatcherPointee) {
  545. int n = 1;
  546. Matcher<int*> m = Pointee(Eq(1));
  547. EXPECT_TRUE(m.Matches(&n));
  548. }
  549. // Tests the linkage of the Truly matcher.
  550. TEST(LinkTest, TestMatcherTruly) {
  551. Matcher<const char*> m = Truly(&InvokeHelper::StaticBoolFromString);
  552. EXPECT_TRUE(m.Matches(NULL));
  553. }
  554. // Tests the linkage of the AllOf matcher.
  555. TEST(LinkTest, TestMatcherAllOf) {
  556. Matcher<int> m = AllOf(_, Eq(1));
  557. EXPECT_TRUE(m.Matches(1));
  558. }
  559. // Tests the linkage of the AnyOf matcher.
  560. TEST(LinkTest, TestMatcherAnyOf) {
  561. Matcher<int> m = AnyOf(_, Eq(1));
  562. EXPECT_TRUE(m.Matches(1));
  563. }
  564. // Tests the linkage of the Not matcher.
  565. TEST(LinkTest, TestMatcherNot) {
  566. Matcher<int> m = Not(_);
  567. EXPECT_FALSE(m.Matches(1));
  568. }
  569. // Tests the linkage of the MatcherCast<T>() function.
  570. TEST(LinkTest, TestMatcherCast) {
  571. Matcher<const char*> m = MatcherCast<const char*>(_);
  572. EXPECT_TRUE(m.Matches(NULL));
  573. }
  574. #endif // GMOCK_TEST_GMOCK_LINK_TEST_H_