gmock_link_test.h 19 KB

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