gmock_output_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 // Tests Google Mock's output in various scenarios.  This ensures that
00033 // Google Mock's messages are readable and useful.
00034 
00035 #include "gmock/gmock.h"
00036 
00037 #include <stdio.h>
00038 #include <string>
00039 
00040 #include "gtest/gtest.h"
00041 
00042 using testing::_;
00043 using testing::AnyNumber;
00044 using testing::Ge;
00045 using testing::InSequence;
00046 using testing::NaggyMock;
00047 using testing::Ref;
00048 using testing::Return;
00049 using testing::Sequence;
00050 
00051 class MockFoo {
00052  public:
00053   MockFoo() {}
00054 
00055   MOCK_METHOD3(Bar, char(const std::string& s, int i, double x));
00056   MOCK_METHOD2(Bar2, bool(int x, int y));
00057   MOCK_METHOD2(Bar3, void(int x, int y));
00058 
00059  private:
00060   GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo);
00061 };
00062 
00063 class GMockOutputTest : public testing::Test {
00064  protected:
00065   NaggyMock<MockFoo> foo_;
00066 };
00067 
00068 TEST_F(GMockOutputTest, ExpectedCall) {
00069   testing::GMOCK_FLAG(verbose) = "info";
00070 
00071   EXPECT_CALL(foo_, Bar2(0, _));
00072   foo_.Bar2(0, 0);  // Expected call
00073 
00074   testing::GMOCK_FLAG(verbose) = "warning";
00075 }
00076 
00077 TEST_F(GMockOutputTest, ExpectedCallToVoidFunction) {
00078   testing::GMOCK_FLAG(verbose) = "info";
00079 
00080   EXPECT_CALL(foo_, Bar3(0, _));
00081   foo_.Bar3(0, 0);  // Expected call
00082 
00083   testing::GMOCK_FLAG(verbose) = "warning";
00084 }
00085 
00086 TEST_F(GMockOutputTest, ExplicitActionsRunOut) {
00087   EXPECT_CALL(foo_, Bar2(_, _))
00088       .Times(2)
00089       .WillOnce(Return(false));
00090   foo_.Bar2(2, 2);
00091   foo_.Bar2(1, 1);  // Explicit actions in EXPECT_CALL run out.
00092 }
00093 
00094 TEST_F(GMockOutputTest, UnexpectedCall) {
00095   EXPECT_CALL(foo_, Bar2(0, _));
00096 
00097   foo_.Bar2(1, 0);  // Unexpected call
00098   foo_.Bar2(0, 0);  // Expected call
00099 }
00100 
00101 TEST_F(GMockOutputTest, UnexpectedCallToVoidFunction) {
00102   EXPECT_CALL(foo_, Bar3(0, _));
00103 
00104   foo_.Bar3(1, 0);  // Unexpected call
00105   foo_.Bar3(0, 0);  // Expected call
00106 }
00107 
00108 TEST_F(GMockOutputTest, ExcessiveCall) {
00109   EXPECT_CALL(foo_, Bar2(0, _));
00110 
00111   foo_.Bar2(0, 0);  // Expected call
00112   foo_.Bar2(0, 1);  // Excessive call
00113 }
00114 
00115 TEST_F(GMockOutputTest, ExcessiveCallToVoidFunction) {
00116   EXPECT_CALL(foo_, Bar3(0, _));
00117 
00118   foo_.Bar3(0, 0);  // Expected call
00119   foo_.Bar3(0, 1);  // Excessive call
00120 }
00121 
00122 TEST_F(GMockOutputTest, UninterestingCall) {
00123   foo_.Bar2(0, 1);  // Uninteresting call
00124 }
00125 
00126 TEST_F(GMockOutputTest, UninterestingCallToVoidFunction) {
00127   foo_.Bar3(0, 1);  // Uninteresting call
00128 }
00129 
00130 TEST_F(GMockOutputTest, RetiredExpectation) {
00131   EXPECT_CALL(foo_, Bar2(_, _))
00132       .RetiresOnSaturation();
00133   EXPECT_CALL(foo_, Bar2(0, 0));
00134 
00135   foo_.Bar2(1, 1);
00136   foo_.Bar2(1, 1);  // Matches a retired expectation
00137   foo_.Bar2(0, 0);
00138 }
00139 
00140 TEST_F(GMockOutputTest, UnsatisfiedPrerequisite) {
00141   {
00142     InSequence s;
00143     EXPECT_CALL(foo_, Bar(_, 0, _));
00144     EXPECT_CALL(foo_, Bar2(0, 0));
00145     EXPECT_CALL(foo_, Bar2(1, _));
00146   }
00147 
00148   foo_.Bar2(1, 0);  // Has one immediate unsatisfied pre-requisite
00149   foo_.Bar("Hi", 0, 0);
00150   foo_.Bar2(0, 0);
00151   foo_.Bar2(1, 0);
00152 }
00153 
00154 TEST_F(GMockOutputTest, UnsatisfiedPrerequisites) {
00155   Sequence s1, s2;
00156 
00157   EXPECT_CALL(foo_, Bar(_, 0, _))
00158       .InSequence(s1);
00159   EXPECT_CALL(foo_, Bar2(0, 0))
00160       .InSequence(s2);
00161   EXPECT_CALL(foo_, Bar2(1, _))
00162       .InSequence(s1, s2);
00163 
00164   foo_.Bar2(1, 0);  // Has two immediate unsatisfied pre-requisites
00165   foo_.Bar("Hi", 0, 0);
00166   foo_.Bar2(0, 0);
00167   foo_.Bar2(1, 0);
00168 }
00169 
00170 TEST_F(GMockOutputTest, UnsatisfiedWith) {
00171   EXPECT_CALL(foo_, Bar2(_, _)).With(Ge());
00172 }
00173 
00174 TEST_F(GMockOutputTest, UnsatisfiedExpectation) {
00175   EXPECT_CALL(foo_, Bar(_, _, _));
00176   EXPECT_CALL(foo_, Bar2(0, _))
00177       .Times(2);
00178 
00179   foo_.Bar2(0, 1);
00180 }
00181 
00182 TEST_F(GMockOutputTest, MismatchArguments) {
00183   const std::string s = "Hi";
00184   EXPECT_CALL(foo_, Bar(Ref(s), _, Ge(0)));
00185 
00186   foo_.Bar("Ho", 0, -0.1);  // Mismatch arguments
00187   foo_.Bar(s, 0, 0);
00188 }
00189 
00190 TEST_F(GMockOutputTest, MismatchWith) {
00191   EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1)))
00192       .With(Ge());
00193 
00194   foo_.Bar2(2, 3);  // Mismatch With()
00195   foo_.Bar2(2, 1);
00196 }
00197 
00198 TEST_F(GMockOutputTest, MismatchArgumentsAndWith) {
00199   EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1)))
00200       .With(Ge());
00201 
00202   foo_.Bar2(1, 3);  // Mismatch arguments and mismatch With()
00203   foo_.Bar2(2, 1);
00204 }
00205 
00206 TEST_F(GMockOutputTest, UnexpectedCallWithDefaultAction) {
00207   ON_CALL(foo_, Bar2(_, _))
00208       .WillByDefault(Return(true));   // Default action #1
00209   ON_CALL(foo_, Bar2(1, _))
00210       .WillByDefault(Return(false));  // Default action #2
00211 
00212   EXPECT_CALL(foo_, Bar2(2, 2));
00213   foo_.Bar2(1, 0);  // Unexpected call, takes default action #2.
00214   foo_.Bar2(0, 0);  // Unexpected call, takes default action #1.
00215   foo_.Bar2(2, 2);  // Expected call.
00216 }
00217 
00218 TEST_F(GMockOutputTest, ExcessiveCallWithDefaultAction) {
00219   ON_CALL(foo_, Bar2(_, _))
00220       .WillByDefault(Return(true));   // Default action #1
00221   ON_CALL(foo_, Bar2(1, _))
00222       .WillByDefault(Return(false));  // Default action #2
00223 
00224   EXPECT_CALL(foo_, Bar2(2, 2));
00225   EXPECT_CALL(foo_, Bar2(1, 1));
00226 
00227   foo_.Bar2(2, 2);  // Expected call.
00228   foo_.Bar2(2, 2);  // Excessive call, takes default action #1.
00229   foo_.Bar2(1, 1);  // Expected call.
00230   foo_.Bar2(1, 1);  // Excessive call, takes default action #2.
00231 }
00232 
00233 TEST_F(GMockOutputTest, UninterestingCallWithDefaultAction) {
00234   ON_CALL(foo_, Bar2(_, _))
00235       .WillByDefault(Return(true));   // Default action #1
00236   ON_CALL(foo_, Bar2(1, _))
00237       .WillByDefault(Return(false));  // Default action #2
00238 
00239   foo_.Bar2(2, 2);  // Uninteresting call, takes default action #1.
00240   foo_.Bar2(1, 1);  // Uninteresting call, takes default action #2.
00241 }
00242 
00243 TEST_F(GMockOutputTest, ExplicitActionsRunOutWithDefaultAction) {
00244   ON_CALL(foo_, Bar2(_, _))
00245       .WillByDefault(Return(true));   // Default action #1
00246 
00247   EXPECT_CALL(foo_, Bar2(_, _))
00248       .Times(2)
00249       .WillOnce(Return(false));
00250   foo_.Bar2(2, 2);
00251   foo_.Bar2(1, 1);  // Explicit actions in EXPECT_CALL run out.
00252 }
00253 
00254 TEST_F(GMockOutputTest, CatchesLeakedMocks) {
00255   MockFoo* foo1 = new MockFoo;
00256   MockFoo* foo2 = new MockFoo;
00257 
00258   // Invokes ON_CALL on foo1.
00259   ON_CALL(*foo1, Bar(_, _, _)).WillByDefault(Return('a'));
00260 
00261   // Invokes EXPECT_CALL on foo2.
00262   EXPECT_CALL(*foo2, Bar2(_, _));
00263   EXPECT_CALL(*foo2, Bar2(1, _));
00264   EXPECT_CALL(*foo2, Bar3(_, _)).Times(AnyNumber());
00265   foo2->Bar2(2, 1);
00266   foo2->Bar2(1, 1);
00267 
00268   // Both foo1 and foo2 are deliberately leaked.
00269 }
00270 
00271 void TestCatchesLeakedMocksInAdHocTests() {
00272   MockFoo* foo = new MockFoo;
00273 
00274   // Invokes EXPECT_CALL on foo.
00275   EXPECT_CALL(*foo, Bar2(_, _));
00276   foo->Bar2(2, 1);
00277 
00278   // foo is deliberately leaked.
00279 }
00280 
00281 int main(int argc, char **argv) {
00282   testing::InitGoogleMock(&argc, argv);
00283 
00284   // Ensures that the tests pass no matter what value of
00285   // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies.
00286   testing::GMOCK_FLAG(catch_leaked_mocks) = true;
00287   testing::GMOCK_FLAG(verbose) = "warning";
00288 
00289   TestCatchesLeakedMocksInAdHocTests();
00290   return RUN_ALL_TESTS();
00291 }


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