blob: d0adcbbed80c8ebe39e806bdad39852e94c72fe2 [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
54using testing::internal::string;
55using testing::GMOCK_FLAG(verbose);
56using testing::HasSubstr;
zhanyong.wan844fa942013-03-01 01:54:22 +000057using testing::NaggyMock;
shiqiane35fdd92008-12-10 05:08:54 +000058using testing::NiceMock;
59using testing::StrictMock;
60
zhanyong.wan2516f602010-08-31 18:28:02 +000061#if GTEST_HAS_STREAM_REDIRECTION
zhanyong.wan470df422010-02-02 22:34:58 +000062using testing::internal::CaptureStdout;
63using testing::internal::GetCapturedStdout;
zhanyong.wan2516f602010-08-31 18:28:02 +000064#endif
zhanyong.wan470df422010-02-02 22:34:58 +000065
shiqiane35fdd92008-12-10 05:08:54 +000066// Defines some mock classes needed by the tests.
67
68class Foo {
69 public:
70 virtual ~Foo() {}
71
72 virtual void DoThis() = 0;
73 virtual int DoThat(bool flag) = 0;
74};
75
76class MockFoo : public Foo {
77 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +000078 MockFoo() {}
shiqiane35fdd92008-12-10 05:08:54 +000079 void Delete() { delete this; }
80
81 MOCK_METHOD0(DoThis, void());
82 MOCK_METHOD1(DoThat, int(bool flag));
zhanyong.wan32de5f52009-12-23 00:13:23 +000083
84 private:
85 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo);
shiqiane35fdd92008-12-10 05:08:54 +000086};
87
88class MockBar {
89 public:
90 explicit MockBar(const string& s) : str_(s) {}
91
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');
96 }
97
98 virtual ~MockBar() {}
99
100 const string& str() const { return str_; }
101
102 MOCK_METHOD0(This, int());
103 MOCK_METHOD2(That, string(int, bool));
104
105 private:
106 string str_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000107
108 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockBar);
shiqiane35fdd92008-12-10 05:08:54 +0000109};
110
zhanyong.wan2516f602010-08-31 18:28:02 +0000111#if GTEST_HAS_STREAM_REDIRECTION
shiqiane35fdd92008-12-10 05:08:54 +0000112
zhanyong.wanc8965042013-03-01 07:10:07 +0000113// Tests that a raw mock generates warnings for uninteresting calls.
114TEST(RawMockTest, WarningForUninterestingCall) {
115 const string saved_flag = GMOCK_FLAG(verbose);
116 GMOCK_FLAG(verbose) = "warning";
117
118 MockFoo raw_foo;
119
120 CaptureStdout();
121 raw_foo.DoThis();
122 raw_foo.DoThat(true);
123 EXPECT_THAT(GetCapturedStdout(),
124 HasSubstr("Uninteresting mock function call"));
125
126 GMOCK_FLAG(verbose) = saved_flag;
127}
128
129// Tests that a raw mock generates warnings for uninteresting calls
130// that delete the mock object.
131TEST(RawMockTest, WarningForUninterestingCallAfterDeath) {
132 const string saved_flag = GMOCK_FLAG(verbose);
133 GMOCK_FLAG(verbose) = "warning";
134
135 MockFoo* const raw_foo = new MockFoo;
136
137 ON_CALL(*raw_foo, DoThis())
138 .WillByDefault(Invoke(raw_foo, &MockFoo::Delete));
139
140 CaptureStdout();
141 raw_foo->DoThis();
142 EXPECT_THAT(GetCapturedStdout(),
143 HasSubstr("Uninteresting mock function call"));
144
145 GMOCK_FLAG(verbose) = saved_flag;
146}
147
148// Tests that a raw mock generates informational logs for
149// uninteresting calls.
150TEST(RawMockTest, InfoForUninterestingCall) {
151 MockFoo raw_foo;
152
153 const string saved_flag = GMOCK_FLAG(verbose);
154 GMOCK_FLAG(verbose) = "info";
155 CaptureStdout();
156 raw_foo.DoThis();
157 EXPECT_THAT(GetCapturedStdout(),
158 HasSubstr("Uninteresting mock function call"));
159
160 GMOCK_FLAG(verbose) = saved_flag;
161}
162
shiqiane35fdd92008-12-10 05:08:54 +0000163// Tests that a nice mock generates no warning for uninteresting calls.
164TEST(NiceMockTest, NoWarningForUninterestingCall) {
165 NiceMock<MockFoo> nice_foo;
166
zhanyong.wan470df422010-02-02 22:34:58 +0000167 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +0000168 nice_foo.DoThis();
169 nice_foo.DoThat(true);
zhanyong.wan844fa942013-03-01 01:54:22 +0000170 EXPECT_EQ("", GetCapturedStdout());
shiqiane35fdd92008-12-10 05:08:54 +0000171}
172
173// Tests that a nice mock generates no warning for uninteresting calls
174// that delete the mock object.
175TEST(NiceMockTest, NoWarningForUninterestingCallAfterDeath) {
176 NiceMock<MockFoo>* const nice_foo = new NiceMock<MockFoo>;
177
178 ON_CALL(*nice_foo, DoThis())
179 .WillByDefault(Invoke(nice_foo, &MockFoo::Delete));
180
zhanyong.wan470df422010-02-02 22:34:58 +0000181 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +0000182 nice_foo->DoThis();
zhanyong.wan844fa942013-03-01 01:54:22 +0000183 EXPECT_EQ("", GetCapturedStdout());
shiqiane35fdd92008-12-10 05:08:54 +0000184}
185
186// Tests that a nice mock generates informational logs for
187// uninteresting calls.
188TEST(NiceMockTest, InfoForUninterestingCall) {
189 NiceMock<MockFoo> nice_foo;
190
vladlosev76c1c612010-05-05 19:47:46 +0000191 const string saved_flag = GMOCK_FLAG(verbose);
shiqiane35fdd92008-12-10 05:08:54 +0000192 GMOCK_FLAG(verbose) = "info";
zhanyong.wan470df422010-02-02 22:34:58 +0000193 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +0000194 nice_foo.DoThis();
zhanyong.wan844fa942013-03-01 01:54:22 +0000195 EXPECT_THAT(GetCapturedStdout(),
shiqiane35fdd92008-12-10 05:08:54 +0000196 HasSubstr("Uninteresting mock function call"));
197
vladlosev76c1c612010-05-05 19:47:46 +0000198 GMOCK_FLAG(verbose) = saved_flag;
shiqiane35fdd92008-12-10 05:08:54 +0000199}
200
zhanyong.wan2516f602010-08-31 18:28:02 +0000201#endif // GTEST_HAS_STREAM_REDIRECTION
shiqiane35fdd92008-12-10 05:08:54 +0000202
203// Tests that a nice mock allows expected calls.
204TEST(NiceMockTest, AllowsExpectedCall) {
205 NiceMock<MockFoo> nice_foo;
206
207 EXPECT_CALL(nice_foo, DoThis());
208 nice_foo.DoThis();
209}
210
211// Tests that an unexpected call on a nice mock fails.
212TEST(NiceMockTest, UnexpectedCallFails) {
213 NiceMock<MockFoo> nice_foo;
214
215 EXPECT_CALL(nice_foo, DoThis()).Times(0);
216 EXPECT_NONFATAL_FAILURE(nice_foo.DoThis(), "called more times than expected");
217}
218
219// Tests that NiceMock works with a mock class that has a non-default
220// constructor.
221TEST(NiceMockTest, NonDefaultConstructor) {
222 NiceMock<MockBar> nice_bar("hi");
223 EXPECT_EQ("hi", nice_bar.str());
224
225 nice_bar.This();
226 nice_bar.That(5, true);
227}
228
229// Tests that NiceMock works with a mock class that has a 10-ary
230// non-default constructor.
231TEST(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());
235
236 nice_bar.This();
237 nice_bar.That(5, true);
238}
239
vladlosev6c54a5e2009-10-21 06:15:34 +0000240#if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000241// Tests that NiceMock<Mock> compiles where Mock is a user-defined
zhanyong.wan844fa942013-03-01 01:54:22 +0000242// class (as opposed to ::testing::Mock). We had to work around an
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000243// 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.
zhanyong.wan93244dc2009-09-17 19:11:00 +0000246//
vladlosev6c54a5e2009-10-21 06:15:34 +0000247// 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.
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000249TEST(NiceMockTest, AcceptsClassNamedMock) {
250 NiceMock< ::Mock> nice;
251 EXPECT_CALL(nice, DoThis());
252 nice.DoThis();
253}
vladlosev6c54a5e2009-10-21 06:15:34 +0000254#endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000255
zhanyong.wan844fa942013-03-01 01:54:22 +0000256#if GTEST_HAS_STREAM_REDIRECTION
257
258// Tests that a naggy mock generates warnings for uninteresting calls.
259TEST(NaggyMockTest, WarningForUninterestingCall) {
260 const string saved_flag = GMOCK_FLAG(verbose);
261 GMOCK_FLAG(verbose) = "warning";
262
263 NaggyMock<MockFoo> naggy_foo;
264
265 CaptureStdout();
266 naggy_foo.DoThis();
267 naggy_foo.DoThat(true);
268 EXPECT_THAT(GetCapturedStdout(),
269 HasSubstr("Uninteresting mock function call"));
270
271 GMOCK_FLAG(verbose) = saved_flag;
272}
273
274// Tests that a naggy mock generates a warning for an uninteresting call
275// that deletes the mock object.
276TEST(NaggyMockTest, WarningForUninterestingCallAfterDeath) {
277 const string saved_flag = GMOCK_FLAG(verbose);
278 GMOCK_FLAG(verbose) = "warning";
279
280 NaggyMock<MockFoo>* const naggy_foo = new NaggyMock<MockFoo>;
281
282 ON_CALL(*naggy_foo, DoThis())
283 .WillByDefault(Invoke(naggy_foo, &MockFoo::Delete));
284
285 CaptureStdout();
286 naggy_foo->DoThis();
287 EXPECT_THAT(GetCapturedStdout(),
288 HasSubstr("Uninteresting mock function call"));
289
290 GMOCK_FLAG(verbose) = saved_flag;
291}
292
293#endif // GTEST_HAS_STREAM_REDIRECTION
294
295// Tests that a naggy mock allows expected calls.
296TEST(NaggyMockTest, AllowsExpectedCall) {
297 NaggyMock<MockFoo> naggy_foo;
298
299 EXPECT_CALL(naggy_foo, DoThis());
300 naggy_foo.DoThis();
301}
302
303// Tests that an unexpected call on a naggy mock fails.
304TEST(NaggyMockTest, UnexpectedCallFails) {
305 NaggyMock<MockFoo> naggy_foo;
306
307 EXPECT_CALL(naggy_foo, DoThis()).Times(0);
308 EXPECT_NONFATAL_FAILURE(naggy_foo.DoThis(),
309 "called more times than expected");
310}
311
312// Tests that NaggyMock works with a mock class that has a non-default
313// constructor.
314TEST(NaggyMockTest, NonDefaultConstructor) {
315 NaggyMock<MockBar> naggy_bar("hi");
316 EXPECT_EQ("hi", naggy_bar.str());
317
318 naggy_bar.This();
319 naggy_bar.That(5, true);
320}
321
322// Tests that NaggyMock works with a mock class that has a 10-ary
323// non-default constructor.
324TEST(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());
328
329 naggy_bar.This();
330 naggy_bar.That(5, true);
331}
332
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.
339//
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.
342TEST(NaggyMockTest, AcceptsClassNamedMock) {
343 NaggyMock< ::Mock> naggy;
344 EXPECT_CALL(naggy, DoThis());
345 naggy.DoThis();
346}
347#endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
348
shiqiane35fdd92008-12-10 05:08:54 +0000349// Tests that a strict mock allows expected calls.
350TEST(StrictMockTest, AllowsExpectedCall) {
351 StrictMock<MockFoo> strict_foo;
352
353 EXPECT_CALL(strict_foo, DoThis());
354 strict_foo.DoThis();
355}
356
357// Tests that an unexpected call on a strict mock fails.
358TEST(StrictMockTest, UnexpectedCallFails) {
359 StrictMock<MockFoo> strict_foo;
360
361 EXPECT_CALL(strict_foo, DoThis()).Times(0);
362 EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
363 "called more times than expected");
364}
365
366// Tests that an uninteresting call on a strict mock fails.
367TEST(StrictMockTest, UninterestingCallFails) {
368 StrictMock<MockFoo> strict_foo;
369
370 EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
371 "Uninteresting mock function call");
372}
373
374// Tests that an uninteresting call on a strict mock fails, even if
375// the call deletes the mock object.
376TEST(StrictMockTest, UninterestingCallFailsAfterDeath) {
377 StrictMock<MockFoo>* const strict_foo = new StrictMock<MockFoo>;
378
379 ON_CALL(*strict_foo, DoThis())
380 .WillByDefault(Invoke(strict_foo, &MockFoo::Delete));
381
382 EXPECT_NONFATAL_FAILURE(strict_foo->DoThis(),
383 "Uninteresting mock function call");
384}
385
386// Tests that StrictMock works with a mock class that has a
387// non-default constructor.
388TEST(StrictMockTest, NonDefaultConstructor) {
389 StrictMock<MockBar> strict_bar("hi");
390 EXPECT_EQ("hi", strict_bar.str());
391
392 EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
393 "Uninteresting mock function call");
394}
395
396// Tests that StrictMock works with a mock class that has a 10-ary
397// non-default constructor.
398TEST(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());
402
403 EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
404 "Uninteresting mock function call");
405}
406
vladlosev6c54a5e2009-10-21 06:15:34 +0000407#if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000408// Tests that StrictMock<Mock> compiles where Mock is a user-defined
zhanyong.wan844fa942013-03-01 01:54:22 +0000409// class (as opposed to ::testing::Mock). We had to work around an
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000410// 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.
zhanyong.wan93244dc2009-09-17 19:11:00 +0000413//
vladlosev6c54a5e2009-10-21 06:15:34 +0000414// 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.
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000416TEST(StrictMockTest, AcceptsClassNamedMock) {
zhanyong.wan93244dc2009-09-17 19:11:00 +0000417 StrictMock< ::Mock> strict;
418 EXPECT_CALL(strict, DoThis());
419 strict.DoThis();
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000420}
vladlosev6c54a5e2009-10-21 06:15:34 +0000421#endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000422
shiqiane35fdd92008-12-10 05:08:54 +0000423} // namespace gmock_nice_strict_test
424} // namespace testing