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::internal::string;
     55 using testing::GMOCK_FLAG(verbose);
     56 using testing::HasSubstr;
     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 string& s) : str_(s) {}
     90 
     91   MockBar(char a1, char a2, string a3, string a4, int a5, int a6,
     92           const string& a7, const string& a8, bool a9, bool a10) {
     93     str_ = 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 string& str() const { return str_; }
    100 
    101   MOCK_METHOD0(This, int());
    102   MOCK_METHOD2(That, string(int, bool));
    103 
    104  private:
    105   string str_;
    106 
    107   GTEST_DISALLOW_COPY_AND_ASSIGN_(MockBar);
    108 };
    109 
    110 #if GTEST_HAS_STREAM_REDIRECTION
    111 
    112 // Tests that a nice mock generates no warning for uninteresting calls.
    113 TEST(NiceMockTest, NoWarningForUninterestingCall) {
    114   NiceMock<MockFoo> nice_foo;
    115 
    116   CaptureStdout();
    117   nice_foo.DoThis();
    118   nice_foo.DoThat(true);
    119   EXPECT_STREQ("", GetCapturedStdout().c_str());
    120 }
    121 
    122 // Tests that a nice mock generates no warning for uninteresting calls
    123 // that delete the mock object.
    124 TEST(NiceMockTest, NoWarningForUninterestingCallAfterDeath) {
    125   NiceMock<MockFoo>* const nice_foo = new NiceMock<MockFoo>;
    126 
    127   ON_CALL(*nice_foo, DoThis())
    128       .WillByDefault(Invoke(nice_foo, &MockFoo::Delete));
    129 
    130   CaptureStdout();
    131   nice_foo->DoThis();
    132   EXPECT_STREQ("", GetCapturedStdout().c_str());
    133 }
    134 
    135 // Tests that a nice mock generates informational logs for
    136 // uninteresting calls.
    137 TEST(NiceMockTest, InfoForUninterestingCall) {
    138   NiceMock<MockFoo> nice_foo;
    139 
    140   const string saved_flag = GMOCK_FLAG(verbose);
    141   GMOCK_FLAG(verbose) = "info";
    142   CaptureStdout();
    143   nice_foo.DoThis();
    144   EXPECT_THAT(GetCapturedStdout(),
    145               HasSubstr("Uninteresting mock function call"));
    146 
    147   CaptureStdout();
    148   nice_foo.DoThat(true);
    149   EXPECT_THAT(GetCapturedStdout(),
    150               HasSubstr("Uninteresting mock function call"));
    151   GMOCK_FLAG(verbose) = saved_flag;
    152 }
    153 
    154 #endif  // GTEST_HAS_STREAM_REDIRECTION
    155 
    156 // Tests that a nice mock allows expected calls.
    157 TEST(NiceMockTest, AllowsExpectedCall) {
    158   NiceMock<MockFoo> nice_foo;
    159 
    160   EXPECT_CALL(nice_foo, DoThis());
    161   nice_foo.DoThis();
    162 }
    163 
    164 // Tests that an unexpected call on a nice mock fails.
    165 TEST(NiceMockTest, UnexpectedCallFails) {
    166   NiceMock<MockFoo> nice_foo;
    167 
    168   EXPECT_CALL(nice_foo, DoThis()).Times(0);
    169   EXPECT_NONFATAL_FAILURE(nice_foo.DoThis(), "called more times than expected");
    170 }
    171 
    172 // Tests that NiceMock works with a mock class that has a non-default
    173 // constructor.
    174 TEST(NiceMockTest, NonDefaultConstructor) {
    175   NiceMock<MockBar> nice_bar("hi");
    176   EXPECT_EQ("hi", nice_bar.str());
    177 
    178   nice_bar.This();
    179   nice_bar.That(5, true);
    180 }
    181 
    182 // Tests that NiceMock works with a mock class that has a 10-ary
    183 // non-default constructor.
    184 TEST(NiceMockTest, NonDefaultConstructor10) {
    185   NiceMock<MockBar> nice_bar('a', 'b', "c", "d", 'e', 'f',
    186                              "g", "h", true, false);
    187   EXPECT_EQ("abcdefghTF", nice_bar.str());
    188 
    189   nice_bar.This();
    190   nice_bar.That(5, true);
    191 }
    192 
    193 #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
    194 // Tests that NiceMock<Mock> compiles where Mock is a user-defined
    195 // class (as opposed to ::testing::Mock).  We had to workaround an
    196 // MSVC 8.0 bug that caused the symbol Mock used in the definition of
    197 // NiceMock to be looked up in the wrong context, and this test
    198 // ensures that our fix works.
    199 //
    200 // We have to skip this test on Symbian and Windows Mobile, as it
    201 // causes the program to crash there, for reasons unclear to us yet.
    202 TEST(NiceMockTest, AcceptsClassNamedMock) {
    203   NiceMock< ::Mock> nice;
    204   EXPECT_CALL(nice, DoThis());
    205   nice.DoThis();
    206 }
    207 #endif  // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
    208 
    209 // Tests that a strict mock allows expected calls.
    210 TEST(StrictMockTest, AllowsExpectedCall) {
    211   StrictMock<MockFoo> strict_foo;
    212 
    213   EXPECT_CALL(strict_foo, DoThis());
    214   strict_foo.DoThis();
    215 }
    216 
    217 // Tests that an unexpected call on a strict mock fails.
    218 TEST(StrictMockTest, UnexpectedCallFails) {
    219   StrictMock<MockFoo> strict_foo;
    220 
    221   EXPECT_CALL(strict_foo, DoThis()).Times(0);
    222   EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
    223                           "called more times than expected");
    224 }
    225 
    226 // Tests that an uninteresting call on a strict mock fails.
    227 TEST(StrictMockTest, UninterestingCallFails) {
    228   StrictMock<MockFoo> strict_foo;
    229 
    230   EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
    231                           "Uninteresting mock function call");
    232 }
    233 
    234 // Tests that an uninteresting call on a strict mock fails, even if
    235 // the call deletes the mock object.
    236 TEST(StrictMockTest, UninterestingCallFailsAfterDeath) {
    237   StrictMock<MockFoo>* const strict_foo = new StrictMock<MockFoo>;
    238 
    239   ON_CALL(*strict_foo, DoThis())
    240       .WillByDefault(Invoke(strict_foo, &MockFoo::Delete));
    241 
    242   EXPECT_NONFATAL_FAILURE(strict_foo->DoThis(),
    243                           "Uninteresting mock function call");
    244 }
    245 
    246 // Tests that StrictMock works with a mock class that has a
    247 // non-default constructor.
    248 TEST(StrictMockTest, NonDefaultConstructor) {
    249   StrictMock<MockBar> strict_bar("hi");
    250   EXPECT_EQ("hi", strict_bar.str());
    251 
    252   EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
    253                           "Uninteresting mock function call");
    254 }
    255 
    256 // Tests that StrictMock works with a mock class that has a 10-ary
    257 // non-default constructor.
    258 TEST(StrictMockTest, NonDefaultConstructor10) {
    259   StrictMock<MockBar> strict_bar('a', 'b', "c", "d", 'e', 'f',
    260                                  "g", "h", true, false);
    261   EXPECT_EQ("abcdefghTF", strict_bar.str());
    262 
    263   EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
    264                           "Uninteresting mock function call");
    265 }
    266 
    267 #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
    268 // Tests that StrictMock<Mock> compiles where Mock is a user-defined
    269 // class (as opposed to ::testing::Mock).  We had to workaround an
    270 // MSVC 8.0 bug that caused the symbol Mock used in the definition of
    271 // StrictMock to be looked up in the wrong context, and this test
    272 // ensures that our fix works.
    273 //
    274 // We have to skip this test on Symbian and Windows Mobile, as it
    275 // causes the program to crash there, for reasons unclear to us yet.
    276 TEST(StrictMockTest, AcceptsClassNamedMock) {
    277   StrictMock< ::Mock> strict;
    278   EXPECT_CALL(strict, DoThis());
    279   strict.DoThis();
    280 }
    281 #endif  // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
    282 
    283 }  // namespace gmock_nice_strict_test
    284 }  // namespace testing
    285