Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
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//
30// Author: wan@google.com (Zhanyong Wan)
31
33
34#include <string>
35#include <utility>
36#include "gmock/gmock.h"
37#include "gtest/gtest-spi.h"
38#include "gtest/gtest.h"
39
40// This must not be defined inside the ::testing namespace, or it will
41// clash with ::testing::Mock.
42class Mock {
43 public:
44 Mock() {}
45
46 MOCK_METHOD0(DoThis, void());
47
48 private:
50};
51
52namespace testing {
53namespace gmock_nice_strict_test {
54
55using testing::GMOCK_FLAG(verbose);
60
61#if GTEST_HAS_STREAM_REDIRECTION
64#endif
65
66// Class without default constructor.
68 public:
69 explicit NotDefaultConstructible(int) {}
70};
71
72// Defines some mock classes needed by the tests.
73
74class Foo {
75 public:
76 virtual ~Foo() {}
77
78 virtual void DoThis() = 0;
79 virtual int DoThat(bool flag) = 0;
80};
81
82class MockFoo : public Foo {
83 public:
85 void Delete() { delete this; }
86
88 MOCK_METHOD1(DoThat, int(bool flag));
89 MOCK_METHOD0(ReturnNonDefaultConstructible, NotDefaultConstructible());
90
91 private:
93};
94
95class MockBar {
96 public:
97 explicit MockBar(const std::string& s) : str_(s) {}
98
99 MockBar(char a1, char a2, std::string a3, std::string a4, int a5, int a6,
100 const std::string& a7, const std::string& a8, bool a9, bool a10) {
101 str_ = std::string() + a1 + a2 + a3 + a4 + static_cast<char>(a5) +
102 static_cast<char>(a6) + a7 + a8 + (a9 ? 'T' : 'F') + (a10 ? 'T' : 'F');
103 }
104
105 virtual ~MockBar() {}
106
107 const std::string& str() const { return str_; }
108
109 MOCK_METHOD0(This, int());
110 MOCK_METHOD2(That, std::string(int, bool));
111
112 private:
113 std::string str_;
114
116};
117
118#if GTEST_GTEST_LANG_CXX11
119
120class MockBaz {
121 public:
122 class MoveOnly {
123 MoveOnly() = default;
124
125 MoveOnly(const MoveOnly&) = delete;
126 operator=(const MoveOnly&) = delete;
127
128 MoveOnly(MoveOnly&&) = default;
129 operator=(MoveOnly&&) = default;
130 };
131
132 MockBaz(MoveOnly) {}
133}
134#endif // GTEST_GTEST_LANG_CXX11 && GTEST_HAS_STD_MOVE_
135
136#if GTEST_HAS_STREAM_REDIRECTION
137
138// Tests that a raw mock generates warnings for uninteresting calls.
139TEST(RawMockTest, WarningForUninterestingCall) {
140 const std::string saved_flag = GMOCK_FLAG(verbose);
141 GMOCK_FLAG(verbose) = "warning";
142
143 MockFoo raw_foo;
144
146 raw_foo.DoThis();
147 raw_foo.DoThat(true);
149 HasSubstr("Uninteresting mock function call"));
150
151 GMOCK_FLAG(verbose) = saved_flag;
152}
153
154// Tests that a raw mock generates warnings for uninteresting calls
155// that delete the mock object.
156TEST(RawMockTest, WarningForUninterestingCallAfterDeath) {
157 const std::string saved_flag = GMOCK_FLAG(verbose);
158 GMOCK_FLAG(verbose) = "warning";
159
160 MockFoo* const raw_foo = new MockFoo;
161
162 ON_CALL(*raw_foo, DoThis())
163 .WillByDefault(Invoke(raw_foo, &MockFoo::Delete));
164
166 raw_foo->DoThis();
168 HasSubstr("Uninteresting mock function call"));
169
170 GMOCK_FLAG(verbose) = saved_flag;
171}
172
173// Tests that a raw mock generates informational logs for
174// uninteresting calls.
175TEST(RawMockTest, InfoForUninterestingCall) {
176 MockFoo raw_foo;
177
178 const std::string saved_flag = GMOCK_FLAG(verbose);
179 GMOCK_FLAG(verbose) = "info";
181 raw_foo.DoThis();
183 HasSubstr("Uninteresting mock function call"));
184
185 GMOCK_FLAG(verbose) = saved_flag;
186}
187
188// Tests that a nice mock generates no warning for uninteresting calls.
189TEST(NiceMockTest, NoWarningForUninterestingCall) {
190 NiceMock<MockFoo> nice_foo;
191
193 nice_foo.DoThis();
194 nice_foo.DoThat(true);
196}
197
198// Tests that a nice mock generates no warning for uninteresting calls
199// that delete the mock object.
200TEST(NiceMockTest, NoWarningForUninterestingCallAfterDeath) {
201 NiceMock<MockFoo>* const nice_foo = new NiceMock<MockFoo>;
202
203 ON_CALL(*nice_foo, DoThis())
204 .WillByDefault(Invoke(nice_foo, &MockFoo::Delete));
205
207 nice_foo->DoThis();
209}
210
211// Tests that a nice mock generates informational logs for
212// uninteresting calls.
213TEST(NiceMockTest, InfoForUninterestingCall) {
214 NiceMock<MockFoo> nice_foo;
215
216 const std::string saved_flag = GMOCK_FLAG(verbose);
217 GMOCK_FLAG(verbose) = "info";
219 nice_foo.DoThis();
221 HasSubstr("Uninteresting mock function call"));
222
223 GMOCK_FLAG(verbose) = saved_flag;
224}
225
226#endif // GTEST_HAS_STREAM_REDIRECTION
227
228// Tests that a nice mock allows expected calls.
229TEST(NiceMockTest, AllowsExpectedCall) {
230 NiceMock<MockFoo> nice_foo;
231
232 EXPECT_CALL(nice_foo, DoThis());
233 nice_foo.DoThis();
234}
235
236// Tests that an unexpected call on a nice mock which returns a
237// not-default-constructible type throws an exception and the exception contains
238// the method's name.
239TEST(NiceMockTest, ThrowsExceptionForUnknownReturnTypes) {
240 NiceMock<MockFoo> nice_foo;
241#if GTEST_HAS_EXCEPTIONS
242 try {
243 nice_foo.ReturnNonDefaultConstructible();
244 FAIL();
245 } catch (const std::runtime_error& ex) {
246 EXPECT_THAT(ex.what(), HasSubstr("ReturnNonDefaultConstructible"));
247 }
248#else
249 EXPECT_DEATH_IF_SUPPORTED({ nice_foo.ReturnNonDefaultConstructible(); }, "");
250#endif
251}
252
253// Tests that an unexpected call on a nice mock fails.
254TEST(NiceMockTest, UnexpectedCallFails) {
255 NiceMock<MockFoo> nice_foo;
256
257 EXPECT_CALL(nice_foo, DoThis()).Times(0);
258 EXPECT_NONFATAL_FAILURE(nice_foo.DoThis(), "called more times than expected");
259}
260
261// Tests that NiceMock works with a mock class that has a non-default
262// constructor.
263TEST(NiceMockTest, NonDefaultConstructor) {
264 NiceMock<MockBar> nice_bar("hi");
265 EXPECT_EQ("hi", nice_bar.str());
266
267 nice_bar.This();
268 nice_bar.That(5, true);
269}
270
271// Tests that NiceMock works with a mock class that has a 10-ary
272// non-default constructor.
273TEST(NiceMockTest, NonDefaultConstructor10) {
274 NiceMock<MockBar> nice_bar('a', 'b', "c", "d", 'e', 'f',
275 "g", "h", true, false);
276 EXPECT_EQ("abcdefghTF", nice_bar.str());
277
278 nice_bar.This();
279 nice_bar.That(5, true);
280}
281
282TEST(NiceMockTest, AllowLeak) {
284 Mock::AllowLeak(leaked);
285 EXPECT_CALL(*leaked, DoThis());
286 leaked->DoThis();
287}
288
289#if GTEST_GTEST_LANG_CXX11 && GTEST_HAS_STD_MOVE_
290
291TEST(NiceMockTest, MoveOnlyConstructor) {
292 NiceMock<MockBaz> nice_baz(MockBaz::MoveOnly());
293}
294
295#endif // GTEST_LANG_CXX11 && GTEST_HAS_STD_MOVE_
296
297#if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
298// Tests that NiceMock<Mock> compiles where Mock is a user-defined
299// class (as opposed to ::testing::Mock). We had to work around an
300// MSVC 8.0 bug that caused the symbol Mock used in the definition of
301// NiceMock to be looked up in the wrong context, and this test
302// ensures that our fix works.
303//
304// We have to skip this test on Symbian and Windows Mobile, as it
305// causes the program to crash there, for reasons unclear to us yet.
306TEST(NiceMockTest, AcceptsClassNamedMock) {
308 EXPECT_CALL(nice, DoThis());
309 nice.DoThis();
310}
311#endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
312
313#if GTEST_HAS_STREAM_REDIRECTION
314
315// Tests that a naggy mock generates warnings for uninteresting calls.
316TEST(NaggyMockTest, WarningForUninterestingCall) {
317 const std::string saved_flag = GMOCK_FLAG(verbose);
318 GMOCK_FLAG(verbose) = "warning";
319
320 NaggyMock<MockFoo> naggy_foo;
321
322 CaptureStdout();
323 naggy_foo.DoThis();
324 naggy_foo.DoThat(true);
325 EXPECT_THAT(GetCapturedStdout(),
326 HasSubstr("Uninteresting mock function call"));
327
328 GMOCK_FLAG(verbose) = saved_flag;
329}
330
331// Tests that a naggy mock generates a warning for an uninteresting call
332// that deletes the mock object.
333TEST(NaggyMockTest, WarningForUninterestingCallAfterDeath) {
334 const std::string saved_flag = GMOCK_FLAG(verbose);
335 GMOCK_FLAG(verbose) = "warning";
336
337 NaggyMock<MockFoo>* const naggy_foo = new NaggyMock<MockFoo>;
338
339 ON_CALL(*naggy_foo, DoThis())
340 .WillByDefault(Invoke(naggy_foo, &MockFoo::Delete));
341
342 CaptureStdout();
343 naggy_foo->DoThis();
344 EXPECT_THAT(GetCapturedStdout(),
345 HasSubstr("Uninteresting mock function call"));
346
347 GMOCK_FLAG(verbose) = saved_flag;
348}
349
350#endif // GTEST_HAS_STREAM_REDIRECTION
351
352// Tests that a naggy mock allows expected calls.
353TEST(NaggyMockTest, AllowsExpectedCall) {
354 NaggyMock<MockFoo> naggy_foo;
355
356 EXPECT_CALL(naggy_foo, DoThis());
357 naggy_foo.DoThis();
358}
359
360// Tests that an unexpected call on a naggy mock fails.
361TEST(NaggyMockTest, UnexpectedCallFails) {
362 NaggyMock<MockFoo> naggy_foo;
363
364 EXPECT_CALL(naggy_foo, DoThis()).Times(0);
365 EXPECT_NONFATAL_FAILURE(naggy_foo.DoThis(),
366 "called more times than expected");
367}
368
369// Tests that NaggyMock works with a mock class that has a non-default
370// constructor.
371TEST(NaggyMockTest, NonDefaultConstructor) {
372 NaggyMock<MockBar> naggy_bar("hi");
373 EXPECT_EQ("hi", naggy_bar.str());
374
375 naggy_bar.This();
376 naggy_bar.That(5, true);
377}
378
379// Tests that NaggyMock works with a mock class that has a 10-ary
380// non-default constructor.
381TEST(NaggyMockTest, NonDefaultConstructor10) {
382 NaggyMock<MockBar> naggy_bar('0', '1', "2", "3", '4', '5',
383 "6", "7", true, false);
384 EXPECT_EQ("01234567TF", naggy_bar.str());
385
386 naggy_bar.This();
387 naggy_bar.That(5, true);
388}
389
390TEST(NaggyMockTest, AllowLeak) {
392 Mock::AllowLeak(leaked);
393 EXPECT_CALL(*leaked, DoThis());
394 leaked->DoThis();
395}
396
397#if GTEST_GTEST_LANG_CXX11 && GTEST_HAS_STD_MOVE_
398
399TEST(NaggyMockTest, MoveOnlyConstructor) {
400 NaggyMock<MockBaz> naggy_baz(MockBaz::MoveOnly());
401}
402
403#endif // GTEST_LANG_CXX11 && GTEST_HAS_STD_MOVE_
404
405#if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
406// Tests that NaggyMock<Mock> compiles where Mock is a user-defined
407// class (as opposed to ::testing::Mock). We had to work around an
408// MSVC 8.0 bug that caused the symbol Mock used in the definition of
409// NaggyMock to be looked up in the wrong context, and this test
410// ensures that our fix works.
411//
412// We have to skip this test on Symbian and Windows Mobile, as it
413// causes the program to crash there, for reasons unclear to us yet.
414TEST(NaggyMockTest, AcceptsClassNamedMock) {
415 NaggyMock< ::Mock> naggy;
416 EXPECT_CALL(naggy, DoThis());
417 naggy.DoThis();
418}
419#endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
420
421// Tests that a strict mock allows expected calls.
422TEST(StrictMockTest, AllowsExpectedCall) {
423 StrictMock<MockFoo> strict_foo;
424
425 EXPECT_CALL(strict_foo, DoThis());
426 strict_foo.DoThis();
427}
428
429// Tests that an unexpected call on a strict mock fails.
430TEST(StrictMockTest, UnexpectedCallFails) {
431 StrictMock<MockFoo> strict_foo;
432
433 EXPECT_CALL(strict_foo, DoThis()).Times(0);
434 EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
435 "called more times than expected");
436}
437
438// Tests that an uninteresting call on a strict mock fails.
439TEST(StrictMockTest, UninterestingCallFails) {
440 StrictMock<MockFoo> strict_foo;
441
442 EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
443 "Uninteresting mock function call");
444}
445
446// Tests that an uninteresting call on a strict mock fails, even if
447// the call deletes the mock object.
448TEST(StrictMockTest, UninterestingCallFailsAfterDeath) {
449 StrictMock<MockFoo>* const strict_foo = new StrictMock<MockFoo>;
450
451 ON_CALL(*strict_foo, DoThis())
452 .WillByDefault(Invoke(strict_foo, &MockFoo::Delete));
453
454 EXPECT_NONFATAL_FAILURE(strict_foo->DoThis(),
455 "Uninteresting mock function call");
456}
457
458// Tests that StrictMock works with a mock class that has a
459// non-default constructor.
460TEST(StrictMockTest, NonDefaultConstructor) {
461 StrictMock<MockBar> strict_bar("hi");
462 EXPECT_EQ("hi", strict_bar.str());
463
464 EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
465 "Uninteresting mock function call");
466}
467
468// Tests that StrictMock works with a mock class that has a 10-ary
469// non-default constructor.
470TEST(StrictMockTest, NonDefaultConstructor10) {
471 StrictMock<MockBar> strict_bar('a', 'b', "c", "d", 'e', 'f',
472 "g", "h", true, false);
473 EXPECT_EQ("abcdefghTF", strict_bar.str());
474
475 EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
476 "Uninteresting mock function call");
477}
478
479TEST(StrictMockTest, AllowLeak) {
481 Mock::AllowLeak(leaked);
482 EXPECT_CALL(*leaked, DoThis());
483 leaked->DoThis();
484}
485
486#if GTEST_GTEST_LANG_CXX11 && GTEST_HAS_STD_MOVE_
487
488TEST(StrictMockTest, MoveOnlyConstructor) {
489 StrictMock<MockBaz> strict_baz(MockBaz::MoveOnly());
490}
491
492#endif // GTEST_LANG_CXX11 && GTEST_HAS_STD_MOVE_
493
494#if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
495// Tests that StrictMock<Mock> compiles where Mock is a user-defined
496// class (as opposed to ::testing::Mock). We had to work around an
497// MSVC 8.0 bug that caused the symbol Mock used in the definition of
498// StrictMock to be looked up in the wrong context, and this test
499// ensures that our fix works.
500//
501// We have to skip this test on Symbian and Windows Mobile, as it
502// causes the program to crash there, for reasons unclear to us yet.
503TEST(StrictMockTest, AcceptsClassNamedMock) {
504 StrictMock< ::Mock> strict;
505 EXPECT_CALL(strict, DoThis());
506 strict.DoThis();
507}
508#endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
509
510} // namespace gmock_nice_strict_test
511} // namespace testing
MOCK_METHOD0(DoThis, void())
virtual int DoThat(bool flag)=0
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))
MOCK_METHOD0(ReturnNonDefaultConstructible, NotDefaultConstructible())
MOCK_METHOD1(DoThat, int(bool flag))
#define EXPECT_THAT(value, matcher)
#define GMOCK_FLAG(name)
Definition gmock-port.h:66
#define EXPECT_CALL(obj, call)
#define ON_CALL(obj, call)
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex)
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
Definition gtest-port.h:917
#define EXPECT_NONFATAL_FAILURE(statement, substr)
Definition gtest-spi.h:203
#define FAIL()
Definition gtest.h:1858
#define EXPECT_EQ(val1, val2)
Definition gtest.h:1954
#define TEST(test_case_name, test_name)
Definition gtest.h:2275
GTEST_API_ void CaptureStdout()
GTEST_API_ std::string GetCapturedStdout()
PolymorphicMatcher< internal::HasSubstrMatcher< std::string > > HasSubstr(const std::string &substring)
PolymorphicAction< internal::InvokeAction< FunctionImpl > > Invoke(FunctionImpl function_impl)
bool verbose
Definition main.cpp:190
char * s