curious_beast
curious_beast

Reputation: 85

How to check the structs(fields) stored in the vector using gtest constructs?

I want to check the validity of the structs stored in the container using gtest. One way of doing it is retrieving the struct and comparing with the expected value. Is there a better way other than this using gtest constructs.

auto receivedflexmessages = m_pugwintegration->getFlexMessages();
EXPECT_EQ(m_expectedflexmessages.size(), receivedflexmessages.size()) << "Mismatch in the received messages";

for (auto i = 0; i < receivedflexmessages.size(); i++)
{
    EXPECT_EQ(m_expectedflexmessages[i].m_extId, receivedflexmessages[i].m_extId) << i << "Mismatch in extId";
    EXPECT_EQ(m_expectedflexmessages[i].m_handle, receivedflexmessages[i].m_handle) << i << "Mismatch in handle";
    EXPECT_EQ(m_expectedflexmessages[i].m_payloadLength, receivedflexmessages[i].m_payloadLength) << i << "Mismatch in payloadlength";

    if (m_expectedflexmessages[i].m_payloadLength == receivedflexmessages[i].m_payloadLength)
    {
        for (auto j = 0; j < m_expectedflexmessages[i].m_payloadLength; ++j)
        {
            EXPECT_EQ(m_expectedflexmessages[i].m_pPayload[j], receivedflexmessages[i].m_pPayload[j]) << "Payload expected and received differ at index " << j;
        }
    }

Upvotes: 2

Views: 2691

Answers (1)

PiotrNycz
PiotrNycz

Reputation: 24412

There are two ways - both with use of gtest Container matchers:

  1. ::testing::ContainerEq with equality operator defined for container value_type

See:

using namespace ::testing;
EXPECT_THAT(receivedflexmessages, ContainerEq(m_expectedflexmessages));

// that is - just 1 line not counting operator == () 
// for decltype(m_expectedflexmessages)::value_type
  1. ::testing::ElementsAreArray and build vector of matchers on a top of m_expectedflexmessages

using namespace ::testing;
using ElementType = decltype(receivedflexmessages)::value_type;
std::vector<Matcher<ElementType >> m_expectedflexmessagesMatchers;

for (auto&& expectedMessage : m_expectedflexmessages)
{
    m_expectedflexmessagesMatchers.push_back(AllOf(
        Field(&ElementType::m_extId, expectedMessage.m_extId),
        Field(&ElementType::m_handle, expectedMessage.m_handle),
        Field(&ElementType::m_payloadLength, expectedMessage.m_payloadLength),
        Field(&ElementType::m_pPayload, ElementsAreArray(expectedMessage.m_pPayload, expectedMessage.m_payloadLength)));
}
EXPECT_THAT(receivedflexmessages, ElementsAreArray(m_expectedflexmessagesMatchers));


I do not advice to use the shortest form EXPECT_EQ(m_expectedflexmessages, receivedflexmessages) (assuming you have operator == for ElementType) - because you will lost information of first non-matching element index (and size) when test fails. With above mentioned matchers - this information will be present on the screen.

Additionally you might want to implement PrintTo(const ElementType&, std::ostream* os) { ... } to have even better presentation what fails to compare

Upvotes: 4

Related Questions