blob: 1d36e03ebb85daf29738b73c24f4e2249d6fb7a6 [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
60// Defines some mock classes needed by the tests.
61
62class Foo {
63 public:
64 virtual ~Foo() {}
65
66 virtual void DoThis() = 0;
67 virtual int DoThat(bool flag) = 0;
68};
69
70class MockFoo : public Foo {
71 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +000072 MockFoo() {}
shiqiane35fdd92008-12-10 05:08:54 +000073 void Delete() { delete this; }
74
75 MOCK_METHOD0(DoThis, void());
76 MOCK_METHOD1(DoThat, int(bool flag));
zhanyong.wan32de5f52009-12-23 00:13:23 +000077
78 private:
79 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo);
shiqiane35fdd92008-12-10 05:08:54 +000080};
81
82class MockBar {
83 public:
84 explicit MockBar(const string& s) : str_(s) {}
85
86 MockBar(char a1, char a2, string a3, string a4, int a5, int a6,
87 const string& a7, const string& a8, bool a9, bool a10) {
88 str_ = string() + a1 + a2 + a3 + a4 + static_cast<char>(a5) +
89 static_cast<char>(a6) + a7 + a8 + (a9 ? 'T' : 'F') + (a10 ? 'T' : 'F');
90 }
91
92 virtual ~MockBar() {}
93
94 const string& str() const { return str_; }
95
96 MOCK_METHOD0(This, int());
97 MOCK_METHOD2(That, string(int, bool));
98
99 private:
100 string str_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000101
102 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockBar);
shiqiane35fdd92008-12-10 05:08:54 +0000103};
104
105// TODO(wan@google.com): find a way to re-enable these tests.
106#if 0
107
108// Tests that a nice mock generates no warning for uninteresting calls.
109TEST(NiceMockTest, NoWarningForUninterestingCall) {
110 NiceMock<MockFoo> nice_foo;
111
112 CaptureTestStdout();
113 nice_foo.DoThis();
114 nice_foo.DoThat(true);
115 EXPECT_EQ("", GetCapturedTestStdout());
116}
117
118// Tests that a nice mock generates no warning for uninteresting calls
119// that delete the mock object.
120TEST(NiceMockTest, NoWarningForUninterestingCallAfterDeath) {
121 NiceMock<MockFoo>* const nice_foo = new NiceMock<MockFoo>;
122
123 ON_CALL(*nice_foo, DoThis())
124 .WillByDefault(Invoke(nice_foo, &MockFoo::Delete));
125
126 CaptureTestStdout();
127 nice_foo->DoThis();
128 EXPECT_EQ("", GetCapturedTestStdout());
129}
130
131// Tests that a nice mock generates informational logs for
132// uninteresting calls.
133TEST(NiceMockTest, InfoForUninterestingCall) {
134 NiceMock<MockFoo> nice_foo;
135
136 GMOCK_FLAG(verbose) = "info";
137 CaptureTestStdout();
138 nice_foo.DoThis();
139 EXPECT_THAT(GetCapturedTestStdout(),
140 HasSubstr("Uninteresting mock function call"));
141
142 CaptureTestStdout();
143 nice_foo.DoThat(true);
144 EXPECT_THAT(GetCapturedTestStdout(),
145 HasSubstr("Uninteresting mock function call"));
146}
147
148#endif // 0
149
150// Tests that a nice mock allows expected calls.
151TEST(NiceMockTest, AllowsExpectedCall) {
152 NiceMock<MockFoo> nice_foo;
153
154 EXPECT_CALL(nice_foo, DoThis());
155 nice_foo.DoThis();
156}
157
158// Tests that an unexpected call on a nice mock fails.
159TEST(NiceMockTest, UnexpectedCallFails) {
160 NiceMock<MockFoo> nice_foo;
161
162 EXPECT_CALL(nice_foo, DoThis()).Times(0);
163 EXPECT_NONFATAL_FAILURE(nice_foo.DoThis(), "called more times than expected");
164}
165
166// Tests that NiceMock works with a mock class that has a non-default
167// constructor.
168TEST(NiceMockTest, NonDefaultConstructor) {
169 NiceMock<MockBar> nice_bar("hi");
170 EXPECT_EQ("hi", nice_bar.str());
171
172 nice_bar.This();
173 nice_bar.That(5, true);
174}
175
176// Tests that NiceMock works with a mock class that has a 10-ary
177// non-default constructor.
178TEST(NiceMockTest, NonDefaultConstructor10) {
179 NiceMock<MockBar> nice_bar('a', 'b', "c", "d", 'e', 'f',
180 "g", "h", true, false);
181 EXPECT_EQ("abcdefghTF", nice_bar.str());
182
183 nice_bar.This();
184 nice_bar.That(5, true);
185}
186
vladlosev6c54a5e2009-10-21 06:15:34 +0000187#if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000188// Tests that NiceMock<Mock> compiles where Mock is a user-defined
189// class (as opposed to ::testing::Mock). We had to workaround an
190// MSVC 8.0 bug that caused the symbol Mock used in the definition of
191// NiceMock to be looked up in the wrong context, and this test
192// ensures that our fix works.
zhanyong.wan93244dc2009-09-17 19:11:00 +0000193//
vladlosev6c54a5e2009-10-21 06:15:34 +0000194// We have to skip this test on Symbian and Windows Mobile, as it
195// causes the program to crash there, for reasons unclear to us yet.
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000196TEST(NiceMockTest, AcceptsClassNamedMock) {
197 NiceMock< ::Mock> nice;
198 EXPECT_CALL(nice, DoThis());
199 nice.DoThis();
200}
vladlosev6c54a5e2009-10-21 06:15:34 +0000201#endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000202
shiqiane35fdd92008-12-10 05:08:54 +0000203// Tests that a strict mock allows expected calls.
204TEST(StrictMockTest, AllowsExpectedCall) {
205 StrictMock<MockFoo> strict_foo;
206
207 EXPECT_CALL(strict_foo, DoThis());
208 strict_foo.DoThis();
209}
210
211// Tests that an unexpected call on a strict mock fails.
212TEST(StrictMockTest, UnexpectedCallFails) {
213 StrictMock<MockFoo> strict_foo;
214
215 EXPECT_CALL(strict_foo, DoThis()).Times(0);
216 EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
217 "called more times than expected");
218}
219
220// Tests that an uninteresting call on a strict mock fails.
221TEST(StrictMockTest, UninterestingCallFails) {
222 StrictMock<MockFoo> strict_foo;
223
224 EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
225 "Uninteresting mock function call");
226}
227
228// Tests that an uninteresting call on a strict mock fails, even if
229// the call deletes the mock object.
230TEST(StrictMockTest, UninterestingCallFailsAfterDeath) {
231 StrictMock<MockFoo>* const strict_foo = new StrictMock<MockFoo>;
232
233 ON_CALL(*strict_foo, DoThis())
234 .WillByDefault(Invoke(strict_foo, &MockFoo::Delete));
235
236 EXPECT_NONFATAL_FAILURE(strict_foo->DoThis(),
237 "Uninteresting mock function call");
238}
239
240// Tests that StrictMock works with a mock class that has a
241// non-default constructor.
242TEST(StrictMockTest, NonDefaultConstructor) {
243 StrictMock<MockBar> strict_bar("hi");
244 EXPECT_EQ("hi", strict_bar.str());
245
246 EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
247 "Uninteresting mock function call");
248}
249
250// Tests that StrictMock works with a mock class that has a 10-ary
251// non-default constructor.
252TEST(StrictMockTest, NonDefaultConstructor10) {
253 StrictMock<MockBar> strict_bar('a', 'b', "c", "d", 'e', 'f',
254 "g", "h", true, false);
255 EXPECT_EQ("abcdefghTF", strict_bar.str());
256
257 EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
258 "Uninteresting mock function call");
259}
260
vladlosev6c54a5e2009-10-21 06:15:34 +0000261#if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000262// Tests that StrictMock<Mock> compiles where Mock is a user-defined
263// class (as opposed to ::testing::Mock). We had to workaround an
264// MSVC 8.0 bug that caused the symbol Mock used in the definition of
265// StrictMock to be looked up in the wrong context, and this test
266// ensures that our fix works.
zhanyong.wan93244dc2009-09-17 19:11:00 +0000267//
vladlosev6c54a5e2009-10-21 06:15:34 +0000268// We have to skip this test on Symbian and Windows Mobile, as it
269// causes the program to crash there, for reasons unclear to us yet.
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000270TEST(StrictMockTest, AcceptsClassNamedMock) {
zhanyong.wan93244dc2009-09-17 19:11:00 +0000271 StrictMock< ::Mock> strict;
272 EXPECT_CALL(strict, DoThis());
273 strict.DoThis();
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000274}
vladlosev6c54a5e2009-10-21 06:15:34 +0000275#endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan4bd79e42009-09-16 17:38:08 +0000276
shiqiane35fdd92008-12-10 05:08:54 +0000277} // namespace gmock_nice_strict_test
278} // namespace testing