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.
30 // Author: vladl@google.com (Vlad Losev)
32 // Google Mock - a framework for writing C++ mock classes.
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
40 // The following constructs are currently tested:
51 // Invoke(object, method)
52 // InvokeWithoutArgs(function)
53 // InvokeWithoutArgs(object, method)
63 // ACTION_P()-generated
64 // ACTION_P2()-generated
76 // NanSensitiveDoubleEq
77 // NanSensitiveFloatEq
100 // Please note: this test does not verify the functioning of these
101 // constructs, only that the programs using them will link successfully.
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.
114 #ifndef GMOCK_TEST_GMOCK_LINK_TEST_H_
115 #define GMOCK_TEST_GMOCK_LINK_TEST_H_
117 #include "gmock/gmock.h"
119 #if !GTEST_OS_WINDOWS_MOBILE
123 #include "gmock/internal/gmock-port.h"
124 #include "gtest/gtest.h"
130 using testing::AllOf
;
131 using testing::AnyOf
;
132 using testing::Assign
;
133 using testing::ContainerEq
;
134 using testing::DoAll
;
135 using testing::DoDefault
;
136 using testing::DoubleEq
;
137 using testing::ElementsAre
;
138 using testing::ElementsAreArray
;
139 using testing::EndsWith
;
141 using testing::Field
;
142 using testing::FloatEq
;
145 using testing::HasSubstr
;
146 using testing::IgnoreResult
;
147 using testing::Invoke
;
148 using testing::InvokeArgument
;
149 using testing::InvokeWithoutArgs
;
150 using testing::IsNull
;
153 using testing::Matcher
;
154 using testing::MatcherCast
;
155 using testing::NanSensitiveDoubleEq
;
156 using testing::NanSensitiveFloatEq
;
159 using testing::NotNull
;
160 using testing::Pointee
;
161 using testing::Property
;
163 using testing::ResultOf
;
164 using testing::Return
;
165 using testing::ReturnNull
;
166 using testing::ReturnRef
;
167 using testing::SetArgPointee
;
168 using testing::SetArrayArgument
;
169 using testing::StartsWith
;
170 using testing::StrCaseEq
;
171 using testing::StrCaseNe
;
172 using testing::StrEq
;
173 using testing::StrNe
;
174 using testing::Truly
;
175 using testing::TypedEq
;
176 using testing::WithArg
;
177 using testing::WithArgs
;
178 using testing::WithoutArgs
;
180 #if !GTEST_OS_WINDOWS_MOBILE
181 using testing::SetErrnoAndReturn
;
184 #if GTEST_HAS_EXCEPTIONS
185 using testing::Throw
;
188 using testing::ContainsRegex
;
189 using testing::MatchesRegex
;
193 virtual ~Interface() {}
194 virtual void VoidFromString(char* str
) = 0;
195 virtual char* StringFromString(char* str
) = 0;
196 virtual int IntFromString(char* str
) = 0;
197 virtual int& IntRefFromString(char* str
) = 0;
198 virtual void VoidFromFunc(void(*func
)(char* str
)) = 0;
199 virtual void VoidFromIntRef(int& n
) = 0; // NOLINT
200 virtual void VoidFromFloat(float n
) = 0;
201 virtual void VoidFromDouble(double n
) = 0;
202 virtual void VoidFromVector(const std::vector
<int>& v
) = 0;
205 class Mock
: public Interface
{
209 MOCK_METHOD1(VoidFromString
, void(char* str
));
210 MOCK_METHOD1(StringFromString
, char*(char* str
));
211 MOCK_METHOD1(IntFromString
, int(char* str
));
212 MOCK_METHOD1(IntRefFromString
, int&(char* str
));
213 MOCK_METHOD1(VoidFromFunc
, void(void(*func
)(char* str
)));
214 MOCK_METHOD1(VoidFromIntRef
, void(int& n
)); // NOLINT
215 MOCK_METHOD1(VoidFromFloat
, void(float n
));
216 MOCK_METHOD1(VoidFromDouble
, void(double n
));
217 MOCK_METHOD1(VoidFromVector
, void(const std::vector
<int>& v
));
220 GTEST_DISALLOW_COPY_AND_ASSIGN_(Mock
);
225 static void StaticVoidFromVoid() {}
226 void VoidFromVoid() {}
227 static void StaticVoidFromString(char* /* str */) {}
228 void VoidFromString(char* /* str */) {}
229 static int StaticIntFromString(char* /* str */) { return 1; }
230 static bool StaticBoolFromString(const char* /* str */) { return true; }
235 explicit FieldHelper(int a_field
) : field_(a_field
) {}
236 int field() const { return field_
; }
237 int field_
; // NOLINT -- need external access to field_ to test
238 // the Field matcher.
241 // Tests the linkage of the ReturnVoid action.
242 TEST(LinkTest
, TestReturnVoid
) {
245 EXPECT_CALL(mock
, VoidFromString(_
)).WillOnce(Return());
246 mock
.VoidFromString(NULL
);
249 // Tests the linkage of the Return action.
250 TEST(LinkTest
, TestReturn
) {
254 EXPECT_CALL(mock
, StringFromString(_
)).WillOnce(Return(&ch
));
255 mock
.StringFromString(NULL
);
258 // Tests the linkage of the ReturnNull action.
259 TEST(LinkTest
, TestReturnNull
) {
262 EXPECT_CALL(mock
, VoidFromString(_
)).WillOnce(Return());
263 mock
.VoidFromString(NULL
);
266 // Tests the linkage of the ReturnRef action.
267 TEST(LinkTest
, TestReturnRef
) {
271 EXPECT_CALL(mock
, IntRefFromString(_
)).WillOnce(ReturnRef(n
));
272 mock
.IntRefFromString(NULL
);
275 // Tests the linkage of the Assign action.
276 TEST(LinkTest
, TestAssign
) {
280 EXPECT_CALL(mock
, VoidFromString(_
)).WillOnce(Assign(&ch
, 'y'));
281 mock
.VoidFromString(NULL
);
284 // Tests the linkage of the SetArgPointee action.
285 TEST(LinkTest
, TestSetArgPointee
) {
289 EXPECT_CALL(mock
, VoidFromString(_
)).WillOnce(SetArgPointee
<0>('y'));
290 mock
.VoidFromString(&ch
);
293 // Tests the linkage of the SetArrayArgument action.
294 TEST(LinkTest
, TestSetArrayArgument
) {
299 EXPECT_CALL(mock
, VoidFromString(_
)).WillOnce(SetArrayArgument
<0>(&ch2
,
301 mock
.VoidFromString(&ch
);
304 #if !GTEST_OS_WINDOWS_MOBILE
306 // Tests the linkage of the SetErrnoAndReturn action.
307 TEST(LinkTest
, TestSetErrnoAndReturn
) {
310 int saved_errno
= errno
;
311 EXPECT_CALL(mock
, IntFromString(_
)).WillOnce(SetErrnoAndReturn(1, -1));
312 mock
.IntFromString(NULL
);
316 #endif // !GTEST_OS_WINDOWS_MOBILE
318 // Tests the linkage of the Invoke(function) and Invoke(object, method) actions.
319 TEST(LinkTest
, TestInvoke
) {
321 InvokeHelper test_invoke_helper
;
323 EXPECT_CALL(mock
, VoidFromString(_
))
324 .WillOnce(Invoke(&InvokeHelper::StaticVoidFromString
))
325 .WillOnce(Invoke(&test_invoke_helper
, &InvokeHelper::VoidFromString
));
326 mock
.VoidFromString(NULL
);
327 mock
.VoidFromString(NULL
);
330 // Tests the linkage of the InvokeWithoutArgs action.
331 TEST(LinkTest
, TestInvokeWithoutArgs
) {
333 InvokeHelper test_invoke_helper
;
335 EXPECT_CALL(mock
, VoidFromString(_
))
336 .WillOnce(InvokeWithoutArgs(&InvokeHelper::StaticVoidFromVoid
))
337 .WillOnce(InvokeWithoutArgs(&test_invoke_helper
,
338 &InvokeHelper::VoidFromVoid
));
339 mock
.VoidFromString(NULL
);
340 mock
.VoidFromString(NULL
);
343 // Tests the linkage of the InvokeArgument action.
344 TEST(LinkTest
, TestInvokeArgument
) {
348 EXPECT_CALL(mock
, VoidFromFunc(_
)).WillOnce(InvokeArgument
<0>(&ch
));
349 mock
.VoidFromFunc(InvokeHelper::StaticVoidFromString
);
352 // Tests the linkage of the WithArg action.
353 TEST(LinkTest
, TestWithArg
) {
356 EXPECT_CALL(mock
, VoidFromString(_
))
357 .WillOnce(WithArg
<0>(Invoke(&InvokeHelper::StaticVoidFromString
)));
358 mock
.VoidFromString(NULL
);
361 // Tests the linkage of the WithArgs action.
362 TEST(LinkTest
, TestWithArgs
) {
365 EXPECT_CALL(mock
, VoidFromString(_
))
366 .WillOnce(WithArgs
<0>(Invoke(&InvokeHelper::StaticVoidFromString
)));
367 mock
.VoidFromString(NULL
);
370 // Tests the linkage of the WithoutArgs action.
371 TEST(LinkTest
, TestWithoutArgs
) {
374 EXPECT_CALL(mock
, VoidFromString(_
)).WillOnce(WithoutArgs(Return()));
375 mock
.VoidFromString(NULL
);
378 // Tests the linkage of the DoAll action.
379 TEST(LinkTest
, TestDoAll
) {
383 EXPECT_CALL(mock
, VoidFromString(_
))
384 .WillOnce(DoAll(SetArgPointee
<0>('y'), Return()));
385 mock
.VoidFromString(&ch
);
388 // Tests the linkage of the DoDefault action.
389 TEST(LinkTest
, TestDoDefault
) {
393 ON_CALL(mock
, VoidFromString(_
)).WillByDefault(Return());
394 EXPECT_CALL(mock
, VoidFromString(_
)).WillOnce(DoDefault());
395 mock
.VoidFromString(&ch
);
398 // Tests the linkage of the IgnoreResult action.
399 TEST(LinkTest
, TestIgnoreResult
) {
402 EXPECT_CALL(mock
, VoidFromString(_
)).WillOnce(IgnoreResult(Return(42)));
403 mock
.VoidFromString(NULL
);
406 #if GTEST_HAS_EXCEPTIONS
407 // Tests the linkage of the Throw action.
408 TEST(LinkTest
, TestThrow
) {
411 EXPECT_CALL(mock
, VoidFromString(_
)).WillOnce(Throw(42));
412 EXPECT_THROW(mock
.VoidFromString(NULL
), int);
414 #endif // GTEST_HAS_EXCEPTIONS
416 // The ACTION*() macros trigger warning C4100 (unreferenced formal
417 // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
418 // the macro definition, as the warnings are generated when the macro
419 // is expanded and macro expansion cannot contain #pragma. Therefore
420 // we suppress them here.
422 # pragma warning(push)
423 # pragma warning(disable:4100)
426 // Tests the linkage of actions created using ACTION macro.
428 ACTION(Return1
) { return 1; }
431 TEST(LinkTest
, TestActionMacro
) {
434 EXPECT_CALL(mock
, IntFromString(_
)).WillOnce(Return1());
435 mock
.IntFromString(NULL
);
438 // Tests the linkage of actions created using ACTION_P macro.
440 ACTION_P(ReturnArgument
, ret_value
) { return ret_value
; }
443 TEST(LinkTest
, TestActionPMacro
) {
446 EXPECT_CALL(mock
, IntFromString(_
)).WillOnce(ReturnArgument(42));
447 mock
.IntFromString(NULL
);
450 // Tests the linkage of actions created using ACTION_P2 macro.
452 ACTION_P2(ReturnEqualsEitherOf
, first
, second
) {
453 return arg0
== first
|| arg0
== second
;
458 # pragma warning(pop)
461 TEST(LinkTest
, TestActionP2Macro
) {
465 EXPECT_CALL(mock
, IntFromString(_
))
466 .WillOnce(ReturnEqualsEitherOf("one", "two"));
467 mock
.IntFromString(&ch
);
470 // Tests the linkage of the "_" matcher.
471 TEST(LinkTest
, TestMatcherAnything
) {
474 ON_CALL(mock
, VoidFromString(_
)).WillByDefault(Return());
477 // Tests the linkage of the A matcher.
478 TEST(LinkTest
, TestMatcherA
) {
481 ON_CALL(mock
, VoidFromString(A
<char*>())).WillByDefault(Return());
484 // Tests the linkage of the Eq and the "bare value" matcher.
485 TEST(LinkTest
, TestMatchersEq
) {
489 ON_CALL(mock
, VoidFromString(Eq(p
))).WillByDefault(Return());
490 ON_CALL(mock
, VoidFromString(const_cast<char*>("y")))
491 .WillByDefault(Return());
494 // Tests the linkage of the Lt, Gt, Le, Ge, and Ne matchers.
495 TEST(LinkTest
, TestMatchersRelations
) {
498 ON_CALL(mock
, VoidFromFloat(Lt(1.0f
))).WillByDefault(Return());
499 ON_CALL(mock
, VoidFromFloat(Gt(1.0f
))).WillByDefault(Return());
500 ON_CALL(mock
, VoidFromFloat(Le(1.0f
))).WillByDefault(Return());
501 ON_CALL(mock
, VoidFromFloat(Ge(1.0f
))).WillByDefault(Return());
502 ON_CALL(mock
, VoidFromFloat(Ne(1.0f
))).WillByDefault(Return());
505 // Tests the linkage of the NotNull matcher.
506 TEST(LinkTest
, TestMatcherNotNull
) {
509 ON_CALL(mock
, VoidFromString(NotNull())).WillByDefault(Return());
512 // Tests the linkage of the IsNull matcher.
513 TEST(LinkTest
, TestMatcherIsNull
) {
516 ON_CALL(mock
, VoidFromString(IsNull())).WillByDefault(Return());
519 // Tests the linkage of the Ref matcher.
520 TEST(LinkTest
, TestMatcherRef
) {
524 ON_CALL(mock
, VoidFromIntRef(Ref(a
))).WillByDefault(Return());
527 // Tests the linkage of the TypedEq matcher.
528 TEST(LinkTest
, TestMatcherTypedEq
) {
532 ON_CALL(mock
, VoidFromIntRef(TypedEq
<int&>(a
))).WillByDefault(Return());
535 // Tests the linkage of the FloatEq, DoubleEq, NanSensitiveFloatEq and
536 // NanSensitiveDoubleEq matchers.
537 TEST(LinkTest
, TestMatchersFloatingPoint
) {
541 ON_CALL(mock
, VoidFromFloat(FloatEq(a
))).WillByDefault(Return());
542 ON_CALL(mock
, VoidFromDouble(DoubleEq(a
))).WillByDefault(Return());
543 ON_CALL(mock
, VoidFromFloat(NanSensitiveFloatEq(a
))).WillByDefault(Return());
544 ON_CALL(mock
, VoidFromDouble(NanSensitiveDoubleEq(a
)))
545 .WillByDefault(Return());
548 // Tests the linkage of the ContainsRegex matcher.
549 TEST(LinkTest
, TestMatcherContainsRegex
) {
552 ON_CALL(mock
, VoidFromString(ContainsRegex(".*"))).WillByDefault(Return());
555 // Tests the linkage of the MatchesRegex matcher.
556 TEST(LinkTest
, TestMatcherMatchesRegex
) {
559 ON_CALL(mock
, VoidFromString(MatchesRegex(".*"))).WillByDefault(Return());
562 // Tests the linkage of the StartsWith, EndsWith, and HasSubstr matchers.
563 TEST(LinkTest
, TestMatchersSubstrings
) {
566 ON_CALL(mock
, VoidFromString(StartsWith("a"))).WillByDefault(Return());
567 ON_CALL(mock
, VoidFromString(EndsWith("c"))).WillByDefault(Return());
568 ON_CALL(mock
, VoidFromString(HasSubstr("b"))).WillByDefault(Return());
571 // Tests the linkage of the StrEq, StrNe, StrCaseEq, and StrCaseNe matchers.
572 TEST(LinkTest
, TestMatchersStringEquality
) {
574 ON_CALL(mock
, VoidFromString(StrEq("a"))).WillByDefault(Return());
575 ON_CALL(mock
, VoidFromString(StrNe("a"))).WillByDefault(Return());
576 ON_CALL(mock
, VoidFromString(StrCaseEq("a"))).WillByDefault(Return());
577 ON_CALL(mock
, VoidFromString(StrCaseNe("a"))).WillByDefault(Return());
580 // Tests the linkage of the ElementsAre matcher.
581 TEST(LinkTest
, TestMatcherElementsAre
) {
584 ON_CALL(mock
, VoidFromVector(ElementsAre('a', _
))).WillByDefault(Return());
587 // Tests the linkage of the ElementsAreArray matcher.
588 TEST(LinkTest
, TestMatcherElementsAreArray
) {
590 char arr
[] = { 'a', 'b' };
592 ON_CALL(mock
, VoidFromVector(ElementsAreArray(arr
))).WillByDefault(Return());
595 // Tests the linkage of the ContainerEq matcher.
596 TEST(LinkTest
, TestMatcherContainerEq
) {
600 ON_CALL(mock
, VoidFromVector(ContainerEq(v
))).WillByDefault(Return());
603 // Tests the linkage of the Field matcher.
604 TEST(LinkTest
, TestMatcherField
) {
605 FieldHelper
helper(0);
607 Matcher
<const FieldHelper
&> m
= Field(&FieldHelper::field_
, Eq(0));
608 EXPECT_TRUE(m
.Matches(helper
));
610 Matcher
<const FieldHelper
*> m2
= Field(&FieldHelper::field_
, Eq(0));
611 EXPECT_TRUE(m2
.Matches(&helper
));
614 // Tests the linkage of the Property matcher.
615 TEST(LinkTest
, TestMatcherProperty
) {
616 FieldHelper
helper(0);
618 Matcher
<const FieldHelper
&> m
= Property(&FieldHelper::field
, Eq(0));
619 EXPECT_TRUE(m
.Matches(helper
));
621 Matcher
<const FieldHelper
*> m2
= Property(&FieldHelper::field
, Eq(0));
622 EXPECT_TRUE(m2
.Matches(&helper
));
625 // Tests the linkage of the ResultOf matcher.
626 TEST(LinkTest
, TestMatcherResultOf
) {
627 Matcher
<char*> m
= ResultOf(&InvokeHelper::StaticIntFromString
, Eq(1));
628 EXPECT_TRUE(m
.Matches(NULL
));
631 // Tests the linkage of the ResultOf matcher.
632 TEST(LinkTest
, TestMatcherPointee
) {
635 Matcher
<int*> m
= Pointee(Eq(1));
636 EXPECT_TRUE(m
.Matches(&n
));
639 // Tests the linkage of the Truly matcher.
640 TEST(LinkTest
, TestMatcherTruly
) {
641 Matcher
<const char*> m
= Truly(&InvokeHelper::StaticBoolFromString
);
642 EXPECT_TRUE(m
.Matches(NULL
));
645 // Tests the linkage of the AllOf matcher.
646 TEST(LinkTest
, TestMatcherAllOf
) {
647 Matcher
<int> m
= AllOf(_
, Eq(1));
648 EXPECT_TRUE(m
.Matches(1));
651 // Tests the linkage of the AnyOf matcher.
652 TEST(LinkTest
, TestMatcherAnyOf
) {
653 Matcher
<int> m
= AnyOf(_
, Eq(1));
654 EXPECT_TRUE(m
.Matches(1));
657 // Tests the linkage of the Not matcher.
658 TEST(LinkTest
, TestMatcherNot
) {
659 Matcher
<int> m
= Not(_
);
660 EXPECT_FALSE(m
.Matches(1));
663 // Tests the linkage of the MatcherCast<T>() function.
664 TEST(LinkTest
, TestMatcherCast
) {
665 Matcher
<const char*> m
= MatcherCast
<const char*>(_
);
666 EXPECT_TRUE(m
.Matches(NULL
));
669 #endif // GMOCK_TEST_GMOCK_LINK_TEST_H_