Home | History | Annotate | Download | only in test
      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 (at) google.com (Zhanyong Wan)
     31 
     32 #include "gmock/gmock-generated-nice-strict.h"
     33 
     34 #include <string>
     35 #include "gmock/gmock.h"
     36 #include "gtest/gtest.h"
     37 #include "gtest/gtest-spi.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_HAS_STREAM_REDIRECTION
    118 
    119 // Tests that a raw mock generates warnings for uninteresting calls.
    120 TEST(RawMockTest, WarningForUninterestingCall) {
    121   const std::string saved_flag = GMOCK_FLAG(verbose);
    122   GMOCK_FLAG(verbose) = "warning";
    123 
    124   MockFoo raw_foo;
    125 
    126   CaptureStdout();
    127   raw_foo.DoThis();
    128   raw_foo.DoThat(true);
    129   EXPECT_THAT(GetCapturedStdout(),
    130               HasSubstr("Uninteresting mock function call"));
    131 
    132   GMOCK_FLAG(verbose) = saved_flag;
    133 }
    134 
    135 // Tests that a raw mock generates warnings for uninteresting calls
    136 // that delete the mock object.
    137 TEST(RawMockTest, WarningForUninterestingCallAfterDeath) {
    138   const std::string saved_flag = GMOCK_FLAG(verbose);
    139   GMOCK_FLAG(verbose) = "warning";
    140 
    141   MockFoo* const raw_foo = new MockFoo;
    142 
    143   ON_CALL(*raw_foo, DoThis())
    144       .WillByDefault(Invoke(raw_foo, &MockFoo::Delete));
    145 
    146   CaptureStdout();
    147   raw_foo->DoThis();
    148   EXPECT_THAT(GetCapturedStdout(),
    149               HasSubstr("Uninteresting mock function call"));
    150 
    151   GMOCK_FLAG(verbose) = saved_flag;
    152 }
    153 
    154 // Tests that a raw mock generates informational logs for
    155 // uninteresting calls.
    156 TEST(RawMockTest, InfoForUninterestingCall) {
    157   MockFoo raw_foo;
    158 
    159   const std::string saved_flag = GMOCK_FLAG(verbose);
    160   GMOCK_FLAG(verbose) = "info";
    161   CaptureStdout();
    162   raw_foo.DoThis();
    163   EXPECT_THAT(GetCapturedStdout(),
    164               HasSubstr("Uninteresting mock function call"));
    165 
    166   GMOCK_FLAG(verbose) = saved_flag;
    167 }
    168 
    169 // Tests that a nice mock generates no warning for uninteresting calls.
    170 TEST(NiceMockTest, NoWarningForUninterestingCall) {
    171   NiceMock<MockFoo> nice_foo;
    172 
    173   CaptureStdout();
    174   nice_foo.DoThis();
    175   nice_foo.DoThat(true);
    176   EXPECT_EQ("", GetCapturedStdout());
    177 }
    178 
    179 // Tests that a nice mock generates no warning for uninteresting calls
    180 // that delete the mock object.
    181 TEST(NiceMockTest, NoWarningForUninterestingCallAfterDeath) {
    182   NiceMock<MockFoo>* const nice_foo = new NiceMock<MockFoo>;
    183 
    184   ON_CALL(*nice_foo, DoThis())
    185       .WillByDefault(Invoke(nice_foo, &MockFoo::Delete));
    186 
    187   CaptureStdout();
    188   nice_foo->DoThis();
    189   EXPECT_EQ("", GetCapturedStdout());
    190 }
    191 
    192 // Tests that a nice mock generates informational logs for
    193 // uninteresting calls.
    194 TEST(NiceMockTest, InfoForUninterestingCall) {
    195   NiceMock<MockFoo> nice_foo;
    196 
    197   const std::string saved_flag = GMOCK_FLAG(verbose);
    198   GMOCK_FLAG(verbose) = "info";
    199   CaptureStdout();
    200   nice_foo.DoThis();
    201   EXPECT_THAT(GetCapturedStdout(),
    202               HasSubstr("Uninteresting mock function call"));
    203 
    204   GMOCK_FLAG(verbose) = saved_flag;
    205 }
    206 
    207 #endif  // GTEST_HAS_STREAM_REDIRECTION
    208 
    209 // Tests that a nice mock allows expected calls.
    210 TEST(NiceMockTest, AllowsExpectedCall) {
    211   NiceMock<MockFoo> nice_foo;
    212 
    213   EXPECT_CALL(nice_foo, DoThis());
    214   nice_foo.DoThis();
    215 }
    216 
    217 // Tests that an unexpected call on a nice mock which returns a not-default-constructible
    218 // type throws an exception and the exception contains the method's name.
    219 TEST(NiceMockTest, ThrowsExceptionForUnknownReturnTypes) {
    220   NiceMock<MockFoo> nice_foo;
    221 #if GTEST_HAS_EXCEPTIONS
    222   try {
    223     nice_foo.ReturnNonDefaultConstructible();
    224     FAIL();
    225   } catch (const std::runtime_error& ex) {
    226     EXPECT_THAT(ex.what(), HasSubstr("ReturnNonDefaultConstructible"));
    227   }
    228 #else
    229   EXPECT_DEATH_IF_SUPPORTED({ nice_foo.ReturnNonDefaultConstructible(); }, "");
    230 #endif
    231 }
    232 
    233 // Tests that an unexpected call on a nice mock fails.
    234 TEST(NiceMockTest, UnexpectedCallFails) {
    235   NiceMock<MockFoo> nice_foo;
    236 
    237   EXPECT_CALL(nice_foo, DoThis()).Times(0);
    238   EXPECT_NONFATAL_FAILURE(nice_foo.DoThis(), "called more times than expected");
    239 }
    240 
    241 // Tests that NiceMock works with a mock class that has a non-default
    242 // constructor.
    243 TEST(NiceMockTest, NonDefaultConstructor) {
    244   NiceMock<MockBar> nice_bar("hi");
    245   EXPECT_EQ("hi", nice_bar.str());
    246 
    247   nice_bar.This();
    248   nice_bar.That(5, true);
    249 }
    250 
    251 // Tests that NiceMock works with a mock class that has a 10-ary
    252 // non-default constructor.
    253 TEST(NiceMockTest, NonDefaultConstructor10) {
    254   NiceMock<MockBar> nice_bar('a', 'b', "c", "d", 'e', 'f',
    255                              "g", "h", true, false);
    256   EXPECT_EQ("abcdefghTF", nice_bar.str());
    257 
    258   nice_bar.This();
    259   nice_bar.That(5, true);
    260 }
    261 
    262 #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
    263 // Tests that NiceMock<Mock> compiles where Mock is a user-defined
    264 // class (as opposed to ::testing::Mock).  We had to work around an
    265 // MSVC 8.0 bug that caused the symbol Mock used in the definition of
    266 // NiceMock to be looked up in the wrong context, and this test
    267 // ensures that our fix works.
    268 //
    269 // We have to skip this test on Symbian and Windows Mobile, as it
    270 // causes the program to crash there, for reasons unclear to us yet.
    271 TEST(NiceMockTest, AcceptsClassNamedMock) {
    272   NiceMock< ::Mock> nice;
    273   EXPECT_CALL(nice, DoThis());
    274   nice.DoThis();
    275 }
    276 #endif  // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
    277 
    278 #if GTEST_HAS_STREAM_REDIRECTION
    279 
    280 // Tests that a naggy mock generates warnings for uninteresting calls.
    281 TEST(NaggyMockTest, WarningForUninterestingCall) {
    282   const std::string saved_flag = GMOCK_FLAG(verbose);
    283   GMOCK_FLAG(verbose) = "warning";
    284 
    285   NaggyMock<MockFoo> naggy_foo;
    286 
    287   CaptureStdout();
    288   naggy_foo.DoThis();
    289   naggy_foo.DoThat(true);
    290   EXPECT_THAT(GetCapturedStdout(),
    291               HasSubstr("Uninteresting mock function call"));
    292 
    293   GMOCK_FLAG(verbose) = saved_flag;
    294 }
    295 
    296 // Tests that a naggy mock generates a warning for an uninteresting call
    297 // that deletes the mock object.
    298 TEST(NaggyMockTest, WarningForUninterestingCallAfterDeath) {
    299   const std::string saved_flag = GMOCK_FLAG(verbose);
    300   GMOCK_FLAG(verbose) = "warning";
    301 
    302   NaggyMock<MockFoo>* const naggy_foo = new NaggyMock<MockFoo>;
    303 
    304   ON_CALL(*naggy_foo, DoThis())
    305       .WillByDefault(Invoke(naggy_foo, &MockFoo::Delete));
    306 
    307   CaptureStdout();
    308   naggy_foo->DoThis();
    309   EXPECT_THAT(GetCapturedStdout(),
    310               HasSubstr("Uninteresting mock function call"));
    311 
    312   GMOCK_FLAG(verbose) = saved_flag;
    313 }
    314 
    315 #endif  // GTEST_HAS_STREAM_REDIRECTION
    316 
    317 // Tests that a naggy mock allows expected calls.
    318 TEST(NaggyMockTest, AllowsExpectedCall) {
    319   NaggyMock<MockFoo> naggy_foo;
    320 
    321   EXPECT_CALL(naggy_foo, DoThis());
    322   naggy_foo.DoThis();
    323 }
    324 
    325 // Tests that an unexpected call on a naggy mock fails.
    326 TEST(NaggyMockTest, UnexpectedCallFails) {
    327   NaggyMock<MockFoo> naggy_foo;
    328 
    329   EXPECT_CALL(naggy_foo, DoThis()).Times(0);
    330   EXPECT_NONFATAL_FAILURE(naggy_foo.DoThis(),
    331                           "called more times than expected");
    332 }
    333 
    334 // Tests that NaggyMock works with a mock class that has a non-default
    335 // constructor.
    336 TEST(NaggyMockTest, NonDefaultConstructor) {
    337   NaggyMock<MockBar> naggy_bar("hi");
    338   EXPECT_EQ("hi", naggy_bar.str());
    339 
    340   naggy_bar.This();
    341   naggy_bar.That(5, true);
    342 }
    343 
    344 // Tests that NaggyMock works with a mock class that has a 10-ary
    345 // non-default constructor.
    346 TEST(NaggyMockTest, NonDefaultConstructor10) {
    347   NaggyMock<MockBar> naggy_bar('0', '1', "2", "3", '4', '5',
    348                                "6", "7", true, false);
    349   EXPECT_EQ("01234567TF", naggy_bar.str());
    350 
    351   naggy_bar.This();
    352   naggy_bar.That(5, true);
    353 }
    354 
    355 #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
    356 // Tests that NaggyMock<Mock> compiles where Mock is a user-defined
    357 // class (as opposed to ::testing::Mock).  We had to work around an
    358 // MSVC 8.0 bug that caused the symbol Mock used in the definition of
    359 // NaggyMock to be looked up in the wrong context, and this test
    360 // ensures that our fix works.
    361 //
    362 // We have to skip this test on Symbian and Windows Mobile, as it
    363 // causes the program to crash there, for reasons unclear to us yet.
    364 TEST(NaggyMockTest, AcceptsClassNamedMock) {
    365   NaggyMock< ::Mock> naggy;
    366   EXPECT_CALL(naggy, DoThis());
    367   naggy.DoThis();
    368 }
    369 #endif  // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
    370 
    371 // Tests that a strict mock allows expected calls.
    372 TEST(StrictMockTest, AllowsExpectedCall) {
    373   StrictMock<MockFoo> strict_foo;
    374 
    375   EXPECT_CALL(strict_foo, DoThis());
    376   strict_foo.DoThis();
    377 }
    378 
    379 // Tests that an unexpected call on a strict mock fails.
    380 TEST(StrictMockTest, UnexpectedCallFails) {
    381   StrictMock<MockFoo> strict_foo;
    382 
    383   EXPECT_CALL(strict_foo, DoThis()).Times(0);
    384   EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
    385                           "called more times than expected");
    386 }
    387 
    388 // Tests that an uninteresting call on a strict mock fails.
    389 TEST(StrictMockTest, UninterestingCallFails) {
    390   StrictMock<MockFoo> strict_foo;
    391 
    392   EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
    393                           "Uninteresting mock function call");
    394 }
    395 
    396 // Tests that an uninteresting call on a strict mock fails, even if
    397 // the call deletes the mock object.
    398 TEST(StrictMockTest, UninterestingCallFailsAfterDeath) {
    399   StrictMock<MockFoo>* const strict_foo = new StrictMock<MockFoo>;
    400 
    401   ON_CALL(*strict_foo, DoThis())
    402       .WillByDefault(Invoke(strict_foo, &MockFoo::Delete));
    403 
    404   EXPECT_NONFATAL_FAILURE(strict_foo->DoThis(),
    405                           "Uninteresting mock function call");
    406 }
    407 
    408 // Tests that StrictMock works with a mock class that has a
    409 // non-default constructor.
    410 TEST(StrictMockTest, NonDefaultConstructor) {
    411   StrictMock<MockBar> strict_bar("hi");
    412   EXPECT_EQ("hi", strict_bar.str());
    413 
    414   EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
    415                           "Uninteresting mock function call");
    416 }
    417 
    418 // Tests that StrictMock works with a mock class that has a 10-ary
    419 // non-default constructor.
    420 TEST(StrictMockTest, NonDefaultConstructor10) {
    421   StrictMock<MockBar> strict_bar('a', 'b', "c", "d", 'e', 'f',
    422                                  "g", "h", true, false);
    423   EXPECT_EQ("abcdefghTF", strict_bar.str());
    424 
    425   EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
    426                           "Uninteresting mock function call");
    427 }
    428 
    429 #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
    430 // Tests that StrictMock<Mock> compiles where Mock is a user-defined
    431 // class (as opposed to ::testing::Mock).  We had to work around an
    432 // MSVC 8.0 bug that caused the symbol Mock used in the definition of
    433 // StrictMock to be looked up in the wrong context, and this test
    434 // ensures that our fix works.
    435 //
    436 // We have to skip this test on Symbian and Windows Mobile, as it
    437 // causes the program to crash there, for reasons unclear to us yet.
    438 TEST(StrictMockTest, AcceptsClassNamedMock) {
    439   StrictMock< ::Mock> strict;
    440   EXPECT_CALL(strict, DoThis());
    441   strict.DoThis();
    442 }
    443 #endif  // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
    444 
    445 }  // namespace gmock_nice_strict_test
    446 }  // namespace testing
    447