mercredi 26 mai 2021

how to validate map items with custom key using gtest?

I've written a custom key for my map

struct custom_key {
  string id;
  string sector;

  bool operator==(const custom_key& other) {
    // I'm needed by gtest
    return id == other.id && sector == other.sector;
  }

};

to that I've added the less then overload

namespace std
{
  template<> struct less<custom_key>
  {
    bool operator() (const custom_key& lhs, const custom_key& rhs) const
    {
      return lhs.id < rhs.id;
    }
  };
}

I've also defined my matcher

MATCHER_P(match_eq, value, "")
{
    return arg == value;
}

with that i've tried to write a test

EXPECT_CALL(
  *stats_,
  stats(
    AllOf(
      Field(&data,
        Contains(
          Pair(
            custom_key{"id", "name"},
            match_eq("expected_value")
          )
        )
      )
   )
);

I've run it against

std::map<custom_key, std::string> my_map = { {"id", "name"}, "expected_value" }

and gtest is saying he did not find the match. And I'm lost. I can not find debugging it any help due to extensive use of templates in impl of gtest. Any thoughts would be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire