blob: 769c85d264c6e67bb3701af0f4e8a56e77d9c056 [file] [log] [blame]
zhanyong.wan38ca64d2009-02-19 22:30:22 +00001// Copyright 2009, 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: vladl@google.com (Vlad Losev)
31
32// Google Mock - a framework for writing C++ mock classes.
33//
34// This file tests that:
35// a. A header file defining a mock class can be included in multiple
36// translation units without causing a link error.
37// b. Actions and matchers can be instantiated with identical template
38// arguments in different translation units without causing link
39// errors.
40// The following constructs are currently tested:
41// Actions:
42// Return()
43// Return(value)
44// ReturnNull
45// ReturnRef
46// Assign
47// SetArgumentPointee
48// SetArrayArgument
49// SetErrnoAndReturn
50// Invoke(function)
51// Invoke(object, method)
52// InvokeWithoutArgs(function)
53// InvokeWithoutArgs(object, method)
54// InvokeArgument
55// WithArg
56// WithArgs
57// WithoutArgs
58// DoAll
59// DoDefault
60// IgnoreResult
61// Throw
62// ACTION()-generated
63// ACTION_P()-generated
64// ACTION_P2()-generated
65// Matchers:
66// _
67// A
68// An
69// Eq
70// Gt, Lt, Ge, Le, Ne
71// NotNull
72// Ref
73// TypedEq
74// DoubleEq
75// FloatEq
76// NanSensitiveDoubleEq
77// NanSensitiveFloatEq
78// ContainsRegex
79// MatchesRegex
80// EndsWith
81// HasSubstr
82// StartsWith
83// StrCaseEq
84// StrCaseNe
85// StrEq
86// StrNe
87// ElementsAre
88// ElementsAreArray
89// ContainerEq
90// Field
91// Property
92// ResultOf(function)
93// Pointee
94// Truly(predicate)
95// AllOf
96// AnyOf
97// Not
98// MatcherCast<T>
99//
100// Please note: this test does not verify the functioning of these
101// constructs, only that the programs using them will link successfully.
102//
103// Implementation note:
104// This test requires identical definitions of Interface and Mock to be
105// included in different translation units. We achieve this by writing
106// them in this header and #including it in gmock_link_test.cc and
107// gmock_link2_test.cc. Because the symbols generated by the compiler for
108// those constructs must be identical in both translation units,
109// definitions of Interface and Mock tests MUST be kept in the SAME
110// NON-ANONYMOUS namespace in this file. The test fixture class LinkTest
111// is defined as LinkTest1 in gmock_link_test.cc and as LinkTest2 in
112// gmock_link2_test.cc to avoid producing linker errors.
113
114#ifndef GMOCK_TEST_GMOCK_LINK_TEST_H_
115#define GMOCK_TEST_GMOCK_LINK_TEST_H_
116
117#include <gmock/gmock.h>
118
119#include <errno.h>
120#include <gtest/gtest.h>
121#include <iostream>
122#include <vector>
123
124using testing::_;
125using testing::A;
126using testing::AllOf;
127using testing::AnyOf;
128using testing::Assign;
129using testing::ContainerEq;
130using testing::DoAll;
131using testing::DoDefault;
132using testing::DoubleEq;
133using testing::ElementsAre;
134using testing::ElementsAreArray;
135using testing::EndsWith;
136using testing::Eq;
137using testing::Field;
138using testing::FloatEq;
139using testing::Ge;
140using testing::Gt;
141using testing::HasSubstr;
142using testing::IgnoreResult;
143using testing::Invoke;
144using testing::InvokeArgument;
145using testing::InvokeWithoutArgs;
146using testing::Le;
147using testing::Lt;
148using testing::Matcher;
149using testing::MatcherCast;
150using testing::NanSensitiveDoubleEq;
151using testing::NanSensitiveFloatEq;
152using testing::Ne;
153using testing::Not;
154using testing::NotNull;
155using testing::Pointee;
156using testing::Property;
157using testing::Ref;
158using testing::ResultOf;
159using testing::Return;
160using testing::ReturnNull;
161using testing::ReturnRef;
162using testing::SetArgumentPointee;
163using testing::SetArrayArgument;
164using testing::SetErrnoAndReturn;
165using testing::StartsWith;
166using testing::StrCaseEq;
167using testing::StrCaseNe;
168using testing::StrEq;
169using testing::StrNe;
170using testing::Truly;
171using testing::TypedEq;
172using testing::WithArg;
173using testing::WithArgs;
174using testing::WithoutArgs;
175
176#if GTEST_HAS_EXCEPTIONS
177using testing::Throw;
178#endif // GTEST_HAS_EXCEPTIONS
179
180#if GMOCK_HAS_REGEX
181using testing::ContainsRegex;
182using testing::MatchesRegex;
183#endif // GMOCK_HAS_REGEX
184
185class Interface {
186 public:
187 virtual ~Interface() {}
188 virtual void VoidFromString(char* str) = 0;
189 virtual char* StringFromString(char* str) = 0;
190 virtual int IntFromString(char* str) = 0;
191 virtual int& IntRefFromString(char* str) = 0;
192 virtual void VoidFromFunc(void(*)(char*)) = 0;
193 virtual void VoidFromIntRef(int& n) = 0;
194 virtual void VoidFromFloat(float n) = 0;
195 virtual void VoidFromDouble(double n) = 0;
196 virtual void VoidFromVector(const std::vector<int>& v) = 0;
197};
198
199class Mock: public Interface {
200 public:
201 MOCK_METHOD1(VoidFromString, void(char* str));
202 MOCK_METHOD1(StringFromString, char*(char* str));
203 MOCK_METHOD1(IntFromString, int(char* str));
204 MOCK_METHOD1(IntRefFromString, int&(char* str));
205 MOCK_METHOD1(VoidFromFunc, void(void(*func)(char* str)));
206 MOCK_METHOD1(VoidFromIntRef, void(int& n));
207 MOCK_METHOD1(VoidFromFloat, void(float n));
208 MOCK_METHOD1(VoidFromDouble, void(double n));
209 MOCK_METHOD1(VoidFromVector, void(const std::vector<int>& v));
210};
211
212class InvokeHelper {
213 public:
214 static void StaticVoidFromVoid() {}
215 void VoidFromVoid() {}
216 static void StaticVoidFromString(char*) {}
217 void VoidFromString(char*) {}
218 static int StaticIntFromString(char*) { return 1; }
219 static bool StaticBoolFromString(const char*) { return true; }
220};
221
222class FieldHelper {
223 public:
224 FieldHelper(int field) : field_(field) {}
225 int field() const { return field_; }
226 int field_; // NOLINT -- need external access to field_ to test
227 // the Field matcher.
228};
229
230// Tests the linkage of the ReturnVoid action.
231TEST(LinkTest, TestReturnVoid) {
232 Mock mock;
233
234 EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Return());
235 mock.VoidFromString(NULL);
236}
237
238// Tests the linkage of the Return action.
239TEST(LinkTest, TestReturn) {
240 Mock mock;
241 char ch = 'x';
242
243 EXPECT_CALL(mock, StringFromString(_)).WillOnce(Return(&ch));
244 mock.StringFromString(NULL);
245}
246
247// Tests the linkage of the ReturnNull action.
248TEST(LinkTest, TestReturnNull) {
249 Mock mock;
250
251 EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Return());
252 mock.VoidFromString(NULL);
253}
254
255// Tests the linkage of the ReturnRef action.
256TEST(LinkTest, TestReturnRef) {
257 Mock mock;
258 int n = 42;
259
260 EXPECT_CALL(mock, IntRefFromString(_)).WillOnce(ReturnRef(n));
261 mock.IntRefFromString(NULL);
262}
263
264// Tests the linkage of the Assign action.
265TEST(LinkTest, TestAssign) {
266 Mock mock;
267 char ch = 'x';
268
269 EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Assign(&ch, 'y'));
270 mock.VoidFromString(NULL);
271}
272
273// Tests the linkage of the SetArgumentPointee action.
274TEST(LinkTest, TestSetArgumentPointee) {
275 Mock mock;
276 char ch = 'x';
277
278 EXPECT_CALL(mock, VoidFromString(_)).WillOnce(SetArgumentPointee<0>('y'));
279 mock.VoidFromString(&ch);
280}
281
282// Tests the linkage of the SetArrayArgument action.
283TEST(LinkTest, TestSetArrayArgument) {
284 Mock mock;
285 char ch = 'x';
286 char ch2 = 'y';
287
288 EXPECT_CALL(mock, VoidFromString(_)).WillOnce(SetArrayArgument<0>(&ch2,
289 &ch2 + 1));
290 mock.VoidFromString(&ch);
291}
292
293// Tests the linkage of the SetErrnoAndReturn action.
294TEST(LinkTest, TestSetErrnoAndReturn) {
295 Mock mock;
296
297 int saved_errno = errno;
298 EXPECT_CALL(mock, IntFromString(_)).WillOnce(SetErrnoAndReturn(1, -1));
299 mock.IntFromString(NULL);
300 errno = saved_errno;
301}
302
303// Tests the linkage of the Invoke(function) and Invoke(object, method) actions.
304TEST(LinkTest, TestInvoke) {
305 Mock mock;
306 InvokeHelper test_invoke_helper;
307
308 EXPECT_CALL(mock, VoidFromString(_))
309 .WillOnce(Invoke(&InvokeHelper::StaticVoidFromString))
310 .WillOnce(Invoke(&test_invoke_helper, &InvokeHelper::VoidFromString));
311 mock.VoidFromString(NULL);
312 mock.VoidFromString(NULL);
313}
314
315// Tests the linkage of the InvokeWithoutArgs action.
316TEST(LinkTest, TestInvokeWithoutArgs) {
317 Mock mock;
318 InvokeHelper test_invoke_helper;
319
320 EXPECT_CALL(mock, VoidFromString(_))
321 .WillOnce(InvokeWithoutArgs(&InvokeHelper::StaticVoidFromVoid))
322 .WillOnce(InvokeWithoutArgs(&test_invoke_helper,
323 &InvokeHelper::VoidFromVoid));
324 mock.VoidFromString(NULL);
325 mock.VoidFromString(NULL);
326}
327
328// Tests the linkage of the InvokeArgument action.
329TEST(LinkTest, TestInvokeArgument) {
330 Mock mock;
331 char ch = 'x';
332
333 EXPECT_CALL(mock, VoidFromFunc(_)).WillOnce(InvokeArgument<0>(&ch));
334 mock.VoidFromFunc(InvokeHelper::StaticVoidFromString);
335}
336
337// Tests the linkage of the WithArg action.
338TEST(LinkTest, TestWithArg) {
339 Mock mock;
340
341 EXPECT_CALL(mock, VoidFromString(_))
342 .WillOnce(WithArg<0>(Invoke(&InvokeHelper::StaticVoidFromString)));
343 mock.VoidFromString(NULL);
344}
345
346// Tests the linkage of the WithArgs action.
347TEST(LinkTest, TestWithArgs) {
348 Mock mock;
349
350 EXPECT_CALL(mock, VoidFromString(_))
351 .WillOnce(WithArgs<0>(Invoke(&InvokeHelper::StaticVoidFromString)));
352 mock.VoidFromString(NULL);
353}
354
355// Tests the linkage of the WithoutArgs action.
356TEST(LinkTest, TestWithoutArgs) {
357 Mock mock;
358
359 EXPECT_CALL(mock, VoidFromString(_)).WillOnce(WithoutArgs(Return()));
360 mock.VoidFromString(NULL);
361}
362
363// Tests the linkage of the DoAll action.
364TEST(LinkTest, TestDoAll) {
365 Mock mock;
366 char ch = 'x';
367
368 EXPECT_CALL(mock, VoidFromString(_))
369 .WillOnce(DoAll(SetArgumentPointee<0>('y'), Return()));
370 mock.VoidFromString(&ch);
371}
372
373// Tests the linkage of the DoDefault action.
374TEST(LinkTest, TestDoDefault) {
375 Mock mock;
376 char ch = 'x';
377
378 ON_CALL(mock, VoidFromString(_)).WillByDefault(Return());
379 EXPECT_CALL(mock, VoidFromString(_)).WillOnce(DoDefault());
380 mock.VoidFromString(&ch);
381}
382
383// Tests the linkage of the IgnoreResult action.
384TEST(LinkTest, TestIgnoreResult) {
385 Mock mock;
386
387 EXPECT_CALL(mock, VoidFromString(_)).WillOnce(IgnoreResult(Return(42)));
388 mock.VoidFromString(NULL);
389}
390
391#if GTEST_HAS_EXCEPTIONS
392// Tests the linkage of the Throw action.
393TEST(LinkTest, TestThrow) {
394 Mock mock;
395
396 EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Throw(42));
397 EXPECT_THROW(mock.VoidFromString(NULL), int);
398}
399#endif // GTEST_HAS_EXCEPTIONS
400
401// Tests the linkage of actions created using ACTION macro.
402namespace {
403ACTION(Return1) { return 1; }
404}
405
406TEST(LinkTest, TestActionMacro) {
407 Mock mock;
408
409 EXPECT_CALL(mock, IntFromString(_)).WillOnce(Return1());
410 mock.IntFromString(NULL);
411}
412
413// Tests the linkage of actions created using ACTION_P macro.
414namespace {
415ACTION_P(ReturnArgument, ret_value) { return ret_value; }
416}
417
418TEST(LinkTest, TestActionPMacro) {
419 Mock mock;
420
421 EXPECT_CALL(mock, IntFromString(_)).WillOnce(ReturnArgument(42));
422 mock.IntFromString(NULL);
423}
424
425// Tests the linkage of actions created using ACTION_P2 macro.
426namespace {
427ACTION_P2(ReturnEqualsEitherOf, first, second) {
428 return arg0 == first || arg0 == second;
429}
430}
431
432TEST(LinkTest, TestActionP2Macro) {
433 Mock mock;
434 char ch = 'x';
435
436 EXPECT_CALL(mock, IntFromString(_))
437 .WillOnce(ReturnEqualsEitherOf("one", "two"));
438 mock.IntFromString(&ch);
439}
440
441// Tests the linkage of the "_" matcher.
442TEST(LinkTest, TestMatcherAnything) {
443 Mock mock;
444
445 ON_CALL(mock, VoidFromString(_)).WillByDefault(Return());
446}
447
448// Tests the linkage of the A matcher.
449TEST(LinkTest, TestMatcherA) {
450 Mock mock;
451
452 ON_CALL(mock, VoidFromString(A<char*>())).WillByDefault(Return());
453}
454
455// Tests the linkage of the Eq and the "bare value" matcher.
456TEST(LinkTest, TestMatchersEq) {
457 Mock mock;
458 const char* p = "x";
459
460 ON_CALL(mock, VoidFromString(Eq(p))).WillByDefault(Return());
461 ON_CALL(mock, VoidFromString(const_cast<char*>("y")))
462 .WillByDefault(Return());
463}
464
465// Tests the linkage of the Lt, Gt, Le, Ge, and Ne matchers.
466TEST(LinkTest, TestMatchersRelations) {
467 Mock mock;
468
469 ON_CALL(mock, VoidFromFloat(Lt(1.0f))).WillByDefault(Return());
470 ON_CALL(mock, VoidFromFloat(Gt(1.0f))).WillByDefault(Return());
471 ON_CALL(mock, VoidFromFloat(Le(1.0f))).WillByDefault(Return());
472 ON_CALL(mock, VoidFromFloat(Ge(1.0f))).WillByDefault(Return());
473 ON_CALL(mock, VoidFromFloat(Ne(1.0f))).WillByDefault(Return());
474}
475
476// Tests the linkage of the NotNull matcher.
477TEST(LinkTest, TestMatcherNotNull) {
478 Mock mock;
479
480 ON_CALL(mock, VoidFromString(NotNull())).WillByDefault(Return());
481}
482
483// Tests the linkage of the Ref matcher.
484TEST(LinkTest, TestMatcherRef) {
485 Mock mock;
486 int a = 0;
487
488 ON_CALL(mock, VoidFromIntRef(Ref(a))).WillByDefault(Return());
489}
490
491// Tests the linkage of the TypedEq matcher.
492TEST(LinkTest, TestMatcherTypedEq) {
493 Mock mock;
494 unsigned long a = 0;
495
496 ON_CALL(mock, VoidFromIntRef(TypedEq<int&>(a))).WillByDefault(Return());
497}
498
499// Tests the linkage of the FloatEq, DoubleEq, NanSensitiveFloatEq and
500// NanSensitiveDoubleEq matchers.
501TEST(LinkTest, TestMatchersFloatingPoint) {
502 Mock mock;
503 float a = 0;
504
505 ON_CALL(mock, VoidFromFloat(FloatEq(a))).WillByDefault(Return());
506 ON_CALL(mock, VoidFromDouble(DoubleEq(a))).WillByDefault(Return());
507 ON_CALL(mock, VoidFromFloat(NanSensitiveFloatEq(a))).WillByDefault(Return());
508 ON_CALL(mock, VoidFromDouble(NanSensitiveDoubleEq(a)))
509 .WillByDefault(Return());
510}
511
512#if GMOCK_HAS_REGEX
513// Tests the linkage of the ContainsRegex matcher.
514TEST(LinkTest, TestMatcherContainsRegex) {
515 Mock mock;
516
517 ON_CALL(mock, VoidFromString(ContainsRegex(".*"))).WillByDefault(Return());
518}
519
520// Tests the linkage of the MatchesRegex matcher.
521TEST(LinkTest, TestMatcherMatchesRegex) {
522 Mock mock;
523
524 ON_CALL(mock, VoidFromString(MatchesRegex(".*"))).WillByDefault(Return());
525}
526#endif // GMOCK_HAS_REGEX
527
528// Tests the linkage of the StartsWith, EndsWith, and HasSubstr matchers.
529TEST(LinkTest, TestMatchersSubstrings) {
530 Mock mock;
531
532 ON_CALL(mock, VoidFromString(StartsWith("a"))).WillByDefault(Return());
533 ON_CALL(mock, VoidFromString(EndsWith("c"))).WillByDefault(Return());
534 ON_CALL(mock, VoidFromString(HasSubstr("b"))).WillByDefault(Return());
535}
536
537// Tests the linkage of the StrEq, StrNe, StrCaseEq, and StrCaseNe matchers.
538TEST(LinkTest, TestMatchersStringEquality) {
539 Mock mock;
540 ON_CALL(mock, VoidFromString(StrEq("a"))).WillByDefault(Return());
541 ON_CALL(mock, VoidFromString(StrNe("a"))).WillByDefault(Return());
542 ON_CALL(mock, VoidFromString(StrCaseEq("a"))).WillByDefault(Return());
543 ON_CALL(mock, VoidFromString(StrCaseNe("a"))).WillByDefault(Return());
544}
545
546// Tests the linkage of the ElementsAre matcher.
547TEST(LinkTest, TestMatcherElementsAre) {
548 Mock mock;
549
550 ON_CALL(mock, VoidFromVector(ElementsAre('a', _))).WillByDefault(Return());
551}
552
553// Tests the linkage of the ElementsAreArray matcher.
554TEST(LinkTest, TestMatcherElementsAreArray) {
555 Mock mock;
556 char arr[] = { 'a', 'b' };
557
558 ON_CALL(mock, VoidFromVector(ElementsAreArray(arr))).WillByDefault(Return());
559}
560
561// Tests the linkage of the ContainerEq matcher.
562TEST(LinkTest, TestMatcherContainerEq) {
563 Mock mock;
564 std::vector<int> v;
565
566 ON_CALL(mock, VoidFromVector(ContainerEq(v))).WillByDefault(Return());
567}
568
569// Tests the linkage of the Field matcher.
570TEST(LinkTest, TestMatcherField) {
571 FieldHelper helper(0);
572
573 Matcher<const FieldHelper&> m = Field(&FieldHelper::field_, Eq(0));
574 EXPECT_TRUE(m.Matches(helper));
575
576 Matcher<const FieldHelper*> m2 = Field(&FieldHelper::field_, Eq(0));
577 EXPECT_TRUE(m2.Matches(&helper));
578}
579
580// Tests the linkage of the Property matcher.
581TEST(LinkTest, TestMatcherProperty) {
582 FieldHelper helper(0);
583
584 Matcher<const FieldHelper&> m = Property(&FieldHelper::field, Eq(0));
585 EXPECT_TRUE(m.Matches(helper));
586
587 Matcher<const FieldHelper*> m2 = Property(&FieldHelper::field, Eq(0));
588 EXPECT_TRUE(m2.Matches(&helper));
589}
590
591// Tests the linkage of the ResultOf matcher.
592TEST(LinkTest, TestMatcherResultOf) {
593 Matcher<char*> m = ResultOf(&InvokeHelper::StaticIntFromString, Eq(1));
594 EXPECT_TRUE(m.Matches(NULL));
595}
596
597// Tests the linkage of the ResultOf matcher.
598TEST(LinkTest, TestMatcherPointee) {
599 int n = 1;
600
601 Matcher<int*> m = Pointee(Eq(1));
602 EXPECT_TRUE(m.Matches(&n));
603}
604
605// Tests the linkage of the Truly matcher.
606TEST(LinkTest, TestMatcherTruly) {
607 Matcher<const char*> m = Truly(&InvokeHelper::StaticBoolFromString);
608 EXPECT_TRUE(m.Matches(NULL));
609}
610
611// Tests the linkage of the AllOf matcher.
612TEST(LinkTest, TestMatcherAllOf) {
613 Matcher<int> m = AllOf(_, Eq(1));
614 EXPECT_TRUE(m.Matches(1));
615}
616
617// Tests the linkage of the AnyOf matcher.
618TEST(LinkTest, TestMatcherAnyOf) {
619 Matcher<int> m = AnyOf(_, Eq(1));
620 EXPECT_TRUE(m.Matches(1));
621}
622
623// Tests the linkage of the Not matcher.
624TEST(LinkTest, TestMatcherNot) {
625 Matcher<int> m = Not(_);
626 EXPECT_FALSE(m.Matches(1));
627}
628
629// Tests the linkage of the MatcherCast<T>() function.
630TEST(LinkTest, TestMatcherCast) {
631 Matcher<const char*> m = MatcherCast<const char*>(_);
632 EXPECT_TRUE(m.Matches(NULL));
633}
634
635#endif // GMOCK_TEST_GMOCK_LINK_TEST_H_