1 // Copyright 2008, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 // Author: wan@google.com (Zhanyong Wan)
32 #include "gmock/gmock-generated-nice-strict.h"
35 #include "gmock/gmock.h"
36 #include "gtest/gtest.h"
37 #include "gtest/gtest-spi.h"
39 // This must not be defined inside the ::testing namespace, or it will
40 // clash with ::testing::Mock.
45 MOCK_METHOD0(DoThis, void());
48 GTEST_DISALLOW_COPY_AND_ASSIGN_(Mock);
52 namespace gmock_nice_strict_test {
54 using testing::internal::string;
55 using testing::GMOCK_FLAG(verbose);
56 using testing::HasSubstr;
57 using testing::NaggyMock;
58 using testing::NiceMock;
59 using testing::StrictMock;
61 #if GTEST_HAS_STREAM_REDIRECTION
62 using testing::internal::CaptureStdout;
63 using testing::internal::GetCapturedStdout;
66 // Defines some mock classes needed by the tests.
72 virtual void DoThis() = 0;
73 virtual int DoThat(bool flag) = 0;
76 class MockFoo : public Foo {
79 void Delete() { delete this; }
81 MOCK_METHOD0(DoThis, void());
82 MOCK_METHOD1(DoThat, int(bool flag));
85 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo);
90 explicit MockBar(const string& s) : str_(s) {}
92 MockBar(char a1, char a2, string a3, string a4, int a5, int a6,
93 const string& a7, const string& a8, bool a9, bool a10) {
94 str_ = string() + a1 + a2 + a3 + a4 + static_cast<char>(a5) +
95 static_cast<char>(a6) + a7 + a8 + (a9 ? 'T' : 'F') + (a10 ? 'T' : 'F');
100 const string& str() const { return str_; }
102 MOCK_METHOD0(This, int());
103 MOCK_METHOD2(That, string(int, bool));
108 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockBar);
111 #if GTEST_HAS_STREAM_REDIRECTION
113 // Tests that a raw mock generates warnings for uninteresting calls.
114 TEST(RawMockTest, WarningForUninterestingCall) {
115 const string saved_flag = GMOCK_FLAG(verbose);
116 GMOCK_FLAG(verbose) = "warning";
122 raw_foo.DoThat(true);
123 EXPECT_THAT(GetCapturedStdout(),
124 HasSubstr("Uninteresting mock function call"));
126 GMOCK_FLAG(verbose) = saved_flag;
129 // Tests that a raw mock generates warnings for uninteresting calls
130 // that delete the mock object.
131 TEST(RawMockTest, WarningForUninterestingCallAfterDeath) {
132 const string saved_flag = GMOCK_FLAG(verbose);
133 GMOCK_FLAG(verbose) = "warning";
135 MockFoo* const raw_foo = new MockFoo;
137 ON_CALL(*raw_foo, DoThis())
138 .WillByDefault(Invoke(raw_foo, &MockFoo::Delete));
142 EXPECT_THAT(GetCapturedStdout(),
143 HasSubstr("Uninteresting mock function call"));
145 GMOCK_FLAG(verbose) = saved_flag;
148 // Tests that a raw mock generates informational logs for
149 // uninteresting calls.
150 TEST(RawMockTest, InfoForUninterestingCall) {
153 const string saved_flag = GMOCK_FLAG(verbose);
154 GMOCK_FLAG(verbose) = "info";
157 EXPECT_THAT(GetCapturedStdout(),
158 HasSubstr("Uninteresting mock function call"));
160 GMOCK_FLAG(verbose) = saved_flag;
163 // Tests that a nice mock generates no warning for uninteresting calls.
164 TEST(NiceMockTest, NoWarningForUninterestingCall) {
165 NiceMock<MockFoo> nice_foo;
169 nice_foo.DoThat(true);
170 EXPECT_EQ("", GetCapturedStdout());
173 // Tests that a nice mock generates no warning for uninteresting calls
174 // that delete the mock object.
175 TEST(NiceMockTest, NoWarningForUninterestingCallAfterDeath) {
176 NiceMock<MockFoo>* const nice_foo = new NiceMock<MockFoo>;
178 ON_CALL(*nice_foo, DoThis())
179 .WillByDefault(Invoke(nice_foo, &MockFoo::Delete));
183 EXPECT_EQ("", GetCapturedStdout());
186 // Tests that a nice mock generates informational logs for
187 // uninteresting calls.
188 TEST(NiceMockTest, InfoForUninterestingCall) {
189 NiceMock<MockFoo> nice_foo;
191 const string saved_flag = GMOCK_FLAG(verbose);
192 GMOCK_FLAG(verbose) = "info";
195 EXPECT_THAT(GetCapturedStdout(),
196 HasSubstr("Uninteresting mock function call"));
198 GMOCK_FLAG(verbose) = saved_flag;
201 #endif // GTEST_HAS_STREAM_REDIRECTION
203 // Tests that a nice mock allows expected calls.
204 TEST(NiceMockTest, AllowsExpectedCall) {
205 NiceMock<MockFoo> nice_foo;
207 EXPECT_CALL(nice_foo, DoThis());
211 // Tests that an unexpected call on a nice mock fails.
212 TEST(NiceMockTest, UnexpectedCallFails) {
213 NiceMock<MockFoo> nice_foo;
215 EXPECT_CALL(nice_foo, DoThis()).Times(0);
216 EXPECT_NONFATAL_FAILURE(nice_foo.DoThis(), "called more times than expected");
219 // Tests that NiceMock works with a mock class that has a non-default
221 TEST(NiceMockTest, NonDefaultConstructor) {
222 NiceMock<MockBar> nice_bar("hi");
223 EXPECT_EQ("hi", nice_bar.str());
226 nice_bar.That(5, true);
229 // Tests that NiceMock works with a mock class that has a 10-ary
230 // non-default constructor.
231 TEST(NiceMockTest, NonDefaultConstructor10) {
232 NiceMock<MockBar> nice_bar('a', 'b', "c", "d", 'e', 'f',
233 "g", "h", true, false);
234 EXPECT_EQ("abcdefghTF", nice_bar.str());
237 nice_bar.That(5, true);
240 #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
241 // Tests that NiceMock<Mock> compiles where Mock is a user-defined
242 // class (as opposed to ::testing::Mock). We had to work around an
243 // MSVC 8.0 bug that caused the symbol Mock used in the definition of
244 // NiceMock to be looked up in the wrong context, and this test
245 // ensures that our fix works.
247 // We have to skip this test on Symbian and Windows Mobile, as it
248 // causes the program to crash there, for reasons unclear to us yet.
249 TEST(NiceMockTest, AcceptsClassNamedMock) {
250 NiceMock< ::Mock> nice;
251 EXPECT_CALL(nice, DoThis());
254 #endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
256 #if GTEST_HAS_STREAM_REDIRECTION
258 // Tests that a naggy mock generates warnings for uninteresting calls.
259 TEST(NaggyMockTest, WarningForUninterestingCall) {
260 const string saved_flag = GMOCK_FLAG(verbose);
261 GMOCK_FLAG(verbose) = "warning";
263 NaggyMock<MockFoo> naggy_foo;
267 naggy_foo.DoThat(true);
268 EXPECT_THAT(GetCapturedStdout(),
269 HasSubstr("Uninteresting mock function call"));
271 GMOCK_FLAG(verbose) = saved_flag;
274 // Tests that a naggy mock generates a warning for an uninteresting call
275 // that deletes the mock object.
276 TEST(NaggyMockTest, WarningForUninterestingCallAfterDeath) {
277 const string saved_flag = GMOCK_FLAG(verbose);
278 GMOCK_FLAG(verbose) = "warning";
280 NaggyMock<MockFoo>* const naggy_foo = new NaggyMock<MockFoo>;
282 ON_CALL(*naggy_foo, DoThis())
283 .WillByDefault(Invoke(naggy_foo, &MockFoo::Delete));
287 EXPECT_THAT(GetCapturedStdout(),
288 HasSubstr("Uninteresting mock function call"));
290 GMOCK_FLAG(verbose) = saved_flag;
293 #endif // GTEST_HAS_STREAM_REDIRECTION
295 // Tests that a naggy mock allows expected calls.
296 TEST(NaggyMockTest, AllowsExpectedCall) {
297 NaggyMock<MockFoo> naggy_foo;
299 EXPECT_CALL(naggy_foo, DoThis());
303 // Tests that an unexpected call on a naggy mock fails.
304 TEST(NaggyMockTest, UnexpectedCallFails) {
305 NaggyMock<MockFoo> naggy_foo;
307 EXPECT_CALL(naggy_foo, DoThis()).Times(0);
308 EXPECT_NONFATAL_FAILURE(naggy_foo.DoThis(),
309 "called more times than expected");
312 // Tests that NaggyMock works with a mock class that has a non-default
314 TEST(NaggyMockTest, NonDefaultConstructor) {
315 NaggyMock<MockBar> naggy_bar("hi");
316 EXPECT_EQ("hi", naggy_bar.str());
319 naggy_bar.That(5, true);
322 // Tests that NaggyMock works with a mock class that has a 10-ary
323 // non-default constructor.
324 TEST(NaggyMockTest, NonDefaultConstructor10) {
325 NaggyMock<MockBar> naggy_bar('0', '1', "2", "3", '4', '5',
326 "6", "7", true, false);
327 EXPECT_EQ("01234567TF", naggy_bar.str());
330 naggy_bar.That(5, true);
333 #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
334 // Tests that NaggyMock<Mock> compiles where Mock is a user-defined
335 // class (as opposed to ::testing::Mock). We had to work around an
336 // MSVC 8.0 bug that caused the symbol Mock used in the definition of
337 // NaggyMock to be looked up in the wrong context, and this test
338 // ensures that our fix works.
340 // We have to skip this test on Symbian and Windows Mobile, as it
341 // causes the program to crash there, for reasons unclear to us yet.
342 TEST(NaggyMockTest, AcceptsClassNamedMock) {
343 NaggyMock< ::Mock> naggy;
344 EXPECT_CALL(naggy, DoThis());
347 #endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
349 // Tests that a strict mock allows expected calls.
350 TEST(StrictMockTest, AllowsExpectedCall) {
351 StrictMock<MockFoo> strict_foo;
353 EXPECT_CALL(strict_foo, DoThis());
357 // Tests that an unexpected call on a strict mock fails.
358 TEST(StrictMockTest, UnexpectedCallFails) {
359 StrictMock<MockFoo> strict_foo;
361 EXPECT_CALL(strict_foo, DoThis()).Times(0);
362 EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
363 "called more times than expected");
366 // Tests that an uninteresting call on a strict mock fails.
367 TEST(StrictMockTest, UninterestingCallFails) {
368 StrictMock<MockFoo> strict_foo;
370 EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
371 "Uninteresting mock function call");
374 // Tests that an uninteresting call on a strict mock fails, even if
375 // the call deletes the mock object.
376 TEST(StrictMockTest, UninterestingCallFailsAfterDeath) {
377 StrictMock<MockFoo>* const strict_foo = new StrictMock<MockFoo>;
379 ON_CALL(*strict_foo, DoThis())
380 .WillByDefault(Invoke(strict_foo, &MockFoo::Delete));
382 EXPECT_NONFATAL_FAILURE(strict_foo->DoThis(),
383 "Uninteresting mock function call");
386 // Tests that StrictMock works with a mock class that has a
387 // non-default constructor.
388 TEST(StrictMockTest, NonDefaultConstructor) {
389 StrictMock<MockBar> strict_bar("hi");
390 EXPECT_EQ("hi", strict_bar.str());
392 EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
393 "Uninteresting mock function call");
396 // Tests that StrictMock works with a mock class that has a 10-ary
397 // non-default constructor.
398 TEST(StrictMockTest, NonDefaultConstructor10) {
399 StrictMock<MockBar> strict_bar('a', 'b', "c", "d", 'e', 'f',
400 "g", "h", true, false);
401 EXPECT_EQ("abcdefghTF", strict_bar.str());
403 EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
404 "Uninteresting mock function call");
407 #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
408 // Tests that StrictMock<Mock> compiles where Mock is a user-defined
409 // class (as opposed to ::testing::Mock). We had to work around an
410 // MSVC 8.0 bug that caused the symbol Mock used in the definition of
411 // StrictMock to be looked up in the wrong context, and this test
412 // ensures that our fix works.
414 // We have to skip this test on Symbian and Windows Mobile, as it
415 // causes the program to crash there, for reasons unclear to us yet.
416 TEST(StrictMockTest, AcceptsClassNamedMock) {
417 StrictMock< ::Mock> strict;
418 EXPECT_CALL(strict, DoThis());
421 #endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
423 } // namespace gmock_nice_strict_test
424 } // namespace testing