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