gmock_output_test_.cc 8.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Copyright 2008, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Gennadiy Civil's avatar
 
Gennadiy Civil committed
29

30
31
32
33
// Tests Google Mock's output in various scenarios.  This ensures that
// Google Mock's messages are readable and useful.

#include <stdio.h>
34

35
#include <string>
36
#include <tuple>
37

38
#include "gmock/gmock.h"
39
#include "gtest/gtest.h"
40

Gennadiy Civil's avatar
 
Gennadiy Civil committed
41
// Silence C4100 (unreferenced formal parameter)
42
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4100)
Gennadiy Civil's avatar
Gennadiy Civil committed
43

44
using testing::_;
45
using testing::AnyNumber;
46
47
using testing::Ge;
using testing::InSequence;
48
using testing::NaggyMock;
49
50
51
using testing::Ref;
using testing::Return;
using testing::Sequence;
Gennadiy Civil's avatar
Gennadiy Civil committed
52
using testing::Value;
53
54
55

class MockFoo {
 public:
56
57
  MockFoo() {}

58
59
60
  MOCK_METHOD3(Bar, char(const std::string& s, int i, double x));
  MOCK_METHOD2(Bar2, bool(int x, int y));
  MOCK_METHOD2(Bar3, void(int x, int y));
61
62

 private:
63
64
  MockFoo(const MockFoo&) = delete;
  MockFoo& operator=(const MockFoo&) = delete;
65
66
67
68
};

class GMockOutputTest : public testing::Test {
 protected:
69
  NaggyMock<MockFoo> foo_;
70
71
72
};

TEST_F(GMockOutputTest, ExpectedCall) {
Abseil Team's avatar
Abseil Team committed
73
  GMOCK_FLAG_SET(verbose, "info");
74
75
76
77

  EXPECT_CALL(foo_, Bar2(0, _));
  foo_.Bar2(0, 0);  // Expected call

Abseil Team's avatar
Abseil Team committed
78
  GMOCK_FLAG_SET(verbose, "warning");
79
80
81
}

TEST_F(GMockOutputTest, ExpectedCallToVoidFunction) {
Abseil Team's avatar
Abseil Team committed
82
  GMOCK_FLAG_SET(verbose, "info");
83
84
85
86

  EXPECT_CALL(foo_, Bar3(0, _));
  foo_.Bar3(0, 0);  // Expected call

Abseil Team's avatar
Abseil Team committed
87
  GMOCK_FLAG_SET(verbose, "warning");
88
89
90
}

TEST_F(GMockOutputTest, ExplicitActionsRunOut) {
91
  EXPECT_CALL(foo_, Bar2(_, _)).Times(2).WillOnce(Return(false));
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
  foo_.Bar2(2, 2);
  foo_.Bar2(1, 1);  // Explicit actions in EXPECT_CALL run out.
}

TEST_F(GMockOutputTest, UnexpectedCall) {
  EXPECT_CALL(foo_, Bar2(0, _));

  foo_.Bar2(1, 0);  // Unexpected call
  foo_.Bar2(0, 0);  // Expected call
}

TEST_F(GMockOutputTest, UnexpectedCallToVoidFunction) {
  EXPECT_CALL(foo_, Bar3(0, _));

  foo_.Bar3(1, 0);  // Unexpected call
  foo_.Bar3(0, 0);  // Expected call
}

TEST_F(GMockOutputTest, ExcessiveCall) {
  EXPECT_CALL(foo_, Bar2(0, _));

  foo_.Bar2(0, 0);  // Expected call
  foo_.Bar2(0, 1);  // Excessive call
}

TEST_F(GMockOutputTest, ExcessiveCallToVoidFunction) {
  EXPECT_CALL(foo_, Bar3(0, _));

  foo_.Bar3(0, 0);  // Expected call
  foo_.Bar3(0, 1);  // Excessive call
}

TEST_F(GMockOutputTest, UninterestingCall) {
  foo_.Bar2(0, 1);  // Uninteresting call
}

TEST_F(GMockOutputTest, UninterestingCallToVoidFunction) {
  foo_.Bar3(0, 1);  // Uninteresting call
}

TEST_F(GMockOutputTest, RetiredExpectation) {
133
  EXPECT_CALL(foo_, Bar2(_, _)).RetiresOnSaturation();
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
  EXPECT_CALL(foo_, Bar2(0, 0));

  foo_.Bar2(1, 1);
  foo_.Bar2(1, 1);  // Matches a retired expectation
  foo_.Bar2(0, 0);
}

TEST_F(GMockOutputTest, UnsatisfiedPrerequisite) {
  {
    InSequence s;
    EXPECT_CALL(foo_, Bar(_, 0, _));
    EXPECT_CALL(foo_, Bar2(0, 0));
    EXPECT_CALL(foo_, Bar2(1, _));
  }

  foo_.Bar2(1, 0);  // Has one immediate unsatisfied pre-requisite
  foo_.Bar("Hi", 0, 0);
  foo_.Bar2(0, 0);
  foo_.Bar2(1, 0);
}

TEST_F(GMockOutputTest, UnsatisfiedPrerequisites) {
  Sequence s1, s2;

158
159
160
  EXPECT_CALL(foo_, Bar(_, 0, _)).InSequence(s1);
  EXPECT_CALL(foo_, Bar2(0, 0)).InSequence(s2);
  EXPECT_CALL(foo_, Bar2(1, _)).InSequence(s1, s2);
161
162
163
164
165
166
167

  foo_.Bar2(1, 0);  // Has two immediate unsatisfied pre-requisites
  foo_.Bar("Hi", 0, 0);
  foo_.Bar2(0, 0);
  foo_.Bar2(1, 0);
}

168
169
170
171
TEST_F(GMockOutputTest, UnsatisfiedWith) {
  EXPECT_CALL(foo_, Bar2(_, _)).With(Ge());
}

172
173
TEST_F(GMockOutputTest, UnsatisfiedExpectation) {
  EXPECT_CALL(foo_, Bar(_, _, _));
174
  EXPECT_CALL(foo_, Bar2(0, _)).Times(2);
175
176
177
178
179
180
181
182
183
184
185
186

  foo_.Bar2(0, 1);
}

TEST_F(GMockOutputTest, MismatchArguments) {
  const std::string s = "Hi";
  EXPECT_CALL(foo_, Bar(Ref(s), _, Ge(0)));

  foo_.Bar("Ho", 0, -0.1);  // Mismatch arguments
  foo_.Bar(s, 0, 0);
}

187
TEST_F(GMockOutputTest, MismatchWith) {
188
  EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1))).With(Ge());
189

190
  foo_.Bar2(2, 3);  // Mismatch With()
191
192
193
  foo_.Bar2(2, 1);
}

194
TEST_F(GMockOutputTest, MismatchArgumentsAndWith) {
195
  EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1))).With(Ge());
196

197
  foo_.Bar2(1, 3);  // Mismatch arguments and mismatch With()
198
199
200
201
  foo_.Bar2(2, 1);
}

TEST_F(GMockOutputTest, UnexpectedCallWithDefaultAction) {
202
203
  ON_CALL(foo_, Bar2(_, _)).WillByDefault(Return(true));   // Default action #1
  ON_CALL(foo_, Bar2(1, _)).WillByDefault(Return(false));  // Default action #2
204
205
206
207
208
209
210
211

  EXPECT_CALL(foo_, Bar2(2, 2));
  foo_.Bar2(1, 0);  // Unexpected call, takes default action #2.
  foo_.Bar2(0, 0);  // Unexpected call, takes default action #1.
  foo_.Bar2(2, 2);  // Expected call.
}

TEST_F(GMockOutputTest, ExcessiveCallWithDefaultAction) {
212
213
  ON_CALL(foo_, Bar2(_, _)).WillByDefault(Return(true));   // Default action #1
  ON_CALL(foo_, Bar2(1, _)).WillByDefault(Return(false));  // Default action #2
214
215
216
217
218
219
220
221
222
223
224

  EXPECT_CALL(foo_, Bar2(2, 2));
  EXPECT_CALL(foo_, Bar2(1, 1));

  foo_.Bar2(2, 2);  // Expected call.
  foo_.Bar2(2, 2);  // Excessive call, takes default action #1.
  foo_.Bar2(1, 1);  // Expected call.
  foo_.Bar2(1, 1);  // Excessive call, takes default action #2.
}

TEST_F(GMockOutputTest, UninterestingCallWithDefaultAction) {
225
226
  ON_CALL(foo_, Bar2(_, _)).WillByDefault(Return(true));   // Default action #1
  ON_CALL(foo_, Bar2(1, _)).WillByDefault(Return(false));  // Default action #2
227
228
229
230
231
232

  foo_.Bar2(2, 2);  // Uninteresting call, takes default action #1.
  foo_.Bar2(1, 1);  // Uninteresting call, takes default action #2.
}

TEST_F(GMockOutputTest, ExplicitActionsRunOutWithDefaultAction) {
233
  ON_CALL(foo_, Bar2(_, _)).WillByDefault(Return(true));  // Default action #1
234

235
  EXPECT_CALL(foo_, Bar2(_, _)).Times(2).WillOnce(Return(false));
236
237
238
  foo_.Bar2(2, 2);
  foo_.Bar2(1, 1);  // Explicit actions in EXPECT_CALL run out.
}
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256

TEST_F(GMockOutputTest, CatchesLeakedMocks) {
  MockFoo* foo1 = new MockFoo;
  MockFoo* foo2 = new MockFoo;

  // Invokes ON_CALL on foo1.
  ON_CALL(*foo1, Bar(_, _, _)).WillByDefault(Return('a'));

  // Invokes EXPECT_CALL on foo2.
  EXPECT_CALL(*foo2, Bar2(_, _));
  EXPECT_CALL(*foo2, Bar2(1, _));
  EXPECT_CALL(*foo2, Bar3(_, _)).Times(AnyNumber());
  foo2->Bar2(2, 1);
  foo2->Bar2(1, 1);

  // Both foo1 and foo2 are deliberately leaked.
}

Gennadiy Civil's avatar
Gennadiy Civil committed
257
MATCHER_P2(IsPair, first, second, "") {
258
  return Value(std::get<0>(arg), first) && Value(std::get<1>(arg), second);
Gennadiy Civil's avatar
Gennadiy Civil committed
259
260
}

Gennadiy Civil's avatar
Gennadiy Civil committed
261
262
TEST_F(GMockOutputTest, PrintsMatcher) {
  const testing::Matcher<int> m1 = Ge(48);
263
264
265
266
267
  // Explicitly using std::tuple instead of std::pair due to differences between
  // MSVC and other compilers. std::pair is printed as
  // "struct std::pair<int,bool>" when using MSVC vs "std::pair<int,bool>" with
  // other compilers.
  EXPECT_THAT((std::tuple<int, bool>(42, true)), IsPair(m1, true));
Gennadiy Civil's avatar
Gennadiy Civil committed
268
269
}

270
271
272
273
274
275
276
277
278
279
void TestCatchesLeakedMocksInAdHocTests() {
  MockFoo* foo = new MockFoo;

  // Invokes EXPECT_CALL on foo.
  EXPECT_CALL(*foo, Bar2(_, _));
  foo->Bar2(2, 1);

  // foo is deliberately leaked.
}

280
int main(int argc, char** argv) {
281
282
283
  testing::InitGoogleMock(&argc, argv);
  // Ensures that the tests pass no matter what value of
  // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies.
Abseil Team's avatar
Abseil Team committed
284
285
  GMOCK_FLAG_SET(catch_leaked_mocks, true);
  GMOCK_FLAG_SET(verbose, "warning");
286

287
  TestCatchesLeakedMocksInAdHocTests();
288
289
  return RUN_ALL_TESTS();
}
Gennadiy Civil's avatar
 
Gennadiy Civil committed
290

291
GTEST_DISABLE_MSC_WARNINGS_POP_()  // 4100