Improve YAML set helpers tests

This commit is contained in:
Oliver Hamlet
2016-03-24 11:53:31 +00:00
parent 5db35de967
commit 3d5b5677e6
4 changed files with 140 additions and 125 deletions
+1 -1
View File
@@ -243,7 +243,7 @@ set (LOOT_TESTS_HEADERS "${CMAKE_SOURCE_DIR}/src/tests/base_game_test.h"
"${CMAKE_SOURCE_DIR}/src/tests/backend/helpers/helpers_test.h"
"${CMAKE_SOURCE_DIR}/src/tests/backend/helpers/language_test.h"
"${CMAKE_SOURCE_DIR}/src/tests/backend/helpers/version_test.h"
"${CMAKE_SOURCE_DIR}/src/tests/backend/helpers/test_yaml_set_helpers.h"
"${CMAKE_SOURCE_DIR}/src/tests/backend/helpers/yaml_set_helpers_test.h"
"${CMAKE_SOURCE_DIR}/src/tests/backend/metadata/test_condition_grammar.h"
"${CMAKE_SOURCE_DIR}/src/tests/backend/metadata/test_conditional_metadata.h"
"${CMAKE_SOURCE_DIR}/src/tests/backend/metadata/test_file.h"
@@ -1,123 +0,0 @@
/* LOOT
A load order optimisation tool for Oblivion, Skyrim, Fallout 3 and
Fallout: New Vegas.
Copyright (C) 2014-2016 WrinklyNinja
This file is part of LOOT.
LOOT is free software: you can redistribute
it and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
LOOT is distributed in the hope that it will
be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with LOOT. If not, see
<http://www.gnu.org/licenses/>.
*/
#ifndef LOOT_TEST_BACKEND_HELPERS_YAML_SET_HELPERS
#define LOOT_TEST_BACKEND_HELPERS_YAML_SET_HELPERS
#include "backend/helpers/yaml_set_helpers.h"
#include "tests/fixtures.h"
namespace loot {
namespace test {
TEST(set, YamlEncode) {
YAML::Node node;
std::set<std::string> stringSet({"a", "b", "c"});
node = stringSet;
EXPECT_EQ("a", node[0].as<std::string>());
EXPECT_EQ("b", node[1].as<std::string>());
EXPECT_EQ("c", node[2].as<std::string>());
}
TEST(set, YamlDecode) {
YAML::Node node;
std::set<std::string> stringSet;
node = YAML::Load("[a, b, c]");
stringSet = node.as<std::set<std::string>>();
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) {
std::set<std::string> stringSet({"a", "b", "c"});
YAML::Emitter e1;
e1 << stringSet;
EXPECT_STREQ("- a\n- b\n- c", e1.c_str());
}
bool nodeContains(const std::string& value, const YAML::Node& node) {
for (const auto& element : node) {
if (element.as<std::string>() == value)
return true;
}
return false;
}
TEST(unordered_set, YamlEncode) {
YAML::Node node;
std::unordered_set<std::string> stringSet({"a", "b", "c"});
node = stringSet;
// Check that the values are all present in the node.
EXPECT_PRED2(nodeContains, "a", node);
EXPECT_PRED2(nodeContains, "b", node);
EXPECT_PRED2(nodeContains, "c", node);
}
TEST(unordered_set, YAMLDecode) {
YAML::Node node;
std::unordered_set<std::string> stringSet;
node = YAML::Load("[a, b, c]");
stringSet = node.as<std::unordered_set<std::string>>();
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,
const char val2,
const char val3,
const std::string& seq) {
if (seq.substr(0, 2) != "- "
|| seq.substr(3, 3) != "\n- "
|| seq.substr(7, 3) != "\n- ")
return false;
std::list<char> values({val1, val2, val3});
std::list<char> found({seq[2], seq[6], seq[10]});
values.sort();
found.sort();
return values == found;
}
TEST(unordered_set, YamlEmitter) {
std::unordered_set<std::string> stringSet({"a", "b", "c"});
YAML::Emitter e1;
e1 << stringSet;
ASSERT_EQ(11, e1.size());
EXPECT_PRED4(sequenceOf, 'a', 'b', 'c', e1.c_str());
}
}
}
#endif
@@ -0,0 +1,138 @@
/* LOOT
A load order optimisation tool for Oblivion, Skyrim, Fallout 3 and
Fallout: New Vegas.
Copyright (C) 2014-2016 WrinklyNinja
This file is part of LOOT.
LOOT is free software: you can redistribute
it and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
LOOT is distributed in the hope that it will
be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with LOOT. If not, see
<http://www.gnu.org/licenses/>.
*/
#ifndef LOOT_TEST_BACKEND_HELPERS_YAML_SET_HELPERS
#define LOOT_TEST_BACKEND_HELPERS_YAML_SET_HELPERS
#include "backend/helpers/yaml_set_helpers.h"
#include <gtest/gtest.h>
namespace loot {
namespace test {
TEST(set, encodingAsYamlShouldStoreAllValuesInSetOrder) {
std::set<std::string> stringSet({"a", "b", "c"});
YAML::Node node;
node = stringSet;
EXPECT_TRUE(node.IsSequence());
ASSERT_EQ(3, node.size());
EXPECT_EQ("a", node[0].as<std::string>());
EXPECT_EQ("b", node[1].as<std::string>());
EXPECT_EQ("c", node[2].as<std::string>());
}
TEST(set, decodingFromAYamlListShouldStoreValuesCorrectly) {
YAML::Node node = YAML::Load("[a, b, c]");
std::set<std::string> stringSet = node.as<std::set<std::string>>();
std::set<std::string> expectedStringSet({"a", "b", "c"});
EXPECT_EQ(expectedStringSet, stringSet);
}
TEST(set, decodingFromAYamlListThatContainsDuplicateElementsShouldThrow) {
YAML::Node node = YAML::Load("[a, b, c, c]");
EXPECT_ANY_THROW(node.as<std::set<std::string>>());
}
TEST(set, emittingAsYamlShouldOutputAYamlListContainingAllValues) {
std::set<std::string> stringSet({"a", "b", "c"});
YAML::Emitter e1;
e1 << stringSet;
YAML::Node node = YAML::Load(e1.c_str());
EXPECT_TRUE(node.IsSequence());
ASSERT_EQ(3, node.size());
EXPECT_EQ("a", node[0].as<std::string>());
EXPECT_EQ("b", node[1].as<std::string>());
EXPECT_EQ("c", node[2].as<std::string>());
}
class unordered_set : public ::testing::Test {
protected:
static bool nodeContains(const YAML::Node& node, const std::string& value) {
for (const auto& element : node) {
if (element.as<std::string>() == value)
return true;
}
return false;
}
static bool isSequenceOf(const std::string& sequence, const std::vector<std::string>& values) {
std::set<std::string> sortedValues(std::begin(values), std::end(values));
std::set<std::string> found;
size_t i = 2;
while (i < sequence.length()) {
size_t separatorPos = sequence.find("\n- ", i);
found.insert(sequence.substr(i, separatorPos));
}
return sortedValues == found;
}
};
TEST_F(unordered_set, encodingAsYamlShouldStoreAllValuesInUndefinedOrder) {
std::unordered_set<std::string> stringSet({"a", "b", "c"});
YAML::Node node;
node = stringSet;
EXPECT_TRUE(node.IsSequence());
ASSERT_EQ(3, node.size());
EXPECT_PRED2(&nodeContains, node, "a");
EXPECT_PRED2(&nodeContains, node, "b");
EXPECT_PRED2(&nodeContains, node, "c");
}
TEST_F(unordered_set, decodingFromAYamlListShouldStoreValuesCorrectly) {
YAML::Node node = YAML::Load("[a, b, c]");
std::unordered_set<std::string> stringSet = node.as<std::unordered_set<std::string>>();
EXPECT_EQ(1, stringSet.count("a"));
EXPECT_EQ(1, stringSet.count("b"));
EXPECT_EQ(1, stringSet.count("c"));
}
TEST_F(unordered_set, decodingFromAYamlListThatContainsDuplicateElementsShouldThrow) {
YAML::Node node = YAML::Load("[a, b, c, c]");
EXPECT_ANY_THROW(node.as<std::unordered_set<std::string>>());
}
TEST_F(unordered_set, emittingAsYamlShouldOutputAYamlListContainingAllValues) {
std::unordered_set<std::string> stringSet({"a", "b", "c"});
YAML::Emitter e1;
e1 << stringSet;
YAML::Node node = YAML::Load(e1.c_str());
EXPECT_TRUE(node.IsSequence());
ASSERT_EQ(3, node.size());
EXPECT_PRED2(&nodeContains, node, "a");
EXPECT_PRED2(&nodeContains, node, "b");
EXPECT_PRED2(&nodeContains, node, "c");
}
}
}
#endif
+1 -1
View File
@@ -49,7 +49,7 @@
#include "backend/helpers/helpers_test.h"
#include "backend/helpers/language_test.h"
#include "backend/helpers/version_test.h"
#include "backend/helpers/test_yaml_set_helpers.h"
#include "backend/helpers/yaml_set_helpers_test.h"
#include "backend/metadata/test_condition_grammar.h"
#include "backend/metadata/test_conditional_metadata.h"
#include "backend/metadata/test_file.h"