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 #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
00040
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
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
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
00130
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
00149
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
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
00174
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
00187
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
00204 TEST(NiceMockTest, AllowsExpectedCall) {
00205 NiceMock<MockFoo> nice_foo;
00206
00207 EXPECT_CALL(nice_foo, DoThis());
00208 nice_foo.DoThis();
00209 }
00210
00211
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
00220
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
00230
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
00242
00243
00244
00245
00246
00247
00248
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
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
00275
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
00296 TEST(NaggyMockTest, AllowsExpectedCall) {
00297 NaggyMock<MockFoo> naggy_foo;
00298
00299 EXPECT_CALL(naggy_foo, DoThis());
00300 naggy_foo.DoThis();
00301 }
00302
00303
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
00313
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
00323
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
00335
00336
00337
00338
00339
00340
00341
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
00350 TEST(StrictMockTest, AllowsExpectedCall) {
00351 StrictMock<MockFoo> strict_foo;
00352
00353 EXPECT_CALL(strict_foo, DoThis());
00354 strict_foo.DoThis();
00355 }
00356
00357
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
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
00375
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
00387
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
00397
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
00409
00410
00411
00412
00413
00414
00415
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 }
00424 }