File indexing completed on 2025-08-06 08:19:59
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035 #include "gmock/gmock.h"
0036
0037 #include <stdio.h>
0038 #include <string>
0039
0040 #include "gtest/gtest.h"
0041
0042 using testing::_;
0043 using testing::AnyNumber;
0044 using testing::Ge;
0045 using testing::InSequence;
0046 using testing::NaggyMock;
0047 using testing::Ref;
0048 using testing::Return;
0049 using testing::Sequence;
0050
0051 class MockFoo {
0052 public:
0053 MockFoo() {}
0054
0055 MOCK_METHOD3(Bar, char(const std::string& s, int i, double x));
0056 MOCK_METHOD2(Bar2, bool(int x, int y));
0057 MOCK_METHOD2(Bar3, void(int x, int y));
0058
0059 private:
0060 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo);
0061 };
0062
0063 class GMockOutputTest : public testing::Test {
0064 protected:
0065 NaggyMock<MockFoo> foo_;
0066 };
0067
0068 TEST_F(GMockOutputTest, ExpectedCall) {
0069 testing::GMOCK_FLAG(verbose) = "info";
0070
0071 EXPECT_CALL(foo_, Bar2(0, _));
0072 foo_.Bar2(0, 0);
0073
0074 testing::GMOCK_FLAG(verbose) = "warning";
0075 }
0076
0077 TEST_F(GMockOutputTest, ExpectedCallToVoidFunction) {
0078 testing::GMOCK_FLAG(verbose) = "info";
0079
0080 EXPECT_CALL(foo_, Bar3(0, _));
0081 foo_.Bar3(0, 0);
0082
0083 testing::GMOCK_FLAG(verbose) = "warning";
0084 }
0085
0086 TEST_F(GMockOutputTest, ExplicitActionsRunOut) {
0087 EXPECT_CALL(foo_, Bar2(_, _))
0088 .Times(2)
0089 .WillOnce(Return(false));
0090 foo_.Bar2(2, 2);
0091 foo_.Bar2(1, 1);
0092 }
0093
0094 TEST_F(GMockOutputTest, UnexpectedCall) {
0095 EXPECT_CALL(foo_, Bar2(0, _));
0096
0097 foo_.Bar2(1, 0);
0098 foo_.Bar2(0, 0);
0099 }
0100
0101 TEST_F(GMockOutputTest, UnexpectedCallToVoidFunction) {
0102 EXPECT_CALL(foo_, Bar3(0, _));
0103
0104 foo_.Bar3(1, 0);
0105 foo_.Bar3(0, 0);
0106 }
0107
0108 TEST_F(GMockOutputTest, ExcessiveCall) {
0109 EXPECT_CALL(foo_, Bar2(0, _));
0110
0111 foo_.Bar2(0, 0);
0112 foo_.Bar2(0, 1);
0113 }
0114
0115 TEST_F(GMockOutputTest, ExcessiveCallToVoidFunction) {
0116 EXPECT_CALL(foo_, Bar3(0, _));
0117
0118 foo_.Bar3(0, 0);
0119 foo_.Bar3(0, 1);
0120 }
0121
0122 TEST_F(GMockOutputTest, UninterestingCall) {
0123 foo_.Bar2(0, 1);
0124 }
0125
0126 TEST_F(GMockOutputTest, UninterestingCallToVoidFunction) {
0127 foo_.Bar3(0, 1);
0128 }
0129
0130 TEST_F(GMockOutputTest, RetiredExpectation) {
0131 EXPECT_CALL(foo_, Bar2(_, _))
0132 .RetiresOnSaturation();
0133 EXPECT_CALL(foo_, Bar2(0, 0));
0134
0135 foo_.Bar2(1, 1);
0136 foo_.Bar2(1, 1);
0137 foo_.Bar2(0, 0);
0138 }
0139
0140 TEST_F(GMockOutputTest, UnsatisfiedPrerequisite) {
0141 {
0142 InSequence s;
0143 EXPECT_CALL(foo_, Bar(_, 0, _));
0144 EXPECT_CALL(foo_, Bar2(0, 0));
0145 EXPECT_CALL(foo_, Bar2(1, _));
0146 }
0147
0148 foo_.Bar2(1, 0);
0149 foo_.Bar("Hi", 0, 0);
0150 foo_.Bar2(0, 0);
0151 foo_.Bar2(1, 0);
0152 }
0153
0154 TEST_F(GMockOutputTest, UnsatisfiedPrerequisites) {
0155 Sequence s1, s2;
0156
0157 EXPECT_CALL(foo_, Bar(_, 0, _))
0158 .InSequence(s1);
0159 EXPECT_CALL(foo_, Bar2(0, 0))
0160 .InSequence(s2);
0161 EXPECT_CALL(foo_, Bar2(1, _))
0162 .InSequence(s1, s2);
0163
0164 foo_.Bar2(1, 0);
0165 foo_.Bar("Hi", 0, 0);
0166 foo_.Bar2(0, 0);
0167 foo_.Bar2(1, 0);
0168 }
0169
0170 TEST_F(GMockOutputTest, UnsatisfiedWith) {
0171 EXPECT_CALL(foo_, Bar2(_, _)).With(Ge());
0172 }
0173
0174 TEST_F(GMockOutputTest, UnsatisfiedExpectation) {
0175 EXPECT_CALL(foo_, Bar(_, _, _));
0176 EXPECT_CALL(foo_, Bar2(0, _))
0177 .Times(2);
0178
0179 foo_.Bar2(0, 1);
0180 }
0181
0182 TEST_F(GMockOutputTest, MismatchArguments) {
0183 const std::string s = "Hi";
0184 EXPECT_CALL(foo_, Bar(Ref(s), _, Ge(0)));
0185
0186 foo_.Bar("Ho", 0, -0.1);
0187 foo_.Bar(s, 0, 0);
0188 }
0189
0190 TEST_F(GMockOutputTest, MismatchWith) {
0191 EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1)))
0192 .With(Ge());
0193
0194 foo_.Bar2(2, 3);
0195 foo_.Bar2(2, 1);
0196 }
0197
0198 TEST_F(GMockOutputTest, MismatchArgumentsAndWith) {
0199 EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1)))
0200 .With(Ge());
0201
0202 foo_.Bar2(1, 3);
0203 foo_.Bar2(2, 1);
0204 }
0205
0206 TEST_F(GMockOutputTest, UnexpectedCallWithDefaultAction) {
0207 ON_CALL(foo_, Bar2(_, _))
0208 .WillByDefault(Return(true));
0209 ON_CALL(foo_, Bar2(1, _))
0210 .WillByDefault(Return(false));
0211
0212 EXPECT_CALL(foo_, Bar2(2, 2));
0213 foo_.Bar2(1, 0);
0214 foo_.Bar2(0, 0);
0215 foo_.Bar2(2, 2);
0216 }
0217
0218 TEST_F(GMockOutputTest, ExcessiveCallWithDefaultAction) {
0219 ON_CALL(foo_, Bar2(_, _))
0220 .WillByDefault(Return(true));
0221 ON_CALL(foo_, Bar2(1, _))
0222 .WillByDefault(Return(false));
0223
0224 EXPECT_CALL(foo_, Bar2(2, 2));
0225 EXPECT_CALL(foo_, Bar2(1, 1));
0226
0227 foo_.Bar2(2, 2);
0228 foo_.Bar2(2, 2);
0229 foo_.Bar2(1, 1);
0230 foo_.Bar2(1, 1);
0231 }
0232
0233 TEST_F(GMockOutputTest, UninterestingCallWithDefaultAction) {
0234 ON_CALL(foo_, Bar2(_, _))
0235 .WillByDefault(Return(true));
0236 ON_CALL(foo_, Bar2(1, _))
0237 .WillByDefault(Return(false));
0238
0239 foo_.Bar2(2, 2);
0240 foo_.Bar2(1, 1);
0241 }
0242
0243 TEST_F(GMockOutputTest, ExplicitActionsRunOutWithDefaultAction) {
0244 ON_CALL(foo_, Bar2(_, _))
0245 .WillByDefault(Return(true));
0246
0247 EXPECT_CALL(foo_, Bar2(_, _))
0248 .Times(2)
0249 .WillOnce(Return(false));
0250 foo_.Bar2(2, 2);
0251 foo_.Bar2(1, 1);
0252 }
0253
0254 TEST_F(GMockOutputTest, CatchesLeakedMocks) {
0255 MockFoo* foo1 = new MockFoo;
0256 MockFoo* foo2 = new MockFoo;
0257
0258
0259 ON_CALL(*foo1, Bar(_, _, _)).WillByDefault(Return('a'));
0260
0261
0262 EXPECT_CALL(*foo2, Bar2(_, _));
0263 EXPECT_CALL(*foo2, Bar2(1, _));
0264 EXPECT_CALL(*foo2, Bar3(_, _)).Times(AnyNumber());
0265 foo2->Bar2(2, 1);
0266 foo2->Bar2(1, 1);
0267
0268
0269 }
0270
0271 void TestCatchesLeakedMocksInAdHocTests() {
0272 MockFoo* foo = new MockFoo;
0273
0274
0275 EXPECT_CALL(*foo, Bar2(_, _));
0276 foo->Bar2(2, 1);
0277
0278
0279 }
0280
0281 int main(int argc, char **argv) {
0282 testing::InitGoogleMock(&argc, argv);
0283
0284
0285
0286 testing::GMOCK_FLAG(catch_leaked_mocks) = true;
0287 testing::GMOCK_FLAG(verbose) = "warning";
0288
0289 TestCatchesLeakedMocksInAdHocTests();
0290 return RUN_ALL_TESTS();
0291 }