blob: 9b83d88e2a8d343f8a83b4e32c5166626f333236 [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
113// Tests that a nice mock generates no warning for uninteresting calls.
114TEST(NiceMockTest, NoWarningForUninterestingCall) {
115 NiceMock<MockFoo> nice_foo;
116
zhanyong.wan470df422010-02-02 22:34:58 +0000117 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +0000118 nice_foo.DoThis();
119 nice_foo.DoThat(true);
zhanyong.wan844fa942013-03-01 01:54:22 +0000120 EXPECT_EQ("", GetCapturedStdout());
shiqiane35fdd92008-12-10 05:08:54 +0000121}
122
123// Tests that a nice mock generates no warning for uninteresting calls
124// that delete the mock object.
125TEST(NiceMockTest, NoWarningForUninterestingCallAfterDeath) {
126 NiceMock<MockFoo>* const nice_foo = new NiceMock<MockFoo>;
127
128 ON_CALL(*nice_foo, DoThis())
129 .WillByDefault(Invoke(nice_foo, &MockFoo::Delete));
130
zhanyong.wan470df422010-02-02 22:34:58 +0000131 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +0000132 nice_foo->DoThis();
zhanyong.wan844fa942013-03-01 01:54:22 +0000133 EXPECT_EQ("", GetCapturedStdout());
shiqiane35fdd92008-12-10 05:08:54 +0000134}
135
136// Tests that a nice mock generates informational logs for
137// uninteresting calls.
138TEST(NiceMockTest, InfoForUninterestingCall) {
139 NiceMock<MockFoo> nice_foo;
140
vladlosev76c1c612010-05-05 19:47:46 +0000141 const string saved_flag = GMOCK_FLAG(verbose);
shiqiane35fdd92008-12-10 05:08:54 +0000142 GMOCK_FLAG(verbose) = "info";
zhanyong.wan470df422010-02-02 22:34:58 +0000143 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +0000144 nice_foo.DoThis();
zhanyong.wan844fa942013-03-01 01:54:22 +0000145 EXPECT_THAT(GetCapturedStdout(),
shiqiane35fdd92008-12-10 05:08:54 +0000146 HasSubstr("Uninteresting mock function call"));
147
zhanyong.wan470df422010-02-02 22:34:58 +0000148 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +0000149 nice_foo.DoThat(true);
zhanyong.wan844fa942013-03-01 01:54:22 +0000150 EXPECT_THAT(GetCapturedStdout(),
shiqiane35fdd92008-12-10 05:08:54 +0000151 HasSubstr("Uninteresting mock function call"));
vladlosev76c1c612010-05-05 19:47:46 +0000152 GMOCK_FLAG(verbose) = saved_flag;
shiqiane35fdd92008-12-10 05:08:54 +0000153}
154
zhanyong.wan2516f602010-08-31 18:28:02 +0000155#endif // GTEST_HAS_STREAM_REDIRECTION
shiqiane35fdd92008-12-10 05:08:54 +0000156
157// Tests that a nice mock allows expected calls.
158TEST(NiceMockTest, AllowsExpectedCall) {
159 NiceMock<MockFoo> nice_foo;
160
161 EXPECT_CALL(nice_foo, DoThis());
162 nice_foo.DoThis();
163}
164
165// Tests that an unexpected call on a nice mock fails.
166TEST(NiceMockTest, UnexpectedCallFails) {
167 NiceMock<MockFoo> nice_foo;
168
169 EXPECT_CALL(nice_foo, DoThis()).Times(0);
170 EXPECT_NONFATAL_FAILURE(nice_foo.DoThis(), "called more times than expected");
171}
172
173// Tests that NiceMock works with a mock class that has a non-default
174// constructor.
175TEST(NiceMockTest, NonDefaultConstructor) {
176 NiceMock<MockBar> nice_bar("hi");
177 EXPECT_EQ("hi", nice_bar.str());
178
179 nice_bar.This();
180 nice_bar.That(5, true);
181}
182
183// Tests that NiceMock works with a mock class that has a 10-ary
184// non-default constructor.
185TEST(NiceMockTest, NonDefaultConstructor10) {
186 NiceMock<MockBar> nice_bar('a', 'b', "c", "d", 'e', 'f',
187 "g", "h", true, false);
188 EXPECT_EQ("abcdefghTF", nice_bar.str());
189
190 nice_bar.This();
191 nice_bar.That(5, true);
192}
193
vladlosev6c54a5e2009-10-21 06:15:34 +0000194#if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000195// Tests that NiceMock<Mock> compiles where Mock is a user-defined
zhanyong.wan844fa942013-03-01 01:54:22 +0000196// class (as opposed to ::testing::Mock). We had to work around an
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000197// MSVC 8.0 bug that caused the symbol Mock used in the definition of
198// NiceMock to be looked up in the wrong context, and this test
199// ensures that our fix works.
zhanyong.wan93244dc2009-09-17 19:11:00 +0000200//
vladlosev6c54a5e2009-10-21 06:15:34 +0000201// We have to skip this test on Symbian and Windows Mobile, as it
202// causes the program to crash there, for reasons unclear to us yet.
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000203TEST(NiceMockTest, AcceptsClassNamedMock) {
204 NiceMock< ::Mock> nice;
205 EXPECT_CALL(nice, DoThis());
206 nice.DoThis();
207}
vladlosev6c54a5e2009-10-21 06:15:34 +0000208#endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000209
zhanyong.wan844fa942013-03-01 01:54:22 +0000210#if GTEST_HAS_STREAM_REDIRECTION
211
212// Tests that a naggy mock generates warnings for uninteresting calls.
213TEST(NaggyMockTest, WarningForUninterestingCall) {
214 const string saved_flag = GMOCK_FLAG(verbose);
215 GMOCK_FLAG(verbose) = "warning";
216
217 NaggyMock<MockFoo> naggy_foo;
218
219 CaptureStdout();
220 naggy_foo.DoThis();
221 naggy_foo.DoThat(true);
222 EXPECT_THAT(GetCapturedStdout(),
223 HasSubstr("Uninteresting mock function call"));
224
225 GMOCK_FLAG(verbose) = saved_flag;
226}
227
228// Tests that a naggy mock generates a warning for an uninteresting call
229// that deletes the mock object.
230TEST(NaggyMockTest, WarningForUninterestingCallAfterDeath) {
231 const string saved_flag = GMOCK_FLAG(verbose);
232 GMOCK_FLAG(verbose) = "warning";
233
234 NaggyMock<MockFoo>* const naggy_foo = new NaggyMock<MockFoo>;
235
236 ON_CALL(*naggy_foo, DoThis())
237 .WillByDefault(Invoke(naggy_foo, &MockFoo::Delete));
238
239 CaptureStdout();
240 naggy_foo->DoThis();
241 EXPECT_THAT(GetCapturedStdout(),
242 HasSubstr("Uninteresting mock function call"));
243
244 GMOCK_FLAG(verbose) = saved_flag;
245}
246
247#endif // GTEST_HAS_STREAM_REDIRECTION
248
249// Tests that a naggy mock allows expected calls.
250TEST(NaggyMockTest, AllowsExpectedCall) {
251 NaggyMock<MockFoo> naggy_foo;
252
253 EXPECT_CALL(naggy_foo, DoThis());
254 naggy_foo.DoThis();
255}
256
257// Tests that an unexpected call on a naggy mock fails.
258TEST(NaggyMockTest, UnexpectedCallFails) {
259 NaggyMock<MockFoo> naggy_foo;
260
261 EXPECT_CALL(naggy_foo, DoThis()).Times(0);
262 EXPECT_NONFATAL_FAILURE(naggy_foo.DoThis(),
263 "called more times than expected");
264}
265
266// Tests that NaggyMock works with a mock class that has a non-default
267// constructor.
268TEST(NaggyMockTest, NonDefaultConstructor) {
269 NaggyMock<MockBar> naggy_bar("hi");
270 EXPECT_EQ("hi", naggy_bar.str());
271
272 naggy_bar.This();
273 naggy_bar.That(5, true);
274}
275
276// Tests that NaggyMock works with a mock class that has a 10-ary
277// non-default constructor.
278TEST(NaggyMockTest, NonDefaultConstructor10) {
279 NaggyMock<MockBar> naggy_bar('0', '1', "2", "3", '4', '5',
280 "6", "7", true, false);
281 EXPECT_EQ("01234567TF", naggy_bar.str());
282
283 naggy_bar.This();
284 naggy_bar.That(5, true);
285}
286
287#if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
288// Tests that NaggyMock<Mock> compiles where Mock is a user-defined
289// class (as opposed to ::testing::Mock). We had to work around an
290// MSVC 8.0 bug that caused the symbol Mock used in the definition of
291// NaggyMock to be looked up in the wrong context, and this test
292// ensures that our fix works.
293//
294// We have to skip this test on Symbian and Windows Mobile, as it
295// causes the program to crash there, for reasons unclear to us yet.
296TEST(NaggyMockTest, AcceptsClassNamedMock) {
297 NaggyMock< ::Mock> naggy;
298 EXPECT_CALL(naggy, DoThis());
299 naggy.DoThis();
300}
301#endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
302
shiqiane35fdd92008-12-10 05:08:54 +0000303// Tests that a strict mock allows expected calls.
304TEST(StrictMockTest, AllowsExpectedCall) {
305 StrictMock<MockFoo> strict_foo;
306
307 EXPECT_CALL(strict_foo, DoThis());
308 strict_foo.DoThis();
309}
310
311// Tests that an unexpected call on a strict mock fails.
312TEST(StrictMockTest, UnexpectedCallFails) {
313 StrictMock<MockFoo> strict_foo;
314
315 EXPECT_CALL(strict_foo, DoThis()).Times(0);
316 EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
317 "called more times than expected");
318}
319
320// Tests that an uninteresting call on a strict mock fails.
321TEST(StrictMockTest, UninterestingCallFails) {
322 StrictMock<MockFoo> strict_foo;
323
324 EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
325 "Uninteresting mock function call");
326}
327
328// Tests that an uninteresting call on a strict mock fails, even if
329// the call deletes the mock object.
330TEST(StrictMockTest, UninterestingCallFailsAfterDeath) {
331 StrictMock<MockFoo>* const strict_foo = new StrictMock<MockFoo>;
332
333 ON_CALL(*strict_foo, DoThis())
334 .WillByDefault(Invoke(strict_foo, &MockFoo::Delete));
335
336 EXPECT_NONFATAL_FAILURE(strict_foo->DoThis(),
337 "Uninteresting mock function call");
338}
339
340// Tests that StrictMock works with a mock class that has a
341// non-default constructor.
342TEST(StrictMockTest, NonDefaultConstructor) {
343 StrictMock<MockBar> strict_bar("hi");
344 EXPECT_EQ("hi", strict_bar.str());
345
346 EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
347 "Uninteresting mock function call");
348}
349
350// Tests that StrictMock works with a mock class that has a 10-ary
351// non-default constructor.
352TEST(StrictMockTest, NonDefaultConstructor10) {
353 StrictMock<MockBar> strict_bar('a', 'b', "c", "d", 'e', 'f',
354 "g", "h", true, false);
355 EXPECT_EQ("abcdefghTF", strict_bar.str());
356
357 EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
358 "Uninteresting mock function call");
359}
360
vladlosev6c54a5e2009-10-21 06:15:34 +0000361#if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000362// Tests that StrictMock<Mock> compiles where Mock is a user-defined
zhanyong.wan844fa942013-03-01 01:54:22 +0000363// class (as opposed to ::testing::Mock). We had to work around an
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000364// MSVC 8.0 bug that caused the symbol Mock used in the definition of
365// StrictMock to be looked up in the wrong context, and this test
366// ensures that our fix works.
zhanyong.wan93244dc2009-09-17 19:11:00 +0000367//
vladlosev6c54a5e2009-10-21 06:15:34 +0000368// We have to skip this test on Symbian and Windows Mobile, as it
369// causes the program to crash there, for reasons unclear to us yet.
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000370TEST(StrictMockTest, AcceptsClassNamedMock) {
zhanyong.wan93244dc2009-09-17 19:11:00 +0000371 StrictMock< ::Mock> strict;
372 EXPECT_CALL(strict, DoThis());
373 strict.DoThis();
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000374}
vladlosev6c54a5e2009-10-21 06:15:34 +0000375#endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000376
shiqiane35fdd92008-12-10 05:08:54 +0000377} // namespace gmock_nice_strict_test
378} // namespace testing