5

I want to pass a string "Device Name" to a void * pointer argument of a method and retrieve it to a character array later.

For this I've done as shown below.

Here I have created an action to achieve this.

ACTION_P(SetArg2ToChar, value) {*static_cast<char*>(arg2) = *value; }

Actual method to be called/mocked

bool getDictItem(WORD wIndex, BYTE bSubIndex, void * pObjData, DWORD dwLength, CSdo& sdo)

My mock method

  MOCK_METHOD5(getDictItem,
  bool(WORD wIndex, BYTE bSubIndex, void * pObjData, DWORD dwLength, CSdo& sdo));

in code it is called as

if( !can.getDictItem(wIndex, bSubIndex, pObjData, dwLength, tSdo) )

I want to pass a string to this pObjData (3rd argument in the list).

In my google tests, I'm doing like this.

char szDeviceName[30]= {0};
snprintf(szDeviceName, sizeof(szDeviceName), "%s", "Device Name" );

EXPECT_CALL( mockCan, getDictItem(_,_,_,_,_) )
            .WillOnce(DoAll(SetArg2ToChar(szDeviceName),
            Return(true)))
            .RetiresOnSaturation();

/*    Call a real method within which this mock method is called */

If I try to set this argument(pObjData) using "SetArgPointee<2>" directly, I get the below error.

error: 'void' is not a pointer-to-object type*

Hence I'm trying with ACTION_P

Now with this implementation, I only get the first letter of the szDeviceName variable (into this pObjData) i.e., "D" followed by 29 0's in the real code flow after this mock object is called.

I want to get the full string name set into this void * arguement.

I refered to this below question and was able progress this far. But I'm not able to pass the full string. How to set, in google mock, a void* argument to a set of values?

Any information regarding this will be helpful.

Community
  • 1
  • 1
Manjunath K Mayya
  • 1,078
  • 1
  • 11
  • 20

5 Answers5

1

Rather then doing that, you could invoke a function (or a method) and copy the parameter.

Something like this in the source file where the test is :

int invokedPObjData;

bool FakeGetDictItem(WORD wIndex, BYTE bSubIndex, void * pObjData, DWORD dwLength, CSdo& sdo)
{
  // copy data. here I assumed it is an int
  invokedPObjData = *static_cast< int* >( pObjData );

  return true; // or whatever makes sense
}

in test :

EXPECT_CALL( mockCan, getDictItem(_,_,_,_,_) )
            .WillOnce(Call(FakeGetDictItem))
            .RetiresOnSaturation();

then later in test check what needs to be checked.

BЈовић
  • 62,405
  • 41
  • 173
  • 273
1

The ACTION_P approach is basically OK. But as you are dealing with a C string, you can't just use the assignment operation (which just copies the first character) but instead you should use a string copy function like ACTION_P(SetArg2ToCharWithSizeArg3, value) { strcpy_s(static_cast<char*>(arg2), arg3, value); } (I couldn't resist to slightly rename the action).

rknuus
  • 118
  • 1
  • 9
0

I recently had a similar need and came up with this as a generic solution. It's based on the built-in SetArgPointee and has the same syntax:

template <size_t N, typename A>
class SetArgumentPointeeVoidAction {
public:
    explicit SetArgumentPointeeVoidAction(const A& value) : value_(value) {}
    void operator=(SetArgumentPointeeVoidAction const&) = delete;

    template <typename Result, typename ArgumentTuple>
    void Perform(const ArgumentTuple& args) const
    {
        ::testing::StaticAssertTypeEq<void, Result>();
        ::testing::StaticAssertTypeEq<void*,
            std::decay<decltype(::testing::get<N>(args))>::type>();
        *static_cast<A*>(::testing::get<N>(args)) = value_;
    }

private:
    const A value_;
};

/**
 * \brief  Sets a \c void* output argument to the contents of the
 *         supplied object.  It's on you to ensure this is safe.
 * \tparam N The argument index.
 * \tparam T The real argument type.
 * \param  x The argument to assign to the output argument.
 * \return A GMock Action that performs the requested assignment.
 * \note   Use \c SetArgPointee when it's not a \c void*.
 */
template <size_t N, typename T>
::testing::PolymorphicAction< SetArgumentPointeeVoidAction<N, T> >
SetArgPointeeVoid(const T& x)
{
    return ::testing::MakePolymorphicAction(
        SetArgumentPointeeVoidAction<N, T>(x));
}

It will give you a compile error if you try to use this on an argument that isn't a void*, so it should be relatively safe as long as you ensure you supply the correct argument.

It's also possible to implement this using ACTION_TEMPLATE, which is a bit shorter, but it generates unused argument warnings, which can be irritating.

(In older versions of GMock you might have to use ::std::tr1::get instead of ::testing::get.)

Left as an exercise for the reader: it's possible to enhance this with perfect forwarding to allow this to move-construct and move-assign for a slight efficiency boost. Although if you're passing anything other than PODs around as void*s then you're probably doing it wrong.

Miral
  • 12,637
  • 4
  • 53
  • 93
0

Here is an example using ACTION_TEMPLATE allowing a string to be assigned to a void *, for reference...

ACTION_TEMPLATE(StrCpyArgToVoidPointer,
                HAS_1_TEMPLATE_PARAMS(int, k),
                AND_2_VALUE_PARAMS(value, size))
{
    strncpy(static_cast<char *>(::testing::get<k>(args)), value, size);
    return;
}
Matt Budish
  • 127
  • 1
  • 4
0

Please find the steps to set Void Pointer variable in class using invoke Method.

//Actual Function under Test 
void testFunction(void)
{
   uint16 Frequency;
   uint8  PwmId;
   uint8  DutyCycle;

   Frequency = PORTEXTENDER_ZERO;
   PwmId = PORTEXTENDER_ZERO;
   DutyCycle = PORTEXTENDER_ZERO;

//for this mock is available  and we need to set value of Ex. PwmId 
   IncCom_ReceiveSignal(SCC_PORT_EXTENDER_PWM_C_SET_PWM_Id, &PwmId);

  if((PwmId <= PORTEXTENDER_NUM_PWM_CHANNELS) && (DutyCycle <= 100))
  {
  }

 // Actual Defination of mock ..where we have to set void pointer

 void mock_defination(PortExtender_C_SignalId_e SignalId, void* SignalDataPtr)
{

}

//cpp Test class 

class testClass : public :: testing :: Test 
{
           protected:
           /* Fixture tear down */
                      virtual void TearDown()
                      {

                      }

                      uint8 testval1{1};

           public:
                      void setTestvalue(uint8 val) // if we want to set multiple time
                      {
                                 testval1= val;
                      }
                      void test1(PortExtender_C_SignalId_e SignalId, void* SignalDataPtr) //this is method to invoke from test_F
                      {
                                 * (uint8*)SignalDataPtr =testval1;
                      }              
}

//Test Case 
TEST_F(testClass,
testcase_PortExtender_CompStatusResponse_ifConditionSatisfied)
{
setTestvalue(1); //To Set Value 
EXPECT_CALL(m_portExtender_SomeIpMock,m_C_Signal(SCC_PORT_EXTENDER_C_COMPONENT_STATUS_HostApplStat,_))
           .WillOnce(Invoke(this,&testClass::test1));
}
David Buck
  • 3,752
  • 35
  • 31
  • 35
Digambar
  • 11
  • 3