gmock-nice-strict_test.cc 15 KB

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