Path: blob/main/contrib/googletest/googlemock/test/gmock_link_test.h
48254 views
// Copyright 2009, Google Inc.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above10// copyright notice, this list of conditions and the following disclaimer11// in the documentation and/or other materials provided with the12// distribution.13// * Neither the name of Google Inc. nor the names of its14// contributors may be used to endorse or promote products derived from15// this software without specific prior written permission.16//17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2829// Google Mock - a framework for writing C++ mock classes.30//31// This file tests that:32// a. A header file defining a mock class can be included in multiple33// translation units without causing a link error.34// b. Actions and matchers can be instantiated with identical template35// arguments in different translation units without causing link36// errors.37// The following constructs are currently tested:38// Actions:39// Return()40// Return(value)41// ReturnNull42// ReturnRef43// Assign44// SetArgPointee45// SetArrayArgument46// SetErrnoAndReturn47// Invoke(function)48// Invoke(object, method)49// InvokeWithoutArgs(function)50// InvokeWithoutArgs(object, method)51// InvokeArgument52// WithArg53// WithArgs54// WithoutArgs55// DoAll56// DoDefault57// IgnoreResult58// Throw59// ACTION()-generated60// ACTION_P()-generated61// ACTION_P2()-generated62// Matchers:63// _64// A65// An66// Eq67// Gt, Lt, Ge, Le, Ne68// NotNull69// Ref70// TypedEq71// DoubleEq72// FloatEq73// NanSensitiveDoubleEq74// NanSensitiveFloatEq75// ContainsRegex76// MatchesRegex77// EndsWith78// HasSubstr79// StartsWith80// StrCaseEq81// StrCaseNe82// StrEq83// StrNe84// ElementsAre85// ElementsAreArray86// ContainerEq87// Field88// Property89// ResultOf(function)90// ResultOf(callback)91// Pointee92// Truly(predicate)93// AddressSatisfies94// AllOf95// AnyOf96// Not97// MatcherCast<T>98//99// Please note: this test does not verify the functioning of these100// constructs, only that the programs using them will link successfully.101//102// Implementation note:103// This test requires identical definitions of Interface and Mock to be104// included in different translation units. We achieve this by writing105// them in this header and #including it in gmock_link_test.cc and106// gmock_link2_test.cc. Because the symbols generated by the compiler for107// those constructs must be identical in both translation units,108// definitions of Interface and Mock tests MUST be kept in the SAME109// NON-ANONYMOUS namespace in this file. The test fixture class LinkTest110// is defined as LinkTest1 in gmock_link_test.cc and as LinkTest2 in111// gmock_link2_test.cc to avoid producing linker errors.112113#ifndef GOOGLEMOCK_TEST_GMOCK_LINK_TEST_H_114#define GOOGLEMOCK_TEST_GMOCK_LINK_TEST_H_115116#include "gmock/gmock.h"117118#ifndef GTEST_OS_WINDOWS_MOBILE119#include <errno.h>120#endif121122#include <iostream>123#include <vector>124125#include "gtest/gtest.h"126#include "gtest/internal/gtest-port.h"127128using testing::_;129using testing::A;130using testing::Action;131using testing::AllOf;132using testing::AnyOf;133using testing::Assign;134using testing::ContainerEq;135using testing::DoAll;136using testing::DoDefault;137using testing::DoubleEq;138using testing::ElementsAre;139using testing::ElementsAreArray;140using testing::EndsWith;141using testing::Eq;142using testing::Field;143using testing::FloatEq;144using testing::Ge;145using testing::Gt;146using testing::HasSubstr;147using testing::IgnoreResult;148using testing::Invoke;149using testing::InvokeArgument;150using testing::InvokeWithoutArgs;151using testing::IsNull;152using testing::IsSubsetOf;153using testing::IsSupersetOf;154using testing::Le;155using testing::Lt;156using testing::Matcher;157using testing::MatcherCast;158using testing::NanSensitiveDoubleEq;159using testing::NanSensitiveFloatEq;160using testing::Ne;161using testing::Not;162using testing::NotNull;163using testing::Pointee;164using testing::Property;165using testing::Ref;166using testing::ResultOf;167using testing::Return;168using testing::ReturnNull;169using testing::ReturnRef;170using testing::SetArgPointee;171using testing::SetArrayArgument;172using testing::StartsWith;173using testing::StrCaseEq;174using testing::StrCaseNe;175using testing::StrEq;176using testing::StrNe;177using testing::Truly;178using testing::TypedEq;179using testing::WithArg;180using testing::WithArgs;181using testing::WithoutArgs;182183#ifndef GTEST_OS_WINDOWS_MOBILE184using testing::SetErrnoAndReturn;185#endif186187#if GTEST_HAS_EXCEPTIONS188using testing::Throw;189using testing::Rethrow;190#endif191192using testing::ContainsRegex;193using testing::MatchesRegex;194195class Interface {196public:197virtual ~Interface() = default;198virtual void VoidFromString(char* str) = 0;199virtual char* StringFromString(char* str) = 0;200virtual int IntFromString(char* str) = 0;201virtual int& IntRefFromString(char* str) = 0;202virtual void VoidFromFunc(void (*func)(char* str)) = 0;203virtual void VoidFromIntRef(int& n) = 0; // NOLINT204virtual void VoidFromFloat(float n) = 0;205virtual void VoidFromDouble(double n) = 0;206virtual void VoidFromVector(const std::vector<int>& v) = 0;207};208209class Mock : public Interface {210public:211Mock() = default;212213MOCK_METHOD1(VoidFromString, void(char* str));214MOCK_METHOD1(StringFromString, char*(char* str));215MOCK_METHOD1(IntFromString, int(char* str));216MOCK_METHOD1(IntRefFromString, int&(char* str));217MOCK_METHOD1(VoidFromFunc, void(void (*func)(char* str)));218MOCK_METHOD1(VoidFromIntRef, void(int& n)); // NOLINT219MOCK_METHOD1(VoidFromFloat, void(float n));220MOCK_METHOD1(VoidFromDouble, void(double n));221MOCK_METHOD1(VoidFromVector, void(const std::vector<int>& v));222223private:224Mock(const Mock&) = delete;225Mock& operator=(const Mock&) = delete;226};227228class InvokeHelper {229public:230static void StaticVoidFromVoid() {}231void VoidFromVoid() {}232static void StaticVoidFromString(char* /* str */) {}233void VoidFromString(char* /* str */) {}234static int StaticIntFromString(char* /* str */) { return 1; }235static bool StaticBoolFromString(const char* /* str */) { return true; }236};237238class FieldHelper {239public:240explicit FieldHelper(int a_field) : field_(a_field) {}241int field() const { return field_; }242int field_; // NOLINT -- need external access to field_ to test243// the Field matcher.244};245246// Tests the linkage of the ReturnVoid action.247TEST(LinkTest, TestReturnVoid) {248Mock mock;249250EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Return());251mock.VoidFromString(nullptr);252}253254// Tests the linkage of the Return action.255TEST(LinkTest, TestReturn) {256Mock mock;257char ch = 'x';258259EXPECT_CALL(mock, StringFromString(_)).WillOnce(Return(&ch));260mock.StringFromString(nullptr);261}262263// Tests the linkage of the ReturnNull action.264TEST(LinkTest, TestReturnNull) {265Mock mock;266267EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Return());268mock.VoidFromString(nullptr);269}270271// Tests the linkage of the ReturnRef action.272TEST(LinkTest, TestReturnRef) {273Mock mock;274int n = 42;275276EXPECT_CALL(mock, IntRefFromString(_)).WillOnce(ReturnRef(n));277mock.IntRefFromString(nullptr);278}279280// Tests the linkage of the Assign action.281TEST(LinkTest, TestAssign) {282Mock mock;283char ch = 'x';284285EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Assign(&ch, 'y'));286mock.VoidFromString(nullptr);287}288289// Tests the linkage of the SetArgPointee action.290TEST(LinkTest, TestSetArgPointee) {291Mock mock;292char ch = 'x';293294EXPECT_CALL(mock, VoidFromString(_)).WillOnce(SetArgPointee<0>('y'));295mock.VoidFromString(&ch);296}297298// Tests the linkage of the SetArrayArgument action.299TEST(LinkTest, TestSetArrayArgument) {300Mock mock;301char ch = 'x';302char ch2 = 'y';303304EXPECT_CALL(mock, VoidFromString(_))305.WillOnce(SetArrayArgument<0>(&ch2, &ch2 + 1));306mock.VoidFromString(&ch);307}308309#ifndef GTEST_OS_WINDOWS_MOBILE310311// Tests the linkage of the SetErrnoAndReturn action.312TEST(LinkTest, TestSetErrnoAndReturn) {313Mock mock;314315int saved_errno = errno;316EXPECT_CALL(mock, IntFromString(_)).WillOnce(SetErrnoAndReturn(1, -1));317mock.IntFromString(nullptr);318errno = saved_errno;319}320321#endif // !GTEST_OS_WINDOWS_MOBILE322323// Tests the linkage of the Invoke(function) and Invoke(object, method) actions.324TEST(LinkTest, TestInvoke) {325Mock mock;326InvokeHelper test_invoke_helper;327328EXPECT_CALL(mock, VoidFromString(_))329.WillOnce(Invoke(&InvokeHelper::StaticVoidFromString))330.WillOnce(Invoke(&test_invoke_helper, &InvokeHelper::VoidFromString));331mock.VoidFromString(nullptr);332mock.VoidFromString(nullptr);333}334335// Tests the linkage of the InvokeWithoutArgs action.336TEST(LinkTest, TestInvokeWithoutArgs) {337Mock mock;338InvokeHelper test_invoke_helper;339340EXPECT_CALL(mock, VoidFromString(_))341.WillOnce(InvokeWithoutArgs(&InvokeHelper::StaticVoidFromVoid))342.WillOnce(343InvokeWithoutArgs(&test_invoke_helper, &InvokeHelper::VoidFromVoid));344mock.VoidFromString(nullptr);345mock.VoidFromString(nullptr);346}347348// Tests the linkage of the InvokeArgument action.349TEST(LinkTest, TestInvokeArgument) {350Mock mock;351char ch = 'x';352353EXPECT_CALL(mock, VoidFromFunc(_)).WillOnce(InvokeArgument<0>(&ch));354mock.VoidFromFunc(InvokeHelper::StaticVoidFromString);355}356357// Tests the linkage of the WithArg action.358TEST(LinkTest, TestWithArg) {359Mock mock;360361EXPECT_CALL(mock, VoidFromString(_))362.WillOnce(WithArg<0>(Invoke(&InvokeHelper::StaticVoidFromString)));363mock.VoidFromString(nullptr);364}365366// Tests the linkage of the WithArgs action.367TEST(LinkTest, TestWithArgs) {368Mock mock;369370EXPECT_CALL(mock, VoidFromString(_))371.WillOnce(WithArgs<0>(Invoke(&InvokeHelper::StaticVoidFromString)));372mock.VoidFromString(nullptr);373}374375// Tests the linkage of the WithoutArgs action.376TEST(LinkTest, TestWithoutArgs) {377Mock mock;378379EXPECT_CALL(mock, VoidFromString(_)).WillOnce(WithoutArgs(Return()));380mock.VoidFromString(nullptr);381}382383// Tests the linkage of the DoAll action.384TEST(LinkTest, TestDoAll) {385Mock mock;386char ch = 'x';387388EXPECT_CALL(mock, VoidFromString(_))389.WillOnce(DoAll(SetArgPointee<0>('y'), Return()));390mock.VoidFromString(&ch);391}392393// Tests the linkage of the DoDefault action.394TEST(LinkTest, TestDoDefault) {395Mock mock;396char ch = 'x';397398ON_CALL(mock, VoidFromString(_)).WillByDefault(Return());399EXPECT_CALL(mock, VoidFromString(_)).WillOnce(DoDefault());400mock.VoidFromString(&ch);401}402403// Tests the linkage of the IgnoreResult action.404TEST(LinkTest, TestIgnoreResult) {405Mock mock;406407EXPECT_CALL(mock, VoidFromString(_)).WillOnce(IgnoreResult(Return(42)));408mock.VoidFromString(nullptr);409}410411#if GTEST_HAS_EXCEPTIONS412// Tests the linkage of the Throw action.413TEST(LinkTest, TestThrow) {414Mock mock;415416EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Throw(42));417EXPECT_THROW(mock.VoidFromString(nullptr), int);418}419// Tests the linkage of the Rethrow action.420TEST(LinkTest, TestRethrow) {421Mock mock;422423EXPECT_CALL(mock, VoidFromString(_))424.WillOnce(Rethrow(std::make_exception_ptr(42)));425EXPECT_THROW(mock.VoidFromString(nullptr), int);426}427#endif // GTEST_HAS_EXCEPTIONS428429// The ACTION*() macros trigger warning C4100 (unreferenced formal430// parameter) in MSVC with -W4. Unfortunately they cannot be fixed in431// the macro definition, as the warnings are generated when the macro432// is expanded and macro expansion cannot contain #pragma. Therefore433// we suppress them here.434GTEST_DISABLE_MSC_WARNINGS_PUSH_(4100)435436// Tests the linkage of actions created using ACTION macro.437namespace {438ACTION(Return1) { return 1; }439} // namespace440441TEST(LinkTest, TestActionMacro) {442Mock mock;443444EXPECT_CALL(mock, IntFromString(_)).WillOnce(Return1());445mock.IntFromString(nullptr);446}447448// Tests the linkage of actions created using ACTION_P macro.449namespace {450ACTION_P(ReturnArgument, ret_value) { return ret_value; }451} // namespace452453TEST(LinkTest, TestActionPMacro) {454Mock mock;455456EXPECT_CALL(mock, IntFromString(_)).WillOnce(ReturnArgument(42));457mock.IntFromString(nullptr);458}459460// Tests the linkage of actions created using ACTION_P2 macro.461namespace {462ACTION_P2(ReturnEqualsEitherOf, first, second) {463return arg0 == first || arg0 == second;464}465} // namespace466467GTEST_DISABLE_MSC_WARNINGS_POP_() // 4100468469TEST(LinkTest, TestActionP2Macro) {470Mock mock;471char ch = 'x';472473EXPECT_CALL(mock, IntFromString(_))474.WillOnce(ReturnEqualsEitherOf("one", "two"));475mock.IntFromString(&ch);476}477478// Tests the linkage of the "_" matcher.479TEST(LinkTest, TestMatcherAnything) {480Mock mock;481482ON_CALL(mock, VoidFromString(_)).WillByDefault(Return());483}484485// Tests the linkage of the A matcher.486TEST(LinkTest, TestMatcherA) {487Mock mock;488489ON_CALL(mock, VoidFromString(A<char*>())).WillByDefault(Return());490}491492// Tests the linkage of the Eq and the "bare value" matcher.493TEST(LinkTest, TestMatchersEq) {494Mock mock;495const char* p = "x";496497ON_CALL(mock, VoidFromString(Eq(p))).WillByDefault(Return());498ON_CALL(mock, VoidFromString(const_cast<char*>("y"))).WillByDefault(Return());499}500501// Tests the linkage of the Lt, Gt, Le, Ge, and Ne matchers.502TEST(LinkTest, TestMatchersRelations) {503Mock mock;504505ON_CALL(mock, VoidFromFloat(Lt(1.0f))).WillByDefault(Return());506ON_CALL(mock, VoidFromFloat(Gt(1.0f))).WillByDefault(Return());507ON_CALL(mock, VoidFromFloat(Le(1.0f))).WillByDefault(Return());508ON_CALL(mock, VoidFromFloat(Ge(1.0f))).WillByDefault(Return());509ON_CALL(mock, VoidFromFloat(Ne(1.0f))).WillByDefault(Return());510}511512// Tests the linkage of the NotNull matcher.513TEST(LinkTest, TestMatcherNotNull) {514Mock mock;515516ON_CALL(mock, VoidFromString(NotNull())).WillByDefault(Return());517}518519// Tests the linkage of the IsNull matcher.520TEST(LinkTest, TestMatcherIsNull) {521Mock mock;522523ON_CALL(mock, VoidFromString(IsNull())).WillByDefault(Return());524}525526// Tests the linkage of the Ref matcher.527TEST(LinkTest, TestMatcherRef) {528Mock mock;529int a = 0;530531ON_CALL(mock, VoidFromIntRef(Ref(a))).WillByDefault(Return());532}533534// Tests the linkage of the TypedEq matcher.535TEST(LinkTest, TestMatcherTypedEq) {536Mock mock;537long a = 0;538539ON_CALL(mock, VoidFromIntRef(TypedEq<int&>(a))).WillByDefault(Return());540}541542// Tests the linkage of the FloatEq, DoubleEq, NanSensitiveFloatEq and543// NanSensitiveDoubleEq matchers.544TEST(LinkTest, TestMatchersFloatingPoint) {545Mock mock;546float a = 0;547548ON_CALL(mock, VoidFromFloat(FloatEq(a))).WillByDefault(Return());549ON_CALL(mock, VoidFromDouble(DoubleEq(a))).WillByDefault(Return());550ON_CALL(mock, VoidFromFloat(NanSensitiveFloatEq(a))).WillByDefault(Return());551ON_CALL(mock, VoidFromDouble(NanSensitiveDoubleEq(a)))552.WillByDefault(Return());553}554555// Tests the linkage of the ContainsRegex matcher.556TEST(LinkTest, TestMatcherContainsRegex) {557Mock mock;558559ON_CALL(mock, VoidFromString(ContainsRegex(".*"))).WillByDefault(Return());560}561562// Tests the linkage of the MatchesRegex matcher.563TEST(LinkTest, TestMatcherMatchesRegex) {564Mock mock;565566ON_CALL(mock, VoidFromString(MatchesRegex(".*"))).WillByDefault(Return());567}568569// Tests the linkage of the StartsWith, EndsWith, and HasSubstr matchers.570TEST(LinkTest, TestMatchersSubstrings) {571Mock mock;572573ON_CALL(mock, VoidFromString(StartsWith("a"))).WillByDefault(Return());574ON_CALL(mock, VoidFromString(EndsWith("c"))).WillByDefault(Return());575ON_CALL(mock, VoidFromString(HasSubstr("b"))).WillByDefault(Return());576}577578// Tests the linkage of the StrEq, StrNe, StrCaseEq, and StrCaseNe matchers.579TEST(LinkTest, TestMatchersStringEquality) {580Mock mock;581ON_CALL(mock, VoidFromString(StrEq("a"))).WillByDefault(Return());582ON_CALL(mock, VoidFromString(StrNe("a"))).WillByDefault(Return());583ON_CALL(mock, VoidFromString(StrCaseEq("a"))).WillByDefault(Return());584ON_CALL(mock, VoidFromString(StrCaseNe("a"))).WillByDefault(Return());585}586587// Tests the linkage of the ElementsAre matcher.588TEST(LinkTest, TestMatcherElementsAre) {589Mock mock;590591ON_CALL(mock, VoidFromVector(ElementsAre('a', _))).WillByDefault(Return());592}593594// Tests the linkage of the ElementsAreArray matcher.595TEST(LinkTest, TestMatcherElementsAreArray) {596Mock mock;597char arr[] = {'a', 'b'};598599ON_CALL(mock, VoidFromVector(ElementsAreArray(arr))).WillByDefault(Return());600}601602// Tests the linkage of the IsSubsetOf matcher.603TEST(LinkTest, TestMatcherIsSubsetOf) {604Mock mock;605char arr[] = {'a', 'b'};606607ON_CALL(mock, VoidFromVector(IsSubsetOf(arr))).WillByDefault(Return());608}609610// Tests the linkage of the IsSupersetOf matcher.611TEST(LinkTest, TestMatcherIsSupersetOf) {612Mock mock;613char arr[] = {'a', 'b'};614615ON_CALL(mock, VoidFromVector(IsSupersetOf(arr))).WillByDefault(Return());616}617618// Tests the linkage of the ContainerEq matcher.619TEST(LinkTest, TestMatcherContainerEq) {620Mock mock;621std::vector<int> v;622623ON_CALL(mock, VoidFromVector(ContainerEq(v))).WillByDefault(Return());624}625626// Tests the linkage of the Field matcher.627TEST(LinkTest, TestMatcherField) {628FieldHelper helper(0);629630Matcher<const FieldHelper&> m = Field(&FieldHelper::field_, Eq(0));631EXPECT_TRUE(m.Matches(helper));632633Matcher<const FieldHelper*> m2 = Field(&FieldHelper::field_, Eq(0));634EXPECT_TRUE(m2.Matches(&helper));635}636637// Tests the linkage of the Property matcher.638TEST(LinkTest, TestMatcherProperty) {639FieldHelper helper(0);640641Matcher<const FieldHelper&> m = Property(&FieldHelper::field, Eq(0));642EXPECT_TRUE(m.Matches(helper));643644Matcher<const FieldHelper*> m2 = Property(&FieldHelper::field, Eq(0));645EXPECT_TRUE(m2.Matches(&helper));646}647648// Tests the linkage of the ResultOf matcher.649TEST(LinkTest, TestMatcherResultOf) {650Matcher<char*> m = ResultOf(&InvokeHelper::StaticIntFromString, Eq(1));651EXPECT_TRUE(m.Matches(nullptr));652}653654// Tests the linkage of the ResultOf matcher.655TEST(LinkTest, TestMatcherPointee) {656int n = 1;657658Matcher<int*> m = Pointee(Eq(1));659EXPECT_TRUE(m.Matches(&n));660}661662// Tests the linkage of the Truly matcher.663TEST(LinkTest, TestMatcherTruly) {664Matcher<const char*> m = Truly(&InvokeHelper::StaticBoolFromString);665EXPECT_TRUE(m.Matches(nullptr));666}667668// Tests the linkage of the AllOf matcher.669TEST(LinkTest, TestMatcherAllOf) {670Matcher<int> m = AllOf(_, Eq(1));671EXPECT_TRUE(m.Matches(1));672}673674// Tests the linkage of the AnyOf matcher.675TEST(LinkTest, TestMatcherAnyOf) {676Matcher<int> m = AnyOf(_, Eq(1));677EXPECT_TRUE(m.Matches(1));678}679680// Tests the linkage of the Not matcher.681TEST(LinkTest, TestMatcherNot) {682Matcher<int> m = Not(_);683EXPECT_FALSE(m.Matches(1));684}685686// Tests the linkage of the MatcherCast<T>() function.687TEST(LinkTest, TestMatcherCast) {688Matcher<const char*> m = MatcherCast<const char*>(_);689EXPECT_TRUE(m.Matches(nullptr));690}691692#endif // GOOGLEMOCK_TEST_GMOCK_LINK_TEST_H_693694695