blob: bb56b7cd70be751ffee324909d7fdef31cc9a551 [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// Tests Google Mock's output in various scenarios. This ensures that
33// Google Mock's messages are readable and useful.
34
35#include <gmock/gmock.h>
36
37#include <stdio.h>
38#include <string>
39
40#include <gtest/gtest.h>
41
42using testing::_;
43using testing::Ge;
44using testing::InSequence;
45using testing::Ref;
46using testing::Return;
47using testing::Sequence;
48
49class MockFoo {
50 public:
51 MOCK_METHOD3(Bar, char(const std::string& s, int i, double x));
52 MOCK_METHOD2(Bar2, bool(int x, int y));
53 MOCK_METHOD2(Bar3, void(int x, int y));
54};
55
56class GMockOutputTest : public testing::Test {
57 protected:
58 MockFoo foo_;
59};
60
61TEST_F(GMockOutputTest, ExpectedCall) {
62 testing::GMOCK_FLAG(verbose) = "info";
63
64 EXPECT_CALL(foo_, Bar2(0, _));
65 foo_.Bar2(0, 0); // Expected call
66
67 testing::GMOCK_FLAG(verbose) = "warning";
68}
69
70TEST_F(GMockOutputTest, ExpectedCallToVoidFunction) {
71 testing::GMOCK_FLAG(verbose) = "info";
72
73 EXPECT_CALL(foo_, Bar3(0, _));
74 foo_.Bar3(0, 0); // Expected call
75
76 testing::GMOCK_FLAG(verbose) = "warning";
77}
78
79TEST_F(GMockOutputTest, ExplicitActionsRunOut) {
80 EXPECT_CALL(foo_, Bar2(_, _))
81 .Times(2)
82 .WillOnce(Return(false));
83 foo_.Bar2(2, 2);
84 foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out.
85}
86
87TEST_F(GMockOutputTest, UnexpectedCall) {
88 EXPECT_CALL(foo_, Bar2(0, _));
89
90 foo_.Bar2(1, 0); // Unexpected call
91 foo_.Bar2(0, 0); // Expected call
92}
93
94TEST_F(GMockOutputTest, UnexpectedCallToVoidFunction) {
95 EXPECT_CALL(foo_, Bar3(0, _));
96
97 foo_.Bar3(1, 0); // Unexpected call
98 foo_.Bar3(0, 0); // Expected call
99}
100
101TEST_F(GMockOutputTest, ExcessiveCall) {
102 EXPECT_CALL(foo_, Bar2(0, _));
103
104 foo_.Bar2(0, 0); // Expected call
105 foo_.Bar2(0, 1); // Excessive call
106}
107
108TEST_F(GMockOutputTest, ExcessiveCallToVoidFunction) {
109 EXPECT_CALL(foo_, Bar3(0, _));
110
111 foo_.Bar3(0, 0); // Expected call
112 foo_.Bar3(0, 1); // Excessive call
113}
114
115TEST_F(GMockOutputTest, UninterestingCall) {
116 foo_.Bar2(0, 1); // Uninteresting call
117}
118
119TEST_F(GMockOutputTest, UninterestingCallToVoidFunction) {
120 foo_.Bar3(0, 1); // Uninteresting call
121}
122
123TEST_F(GMockOutputTest, RetiredExpectation) {
124 EXPECT_CALL(foo_, Bar2(_, _))
125 .RetiresOnSaturation();
126 EXPECT_CALL(foo_, Bar2(0, 0));
127
128 foo_.Bar2(1, 1);
129 foo_.Bar2(1, 1); // Matches a retired expectation
130 foo_.Bar2(0, 0);
131}
132
133TEST_F(GMockOutputTest, UnsatisfiedPrerequisite) {
134 {
135 InSequence s;
136 EXPECT_CALL(foo_, Bar(_, 0, _));
137 EXPECT_CALL(foo_, Bar2(0, 0));
138 EXPECT_CALL(foo_, Bar2(1, _));
139 }
140
141 foo_.Bar2(1, 0); // Has one immediate unsatisfied pre-requisite
142 foo_.Bar("Hi", 0, 0);
143 foo_.Bar2(0, 0);
144 foo_.Bar2(1, 0);
145}
146
147TEST_F(GMockOutputTest, UnsatisfiedPrerequisites) {
148 Sequence s1, s2;
149
150 EXPECT_CALL(foo_, Bar(_, 0, _))
151 .InSequence(s1);
152 EXPECT_CALL(foo_, Bar2(0, 0))
153 .InSequence(s2);
154 EXPECT_CALL(foo_, Bar2(1, _))
155 .InSequence(s1, s2);
156
157 foo_.Bar2(1, 0); // Has two immediate unsatisfied pre-requisites
158 foo_.Bar("Hi", 0, 0);
159 foo_.Bar2(0, 0);
160 foo_.Bar2(1, 0);
161}
162
163TEST_F(GMockOutputTest, UnsatisfiedExpectation) {
164 EXPECT_CALL(foo_, Bar(_, _, _));
165 EXPECT_CALL(foo_, Bar2(0, _))
166 .Times(2);
167
168 foo_.Bar2(0, 1);
169}
170
171TEST_F(GMockOutputTest, MismatchArguments) {
172 const std::string s = "Hi";
173 EXPECT_CALL(foo_, Bar(Ref(s), _, Ge(0)));
174
175 foo_.Bar("Ho", 0, -0.1); // Mismatch arguments
176 foo_.Bar(s, 0, 0);
177}
178
179TEST_F(GMockOutputTest, MismatchWithArguments) {
180 EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1)))
181 .WithArguments(Ge());
182
183 foo_.Bar2(2, 3); // Mismatch WithArguments()
184 foo_.Bar2(2, 1);
185}
186
187TEST_F(GMockOutputTest, MismatchArgumentsAndWithArguments) {
188 EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1)))
189 .WithArguments(Ge());
190
191 foo_.Bar2(1, 3); // Mismatch arguments and mismatch WithArguments()
192 foo_.Bar2(2, 1);
193}
194
195TEST_F(GMockOutputTest, UnexpectedCallWithDefaultAction) {
196 ON_CALL(foo_, Bar2(_, _))
197 .WillByDefault(Return(true)); // Default action #1
198 ON_CALL(foo_, Bar2(1, _))
199 .WillByDefault(Return(false)); // Default action #2
200
201 EXPECT_CALL(foo_, Bar2(2, 2));
202 foo_.Bar2(1, 0); // Unexpected call, takes default action #2.
203 foo_.Bar2(0, 0); // Unexpected call, takes default action #1.
204 foo_.Bar2(2, 2); // Expected call.
205}
206
207TEST_F(GMockOutputTest, ExcessiveCallWithDefaultAction) {
208 ON_CALL(foo_, Bar2(_, _))
209 .WillByDefault(Return(true)); // Default action #1
210 ON_CALL(foo_, Bar2(1, _))
211 .WillByDefault(Return(false)); // Default action #2
212
213 EXPECT_CALL(foo_, Bar2(2, 2));
214 EXPECT_CALL(foo_, Bar2(1, 1));
215
216 foo_.Bar2(2, 2); // Expected call.
217 foo_.Bar2(2, 2); // Excessive call, takes default action #1.
218 foo_.Bar2(1, 1); // Expected call.
219 foo_.Bar2(1, 1); // Excessive call, takes default action #2.
220}
221
222TEST_F(GMockOutputTest, UninterestingCallWithDefaultAction) {
223 ON_CALL(foo_, Bar2(_, _))
224 .WillByDefault(Return(true)); // Default action #1
225 ON_CALL(foo_, Bar2(1, _))
226 .WillByDefault(Return(false)); // Default action #2
227
228 foo_.Bar2(2, 2); // Uninteresting call, takes default action #1.
229 foo_.Bar2(1, 1); // Uninteresting call, takes default action #2.
230}
231
232TEST_F(GMockOutputTest, ExplicitActionsRunOutWithDefaultAction) {
233 ON_CALL(foo_, Bar2(_, _))
234 .WillByDefault(Return(true)); // Default action #1
235
236 EXPECT_CALL(foo_, Bar2(_, _))
237 .Times(2)
238 .WillOnce(Return(false));
239 foo_.Bar2(2, 2);
240 foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out.
241}