Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:19:58

0001 // Copyright 2008, Google Inc.
0002 // All rights reserved.
0003 //
0004 // Redistribution and use in source and binary forms, with or without
0005 // modification, are permitted provided that the following conditions are
0006 // met:
0007 //
0008 //     * Redistributions of source code must retain the above copyright
0009 // notice, this list of conditions and the following disclaimer.
0010 //     * Redistributions in binary form must reproduce the above
0011 // copyright notice, this list of conditions and the following disclaimer
0012 // in the documentation and/or other materials provided with the
0013 // distribution.
0014 //     * Neither the name of Google Inc. nor the names of its
0015 // contributors may be used to endorse or promote products derived from
0016 // this software without specific prior written permission.
0017 //
0018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0029 //
0030 // Author: wan@google.com (Zhanyong Wan)
0031 
0032 #include "gmock/gmock-generated-nice-strict.h"
0033 
0034 #include <string>
0035 #include "gmock/gmock.h"
0036 #include "gtest/gtest.h"
0037 #include "gtest/gtest-spi.h"
0038 
0039 // This must not be defined inside the ::testing namespace, or it will
0040 // clash with ::testing::Mock.
0041 class Mock {
0042  public:
0043   Mock() {}
0044 
0045   MOCK_METHOD0(DoThis, void());
0046 
0047  private:
0048   GTEST_DISALLOW_COPY_AND_ASSIGN_(Mock);
0049 };
0050 
0051 namespace testing {
0052 namespace gmock_nice_strict_test {
0053 
0054 using testing::internal::string;
0055 using testing::GMOCK_FLAG(verbose);
0056 using testing::HasSubstr;
0057 using testing::NaggyMock;
0058 using testing::NiceMock;
0059 using testing::StrictMock;
0060 
0061 #if GTEST_HAS_STREAM_REDIRECTION
0062 using testing::internal::CaptureStdout;
0063 using testing::internal::GetCapturedStdout;
0064 #endif
0065 
0066 // Defines some mock classes needed by the tests.
0067 
0068 class Foo {
0069  public:
0070   virtual ~Foo() {}
0071 
0072   virtual void DoThis() = 0;
0073   virtual int DoThat(bool flag) = 0;
0074 };
0075 
0076 class MockFoo : public Foo {
0077  public:
0078   MockFoo() {}
0079   void Delete() { delete this; }
0080 
0081   MOCK_METHOD0(DoThis, void());
0082   MOCK_METHOD1(DoThat, int(bool flag));
0083 
0084  private:
0085   GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo);
0086 };
0087 
0088 class MockBar {
0089  public:
0090   explicit MockBar(const string& s) : str_(s) {}
0091 
0092   MockBar(char a1, char a2, string a3, string a4, int a5, int a6,
0093           const string& a7, const string& a8, bool a9, bool a10) {
0094     str_ = string() + a1 + a2 + a3 + a4 + static_cast<char>(a5) +
0095         static_cast<char>(a6) + a7 + a8 + (a9 ? 'T' : 'F') + (a10 ? 'T' : 'F');
0096   }
0097 
0098   virtual ~MockBar() {}
0099 
0100   const string& str() const { return str_; }
0101 
0102   MOCK_METHOD0(This, int());
0103   MOCK_METHOD2(That, string(int, bool));
0104 
0105  private:
0106   string str_;
0107 
0108   GTEST_DISALLOW_COPY_AND_ASSIGN_(MockBar);
0109 };
0110 
0111 #if GTEST_HAS_STREAM_REDIRECTION
0112 
0113 // Tests that a raw mock generates warnings for uninteresting calls.
0114 TEST(RawMockTest, WarningForUninterestingCall) {
0115   const string saved_flag = GMOCK_FLAG(verbose);
0116   GMOCK_FLAG(verbose) = "warning";
0117 
0118   MockFoo raw_foo;
0119 
0120   CaptureStdout();
0121   raw_foo.DoThis();
0122   raw_foo.DoThat(true);
0123   EXPECT_THAT(GetCapturedStdout(),
0124               HasSubstr("Uninteresting mock function call"));
0125 
0126   GMOCK_FLAG(verbose) = saved_flag;
0127 }
0128 
0129 // Tests that a raw mock generates warnings for uninteresting calls
0130 // that delete the mock object.
0131 TEST(RawMockTest, WarningForUninterestingCallAfterDeath) {
0132   const string saved_flag = GMOCK_FLAG(verbose);
0133   GMOCK_FLAG(verbose) = "warning";
0134 
0135   MockFoo* const raw_foo = new MockFoo;
0136 
0137   ON_CALL(*raw_foo, DoThis())
0138       .WillByDefault(Invoke(raw_foo, &MockFoo::Delete));
0139 
0140   CaptureStdout();
0141   raw_foo->DoThis();
0142   EXPECT_THAT(GetCapturedStdout(),
0143               HasSubstr("Uninteresting mock function call"));
0144 
0145   GMOCK_FLAG(verbose) = saved_flag;
0146 }
0147 
0148 // Tests that a raw mock generates informational logs for
0149 // uninteresting calls.
0150 TEST(RawMockTest, InfoForUninterestingCall) {
0151   MockFoo raw_foo;
0152 
0153   const string saved_flag = GMOCK_FLAG(verbose);
0154   GMOCK_FLAG(verbose) = "info";
0155   CaptureStdout();
0156   raw_foo.DoThis();
0157   EXPECT_THAT(GetCapturedStdout(),
0158               HasSubstr("Uninteresting mock function call"));
0159 
0160   GMOCK_FLAG(verbose) = saved_flag;
0161 }
0162 
0163 // Tests that a nice mock generates no warning for uninteresting calls.
0164 TEST(NiceMockTest, NoWarningForUninterestingCall) {
0165   NiceMock<MockFoo> nice_foo;
0166 
0167   CaptureStdout();
0168   nice_foo.DoThis();
0169   nice_foo.DoThat(true);
0170   EXPECT_EQ("", GetCapturedStdout());
0171 }
0172 
0173 // Tests that a nice mock generates no warning for uninteresting calls
0174 // that delete the mock object.
0175 TEST(NiceMockTest, NoWarningForUninterestingCallAfterDeath) {
0176   NiceMock<MockFoo>* const nice_foo = new NiceMock<MockFoo>;
0177 
0178   ON_CALL(*nice_foo, DoThis())
0179       .WillByDefault(Invoke(nice_foo, &MockFoo::Delete));
0180 
0181   CaptureStdout();
0182   nice_foo->DoThis();
0183   EXPECT_EQ("", GetCapturedStdout());
0184 }
0185 
0186 // Tests that a nice mock generates informational logs for
0187 // uninteresting calls.
0188 TEST(NiceMockTest, InfoForUninterestingCall) {
0189   NiceMock<MockFoo> nice_foo;
0190 
0191   const string saved_flag = GMOCK_FLAG(verbose);
0192   GMOCK_FLAG(verbose) = "info";
0193   CaptureStdout();
0194   nice_foo.DoThis();
0195   EXPECT_THAT(GetCapturedStdout(),
0196               HasSubstr("Uninteresting mock function call"));
0197 
0198   GMOCK_FLAG(verbose) = saved_flag;
0199 }
0200 
0201 #endif  // GTEST_HAS_STREAM_REDIRECTION
0202 
0203 // Tests that a nice mock allows expected calls.
0204 TEST(NiceMockTest, AllowsExpectedCall) {
0205   NiceMock<MockFoo> nice_foo;
0206 
0207   EXPECT_CALL(nice_foo, DoThis());
0208   nice_foo.DoThis();
0209 }
0210 
0211 // Tests that an unexpected call on a nice mock fails.
0212 TEST(NiceMockTest, UnexpectedCallFails) {
0213   NiceMock<MockFoo> nice_foo;
0214 
0215   EXPECT_CALL(nice_foo, DoThis()).Times(0);
0216   EXPECT_NONFATAL_FAILURE(nice_foo.DoThis(), "called more times than expected");
0217 }
0218 
0219 // Tests that NiceMock works with a mock class that has a non-default
0220 // constructor.
0221 TEST(NiceMockTest, NonDefaultConstructor) {
0222   NiceMock<MockBar> nice_bar("hi");
0223   EXPECT_EQ("hi", nice_bar.str());
0224 
0225   nice_bar.This();
0226   nice_bar.That(5, true);
0227 }
0228 
0229 // Tests that NiceMock works with a mock class that has a 10-ary
0230 // non-default constructor.
0231 TEST(NiceMockTest, NonDefaultConstructor10) {
0232   NiceMock<MockBar> nice_bar('a', 'b', "c", "d", 'e', 'f',
0233                              "g", "h", true, false);
0234   EXPECT_EQ("abcdefghTF", nice_bar.str());
0235 
0236   nice_bar.This();
0237   nice_bar.That(5, true);
0238 }
0239 
0240 #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
0241 // Tests that NiceMock<Mock> compiles where Mock is a user-defined
0242 // class (as opposed to ::testing::Mock).  We had to work around an
0243 // MSVC 8.0 bug that caused the symbol Mock used in the definition of
0244 // NiceMock to be looked up in the wrong context, and this test
0245 // ensures that our fix works.
0246 //
0247 // We have to skip this test on Symbian and Windows Mobile, as it
0248 // causes the program to crash there, for reasons unclear to us yet.
0249 TEST(NiceMockTest, AcceptsClassNamedMock) {
0250   NiceMock< ::Mock> nice;
0251   EXPECT_CALL(nice, DoThis());
0252   nice.DoThis();
0253 }
0254 #endif  // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
0255 
0256 #if GTEST_HAS_STREAM_REDIRECTION
0257 
0258 // Tests that a naggy mock generates warnings for uninteresting calls.
0259 TEST(NaggyMockTest, WarningForUninterestingCall) {
0260   const string saved_flag = GMOCK_FLAG(verbose);
0261   GMOCK_FLAG(verbose) = "warning";
0262 
0263   NaggyMock<MockFoo> naggy_foo;
0264 
0265   CaptureStdout();
0266   naggy_foo.DoThis();
0267   naggy_foo.DoThat(true);
0268   EXPECT_THAT(GetCapturedStdout(),
0269               HasSubstr("Uninteresting mock function call"));
0270 
0271   GMOCK_FLAG(verbose) = saved_flag;
0272 }
0273 
0274 // Tests that a naggy mock generates a warning for an uninteresting call
0275 // that deletes the mock object.
0276 TEST(NaggyMockTest, WarningForUninterestingCallAfterDeath) {
0277   const string saved_flag = GMOCK_FLAG(verbose);
0278   GMOCK_FLAG(verbose) = "warning";
0279 
0280   NaggyMock<MockFoo>* const naggy_foo = new NaggyMock<MockFoo>;
0281 
0282   ON_CALL(*naggy_foo, DoThis())
0283       .WillByDefault(Invoke(naggy_foo, &MockFoo::Delete));
0284 
0285   CaptureStdout();
0286   naggy_foo->DoThis();
0287   EXPECT_THAT(GetCapturedStdout(),
0288               HasSubstr("Uninteresting mock function call"));
0289 
0290   GMOCK_FLAG(verbose) = saved_flag;
0291 }
0292 
0293 #endif  // GTEST_HAS_STREAM_REDIRECTION
0294 
0295 // Tests that a naggy mock allows expected calls.
0296 TEST(NaggyMockTest, AllowsExpectedCall) {
0297   NaggyMock<MockFoo> naggy_foo;
0298 
0299   EXPECT_CALL(naggy_foo, DoThis());
0300   naggy_foo.DoThis();
0301 }
0302 
0303 // Tests that an unexpected call on a naggy mock fails.
0304 TEST(NaggyMockTest, UnexpectedCallFails) {
0305   NaggyMock<MockFoo> naggy_foo;
0306 
0307   EXPECT_CALL(naggy_foo, DoThis()).Times(0);
0308   EXPECT_NONFATAL_FAILURE(naggy_foo.DoThis(),
0309                           "called more times than expected");
0310 }
0311 
0312 // Tests that NaggyMock works with a mock class that has a non-default
0313 // constructor.
0314 TEST(NaggyMockTest, NonDefaultConstructor) {
0315   NaggyMock<MockBar> naggy_bar("hi");
0316   EXPECT_EQ("hi", naggy_bar.str());
0317 
0318   naggy_bar.This();
0319   naggy_bar.That(5, true);
0320 }
0321 
0322 // Tests that NaggyMock works with a mock class that has a 10-ary
0323 // non-default constructor.
0324 TEST(NaggyMockTest, NonDefaultConstructor10) {
0325   NaggyMock<MockBar> naggy_bar('0', '1', "2", "3", '4', '5',
0326                                "6", "7", true, false);
0327   EXPECT_EQ("01234567TF", naggy_bar.str());
0328 
0329   naggy_bar.This();
0330   naggy_bar.That(5, true);
0331 }
0332 
0333 #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
0334 // Tests that NaggyMock<Mock> compiles where Mock is a user-defined
0335 // class (as opposed to ::testing::Mock).  We had to work around an
0336 // MSVC 8.0 bug that caused the symbol Mock used in the definition of
0337 // NaggyMock to be looked up in the wrong context, and this test
0338 // ensures that our fix works.
0339 //
0340 // We have to skip this test on Symbian and Windows Mobile, as it
0341 // causes the program to crash there, for reasons unclear to us yet.
0342 TEST(NaggyMockTest, AcceptsClassNamedMock) {
0343   NaggyMock< ::Mock> naggy;
0344   EXPECT_CALL(naggy, DoThis());
0345   naggy.DoThis();
0346 }
0347 #endif  // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
0348 
0349 // Tests that a strict mock allows expected calls.
0350 TEST(StrictMockTest, AllowsExpectedCall) {
0351   StrictMock<MockFoo> strict_foo;
0352 
0353   EXPECT_CALL(strict_foo, DoThis());
0354   strict_foo.DoThis();
0355 }
0356 
0357 // Tests that an unexpected call on a strict mock fails.
0358 TEST(StrictMockTest, UnexpectedCallFails) {
0359   StrictMock<MockFoo> strict_foo;
0360 
0361   EXPECT_CALL(strict_foo, DoThis()).Times(0);
0362   EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
0363                           "called more times than expected");
0364 }
0365 
0366 // Tests that an uninteresting call on a strict mock fails.
0367 TEST(StrictMockTest, UninterestingCallFails) {
0368   StrictMock<MockFoo> strict_foo;
0369 
0370   EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
0371                           "Uninteresting mock function call");
0372 }
0373 
0374 // Tests that an uninteresting call on a strict mock fails, even if
0375 // the call deletes the mock object.
0376 TEST(StrictMockTest, UninterestingCallFailsAfterDeath) {
0377   StrictMock<MockFoo>* const strict_foo = new StrictMock<MockFoo>;
0378 
0379   ON_CALL(*strict_foo, DoThis())
0380       .WillByDefault(Invoke(strict_foo, &MockFoo::Delete));
0381 
0382   EXPECT_NONFATAL_FAILURE(strict_foo->DoThis(),
0383                           "Uninteresting mock function call");
0384 }
0385 
0386 // Tests that StrictMock works with a mock class that has a
0387 // non-default constructor.
0388 TEST(StrictMockTest, NonDefaultConstructor) {
0389   StrictMock<MockBar> strict_bar("hi");
0390   EXPECT_EQ("hi", strict_bar.str());
0391 
0392   EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
0393                           "Uninteresting mock function call");
0394 }
0395 
0396 // Tests that StrictMock works with a mock class that has a 10-ary
0397 // non-default constructor.
0398 TEST(StrictMockTest, NonDefaultConstructor10) {
0399   StrictMock<MockBar> strict_bar('a', 'b', "c", "d", 'e', 'f',
0400                                  "g", "h", true, false);
0401   EXPECT_EQ("abcdefghTF", strict_bar.str());
0402 
0403   EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
0404                           "Uninteresting mock function call");
0405 }
0406 
0407 #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
0408 // Tests that StrictMock<Mock> compiles where Mock is a user-defined
0409 // class (as opposed to ::testing::Mock).  We had to work around an
0410 // MSVC 8.0 bug that caused the symbol Mock used in the definition of
0411 // StrictMock to be looked up in the wrong context, and this test
0412 // ensures that our fix works.
0413 //
0414 // We have to skip this test on Symbian and Windows Mobile, as it
0415 // causes the program to crash there, for reasons unclear to us yet.
0416 TEST(StrictMockTest, AcceptsClassNamedMock) {
0417   StrictMock< ::Mock> strict;
0418   EXPECT_CALL(strict, DoThis());
0419   strict.DoThis();
0420 }
0421 #endif  // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
0422 
0423 }  // namespace gmock_nice_strict_test
0424 }  // namespace testing