tesseract v5.3.3.20231005
gmock-nice-strict_test.cc
Go to the documentation of this file.
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
31
32#include <string>
33#include <utility>
34#include "gmock/gmock.h"
35#include "gtest/gtest-spi.h"
36#include "gtest/gtest.h"
37
38// This must not be defined inside the ::testing namespace, or it will
39// clash with ::testing::Mock.
40class Mock {
41 public:
42 Mock() {}
43
44 MOCK_METHOD0(DoThis, void());
45
46 private:
47 GTEST_DISALLOW_COPY_AND_ASSIGN_(Mock);
48};
49
50namespace testing {
51namespace gmock_nice_strict_test {
52
53using testing::GMOCK_FLAG(verbose);
54using testing::HasSubstr;
58
59#if GTEST_HAS_STREAM_REDIRECTION
62#endif
63
64// Class without default constructor.
66 public:
67 explicit NotDefaultConstructible(int) {}
68};
69
71 public:
73 MOCK_METHOD(void, OnDestroy, ());
74};
75
76// Defines some mock classes needed by the tests.
77
78class Foo {
79 public:
80 virtual ~Foo() {}
81
82 virtual void DoThis() = 0;
83 virtual int DoThat(bool flag) = 0;
84};
85
86class MockFoo : public Foo {
87 public:
89 void Delete() { delete this; }
90
92 MOCK_METHOD1(DoThat, int(bool flag));
93 MOCK_METHOD0(ReturnNonDefaultConstructible, NotDefaultConstructible());
94
95 private:
96 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo);
97};
98
99class MockBar {
100 public:
101 explicit MockBar(const std::string& s) : str_(s) {}
102
103 MockBar(char a1, char a2, std::string a3, std::string a4, int a5, int a6,
104 const std::string& a7, const std::string& a8, bool a9, bool a10) {
105 str_ = std::string() + a1 + a2 + a3 + a4 + static_cast<char>(a5) +
106 static_cast<char>(a6) + a7 + a8 + (a9 ? 'T' : 'F') + (a10 ? 'T' : 'F');
107 }
108
109 virtual ~MockBar() {}
110
111 const std::string& str() const { return str_; }
112
113 MOCK_METHOD0(This, int());
114 MOCK_METHOD2(That, std::string(int, bool));
115
116 private:
117 std::string str_;
118
119 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockBar);
120};
121
122
123class MockBaz {
124 public:
125 class MoveOnly {
126 public:
127 MoveOnly() = default;
128
129 MoveOnly(const MoveOnly&) = delete;
130 MoveOnly& operator=(const MoveOnly&) = delete;
131
132 MoveOnly(MoveOnly&&) = default;
134 };
135
137};
138
139#if GTEST_HAS_STREAM_REDIRECTION
140
141// Tests that a raw mock generates warnings for uninteresting calls.
142TEST(RawMockTest, WarningForUninterestingCall) {
143 const std::string saved_flag = GMOCK_FLAG(verbose);
144 GMOCK_FLAG(verbose) = "warning";
145
146 MockFoo raw_foo;
147
149 raw_foo.DoThis();
150 raw_foo.DoThat(true);
152 HasSubstr("Uninteresting mock function call"));
153
154 GMOCK_FLAG(verbose) = saved_flag;
155}
156
157// Tests that a raw mock generates warnings for uninteresting calls
158// that delete the mock object.
159TEST(RawMockTest, WarningForUninterestingCallAfterDeath) {
160 const std::string saved_flag = GMOCK_FLAG(verbose);
161 GMOCK_FLAG(verbose) = "warning";
162
163 MockFoo* const raw_foo = new MockFoo;
164
165 ON_CALL(*raw_foo, DoThis())
166 .WillByDefault(Invoke(raw_foo, &MockFoo::Delete));
167
169 raw_foo->DoThis();
171 HasSubstr("Uninteresting mock function call"));
172
173 GMOCK_FLAG(verbose) = saved_flag;
174}
175
176// Tests that a raw mock generates informational logs for
177// uninteresting calls.
178TEST(RawMockTest, InfoForUninterestingCall) {
179 MockFoo raw_foo;
180
181 const std::string saved_flag = GMOCK_FLAG(verbose);
182 GMOCK_FLAG(verbose) = "info";
184 raw_foo.DoThis();
186 HasSubstr("Uninteresting mock function call"));
187
188 GMOCK_FLAG(verbose) = saved_flag;
189}
190
191TEST(RawMockTest, IsNaggy_IsNice_IsStrict) {
192 MockFoo raw_foo;
193 EXPECT_TRUE(Mock::IsNaggy(&raw_foo));
194 EXPECT_FALSE(Mock::IsNice(&raw_foo));
195 EXPECT_FALSE(Mock::IsStrict(&raw_foo));
196}
197
198// Tests that a nice mock generates no warning for uninteresting calls.
199TEST(NiceMockTest, NoWarningForUninterestingCall) {
200 NiceMock<MockFoo> nice_foo;
201
203 nice_foo.DoThis();
204 nice_foo.DoThat(true);
206}
207
208// Tests that a nice mock generates no warning for uninteresting calls
209// that delete the mock object.
210TEST(NiceMockTest, NoWarningForUninterestingCallAfterDeath) {
211 NiceMock<MockFoo>* const nice_foo = new NiceMock<MockFoo>;
212
213 ON_CALL(*nice_foo, DoThis())
214 .WillByDefault(Invoke(nice_foo, &MockFoo::Delete));
215
217 nice_foo->DoThis();
219}
220
221// Tests that a nice mock generates informational logs for
222// uninteresting calls.
223TEST(NiceMockTest, InfoForUninterestingCall) {
224 NiceMock<MockFoo> nice_foo;
225
226 const std::string saved_flag = GMOCK_FLAG(verbose);
227 GMOCK_FLAG(verbose) = "info";
229 nice_foo.DoThis();
231 HasSubstr("Uninteresting mock function call"));
232
233 GMOCK_FLAG(verbose) = saved_flag;
234}
235
236#endif // GTEST_HAS_STREAM_REDIRECTION
237
238// Tests that a nice mock allows expected calls.
239TEST(NiceMockTest, AllowsExpectedCall) {
240 NiceMock<MockFoo> nice_foo;
241
242 EXPECT_CALL(nice_foo, DoThis());
243 nice_foo.DoThis();
244}
245
246// Tests that an unexpected call on a nice mock which returns a
247// not-default-constructible type throws an exception and the exception contains
248// the method's name.
249TEST(NiceMockTest, ThrowsExceptionForUnknownReturnTypes) {
250 NiceMock<MockFoo> nice_foo;
251#if GTEST_HAS_EXCEPTIONS
252 try {
253 nice_foo.ReturnNonDefaultConstructible();
254 FAIL();
255 } catch (const std::runtime_error& ex) {
256 EXPECT_THAT(ex.what(), HasSubstr("ReturnNonDefaultConstructible"));
257 }
258#else
259 EXPECT_DEATH_IF_SUPPORTED({ nice_foo.ReturnNonDefaultConstructible(); }, "");
260#endif
261}
262
263// Tests that an unexpected call on a nice mock fails.
264TEST(NiceMockTest, UnexpectedCallFails) {
265 NiceMock<MockFoo> nice_foo;
266
267 EXPECT_CALL(nice_foo, DoThis()).Times(0);
268 EXPECT_NONFATAL_FAILURE(nice_foo.DoThis(), "called more times than expected");
269}
270
271// Tests that NiceMock works with a mock class that has a non-default
272// constructor.
273TEST(NiceMockTest, NonDefaultConstructor) {
274 NiceMock<MockBar> nice_bar("hi");
275 EXPECT_EQ("hi", nice_bar.str());
276
277 nice_bar.This();
278 nice_bar.That(5, true);
279}
280
281// Tests that NiceMock works with a mock class that has a 10-ary
282// non-default constructor.
283TEST(NiceMockTest, NonDefaultConstructor10) {
284 NiceMock<MockBar> nice_bar('a', 'b', "c", "d", 'e', 'f',
285 "g", "h", true, false);
286 EXPECT_EQ("abcdefghTF", nice_bar.str());
287
288 nice_bar.This();
289 nice_bar.That(5, true);
290}
291
292TEST(NiceMockTest, AllowLeak) {
294 Mock::AllowLeak(leaked);
295 EXPECT_CALL(*leaked, DoThis());
296 leaked->DoThis();
297}
298
299TEST(NiceMockTest, MoveOnlyConstructor) {
301}
302
303// Tests that NiceMock<Mock> compiles where Mock is a user-defined
304// class (as opposed to ::testing::Mock).
305TEST(NiceMockTest, AcceptsClassNamedMock) {
307 EXPECT_CALL(nice, DoThis());
308 nice.DoThis();
309}
310
311TEST(NiceMockTest, IsNiceInDestructor) {
312 {
314 // Don't add an expectation for the call before the mock goes out of scope.
315 }
316}
317
318TEST(NiceMockTest, IsNaggy_IsNice_IsStrict) {
319 NiceMock<MockFoo> nice_foo;
320 EXPECT_FALSE(Mock::IsNaggy(&nice_foo));
321 EXPECT_TRUE(Mock::IsNice(&nice_foo));
322 EXPECT_FALSE(Mock::IsStrict(&nice_foo));
323}
324
325#if GTEST_HAS_STREAM_REDIRECTION
326
327// Tests that a naggy mock generates warnings for uninteresting calls.
328TEST(NaggyMockTest, WarningForUninterestingCall) {
329 const std::string saved_flag = GMOCK_FLAG(verbose);
330 GMOCK_FLAG(verbose) = "warning";
331
332 NaggyMock<MockFoo> naggy_foo;
333
335 naggy_foo.DoThis();
336 naggy_foo.DoThat(true);
338 HasSubstr("Uninteresting mock function call"));
339
340 GMOCK_FLAG(verbose) = saved_flag;
341}
342
343// Tests that a naggy mock generates a warning for an uninteresting call
344// that deletes the mock object.
345TEST(NaggyMockTest, WarningForUninterestingCallAfterDeath) {
346 const std::string saved_flag = GMOCK_FLAG(verbose);
347 GMOCK_FLAG(verbose) = "warning";
348
349 NaggyMock<MockFoo>* const naggy_foo = new NaggyMock<MockFoo>;
350
351 ON_CALL(*naggy_foo, DoThis())
352 .WillByDefault(Invoke(naggy_foo, &MockFoo::Delete));
353
355 naggy_foo->DoThis();
357 HasSubstr("Uninteresting mock function call"));
358
359 GMOCK_FLAG(verbose) = saved_flag;
360}
361
362#endif // GTEST_HAS_STREAM_REDIRECTION
363
364// Tests that a naggy mock allows expected calls.
365TEST(NaggyMockTest, AllowsExpectedCall) {
366 NaggyMock<MockFoo> naggy_foo;
367
368 EXPECT_CALL(naggy_foo, DoThis());
369 naggy_foo.DoThis();
370}
371
372// Tests that an unexpected call on a naggy mock fails.
373TEST(NaggyMockTest, UnexpectedCallFails) {
374 NaggyMock<MockFoo> naggy_foo;
375
376 EXPECT_CALL(naggy_foo, DoThis()).Times(0);
377 EXPECT_NONFATAL_FAILURE(naggy_foo.DoThis(),
378 "called more times than expected");
379}
380
381// Tests that NaggyMock works with a mock class that has a non-default
382// constructor.
383TEST(NaggyMockTest, NonDefaultConstructor) {
384 NaggyMock<MockBar> naggy_bar("hi");
385 EXPECT_EQ("hi", naggy_bar.str());
386
387 naggy_bar.This();
388 naggy_bar.That(5, true);
389}
390
391// Tests that NaggyMock works with a mock class that has a 10-ary
392// non-default constructor.
393TEST(NaggyMockTest, NonDefaultConstructor10) {
394 NaggyMock<MockBar> naggy_bar('0', '1', "2", "3", '4', '5',
395 "6", "7", true, false);
396 EXPECT_EQ("01234567TF", naggy_bar.str());
397
398 naggy_bar.This();
399 naggy_bar.That(5, true);
400}
401
402TEST(NaggyMockTest, AllowLeak) {
404 Mock::AllowLeak(leaked);
405 EXPECT_CALL(*leaked, DoThis());
406 leaked->DoThis();
407}
408
409TEST(NaggyMockTest, MoveOnlyConstructor) {
411}
412
413// Tests that NaggyMock<Mock> compiles where Mock is a user-defined
414// class (as opposed to ::testing::Mock).
415TEST(NaggyMockTest, AcceptsClassNamedMock) {
416 NaggyMock< ::Mock> naggy;
417 EXPECT_CALL(naggy, DoThis());
418 naggy.DoThis();
419}
420
421TEST(NaggyMockTest, IsNaggyInDestructor) {
422 const std::string saved_flag = GMOCK_FLAG(verbose);
423 GMOCK_FLAG(verbose) = "warning";
425
426 {
428 // Don't add an expectation for the call before the mock goes out of scope.
429 }
430
432 HasSubstr("Uninteresting mock function call"));
433
434 GMOCK_FLAG(verbose) = saved_flag;
435}
436
437TEST(NaggyMockTest, IsNaggy_IsNice_IsStrict) {
438 NaggyMock<MockFoo> naggy_foo;
439 EXPECT_TRUE(Mock::IsNaggy(&naggy_foo));
440 EXPECT_FALSE(Mock::IsNice(&naggy_foo));
441 EXPECT_FALSE(Mock::IsStrict(&naggy_foo));
442}
443
444// Tests that a strict mock allows expected calls.
445TEST(StrictMockTest, AllowsExpectedCall) {
446 StrictMock<MockFoo> strict_foo;
447
448 EXPECT_CALL(strict_foo, DoThis());
449 strict_foo.DoThis();
450}
451
452// Tests that an unexpected call on a strict mock fails.
453TEST(StrictMockTest, UnexpectedCallFails) {
454 StrictMock<MockFoo> strict_foo;
455
456 EXPECT_CALL(strict_foo, DoThis()).Times(0);
457 EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
458 "called more times than expected");
459}
460
461// Tests that an uninteresting call on a strict mock fails.
462TEST(StrictMockTest, UninterestingCallFails) {
463 StrictMock<MockFoo> strict_foo;
464
465 EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
466 "Uninteresting mock function call");
467}
468
469// Tests that an uninteresting call on a strict mock fails, even if
470// the call deletes the mock object.
471TEST(StrictMockTest, UninterestingCallFailsAfterDeath) {
472 StrictMock<MockFoo>* const strict_foo = new StrictMock<MockFoo>;
473
474 ON_CALL(*strict_foo, DoThis())
475 .WillByDefault(Invoke(strict_foo, &MockFoo::Delete));
476
477 EXPECT_NONFATAL_FAILURE(strict_foo->DoThis(),
478 "Uninteresting mock function call");
479}
480
481// Tests that StrictMock works with a mock class that has a
482// non-default constructor.
483TEST(StrictMockTest, NonDefaultConstructor) {
484 StrictMock<MockBar> strict_bar("hi");
485 EXPECT_EQ("hi", strict_bar.str());
486
487 EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
488 "Uninteresting mock function call");
489}
490
491// Tests that StrictMock works with a mock class that has a 10-ary
492// non-default constructor.
493TEST(StrictMockTest, NonDefaultConstructor10) {
494 StrictMock<MockBar> strict_bar('a', 'b', "c", "d", 'e', 'f',
495 "g", "h", true, false);
496 EXPECT_EQ("abcdefghTF", strict_bar.str());
497
498 EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
499 "Uninteresting mock function call");
500}
501
502TEST(StrictMockTest, AllowLeak) {
504 Mock::AllowLeak(leaked);
505 EXPECT_CALL(*leaked, DoThis());
506 leaked->DoThis();
507}
508
509TEST(StrictMockTest, MoveOnlyConstructor) {
511}
512
513// Tests that StrictMock<Mock> compiles where Mock is a user-defined
514// class (as opposed to ::testing::Mock).
515TEST(StrictMockTest, AcceptsClassNamedMock) {
516 StrictMock< ::Mock> strict;
517 EXPECT_CALL(strict, DoThis());
518 strict.DoThis();
519}
520
521TEST(StrictMockTest, IsStrictInDestructor) {
523 {
525 // Don't add an expectation for the call before the mock goes out of
526 // scope.
527 },
528 "Uninteresting mock function call");
529}
530
531TEST(StrictMockTest, IsNaggy_IsNice_IsStrict) {
532 StrictMock<MockFoo> strict_foo;
533 EXPECT_FALSE(Mock::IsNaggy(&strict_foo));
534 EXPECT_FALSE(Mock::IsNice(&strict_foo));
535 EXPECT_TRUE(Mock::IsStrict(&strict_foo));
536}
537
538} // namespace gmock_nice_strict_test
539} // namespace testing
#define EXPECT_CALL(obj, call)
#define ON_CALL(obj, call)
#define GMOCK_FLAG(name)
Definition: gmock-port.h:67
#define EXPECT_THAT(value, matcher)
#define FAIL()
Definition: gtest.h:1928
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:2043
#define EXPECT_TRUE(condition)
Definition: gtest.h:1982
#define EXPECT_FALSE(condition)
Definition: gtest.h:1986
#define EXPECT_NONFATAL_FAILURE(statement, substr)
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex)
std::decay< FunctionImpl >::type Invoke(FunctionImpl &&function_impl)
GTEST_API_ void CaptureStdout()
GTEST_API_ std::string GetCapturedStdout()
TEST(NiceMockTest, AllowsExpectedCall)
MOCK_METHOD0(DoThis, void())
virtual int DoThat(bool flag)=0
MOCK_METHOD0(ReturnNonDefaultConstructible, NotDefaultConstructible())
MOCK_METHOD1(DoThat, int(bool flag))
MockBar(char a1, char a2, std::string a3, std::string a4, int a5, int a6, const std::string &a7, const std::string &a8, bool a9, bool a10)
MOCK_METHOD2(That, std::string(int, bool))
MoveOnly & operator=(MoveOnly &&)=default
MoveOnly & operator=(const MoveOnly &)=delete