c++pointersgooglemockbit-fields

Use of gmock Field matcher with bitfields


I am trying to create a gmock matcher for a structure which contains bitfields:

struct s {
    uint32_t x;
    uint32_t y : 12;
    uint32_t z : 20;
};

I can create a testing::Field(&s::x, expected_x); just fine, but testing::Field(&s::y, expected_y); fails:

error: invalid pointer to bit-field 's::y'

Is there any good way around this? I have already tried and ruled out the following:

  1. testing::Truly - does not print why predicate failed to match if it did
  2. EXPECT_EQ in WillOnce/WillRepeatedly - does not prevent expect from matching wrong calls

I cannot modify the struct s to introduce some getters, as it is part of C API.


Solution

  • Re-reading matchers reference, I found ResultOf which works with a lambda and provides a nice output on error:

    testing::ResultOf("y", [](const auto& s) { return s.y; }, expected_y);
    
    Expected arg #0: whose y is equal to 3
             Actual: whose y is 7