mercredi 22 février 2023

googlemock expectation: Copy varying array contents to out parameter

Using google test, I want to set expectations (or default behavior) for a function that provides multiple data as out parameters, including a fixed-size array, to which a pointer is passed. Using SetArgPointee to set the out parameter won't work with an array, so my guess is that defining a custom parametrized action should work. I reduced the problem (and my attempt at a solution) to this:

#include "gtest/gtest.h"
#include "gmock/gmock.h"

class MyInterface {
  virtual bool GetFixedSizeArray(unsigned char* msg) = 0;
};

class MYMock : public MyInterface {
public: 
  MOCK_METHOD1(GetFixedSizeArray, bool(unsigned char* msg));
 };

ACTION_P(MyCopyAction, src) { for (int i = 0; i < 6; i++) arg0[i] = src[i]; }

using namespace ::testing;

TEST(Case1, Test1) {
  MYMock mock;

  unsigned char dummymsg[6] = { 1,2,3,4,5,6 };

  ON_CALL(mock, GetFixedSizeArray(_)).WillByDefault(DoAll(
    WithArg<1>(MyCopyAction(dummymsg)),
    Return(true)));

  // ... expectations and 
  // code under test

}

This version looks fine in VS2019 Intellisense, but it fails to compile with the message:

 error C2504: 'std::tr1::_Not_nil<std::tr1::_Nil>' : base class undefined

The ON_CALL should be able to take an argument for the array contents, so that it can change over multiple calls to GetFixedSizeArray. I am working on a legacy project with MSVC 2010, hence the googletest 1.1.8 syntax.

Any suggestions on how to do this?

Aucun commentaire:

Enregistrer un commentaire