You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

500 lines
14 KiB

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