Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference to an array parameter in google test/mock framework

I have a member function with a reference to an array output parameter and I would like to use it in google mock, but it does not work.

The class:

class Class
{
  // returns: number of the rewritten elements in the array
  int foo(Struct (&bar)[ArraySize]) const;
};

The mock class:

class MockClass : public Class
{
  MOCK_CONST_METHOD1(foo, int(Struct (&)[ArraySize]));
};

When I want to use it I write the following:

ON_CALL(mMockClass, foo(_))
  .WillByDefault( DoAll( SetArgReferee<0>(mBar)
                       , Return(5)
                       )
                );

mBar is an appropriate array of Struct (Struct mBar[ArraySize];). When I compile it I get the following error message:

../../../../vendor/gmock-1.7.0/include/gmock/gmock-more-actions.h: In member
function ‘typename testing::internal::Function<F>::Result
testing::SetArgRefereeActionP<k,
value_type>::gmock_Impl<F>::gmock_PerformImpl(const typename
testing::internal::Function<F>::ArgumentTuple&, arg0_type, arg1_type,
arg2_type, arg3_type, arg4_type, arg5_type, arg6_type, arg7_type, arg8_type,
arg9_type) const [with arg0_type = Struct (&)[32], arg1_type =
testing::internal::ExcessiveArg, arg2_type = testing::internal::ExcessiveArg,
arg3_type = testing::internal::ExcessiveArg, arg4_type =
testing::internal::ExcessiveArg, arg5_type = testing::internal::ExcessiveArg,
arg6_type = testing::internal::ExcessiveArg, arg7_type =
testing::internal::ExcessiveArg, arg8_type = testing::internal::ExcessiveArg,
arg9_type = testing::internal::ExcessiveArg, F = void(Struct (&)[32]), int k =
0, value_type = Struct*]’:
../../../../vendor/gmock-1.7.0/include/gmock/gmock-generated-actions.h:655:
instantiated from ‘static Result testing::internal::ActionHelper<Result,
Impl>::Perform(Impl*, const std::tr1::tuple<A0>&) [with A0 = Struct (&)[32],
Result = void, Impl = testing::SetArgRefereeActionP<0,
Struct*>::gmock_Impl<void(Struct (&)[32])>]’
../../../../vendor/gmock-1.7.0/include/gmock/gmock-more-actions.h:170:
instantiated from ‘typename testing::internal::Function<F>::Result
testing::SetArgRefereeActionP<k, value_type>::gmock_Impl<F>::Perform(const
typename testing::internal::Function<F>::ArgumentTuple&) [with F = void(Struct
(&)[32]), int k = 0, value_type = Struct*]’
SomeTest.cpp:120:   instantiated from here
../../../../vendor/gmock-1.7.0/include/gmock/gmock-more-actions.h:177: error:
incompatible types in assignment of ‘Struct* const’ to ‘Struct [32]’
make: *** [SomeTest] Error 1

I am sorry for the error message format. As far as I understand the compiler's problem is it assumes a reference to an array but it gets a pointer. What did I wrong?

like image 806
Gabor Meszaros Avatar asked Oct 20 '22 14:10

Gabor Meszaros


1 Answers

UPDATE : Solution

For passing parameter of type array SetArrayArgument<N>(first, last)should be used.

Copies the elements in source range [first, last) to the array pointed to by the N-th (0-based) argument, which can be either a pointer or an iterator. The action does not take ownership of the elements in the source range

More on Google C++ Mocking Framework [here][1]

http://code.google.com/p/googlemock/wiki/CheatSheet

like image 119
deimus Avatar answered Oct 23 '22 15:10

deimus