Fix Struct::iterator test to be order agnostic

This commit is contained in:
Simon Hausmann 2021-03-23 17:49:28 +01:00
parent 715c9f407e
commit b51549ab58
2 changed files with 17 additions and 12 deletions

View file

@ -120,6 +120,8 @@ public:
/// references become invalid when the iterator or the Struct is changed, so make sure to make /// references become invalid when the iterator or the Struct is changed, so make sure to make
/// copies if you want to retain the name or value. /// copies if you want to retain the name or value.
/// ///
/// Note that the order in which the iterator exposes the fields is not defined.
///
/// If you're using C++ 17, you can use the convenience destructuring syntax to extract the name /// If you're using C++ 17, you can use the convenience destructuring syntax to extract the name
/// and value in one go: /// and value in one go:
/// ///

View file

@ -226,19 +226,22 @@ SCENARIO("Struct field iteration")
auto end = struc.end(); auto end = struc.end();
REQUIRE(it != end); REQUIRE(it != end);
{ auto check_valid_entry = [](const auto &key, const auto &value) -> bool {
auto [key, value] = *it; if (key == "field_a")
REQUIRE(key == "field_a"); return value == Value(true);
REQUIRE(value == Value(true)); if (key == "field_b")
return value == Value(42.0);
return false;
};
std::set<std::string> seen_fields;
for (; it != end; ++it) {
const auto [key, value] = *it;
REQUIRE(check_valid_entry(key, value));
auto [insert_it, value_inserted] = seen_fields.insert(std::string(key));
REQUIRE(value_inserted);
} }
++it;
{
auto [key, value] = *it;
REQUIRE(key == "field_b");
REQUIRE(value == Value(42.0));
}
++it;
REQUIRE(it == end);
} }
SCENARIO("Component Compiler") SCENARIO("Component Compiler")