blob: f6f278e85747d99a10f10397d77f2e3e60ee92bc [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.wan470df422010-02-02 22:34:58 +000060#if GTEST_HAS_STREAM_REDIRECTION_
61using testing::internal::CaptureStdout;
62using testing::internal::GetCapturedStdout;
63#endif // GTEST_HAS_STREAM_REDIRECTION_
64
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.wan470df422010-02-02 22:34:58 +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
140 GMOCK_FLAG(verbose) = "info";
zhanyong.wan470df422010-02-02 22:34:58 +0000141 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +0000142 nice_foo.DoThis();
zhanyong.wan470df422010-02-02 22:34:58 +0000143 EXPECT_THAT(GetCapturedStdout(),
shiqiane35fdd92008-12-10 05:08:54 +0000144 HasSubstr("Uninteresting mock function call"));
145
zhanyong.wan470df422010-02-02 22:34:58 +0000146 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +0000147 nice_foo.DoThat(true);
zhanyong.wan470df422010-02-02 22:34:58 +0000148 EXPECT_THAT(GetCapturedStdout(),
shiqiane35fdd92008-12-10 05:08:54 +0000149 HasSubstr("Uninteresting mock function call"));
150}
151
zhanyong.wan470df422010-02-02 22:34:58 +0000152#endif // GTEST_HAS_STREAM_REDIRECTION_
shiqiane35fdd92008-12-10 05:08:54 +0000153
154// Tests that a nice mock allows expected calls.
155TEST(NiceMockTest, AllowsExpectedCall) {
156 NiceMock<MockFoo> nice_foo;
157
158 EXPECT_CALL(nice_foo, DoThis());
159 nice_foo.DoThis();
160}
161
162// Tests that an unexpected call on a nice mock fails.
163TEST(NiceMockTest, UnexpectedCallFails) {
164 NiceMock<MockFoo> nice_foo;
165
166 EXPECT_CALL(nice_foo, DoThis()).Times(0);
167 EXPECT_NONFATAL_FAILURE(nice_foo.DoThis(), "called more times than expected");
168}
169
170// Tests that NiceMock works with a mock class that has a non-default
171// constructor.
172TEST(NiceMockTest, NonDefaultConstructor) {
173 NiceMock<MockBar> nice_bar("hi");
174 EXPECT_EQ("hi", nice_bar.str());
175
176 nice_bar.This();
177 nice_bar.That(5, true);
178}
179
180// Tests that NiceMock works with a mock class that has a 10-ary
181// non-default constructor.
182TEST(NiceMockTest, NonDefaultConstructor10) {
183 NiceMock<MockBar> nice_bar('a', 'b', "c", "d", 'e', 'f',
184 "g", "h", true, false);
185 EXPECT_EQ("abcdefghTF", nice_bar.str());
186
187 nice_bar.This();
188 nice_bar.That(5, true);
189}
190
vladlosev6c54a5e2009-10-21 06:15:34 +0000191#if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000192// Tests that NiceMock<Mock> compiles where Mock is a user-defined
193// class (as opposed to ::testing::Mock). We had to workaround an
194// MSVC 8.0 bug that caused the symbol Mock used in the definition of
195// NiceMock to be looked up in the wrong context, and this test
196// ensures that our fix works.
zhanyong.wan93244dc2009-09-17 19:11:00 +0000197//
vladlosev6c54a5e2009-10-21 06:15:34 +0000198// We have to skip this test on Symbian and Windows Mobile, as it
199// causes the program to crash there, for reasons unclear to us yet.
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000200TEST(NiceMockTest, AcceptsClassNamedMock) {
201 NiceMock< ::Mock> nice;
202 EXPECT_CALL(nice, DoThis());
203 nice.DoThis();
204}
vladlosev6c54a5e2009-10-21 06:15:34 +0000205#endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000206
shiqiane35fdd92008-12-10 05:08:54 +0000207// Tests that a strict mock allows expected calls.
208TEST(StrictMockTest, AllowsExpectedCall) {
209 StrictMock<MockFoo> strict_foo;
210
211 EXPECT_CALL(strict_foo, DoThis());
212 strict_foo.DoThis();
213}
214
215// Tests that an unexpected call on a strict mock fails.
216TEST(StrictMockTest, UnexpectedCallFails) {
217 StrictMock<MockFoo> strict_foo;
218
219 EXPECT_CALL(strict_foo, DoThis()).Times(0);
220 EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
221 "called more times than expected");
222}
223
224// Tests that an uninteresting call on a strict mock fails.
225TEST(StrictMockTest, UninterestingCallFails) {
226 StrictMock<MockFoo> strict_foo;
227
228 EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
229 "Uninteresting mock function call");
230}
231
232// Tests that an uninteresting call on a strict mock fails, even if
233// the call deletes the mock object.
234TEST(StrictMockTest, UninterestingCallFailsAfterDeath) {
235 StrictMock<MockFoo>* const strict_foo = new StrictMock<MockFoo>;
236
237 ON_CALL(*strict_foo, DoThis())
238 .WillByDefault(Invoke(strict_foo, &MockFoo::Delete));
239
240 EXPECT_NONFATAL_FAILURE(strict_foo->DoThis(),
241 "Uninteresting mock function call");
242}
243
244// Tests that StrictMock works with a mock class that has a
245// non-default constructor.
246TEST(StrictMockTest, NonDefaultConstructor) {
247 StrictMock<MockBar> strict_bar("hi");
248 EXPECT_EQ("hi", strict_bar.str());
249
250 EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
251 "Uninteresting mock function call");
252}
253
254// Tests that StrictMock works with a mock class that has a 10-ary
255// non-default constructor.
256TEST(StrictMockTest, NonDefaultConstructor10) {
257 StrictMock<MockBar> strict_bar('a', 'b', "c", "d", 'e', 'f',
258 "g", "h", true, false);
259 EXPECT_EQ("abcdefghTF", strict_bar.str());
260
261 EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
262 "Uninteresting mock function call");
263}
264
vladlosev6c54a5e2009-10-21 06:15:34 +0000265#if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000266// Tests that StrictMock<Mock> compiles where Mock is a user-defined
267// class (as opposed to ::testing::Mock). We had to workaround an
268// MSVC 8.0 bug that caused the symbol Mock used in the definition of
269// StrictMock to be looked up in the wrong context, and this test
270// ensures that our fix works.
zhanyong.wan93244dc2009-09-17 19:11:00 +0000271//
vladlosev6c54a5e2009-10-21 06:15:34 +0000272// We have to skip this test on Symbian and Windows Mobile, as it
273// causes the program to crash there, for reasons unclear to us yet.
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000274TEST(StrictMockTest, AcceptsClassNamedMock) {
zhanyong.wan93244dc2009-09-17 19:11:00 +0000275 StrictMock< ::Mock> strict;
276 EXPECT_CALL(strict, DoThis());
277 strict.DoThis();
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000278}
vladlosev6c54a5e2009-10-21 06:15:34 +0000279#endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000280
shiqiane35fdd92008-12-10 05:08:54 +0000281} // namespace gmock_nice_strict_test
282} // namespace testing