1 // Copyright 2009, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
31 // Google Mock - a framework for writing C++ mock classes.
33 // This file tests that:
34 // a. A header file defining a mock class can be included in multiple
35 // translation units without causing a link error.
36 // b. Actions and matchers can be instantiated with identical template
37 // arguments in different translation units without causing link
39 // The following constructs are currently tested:
50 // Invoke(object, method)
51 // InvokeWithoutArgs(function)
52 // InvokeWithoutArgs(object, method)
62 // ACTION_P()-generated
63 // ACTION_P2()-generated
75 // NanSensitiveDoubleEq
76 // NanSensitiveFloatEq
101 // Please note: this test does not verify the functioning of these
102 // constructs, only that the programs using them will link successfully.
104 // Implementation note:
105 // This test requires identical definitions of Interface and Mock to be
106 // included in different translation units. We achieve this by writing
107 // them in this header and #including it in gmock_link_test.cc and
108 // gmock_link2_test.cc. Because the symbols generated by the compiler for
109 // those constructs must be identical in both translation units,
110 // definitions of Interface and Mock tests MUST be kept in the SAME
111 // NON-ANONYMOUS namespace in this file. The test fixture class LinkTest
112 // is defined as LinkTest1 in gmock_link_test.cc and as LinkTest2 in
113 // gmock_link2_test.cc to avoid producing linker errors.
115 #ifndef GMOCK_TEST_GMOCK_LINK_TEST_H_
116 #define GMOCK_TEST_GMOCK_LINK_TEST_H_
118 #include "gmock/gmock.h"
120 #if !GTEST_OS_WINDOWS_MOBILE
127 #include "gtest/gtest.h"
128 #include "gtest/internal/gtest-port.h"
132 using testing::Action
;
133 using testing::AllOf
;
134 using testing::AnyOf
;
135 using testing::Assign
;
136 using testing::ContainerEq
;
137 using testing::DoAll
;
138 using testing::DoDefault
;
139 using testing::DoubleEq
;
140 using testing::ElementsAre
;
141 using testing::ElementsAreArray
;
142 using testing::EndsWith
;
144 using testing::Field
;
145 using testing::FloatEq
;
148 using testing::HasSubstr
;
149 using testing::IgnoreResult
;
150 using testing::Invoke
;
151 using testing::InvokeArgument
;
152 using testing::InvokeWithoutArgs
;
153 using testing::IsNull
;
154 using testing::IsSubsetOf
;
155 using testing::IsSupersetOf
;
158 using testing::Matcher
;
159 using testing::MatcherCast
;
160 using testing::NanSensitiveDoubleEq
;
161 using testing::NanSensitiveFloatEq
;
164 using testing::NotNull
;
165 using testing::Pointee
;
166 using testing::Property
;
168 using testing::ResultOf
;
169 using testing::Return
;
170 using testing::ReturnNull
;
171 using testing::ReturnRef
;
172 using testing::SetArgPointee
;
173 using testing::SetArrayArgument
;
174 using testing::StartsWith
;
175 using testing::StrCaseEq
;
176 using testing::StrCaseNe
;
177 using testing::StrEq
;
178 using testing::StrNe
;
179 using testing::Truly
;
180 using testing::TypedEq
;
181 using testing::WithArg
;
182 using testing::WithArgs
;
183 using testing::WithoutArgs
;
185 #if !GTEST_OS_WINDOWS_MOBILE
186 using testing::SetErrnoAndReturn
;
189 #if GTEST_HAS_EXCEPTIONS
190 using testing::Throw
;
193 using testing::ContainsRegex
;
194 using testing::MatchesRegex
;
198 virtual ~Interface() {}
199 virtual void VoidFromString(char* str
) = 0;
200 virtual char* StringFromString(char* str
) = 0;
201 virtual int IntFromString(char* str
) = 0;
202 virtual int& IntRefFromString(char* str
) = 0;
203 virtual void VoidFromFunc(void(*func
)(char* str
)) = 0;
204 virtual void VoidFromIntRef(int& n
) = 0; // NOLINT
205 virtual void VoidFromFloat(float n
) = 0;
206 virtual void VoidFromDouble(double n
) = 0;
207 virtual void VoidFromVector(const std::vector
<int>& v
) = 0;
210 class Mock
: public Interface
{
214 MOCK_METHOD1(VoidFromString
, void(char* str
));
215 MOCK_METHOD1(StringFromString
, char*(char* str
));
216 MOCK_METHOD1(IntFromString
, int(char* str
));
217 MOCK_METHOD1(IntRefFromString
, int&(char* str
));
218 MOCK_METHOD1(VoidFromFunc
, void(void(*func
)(char* str
)));
219 MOCK_METHOD1(VoidFromIntRef
, void(int& n
)); // NOLINT
220 MOCK_METHOD1(VoidFromFloat
, void(float n
));
221 MOCK_METHOD1(VoidFromDouble
, void(double n
));
222 MOCK_METHOD1(VoidFromVector
, void(const std::vector
<int>& v
));
225 GTEST_DISALLOW_COPY_AND_ASSIGN_(Mock
);
230 static void StaticVoidFromVoid() {}
231 void VoidFromVoid() {}
232 static void StaticVoidFromString(char* /* str */) {}
233 void VoidFromString(char* /* str */) {}
234 static int StaticIntFromString(char* /* str */) { return 1; }
235 static bool StaticBoolFromString(const char* /* str */) { return true; }
240 explicit FieldHelper(int a_field
) : field_(a_field
) {}
241 int field() const { return field_
; }
242 int field_
; // NOLINT -- need external access to field_ to test
243 // the Field matcher.
246 // Tests the linkage of the ReturnVoid action.
247 TEST(LinkTest
, TestReturnVoid
) {
250 EXPECT_CALL(mock
, VoidFromString(_
)).WillOnce(Return());
251 mock
.VoidFromString(nullptr);
254 // Tests the linkage of the Return action.
255 TEST(LinkTest
, TestReturn
) {
259 EXPECT_CALL(mock
, StringFromString(_
)).WillOnce(Return(&ch
));
260 mock
.StringFromString(nullptr);
263 // Tests the linkage of the ReturnNull action.
264 TEST(LinkTest
, TestReturnNull
) {
267 EXPECT_CALL(mock
, VoidFromString(_
)).WillOnce(Return());
268 mock
.VoidFromString(nullptr);
271 // Tests the linkage of the ReturnRef action.
272 TEST(LinkTest
, TestReturnRef
) {
276 EXPECT_CALL(mock
, IntRefFromString(_
)).WillOnce(ReturnRef(n
));
277 mock
.IntRefFromString(nullptr);
280 // Tests the linkage of the Assign action.
281 TEST(LinkTest
, TestAssign
) {
285 EXPECT_CALL(mock
, VoidFromString(_
)).WillOnce(Assign(&ch
, 'y'));
286 mock
.VoidFromString(nullptr);
289 // Tests the linkage of the SetArgPointee action.
290 TEST(LinkTest
, TestSetArgPointee
) {
294 EXPECT_CALL(mock
, VoidFromString(_
)).WillOnce(SetArgPointee
<0>('y'));
295 mock
.VoidFromString(&ch
);
298 // Tests the linkage of the SetArrayArgument action.
299 TEST(LinkTest
, TestSetArrayArgument
) {
304 EXPECT_CALL(mock
, VoidFromString(_
)).WillOnce(SetArrayArgument
<0>(&ch2
,
306 mock
.VoidFromString(&ch
);
309 #if !GTEST_OS_WINDOWS_MOBILE
311 // Tests the linkage of the SetErrnoAndReturn action.
312 TEST(LinkTest
, TestSetErrnoAndReturn
) {
315 int saved_errno
= errno
;
316 EXPECT_CALL(mock
, IntFromString(_
)).WillOnce(SetErrnoAndReturn(1, -1));
317 mock
.IntFromString(nullptr);
321 #endif // !GTEST_OS_WINDOWS_MOBILE
323 // Tests the linkage of the Invoke(function) and Invoke(object, method) actions.
324 TEST(LinkTest
, TestInvoke
) {
326 InvokeHelper test_invoke_helper
;
328 EXPECT_CALL(mock
, VoidFromString(_
))
329 .WillOnce(Invoke(&InvokeHelper::StaticVoidFromString
))
330 .WillOnce(Invoke(&test_invoke_helper
, &InvokeHelper::VoidFromString
));
331 mock
.VoidFromString(nullptr);
332 mock
.VoidFromString(nullptr);
335 // Tests the linkage of the InvokeWithoutArgs action.
336 TEST(LinkTest
, TestInvokeWithoutArgs
) {
338 InvokeHelper test_invoke_helper
;
340 EXPECT_CALL(mock
, VoidFromString(_
))
341 .WillOnce(InvokeWithoutArgs(&InvokeHelper::StaticVoidFromVoid
))
342 .WillOnce(InvokeWithoutArgs(&test_invoke_helper
,
343 &InvokeHelper::VoidFromVoid
));
344 mock
.VoidFromString(nullptr);
345 mock
.VoidFromString(nullptr);
348 // Tests the linkage of the InvokeArgument action.
349 TEST(LinkTest
, TestInvokeArgument
) {
353 EXPECT_CALL(mock
, VoidFromFunc(_
)).WillOnce(InvokeArgument
<0>(&ch
));
354 mock
.VoidFromFunc(InvokeHelper::StaticVoidFromString
);
357 // Tests the linkage of the WithArg action.
358 TEST(LinkTest
, TestWithArg
) {
361 EXPECT_CALL(mock
, VoidFromString(_
))
362 .WillOnce(WithArg
<0>(Invoke(&InvokeHelper::StaticVoidFromString
)));
363 mock
.VoidFromString(nullptr);
366 // Tests the linkage of the WithArgs action.
367 TEST(LinkTest
, TestWithArgs
) {
370 EXPECT_CALL(mock
, VoidFromString(_
))
371 .WillOnce(WithArgs
<0>(Invoke(&InvokeHelper::StaticVoidFromString
)));
372 mock
.VoidFromString(nullptr);
375 // Tests the linkage of the WithoutArgs action.
376 TEST(LinkTest
, TestWithoutArgs
) {
379 EXPECT_CALL(mock
, VoidFromString(_
)).WillOnce(WithoutArgs(Return()));
380 mock
.VoidFromString(nullptr);
383 // Tests the linkage of the DoAll action.
384 TEST(LinkTest
, TestDoAll
) {
388 EXPECT_CALL(mock
, VoidFromString(_
))
389 .WillOnce(DoAll(SetArgPointee
<0>('y'), Return()));
390 mock
.VoidFromString(&ch
);
393 // Tests the linkage of the DoDefault action.
394 TEST(LinkTest
, TestDoDefault
) {
398 ON_CALL(mock
, VoidFromString(_
)).WillByDefault(Return());
399 EXPECT_CALL(mock
, VoidFromString(_
)).WillOnce(DoDefault());
400 mock
.VoidFromString(&ch
);
403 // Tests the linkage of the IgnoreResult action.
404 TEST(LinkTest
, TestIgnoreResult
) {
407 EXPECT_CALL(mock
, VoidFromString(_
)).WillOnce(IgnoreResult(Return(42)));
408 mock
.VoidFromString(nullptr);
411 #if GTEST_HAS_EXCEPTIONS
412 // Tests the linkage of the Throw action.
413 TEST(LinkTest
, TestThrow
) {
416 EXPECT_CALL(mock
, VoidFromString(_
)).WillOnce(Throw(42));
417 EXPECT_THROW(mock
.VoidFromString(nullptr), int);
419 #endif // GTEST_HAS_EXCEPTIONS
421 // The ACTION*() macros trigger warning C4100 (unreferenced formal
422 // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
423 // the macro definition, as the warnings are generated when the macro
424 // is expanded and macro expansion cannot contain #pragma. Therefore
425 // we suppress them here.
427 # pragma warning(push)
428 # pragma warning(disable:4100)
431 // Tests the linkage of actions created using ACTION macro.
433 ACTION(Return1
) { return 1; }
436 TEST(LinkTest
, TestActionMacro
) {
439 EXPECT_CALL(mock
, IntFromString(_
)).WillOnce(Return1());
440 mock
.IntFromString(nullptr);
443 // Tests the linkage of actions created using ACTION_P macro.
445 ACTION_P(ReturnArgument
, ret_value
) { return ret_value
; }
448 TEST(LinkTest
, TestActionPMacro
) {
451 EXPECT_CALL(mock
, IntFromString(_
)).WillOnce(ReturnArgument(42));
452 mock
.IntFromString(nullptr);
455 // Tests the linkage of actions created using ACTION_P2 macro.
457 ACTION_P2(ReturnEqualsEitherOf
, first
, second
) {
458 return arg0
== first
|| arg0
== second
;
463 # pragma warning(pop)
466 TEST(LinkTest
, TestActionP2Macro
) {
470 EXPECT_CALL(mock
, IntFromString(_
))
471 .WillOnce(ReturnEqualsEitherOf("one", "two"));
472 mock
.IntFromString(&ch
);
475 // Tests the linkage of the "_" matcher.
476 TEST(LinkTest
, TestMatcherAnything
) {
479 ON_CALL(mock
, VoidFromString(_
)).WillByDefault(Return());
482 // Tests the linkage of the A matcher.
483 TEST(LinkTest
, TestMatcherA
) {
486 ON_CALL(mock
, VoidFromString(A
<char*>())).WillByDefault(Return());
489 // Tests the linkage of the Eq and the "bare value" matcher.
490 TEST(LinkTest
, TestMatchersEq
) {
494 ON_CALL(mock
, VoidFromString(Eq(p
))).WillByDefault(Return());
495 ON_CALL(mock
, VoidFromString(const_cast<char*>("y")))
496 .WillByDefault(Return());
499 // Tests the linkage of the Lt, Gt, Le, Ge, and Ne matchers.
500 TEST(LinkTest
, TestMatchersRelations
) {
503 ON_CALL(mock
, VoidFromFloat(Lt(1.0f
))).WillByDefault(Return());
504 ON_CALL(mock
, VoidFromFloat(Gt(1.0f
))).WillByDefault(Return());
505 ON_CALL(mock
, VoidFromFloat(Le(1.0f
))).WillByDefault(Return());
506 ON_CALL(mock
, VoidFromFloat(Ge(1.0f
))).WillByDefault(Return());
507 ON_CALL(mock
, VoidFromFloat(Ne(1.0f
))).WillByDefault(Return());
510 // Tests the linkage of the NotNull matcher.
511 TEST(LinkTest
, TestMatcherNotNull
) {
514 ON_CALL(mock
, VoidFromString(NotNull())).WillByDefault(Return());
517 // Tests the linkage of the IsNull matcher.
518 TEST(LinkTest
, TestMatcherIsNull
) {
521 ON_CALL(mock
, VoidFromString(IsNull())).WillByDefault(Return());
524 // Tests the linkage of the Ref matcher.
525 TEST(LinkTest
, TestMatcherRef
) {
529 ON_CALL(mock
, VoidFromIntRef(Ref(a
))).WillByDefault(Return());
532 // Tests the linkage of the TypedEq matcher.
533 TEST(LinkTest
, TestMatcherTypedEq
) {
537 ON_CALL(mock
, VoidFromIntRef(TypedEq
<int&>(a
))).WillByDefault(Return());
540 // Tests the linkage of the FloatEq, DoubleEq, NanSensitiveFloatEq and
541 // NanSensitiveDoubleEq matchers.
542 TEST(LinkTest
, TestMatchersFloatingPoint
) {
546 ON_CALL(mock
, VoidFromFloat(FloatEq(a
))).WillByDefault(Return());
547 ON_CALL(mock
, VoidFromDouble(DoubleEq(a
))).WillByDefault(Return());
548 ON_CALL(mock
, VoidFromFloat(NanSensitiveFloatEq(a
))).WillByDefault(Return());
549 ON_CALL(mock
, VoidFromDouble(NanSensitiveDoubleEq(a
)))
550 .WillByDefault(Return());
553 // Tests the linkage of the ContainsRegex matcher.
554 TEST(LinkTest
, TestMatcherContainsRegex
) {
557 ON_CALL(mock
, VoidFromString(ContainsRegex(".*"))).WillByDefault(Return());
560 // Tests the linkage of the MatchesRegex matcher.
561 TEST(LinkTest
, TestMatcherMatchesRegex
) {
564 ON_CALL(mock
, VoidFromString(MatchesRegex(".*"))).WillByDefault(Return());
567 // Tests the linkage of the StartsWith, EndsWith, and HasSubstr matchers.
568 TEST(LinkTest
, TestMatchersSubstrings
) {
571 ON_CALL(mock
, VoidFromString(StartsWith("a"))).WillByDefault(Return());
572 ON_CALL(mock
, VoidFromString(EndsWith("c"))).WillByDefault(Return());
573 ON_CALL(mock
, VoidFromString(HasSubstr("b"))).WillByDefault(Return());
576 // Tests the linkage of the StrEq, StrNe, StrCaseEq, and StrCaseNe matchers.
577 TEST(LinkTest
, TestMatchersStringEquality
) {
579 ON_CALL(mock
, VoidFromString(StrEq("a"))).WillByDefault(Return());
580 ON_CALL(mock
, VoidFromString(StrNe("a"))).WillByDefault(Return());
581 ON_CALL(mock
, VoidFromString(StrCaseEq("a"))).WillByDefault(Return());
582 ON_CALL(mock
, VoidFromString(StrCaseNe("a"))).WillByDefault(Return());
585 // Tests the linkage of the ElementsAre matcher.
586 TEST(LinkTest
, TestMatcherElementsAre
) {
589 ON_CALL(mock
, VoidFromVector(ElementsAre('a', _
))).WillByDefault(Return());
592 // Tests the linkage of the ElementsAreArray matcher.
593 TEST(LinkTest
, TestMatcherElementsAreArray
) {
595 char arr
[] = { 'a', 'b' };
597 ON_CALL(mock
, VoidFromVector(ElementsAreArray(arr
))).WillByDefault(Return());
600 // Tests the linkage of the IsSubsetOf matcher.
601 TEST(LinkTest
, TestMatcherIsSubsetOf
) {
603 char arr
[] = {'a', 'b'};
605 ON_CALL(mock
, VoidFromVector(IsSubsetOf(arr
))).WillByDefault(Return());
608 // Tests the linkage of the IsSupersetOf matcher.
609 TEST(LinkTest
, TestMatcherIsSupersetOf
) {
611 char arr
[] = {'a', 'b'};
613 ON_CALL(mock
, VoidFromVector(IsSupersetOf(arr
))).WillByDefault(Return());
616 // Tests the linkage of the ContainerEq matcher.
617 TEST(LinkTest
, TestMatcherContainerEq
) {
621 ON_CALL(mock
, VoidFromVector(ContainerEq(v
))).WillByDefault(Return());
624 // Tests the linkage of the Field matcher.
625 TEST(LinkTest
, TestMatcherField
) {
626 FieldHelper
helper(0);
628 Matcher
<const FieldHelper
&> m
= Field(&FieldHelper::field_
, Eq(0));
629 EXPECT_TRUE(m
.Matches(helper
));
631 Matcher
<const FieldHelper
*> m2
= Field(&FieldHelper::field_
, Eq(0));
632 EXPECT_TRUE(m2
.Matches(&helper
));
635 // Tests the linkage of the Property matcher.
636 TEST(LinkTest
, TestMatcherProperty
) {
637 FieldHelper
helper(0);
639 Matcher
<const FieldHelper
&> m
= Property(&FieldHelper::field
, Eq(0));
640 EXPECT_TRUE(m
.Matches(helper
));
642 Matcher
<const FieldHelper
*> m2
= Property(&FieldHelper::field
, Eq(0));
643 EXPECT_TRUE(m2
.Matches(&helper
));
646 // Tests the linkage of the ResultOf matcher.
647 TEST(LinkTest
, TestMatcherResultOf
) {
648 Matcher
<char*> m
= ResultOf(&InvokeHelper::StaticIntFromString
, Eq(1));
649 EXPECT_TRUE(m
.Matches(nullptr));
652 // Tests the linkage of the ResultOf matcher.
653 TEST(LinkTest
, TestMatcherPointee
) {
656 Matcher
<int*> m
= Pointee(Eq(1));
657 EXPECT_TRUE(m
.Matches(&n
));
660 // Tests the linkage of the Truly matcher.
661 TEST(LinkTest
, TestMatcherTruly
) {
662 Matcher
<const char*> m
= Truly(&InvokeHelper::StaticBoolFromString
);
663 EXPECT_TRUE(m
.Matches(nullptr));
666 // Tests the linkage of the AllOf matcher.
667 TEST(LinkTest
, TestMatcherAllOf
) {
668 Matcher
<int> m
= AllOf(_
, Eq(1));
669 EXPECT_TRUE(m
.Matches(1));
672 // Tests the linkage of the AnyOf matcher.
673 TEST(LinkTest
, TestMatcherAnyOf
) {
674 Matcher
<int> m
= AnyOf(_
, Eq(1));
675 EXPECT_TRUE(m
.Matches(1));
678 // Tests the linkage of the Not matcher.
679 TEST(LinkTest
, TestMatcherNot
) {
680 Matcher
<int> m
= Not(_
);
681 EXPECT_FALSE(m
.Matches(1));
684 // Tests the linkage of the MatcherCast<T>() function.
685 TEST(LinkTest
, TestMatcherCast
) {
686 Matcher
<const char*> m
= MatcherCast
<const char*>(_
);
687 EXPECT_TRUE(m
.Matches(nullptr));
690 #endif // GMOCK_TEST_GMOCK_LINK_TEST_H_