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 // Google Mock - a framework for writing C++ mock classes.
32 // This file tests that:
33 // a. A header file defining a mock class can be included in multiple
34 // translation units without causing a link error.
35 // b. Actions and matchers can be instantiated with identical template
36 // arguments in different translation units without causing link
38 // The following constructs are currently tested:
49 // Invoke(object, method)
50 // InvokeWithoutArgs(function)
51 // InvokeWithoutArgs(object, method)
61 // ACTION_P()-generated
62 // ACTION_P2()-generated
74 // NanSensitiveDoubleEq
75 // 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 GOOGLEMOCK_TEST_GMOCK_LINK_TEST_H_
115 #define GOOGLEMOCK_TEST_GMOCK_LINK_TEST_H_
117 #include "gmock/gmock.h"
119 #if !GTEST_OS_WINDOWS_MOBILE
126 #include "gtest/gtest.h"
127 #include "gtest/internal/gtest-port.h"
131 using testing::Action;
132 using testing::AllOf;
133 using testing::AnyOf;
134 using testing::Assign;
135 using testing::ContainerEq;
136 using testing::DoAll;
137 using testing::DoDefault;
138 using testing::DoubleEq;
139 using testing::ElementsAre;
140 using testing::ElementsAreArray;
141 using testing::EndsWith;
143 using testing::Field;
144 using testing::FloatEq;
147 using testing::HasSubstr;
148 using testing::IgnoreResult;
149 using testing::Invoke;
150 using testing::InvokeArgument;
151 using testing::InvokeWithoutArgs;
152 using testing::IsNull;
153 using testing::IsSubsetOf;
154 using testing::IsSupersetOf;
157 using testing::Matcher;
158 using testing::MatcherCast;
159 using testing::NanSensitiveDoubleEq;
160 using testing::NanSensitiveFloatEq;
163 using testing::NotNull;
164 using testing::Pointee;
165 using testing::Property;
167 using testing::ResultOf;
168 using testing::Return;
169 using testing::ReturnNull;
170 using testing::ReturnRef;
171 using testing::SetArgPointee;
172 using testing::SetArrayArgument;
173 using testing::StartsWith;
174 using testing::StrCaseEq;
175 using testing::StrCaseNe;
176 using testing::StrEq;
177 using testing::StrNe;
178 using testing::Truly;
179 using testing::TypedEq;
180 using testing::WithArg;
181 using testing::WithArgs;
182 using testing::WithoutArgs;
184 #if !GTEST_OS_WINDOWS_MOBILE
185 using testing::SetErrnoAndReturn;
188 #if GTEST_HAS_EXCEPTIONS
189 using testing::Throw;
192 using testing::ContainsRegex;
193 using testing::MatchesRegex;
197 virtual ~Interface() {}
198 virtual void VoidFromString(char* str) = 0;
199 virtual char* StringFromString(char* str) = 0;
200 virtual int IntFromString(char* str) = 0;
201 virtual int& IntRefFromString(char* str) = 0;
202 virtual void VoidFromFunc(void (*func)(char* str)) = 0;
203 virtual void VoidFromIntRef(int& n) = 0; // NOLINT
204 virtual void VoidFromFloat(float n) = 0;
205 virtual void VoidFromDouble(double n) = 0;
206 virtual void VoidFromVector(const std::vector<int>& v) = 0;
209 class Mock : public Interface {
213 MOCK_METHOD1(VoidFromString, void(char* str));
214 MOCK_METHOD1(StringFromString, char*(char* str));
215 MOCK_METHOD1(IntFromString, int(char* str));
216 MOCK_METHOD1(IntRefFromString, int&(char* str));
217 MOCK_METHOD1(VoidFromFunc, void(void (*func)(char* str)));
218 MOCK_METHOD1(VoidFromIntRef, void(int& n)); // NOLINT
219 MOCK_METHOD1(VoidFromFloat, void(float n));
220 MOCK_METHOD1(VoidFromDouble, void(double n));
221 MOCK_METHOD1(VoidFromVector, void(const std::vector<int>& v));
224 Mock(const Mock&) = delete;
225 Mock& operator=(const Mock&) = delete;
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(_))
305 .WillOnce(SetArrayArgument<0>(&ch2, &ch2 + 1));
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))
343 InvokeWithoutArgs(&test_invoke_helper, &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;
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"))).WillByDefault(Return());
498 // Tests the linkage of the Lt, Gt, Le, Ge, and Ne matchers.
499 TEST(LinkTest, TestMatchersRelations) {
502 ON_CALL(mock, VoidFromFloat(Lt(1.0f))).WillByDefault(Return());
503 ON_CALL(mock, VoidFromFloat(Gt(1.0f))).WillByDefault(Return());
504 ON_CALL(mock, VoidFromFloat(Le(1.0f))).WillByDefault(Return());
505 ON_CALL(mock, VoidFromFloat(Ge(1.0f))).WillByDefault(Return());
506 ON_CALL(mock, VoidFromFloat(Ne(1.0f))).WillByDefault(Return());
509 // Tests the linkage of the NotNull matcher.
510 TEST(LinkTest, TestMatcherNotNull) {
513 ON_CALL(mock, VoidFromString(NotNull())).WillByDefault(Return());
516 // Tests the linkage of the IsNull matcher.
517 TEST(LinkTest, TestMatcherIsNull) {
520 ON_CALL(mock, VoidFromString(IsNull())).WillByDefault(Return());
523 // Tests the linkage of the Ref matcher.
524 TEST(LinkTest, TestMatcherRef) {
528 ON_CALL(mock, VoidFromIntRef(Ref(a))).WillByDefault(Return());
531 // Tests the linkage of the TypedEq matcher.
532 TEST(LinkTest, TestMatcherTypedEq) {
536 ON_CALL(mock, VoidFromIntRef(TypedEq<int&>(a))).WillByDefault(Return());
539 // Tests the linkage of the FloatEq, DoubleEq, NanSensitiveFloatEq and
540 // NanSensitiveDoubleEq matchers.
541 TEST(LinkTest, TestMatchersFloatingPoint) {
545 ON_CALL(mock, VoidFromFloat(FloatEq(a))).WillByDefault(Return());
546 ON_CALL(mock, VoidFromDouble(DoubleEq(a))).WillByDefault(Return());
547 ON_CALL(mock, VoidFromFloat(NanSensitiveFloatEq(a))).WillByDefault(Return());
548 ON_CALL(mock, VoidFromDouble(NanSensitiveDoubleEq(a)))
549 .WillByDefault(Return());
552 // Tests the linkage of the ContainsRegex matcher.
553 TEST(LinkTest, TestMatcherContainsRegex) {
556 ON_CALL(mock, VoidFromString(ContainsRegex(".*"))).WillByDefault(Return());
559 // Tests the linkage of the MatchesRegex matcher.
560 TEST(LinkTest, TestMatcherMatchesRegex) {
563 ON_CALL(mock, VoidFromString(MatchesRegex(".*"))).WillByDefault(Return());
566 // Tests the linkage of the StartsWith, EndsWith, and HasSubstr matchers.
567 TEST(LinkTest, TestMatchersSubstrings) {
570 ON_CALL(mock, VoidFromString(StartsWith("a"))).WillByDefault(Return());
571 ON_CALL(mock, VoidFromString(EndsWith("c"))).WillByDefault(Return());
572 ON_CALL(mock, VoidFromString(HasSubstr("b"))).WillByDefault(Return());
575 // Tests the linkage of the StrEq, StrNe, StrCaseEq, and StrCaseNe matchers.
576 TEST(LinkTest, TestMatchersStringEquality) {
578 ON_CALL(mock, VoidFromString(StrEq("a"))).WillByDefault(Return());
579 ON_CALL(mock, VoidFromString(StrNe("a"))).WillByDefault(Return());
580 ON_CALL(mock, VoidFromString(StrCaseEq("a"))).WillByDefault(Return());
581 ON_CALL(mock, VoidFromString(StrCaseNe("a"))).WillByDefault(Return());
584 // Tests the linkage of the ElementsAre matcher.
585 TEST(LinkTest, TestMatcherElementsAre) {
588 ON_CALL(mock, VoidFromVector(ElementsAre('a', _))).WillByDefault(Return());
591 // Tests the linkage of the ElementsAreArray matcher.
592 TEST(LinkTest, TestMatcherElementsAreArray) {
594 char arr[] = {'a', 'b'};
596 ON_CALL(mock, VoidFromVector(ElementsAreArray(arr))).WillByDefault(Return());
599 // Tests the linkage of the IsSubsetOf matcher.
600 TEST(LinkTest, TestMatcherIsSubsetOf) {
602 char arr[] = {'a', 'b'};
604 ON_CALL(mock, VoidFromVector(IsSubsetOf(arr))).WillByDefault(Return());
607 // Tests the linkage of the IsSupersetOf matcher.
608 TEST(LinkTest, TestMatcherIsSupersetOf) {
610 char arr[] = {'a', 'b'};
612 ON_CALL(mock, VoidFromVector(IsSupersetOf(arr))).WillByDefault(Return());
615 // Tests the linkage of the ContainerEq matcher.
616 TEST(LinkTest, TestMatcherContainerEq) {
620 ON_CALL(mock, VoidFromVector(ContainerEq(v))).WillByDefault(Return());
623 // Tests the linkage of the Field matcher.
624 TEST(LinkTest, TestMatcherField) {
625 FieldHelper helper(0);
627 Matcher<const FieldHelper&> m = Field(&FieldHelper::field_, Eq(0));
628 EXPECT_TRUE(m.Matches(helper));
630 Matcher<const FieldHelper*> m2 = Field(&FieldHelper::field_, Eq(0));
631 EXPECT_TRUE(m2.Matches(&helper));
634 // Tests the linkage of the Property matcher.
635 TEST(LinkTest, TestMatcherProperty) {
636 FieldHelper helper(0);
638 Matcher<const FieldHelper&> m = Property(&FieldHelper::field, Eq(0));
639 EXPECT_TRUE(m.Matches(helper));
641 Matcher<const FieldHelper*> m2 = Property(&FieldHelper::field, Eq(0));
642 EXPECT_TRUE(m2.Matches(&helper));
645 // Tests the linkage of the ResultOf matcher.
646 TEST(LinkTest, TestMatcherResultOf) {
647 Matcher<char*> m = ResultOf(&InvokeHelper::StaticIntFromString, Eq(1));
648 EXPECT_TRUE(m.Matches(nullptr));
651 // Tests the linkage of the ResultOf matcher.
652 TEST(LinkTest, TestMatcherPointee) {
655 Matcher<int*> m = Pointee(Eq(1));
656 EXPECT_TRUE(m.Matches(&n));
659 // Tests the linkage of the Truly matcher.
660 TEST(LinkTest, TestMatcherTruly) {
661 Matcher<const char*> m = Truly(&InvokeHelper::StaticBoolFromString);
662 EXPECT_TRUE(m.Matches(nullptr));
665 // Tests the linkage of the AllOf matcher.
666 TEST(LinkTest, TestMatcherAllOf) {
667 Matcher<int> m = AllOf(_, Eq(1));
668 EXPECT_TRUE(m.Matches(1));
671 // Tests the linkage of the AnyOf matcher.
672 TEST(LinkTest, TestMatcherAnyOf) {
673 Matcher<int> m = AnyOf(_, Eq(1));
674 EXPECT_TRUE(m.Matches(1));
677 // Tests the linkage of the Not matcher.
678 TEST(LinkTest, TestMatcherNot) {
679 Matcher<int> m = Not(_);
680 EXPECT_FALSE(m.Matches(1));
683 // Tests the linkage of the MatcherCast<T>() function.
684 TEST(LinkTest, TestMatcherCast) {
685 Matcher<const char*> m = MatcherCast<const char*>(_);
686 EXPECT_TRUE(m.Matches(nullptr));
689 #endif // GOOGLEMOCK_TEST_GMOCK_LINK_TEST_H_