gmock-nice-strict_test.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. // Copyright 2008, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. #include "gmock/gmock-nice-strict.h"
  30. #include <string>
  31. #include <utility>
  32. #include "gmock/gmock.h"
  33. #include "gtest/gtest-spi.h"
  34. #include "gtest/gtest.h"
  35. // This must not be defined inside the ::testing namespace, or it will
  36. // clash with ::testing::Mock.
  37. class Mock {
  38. public:
  39. Mock() {}
  40. MOCK_METHOD0(DoThis, void());
  41. private:
  42. GTEST_DISALLOW_COPY_AND_ASSIGN_(Mock);
  43. };
  44. namespace testing {
  45. namespace gmock_nice_strict_test {
  46. using testing::GMOCK_FLAG(verbose);
  47. using testing::HasSubstr;
  48. using testing::NaggyMock;
  49. using testing::NiceMock;
  50. using testing::StrictMock;
  51. #if GTEST_HAS_STREAM_REDIRECTION
  52. using testing::internal::CaptureStdout;
  53. using testing::internal::GetCapturedStdout;
  54. #endif
  55. // Class without default constructor.
  56. class NotDefaultConstructible {
  57. public:
  58. explicit NotDefaultConstructible(int) {}
  59. };
  60. class CallsMockMethodInDestructor {
  61. public:
  62. ~CallsMockMethodInDestructor() { OnDestroy(); }
  63. MOCK_METHOD(void, OnDestroy, ());
  64. };
  65. // Defines some mock classes needed by the tests.
  66. class Foo {
  67. public:
  68. virtual ~Foo() {}
  69. virtual void DoThis() = 0;
  70. virtual int DoThat(bool flag) = 0;
  71. };
  72. class MockFoo : public Foo {
  73. public:
  74. MockFoo() {}
  75. void Delete() { delete this; }
  76. MOCK_METHOD0(DoThis, void());
  77. MOCK_METHOD1(DoThat, int(bool flag));
  78. MOCK_METHOD0(ReturnNonDefaultConstructible, NotDefaultConstructible());
  79. private:
  80. GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo);
  81. };
  82. class MockBar {
  83. public:
  84. explicit MockBar(const std::string& s) : str_(s) {}
  85. MockBar(char a1, char a2, std::string a3, std::string a4, int a5, int a6,
  86. const std::string& a7, const std::string& a8, bool a9, bool a10) {
  87. str_ = std::string() + a1 + a2 + a3 + a4 + static_cast<char>(a5) +
  88. static_cast<char>(a6) + a7 + a8 + (a9 ? 'T' : 'F') + (a10 ? 'T' : 'F');
  89. }
  90. virtual ~MockBar() {}
  91. const std::string& str() const { return str_; }
  92. MOCK_METHOD0(This, int());
  93. MOCK_METHOD2(That, std::string(int, bool));
  94. private:
  95. std::string str_;
  96. GTEST_DISALLOW_COPY_AND_ASSIGN_(MockBar);
  97. };
  98. class MockBaz {
  99. public:
  100. class MoveOnly {
  101. public:
  102. MoveOnly() = default;
  103. MoveOnly(const MoveOnly&) = delete;
  104. MoveOnly& operator=(const MoveOnly&) = delete;
  105. MoveOnly(MoveOnly&&) = default;
  106. MoveOnly& operator=(MoveOnly&&) = default;
  107. };
  108. MockBaz(MoveOnly) {}
  109. };
  110. #if GTEST_HAS_STREAM_REDIRECTION
  111. // Tests that a raw mock generates warnings for uninteresting calls.
  112. TEST(RawMockTest, WarningForUninterestingCall) {
  113. const std::string saved_flag = GMOCK_FLAG(verbose);
  114. GMOCK_FLAG(verbose) = "warning";
  115. MockFoo raw_foo;
  116. CaptureStdout();
  117. raw_foo.DoThis();
  118. raw_foo.DoThat(true);
  119. EXPECT_THAT(GetCapturedStdout(),
  120. HasSubstr("Uninteresting mock function call"));
  121. GMOCK_FLAG(verbose) = saved_flag;
  122. }
  123. // Tests that a raw mock generates warnings for uninteresting calls
  124. // that delete the mock object.
  125. TEST(RawMockTest, WarningForUninterestingCallAfterDeath) {
  126. const std::string saved_flag = GMOCK_FLAG(verbose);
  127. GMOCK_FLAG(verbose) = "warning";
  128. MockFoo* const raw_foo = new MockFoo;
  129. ON_CALL(*raw_foo, DoThis())
  130. .WillByDefault(Invoke(raw_foo, &MockFoo::Delete));
  131. CaptureStdout();
  132. raw_foo->DoThis();
  133. EXPECT_THAT(GetCapturedStdout(),
  134. HasSubstr("Uninteresting mock function call"));
  135. GMOCK_FLAG(verbose) = saved_flag;
  136. }
  137. // Tests that a raw mock generates informational logs for
  138. // uninteresting calls.
  139. TEST(RawMockTest, InfoForUninterestingCall) {
  140. MockFoo raw_foo;
  141. const std::string saved_flag = GMOCK_FLAG(verbose);
  142. GMOCK_FLAG(verbose) = "info";
  143. CaptureStdout();
  144. raw_foo.DoThis();
  145. EXPECT_THAT(GetCapturedStdout(),
  146. HasSubstr("Uninteresting mock function call"));
  147. GMOCK_FLAG(verbose) = saved_flag;
  148. }
  149. TEST(RawMockTest, IsNaggy_IsNice_IsStrict) {
  150. MockFoo raw_foo;
  151. EXPECT_TRUE(Mock::IsNaggy(&raw_foo));
  152. EXPECT_FALSE(Mock::IsNice(&raw_foo));
  153. EXPECT_FALSE(Mock::IsStrict(&raw_foo));
  154. }
  155. // Tests that a nice mock generates no warning for uninteresting calls.
  156. TEST(NiceMockTest, NoWarningForUninterestingCall) {
  157. NiceMock<MockFoo> nice_foo;
  158. CaptureStdout();
  159. nice_foo.DoThis();
  160. nice_foo.DoThat(true);
  161. EXPECT_EQ("", GetCapturedStdout());
  162. }
  163. // Tests that a nice mock generates no warning for uninteresting calls
  164. // that delete the mock object.
  165. TEST(NiceMockTest, NoWarningForUninterestingCallAfterDeath) {
  166. NiceMock<MockFoo>* const nice_foo = new NiceMock<MockFoo>;
  167. ON_CALL(*nice_foo, DoThis())
  168. .WillByDefault(Invoke(nice_foo, &MockFoo::Delete));
  169. CaptureStdout();
  170. nice_foo->DoThis();
  171. EXPECT_EQ("", GetCapturedStdout());
  172. }
  173. // Tests that a nice mock generates informational logs for
  174. // uninteresting calls.
  175. TEST(NiceMockTest, InfoForUninterestingCall) {
  176. NiceMock<MockFoo> nice_foo;
  177. const std::string saved_flag = GMOCK_FLAG(verbose);
  178. GMOCK_FLAG(verbose) = "info";
  179. CaptureStdout();
  180. nice_foo.DoThis();
  181. EXPECT_THAT(GetCapturedStdout(),
  182. HasSubstr("Uninteresting mock function call"));
  183. GMOCK_FLAG(verbose) = saved_flag;
  184. }
  185. #endif // GTEST_HAS_STREAM_REDIRECTION
  186. // Tests that a nice mock allows expected calls.
  187. TEST(NiceMockTest, AllowsExpectedCall) {
  188. NiceMock<MockFoo> nice_foo;
  189. EXPECT_CALL(nice_foo, DoThis());
  190. nice_foo.DoThis();
  191. }
  192. // Tests that an unexpected call on a nice mock which returns a
  193. // not-default-constructible type throws an exception and the exception contains
  194. // the method's name.
  195. TEST(NiceMockTest, ThrowsExceptionForUnknownReturnTypes) {
  196. NiceMock<MockFoo> nice_foo;
  197. #if GTEST_HAS_EXCEPTIONS
  198. try {
  199. nice_foo.ReturnNonDefaultConstructible();
  200. FAIL();
  201. } catch (const std::runtime_error& ex) {
  202. EXPECT_THAT(ex.what(), HasSubstr("ReturnNonDefaultConstructible"));
  203. }
  204. #else
  205. EXPECT_DEATH_IF_SUPPORTED({ nice_foo.ReturnNonDefaultConstructible(); }, "");
  206. #endif
  207. }
  208. // Tests that an unexpected call on a nice mock fails.
  209. TEST(NiceMockTest, UnexpectedCallFails) {
  210. NiceMock<MockFoo> nice_foo;
  211. EXPECT_CALL(nice_foo, DoThis()).Times(0);
  212. EXPECT_NONFATAL_FAILURE(nice_foo.DoThis(), "called more times than expected");
  213. }
  214. // Tests that NiceMock works with a mock class that has a non-default
  215. // constructor.
  216. TEST(NiceMockTest, NonDefaultConstructor) {
  217. NiceMock<MockBar> nice_bar("hi");
  218. EXPECT_EQ("hi", nice_bar.str());
  219. nice_bar.This();
  220. nice_bar.That(5, true);
  221. }
  222. // Tests that NiceMock works with a mock class that has a 10-ary
  223. // non-default constructor.
  224. TEST(NiceMockTest, NonDefaultConstructor10) {
  225. NiceMock<MockBar> nice_bar('a', 'b', "c", "d", 'e', 'f',
  226. "g", "h", true, false);
  227. EXPECT_EQ("abcdefghTF", nice_bar.str());
  228. nice_bar.This();
  229. nice_bar.That(5, true);
  230. }
  231. TEST(NiceMockTest, AllowLeak) {
  232. NiceMock<MockFoo>* leaked = new NiceMock<MockFoo>;
  233. Mock::AllowLeak(leaked);
  234. EXPECT_CALL(*leaked, DoThis());
  235. leaked->DoThis();
  236. }
  237. TEST(NiceMockTest, MoveOnlyConstructor) {
  238. NiceMock<MockBaz> nice_baz(MockBaz::MoveOnly{});
  239. }
  240. // Tests that NiceMock<Mock> compiles where Mock is a user-defined
  241. // class (as opposed to ::testing::Mock).
  242. TEST(NiceMockTest, AcceptsClassNamedMock) {
  243. NiceMock< ::Mock> nice;
  244. EXPECT_CALL(nice, DoThis());
  245. nice.DoThis();
  246. }
  247. TEST(NiceMockTest, IsNiceInDestructor) {
  248. {
  249. NiceMock<CallsMockMethodInDestructor> nice_on_destroy;
  250. // Don't add an expectation for the call before the mock goes out of scope.
  251. }
  252. }
  253. TEST(NiceMockTest, IsNaggy_IsNice_IsStrict) {
  254. NiceMock<MockFoo> nice_foo;
  255. EXPECT_FALSE(Mock::IsNaggy(&nice_foo));
  256. EXPECT_TRUE(Mock::IsNice(&nice_foo));
  257. EXPECT_FALSE(Mock::IsStrict(&nice_foo));
  258. }
  259. #if GTEST_HAS_STREAM_REDIRECTION
  260. // Tests that a naggy mock generates warnings for uninteresting calls.
  261. TEST(NaggyMockTest, WarningForUninterestingCall) {
  262. const std::string saved_flag = GMOCK_FLAG(verbose);
  263. GMOCK_FLAG(verbose) = "warning";
  264. NaggyMock<MockFoo> naggy_foo;
  265. CaptureStdout();
  266. naggy_foo.DoThis();
  267. naggy_foo.DoThat(true);
  268. EXPECT_THAT(GetCapturedStdout(),
  269. HasSubstr("Uninteresting mock function call"));
  270. GMOCK_FLAG(verbose) = saved_flag;
  271. }
  272. // Tests that a naggy mock generates a warning for an uninteresting call
  273. // that deletes the mock object.
  274. TEST(NaggyMockTest, WarningForUninterestingCallAfterDeath) {
  275. const std::string saved_flag = GMOCK_FLAG(verbose);
  276. GMOCK_FLAG(verbose) = "warning";
  277. NaggyMock<MockFoo>* const naggy_foo = new NaggyMock<MockFoo>;
  278. ON_CALL(*naggy_foo, DoThis())
  279. .WillByDefault(Invoke(naggy_foo, &MockFoo::Delete));
  280. CaptureStdout();
  281. naggy_foo->DoThis();
  282. EXPECT_THAT(GetCapturedStdout(),
  283. HasSubstr("Uninteresting mock function call"));
  284. GMOCK_FLAG(verbose) = saved_flag;
  285. }
  286. #endif // GTEST_HAS_STREAM_REDIRECTION
  287. // Tests that a naggy mock allows expected calls.
  288. TEST(NaggyMockTest, AllowsExpectedCall) {
  289. NaggyMock<MockFoo> naggy_foo;
  290. EXPECT_CALL(naggy_foo, DoThis());
  291. naggy_foo.DoThis();
  292. }
  293. // Tests that an unexpected call on a naggy mock fails.
  294. TEST(NaggyMockTest, UnexpectedCallFails) {
  295. NaggyMock<MockFoo> naggy_foo;
  296. EXPECT_CALL(naggy_foo, DoThis()).Times(0);
  297. EXPECT_NONFATAL_FAILURE(naggy_foo.DoThis(),
  298. "called more times than expected");
  299. }
  300. // Tests that NaggyMock works with a mock class that has a non-default
  301. // constructor.
  302. TEST(NaggyMockTest, NonDefaultConstructor) {
  303. NaggyMock<MockBar> naggy_bar("hi");
  304. EXPECT_EQ("hi", naggy_bar.str());
  305. naggy_bar.This();
  306. naggy_bar.That(5, true);
  307. }
  308. // Tests that NaggyMock works with a mock class that has a 10-ary
  309. // non-default constructor.
  310. TEST(NaggyMockTest, NonDefaultConstructor10) {
  311. NaggyMock<MockBar> naggy_bar('0', '1', "2", "3", '4', '5',
  312. "6", "7", true, false);
  313. EXPECT_EQ("01234567TF", naggy_bar.str());
  314. naggy_bar.This();
  315. naggy_bar.That(5, true);
  316. }
  317. TEST(NaggyMockTest, AllowLeak) {
  318. NaggyMock<MockFoo>* leaked = new NaggyMock<MockFoo>;
  319. Mock::AllowLeak(leaked);
  320. EXPECT_CALL(*leaked, DoThis());
  321. leaked->DoThis();
  322. }
  323. TEST(NaggyMockTest, MoveOnlyConstructor) {
  324. NaggyMock<MockBaz> naggy_baz(MockBaz::MoveOnly{});
  325. }
  326. // Tests that NaggyMock<Mock> compiles where Mock is a user-defined
  327. // class (as opposed to ::testing::Mock).
  328. TEST(NaggyMockTest, AcceptsClassNamedMock) {
  329. NaggyMock< ::Mock> naggy;
  330. EXPECT_CALL(naggy, DoThis());
  331. naggy.DoThis();
  332. }
  333. TEST(NaggyMockTest, IsNaggyInDestructor) {
  334. const std::string saved_flag = GMOCK_FLAG(verbose);
  335. GMOCK_FLAG(verbose) = "warning";
  336. CaptureStdout();
  337. {
  338. NaggyMock<CallsMockMethodInDestructor> naggy_on_destroy;
  339. // Don't add an expectation for the call before the mock goes out of scope.
  340. }
  341. EXPECT_THAT(GetCapturedStdout(),
  342. HasSubstr("Uninteresting mock function call"));
  343. GMOCK_FLAG(verbose) = saved_flag;
  344. }
  345. TEST(NaggyMockTest, IsNaggy_IsNice_IsStrict) {
  346. NaggyMock<MockFoo> naggy_foo;
  347. EXPECT_TRUE(Mock::IsNaggy(&naggy_foo));
  348. EXPECT_FALSE(Mock::IsNice(&naggy_foo));
  349. EXPECT_FALSE(Mock::IsStrict(&naggy_foo));
  350. }
  351. // Tests that a strict mock allows expected calls.
  352. TEST(StrictMockTest, AllowsExpectedCall) {
  353. StrictMock<MockFoo> strict_foo;
  354. EXPECT_CALL(strict_foo, DoThis());
  355. strict_foo.DoThis();
  356. }
  357. // Tests that an unexpected call on a strict mock fails.
  358. TEST(StrictMockTest, UnexpectedCallFails) {
  359. StrictMock<MockFoo> strict_foo;
  360. EXPECT_CALL(strict_foo, DoThis()).Times(0);
  361. EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
  362. "called more times than expected");
  363. }
  364. // Tests that an uninteresting call on a strict mock fails.
  365. TEST(StrictMockTest, UninterestingCallFails) {
  366. StrictMock<MockFoo> strict_foo;
  367. EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
  368. "Uninteresting mock function call");
  369. }
  370. // Tests that an uninteresting call on a strict mock fails, even if
  371. // the call deletes the mock object.
  372. TEST(StrictMockTest, UninterestingCallFailsAfterDeath) {
  373. StrictMock<MockFoo>* const strict_foo = new StrictMock<MockFoo>;
  374. ON_CALL(*strict_foo, DoThis())
  375. .WillByDefault(Invoke(strict_foo, &MockFoo::Delete));
  376. EXPECT_NONFATAL_FAILURE(strict_foo->DoThis(),
  377. "Uninteresting mock function call");
  378. }
  379. // Tests that StrictMock works with a mock class that has a
  380. // non-default constructor.
  381. TEST(StrictMockTest, NonDefaultConstructor) {
  382. StrictMock<MockBar> strict_bar("hi");
  383. EXPECT_EQ("hi", strict_bar.str());
  384. EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
  385. "Uninteresting mock function call");
  386. }
  387. // Tests that StrictMock works with a mock class that has a 10-ary
  388. // non-default constructor.
  389. TEST(StrictMockTest, NonDefaultConstructor10) {
  390. StrictMock<MockBar> strict_bar('a', 'b', "c", "d", 'e', 'f',
  391. "g", "h", true, false);
  392. EXPECT_EQ("abcdefghTF", strict_bar.str());
  393. EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
  394. "Uninteresting mock function call");
  395. }
  396. TEST(StrictMockTest, AllowLeak) {
  397. StrictMock<MockFoo>* leaked = new StrictMock<MockFoo>;
  398. Mock::AllowLeak(leaked);
  399. EXPECT_CALL(*leaked, DoThis());
  400. leaked->DoThis();
  401. }
  402. TEST(StrictMockTest, MoveOnlyConstructor) {
  403. StrictMock<MockBaz> strict_baz(MockBaz::MoveOnly{});
  404. }
  405. // Tests that StrictMock<Mock> compiles where Mock is a user-defined
  406. // class (as opposed to ::testing::Mock).
  407. TEST(StrictMockTest, AcceptsClassNamedMock) {
  408. StrictMock< ::Mock> strict;
  409. EXPECT_CALL(strict, DoThis());
  410. strict.DoThis();
  411. }
  412. TEST(StrictMockTest, IsStrictInDestructor) {
  413. EXPECT_NONFATAL_FAILURE(
  414. {
  415. StrictMock<CallsMockMethodInDestructor> strict_on_destroy;
  416. // Don't add an expectation for the call before the mock goes out of
  417. // scope.
  418. },
  419. "Uninteresting mock function call");
  420. }
  421. TEST(StrictMockTest, IsNaggy_IsNice_IsStrict) {
  422. StrictMock<MockFoo> strict_foo;
  423. EXPECT_FALSE(Mock::IsNaggy(&strict_foo));
  424. EXPECT_FALSE(Mock::IsNice(&strict_foo));
  425. EXPECT_TRUE(Mock::IsStrict(&strict_foo));
  426. }
  427. } // namespace gmock_nice_strict_test
  428. } // namespace testing