File indexing completed on 2025-08-06 08:19:58
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 #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
0040
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
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
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
0130
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
0149
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
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
0174
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
0187
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
0202
0203
0204 TEST(NiceMockTest, AllowsExpectedCall) {
0205 NiceMock<MockFoo> nice_foo;
0206
0207 EXPECT_CALL(nice_foo, DoThis());
0208 nice_foo.DoThis();
0209 }
0210
0211
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
0220
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
0230
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
0242
0243
0244
0245
0246
0247
0248
0249 TEST(NiceMockTest, AcceptsClassNamedMock) {
0250 NiceMock< ::Mock> nice;
0251 EXPECT_CALL(nice, DoThis());
0252 nice.DoThis();
0253 }
0254 #endif
0255
0256 #if GTEST_HAS_STREAM_REDIRECTION
0257
0258
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
0275
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
0294
0295
0296 TEST(NaggyMockTest, AllowsExpectedCall) {
0297 NaggyMock<MockFoo> naggy_foo;
0298
0299 EXPECT_CALL(naggy_foo, DoThis());
0300 naggy_foo.DoThis();
0301 }
0302
0303
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
0313
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
0323
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
0335
0336
0337
0338
0339
0340
0341
0342 TEST(NaggyMockTest, AcceptsClassNamedMock) {
0343 NaggyMock< ::Mock> naggy;
0344 EXPECT_CALL(naggy, DoThis());
0345 naggy.DoThis();
0346 }
0347 #endif
0348
0349
0350 TEST(StrictMockTest, AllowsExpectedCall) {
0351 StrictMock<MockFoo> strict_foo;
0352
0353 EXPECT_CALL(strict_foo, DoThis());
0354 strict_foo.DoThis();
0355 }
0356
0357
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
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
0375
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
0387
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
0397
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
0409
0410
0411
0412
0413
0414
0415
0416 TEST(StrictMockTest, AcceptsClassNamedMock) {
0417 StrictMock< ::Mock> strict;
0418 EXPECT_CALL(strict, DoThis());
0419 strict.DoThis();
0420 }
0421 #endif
0422
0423 }
0424 }