?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

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