tesseract v5.3.3.20231005
gmock_output_test_.cc File Reference
#include "gmock/gmock.h"
#include <stdio.h>
#include <string>
#include "gtest/gtest.h"

Go to the source code of this file.

Classes

class  MockFoo
 
class  GMockOutputTest
 

Functions

 TEST_F (GMockOutputTest, ExpectedCall)
 
 TEST_F (GMockOutputTest, ExpectedCallToVoidFunction)
 
 TEST_F (GMockOutputTest, ExplicitActionsRunOut)
 
 TEST_F (GMockOutputTest, UnexpectedCall)
 
 TEST_F (GMockOutputTest, UnexpectedCallToVoidFunction)
 
 TEST_F (GMockOutputTest, ExcessiveCall)
 
 TEST_F (GMockOutputTest, ExcessiveCallToVoidFunction)
 
 TEST_F (GMockOutputTest, UninterestingCall)
 
 TEST_F (GMockOutputTest, UninterestingCallToVoidFunction)
 
 TEST_F (GMockOutputTest, RetiredExpectation)
 
 TEST_F (GMockOutputTest, UnsatisfiedPrerequisite)
 
 TEST_F (GMockOutputTest, UnsatisfiedPrerequisites)
 
 TEST_F (GMockOutputTest, UnsatisfiedWith)
 
 TEST_F (GMockOutputTest, UnsatisfiedExpectation)
 
 TEST_F (GMockOutputTest, MismatchArguments)
 
 TEST_F (GMockOutputTest, MismatchWith)
 
 TEST_F (GMockOutputTest, MismatchArgumentsAndWith)
 
 TEST_F (GMockOutputTest, UnexpectedCallWithDefaultAction)
 
 TEST_F (GMockOutputTest, ExcessiveCallWithDefaultAction)
 
 TEST_F (GMockOutputTest, UninterestingCallWithDefaultAction)
 
 TEST_F (GMockOutputTest, ExplicitActionsRunOutWithDefaultAction)
 
 TEST_F (GMockOutputTest, CatchesLeakedMocks)
 
 MATCHER_P2 (IsPair, first, second, "")
 
 TEST_F (GMockOutputTest, PrintsMatcher)
 
void TestCatchesLeakedMocksInAdHocTests ()
 
int main (int argc, char **argv)
 

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 296 of file gmock_output_test_.cc.

296 {
297 testing::InitGoogleMock(&argc, argv);
298 // Ensures that the tests pass no matter what value of
299 // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies.
300 testing::GMOCK_FLAG(catch_leaked_mocks) = true;
301 testing::GMOCK_FLAG(verbose) = "warning";
302
304 return RUN_ALL_TESTS();
305}
#define GMOCK_FLAG(name)
Definition: gmock-port.h:67
void TestCatchesLeakedMocksInAdHocTests()
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2489
GTEST_API_ void InitGoogleMock(int *argc, char **argv)
Definition: gmock.cc:191

◆ MATCHER_P2()

MATCHER_P2 ( IsPair  ,
first  ,
second  ,
""   
)

Definition at line 277 of file gmock_output_test_.cc.

277 {
278 return Value(arg.first, first) && Value(arg.second, second);
279}

◆ TEST_F() [1/23]

TEST_F ( GMockOutputTest  ,
CatchesLeakedMocks   
)

Definition at line 260 of file gmock_output_test_.cc.

260 {
261 MockFoo* foo1 = new MockFoo;
262 MockFoo* foo2 = new MockFoo;
263
264 // Invokes ON_CALL on foo1.
265 ON_CALL(*foo1, Bar(_, _, _)).WillByDefault(Return('a'));
266
267 // Invokes EXPECT_CALL on foo2.
268 EXPECT_CALL(*foo2, Bar2(_, _));
269 EXPECT_CALL(*foo2, Bar2(1, _));
270 EXPECT_CALL(*foo2, Bar3(_, _)).Times(AnyNumber());
271 foo2->Bar2(2, 1);
272 foo2->Bar2(1, 1);
273
274 // Both foo1 and foo2 are deliberately leaked.
275}
#define EXPECT_CALL(obj, call)
#define ON_CALL(obj, call)
GTEST_API_ Cardinality AnyNumber()
internal::ReturnAction< R > Return(R value)

◆ TEST_F() [2/23]

TEST_F ( GMockOutputTest  ,
ExcessiveCall   
)

Definition at line 114 of file gmock_output_test_.cc.

114 {
115 EXPECT_CALL(foo_, Bar2(0, _));
116
117 foo_.Bar2(0, 0); // Expected call
118 foo_.Bar2(0, 1); // Excessive call
119}

◆ TEST_F() [3/23]

TEST_F ( GMockOutputTest  ,
ExcessiveCallToVoidFunction   
)

Definition at line 121 of file gmock_output_test_.cc.

121 {
122 EXPECT_CALL(foo_, Bar3(0, _));
123
124 foo_.Bar3(0, 0); // Expected call
125 foo_.Bar3(0, 1); // Excessive call
126}

◆ TEST_F() [4/23]

TEST_F ( GMockOutputTest  ,
ExcessiveCallWithDefaultAction   
)

Definition at line 224 of file gmock_output_test_.cc.

224 {
225 ON_CALL(foo_, Bar2(_, _))
226 .WillByDefault(Return(true)); // Default action #1
227 ON_CALL(foo_, Bar2(1, _))
228 .WillByDefault(Return(false)); // Default action #2
229
230 EXPECT_CALL(foo_, Bar2(2, 2));
231 EXPECT_CALL(foo_, Bar2(1, 1));
232
233 foo_.Bar2(2, 2); // Expected call.
234 foo_.Bar2(2, 2); // Excessive call, takes default action #1.
235 foo_.Bar2(1, 1); // Expected call.
236 foo_.Bar2(1, 1); // Excessive call, takes default action #2.
237}

◆ TEST_F() [5/23]

TEST_F ( GMockOutputTest  ,
ExpectedCall   
)

Definition at line 74 of file gmock_output_test_.cc.

74 {
75 testing::GMOCK_FLAG(verbose) = "info";
76
77 EXPECT_CALL(foo_, Bar2(0, _));
78 foo_.Bar2(0, 0); // Expected call
79
80 testing::GMOCK_FLAG(verbose) = "warning";
81}

◆ TEST_F() [6/23]

TEST_F ( GMockOutputTest  ,
ExpectedCallToVoidFunction   
)

Definition at line 83 of file gmock_output_test_.cc.

83 {
84 testing::GMOCK_FLAG(verbose) = "info";
85
86 EXPECT_CALL(foo_, Bar3(0, _));
87 foo_.Bar3(0, 0); // Expected call
88
89 testing::GMOCK_FLAG(verbose) = "warning";
90}

◆ TEST_F() [7/23]

TEST_F ( GMockOutputTest  ,
ExplicitActionsRunOut   
)

Definition at line 92 of file gmock_output_test_.cc.

92 {
93 EXPECT_CALL(foo_, Bar2(_, _))
94 .Times(2)
95 .WillOnce(Return(false));
96 foo_.Bar2(2, 2);
97 foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out.
98}

◆ TEST_F() [8/23]

TEST_F ( GMockOutputTest  ,
ExplicitActionsRunOutWithDefaultAction   
)

Definition at line 249 of file gmock_output_test_.cc.

249 {
250 ON_CALL(foo_, Bar2(_, _))
251 .WillByDefault(Return(true)); // Default action #1
252
253 EXPECT_CALL(foo_, Bar2(_, _))
254 .Times(2)
255 .WillOnce(Return(false));
256 foo_.Bar2(2, 2);
257 foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out.
258}

◆ TEST_F() [9/23]

TEST_F ( GMockOutputTest  ,
MismatchArguments   
)

Definition at line 188 of file gmock_output_test_.cc.

188 {
189 const std::string s = "Hi";
190 EXPECT_CALL(foo_, Bar(Ref(s), _, Ge(0)));
191
192 foo_.Bar("Ho", 0, -0.1); // Mismatch arguments
193 foo_.Bar(s, 0, 0);
194}

◆ TEST_F() [10/23]

TEST_F ( GMockOutputTest  ,
MismatchArgumentsAndWith   
)

Definition at line 204 of file gmock_output_test_.cc.

204 {
205 EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1)))
206 .With(Ge());
207
208 foo_.Bar2(1, 3); // Mismatch arguments and mismatch With()
209 foo_.Bar2(2, 1);
210}

◆ TEST_F() [11/23]

TEST_F ( GMockOutputTest  ,
MismatchWith   
)

Definition at line 196 of file gmock_output_test_.cc.

196 {
197 EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1)))
198 .With(Ge());
199
200 foo_.Bar2(2, 3); // Mismatch With()
201 foo_.Bar2(2, 1);
202}

◆ TEST_F() [12/23]

TEST_F ( GMockOutputTest  ,
PrintsMatcher   
)

Definition at line 281 of file gmock_output_test_.cc.

281 {
282 const testing::Matcher<int> m1 = Ge(48);
283 EXPECT_THAT((std::pair<int, bool>(42, true)), IsPair(m1, true));
284}
#define EXPECT_THAT(value, matcher)

◆ TEST_F() [13/23]

TEST_F ( GMockOutputTest  ,
RetiredExpectation   
)

Definition at line 136 of file gmock_output_test_.cc.

136 {
137 EXPECT_CALL(foo_, Bar2(_, _))
138 .RetiresOnSaturation();
139 EXPECT_CALL(foo_, Bar2(0, 0));
140
141 foo_.Bar2(1, 1);
142 foo_.Bar2(1, 1); // Matches a retired expectation
143 foo_.Bar2(0, 0);
144}

◆ TEST_F() [14/23]

TEST_F ( GMockOutputTest  ,
UnexpectedCall   
)

Definition at line 100 of file gmock_output_test_.cc.

100 {
101 EXPECT_CALL(foo_, Bar2(0, _));
102
103 foo_.Bar2(1, 0); // Unexpected call
104 foo_.Bar2(0, 0); // Expected call
105}

◆ TEST_F() [15/23]

TEST_F ( GMockOutputTest  ,
UnexpectedCallToVoidFunction   
)

Definition at line 107 of file gmock_output_test_.cc.

107 {
108 EXPECT_CALL(foo_, Bar3(0, _));
109
110 foo_.Bar3(1, 0); // Unexpected call
111 foo_.Bar3(0, 0); // Expected call
112}

◆ TEST_F() [16/23]

TEST_F ( GMockOutputTest  ,
UnexpectedCallWithDefaultAction   
)

Definition at line 212 of file gmock_output_test_.cc.

212 {
213 ON_CALL(foo_, Bar2(_, _))
214 .WillByDefault(Return(true)); // Default action #1
215 ON_CALL(foo_, Bar2(1, _))
216 .WillByDefault(Return(false)); // Default action #2
217
218 EXPECT_CALL(foo_, Bar2(2, 2));
219 foo_.Bar2(1, 0); // Unexpected call, takes default action #2.
220 foo_.Bar2(0, 0); // Unexpected call, takes default action #1.
221 foo_.Bar2(2, 2); // Expected call.
222}

◆ TEST_F() [17/23]

TEST_F ( GMockOutputTest  ,
UninterestingCall   
)

Definition at line 128 of file gmock_output_test_.cc.

128 {
129 foo_.Bar2(0, 1); // Uninteresting call
130}

◆ TEST_F() [18/23]

TEST_F ( GMockOutputTest  ,
UninterestingCallToVoidFunction   
)

Definition at line 132 of file gmock_output_test_.cc.

132 {
133 foo_.Bar3(0, 1); // Uninteresting call
134}

◆ TEST_F() [19/23]

TEST_F ( GMockOutputTest  ,
UninterestingCallWithDefaultAction   
)

Definition at line 239 of file gmock_output_test_.cc.

239 {
240 ON_CALL(foo_, Bar2(_, _))
241 .WillByDefault(Return(true)); // Default action #1
242 ON_CALL(foo_, Bar2(1, _))
243 .WillByDefault(Return(false)); // Default action #2
244
245 foo_.Bar2(2, 2); // Uninteresting call, takes default action #1.
246 foo_.Bar2(1, 1); // Uninteresting call, takes default action #2.
247}

◆ TEST_F() [20/23]

TEST_F ( GMockOutputTest  ,
UnsatisfiedExpectation   
)

Definition at line 180 of file gmock_output_test_.cc.

180 {
181 EXPECT_CALL(foo_, Bar(_, _, _));
182 EXPECT_CALL(foo_, Bar2(0, _))
183 .Times(2);
184
185 foo_.Bar2(0, 1);
186}

◆ TEST_F() [21/23]

TEST_F ( GMockOutputTest  ,
UnsatisfiedPrerequisite   
)

Definition at line 146 of file gmock_output_test_.cc.

146 {
147 {
148 InSequence s;
149 EXPECT_CALL(foo_, Bar(_, 0, _));
150 EXPECT_CALL(foo_, Bar2(0, 0));
151 EXPECT_CALL(foo_, Bar2(1, _));
152 }
153
154 foo_.Bar2(1, 0); // Has one immediate unsatisfied pre-requisite
155 foo_.Bar("Hi", 0, 0);
156 foo_.Bar2(0, 0);
157 foo_.Bar2(1, 0);
158}

◆ TEST_F() [22/23]

TEST_F ( GMockOutputTest  ,
UnsatisfiedPrerequisites   
)

Definition at line 160 of file gmock_output_test_.cc.

160 {
161 Sequence s1, s2;
162
163 EXPECT_CALL(foo_, Bar(_, 0, _))
164 .InSequence(s1);
165 EXPECT_CALL(foo_, Bar2(0, 0))
166 .InSequence(s2);
167 EXPECT_CALL(foo_, Bar2(1, _))
168 .InSequence(s1, s2);
169
170 foo_.Bar2(1, 0); // Has two immediate unsatisfied pre-requisites
171 foo_.Bar("Hi", 0, 0);
172 foo_.Bar2(0, 0);
173 foo_.Bar2(1, 0);
174}

◆ TEST_F() [23/23]

TEST_F ( GMockOutputTest  ,
UnsatisfiedWith   
)

Definition at line 176 of file gmock_output_test_.cc.

176 {
177 EXPECT_CALL(foo_, Bar2(_, _)).With(Ge());
178}

◆ TestCatchesLeakedMocksInAdHocTests()

void TestCatchesLeakedMocksInAdHocTests ( )

Definition at line 286 of file gmock_output_test_.cc.

286 {
287 MockFoo* foo = new MockFoo;
288
289 // Invokes EXPECT_CALL on foo.
290 EXPECT_CALL(*foo, Bar2(_, _));
291 foo->Bar2(2, 1);
292
293 // foo is deliberately leaked.
294}