proxygen
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)
 
void TestCatchesLeakedMocksInAdHocTests ()
 
int main (int argc, char **argv)
 

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 281 of file gmock_output_test_.cc.

References GMOCK_FLAG, testing::InitGoogleMock(), RUN_ALL_TESTS(), and TestCatchesLeakedMocksInAdHocTests().

281  {
283 
284  // Ensures that the tests pass no matter what value of
285  // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies.
286  testing::GMOCK_FLAG(catch_leaked_mocks) = true;
287  testing::GMOCK_FLAG(verbose) = "warning";
288 
290  return RUN_ALL_TESTS();
291 }
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2232
GTEST_API_ void InitGoogleMock(int *argc, char **argv)
Definition: gmock.cc:173
char ** argv
#define GMOCK_FLAG(name)
Definition: gmock-port.h:70
void TestCatchesLeakedMocksInAdHocTests()
TEST_F ( GMockOutputTest  ,
ExpectedCall   
)

Definition at line 68 of file gmock_output_test_.cc.

References testing::_, EXPECT_CALL, and GMOCK_FLAG.

68  {
69  testing::GMOCK_FLAG(verbose) = "info";
70 
71  EXPECT_CALL(foo_, Bar2(0, _));
72  foo_.Bar2(0, 0); // Expected call
73 
74  testing::GMOCK_FLAG(verbose) = "warning";
75 }
#define GMOCK_FLAG(name)
Definition: gmock-port.h:70
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
TEST_F ( GMockOutputTest  ,
ExpectedCallToVoidFunction   
)

Definition at line 77 of file gmock_output_test_.cc.

References testing::_, EXPECT_CALL, and GMOCK_FLAG.

77  {
78  testing::GMOCK_FLAG(verbose) = "info";
79 
80  EXPECT_CALL(foo_, Bar3(0, _));
81  foo_.Bar3(0, 0); // Expected call
82 
83  testing::GMOCK_FLAG(verbose) = "warning";
84 }
#define GMOCK_FLAG(name)
Definition: gmock-port.h:70
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
TEST_F ( GMockOutputTest  ,
ExplicitActionsRunOut   
)

Definition at line 86 of file gmock_output_test_.cc.

References testing::_, EXPECT_CALL, and testing::Return().

86  {
87  EXPECT_CALL(foo_, Bar2(_, _))
88  .Times(2)
89  .WillOnce(Return(false));
90  foo_.Bar2(2, 2);
91  foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out.
92 }
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
internal::ReturnAction< R > Return(R value)
TEST_F ( GMockOutputTest  ,
UnexpectedCall   
)

Definition at line 94 of file gmock_output_test_.cc.

References testing::_, and EXPECT_CALL.

94  {
95  EXPECT_CALL(foo_, Bar2(0, _));
96 
97  foo_.Bar2(1, 0); // Unexpected call
98  foo_.Bar2(0, 0); // Expected call
99 }
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
TEST_F ( GMockOutputTest  ,
UnexpectedCallToVoidFunction   
)

Definition at line 101 of file gmock_output_test_.cc.

References testing::_, and EXPECT_CALL.

101  {
102  EXPECT_CALL(foo_, Bar3(0, _));
103 
104  foo_.Bar3(1, 0); // Unexpected call
105  foo_.Bar3(0, 0); // Expected call
106 }
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
TEST_F ( GMockOutputTest  ,
ExcessiveCall   
)

Definition at line 108 of file gmock_output_test_.cc.

References testing::_, and EXPECT_CALL.

108  {
109  EXPECT_CALL(foo_, Bar2(0, _));
110 
111  foo_.Bar2(0, 0); // Expected call
112  foo_.Bar2(0, 1); // Excessive call
113 }
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
TEST_F ( GMockOutputTest  ,
ExcessiveCallToVoidFunction   
)

Definition at line 115 of file gmock_output_test_.cc.

References testing::_, and EXPECT_CALL.

115  {
116  EXPECT_CALL(foo_, Bar3(0, _));
117 
118  foo_.Bar3(0, 0); // Expected call
119  foo_.Bar3(0, 1); // Excessive call
120 }
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
TEST_F ( GMockOutputTest  ,
UninterestingCall   
)

Definition at line 122 of file gmock_output_test_.cc.

122  {
123  foo_.Bar2(0, 1); // Uninteresting call
124 }
TEST_F ( GMockOutputTest  ,
UninterestingCallToVoidFunction   
)

Definition at line 126 of file gmock_output_test_.cc.

126  {
127  foo_.Bar3(0, 1); // Uninteresting call
128 }
TEST_F ( GMockOutputTest  ,
RetiredExpectation   
)

Definition at line 130 of file gmock_output_test_.cc.

References testing::_, and EXPECT_CALL.

130  {
131  EXPECT_CALL(foo_, Bar2(_, _))
132  .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 }
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
TEST_F ( GMockOutputTest  ,
UnsatisfiedPrerequisite   
)

Definition at line 140 of file gmock_output_test_.cc.

References testing::_, EXPECT_CALL, and s.

140  {
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 }
static set< string > s
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
TEST_F ( GMockOutputTest  ,
UnsatisfiedPrerequisites   
)

Definition at line 154 of file gmock_output_test_.cc.

References testing::_, and EXPECT_CALL.

154  {
155  Sequence s1, s2;
156 
157  EXPECT_CALL(foo_, Bar(_, 0, _))
158  .InSequence(s1);
159  EXPECT_CALL(foo_, Bar2(0, 0))
160  .InSequence(s2);
161  EXPECT_CALL(foo_, Bar2(1, _))
162  .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 }
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
TEST_F ( GMockOutputTest  ,
UnsatisfiedWith   
)

Definition at line 170 of file gmock_output_test_.cc.

References testing::_, EXPECT_CALL, and testing::Ge().

170  {
171  EXPECT_CALL(foo_, Bar2(_, _)).With(Ge());
172 }
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
internal::GeMatcher< Rhs > Ge(Rhs x)
TEST_F ( GMockOutputTest  ,
UnsatisfiedExpectation   
)

Definition at line 174 of file gmock_output_test_.cc.

References testing::_, and EXPECT_CALL.

174  {
175  EXPECT_CALL(foo_, Bar(_, _, _));
176  EXPECT_CALL(foo_, Bar2(0, _))
177  .Times(2);
178 
179  foo_.Bar2(0, 1);
180 }
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
TEST_F ( GMockOutputTest  ,
MismatchArguments   
)

Definition at line 182 of file gmock_output_test_.cc.

References testing::_, EXPECT_CALL, testing::Ge(), testing::Ref(), s, and string.

182  {
183  const std::string s = "Hi";
184  EXPECT_CALL(foo_, Bar(Ref(s), _, Ge(0)));
185 
186  foo_.Bar("Ho", 0, -0.1); // Mismatch arguments
187  foo_.Bar(s, 0, 0);
188 }
internal::RefMatcher< T & > Ref(T &x)
const char * string
Definition: Conv.cpp:212
static set< string > s
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
internal::GeMatcher< Rhs > Ge(Rhs x)
TEST_F ( GMockOutputTest  ,
MismatchWith   
)

Definition at line 190 of file gmock_output_test_.cc.

References EXPECT_CALL, and testing::Ge().

190  {
191  EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1)))
192  .With(Ge());
193 
194  foo_.Bar2(2, 3); // Mismatch With()
195  foo_.Bar2(2, 1);
196 }
#define EXPECT_CALL(obj, call)
internal::GeMatcher< Rhs > Ge(Rhs x)
TEST_F ( GMockOutputTest  ,
MismatchArgumentsAndWith   
)

Definition at line 198 of file gmock_output_test_.cc.

References EXPECT_CALL, and testing::Ge().

198  {
199  EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1)))
200  .With(Ge());
201 
202  foo_.Bar2(1, 3); // Mismatch arguments and mismatch With()
203  foo_.Bar2(2, 1);
204 }
#define EXPECT_CALL(obj, call)
internal::GeMatcher< Rhs > Ge(Rhs x)
TEST_F ( GMockOutputTest  ,
UnexpectedCallWithDefaultAction   
)

Definition at line 206 of file gmock_output_test_.cc.

References testing::_, EXPECT_CALL, ON_CALL, and testing::Return().

206  {
207  ON_CALL(foo_, Bar2(_, _))
208  .WillByDefault(Return(true)); // Default action #1
209  ON_CALL(foo_, Bar2(1, _))
210  .WillByDefault(Return(false)); // Default action #2
211 
212  EXPECT_CALL(foo_, Bar2(2, 2));
213  foo_.Bar2(1, 0); // Unexpected call, takes default action #2.
214  foo_.Bar2(0, 0); // Unexpected call, takes default action #1.
215  foo_.Bar2(2, 2); // Expected call.
216 }
#define ON_CALL(obj, call)
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
internal::ReturnAction< R > Return(R value)
TEST_F ( GMockOutputTest  ,
ExcessiveCallWithDefaultAction   
)

Definition at line 218 of file gmock_output_test_.cc.

References testing::_, EXPECT_CALL, ON_CALL, and testing::Return().

218  {
219  ON_CALL(foo_, Bar2(_, _))
220  .WillByDefault(Return(true)); // Default action #1
221  ON_CALL(foo_, Bar2(1, _))
222  .WillByDefault(Return(false)); // Default action #2
223 
224  EXPECT_CALL(foo_, Bar2(2, 2));
225  EXPECT_CALL(foo_, Bar2(1, 1));
226 
227  foo_.Bar2(2, 2); // Expected call.
228  foo_.Bar2(2, 2); // Excessive call, takes default action #1.
229  foo_.Bar2(1, 1); // Expected call.
230  foo_.Bar2(1, 1); // Excessive call, takes default action #2.
231 }
#define ON_CALL(obj, call)
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
internal::ReturnAction< R > Return(R value)
TEST_F ( GMockOutputTest  ,
UninterestingCallWithDefaultAction   
)

Definition at line 233 of file gmock_output_test_.cc.

References testing::_, ON_CALL, and testing::Return().

233  {
234  ON_CALL(foo_, Bar2(_, _))
235  .WillByDefault(Return(true)); // Default action #1
236  ON_CALL(foo_, Bar2(1, _))
237  .WillByDefault(Return(false)); // Default action #2
238 
239  foo_.Bar2(2, 2); // Uninteresting call, takes default action #1.
240  foo_.Bar2(1, 1); // Uninteresting call, takes default action #2.
241 }
#define ON_CALL(obj, call)
const internal::AnythingMatcher _
internal::ReturnAction< R > Return(R value)
TEST_F ( GMockOutputTest  ,
ExplicitActionsRunOutWithDefaultAction   
)

Definition at line 243 of file gmock_output_test_.cc.

References testing::_, EXPECT_CALL, ON_CALL, and testing::Return().

243  {
244  ON_CALL(foo_, Bar2(_, _))
245  .WillByDefault(Return(true)); // Default action #1
246 
247  EXPECT_CALL(foo_, Bar2(_, _))
248  .Times(2)
249  .WillOnce(Return(false));
250  foo_.Bar2(2, 2);
251  foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out.
252 }
#define ON_CALL(obj, call)
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
internal::ReturnAction< R > Return(R value)
TEST_F ( GMockOutputTest  ,
CatchesLeakedMocks   
)

Definition at line 254 of file gmock_output_test_.cc.

References testing::_, testing::AnyNumber(), EXPECT_CALL, foo1(), foo2(), ON_CALL, and testing::Return().

254  {
255  MockFoo* foo1 = new MockFoo;
256  MockFoo* foo2 = new MockFoo;
257 
258  // Invokes ON_CALL on foo1.
259  ON_CALL(*foo1, Bar(_, _, _)).WillByDefault(Return('a'));
260 
261  // Invokes EXPECT_CALL on foo2.
262  EXPECT_CALL(*foo2, Bar2(_, _));
263  EXPECT_CALL(*foo2, Bar2(1, _));
264  EXPECT_CALL(*foo2, Bar3(_, _)).Times(AnyNumber());
265  foo2->Bar2(2, 1);
266  foo2->Bar2(1, 1);
267 
268  // Both foo1 and foo2 are deliberately leaked.
269 }
FOLLY_NOINLINE void foo2()
FOLLY_NOINLINE void foo1()
GTEST_API_ Cardinality AnyNumber()
#define ON_CALL(obj, call)
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
internal::ReturnAction< R > Return(R value)
void TestCatchesLeakedMocksInAdHocTests ( )

Definition at line 271 of file gmock_output_test_.cc.

References testing::_, and EXPECT_CALL.

Referenced by main().

271  {
272  MockFoo* foo = new MockFoo;
273 
274  // Invokes EXPECT_CALL on foo.
275  EXPECT_CALL(*foo, Bar2(_, _));
276  foo->Bar2(2, 1);
277 
278  // foo is deliberately leaked.
279 }
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _