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