Question

I am writing some unit tests for some classes. These classes use a another one comms_client to perform network communication. For example, comms_clientprovides a send method which receives as parameter a boost::shared_array:

class comms_client
{
    ...

    public:
        void send(boost::shared_array<boost::uint8_t> const & buffer, size_t size) = 0;

    ...
};

In order to test what the classes are sending, I want to create a mock class that allows me to check if the content of the arrays being sent is correct. The mock class looks like

class comms_client_mock : public comms_client
{
    ...

    public:
        MOCK_METHOD2(send, void(boost::shared_array<boost::uint8_t> const & buffer, size_t size));

    ..
};

Unfortunately, I haven't found the way to place an expectation in a parameter with that kind of type. I tried using the testing::ElementsAreArraybut it raises a compilation error. Is there a function that can be used to place expectations in parameters with such types? Can you provide the gmock library with a custom predicate or class to compare the expected and the actual parameters?

Was it helpful?

Solution

After googling and reading some documentation, finally I got to the GoogleMock Cookbook. To write a simple custom matcher following the specifications and examples contained there is pretty straightforward.

This is the resulting matcher:

#include <gmock/gmock.h>

MATCHER_P2(CompareArray, expected, size, "The buffer doesn't match the expected value.")
{
    return std::equal(arg.get(), arg.get() + size, expected.get());
}

And this is how it would be used:

boost::shared_array<boost::uint8_t> buffer(new boost::uint8_t[1]);
buffer[0] = 0x01;
EXPECT_CALL(send(_camera.get(), CompareArray(buffer, 1), 1));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top