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 "gmock/gmock.h"
36 #include "gtest/gtest.h"
37 #include "gtest/gtest-spi.h"
38 
39 // This must not be defined inside the ::testing namespace, or it will
40 // clash with ::testing::Mock.
41 class Mock {
42  public:
43  Mock() {}
44 
45  MOCK_METHOD0(DoThis, void());
46 
47  private:
49 };
50 
51 namespace testing {
52 namespace gmock_nice_strict_test {
53 
55 using testing::GMOCK_FLAG(verbose);
56 using testing::HasSubstr;
57 using testing::NaggyMock;
58 using testing::NiceMock;
60 
61 #if GTEST_HAS_STREAM_REDIRECTION
64 #endif
65 
66 // Defines some mock classes needed by the tests.
67 
68 class Foo {
69  public:
70  virtual ~Foo() {}
71 
72  virtual void DoThis() = 0;
73  virtual int DoThat(bool flag) = 0;
74 };
75 
76 class MockFoo : public Foo {
77  public:
78  MockFoo() {}
79  void Delete() { delete this; }
80 
81  MOCK_METHOD0(DoThis, void());
82  MOCK_METHOD1(DoThat, int(bool flag));
83 
84  private:
86 };
87 
88 class MockBar {
89  public:
90  explicit MockBar(const string& s) : str_(s) {}
91 
92  MockBar(char a1, char a2, string a3, string a4, int a5, int a6,
93  const string& a7, const string& a8, bool a9, bool a10) {
94  str_ = string() + a1 + a2 + a3 + a4 + static_cast<char>(a5) +
95  static_cast<char>(a6) + a7 + a8 + (a9 ? 'T' : 'F') + (a10 ? 'T' : 'F');
96  }
97 
98  virtual ~MockBar() {}
99 
100  const string& str() const { return str_; }
101 
102  MOCK_METHOD0(This, int());
103  MOCK_METHOD2(That, string(int, bool));
104 
105  private:
106  string str_;
107 
109 };
110 
111 #if GTEST_HAS_STREAM_REDIRECTION
112 
113 // Tests that a raw mock generates warnings for uninteresting calls.
114 TEST(RawMockTest, WarningForUninterestingCall) {
115  const string saved_flag = GMOCK_FLAG(verbose);
116  GMOCK_FLAG(verbose) = "warning";
117 
118  MockFoo raw_foo;
119 
120  CaptureStdout();
121  raw_foo.DoThis();
122  raw_foo.DoThat(true);
124  HasSubstr("Uninteresting mock function call"));
125 
126  GMOCK_FLAG(verbose) = saved_flag;
127 }
128 
129 // Tests that a raw mock generates warnings for uninteresting calls
130 // that delete the mock object.
131 TEST(RawMockTest, WarningForUninterestingCallAfterDeath) {
132  const string saved_flag = GMOCK_FLAG(verbose);
133  GMOCK_FLAG(verbose) = "warning";
134 
135  MockFoo* const raw_foo = new MockFoo;
136 
137  ON_CALL(*raw_foo, DoThis())
138  .WillByDefault(Invoke(raw_foo, &MockFoo::Delete));
139 
140  CaptureStdout();
141  raw_foo->DoThis();
143  HasSubstr("Uninteresting mock function call"));
144 
145  GMOCK_FLAG(verbose) = saved_flag;
146 }
147 
148 // Tests that a raw mock generates informational logs for
149 // uninteresting calls.
150 TEST(RawMockTest, InfoForUninterestingCall) {
151  MockFoo raw_foo;
152 
153  const string saved_flag = GMOCK_FLAG(verbose);
154  GMOCK_FLAG(verbose) = "info";
155  CaptureStdout();
156  raw_foo.DoThis();
158  HasSubstr("Uninteresting mock function call"));
159 
160  GMOCK_FLAG(verbose) = saved_flag;
161 }
162 
163 // Tests that a nice mock generates no warning for uninteresting calls.
164 TEST(NiceMockTest, NoWarningForUninterestingCall) {
165  NiceMock<MockFoo> nice_foo;
166 
167  CaptureStdout();
168  nice_foo.DoThis();
169  nice_foo.DoThat(true);
171 }
172 
173 // Tests that a nice mock generates no warning for uninteresting calls
174 // that delete the mock object.
175 TEST(NiceMockTest, NoWarningForUninterestingCallAfterDeath) {
176  NiceMock<MockFoo>* const nice_foo = new NiceMock<MockFoo>;
177 
178  ON_CALL(*nice_foo, DoThis())
179  .WillByDefault(Invoke(nice_foo, &MockFoo::Delete));
180 
181  CaptureStdout();
182  nice_foo->DoThis();
184 }
185 
186 // Tests that a nice mock generates informational logs for
187 // uninteresting calls.
188 TEST(NiceMockTest, InfoForUninterestingCall) {
189  NiceMock<MockFoo> nice_foo;
190 
191  const string saved_flag = GMOCK_FLAG(verbose);
192  GMOCK_FLAG(verbose) = "info";
193  CaptureStdout();
194  nice_foo.DoThis();
196  HasSubstr("Uninteresting mock function call"));
197 
198  GMOCK_FLAG(verbose) = saved_flag;
199 }
200 
201 #endif // GTEST_HAS_STREAM_REDIRECTION
202 
203 // Tests that a nice mock allows expected calls.
204 TEST(NiceMockTest, AllowsExpectedCall) {
205  NiceMock<MockFoo> nice_foo;
206 
207  EXPECT_CALL(nice_foo, DoThis());
208  nice_foo.DoThis();
209 }
210 
211 // Tests that an unexpected call on a nice mock fails.
212 TEST(NiceMockTest, UnexpectedCallFails) {
213  NiceMock<MockFoo> nice_foo;
214 
215  EXPECT_CALL(nice_foo, DoThis()).Times(0);
216  EXPECT_NONFATAL_FAILURE(nice_foo.DoThis(), "called more times than expected");
217 }
218 
219 // Tests that NiceMock works with a mock class that has a non-default
220 // constructor.
221 TEST(NiceMockTest, NonDefaultConstructor) {
222  NiceMock<MockBar> nice_bar("hi");
223  EXPECT_EQ("hi", nice_bar.str());
224 
225  nice_bar.This();
226  nice_bar.That(5, true);
227 }
228 
229 // Tests that NiceMock works with a mock class that has a 10-ary
230 // non-default constructor.
231 TEST(NiceMockTest, NonDefaultConstructor10) {
232  NiceMock<MockBar> nice_bar('a', 'b', "c", "d", 'e', 'f',
233  "g", "h", true, false);
234  EXPECT_EQ("abcdefghTF", nice_bar.str());
235 
236  nice_bar.This();
237  nice_bar.That(5, true);
238 }
239 
240 #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
241 // Tests that NiceMock<Mock> compiles where Mock is a user-defined
242 // class (as opposed to ::testing::Mock). We had to work around an
243 // MSVC 8.0 bug that caused the symbol Mock used in the definition of
244 // NiceMock to be looked up in the wrong context, and this test
245 // ensures that our fix works.
246 //
247 // We have to skip this test on Symbian and Windows Mobile, as it
248 // causes the program to crash there, for reasons unclear to us yet.
249 TEST(NiceMockTest, AcceptsClassNamedMock) {
250  NiceMock< ::Mock> nice;
251  EXPECT_CALL(nice, DoThis());
252  nice.DoThis();
253 }
254 #endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
255 
256 #if GTEST_HAS_STREAM_REDIRECTION
257 
258 // Tests that a naggy mock generates warnings for uninteresting calls.
259 TEST(NaggyMockTest, WarningForUninterestingCall) {
260  const string saved_flag = GMOCK_FLAG(verbose);
261  GMOCK_FLAG(verbose) = "warning";
262 
263  NaggyMock<MockFoo> naggy_foo;
264 
265  CaptureStdout();
266  naggy_foo.DoThis();
267  naggy_foo.DoThat(true);
269  HasSubstr("Uninteresting mock function call"));
270 
271  GMOCK_FLAG(verbose) = saved_flag;
272 }
273 
274 // Tests that a naggy mock generates a warning for an uninteresting call
275 // that deletes the mock object.
276 TEST(NaggyMockTest, WarningForUninterestingCallAfterDeath) {
277  const string saved_flag = GMOCK_FLAG(verbose);
278  GMOCK_FLAG(verbose) = "warning";
279 
280  NaggyMock<MockFoo>* const naggy_foo = new NaggyMock<MockFoo>;
281 
282  ON_CALL(*naggy_foo, DoThis())
283  .WillByDefault(Invoke(naggy_foo, &MockFoo::Delete));
284 
285  CaptureStdout();
286  naggy_foo->DoThis();
288  HasSubstr("Uninteresting mock function call"));
289 
290  GMOCK_FLAG(verbose) = saved_flag;
291 }
292 
293 #endif // GTEST_HAS_STREAM_REDIRECTION
294 
295 // Tests that a naggy mock allows expected calls.
296 TEST(NaggyMockTest, AllowsExpectedCall) {
297  NaggyMock<MockFoo> naggy_foo;
298 
299  EXPECT_CALL(naggy_foo, DoThis());
300  naggy_foo.DoThis();
301 }
302 
303 // Tests that an unexpected call on a naggy mock fails.
304 TEST(NaggyMockTest, UnexpectedCallFails) {
305  NaggyMock<MockFoo> naggy_foo;
306 
307  EXPECT_CALL(naggy_foo, DoThis()).Times(0);
308  EXPECT_NONFATAL_FAILURE(naggy_foo.DoThis(),
309  "called more times than expected");
310 }
311 
312 // Tests that NaggyMock works with a mock class that has a non-default
313 // constructor.
314 TEST(NaggyMockTest, NonDefaultConstructor) {
315  NaggyMock<MockBar> naggy_bar("hi");
316  EXPECT_EQ("hi", naggy_bar.str());
317 
318  naggy_bar.This();
319  naggy_bar.That(5, true);
320 }
321 
322 // Tests that NaggyMock works with a mock class that has a 10-ary
323 // non-default constructor.
324 TEST(NaggyMockTest, NonDefaultConstructor10) {
325  NaggyMock<MockBar> naggy_bar('0', '1', "2", "3", '4', '5',
326  "6", "7", true, false);
327  EXPECT_EQ("01234567TF", naggy_bar.str());
328 
329  naggy_bar.This();
330  naggy_bar.That(5, true);
331 }
332 
333 #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
334 // Tests that NaggyMock<Mock> compiles where Mock is a user-defined
335 // class (as opposed to ::testing::Mock). We had to work around an
336 // MSVC 8.0 bug that caused the symbol Mock used in the definition of
337 // NaggyMock to be looked up in the wrong context, and this test
338 // ensures that our fix works.
339 //
340 // We have to skip this test on Symbian and Windows Mobile, as it
341 // causes the program to crash there, for reasons unclear to us yet.
342 TEST(NaggyMockTest, AcceptsClassNamedMock) {
343  NaggyMock< ::Mock> naggy;
344  EXPECT_CALL(naggy, DoThis());
345  naggy.DoThis();
346 }
347 #endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
348 
349 // Tests that a strict mock allows expected calls.
350 TEST(StrictMockTest, AllowsExpectedCall) {
351  StrictMock<MockFoo> strict_foo;
352 
353  EXPECT_CALL(strict_foo, DoThis());
354  strict_foo.DoThis();
355 }
356 
357 // Tests that an unexpected call on a strict mock fails.
358 TEST(StrictMockTest, UnexpectedCallFails) {
359  StrictMock<MockFoo> strict_foo;
360 
361  EXPECT_CALL(strict_foo, DoThis()).Times(0);
362  EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
363  "called more times than expected");
364 }
365 
366 // Tests that an uninteresting call on a strict mock fails.
367 TEST(StrictMockTest, UninterestingCallFails) {
368  StrictMock<MockFoo> strict_foo;
369 
370  EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
371  "Uninteresting mock function call");
372 }
373 
374 // Tests that an uninteresting call on a strict mock fails, even if
375 // the call deletes the mock object.
376 TEST(StrictMockTest, UninterestingCallFailsAfterDeath) {
377  StrictMock<MockFoo>* const strict_foo = new StrictMock<MockFoo>;
378 
379  ON_CALL(*strict_foo, DoThis())
380  .WillByDefault(Invoke(strict_foo, &MockFoo::Delete));
381 
382  EXPECT_NONFATAL_FAILURE(strict_foo->DoThis(),
383  "Uninteresting mock function call");
384 }
385 
386 // Tests that StrictMock works with a mock class that has a
387 // non-default constructor.
388 TEST(StrictMockTest, NonDefaultConstructor) {
389  StrictMock<MockBar> strict_bar("hi");
390  EXPECT_EQ("hi", strict_bar.str());
391 
392  EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
393  "Uninteresting mock function call");
394 }
395 
396 // Tests that StrictMock works with a mock class that has a 10-ary
397 // non-default constructor.
398 TEST(StrictMockTest, NonDefaultConstructor10) {
399  StrictMock<MockBar> strict_bar('a', 'b', "c", "d", 'e', 'f',
400  "g", "h", true, false);
401  EXPECT_EQ("abcdefghTF", strict_bar.str());
402 
403  EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
404  "Uninteresting mock function call");
405 }
406 
407 #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
408 // Tests that StrictMock<Mock> compiles where Mock is a user-defined
409 // class (as opposed to ::testing::Mock). We had to work around an
410 // MSVC 8.0 bug that caused the symbol Mock used in the definition of
411 // StrictMock to be looked up in the wrong context, and this test
412 // ensures that our fix works.
413 //
414 // We have to skip this test on Symbian and Windows Mobile, as it
415 // causes the program to crash there, for reasons unclear to us yet.
416 TEST(StrictMockTest, AcceptsClassNamedMock) {
417  StrictMock< ::Mock> strict;
418  EXPECT_CALL(strict, DoThis());
419  strict.DoThis();
420 }
421 #endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
422 
423 } // namespace gmock_nice_strict_test
424 } // namespace testing
std::string GetCapturedStdout()
#define EXPECT_NONFATAL_FAILURE(statement, substr)
MOCK_METHOD0(DoThis, void())
#define EXPECT_THAT(value, matcher)
GTEST_DISALLOW_COPY_AND_ASSIGN_(Mock)
#define ON_CALL(obj, call)
MOCK_METHOD1(VoidFromString, void(char *str))
MockBar(char a1, char a2, string a3, string a4, int a5, int a6, const string &a7, const string &a8, bool a9, bool a10)
#define MOCK_METHOD2(m,...)
PolymorphicMatcher< internal::HasSubstrMatcher< internal::string > > HasSubstr(const internal::string &substring)
PolymorphicAction< internal::InvokeAction< FunctionImpl > > Invoke(FunctionImpl function_impl)
TEST(StrictMockTest, AcceptsClassNamedMock)
#define GMOCK_FLAG(name)
#define EXPECT_CALL(obj, call)
#define EXPECT_EQ(expected, actual)
static void Delete(T *x)
virtual int DoThat(bool flag)=0


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:06:07