gmock-nice-strict_test.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. Mock(const Mock&) = delete;
  43. Mock& operator=(const Mock&) = delete;
  44. };
  45. namespace testing {
  46. namespace gmock_nice_strict_test {
  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. MockFoo(const MockFoo&) = delete;
  81. MockFoo& operator=(const MockFoo&) = delete;
  82. };
  83. class MockBar {
  84. public:
  85. explicit MockBar(const std::string& s) : str_(s) {}
  86. MockBar(char a1, char a2, std::string a3, std::string a4, int a5, int a6,
  87. const std::string& a7, const std::string& a8, bool a9, bool a10) {
  88. str_ = std::string() + a1 + a2 + a3 + a4 + static_cast<char>(a5) +
  89. static_cast<char>(a6) + a7 + a8 + (a9 ? 'T' : 'F') +
  90. (a10 ? 'T' : 'F');
  91. }
  92. virtual ~MockBar() {}
  93. const std::string& str() const { return str_; }
  94. MOCK_METHOD0(This, int());
  95. MOCK_METHOD2(That, std::string(int, bool));
  96. private:
  97. std::string str_;
  98. MockBar(const MockBar&) = delete;
  99. MockBar& operator=(const MockBar&) = delete;
  100. };
  101. class MockBaz {
  102. public:
  103. class MoveOnly {
  104. public:
  105. MoveOnly() = default;
  106. MoveOnly(const MoveOnly&) = delete;
  107. MoveOnly& operator=(const MoveOnly&) = delete;
  108. MoveOnly(MoveOnly&&) = default;
  109. MoveOnly& operator=(MoveOnly&&) = default;
  110. };
  111. MockBaz(MoveOnly) {}
  112. };
  113. #if GTEST_HAS_STREAM_REDIRECTION
  114. // Tests that a raw mock generates warnings for uninteresting calls.
  115. TEST(RawMockTest, WarningForUninterestingCall) {
  116. const std::string saved_flag = GMOCK_FLAG_GET(verbose);
  117. GMOCK_FLAG_SET(verbose, "warning");
  118. MockFoo raw_foo;
  119. CaptureStdout();
  120. raw_foo.DoThis();
  121. raw_foo.DoThat(true);
  122. EXPECT_THAT(GetCapturedStdout(),
  123. HasSubstr("Uninteresting mock function call"));
  124. GMOCK_FLAG_SET(verbose, saved_flag);
  125. }
  126. // Tests that a raw mock generates warnings for uninteresting calls
  127. // that delete the mock object.
  128. TEST(RawMockTest, WarningForUninterestingCallAfterDeath) {
  129. const std::string saved_flag = GMOCK_FLAG_GET(verbose);
  130. GMOCK_FLAG_SET(verbose, "warning");
  131. MockFoo* const raw_foo = new MockFoo;
  132. ON_CALL(*raw_foo, DoThis()).WillByDefault(Invoke(raw_foo, &MockFoo::Delete));
  133. CaptureStdout();
  134. raw_foo->DoThis();
  135. EXPECT_THAT(GetCapturedStdout(),
  136. HasSubstr("Uninteresting mock function call"));
  137. GMOCK_FLAG_SET(verbose, saved_flag);
  138. }
  139. // Tests that a raw mock generates informational logs for
  140. // uninteresting calls.
  141. TEST(RawMockTest, InfoForUninterestingCall) {
  142. MockFoo raw_foo;
  143. const std::string saved_flag = GMOCK_FLAG_GET(verbose);
  144. GMOCK_FLAG_SET(verbose, "info");
  145. CaptureStdout();
  146. raw_foo.DoThis();
  147. EXPECT_THAT(GetCapturedStdout(),
  148. HasSubstr("Uninteresting mock function call"));
  149. GMOCK_FLAG_SET(verbose, saved_flag);
  150. }
  151. TEST(RawMockTest, IsNaggy_IsNice_IsStrict) {
  152. MockFoo raw_foo;
  153. EXPECT_TRUE(Mock::IsNaggy(&raw_foo));
  154. EXPECT_FALSE(Mock::IsNice(&raw_foo));
  155. EXPECT_FALSE(Mock::IsStrict(&raw_foo));
  156. }
  157. // Tests that a nice mock generates no warning for uninteresting calls.
  158. TEST(NiceMockTest, NoWarningForUninterestingCall) {
  159. NiceMock<MockFoo> nice_foo;
  160. CaptureStdout();
  161. nice_foo.DoThis();
  162. nice_foo.DoThat(true);
  163. EXPECT_EQ("", GetCapturedStdout());
  164. }
  165. // Tests that a nice mock generates no warning for uninteresting calls
  166. // that delete the mock object.
  167. TEST(NiceMockTest, NoWarningForUninterestingCallAfterDeath) {
  168. NiceMock<MockFoo>* const nice_foo = new NiceMock<MockFoo>;
  169. ON_CALL(*nice_foo, DoThis())
  170. .WillByDefault(Invoke(nice_foo, &MockFoo::Delete));
  171. CaptureStdout();
  172. nice_foo->DoThis();
  173. EXPECT_EQ("", GetCapturedStdout());
  174. }
  175. // Tests that a nice mock generates informational logs for
  176. // uninteresting calls.
  177. TEST(NiceMockTest, InfoForUninterestingCall) {
  178. NiceMock<MockFoo> nice_foo;
  179. const std::string saved_flag = GMOCK_FLAG_GET(verbose);
  180. GMOCK_FLAG_SET(verbose, "info");
  181. CaptureStdout();
  182. nice_foo.DoThis();
  183. EXPECT_THAT(GetCapturedStdout(),
  184. HasSubstr("Uninteresting mock function call"));
  185. GMOCK_FLAG_SET(verbose, saved_flag);
  186. }
  187. #endif // GTEST_HAS_STREAM_REDIRECTION
  188. // Tests that a nice mock allows expected calls.
  189. TEST(NiceMockTest, AllowsExpectedCall) {
  190. NiceMock<MockFoo> nice_foo;
  191. EXPECT_CALL(nice_foo, DoThis());
  192. nice_foo.DoThis();
  193. }
  194. // Tests that an unexpected call on a nice mock which returns a
  195. // not-default-constructible type throws an exception and the exception contains
  196. // the method's name.
  197. TEST(NiceMockTest, ThrowsExceptionForUnknownReturnTypes) {
  198. NiceMock<MockFoo> nice_foo;
  199. #if GTEST_HAS_EXCEPTIONS
  200. try {
  201. nice_foo.ReturnNonDefaultConstructible();
  202. FAIL();
  203. } catch (const std::runtime_error& ex) {
  204. EXPECT_THAT(ex.what(), HasSubstr("ReturnNonDefaultConstructible"));
  205. }
  206. #else
  207. EXPECT_DEATH_IF_SUPPORTED({ nice_foo.ReturnNonDefaultConstructible(); }, "");
  208. #endif
  209. }
  210. // Tests that an unexpected call on a nice mock fails.
  211. TEST(NiceMockTest, UnexpectedCallFails) {
  212. NiceMock<MockFoo> nice_foo;
  213. EXPECT_CALL(nice_foo, DoThis()).Times(0);
  214. EXPECT_NONFATAL_FAILURE(nice_foo.DoThis(), "called more times than expected");
  215. }
  216. // Tests that NiceMock works with a mock class that has a non-default
  217. // constructor.
  218. TEST(NiceMockTest, NonDefaultConstructor) {
  219. NiceMock<MockBar> nice_bar("hi");
  220. EXPECT_EQ("hi", nice_bar.str());
  221. nice_bar.This();
  222. nice_bar.That(5, true);
  223. }
  224. // Tests that NiceMock works with a mock class that has a 10-ary
  225. // non-default constructor.
  226. TEST(NiceMockTest, NonDefaultConstructor10) {
  227. NiceMock<MockBar> nice_bar('a', 'b', "c", "d", 'e', 'f', "g", "h", true,
  228. false);
  229. EXPECT_EQ("abcdefghTF", nice_bar.str());
  230. nice_bar.This();
  231. nice_bar.That(5, true);
  232. }
  233. TEST(NiceMockTest, AllowLeak) {
  234. NiceMock<MockFoo>* leaked = new NiceMock<MockFoo>;
  235. Mock::AllowLeak(leaked);
  236. EXPECT_CALL(*leaked, DoThis());
  237. leaked->DoThis();
  238. }
  239. TEST(NiceMockTest, MoveOnlyConstructor) {
  240. NiceMock<MockBaz> nice_baz(MockBaz::MoveOnly{});
  241. }
  242. // Tests that NiceMock<Mock> compiles where Mock is a user-defined
  243. // class (as opposed to ::testing::Mock).
  244. TEST(NiceMockTest, AcceptsClassNamedMock) {
  245. NiceMock< ::Mock> nice;
  246. EXPECT_CALL(nice, DoThis());
  247. nice.DoThis();
  248. }
  249. TEST(NiceMockTest, IsNiceInDestructor) {
  250. {
  251. NiceMock<CallsMockMethodInDestructor> nice_on_destroy;
  252. // Don't add an expectation for the call before the mock goes out of scope.
  253. }
  254. }
  255. TEST(NiceMockTest, IsNaggy_IsNice_IsStrict) {
  256. NiceMock<MockFoo> nice_foo;
  257. EXPECT_FALSE(Mock::IsNaggy(&nice_foo));
  258. EXPECT_TRUE(Mock::IsNice(&nice_foo));
  259. EXPECT_FALSE(Mock::IsStrict(&nice_foo));
  260. }
  261. #if GTEST_HAS_STREAM_REDIRECTION
  262. // Tests that a naggy mock generates warnings for uninteresting calls.
  263. TEST(NaggyMockTest, WarningForUninterestingCall) {
  264. const std::string saved_flag = GMOCK_FLAG_GET(verbose);
  265. GMOCK_FLAG_SET(verbose, "warning");
  266. NaggyMock<MockFoo> naggy_foo;
  267. CaptureStdout();
  268. naggy_foo.DoThis();
  269. naggy_foo.DoThat(true);
  270. EXPECT_THAT(GetCapturedStdout(),
  271. HasSubstr("Uninteresting mock function call"));
  272. GMOCK_FLAG_SET(verbose, saved_flag);
  273. }
  274. // Tests that a naggy mock generates a warning for an uninteresting call
  275. // that deletes the mock object.
  276. TEST(NaggyMockTest, WarningForUninterestingCallAfterDeath) {
  277. const std::string saved_flag = GMOCK_FLAG_GET(verbose);
  278. GMOCK_FLAG_SET(verbose, "warning");
  279. NaggyMock<MockFoo>* const naggy_foo = new NaggyMock<MockFoo>;
  280. ON_CALL(*naggy_foo, DoThis())
  281. .WillByDefault(Invoke(naggy_foo, &MockFoo::Delete));
  282. CaptureStdout();
  283. naggy_foo->DoThis();
  284. EXPECT_THAT(GetCapturedStdout(),
  285. HasSubstr("Uninteresting mock function call"));
  286. GMOCK_FLAG_SET(verbose, saved_flag);
  287. }
  288. #endif // GTEST_HAS_STREAM_REDIRECTION
  289. // Tests that a naggy mock allows expected calls.
  290. TEST(NaggyMockTest, AllowsExpectedCall) {
  291. NaggyMock<MockFoo> naggy_foo;
  292. EXPECT_CALL(naggy_foo, DoThis());
  293. naggy_foo.DoThis();
  294. }
  295. // Tests that an unexpected call on a naggy mock fails.
  296. TEST(NaggyMockTest, UnexpectedCallFails) {
  297. NaggyMock<MockFoo> naggy_foo;
  298. EXPECT_CALL(naggy_foo, DoThis()).Times(0);
  299. EXPECT_NONFATAL_FAILURE(naggy_foo.DoThis(),
  300. "called more times than expected");
  301. }
  302. // Tests that NaggyMock works with a mock class that has a non-default
  303. // constructor.
  304. TEST(NaggyMockTest, NonDefaultConstructor) {
  305. NaggyMock<MockBar> naggy_bar("hi");
  306. EXPECT_EQ("hi", naggy_bar.str());
  307. naggy_bar.This();
  308. naggy_bar.That(5, true);
  309. }
  310. // Tests that NaggyMock works with a mock class that has a 10-ary
  311. // non-default constructor.
  312. TEST(NaggyMockTest, NonDefaultConstructor10) {
  313. NaggyMock<MockBar> naggy_bar('0', '1', "2", "3", '4', '5', "6", "7", true,
  314. false);
  315. EXPECT_EQ("01234567TF", naggy_bar.str());
  316. naggy_bar.This();
  317. naggy_bar.That(5, true);
  318. }
  319. TEST(NaggyMockTest, AllowLeak) {
  320. NaggyMock<MockFoo>* leaked = new NaggyMock<MockFoo>;
  321. Mock::AllowLeak(leaked);
  322. EXPECT_CALL(*leaked, DoThis());
  323. leaked->DoThis();
  324. }
  325. TEST(NaggyMockTest, MoveOnlyConstructor) {
  326. NaggyMock<MockBaz> naggy_baz(MockBaz::MoveOnly{});
  327. }
  328. // Tests that NaggyMock<Mock> compiles where Mock is a user-defined
  329. // class (as opposed to ::testing::Mock).
  330. TEST(NaggyMockTest, AcceptsClassNamedMock) {
  331. NaggyMock< ::Mock> naggy;
  332. EXPECT_CALL(naggy, DoThis());
  333. naggy.DoThis();
  334. }
  335. TEST(NaggyMockTest, IsNaggyInDestructor) {
  336. const std::string saved_flag = GMOCK_FLAG_GET(verbose);
  337. GMOCK_FLAG_SET(verbose, "warning");
  338. CaptureStdout();
  339. {
  340. NaggyMock<CallsMockMethodInDestructor> naggy_on_destroy;
  341. // Don't add an expectation for the call before the mock goes out of scope.
  342. }
  343. EXPECT_THAT(GetCapturedStdout(),
  344. HasSubstr("Uninteresting mock function call"));
  345. GMOCK_FLAG_SET(verbose, saved_flag);
  346. }
  347. TEST(NaggyMockTest, IsNaggy_IsNice_IsStrict) {
  348. NaggyMock<MockFoo> naggy_foo;
  349. EXPECT_TRUE(Mock::IsNaggy(&naggy_foo));
  350. EXPECT_FALSE(Mock::IsNice(&naggy_foo));
  351. EXPECT_FALSE(Mock::IsStrict(&naggy_foo));
  352. }
  353. // Tests that a strict mock allows expected calls.
  354. TEST(StrictMockTest, AllowsExpectedCall) {
  355. StrictMock<MockFoo> strict_foo;
  356. EXPECT_CALL(strict_foo, DoThis());
  357. strict_foo.DoThis();
  358. }
  359. // Tests that an unexpected call on a strict mock fails.
  360. TEST(StrictMockTest, UnexpectedCallFails) {
  361. StrictMock<MockFoo> strict_foo;
  362. EXPECT_CALL(strict_foo, DoThis()).Times(0);
  363. EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
  364. "called more times than expected");
  365. }
  366. // Tests that an uninteresting call on a strict mock fails.
  367. TEST(StrictMockTest, UninterestingCallFails) {
  368. StrictMock<MockFoo> strict_foo;
  369. EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
  370. "Uninteresting mock function call");
  371. }
  372. // Tests that an uninteresting call on a strict mock fails, even if
  373. // the call deletes the mock object.
  374. TEST(StrictMockTest, UninterestingCallFailsAfterDeath) {
  375. StrictMock<MockFoo>* const strict_foo = new StrictMock<MockFoo>;
  376. ON_CALL(*strict_foo, DoThis())
  377. .WillByDefault(Invoke(strict_foo, &MockFoo::Delete));
  378. EXPECT_NONFATAL_FAILURE(strict_foo->DoThis(),
  379. "Uninteresting mock function call");
  380. }
  381. // Tests that StrictMock works with a mock class that has a
  382. // non-default constructor.
  383. TEST(StrictMockTest, NonDefaultConstructor) {
  384. StrictMock<MockBar> strict_bar("hi");
  385. EXPECT_EQ("hi", strict_bar.str());
  386. EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
  387. "Uninteresting mock function call");
  388. }
  389. // Tests that StrictMock works with a mock class that has a 10-ary
  390. // non-default constructor.
  391. TEST(StrictMockTest, NonDefaultConstructor10) {
  392. StrictMock<MockBar> strict_bar('a', 'b', "c", "d", 'e', 'f', "g", "h", true,
  393. false);
  394. EXPECT_EQ("abcdefghTF", strict_bar.str());
  395. EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
  396. "Uninteresting mock function call");
  397. }
  398. TEST(StrictMockTest, AllowLeak) {
  399. StrictMock<MockFoo>* leaked = new StrictMock<MockFoo>;
  400. Mock::AllowLeak(leaked);
  401. EXPECT_CALL(*leaked, DoThis());
  402. leaked->DoThis();
  403. }
  404. TEST(StrictMockTest, MoveOnlyConstructor) {
  405. StrictMock<MockBaz> strict_baz(MockBaz::MoveOnly{});
  406. }
  407. // Tests that StrictMock<Mock> compiles where Mock is a user-defined
  408. // class (as opposed to ::testing::Mock).
  409. TEST(StrictMockTest, AcceptsClassNamedMock) {
  410. StrictMock< ::Mock> strict;
  411. EXPECT_CALL(strict, DoThis());
  412. strict.DoThis();
  413. }
  414. TEST(StrictMockTest, IsStrictInDestructor) {
  415. EXPECT_NONFATAL_FAILURE(
  416. {
  417. StrictMock<CallsMockMethodInDestructor> strict_on_destroy;
  418. // Don't add an expectation for the call before the mock goes out of
  419. // scope.
  420. },
  421. "Uninteresting mock function call");
  422. }
  423. TEST(StrictMockTest, IsNaggy_IsNice_IsStrict) {
  424. StrictMock<MockFoo> strict_foo;
  425. EXPECT_FALSE(Mock::IsNaggy(&strict_foo));
  426. EXPECT_FALSE(Mock::IsNice(&strict_foo));
  427. EXPECT_TRUE(Mock::IsStrict(&strict_foo));
  428. }
  429. } // namespace gmock_nice_strict_test
  430. } // namespace testing