Googlemock: how to check elements in an array in an object?

I have a small class:

struct Command
{
    uint8_t cmdId;
    uint8_t len;
    uint8_t payload[MAX_PAYLOAD];
};

And I want to check only the first two elements of the payload using googlemock expectations. I cannot use ElementsAreArray because it checks that the payload lengths and expectations are the same. So far I have an expectation that looks like this:

Command cmd;
cmd.cmdId = 0xD3;
cmd.len = 2;
cmd.payload[0] = 'm';
cmd.payload[1] = 'l';

EXPECT_CALL(mockQueue,
        sendFromIsr(Pointee(AllOf(
                Field(&Command::cmdId, Eq(0xD3)),
                Field(&Command::len, Eq(2)),
                //Field(&BirdCommand::payload, ElementsAreArray(cmd.payload, 2)) !<-- Doesn't work
        ))))
        .WillOnce(Return(true));

Any ideas? The mock class is as follows:

template <typename T>
class MockQueue : public Queue<T>
{
public:
    MOCK_METHOD1_T(sendFromIsr, bool(T &item));
};
+3
source share
3 answers

I got this solution from Piotr Gorak via the googlemock mailing list, discussion here .

MATCHER_P2(CheckFirstTwo, first, second, "Checks the first two elements of an array")
{
    return arg[0] == first && arg[1] == second;
}

And in the test, I check like this:

EXPECT_CALL(mockQueue, sendFromIsr(
        Pointee(AllOf(
            Field(&BirdCommand::cmdId, Eq(0xD3)),
            Field(&BirdCommand::len, Eq(2)),
            Field(&BirdCommand::payload, CheckFirstTwo('m', 'l'))
        ))
));

, , .

+2

:

EXPECT_CALL( mockQueue, sendFromIsr( Ref( cmd ) )
        .WillOnce( Return( true ) );

?

POD, , ( ).


POD, ( , . :

Command cmd = Command();

, .

+2

You tried

EXPECT_CALL(mockQueue,
        sendFromIsr(Pointee(AllOf(
                Field(&Command::cmdId, Eq(0xD3)),
                Field(&Command::len, Eq(2)),
                Field(&Command::payload[0], Eq('m')),
                Field(&Command::payload[1], Eq('l'))
        ))))
        .WillOnce(Return(true));
-1
source

Source: https://habr.com/ru/post/1794713/


All Articles