gmock-nice-strict_test.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. //
  30. // Author: wan@google.com (Zhanyong Wan)
  31. #include "gmock/gmock-generated-nice-strict.h"
  32. #include <string>
  33. #include "gmock/gmock.h"
  34. #include "gtest/gtest.h"
  35. #include "gtest/gtest-spi.h"
  36. // This must not be defined inside the ::testing namespace, or it will
  37. // clash with ::testing::Mock.
  38. class Mock {
  39. public:
  40. Mock() {}
  41. MOCK_METHOD0(DoThis, void());
  42. private:
  43. GTEST_DISALLOW_COPY_AND_ASSIGN_(Mock);
  44. };
  45. namespace testing {
  46. namespace gmock_nice_strict_test {
  47. using testing::internal::string;
  48. using testing::GMOCK_FLAG(verbose);
  49. using testing::HasSubstr;
  50. using testing::NaggyMock;
  51. using testing::NiceMock;
  52. using testing::StrictMock;
  53. #if GTEST_HAS_STREAM_REDIRECTION
  54. using testing::internal::CaptureStdout;
  55. using testing::internal::GetCapturedStdout;
  56. #endif
  57. // Defines some mock classes needed by the tests.
  58. class Foo {
  59. public:
  60. virtual ~Foo() {}
  61. virtual void DoThis() = 0;
  62. virtual int DoThat(bool flag) = 0;
  63. };
  64. class MockFoo : public Foo {
  65. public:
  66. MockFoo() {}
  67. void Delete() { delete this; }
  68. MOCK_METHOD0(DoThis, void());
  69. MOCK_METHOD1(DoThat, int(bool flag));
  70. private:
  71. GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo);
  72. };
  73. class MockBar {
  74. public:
  75. explicit MockBar(const string& s) : str_(s) {}
  76. MockBar(char a1, char a2, string a3, string a4, int a5, int a6,
  77. const string& a7, const string& a8, bool a9, bool a10) {
  78. str_ = string() + a1 + a2 + a3 + a4 + static_cast<char>(a5) +
  79. static_cast<char>(a6) + a7 + a8 + (a9 ? 'T' : 'F') + (a10 ? 'T' : 'F');
  80. }
  81. virtual ~MockBar() {}
  82. const string& str() const { return str_; }
  83. MOCK_METHOD0(This, int());
  84. MOCK_METHOD2(That, string(int, bool));
  85. private:
  86. string str_;
  87. GTEST_DISALLOW_COPY_AND_ASSIGN_(MockBar);
  88. };
  89. #if GTEST_HAS_STREAM_REDIRECTION
  90. // Tests that a raw mock generates warnings for uninteresting calls.
  91. TEST(RawMockTest, WarningForUninterestingCall) {
  92. const string saved_flag = GMOCK_FLAG(verbose);
  93. GMOCK_FLAG(verbose) = "warning";
  94. MockFoo raw_foo;
  95. CaptureStdout();
  96. raw_foo.DoThis();
  97. raw_foo.DoThat(true);
  98. EXPECT_THAT(GetCapturedStdout(),
  99. HasSubstr("Uninteresting mock function call"));
  100. GMOCK_FLAG(verbose) = saved_flag;
  101. }
  102. // Tests that a raw mock generates warnings for uninteresting calls
  103. // that delete the mock object.
  104. TEST(RawMockTest, WarningForUninterestingCallAfterDeath) {
  105. const string saved_flag = GMOCK_FLAG(verbose);
  106. GMOCK_FLAG(verbose) = "warning";
  107. MockFoo* const raw_foo = new MockFoo;
  108. ON_CALL(*raw_foo, DoThis())
  109. .WillByDefault(Invoke(raw_foo, &MockFoo::Delete));
  110. CaptureStdout();
  111. raw_foo->DoThis();
  112. EXPECT_THAT(GetCapturedStdout(),
  113. HasSubstr("Uninteresting mock function call"));
  114. GMOCK_FLAG(verbose) = saved_flag;
  115. }
  116. // Tests that a raw mock generates informational logs for
  117. // uninteresting calls.
  118. TEST(RawMockTest, InfoForUninterestingCall) {
  119. MockFoo raw_foo;
  120. const string saved_flag = GMOCK_FLAG(verbose);
  121. GMOCK_FLAG(verbose) = "info";
  122. CaptureStdout();
  123. raw_foo.DoThis();
  124. EXPECT_THAT(GetCapturedStdout(),
  125. HasSubstr("Uninteresting mock function call"));
  126. GMOCK_FLAG(verbose) = saved_flag;
  127. }
  128. // Tests that a nice mock generates no warning for uninteresting calls.
  129. TEST(NiceMockTest, NoWarningForUninterestingCall) {
  130. NiceMock<MockFoo> nice_foo;
  131. CaptureStdout();
  132. nice_foo.DoThis();
  133. nice_foo.DoThat(true);
  134. EXPECT_EQ("", GetCapturedStdout());
  135. }
  136. // Tests that a nice mock generates no warning for uninteresting calls
  137. // that delete the mock object.
  138. TEST(NiceMockTest, NoWarningForUninterestingCallAfterDeath) {
  139. NiceMock<MockFoo>* const nice_foo = new NiceMock<MockFoo>;
  140. ON_CALL(*nice_foo, DoThis())
  141. .WillByDefault(Invoke(nice_foo, &MockFoo::Delete));
  142. CaptureStdout();
  143. nice_foo->DoThis();
  144. EXPECT_EQ("", GetCapturedStdout());
  145. }
  146. // Tests that a nice mock generates informational logs for
  147. // uninteresting calls.
  148. TEST(NiceMockTest, InfoForUninterestingCall) {
  149. NiceMock<MockFoo> nice_foo;
  150. const string saved_flag = GMOCK_FLAG(verbose);
  151. GMOCK_FLAG(verbose) = "info";
  152. CaptureStdout();
  153. nice_foo.DoThis();
  154. EXPECT_THAT(GetCapturedStdout(),
  155. HasSubstr("Uninteresting mock function call"));
  156. GMOCK_FLAG(verbose) = saved_flag;
  157. }
  158. #endif // GTEST_HAS_STREAM_REDIRECTION
  159. // Tests that a nice mock allows expected calls.
  160. TEST(NiceMockTest, AllowsExpectedCall) {
  161. NiceMock<MockFoo> nice_foo;
  162. EXPECT_CALL(nice_foo, DoThis());
  163. nice_foo.DoThis();
  164. }
  165. // Tests that an unexpected call on a nice mock fails.
  166. TEST(NiceMockTest, UnexpectedCallFails) {
  167. NiceMock<MockFoo> nice_foo;
  168. EXPECT_CALL(nice_foo, DoThis()).Times(0);
  169. EXPECT_NONFATAL_FAILURE(nice_foo.DoThis(), "called more times than expected");
  170. }
  171. // Tests that NiceMock works with a mock class that has a non-default
  172. // constructor.
  173. TEST(NiceMockTest, NonDefaultConstructor) {
  174. NiceMock<MockBar> nice_bar("hi");
  175. EXPECT_EQ("hi", nice_bar.str());
  176. nice_bar.This();
  177. nice_bar.That(5, true);
  178. }
  179. // Tests that NiceMock works with a mock class that has a 10-ary
  180. // non-default constructor.
  181. TEST(NiceMockTest, NonDefaultConstructor10) {
  182. NiceMock<MockBar> nice_bar('a', 'b', "c", "d", 'e', 'f',
  183. "g", "h", true, false);
  184. EXPECT_EQ("abcdefghTF", nice_bar.str());
  185. nice_bar.This();
  186. nice_bar.That(5, true);
  187. }
  188. #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
  189. // Tests that NiceMock<Mock> compiles where Mock is a user-defined
  190. // class (as opposed to ::testing::Mock). We had to work around an
  191. // MSVC 8.0 bug that caused the symbol Mock used in the definition of
  192. // NiceMock to be looked up in the wrong context, and this test
  193. // ensures that our fix works.
  194. //
  195. // We have to skip this test on Symbian and Windows Mobile, as it
  196. // causes the program to crash there, for reasons unclear to us yet.
  197. TEST(NiceMockTest, AcceptsClassNamedMock) {
  198. NiceMock< ::Mock> nice;
  199. EXPECT_CALL(nice, DoThis());
  200. nice.DoThis();
  201. }
  202. #endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
  203. #if GTEST_HAS_STREAM_REDIRECTION
  204. // Tests that a naggy mock generates warnings for uninteresting calls.
  205. TEST(NaggyMockTest, WarningForUninterestingCall) {
  206. const string saved_flag = GMOCK_FLAG(verbose);
  207. GMOCK_FLAG(verbose) = "warning";
  208. NaggyMock<MockFoo> naggy_foo;
  209. CaptureStdout();
  210. naggy_foo.DoThis();
  211. naggy_foo.DoThat(true);
  212. EXPECT_THAT(GetCapturedStdout(),
  213. HasSubstr("Uninteresting mock function call"));
  214. GMOCK_FLAG(verbose) = saved_flag;
  215. }
  216. // Tests that a naggy mock generates a warning for an uninteresting call
  217. // that deletes the mock object.
  218. TEST(NaggyMockTest, WarningForUninterestingCallAfterDeath) {
  219. const string saved_flag = GMOCK_FLAG(verbose);
  220. GMOCK_FLAG(verbose) = "warning";
  221. NaggyMock<MockFoo>* const naggy_foo = new NaggyMock<MockFoo>;
  222. ON_CALL(*naggy_foo, DoThis())
  223. .WillByDefault(Invoke(naggy_foo, &MockFoo::Delete));
  224. CaptureStdout();
  225. naggy_foo->DoThis();
  226. EXPECT_THAT(GetCapturedStdout(),
  227. HasSubstr("Uninteresting mock function call"));
  228. GMOCK_FLAG(verbose) = saved_flag;
  229. }
  230. #endif // GTEST_HAS_STREAM_REDIRECTION
  231. // Tests that a naggy mock allows expected calls.
  232. TEST(NaggyMockTest, AllowsExpectedCall) {
  233. NaggyMock<MockFoo> naggy_foo;
  234. EXPECT_CALL(naggy_foo, DoThis());
  235. naggy_foo.DoThis();
  236. }
  237. // Tests that an unexpected call on a naggy mock fails.
  238. TEST(NaggyMockTest, UnexpectedCallFails) {
  239. NaggyMock<MockFoo> naggy_foo;
  240. EXPECT_CALL(naggy_foo, DoThis()).Times(0);
  241. EXPECT_NONFATAL_FAILURE(naggy_foo.DoThis(),
  242. "called more times than expected");
  243. }
  244. // Tests that NaggyMock works with a mock class that has a non-default
  245. // constructor.
  246. TEST(NaggyMockTest, NonDefaultConstructor) {
  247. NaggyMock<MockBar> naggy_bar("hi");
  248. EXPECT_EQ("hi", naggy_bar.str());
  249. naggy_bar.This();
  250. naggy_bar.That(5, true);
  251. }
  252. // Tests that NaggyMock works with a mock class that has a 10-ary
  253. // non-default constructor.
  254. TEST(NaggyMockTest, NonDefaultConstructor10) {
  255. NaggyMock<MockBar> naggy_bar('0', '1', "2", "3", '4', '5',
  256. "6", "7", true, false);
  257. EXPECT_EQ("01234567TF", naggy_bar.str());
  258. naggy_bar.This();
  259. naggy_bar.That(5, true);
  260. }
  261. #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
  262. // Tests that NaggyMock<Mock> compiles where Mock is a user-defined
  263. // class (as opposed to ::testing::Mock). We had to work around an
  264. // MSVC 8.0 bug that caused the symbol Mock used in the definition of
  265. // NaggyMock to be looked up in the wrong context, and this test
  266. // ensures that our fix works.
  267. //
  268. // We have to skip this test on Symbian and Windows Mobile, as it
  269. // causes the program to crash there, for reasons unclear to us yet.
  270. TEST(NaggyMockTest, AcceptsClassNamedMock) {
  271. NaggyMock< ::Mock> naggy;
  272. EXPECT_CALL(naggy, DoThis());
  273. naggy.DoThis();
  274. }
  275. #endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
  276. // Tests that a strict mock allows expected calls.
  277. TEST(StrictMockTest, AllowsExpectedCall) {
  278. StrictMock<MockFoo> strict_foo;
  279. EXPECT_CALL(strict_foo, DoThis());
  280. strict_foo.DoThis();
  281. }
  282. // Tests that an unexpected call on a strict mock fails.
  283. TEST(StrictMockTest, UnexpectedCallFails) {
  284. StrictMock<MockFoo> strict_foo;
  285. EXPECT_CALL(strict_foo, DoThis()).Times(0);
  286. EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
  287. "called more times than expected");
  288. }
  289. // Tests that an uninteresting call on a strict mock fails.
  290. TEST(StrictMockTest, UninterestingCallFails) {
  291. StrictMock<MockFoo> strict_foo;
  292. EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
  293. "Uninteresting mock function call");
  294. }
  295. // Tests that an uninteresting call on a strict mock fails, even if
  296. // the call deletes the mock object.
  297. TEST(StrictMockTest, UninterestingCallFailsAfterDeath) {
  298. StrictMock<MockFoo>* const strict_foo = new StrictMock<MockFoo>;
  299. ON_CALL(*strict_foo, DoThis())
  300. .WillByDefault(Invoke(strict_foo, &MockFoo::Delete));
  301. EXPECT_NONFATAL_FAILURE(strict_foo->DoThis(),
  302. "Uninteresting mock function call");
  303. }
  304. // Tests that StrictMock works with a mock class that has a
  305. // non-default constructor.
  306. TEST(StrictMockTest, NonDefaultConstructor) {
  307. StrictMock<MockBar> strict_bar("hi");
  308. EXPECT_EQ("hi", strict_bar.str());
  309. EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
  310. "Uninteresting mock function call");
  311. }
  312. // Tests that StrictMock works with a mock class that has a 10-ary
  313. // non-default constructor.
  314. TEST(StrictMockTest, NonDefaultConstructor10) {
  315. StrictMock<MockBar> strict_bar('a', 'b', "c", "d", 'e', 'f',
  316. "g", "h", true, false);
  317. EXPECT_EQ("abcdefghTF", strict_bar.str());
  318. EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
  319. "Uninteresting mock function call");
  320. }
  321. #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
  322. // Tests that StrictMock<Mock> compiles where Mock is a user-defined
  323. // class (as opposed to ::testing::Mock). We had to work around an
  324. // MSVC 8.0 bug that caused the symbol Mock used in the definition of
  325. // StrictMock to be looked up in the wrong context, and this test
  326. // ensures that our fix works.
  327. //
  328. // We have to skip this test on Symbian and Windows Mobile, as it
  329. // causes the program to crash there, for reasons unclear to us yet.
  330. TEST(StrictMockTest, AcceptsClassNamedMock) {
  331. StrictMock< ::Mock> strict;
  332. EXPECT_CALL(strict, DoThis());
  333. strict.DoThis();
  334. }
  335. #endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
  336. } // namespace gmock_nice_strict_test
  337. } // namespace testing