Re: [googlemock: 1596] [Q] Creating a "side effect" for a void* type

1,038 views
Skip to first unread message

Vlad Losev

unread,
Jul 29, 2012, 11:09:00 AM7/29/12
to Google C++ Mocking Framework
Benjamin -

What are you trying to achieve? If you want to save for later inspection the pointer passed into your mock method, then the first snippet should work.

If you want to drop some values in the memory pointed by the parameter, then no standard actions will accomplish what you need because it has no type information. C++ is a strongly typed language and Google Mock will not cast void* to a pointer to a particular type for dereferencing it as it has no information of what the target type should be. You need to write a custom action to achieve that. E.g.:

ACTION_P(SetUnsignedShortValue, val) {
  *reinterpret_cast<unsigned_short*>(arg0) = val;
}

HTH,
Vlad

On Fri, Jul 27, 2012 at 8:36 AM, Brandon Berk <berk.b...@gmail.com> wrote:

We are trying to figure out how to create a "side effect" for a void* type.  Below is the Mock class.

class MockVisaWrapperImpl : public IVisaWrapper, public BrokeredObject

{

public:

 

                MOCK_METHOD3(GetAttribute, ViStatus(

                                int vi,

                                int attrName,

                                void* attrValue));

                                                ..

                                                ..

                                                ..

};

 

 

This is what we tried so far:

------------------------------------------------------------------------------

unsigned short sModelCode = 123;

void* test = &sModelCode;

EXPECT_CALL(*visa, viGetAttribute(_, VI_ATTR_MODEL_CODE, _)).WillOnce(

            DoAll(SaveArg<2>(&test), Return(0)));

 

This Compiles but the value is not set

 

------------------------------------------------------------------------------

unsigned short sModelCode = 123;

void* test = &sModelCode;

 

EXPECT_CALL(*visa, viGetAttribute(_, VI_ATTR_MODEL_CODE, _)).WillOnce(

            DoAll(SaveArgPointee<2>(test), Return(0)));

 

Compilation Error:

gmock-more-actions.h(163): error C2100: illegal indirection

gmock-more-actions.h(163): error C3490: 'pointer' cannot be modified because it is being accessed through a const object

 

------------------------------------------------------------------------------

unsigned short sModelCode = 123;

void* test = &sModelCode;

 

EXPECT_CALL(*visa, viGetAttribute(_, VI_ATTR_MODEL_CODE, _)).WillOnce(

     DoAll(SetArgPointee<2>(test), Return(0)));

 

Compilation Error:

gmock-actions.h(693): error C2100: illegal indirection

gmock-actions.h(693): error C3892: 'std::tr1::get' : you cannot assign to a variable that is const

 

Bellow is more detail information about the errors.

Thanks much,

Brandon Berk

 

*******************************************************************************************************************

EXPECT_CALL(*visa, viGetAttribute(_, VI_ATTR_MODEL_CODE, _)).WillOnce(

            DoAll(SaveArgPointee<2>(test), Return(0)));

 

3>c:\users\fandinoo\documents\lmsts_programs\irad\windows7\code\vendorlibraries\google\gmock-1.6.0\include\gmock\gmock-more-

 

actions.h(163): error C2100: illegal indirection

3>          c:\users\fandinoo\documents\lmsts_programs\irad\windows7\code\vendorlibraries\google\gmock-1.6.0\include\gmock

 

\gmock-generated-actions.h(674) : see reference to function template instantiation 'void

 

testing::SaveArgPointeeActionP<k,pointer_type>::gmock_Impl<F>::gmock_PerformImpl<A0,A1,A2,testing::internal::ExcessiveArg,tes

 

ting::internal::ExcessiveArg,testing::internal::ExcessiveArg,testing::internal::ExcessiveArg,testing::internal::ExcessiveArg,

 

testing::internal::ExcessiveArg,testing::internal::ExcessiveArg>(const std::tr1::tuple<_Arg0,_Arg1,_Arg2>

 

&,arg0_type,arg1_type,arg2_type,arg3_type,arg4_type,arg5_type,arg6_type,arg7_type,arg8_type,arg9_type) const' being compiled

3>          with

3>          [

3>              k=2,

3>              pointer_type=void *,

3>              F=void (ViObject,ViAttr,void *),

3>              A0=ViObject,

3>              A1=ViAttr,

3>              A2=void *,

3>              _Arg0=ViObject,

3>              _Arg1=ViAttr,

3>              _Arg2=void *,

3>              arg0_type=ViObject,

3>              arg1_type=ViAttr,

3>              arg2_type=void *,

3>              arg3_type=testing::internal::ExcessiveArg,

3>              arg4_type=testing::internal::ExcessiveArg,

3>              arg5_type=testing::internal::ExcessiveArg,

3>              arg6_type=testing::internal::ExcessiveArg,

3>              arg7_type=testing::internal::ExcessiveArg,

3>              arg8_type=testing::internal::ExcessiveArg,

3>              arg9_type=testing::internal::ExcessiveArg

3>          ]

3>          c:\users\fandinoo\documents\lmsts_programs\irad\windows7\code\vendorlibraries\google\gmock-1.6.0\include\gmock

 

\gmock-more-actions.h(162) : see reference to function template instantiation 'Result

 

testing::internal::ActionHelper<Result,Impl>::Perform<A1,A2,A3>(Impl *,const std::tr1::tuple<_Arg0,_Arg1,_Arg2> &)' being

 

compiled

3>          with

3>          [

3>              Result=void,

3>              Impl=testing::SaveArgPointeeActionP<2,void *>::gmock_Impl<void (ViObject,ViAttr,void *)>,

3>              A1=ViObject,

3>              A2=ViAttr,

3>              A3=void *,

3>              _Arg0=ViObject,

3>              _Arg1=ViAttr,

3>              _Arg2=void *

3>          ]

3>          c:\users\fandinoo\documents\lmsts_programs\irad\windows7\code\vendorlibraries\google\gmock-1.6.0\include\gmock

 

\gmock-more-actions.h(162) : while compiling class template member function 'void

 

testing::SaveArgPointeeActionP<k,pointer_type>::gmock_Impl<F>::Perform(const std::tr1::tuple<_Arg0,_Arg1,_Arg2> &)'

3>          with

3>          [

3>              k=2,

3>              pointer_type=void *,

3>              F=void (ViObject,ViAttr,void *),

3>              _Arg0=ViObject,

3>              _Arg1=ViAttr,

3>              _Arg2=void *

3>          ]

3>          c:\users\fandinoo\documents\lmsts_programs\irad\windows7\code\vendorlibraries\google\gmock-1.6.0\include\gmock

 

\gmock-more-actions.h(162) : see reference to class template instantiation

 

'testing::SaveArgPointeeActionP<k,pointer_type>::gmock_Impl<F>' being compiled

3>          with

3>          [

3>              k=2,

3>              pointer_type=void *,

3>              F=void (ViObject,ViAttr,void *)

3>          ]

3>          c:\users\fandinoo\documents\lmsts_programs\irad\windows7\code\vendorlibraries\google\gmock-1.6.0\include\gmock

 

\gmock-actions.h(861) : see reference to function template instantiation

 

'testing::SaveArgPointeeActionP<k,pointer_type>::operator testing::Action<F>(void) const<void(A1,A2,A3)>' being compiled

3>          with

3>          [

3>              k=2,

3>              pointer_type=void *,

3>              F=void (ViObject,ViAttr,void *),

3>              A1=ViObject,

3>              A2=ViAttr,

3>              A3=void *

3>          ]

3>          c:\users\fandinoo\documents\lmsts_programs\irad\windows7\code\testers\manager\unit\cratemanagerut

 

\cratemanagerut.cpp(99) : see reference to function template instantiation

 

'testing::internal::DoBothAction<Action1,Action2>::operator testing::Action<F>(void) const<F>' being compiled

3>          with

3>          [

3>              Action1=testing::SaveArgPointeeActionP<2,void *>,

3>              Action2=testing::internal::ReturnAction<int>,

3>              F=long (ViObject,ViAttr,void *)

3>          ]

3>c:\users\fandinoo\documents\lmsts_programs\irad\windows7\code\vendorlibraries\google\gmock-1.6.0\include\gmock\gmock-more-

 

actions.h(163): error C2100: illegal indirection

3>c:\users\fandinoo\documents\lmsts_programs\irad\windows7\code\vendorlibraries\google\gmock-1.6.0\include\gmock\gmock-more-

 

actions.h(163): error C3490: 'pointer' cannot be modified because it is being accessed through a const object

3>

3>Build FAILED.

3>

3>Time Elapsed 00:00:04.14

************************************************************************************************

EXPECT_CALL(*visa, viGetAttribute(_, VI_ATTR_MODEL_CODE, _)).WillOnce(

     DoAll(SetArgPointee<2>(test), Return(0)));

 

3>c:\users\fandinoo\documents\lmsts_programs\irad\windows7\code\vendorlibraries\google\gmock-1.6.0\include\gmock\gmock-

 

actions.h(693): error C2100: illegal indirection

3>          c:\users\fandinoo\documents\lmsts_programs\irad\windows7\code\vendorlibraries\google\gmock-1.6.0\include\gmock

 

\gmock-actions.h(369) : see reference to function template instantiation 'void

 

testing::internal::SetArgumentPointeeAction<N,A,kIsProto>::Perform<void,std::tr1::tuple<_Arg0,_Arg1,_Arg2>>(const

 

ArgumentTuple &) const' being compiled

3>          with

3>          [

3>              N=2,

3>              A=void *,

3>              kIsProto=false,

3>              _Arg0=ViObject,

3>              _Arg1=ViAttr,

3>              _Arg2=void *,

3>              ArgumentTuple=std::tr1::tuple<ViObject,ViAttr,void *>

3>          ]

3>          c:\users\fandinoo\documents\lmsts_programs\irad\windows7\code\vendorlibraries\google\gmock-1.6.0\include\gmock

 

\gmock-actions.h(368) : while compiling class template member function 'void

 

testing::PolymorphicAction<Impl>::MonomorphicImpl<F>::Perform(const std::tr1::tuple<_Arg0,_Arg1,_Arg2> &)'

3>          with

3>          [

3>              Impl=testing::internal::SetArgumentPointeeAction<2,void *,false>,

3>              F=void (ViObject,ViAttr,void *),

3>              _Arg0=ViObject,

3>              _Arg1=ViAttr,

3>              _Arg2=void *

3>          ]

3>          c:\users\fandinoo\documents\lmsts_programs\irad\windows7\code\vendorlibraries\google\gmock-1.6.0\include\gmock

 

\gmock-actions.h(356) : see reference to class template instantiation 'testing::PolymorphicAction<Impl>::MonomorphicImpl<F>'

 

being compiled

3>          with

3>          [

3>              Impl=testing::internal::SetArgumentPointeeAction<2,void *,false>,

3>              F=void (ViObject,ViAttr,void *)

3>          ]

3>          c:\users\fandinoo\documents\lmsts_programs\irad\windows7\code\vendorlibraries\google\gmock-1.6.0\include\gmock

 

\gmock-actions.h(861) : see reference to function template instantiation 'testing::PolymorphicAction<Impl>::operator

 

testing::Action<F>(void) const<void(A1,A2,A3)>' being compiled

3>          with

3>          [

3>              Impl=testing::internal::SetArgumentPointeeAction<2,void *,false>,

3>              F=void (ViObject,ViAttr,void *),

3>              A1=ViObject,

3>              A2=ViAttr,

3>              A3=void *

3>          ]

3>          c:\users\fandinoo\documents\lmsts_programs\irad\windows7\code\testers\manager\unit\cratemanagerut

 

\cratemanagerut.cpp(99) : see reference to function template instantiation

 

'testing::internal::DoBothAction<Action1,Action2>::operator testing::Action<F>(void) const<F>' being compiled

3>          with

3>          [

3>              Action1=testing::PolymorphicAction<testing::internal::SetArgumentPointeeAction<2,void *,false>>,

3>              Action2=testing::internal::ReturnAction<int>,

3>              F=long (ViObject,ViAttr,void *)

3>          ]

3>c:\users\fandinoo\documents\lmsts_programs\irad\windows7\code\vendorlibraries\google\gmock-1.6.0\include\gmock\gmock-

 

actions.h(693): error C3892: 'std::tr1::get' : you cannot assign to a variable that is const

3>

3>Build FAILED.


Reply all
Reply to author
Forward
0 new messages