gmock-nice-strict_test.cc 15 KB

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