gmock-nice-strict_test.cc
Go to the documentation of this file.
00001 // Copyright 2008, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // Author: wan@google.com (Zhanyong Wan)
00031 
00032 #include "gmock/gmock-generated-nice-strict.h"
00033 
00034 #include <string>
00035 #include "gmock/gmock.h"
00036 #include "gtest/gtest.h"
00037 #include "gtest/gtest-spi.h"
00038 
00039 // This must not be defined inside the ::testing namespace, or it will
00040 // clash with ::testing::Mock.
00041 class Mock {
00042  public:
00043   Mock() {}
00044 
00045   MOCK_METHOD0(DoThis, void());
00046 
00047  private:
00048   GTEST_DISALLOW_COPY_AND_ASSIGN_(Mock);
00049 };
00050 
00051 namespace testing {
00052 namespace gmock_nice_strict_test {
00053 
00054 using testing::internal::string;
00055 using testing::GMOCK_FLAG(verbose);
00056 using testing::HasSubstr;
00057 using testing::NaggyMock;
00058 using testing::NiceMock;
00059 using testing::StrictMock;
00060 
00061 #if GTEST_HAS_STREAM_REDIRECTION
00062 using testing::internal::CaptureStdout;
00063 using testing::internal::GetCapturedStdout;
00064 #endif
00065 
00066 // Defines some mock classes needed by the tests.
00067 
00068 class Foo {
00069  public:
00070   virtual ~Foo() {}
00071 
00072   virtual void DoThis() = 0;
00073   virtual int DoThat(bool flag) = 0;
00074 };
00075 
00076 class MockFoo : public Foo {
00077  public:
00078   MockFoo() {}
00079   void Delete() { delete this; }
00080 
00081   MOCK_METHOD0(DoThis, void());
00082   MOCK_METHOD1(DoThat, int(bool flag));
00083 
00084  private:
00085   GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo);
00086 };
00087 
00088 class MockBar {
00089  public:
00090   explicit MockBar(const string& s) : str_(s) {}
00091 
00092   MockBar(char a1, char a2, string a3, string a4, int a5, int a6,
00093           const string& a7, const string& a8, bool a9, bool a10) {
00094     str_ = string() + a1 + a2 + a3 + a4 + static_cast<char>(a5) +
00095         static_cast<char>(a6) + a7 + a8 + (a9 ? 'T' : 'F') + (a10 ? 'T' : 'F');
00096   }
00097 
00098   virtual ~MockBar() {}
00099 
00100   const string& str() const { return str_; }
00101 
00102   MOCK_METHOD0(This, int());
00103   MOCK_METHOD2(That, string(int, bool));
00104 
00105  private:
00106   string str_;
00107 
00108   GTEST_DISALLOW_COPY_AND_ASSIGN_(MockBar);
00109 };
00110 
00111 #if GTEST_HAS_STREAM_REDIRECTION
00112 
00113 // Tests that a raw mock generates warnings for uninteresting calls.
00114 TEST(RawMockTest, WarningForUninterestingCall) {
00115   const string saved_flag = GMOCK_FLAG(verbose);
00116   GMOCK_FLAG(verbose) = "warning";
00117 
00118   MockFoo raw_foo;
00119 
00120   CaptureStdout();
00121   raw_foo.DoThis();
00122   raw_foo.DoThat(true);
00123   EXPECT_THAT(GetCapturedStdout(),
00124               HasSubstr("Uninteresting mock function call"));
00125 
00126   GMOCK_FLAG(verbose) = saved_flag;
00127 }
00128 
00129 // Tests that a raw mock generates warnings for uninteresting calls
00130 // that delete the mock object.
00131 TEST(RawMockTest, WarningForUninterestingCallAfterDeath) {
00132   const string saved_flag = GMOCK_FLAG(verbose);
00133   GMOCK_FLAG(verbose) = "warning";
00134 
00135   MockFoo* const raw_foo = new MockFoo;
00136 
00137   ON_CALL(*raw_foo, DoThis())
00138       .WillByDefault(Invoke(raw_foo, &MockFoo::Delete));
00139 
00140   CaptureStdout();
00141   raw_foo->DoThis();
00142   EXPECT_THAT(GetCapturedStdout(),
00143               HasSubstr("Uninteresting mock function call"));
00144 
00145   GMOCK_FLAG(verbose) = saved_flag;
00146 }
00147 
00148 // Tests that a raw mock generates informational logs for
00149 // uninteresting calls.
00150 TEST(RawMockTest, InfoForUninterestingCall) {
00151   MockFoo raw_foo;
00152 
00153   const string saved_flag = GMOCK_FLAG(verbose);
00154   GMOCK_FLAG(verbose) = "info";
00155   CaptureStdout();
00156   raw_foo.DoThis();
00157   EXPECT_THAT(GetCapturedStdout(),
00158               HasSubstr("Uninteresting mock function call"));
00159 
00160   GMOCK_FLAG(verbose) = saved_flag;
00161 }
00162 
00163 // Tests that a nice mock generates no warning for uninteresting calls.
00164 TEST(NiceMockTest, NoWarningForUninterestingCall) {
00165   NiceMock<MockFoo> nice_foo;
00166 
00167   CaptureStdout();
00168   nice_foo.DoThis();
00169   nice_foo.DoThat(true);
00170   EXPECT_EQ("", GetCapturedStdout());
00171 }
00172 
00173 // Tests that a nice mock generates no warning for uninteresting calls
00174 // that delete the mock object.
00175 TEST(NiceMockTest, NoWarningForUninterestingCallAfterDeath) {
00176   NiceMock<MockFoo>* const nice_foo = new NiceMock<MockFoo>;
00177 
00178   ON_CALL(*nice_foo, DoThis())
00179       .WillByDefault(Invoke(nice_foo, &MockFoo::Delete));
00180 
00181   CaptureStdout();
00182   nice_foo->DoThis();
00183   EXPECT_EQ("", GetCapturedStdout());
00184 }
00185 
00186 // Tests that a nice mock generates informational logs for
00187 // uninteresting calls.
00188 TEST(NiceMockTest, InfoForUninterestingCall) {
00189   NiceMock<MockFoo> nice_foo;
00190 
00191   const string saved_flag = GMOCK_FLAG(verbose);
00192   GMOCK_FLAG(verbose) = "info";
00193   CaptureStdout();
00194   nice_foo.DoThis();
00195   EXPECT_THAT(GetCapturedStdout(),
00196               HasSubstr("Uninteresting mock function call"));
00197 
00198   GMOCK_FLAG(verbose) = saved_flag;
00199 }
00200 
00201 #endif  // GTEST_HAS_STREAM_REDIRECTION
00202 
00203 // Tests that a nice mock allows expected calls.
00204 TEST(NiceMockTest, AllowsExpectedCall) {
00205   NiceMock<MockFoo> nice_foo;
00206 
00207   EXPECT_CALL(nice_foo, DoThis());
00208   nice_foo.DoThis();
00209 }
00210 
00211 // Tests that an unexpected call on a nice mock fails.
00212 TEST(NiceMockTest, UnexpectedCallFails) {
00213   NiceMock<MockFoo> nice_foo;
00214 
00215   EXPECT_CALL(nice_foo, DoThis()).Times(0);
00216   EXPECT_NONFATAL_FAILURE(nice_foo.DoThis(), "called more times than expected");
00217 }
00218 
00219 // Tests that NiceMock works with a mock class that has a non-default
00220 // constructor.
00221 TEST(NiceMockTest, NonDefaultConstructor) {
00222   NiceMock<MockBar> nice_bar("hi");
00223   EXPECT_EQ("hi", nice_bar.str());
00224 
00225   nice_bar.This();
00226   nice_bar.That(5, true);
00227 }
00228 
00229 // Tests that NiceMock works with a mock class that has a 10-ary
00230 // non-default constructor.
00231 TEST(NiceMockTest, NonDefaultConstructor10) {
00232   NiceMock<MockBar> nice_bar('a', 'b', "c", "d", 'e', 'f',
00233                              "g", "h", true, false);
00234   EXPECT_EQ("abcdefghTF", nice_bar.str());
00235 
00236   nice_bar.This();
00237   nice_bar.That(5, true);
00238 }
00239 
00240 #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
00241 // Tests that NiceMock<Mock> compiles where Mock is a user-defined
00242 // class (as opposed to ::testing::Mock).  We had to work around an
00243 // MSVC 8.0 bug that caused the symbol Mock used in the definition of
00244 // NiceMock to be looked up in the wrong context, and this test
00245 // ensures that our fix works.
00246 //
00247 // We have to skip this test on Symbian and Windows Mobile, as it
00248 // causes the program to crash there, for reasons unclear to us yet.
00249 TEST(NiceMockTest, AcceptsClassNamedMock) {
00250   NiceMock< ::Mock> nice;
00251   EXPECT_CALL(nice, DoThis());
00252   nice.DoThis();
00253 }
00254 #endif  // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
00255 
00256 #if GTEST_HAS_STREAM_REDIRECTION
00257 
00258 // Tests that a naggy mock generates warnings for uninteresting calls.
00259 TEST(NaggyMockTest, WarningForUninterestingCall) {
00260   const string saved_flag = GMOCK_FLAG(verbose);
00261   GMOCK_FLAG(verbose) = "warning";
00262 
00263   NaggyMock<MockFoo> naggy_foo;
00264 
00265   CaptureStdout();
00266   naggy_foo.DoThis();
00267   naggy_foo.DoThat(true);
00268   EXPECT_THAT(GetCapturedStdout(),
00269               HasSubstr("Uninteresting mock function call"));
00270 
00271   GMOCK_FLAG(verbose) = saved_flag;
00272 }
00273 
00274 // Tests that a naggy mock generates a warning for an uninteresting call
00275 // that deletes the mock object.
00276 TEST(NaggyMockTest, WarningForUninterestingCallAfterDeath) {
00277   const string saved_flag = GMOCK_FLAG(verbose);
00278   GMOCK_FLAG(verbose) = "warning";
00279 
00280   NaggyMock<MockFoo>* const naggy_foo = new NaggyMock<MockFoo>;
00281 
00282   ON_CALL(*naggy_foo, DoThis())
00283       .WillByDefault(Invoke(naggy_foo, &MockFoo::Delete));
00284 
00285   CaptureStdout();
00286   naggy_foo->DoThis();
00287   EXPECT_THAT(GetCapturedStdout(),
00288               HasSubstr("Uninteresting mock function call"));
00289 
00290   GMOCK_FLAG(verbose) = saved_flag;
00291 }
00292 
00293 #endif  // GTEST_HAS_STREAM_REDIRECTION
00294 
00295 // Tests that a naggy mock allows expected calls.
00296 TEST(NaggyMockTest, AllowsExpectedCall) {
00297   NaggyMock<MockFoo> naggy_foo;
00298 
00299   EXPECT_CALL(naggy_foo, DoThis());
00300   naggy_foo.DoThis();
00301 }
00302 
00303 // Tests that an unexpected call on a naggy mock fails.
00304 TEST(NaggyMockTest, UnexpectedCallFails) {
00305   NaggyMock<MockFoo> naggy_foo;
00306 
00307   EXPECT_CALL(naggy_foo, DoThis()).Times(0);
00308   EXPECT_NONFATAL_FAILURE(naggy_foo.DoThis(),
00309                           "called more times than expected");
00310 }
00311 
00312 // Tests that NaggyMock works with a mock class that has a non-default
00313 // constructor.
00314 TEST(NaggyMockTest, NonDefaultConstructor) {
00315   NaggyMock<MockBar> naggy_bar("hi");
00316   EXPECT_EQ("hi", naggy_bar.str());
00317 
00318   naggy_bar.This();
00319   naggy_bar.That(5, true);
00320 }
00321 
00322 // Tests that NaggyMock works with a mock class that has a 10-ary
00323 // non-default constructor.
00324 TEST(NaggyMockTest, NonDefaultConstructor10) {
00325   NaggyMock<MockBar> naggy_bar('0', '1', "2", "3", '4', '5',
00326                                "6", "7", true, false);
00327   EXPECT_EQ("01234567TF", naggy_bar.str());
00328 
00329   naggy_bar.This();
00330   naggy_bar.That(5, true);
00331 }
00332 
00333 #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
00334 // Tests that NaggyMock<Mock> compiles where Mock is a user-defined
00335 // class (as opposed to ::testing::Mock).  We had to work around an
00336 // MSVC 8.0 bug that caused the symbol Mock used in the definition of
00337 // NaggyMock to be looked up in the wrong context, and this test
00338 // ensures that our fix works.
00339 //
00340 // We have to skip this test on Symbian and Windows Mobile, as it
00341 // causes the program to crash there, for reasons unclear to us yet.
00342 TEST(NaggyMockTest, AcceptsClassNamedMock) {
00343   NaggyMock< ::Mock> naggy;
00344   EXPECT_CALL(naggy, DoThis());
00345   naggy.DoThis();
00346 }
00347 #endif  // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
00348 
00349 // Tests that a strict mock allows expected calls.
00350 TEST(StrictMockTest, AllowsExpectedCall) {
00351   StrictMock<MockFoo> strict_foo;
00352 
00353   EXPECT_CALL(strict_foo, DoThis());
00354   strict_foo.DoThis();
00355 }
00356 
00357 // Tests that an unexpected call on a strict mock fails.
00358 TEST(StrictMockTest, UnexpectedCallFails) {
00359   StrictMock<MockFoo> strict_foo;
00360 
00361   EXPECT_CALL(strict_foo, DoThis()).Times(0);
00362   EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
00363                           "called more times than expected");
00364 }
00365 
00366 // Tests that an uninteresting call on a strict mock fails.
00367 TEST(StrictMockTest, UninterestingCallFails) {
00368   StrictMock<MockFoo> strict_foo;
00369 
00370   EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
00371                           "Uninteresting mock function call");
00372 }
00373 
00374 // Tests that an uninteresting call on a strict mock fails, even if
00375 // the call deletes the mock object.
00376 TEST(StrictMockTest, UninterestingCallFailsAfterDeath) {
00377   StrictMock<MockFoo>* const strict_foo = new StrictMock<MockFoo>;
00378 
00379   ON_CALL(*strict_foo, DoThis())
00380       .WillByDefault(Invoke(strict_foo, &MockFoo::Delete));
00381 
00382   EXPECT_NONFATAL_FAILURE(strict_foo->DoThis(),
00383                           "Uninteresting mock function call");
00384 }
00385 
00386 // Tests that StrictMock works with a mock class that has a
00387 // non-default constructor.
00388 TEST(StrictMockTest, NonDefaultConstructor) {
00389   StrictMock<MockBar> strict_bar("hi");
00390   EXPECT_EQ("hi", strict_bar.str());
00391 
00392   EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
00393                           "Uninteresting mock function call");
00394 }
00395 
00396 // Tests that StrictMock works with a mock class that has a 10-ary
00397 // non-default constructor.
00398 TEST(StrictMockTest, NonDefaultConstructor10) {
00399   StrictMock<MockBar> strict_bar('a', 'b', "c", "d", 'e', 'f',
00400                                  "g", "h", true, false);
00401   EXPECT_EQ("abcdefghTF", strict_bar.str());
00402 
00403   EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
00404                           "Uninteresting mock function call");
00405 }
00406 
00407 #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
00408 // Tests that StrictMock<Mock> compiles where Mock is a user-defined
00409 // class (as opposed to ::testing::Mock).  We had to work around an
00410 // MSVC 8.0 bug that caused the symbol Mock used in the definition of
00411 // StrictMock to be looked up in the wrong context, and this test
00412 // ensures that our fix works.
00413 //
00414 // We have to skip this test on Symbian and Windows Mobile, as it
00415 // causes the program to crash there, for reasons unclear to us yet.
00416 TEST(StrictMockTest, AcceptsClassNamedMock) {
00417   StrictMock< ::Mock> strict;
00418   EXPECT_CALL(strict, DoThis());
00419   strict.DoThis();
00420 }
00421 #endif  // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
00422 
00423 }  // namespace gmock_nice_strict_test
00424 }  // namespace testing


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:42