00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
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);
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);
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);
00092 }
00093
00094 TEST_F(GMockOutputTest, UnexpectedCall) {
00095 EXPECT_CALL(foo_, Bar2(0, _));
00096
00097 foo_.Bar2(1, 0);
00098 foo_.Bar2(0, 0);
00099 }
00100
00101 TEST_F(GMockOutputTest, UnexpectedCallToVoidFunction) {
00102 EXPECT_CALL(foo_, Bar3(0, _));
00103
00104 foo_.Bar3(1, 0);
00105 foo_.Bar3(0, 0);
00106 }
00107
00108 TEST_F(GMockOutputTest, ExcessiveCall) {
00109 EXPECT_CALL(foo_, Bar2(0, _));
00110
00111 foo_.Bar2(0, 0);
00112 foo_.Bar2(0, 1);
00113 }
00114
00115 TEST_F(GMockOutputTest, ExcessiveCallToVoidFunction) {
00116 EXPECT_CALL(foo_, Bar3(0, _));
00117
00118 foo_.Bar3(0, 0);
00119 foo_.Bar3(0, 1);
00120 }
00121
00122 TEST_F(GMockOutputTest, UninterestingCall) {
00123 foo_.Bar2(0, 1);
00124 }
00125
00126 TEST_F(GMockOutputTest, UninterestingCallToVoidFunction) {
00127 foo_.Bar3(0, 1);
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);
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);
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);
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);
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);
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);
00203 foo_.Bar2(2, 1);
00204 }
00205
00206 TEST_F(GMockOutputTest, UnexpectedCallWithDefaultAction) {
00207 ON_CALL(foo_, Bar2(_, _))
00208 .WillByDefault(Return(true));
00209 ON_CALL(foo_, Bar2(1, _))
00210 .WillByDefault(Return(false));
00211
00212 EXPECT_CALL(foo_, Bar2(2, 2));
00213 foo_.Bar2(1, 0);
00214 foo_.Bar2(0, 0);
00215 foo_.Bar2(2, 2);
00216 }
00217
00218 TEST_F(GMockOutputTest, ExcessiveCallWithDefaultAction) {
00219 ON_CALL(foo_, Bar2(_, _))
00220 .WillByDefault(Return(true));
00221 ON_CALL(foo_, Bar2(1, _))
00222 .WillByDefault(Return(false));
00223
00224 EXPECT_CALL(foo_, Bar2(2, 2));
00225 EXPECT_CALL(foo_, Bar2(1, 1));
00226
00227 foo_.Bar2(2, 2);
00228 foo_.Bar2(2, 2);
00229 foo_.Bar2(1, 1);
00230 foo_.Bar2(1, 1);
00231 }
00232
00233 TEST_F(GMockOutputTest, UninterestingCallWithDefaultAction) {
00234 ON_CALL(foo_, Bar2(_, _))
00235 .WillByDefault(Return(true));
00236 ON_CALL(foo_, Bar2(1, _))
00237 .WillByDefault(Return(false));
00238
00239 foo_.Bar2(2, 2);
00240 foo_.Bar2(1, 1);
00241 }
00242
00243 TEST_F(GMockOutputTest, ExplicitActionsRunOutWithDefaultAction) {
00244 ON_CALL(foo_, Bar2(_, _))
00245 .WillByDefault(Return(true));
00246
00247 EXPECT_CALL(foo_, Bar2(_, _))
00248 .Times(2)
00249 .WillOnce(Return(false));
00250 foo_.Bar2(2, 2);
00251 foo_.Bar2(1, 1);
00252 }
00253
00254 TEST_F(GMockOutputTest, CatchesLeakedMocks) {
00255 MockFoo* foo1 = new MockFoo;
00256 MockFoo* foo2 = new MockFoo;
00257
00258
00259 ON_CALL(*foo1, Bar(_, _, _)).WillByDefault(Return('a'));
00260
00261
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
00269 }
00270
00271 void TestCatchesLeakedMocksInAdHocTests() {
00272 MockFoo* foo = new MockFoo;
00273
00274
00275 EXPECT_CALL(*foo, Bar2(_, _));
00276 foo->Bar2(2, 1);
00277
00278
00279 }
00280
00281 int main(int argc, char **argv) {
00282 testing::InitGoogleMock(&argc, argv);
00283
00284
00285
00286 testing::GMOCK_FLAG(catch_leaked_mocks) = true;
00287 testing::GMOCK_FLAG(verbose) = "warning";
00288
00289 TestCatchesLeakedMocksInAdHocTests();
00290 return RUN_ALL_TESTS();
00291 }