blob: 5d6ccc4f100f4c6410189a5c80c67c6b32053b00 [file] [log] [blame]
shiqiane35fdd92008-12-10 05:08:54 +00001// Copyright 2008, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
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
13// distribution.
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.
17//
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.
29//
30// Author: wan@google.com (Zhanyong Wan)
31
zhanyong.wan53e08c42010-09-14 05:38:21 +000032#include "gmock/gmock-generated-nice-strict.h"
shiqiane35fdd92008-12-10 05:08:54 +000033
34#include <string>
zhanyong.wan53e08c42010-09-14 05:38:21 +000035#include "gmock/gmock.h"
36#include "gtest/gtest.h"
37#include "gtest/gtest-spi.h"
shiqiane35fdd92008-12-10 05:08:54 +000038
zhanyong.wan4bd79e42009-09-16 17:38:08 +000039// This must not be defined inside the ::testing namespace, or it will
40// clash with ::testing::Mock.
41class Mock {
42 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +000043 Mock() {}
44
zhanyong.wan4bd79e42009-09-16 17:38:08 +000045 MOCK_METHOD0(DoThis, void());
zhanyong.wan32de5f52009-12-23 00:13:23 +000046
47 private:
48 GTEST_DISALLOW_COPY_AND_ASSIGN_(Mock);
zhanyong.wan4bd79e42009-09-16 17:38:08 +000049};
50
shiqiane35fdd92008-12-10 05:08:54 +000051namespace testing {
52namespace gmock_nice_strict_test {
53
shiqiane35fdd92008-12-10 05:08:54 +000054using testing::GMOCK_FLAG(verbose);
55using testing::HasSubstr;
zhanyong.wan844fa942013-03-01 01:54:22 +000056using testing::NaggyMock;
shiqiane35fdd92008-12-10 05:08:54 +000057using testing::NiceMock;
58using testing::StrictMock;
59
zhanyong.wan2516f602010-08-31 18:28:02 +000060#if GTEST_HAS_STREAM_REDIRECTION
zhanyong.wan470df422010-02-02 22:34:58 +000061using testing::internal::CaptureStdout;
62using testing::internal::GetCapturedStdout;
zhanyong.wan2516f602010-08-31 18:28:02 +000063#endif
zhanyong.wan470df422010-02-02 22:34:58 +000064
shiqiane35fdd92008-12-10 05:08:54 +000065// Defines some mock classes needed by the tests.
66
67class Foo {
68 public:
69 virtual ~Foo() {}
70
71 virtual void DoThis() = 0;
72 virtual int DoThat(bool flag) = 0;
73};
74
75class MockFoo : public Foo {
76 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +000077 MockFoo() {}
shiqiane35fdd92008-12-10 05:08:54 +000078 void Delete() { delete this; }
79
80 MOCK_METHOD0(DoThis, void());
81 MOCK_METHOD1(DoThat, int(bool flag));
zhanyong.wan32de5f52009-12-23 00:13:23 +000082
83 private:
84 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo);
shiqiane35fdd92008-12-10 05:08:54 +000085};
86
87class MockBar {
88 public:
Nico Weber09fd5b32017-05-15 17:07:03 -040089 explicit MockBar(const std::string& s) : str_(s) {}
shiqiane35fdd92008-12-10 05:08:54 +000090
Nico Weber09fd5b32017-05-15 17:07:03 -040091 MockBar(char a1, char a2, std::string a3, std::string a4, int a5, int a6,
92 const std::string& a7, const std::string& a8, bool a9, bool a10) {
93 str_ = std::string() + a1 + a2 + a3 + a4 + static_cast<char>(a5) +
shiqiane35fdd92008-12-10 05:08:54 +000094 static_cast<char>(a6) + a7 + a8 + (a9 ? 'T' : 'F') + (a10 ? 'T' : 'F');
95 }
96
97 virtual ~MockBar() {}
98
Nico Weber09fd5b32017-05-15 17:07:03 -040099 const std::string& str() const { return str_; }
shiqiane35fdd92008-12-10 05:08:54 +0000100
101 MOCK_METHOD0(This, int());
Nico Weber09fd5b32017-05-15 17:07:03 -0400102 MOCK_METHOD2(That, std::string(int, bool));
shiqiane35fdd92008-12-10 05:08:54 +0000103
104 private:
Nico Weber09fd5b32017-05-15 17:07:03 -0400105 std::string str_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000106
107 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockBar);
shiqiane35fdd92008-12-10 05:08:54 +0000108};
109
zhanyong.wan2516f602010-08-31 18:28:02 +0000110#if GTEST_HAS_STREAM_REDIRECTION
shiqiane35fdd92008-12-10 05:08:54 +0000111
zhanyong.wanc8965042013-03-01 07:10:07 +0000112// Tests that a raw mock generates warnings for uninteresting calls.
113TEST(RawMockTest, WarningForUninterestingCall) {
Nico Weber09fd5b32017-05-15 17:07:03 -0400114 const std::string saved_flag = GMOCK_FLAG(verbose);
zhanyong.wanc8965042013-03-01 07:10:07 +0000115 GMOCK_FLAG(verbose) = "warning";
116
117 MockFoo raw_foo;
118
119 CaptureStdout();
120 raw_foo.DoThis();
121 raw_foo.DoThat(true);
122 EXPECT_THAT(GetCapturedStdout(),
123 HasSubstr("Uninteresting mock function call"));
124
125 GMOCK_FLAG(verbose) = saved_flag;
126}
127
128// Tests that a raw mock generates warnings for uninteresting calls
129// that delete the mock object.
130TEST(RawMockTest, WarningForUninterestingCallAfterDeath) {
Nico Weber09fd5b32017-05-15 17:07:03 -0400131 const std::string saved_flag = GMOCK_FLAG(verbose);
zhanyong.wanc8965042013-03-01 07:10:07 +0000132 GMOCK_FLAG(verbose) = "warning";
133
134 MockFoo* const raw_foo = new MockFoo;
135
136 ON_CALL(*raw_foo, DoThis())
137 .WillByDefault(Invoke(raw_foo, &MockFoo::Delete));
138
139 CaptureStdout();
140 raw_foo->DoThis();
141 EXPECT_THAT(GetCapturedStdout(),
142 HasSubstr("Uninteresting mock function call"));
143
144 GMOCK_FLAG(verbose) = saved_flag;
145}
146
147// Tests that a raw mock generates informational logs for
148// uninteresting calls.
149TEST(RawMockTest, InfoForUninterestingCall) {
150 MockFoo raw_foo;
151
Nico Weber09fd5b32017-05-15 17:07:03 -0400152 const std::string saved_flag = GMOCK_FLAG(verbose);
zhanyong.wanc8965042013-03-01 07:10:07 +0000153 GMOCK_FLAG(verbose) = "info";
154 CaptureStdout();
155 raw_foo.DoThis();
156 EXPECT_THAT(GetCapturedStdout(),
157 HasSubstr("Uninteresting mock function call"));
158
159 GMOCK_FLAG(verbose) = saved_flag;
160}
161
shiqiane35fdd92008-12-10 05:08:54 +0000162// Tests that a nice mock generates no warning for uninteresting calls.
163TEST(NiceMockTest, NoWarningForUninterestingCall) {
164 NiceMock<MockFoo> nice_foo;
165
zhanyong.wan470df422010-02-02 22:34:58 +0000166 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +0000167 nice_foo.DoThis();
168 nice_foo.DoThat(true);
zhanyong.wan844fa942013-03-01 01:54:22 +0000169 EXPECT_EQ("", GetCapturedStdout());
shiqiane35fdd92008-12-10 05:08:54 +0000170}
171
172// Tests that a nice mock generates no warning for uninteresting calls
173// that delete the mock object.
174TEST(NiceMockTest, NoWarningForUninterestingCallAfterDeath) {
175 NiceMock<MockFoo>* const nice_foo = new NiceMock<MockFoo>;
176
177 ON_CALL(*nice_foo, DoThis())
178 .WillByDefault(Invoke(nice_foo, &MockFoo::Delete));
179
zhanyong.wan470df422010-02-02 22:34:58 +0000180 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +0000181 nice_foo->DoThis();
zhanyong.wan844fa942013-03-01 01:54:22 +0000182 EXPECT_EQ("", GetCapturedStdout());
shiqiane35fdd92008-12-10 05:08:54 +0000183}
184
185// Tests that a nice mock generates informational logs for
186// uninteresting calls.
187TEST(NiceMockTest, InfoForUninterestingCall) {
188 NiceMock<MockFoo> nice_foo;
189
Nico Weber09fd5b32017-05-15 17:07:03 -0400190 const std::string saved_flag = GMOCK_FLAG(verbose);
shiqiane35fdd92008-12-10 05:08:54 +0000191 GMOCK_FLAG(verbose) = "info";
zhanyong.wan470df422010-02-02 22:34:58 +0000192 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +0000193 nice_foo.DoThis();
zhanyong.wan844fa942013-03-01 01:54:22 +0000194 EXPECT_THAT(GetCapturedStdout(),
shiqiane35fdd92008-12-10 05:08:54 +0000195 HasSubstr("Uninteresting mock function call"));
196
vladlosev76c1c612010-05-05 19:47:46 +0000197 GMOCK_FLAG(verbose) = saved_flag;
shiqiane35fdd92008-12-10 05:08:54 +0000198}
199
zhanyong.wan2516f602010-08-31 18:28:02 +0000200#endif // GTEST_HAS_STREAM_REDIRECTION
shiqiane35fdd92008-12-10 05:08:54 +0000201
202// Tests that a nice mock allows expected calls.
203TEST(NiceMockTest, AllowsExpectedCall) {
204 NiceMock<MockFoo> nice_foo;
205
206 EXPECT_CALL(nice_foo, DoThis());
207 nice_foo.DoThis();
208}
209
210// Tests that an unexpected call on a nice mock fails.
211TEST(NiceMockTest, UnexpectedCallFails) {
212 NiceMock<MockFoo> nice_foo;
213
214 EXPECT_CALL(nice_foo, DoThis()).Times(0);
215 EXPECT_NONFATAL_FAILURE(nice_foo.DoThis(), "called more times than expected");
216}
217
218// Tests that NiceMock works with a mock class that has a non-default
219// constructor.
220TEST(NiceMockTest, NonDefaultConstructor) {
221 NiceMock<MockBar> nice_bar("hi");
222 EXPECT_EQ("hi", nice_bar.str());
223
224 nice_bar.This();
225 nice_bar.That(5, true);
226}
227
228// Tests that NiceMock works with a mock class that has a 10-ary
229// non-default constructor.
230TEST(NiceMockTest, NonDefaultConstructor10) {
231 NiceMock<MockBar> nice_bar('a', 'b', "c", "d", 'e', 'f',
232 "g", "h", true, false);
233 EXPECT_EQ("abcdefghTF", nice_bar.str());
234
235 nice_bar.This();
236 nice_bar.That(5, true);
237}
238
vladlosev6c54a5e2009-10-21 06:15:34 +0000239#if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000240// Tests that NiceMock<Mock> compiles where Mock is a user-defined
zhanyong.wan844fa942013-03-01 01:54:22 +0000241// class (as opposed to ::testing::Mock). We had to work around an
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000242// MSVC 8.0 bug that caused the symbol Mock used in the definition of
243// NiceMock to be looked up in the wrong context, and this test
244// ensures that our fix works.
zhanyong.wan93244dc2009-09-17 19:11:00 +0000245//
vladlosev6c54a5e2009-10-21 06:15:34 +0000246// We have to skip this test on Symbian and Windows Mobile, as it
247// causes the program to crash there, for reasons unclear to us yet.
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000248TEST(NiceMockTest, AcceptsClassNamedMock) {
249 NiceMock< ::Mock> nice;
250 EXPECT_CALL(nice, DoThis());
251 nice.DoThis();
252}
vladlosev6c54a5e2009-10-21 06:15:34 +0000253#endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000254
zhanyong.wan844fa942013-03-01 01:54:22 +0000255#if GTEST_HAS_STREAM_REDIRECTION
256
257// Tests that a naggy mock generates warnings for uninteresting calls.
258TEST(NaggyMockTest, WarningForUninterestingCall) {
Nico Weber09fd5b32017-05-15 17:07:03 -0400259 const std::string saved_flag = GMOCK_FLAG(verbose);
zhanyong.wan844fa942013-03-01 01:54:22 +0000260 GMOCK_FLAG(verbose) = "warning";
261
262 NaggyMock<MockFoo> naggy_foo;
263
264 CaptureStdout();
265 naggy_foo.DoThis();
266 naggy_foo.DoThat(true);
267 EXPECT_THAT(GetCapturedStdout(),
268 HasSubstr("Uninteresting mock function call"));
269
270 GMOCK_FLAG(verbose) = saved_flag;
271}
272
273// Tests that a naggy mock generates a warning for an uninteresting call
274// that deletes the mock object.
275TEST(NaggyMockTest, WarningForUninterestingCallAfterDeath) {
Nico Weber09fd5b32017-05-15 17:07:03 -0400276 const std::string saved_flag = GMOCK_FLAG(verbose);
zhanyong.wan844fa942013-03-01 01:54:22 +0000277 GMOCK_FLAG(verbose) = "warning";
278
279 NaggyMock<MockFoo>* const naggy_foo = new NaggyMock<MockFoo>;
280
281 ON_CALL(*naggy_foo, DoThis())
282 .WillByDefault(Invoke(naggy_foo, &MockFoo::Delete));
283
284 CaptureStdout();
285 naggy_foo->DoThis();
286 EXPECT_THAT(GetCapturedStdout(),
287 HasSubstr("Uninteresting mock function call"));
288
289 GMOCK_FLAG(verbose) = saved_flag;
290}
291
292#endif // GTEST_HAS_STREAM_REDIRECTION
293
294// Tests that a naggy mock allows expected calls.
295TEST(NaggyMockTest, AllowsExpectedCall) {
296 NaggyMock<MockFoo> naggy_foo;
297
298 EXPECT_CALL(naggy_foo, DoThis());
299 naggy_foo.DoThis();
300}
301
302// Tests that an unexpected call on a naggy mock fails.
303TEST(NaggyMockTest, UnexpectedCallFails) {
304 NaggyMock<MockFoo> naggy_foo;
305
306 EXPECT_CALL(naggy_foo, DoThis()).Times(0);
307 EXPECT_NONFATAL_FAILURE(naggy_foo.DoThis(),
308 "called more times than expected");
309}
310
311// Tests that NaggyMock works with a mock class that has a non-default
312// constructor.
313TEST(NaggyMockTest, NonDefaultConstructor) {
314 NaggyMock<MockBar> naggy_bar("hi");
315 EXPECT_EQ("hi", naggy_bar.str());
316
317 naggy_bar.This();
318 naggy_bar.That(5, true);
319}
320
321// Tests that NaggyMock works with a mock class that has a 10-ary
322// non-default constructor.
323TEST(NaggyMockTest, NonDefaultConstructor10) {
324 NaggyMock<MockBar> naggy_bar('0', '1', "2", "3", '4', '5',
325 "6", "7", true, false);
326 EXPECT_EQ("01234567TF", naggy_bar.str());
327
328 naggy_bar.This();
329 naggy_bar.That(5, true);
330}
331
332#if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
333// Tests that NaggyMock<Mock> compiles where Mock is a user-defined
334// class (as opposed to ::testing::Mock). We had to work around an
335// MSVC 8.0 bug that caused the symbol Mock used in the definition of
336// NaggyMock to be looked up in the wrong context, and this test
337// ensures that our fix works.
338//
339// We have to skip this test on Symbian and Windows Mobile, as it
340// causes the program to crash there, for reasons unclear to us yet.
341TEST(NaggyMockTest, AcceptsClassNamedMock) {
342 NaggyMock< ::Mock> naggy;
343 EXPECT_CALL(naggy, DoThis());
344 naggy.DoThis();
345}
346#endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
347
shiqiane35fdd92008-12-10 05:08:54 +0000348// Tests that a strict mock allows expected calls.
349TEST(StrictMockTest, AllowsExpectedCall) {
350 StrictMock<MockFoo> strict_foo;
351
352 EXPECT_CALL(strict_foo, DoThis());
353 strict_foo.DoThis();
354}
355
356// Tests that an unexpected call on a strict mock fails.
357TEST(StrictMockTest, UnexpectedCallFails) {
358 StrictMock<MockFoo> strict_foo;
359
360 EXPECT_CALL(strict_foo, DoThis()).Times(0);
361 EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
362 "called more times than expected");
363}
364
365// Tests that an uninteresting call on a strict mock fails.
366TEST(StrictMockTest, UninterestingCallFails) {
367 StrictMock<MockFoo> strict_foo;
368
369 EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
370 "Uninteresting mock function call");
371}
372
373// Tests that an uninteresting call on a strict mock fails, even if
374// the call deletes the mock object.
375TEST(StrictMockTest, UninterestingCallFailsAfterDeath) {
376 StrictMock<MockFoo>* const strict_foo = new StrictMock<MockFoo>;
377
378 ON_CALL(*strict_foo, DoThis())
379 .WillByDefault(Invoke(strict_foo, &MockFoo::Delete));
380
381 EXPECT_NONFATAL_FAILURE(strict_foo->DoThis(),
382 "Uninteresting mock function call");
383}
384
385// Tests that StrictMock works with a mock class that has a
386// non-default constructor.
387TEST(StrictMockTest, NonDefaultConstructor) {
388 StrictMock<MockBar> strict_bar("hi");
389 EXPECT_EQ("hi", strict_bar.str());
390
391 EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
392 "Uninteresting mock function call");
393}
394
395// Tests that StrictMock works with a mock class that has a 10-ary
396// non-default constructor.
397TEST(StrictMockTest, NonDefaultConstructor10) {
398 StrictMock<MockBar> strict_bar('a', 'b', "c", "d", 'e', 'f',
399 "g", "h", true, false);
400 EXPECT_EQ("abcdefghTF", strict_bar.str());
401
402 EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
403 "Uninteresting mock function call");
404}
405
vladlosev6c54a5e2009-10-21 06:15:34 +0000406#if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000407// Tests that StrictMock<Mock> compiles where Mock is a user-defined
zhanyong.wan844fa942013-03-01 01:54:22 +0000408// class (as opposed to ::testing::Mock). We had to work around an
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000409// MSVC 8.0 bug that caused the symbol Mock used in the definition of
410// StrictMock to be looked up in the wrong context, and this test
411// ensures that our fix works.
zhanyong.wan93244dc2009-09-17 19:11:00 +0000412//
vladlosev6c54a5e2009-10-21 06:15:34 +0000413// We have to skip this test on Symbian and Windows Mobile, as it
414// causes the program to crash there, for reasons unclear to us yet.
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000415TEST(StrictMockTest, AcceptsClassNamedMock) {
zhanyong.wan93244dc2009-09-17 19:11:00 +0000416 StrictMock< ::Mock> strict;
417 EXPECT_CALL(strict, DoThis());
418 strict.DoThis();
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000419}
vladlosev6c54a5e2009-10-21 06:15:34 +0000420#endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000421
shiqiane35fdd92008-12-10 05:08:54 +0000422} // namespace gmock_nice_strict_test
423} // namespace testing