Enforce unique elements in YAML sets.

If a YAML sequence containing non-uniue elements is decoded into a set
or unordered_set, it should fail.
This commit is contained in:
Oliver Hamlet
2015-07-16 11:59:12 +01:00
parent f701b94bda
commit 6e807f12bc
2 changed files with 10 additions and 2 deletions
+4 -2
View File
@@ -47,7 +47,8 @@ namespace YAML {
rhs.clear();
for (const auto &element : node) {
rhs.insert(element.template as<T>());
if (!rhs.insert(element.template as<T>()).second)
return false;
}
return true;
}
@@ -80,7 +81,8 @@ namespace YAML {
rhs.clear();
for (const auto &element : node) {
rhs.insert(element.template as<T>());
if (!rhs.insert(element.template as<T>()).second)
return false;
}
return true;
}
@@ -46,6 +46,9 @@ TEST(set, YamlDecode) {
EXPECT_EQ(stringSet.begin(), stringSet.find("a"));
EXPECT_EQ(++stringSet.begin(), stringSet.find("b"));
EXPECT_EQ(--stringSet.end(), stringSet.find("c"));
node = YAML::Load("[a, b, c, c]");
EXPECT_ANY_THROW(node.as<std::set<std::string>>());
}
TEST(set, YamlEmitter) {
@@ -82,6 +85,9 @@ TEST(unordered_set, YAMLDecode) {
EXPECT_EQ(1, stringSet.count("a"));
EXPECT_EQ(1, stringSet.count("b"));
EXPECT_EQ(1, stringSet.count("c"));
node = YAML::Load("[a, b, c, c]");
EXPECT_ANY_THROW(node.as<std::unordered_set<std::string>>());
}
bool sequenceOf(const char val1,