gmock-nice-strict_test.cc 13.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Copyright 2008, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Author: wan@google.com (Zhanyong Wan)

32
#include "gmock/gmock-generated-nice-strict.h"
33
34

#include <string>
35
36
37
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "gtest/gtest-spi.h"
38

39
40
41
42
// This must not be defined inside the ::testing namespace, or it will
// clash with ::testing::Mock.
class Mock {
 public:
43
44
  Mock() {}

45
  MOCK_METHOD0(DoThis, void());
46
47
48

 private:
  GTEST_DISALLOW_COPY_AND_ASSIGN_(Mock);
49
50
};

51
52
53
54
55
56
namespace testing {
namespace gmock_nice_strict_test {

using testing::internal::string;
using testing::GMOCK_FLAG(verbose);
using testing::HasSubstr;
zhanyong.wan's avatar
zhanyong.wan committed
57
using testing::NaggyMock;
58
59
60
using testing::NiceMock;
using testing::StrictMock;

61
#if GTEST_HAS_STREAM_REDIRECTION
62
63
using testing::internal::CaptureStdout;
using testing::internal::GetCapturedStdout;
64
#endif
65

66
67
68
69
70
71
72
73
74
75
76
77
// Defines some mock classes needed by the tests.

class Foo {
 public:
  virtual ~Foo() {}

  virtual void DoThis() = 0;
  virtual int DoThat(bool flag) = 0;
};

class MockFoo : public Foo {
 public:
78
  MockFoo() {}
79
80
81
82
  void Delete() { delete this; }

  MOCK_METHOD0(DoThis, void());
  MOCK_METHOD1(DoThat, int(bool flag));
83
84
85

 private:
  GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo);
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
};

class MockBar {
 public:
  explicit MockBar(const string& s) : str_(s) {}

  MockBar(char a1, char a2, string a3, string a4, int a5, int a6,
          const string& a7, const string& a8, bool a9, bool a10) {
    str_ = string() + a1 + a2 + a3 + a4 + static_cast<char>(a5) +
        static_cast<char>(a6) + a7 + a8 + (a9 ? 'T' : 'F') + (a10 ? 'T' : 'F');
  }

  virtual ~MockBar() {}

  const string& str() const { return str_; }

  MOCK_METHOD0(This, int());
  MOCK_METHOD2(That, string(int, bool));

 private:
  string str_;
107
108

  GTEST_DISALLOW_COPY_AND_ASSIGN_(MockBar);
109
110
};

111
#if GTEST_HAS_STREAM_REDIRECTION
112

113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Tests that a raw mock generates warnings for uninteresting calls.
TEST(RawMockTest, WarningForUninterestingCall) {
  const string saved_flag = GMOCK_FLAG(verbose);
  GMOCK_FLAG(verbose) = "warning";

  MockFoo raw_foo;

  CaptureStdout();
  raw_foo.DoThis();
  raw_foo.DoThat(true);
  EXPECT_THAT(GetCapturedStdout(),
              HasSubstr("Uninteresting mock function call"));

  GMOCK_FLAG(verbose) = saved_flag;
}

// Tests that a raw mock generates warnings for uninteresting calls
// that delete the mock object.
TEST(RawMockTest, WarningForUninterestingCallAfterDeath) {
  const string saved_flag = GMOCK_FLAG(verbose);
  GMOCK_FLAG(verbose) = "warning";

  MockFoo* const raw_foo = new MockFoo;

  ON_CALL(*raw_foo, DoThis())
      .WillByDefault(Invoke(raw_foo, &MockFoo::Delete));

  CaptureStdout();
  raw_foo->DoThis();
  EXPECT_THAT(GetCapturedStdout(),
              HasSubstr("Uninteresting mock function call"));

  GMOCK_FLAG(verbose) = saved_flag;
}

// Tests that a raw mock generates informational logs for
// uninteresting calls.
TEST(RawMockTest, InfoForUninterestingCall) {
  MockFoo raw_foo;

  const string saved_flag = GMOCK_FLAG(verbose);
  GMOCK_FLAG(verbose) = "info";
  CaptureStdout();
  raw_foo.DoThis();
  EXPECT_THAT(GetCapturedStdout(),
              HasSubstr("Uninteresting mock function call"));

  GMOCK_FLAG(verbose) = saved_flag;
}

163
164
165
166
167
168
169
170
171
TEST(RawMockTest, IsNaggy_IsNice_IsStrict) {
  using internal::CallReaction;
  MockFoo raw_foo;
  ASSERT_EQ(CallReaction::kDefault, CallReaction::kWarn) << "precondition";
  EXPECT_TRUE (Mock::IsNaggy(&raw_foo));
  EXPECT_FALSE(Mock::IsNice(&raw_foo));
  EXPECT_FALSE(Mock::IsStrict(&raw_foo));
}

172
173
174
175
// Tests that a nice mock generates no warning for uninteresting calls.
TEST(NiceMockTest, NoWarningForUninterestingCall) {
  NiceMock<MockFoo> nice_foo;

176
  CaptureStdout();
177
178
  nice_foo.DoThis();
  nice_foo.DoThat(true);
zhanyong.wan's avatar
zhanyong.wan committed
179
  EXPECT_EQ("", GetCapturedStdout());
180
181
182
183
184
185
186
187
188
189
}

// Tests that a nice mock generates no warning for uninteresting calls
// that delete the mock object.
TEST(NiceMockTest, NoWarningForUninterestingCallAfterDeath) {
  NiceMock<MockFoo>* const nice_foo = new NiceMock<MockFoo>;

  ON_CALL(*nice_foo, DoThis())
      .WillByDefault(Invoke(nice_foo, &MockFoo::Delete));

190
  CaptureStdout();
191
  nice_foo->DoThis();
zhanyong.wan's avatar
zhanyong.wan committed
192
  EXPECT_EQ("", GetCapturedStdout());
193
194
195
196
197
198
199
}

// Tests that a nice mock generates informational logs for
// uninteresting calls.
TEST(NiceMockTest, InfoForUninterestingCall) {
  NiceMock<MockFoo> nice_foo;

200
  const string saved_flag = GMOCK_FLAG(verbose);
201
  GMOCK_FLAG(verbose) = "info";
202
  CaptureStdout();
203
  nice_foo.DoThis();
zhanyong.wan's avatar
zhanyong.wan committed
204
  EXPECT_THAT(GetCapturedStdout(),
205
206
              HasSubstr("Uninteresting mock function call"));

207
  GMOCK_FLAG(verbose) = saved_flag;
208
209
}

210
#endif  // GTEST_HAS_STREAM_REDIRECTION
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248

// Tests that a nice mock allows expected calls.
TEST(NiceMockTest, AllowsExpectedCall) {
  NiceMock<MockFoo> nice_foo;

  EXPECT_CALL(nice_foo, DoThis());
  nice_foo.DoThis();
}

// Tests that an unexpected call on a nice mock fails.
TEST(NiceMockTest, UnexpectedCallFails) {
  NiceMock<MockFoo> nice_foo;

  EXPECT_CALL(nice_foo, DoThis()).Times(0);
  EXPECT_NONFATAL_FAILURE(nice_foo.DoThis(), "called more times than expected");
}

// Tests that NiceMock works with a mock class that has a non-default
// constructor.
TEST(NiceMockTest, NonDefaultConstructor) {
  NiceMock<MockBar> nice_bar("hi");
  EXPECT_EQ("hi", nice_bar.str());

  nice_bar.This();
  nice_bar.That(5, true);
}

// Tests that NiceMock works with a mock class that has a 10-ary
// non-default constructor.
TEST(NiceMockTest, NonDefaultConstructor10) {
  NiceMock<MockBar> nice_bar('a', 'b', "c", "d", 'e', 'f',
                             "g", "h", true, false);
  EXPECT_EQ("abcdefghTF", nice_bar.str());

  nice_bar.This();
  nice_bar.That(5, true);
}

249
#if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
250
// Tests that NiceMock<Mock> compiles where Mock is a user-defined
zhanyong.wan's avatar
zhanyong.wan committed
251
// class (as opposed to ::testing::Mock).  We had to work around an
252
253
254
// MSVC 8.0 bug that caused the symbol Mock used in the definition of
// NiceMock to be looked up in the wrong context, and this test
// ensures that our fix works.
255
//
256
257
// We have to skip this test on Symbian and Windows Mobile, as it
// causes the program to crash there, for reasons unclear to us yet.
258
259
260
261
262
TEST(NiceMockTest, AcceptsClassNamedMock) {
  NiceMock< ::Mock> nice;
  EXPECT_CALL(nice, DoThis());
  nice.DoThis();
}
263
#endif  // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
264

265
266
267
268
269
270
271
TEST(NiceMockTest, IsNaggy_IsNice_IsStrict) {
  NiceMock<MockFoo> nice_foo;
  EXPECT_FALSE(Mock::IsNaggy(&nice_foo));
  EXPECT_TRUE (Mock::IsNice(&nice_foo));
  EXPECT_FALSE(Mock::IsStrict(&nice_foo));
}

zhanyong.wan's avatar
zhanyong.wan committed
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#if GTEST_HAS_STREAM_REDIRECTION

// Tests that a naggy mock generates warnings for uninteresting calls.
TEST(NaggyMockTest, WarningForUninterestingCall) {
  const string saved_flag = GMOCK_FLAG(verbose);
  GMOCK_FLAG(verbose) = "warning";

  NaggyMock<MockFoo> naggy_foo;

  CaptureStdout();
  naggy_foo.DoThis();
  naggy_foo.DoThat(true);
  EXPECT_THAT(GetCapturedStdout(),
              HasSubstr("Uninteresting mock function call"));

  GMOCK_FLAG(verbose) = saved_flag;
}

// Tests that a naggy mock generates a warning for an uninteresting call
// that deletes the mock object.
TEST(NaggyMockTest, WarningForUninterestingCallAfterDeath) {
  const string saved_flag = GMOCK_FLAG(verbose);
  GMOCK_FLAG(verbose) = "warning";

  NaggyMock<MockFoo>* const naggy_foo = new NaggyMock<MockFoo>;

  ON_CALL(*naggy_foo, DoThis())
      .WillByDefault(Invoke(naggy_foo, &MockFoo::Delete));

  CaptureStdout();
  naggy_foo->DoThis();
  EXPECT_THAT(GetCapturedStdout(),
              HasSubstr("Uninteresting mock function call"));

  GMOCK_FLAG(verbose) = saved_flag;
}

#endif  // GTEST_HAS_STREAM_REDIRECTION

// Tests that a naggy mock allows expected calls.
TEST(NaggyMockTest, AllowsExpectedCall) {
  NaggyMock<MockFoo> naggy_foo;

  EXPECT_CALL(naggy_foo, DoThis());
  naggy_foo.DoThis();
}

// Tests that an unexpected call on a naggy mock fails.
TEST(NaggyMockTest, UnexpectedCallFails) {
  NaggyMock<MockFoo> naggy_foo;

  EXPECT_CALL(naggy_foo, DoThis()).Times(0);
  EXPECT_NONFATAL_FAILURE(naggy_foo.DoThis(),
                          "called more times than expected");
}

// Tests that NaggyMock works with a mock class that has a non-default
// constructor.
TEST(NaggyMockTest, NonDefaultConstructor) {
  NaggyMock<MockBar> naggy_bar("hi");
  EXPECT_EQ("hi", naggy_bar.str());

  naggy_bar.This();
  naggy_bar.That(5, true);
}

// Tests that NaggyMock works with a mock class that has a 10-ary
// non-default constructor.
TEST(NaggyMockTest, NonDefaultConstructor10) {
  NaggyMock<MockBar> naggy_bar('0', '1', "2", "3", '4', '5',
                               "6", "7", true, false);
  EXPECT_EQ("01234567TF", naggy_bar.str());

  naggy_bar.This();
  naggy_bar.That(5, true);
}

#if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
// Tests that NaggyMock<Mock> compiles where Mock is a user-defined
// class (as opposed to ::testing::Mock).  We had to work around an
// MSVC 8.0 bug that caused the symbol Mock used in the definition of
// NaggyMock to be looked up in the wrong context, and this test
// ensures that our fix works.
//
// We have to skip this test on Symbian and Windows Mobile, as it
// causes the program to crash there, for reasons unclear to us yet.
TEST(NaggyMockTest, AcceptsClassNamedMock) {
  NaggyMock< ::Mock> naggy;
  EXPECT_CALL(naggy, DoThis());
  naggy.DoThis();
}
#endif  // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE

365
366
367
368
369
370
371
TEST(NaggyMockTest, IsNaggy_IsNice_IsStrict) {
  NaggyMock<MockFoo> naggy_foo;
  EXPECT_TRUE (Mock::IsNaggy(&naggy_foo));
  EXPECT_FALSE(Mock::IsNice(&naggy_foo));
  EXPECT_FALSE(Mock::IsStrict(&naggy_foo));
}

372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
// Tests that a strict mock allows expected calls.
TEST(StrictMockTest, AllowsExpectedCall) {
  StrictMock<MockFoo> strict_foo;

  EXPECT_CALL(strict_foo, DoThis());
  strict_foo.DoThis();
}

// Tests that an unexpected call on a strict mock fails.
TEST(StrictMockTest, UnexpectedCallFails) {
  StrictMock<MockFoo> strict_foo;

  EXPECT_CALL(strict_foo, DoThis()).Times(0);
  EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
                          "called more times than expected");
}

// Tests that an uninteresting call on a strict mock fails.
TEST(StrictMockTest, UninterestingCallFails) {
  StrictMock<MockFoo> strict_foo;

  EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
                          "Uninteresting mock function call");
}

// Tests that an uninteresting call on a strict mock fails, even if
// the call deletes the mock object.
TEST(StrictMockTest, UninterestingCallFailsAfterDeath) {
  StrictMock<MockFoo>* const strict_foo = new StrictMock<MockFoo>;

  ON_CALL(*strict_foo, DoThis())
      .WillByDefault(Invoke(strict_foo, &MockFoo::Delete));

  EXPECT_NONFATAL_FAILURE(strict_foo->DoThis(),
                          "Uninteresting mock function call");
}

// Tests that StrictMock works with a mock class that has a
// non-default constructor.
TEST(StrictMockTest, NonDefaultConstructor) {
  StrictMock<MockBar> strict_bar("hi");
  EXPECT_EQ("hi", strict_bar.str());

  EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
                          "Uninteresting mock function call");
}

// Tests that StrictMock works with a mock class that has a 10-ary
// non-default constructor.
TEST(StrictMockTest, NonDefaultConstructor10) {
  StrictMock<MockBar> strict_bar('a', 'b', "c", "d", 'e', 'f',
                                 "g", "h", true, false);
  EXPECT_EQ("abcdefghTF", strict_bar.str());

  EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
                          "Uninteresting mock function call");
}

430
#if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
431
// Tests that StrictMock<Mock> compiles where Mock is a user-defined
zhanyong.wan's avatar
zhanyong.wan committed
432
// class (as opposed to ::testing::Mock).  We had to work around an
433
434
435
// MSVC 8.0 bug that caused the symbol Mock used in the definition of
// StrictMock to be looked up in the wrong context, and this test
// ensures that our fix works.
436
//
437
438
// We have to skip this test on Symbian and Windows Mobile, as it
// causes the program to crash there, for reasons unclear to us yet.
439
TEST(StrictMockTest, AcceptsClassNamedMock) {
440
441
442
  StrictMock< ::Mock> strict;
  EXPECT_CALL(strict, DoThis());
  strict.DoThis();
443
}
444
#endif  // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
445

446
447
448
449
450
451
452
TEST(StrictMockTest, IsNaggy_IsNice_IsStrict) {
  StrictMock<MockFoo> strict_foo;
  EXPECT_FALSE(Mock::IsNaggy(&strict_foo));
  EXPECT_FALSE(Mock::IsNice(&strict_foo));
  EXPECT_TRUE (Mock::IsStrict(&strict_foo));
}

453
454
}  // namespace gmock_nice_strict_test
}  // namespace testing