I have the following struct :
struct can_frame {
canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */
__u8 can_dlc; /* frame payload length in byte (0 .. CAN_MAX_DLEN) */
__u8 __pad; /* padding */
__u8 __res0; /* reserved / padding */
__u8 __res1; /* reserved / padding */
__u8 data[CAN_MAX_DLEN] __attribute__((aligned(8)));
};
I pass the struct by reference to the mocked function :
MOCK_METHOD1(write, int(can_frame* frame));
I would like to match if the passes struct has a given number in the datafield:
EXPECT_CALL(*canSocketMock_, write(**if the value of can_frame->data[1] is equal to 10, else assert false**))).WillOnce(Return(16));
I tried to combine the Pointee, Field and ArrayElement matchers, but failed to create the thing I wanted. The syntax of matchers is a bit too confusing for me.
Edit: The test:
TEST_F(SchunkDeviceShould, applyBreakWritesRightMessage) {
ASSERT_NO_THROW(sut_.reset(new SchunkDevice(
canSocketMock_, 3)));
can_frame frame;
EXPECT_CALL(*canSocketMock_, write(FrameDataEquals(&frame, 1, CMD_STOP)))).WillOnce(Return(16));
ASSERT_TRUE(sut_->applyBreak());
}
The function we call:
bool SchunkDevice::applyBreak() {
can_frame frame;
frame.can_id = MESSAGE_TO_DEVICE+canID_;
frame.can_dlc = 0x02;
frame.data[0] = frame.can_dlc - 1;
frame.data[1] = CMD_STOP;
if (int len = socket_->write(&frame) != 16) {
return false;
}
return true;
}
Output of the test:
Unexpected mock function call - taking default action specified at:
/home/../SchunkDeviceTests.cpp:47:
Function call: write(0x7ffc27c4de60)
Returns: 16
Google Mock tried the following 1 expectation, but it didn't match:
/home/../SchunkDeviceTests.cpp:457: EXPECT_CALL(*canSocketMock_, write(FrameDataEquals(&frame, 1, CMD_STOP)))...
Expected arg #0: frame data equals (0x7ffc27c4def0, 1, 145)
Actual: 0x7ffc27c4de60
Expected: to be called once
Actual: never called - unsatisfied and active
You could define a custom matcher to examine the structure contents.
MATCHER_P2(FrameDataEquals, index, value, "") { return (arg->data[index] == value); }
You would then use it like this:
EXPECT_CALL(mock, write(FrameDataEquals(1, 10))).WillOnce(Return(16));