blob: f340cecbaa6572b5f8fc0288b33f80f208f9d509 [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
32#include <gmock/gmock-generated-nice-strict.h>
33
34#include <string>
35#include <gmock/gmock.h>
36#include <gtest/gtest.h>
37#include <gtest/gtest-spi.h>
38
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;
57using 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:
89 explicit MockBar(const string& s) : str_(s) {}
90
91 MockBar(char a1, char a2, string a3, string a4, int a5, int a6,
92 const string& a7, const string& a8, bool a9, bool a10) {
93 str_ = string() + a1 + a2 + a3 + a4 + static_cast<char>(a5) +
94 static_cast<char>(a6) + a7 + a8 + (a9 ? 'T' : 'F') + (a10 ? 'T' : 'F');
95 }
96
97 virtual ~MockBar() {}
98
99 const string& str() const { return str_; }
100
101 MOCK_METHOD0(This, int());
102 MOCK_METHOD2(That, string(int, bool));
103
104 private:
105 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
112// Tests that a nice mock generates no warning for uninteresting calls.
113TEST(NiceMockTest, NoWarningForUninterestingCall) {
114 NiceMock<MockFoo> nice_foo;
115
zhanyong.wan470df422010-02-02 22:34:58 +0000116 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +0000117 nice_foo.DoThis();
118 nice_foo.DoThat(true);
zhanyong.wan470df422010-02-02 22:34:58 +0000119 EXPECT_STREQ("", GetCapturedStdout().c_str());
shiqiane35fdd92008-12-10 05:08:54 +0000120}
121
122// Tests that a nice mock generates no warning for uninteresting calls
123// that delete the mock object.
124TEST(NiceMockTest, NoWarningForUninterestingCallAfterDeath) {
125 NiceMock<MockFoo>* const nice_foo = new NiceMock<MockFoo>;
126
127 ON_CALL(*nice_foo, DoThis())
128 .WillByDefault(Invoke(nice_foo, &MockFoo::Delete));
129
zhanyong.wan470df422010-02-02 22:34:58 +0000130 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +0000131 nice_foo->DoThis();
zhanyong.wan470df422010-02-02 22:34:58 +0000132 EXPECT_STREQ("", GetCapturedStdout().c_str());
shiqiane35fdd92008-12-10 05:08:54 +0000133}
134
135// Tests that a nice mock generates informational logs for
136// uninteresting calls.
137TEST(NiceMockTest, InfoForUninterestingCall) {
138 NiceMock<MockFoo> nice_foo;
139
vladlosev76c1c612010-05-05 19:47:46 +0000140 const string saved_flag = GMOCK_FLAG(verbose);
shiqiane35fdd92008-12-10 05:08:54 +0000141 GMOCK_FLAG(verbose) = "info";
zhanyong.wan470df422010-02-02 22:34:58 +0000142 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +0000143 nice_foo.DoThis();
zhanyong.wan470df422010-02-02 22:34:58 +0000144 EXPECT_THAT(GetCapturedStdout(),
shiqiane35fdd92008-12-10 05:08:54 +0000145 HasSubstr("Uninteresting mock function call"));
146
zhanyong.wan470df422010-02-02 22:34:58 +0000147 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +0000148 nice_foo.DoThat(true);
zhanyong.wan470df422010-02-02 22:34:58 +0000149 EXPECT_THAT(GetCapturedStdout(),
shiqiane35fdd92008-12-10 05:08:54 +0000150 HasSubstr("Uninteresting mock function call"));
vladlosev76c1c612010-05-05 19:47:46 +0000151 GMOCK_FLAG(verbose) = saved_flag;
shiqiane35fdd92008-12-10 05:08:54 +0000152}
153
zhanyong.wan2516f602010-08-31 18:28:02 +0000154#endif // GTEST_HAS_STREAM_REDIRECTION
shiqiane35fdd92008-12-10 05:08:54 +0000155
156// Tests that a nice mock allows expected calls.
157TEST(NiceMockTest, AllowsExpectedCall) {
158 NiceMock<MockFoo> nice_foo;
159
160 EXPECT_CALL(nice_foo, DoThis());
161 nice_foo.DoThis();
162}
163
164// Tests that an unexpected call on a nice mock fails.
165TEST(NiceMockTest, UnexpectedCallFails) {
166 NiceMock<MockFoo> nice_foo;
167
168 EXPECT_CALL(nice_foo, DoThis()).Times(0);
169 EXPECT_NONFATAL_FAILURE(nice_foo.DoThis(), "called more times than expected");
170}
171
172// Tests that NiceMock works with a mock class that has a non-default
173// constructor.
174TEST(NiceMockTest, NonDefaultConstructor) {
175 NiceMock<MockBar> nice_bar("hi");
176 EXPECT_EQ("hi", nice_bar.str());
177
178 nice_bar.This();
179 nice_bar.That(5, true);
180}
181
182// Tests that NiceMock works with a mock class that has a 10-ary
183// non-default constructor.
184TEST(NiceMockTest, NonDefaultConstructor10) {
185 NiceMock<MockBar> nice_bar('a', 'b', "c", "d", 'e', 'f',
186 "g", "h", true, false);
187 EXPECT_EQ("abcdefghTF", nice_bar.str());
188
189 nice_bar.This();
190 nice_bar.That(5, true);
191}
192
vladlosev6c54a5e2009-10-21 06:15:34 +0000193#if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000194// Tests that NiceMock<Mock> compiles where Mock is a user-defined
195// class (as opposed to ::testing::Mock). We had to workaround an
196// MSVC 8.0 bug that caused the symbol Mock used in the definition of
197// NiceMock to be looked up in the wrong context, and this test
198// ensures that our fix works.
zhanyong.wan93244dc2009-09-17 19:11:00 +0000199//
vladlosev6c54a5e2009-10-21 06:15:34 +0000200// We have to skip this test on Symbian and Windows Mobile, as it
201// causes the program to crash there, for reasons unclear to us yet.
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000202TEST(NiceMockTest, AcceptsClassNamedMock) {
203 NiceMock< ::Mock> nice;
204 EXPECT_CALL(nice, DoThis());
205 nice.DoThis();
206}
vladlosev6c54a5e2009-10-21 06:15:34 +0000207#endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000208
shiqiane35fdd92008-12-10 05:08:54 +0000209// Tests that a strict mock allows expected calls.
210TEST(StrictMockTest, AllowsExpectedCall) {
211 StrictMock<MockFoo> strict_foo;
212
213 EXPECT_CALL(strict_foo, DoThis());
214 strict_foo.DoThis();
215}
216
217// Tests that an unexpected call on a strict mock fails.
218TEST(StrictMockTest, UnexpectedCallFails) {
219 StrictMock<MockFoo> strict_foo;
220
221 EXPECT_CALL(strict_foo, DoThis()).Times(0);
222 EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
223 "called more times than expected");
224}
225
226// Tests that an uninteresting call on a strict mock fails.
227TEST(StrictMockTest, UninterestingCallFails) {
228 StrictMock<MockFoo> strict_foo;
229
230 EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
231 "Uninteresting mock function call");
232}
233
234// Tests that an uninteresting call on a strict mock fails, even if
235// the call deletes the mock object.
236TEST(StrictMockTest, UninterestingCallFailsAfterDeath) {
237 StrictMock<MockFoo>* const strict_foo = new StrictMock<MockFoo>;
238
239 ON_CALL(*strict_foo, DoThis())
240 .WillByDefault(Invoke(strict_foo, &MockFoo::Delete));
241
242 EXPECT_NONFATAL_FAILURE(strict_foo->DoThis(),
243 "Uninteresting mock function call");
244}
245
246// Tests that StrictMock works with a mock class that has a
247// non-default constructor.
248TEST(StrictMockTest, NonDefaultConstructor) {
249 StrictMock<MockBar> strict_bar("hi");
250 EXPECT_EQ("hi", strict_bar.str());
251
252 EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
253 "Uninteresting mock function call");
254}
255
256// Tests that StrictMock works with a mock class that has a 10-ary
257// non-default constructor.
258TEST(StrictMockTest, NonDefaultConstructor10) {
259 StrictMock<MockBar> strict_bar('a', 'b', "c", "d", 'e', 'f',
260 "g", "h", true, false);
261 EXPECT_EQ("abcdefghTF", strict_bar.str());
262
263 EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
264 "Uninteresting mock function call");
265}
266
vladlosev6c54a5e2009-10-21 06:15:34 +0000267#if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000268// Tests that StrictMock<Mock> compiles where Mock is a user-defined
269// class (as opposed to ::testing::Mock). We had to workaround an
270// MSVC 8.0 bug that caused the symbol Mock used in the definition of
271// StrictMock to be looked up in the wrong context, and this test
272// ensures that our fix works.
zhanyong.wan93244dc2009-09-17 19:11:00 +0000273//
vladlosev6c54a5e2009-10-21 06:15:34 +0000274// We have to skip this test on Symbian and Windows Mobile, as it
275// causes the program to crash there, for reasons unclear to us yet.
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000276TEST(StrictMockTest, AcceptsClassNamedMock) {
zhanyong.wan93244dc2009-09-17 19:11:00 +0000277 StrictMock< ::Mock> strict;
278 EXPECT_CALL(strict, DoThis());
279 strict.DoThis();
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000280}
vladlosev6c54a5e2009-10-21 06:15:34 +0000281#endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000282
shiqiane35fdd92008-12-10 05:08:54 +0000283} // namespace gmock_nice_strict_test
284} // namespace testing