xref: /freebsd/contrib/googletest/googlemock/test/gmock_output_test_.cc (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
1 // 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 // Tests Google Mock's output in various scenarios.  This ensures that
31 // Google Mock's messages are readable and useful.
32 
33 #include <stdio.h>
34 
35 #include <string>
36 
37 #include "gmock/gmock.h"
38 #include "gtest/gtest.h"
39 
40 // Silence C4100 (unreferenced formal parameter)
41 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4100)
42 
43 using testing::_;
44 using testing::AnyNumber;
45 using testing::Ge;
46 using testing::InSequence;
47 using testing::NaggyMock;
48 using testing::Ref;
49 using testing::Return;
50 using testing::Sequence;
51 using testing::Value;
52 
53 class MockFoo {
54  public:
55   MockFoo() = default;
56 
57   MOCK_METHOD3(Bar, char(const std::string& s, int i, double x));
58   MOCK_METHOD2(Bar2, bool(int x, int y));
59   MOCK_METHOD2(Bar3, void(int x, int y));
60 
61  private:
62   MockFoo(const MockFoo&) = delete;
63   MockFoo& operator=(const MockFoo&) = delete;
64 };
65 
66 class GMockOutputTest : public testing::Test {
67  protected:
68   NaggyMock<MockFoo> foo_;
69 };
70 
71 TEST_F(GMockOutputTest, ExpectedCall) {
72   GMOCK_FLAG_SET(verbose, "info");
73 
74   EXPECT_CALL(foo_, Bar2(0, _));
75   foo_.Bar2(0, 0);  // Expected call
76 
77   GMOCK_FLAG_SET(verbose, "warning");
78 }
79 
80 TEST_F(GMockOutputTest, ExpectedCallToVoidFunction) {
81   GMOCK_FLAG_SET(verbose, "info");
82 
83   EXPECT_CALL(foo_, Bar3(0, _));
84   foo_.Bar3(0, 0);  // Expected call
85 
86   GMOCK_FLAG_SET(verbose, "warning");
87 }
88 
89 TEST_F(GMockOutputTest, ExplicitActionsRunOut) {
90   EXPECT_CALL(foo_, Bar2(_, _)).Times(2).WillOnce(Return(false));
91   foo_.Bar2(2, 2);
92   foo_.Bar2(1, 1);  // Explicit actions in EXPECT_CALL run out.
93 }
94 
95 TEST_F(GMockOutputTest, UnexpectedCall) {
96   EXPECT_CALL(foo_, Bar2(0, _));
97 
98   foo_.Bar2(1, 0);  // Unexpected call
99   foo_.Bar2(0, 0);  // Expected call
100 }
101 
102 TEST_F(GMockOutputTest, UnexpectedCallToVoidFunction) {
103   EXPECT_CALL(foo_, Bar3(0, _));
104 
105   foo_.Bar3(1, 0);  // Unexpected call
106   foo_.Bar3(0, 0);  // Expected call
107 }
108 
109 TEST_F(GMockOutputTest, ExcessiveCall) {
110   EXPECT_CALL(foo_, Bar2(0, _));
111 
112   foo_.Bar2(0, 0);  // Expected call
113   foo_.Bar2(0, 1);  // Excessive call
114 }
115 
116 TEST_F(GMockOutputTest, ExcessiveCallToVoidFunction) {
117   EXPECT_CALL(foo_, Bar3(0, _));
118 
119   foo_.Bar3(0, 0);  // Expected call
120   foo_.Bar3(0, 1);  // Excessive call
121 }
122 
123 TEST_F(GMockOutputTest, UninterestingCall) {
124   foo_.Bar2(0, 1);  // Uninteresting call
125 }
126 
127 TEST_F(GMockOutputTest, UninterestingCallToVoidFunction) {
128   foo_.Bar3(0, 1);  // Uninteresting call
129 }
130 
131 TEST_F(GMockOutputTest, RetiredExpectation) {
132   EXPECT_CALL(foo_, Bar2(_, _)).RetiresOnSaturation();
133   EXPECT_CALL(foo_, Bar2(0, 0));
134 
135   foo_.Bar2(1, 1);
136   foo_.Bar2(1, 1);  // Matches a retired expectation
137   foo_.Bar2(0, 0);
138 }
139 
140 TEST_F(GMockOutputTest, UnsatisfiedPrerequisite) {
141   {
142     InSequence s;
143     EXPECT_CALL(foo_, Bar(_, 0, _));
144     EXPECT_CALL(foo_, Bar2(0, 0));
145     EXPECT_CALL(foo_, Bar2(1, _));
146   }
147 
148   foo_.Bar2(1, 0);  // Has one immediate unsatisfied pre-requisite
149   foo_.Bar("Hi", 0, 0);
150   foo_.Bar2(0, 0);
151   foo_.Bar2(1, 0);
152 }
153 
154 TEST_F(GMockOutputTest, UnsatisfiedPrerequisites) {
155   Sequence s1, s2;
156 
157   EXPECT_CALL(foo_, Bar(_, 0, _)).InSequence(s1);
158   EXPECT_CALL(foo_, Bar2(0, 0)).InSequence(s2);
159   EXPECT_CALL(foo_, Bar2(1, _)).InSequence(s1, s2);
160 
161   foo_.Bar2(1, 0);  // Has two immediate unsatisfied pre-requisites
162   foo_.Bar("Hi", 0, 0);
163   foo_.Bar2(0, 0);
164   foo_.Bar2(1, 0);
165 }
166 
167 TEST_F(GMockOutputTest, UnsatisfiedWith) {
168   EXPECT_CALL(foo_, Bar2(_, _)).With(Ge());
169 }
170 
171 TEST_F(GMockOutputTest, UnsatisfiedExpectation) {
172   EXPECT_CALL(foo_, Bar(_, _, _));
173   EXPECT_CALL(foo_, Bar2(0, _)).Times(2);
174 
175   foo_.Bar2(0, 1);
176 }
177 
178 TEST_F(GMockOutputTest, MismatchArguments) {
179   const std::string s = "Hi";
180   EXPECT_CALL(foo_, Bar(Ref(s), _, Ge(0)));
181 
182   foo_.Bar("Ho", 0, -0.1);  // Mismatch arguments
183   foo_.Bar(s, 0, 0);
184 }
185 
186 TEST_F(GMockOutputTest, MismatchWith) {
187   EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1))).With(Ge());
188 
189   foo_.Bar2(2, 3);  // Mismatch With()
190   foo_.Bar2(2, 1);
191 }
192 
193 TEST_F(GMockOutputTest, MismatchArgumentsAndWith) {
194   EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1))).With(Ge());
195 
196   foo_.Bar2(1, 3);  // Mismatch arguments and mismatch With()
197   foo_.Bar2(2, 1);
198 }
199 
200 TEST_F(GMockOutputTest, UnexpectedCallWithDefaultAction) {
201   ON_CALL(foo_, Bar2(_, _)).WillByDefault(Return(true));   // Default action #1
202   ON_CALL(foo_, Bar2(1, _)).WillByDefault(Return(false));  // Default action #2
203 
204   EXPECT_CALL(foo_, Bar2(2, 2));
205   foo_.Bar2(1, 0);  // Unexpected call, takes default action #2.
206   foo_.Bar2(0, 0);  // Unexpected call, takes default action #1.
207   foo_.Bar2(2, 2);  // Expected call.
208 }
209 
210 TEST_F(GMockOutputTest, ExcessiveCallWithDefaultAction) {
211   ON_CALL(foo_, Bar2(_, _)).WillByDefault(Return(true));   // Default action #1
212   ON_CALL(foo_, Bar2(1, _)).WillByDefault(Return(false));  // Default action #2
213 
214   EXPECT_CALL(foo_, Bar2(2, 2));
215   EXPECT_CALL(foo_, Bar2(1, 1));
216 
217   foo_.Bar2(2, 2);  // Expected call.
218   foo_.Bar2(2, 2);  // Excessive call, takes default action #1.
219   foo_.Bar2(1, 1);  // Expected call.
220   foo_.Bar2(1, 1);  // Excessive call, takes default action #2.
221 }
222 
223 TEST_F(GMockOutputTest, UninterestingCallWithDefaultAction) {
224   ON_CALL(foo_, Bar2(_, _)).WillByDefault(Return(true));   // Default action #1
225   ON_CALL(foo_, Bar2(1, _)).WillByDefault(Return(false));  // Default action #2
226 
227   foo_.Bar2(2, 2);  // Uninteresting call, takes default action #1.
228   foo_.Bar2(1, 1);  // Uninteresting call, takes default action #2.
229 }
230 
231 TEST_F(GMockOutputTest, ExplicitActionsRunOutWithDefaultAction) {
232   ON_CALL(foo_, Bar2(_, _)).WillByDefault(Return(true));  // Default action #1
233 
234   EXPECT_CALL(foo_, Bar2(_, _)).Times(2).WillOnce(Return(false));
235   foo_.Bar2(2, 2);
236   foo_.Bar2(1, 1);  // Explicit actions in EXPECT_CALL run out.
237 }
238 
239 TEST_F(GMockOutputTest, CatchesLeakedMocks) {
240   MockFoo* foo1 = new MockFoo;
241   MockFoo* foo2 = new MockFoo;
242 
243   // Invokes ON_CALL on foo1.
244   ON_CALL(*foo1, Bar(_, _, _)).WillByDefault(Return('a'));
245 
246   // Invokes EXPECT_CALL on foo2.
247   EXPECT_CALL(*foo2, Bar2(_, _));
248   EXPECT_CALL(*foo2, Bar2(1, _));
249   EXPECT_CALL(*foo2, Bar3(_, _)).Times(AnyNumber());
250   foo2->Bar2(2, 1);
251   foo2->Bar2(1, 1);
252 
253   // Both foo1 and foo2 are deliberately leaked.
254 }
255 
256 MATCHER_P2(IsPair, first, second, "") {
257   return Value(arg.first, first) && Value(arg.second, second);
258 }
259 
260 TEST_F(GMockOutputTest, PrintsMatcher) {
261   const testing::Matcher<int> m1 = Ge(48);
262   EXPECT_THAT((std::pair<int, bool>(42, true)), IsPair(m1, true));
263 }
264 
265 void TestCatchesLeakedMocksInAdHocTests() {
266   MockFoo* foo = new MockFoo;
267 
268   // Invokes EXPECT_CALL on foo.
269   EXPECT_CALL(*foo, Bar2(_, _));
270   foo->Bar2(2, 1);
271 
272   // foo is deliberately leaked.
273 }
274 
275 int main(int argc, char** argv) {
276   testing::InitGoogleMock(&argc, argv);
277   // Ensures that the tests pass no matter what value of
278   // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies.
279   GMOCK_FLAG_SET(catch_leaked_mocks, true);
280   GMOCK_FLAG_SET(verbose, "warning");
281 
282   TestCatchesLeakedMocksInAdHocTests();
283   return RUN_ALL_TESTS();
284 }
285 
286 GTEST_DISABLE_MSC_WARNINGS_POP_()  // 4100
287