diff --git a/CMakeLists.txt b/CMakeLists.txt index dfa9192a..c730d9b5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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" diff --git a/src/tests/backend/helpers/test_yaml_set_helpers.h b/src/tests/backend/helpers/test_yaml_set_helpers.h deleted file mode 100644 index 296b3577..00000000 --- a/src/tests/backend/helpers/test_yaml_set_helpers.h +++ /dev/null @@ -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 -. -*/ - -#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 stringSet({"a", "b", "c"}); - node = stringSet; - EXPECT_EQ("a", node[0].as()); - EXPECT_EQ("b", node[1].as()); - EXPECT_EQ("c", node[2].as()); - } - - TEST(set, YamlDecode) { - YAML::Node node; - std::set stringSet; - - node = YAML::Load("[a, b, c]"); - stringSet = node.as>(); - 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>()); - } - - TEST(set, YamlEmitter) { - std::set 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() == value) - return true; - } - return false; - } - - TEST(unordered_set, YamlEncode) { - YAML::Node node; - std::unordered_set 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 stringSet; - - node = YAML::Load("[a, b, c]"); - stringSet = node.as>(); - 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>()); - } - - 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 values({val1, val2, val3}); - std::list found({seq[2], seq[6], seq[10]}); - - values.sort(); - found.sort(); - - return values == found; - } - - TEST(unordered_set, YamlEmitter) { - std::unordered_set stringSet({"a", "b", "c"}); - YAML::Emitter e1; - e1 << stringSet; - ASSERT_EQ(11, e1.size()); - EXPECT_PRED4(sequenceOf, 'a', 'b', 'c', e1.c_str()); - } - } -} - -#endif diff --git a/src/tests/backend/helpers/yaml_set_helpers_test.h b/src/tests/backend/helpers/yaml_set_helpers_test.h new file mode 100644 index 00000000..86361865 --- /dev/null +++ b/src/tests/backend/helpers/yaml_set_helpers_test.h @@ -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 +. +*/ + +#ifndef LOOT_TEST_BACKEND_HELPERS_YAML_SET_HELPERS +#define LOOT_TEST_BACKEND_HELPERS_YAML_SET_HELPERS + +#include "backend/helpers/yaml_set_helpers.h" + +#include + +namespace loot { + namespace test { + TEST(set, encodingAsYamlShouldStoreAllValuesInSetOrder) { + std::set stringSet({"a", "b", "c"}); + YAML::Node node; + node = stringSet; + + EXPECT_TRUE(node.IsSequence()); + ASSERT_EQ(3, node.size()); + EXPECT_EQ("a", node[0].as()); + EXPECT_EQ("b", node[1].as()); + EXPECT_EQ("c", node[2].as()); + } + + TEST(set, decodingFromAYamlListShouldStoreValuesCorrectly) { + YAML::Node node = YAML::Load("[a, b, c]"); + std::set stringSet = node.as>(); + std::set 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>()); + } + + TEST(set, emittingAsYamlShouldOutputAYamlListContainingAllValues) { + std::set 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()); + EXPECT_EQ("b", node[1].as()); + EXPECT_EQ("c", node[2].as()); + } + + 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() == value) + return true; + } + return false; + } + + static bool isSequenceOf(const std::string& sequence, const std::vector& values) { + std::set sortedValues(std::begin(values), std::end(values)); + + std::set 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 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 stringSet = node.as>(); + + 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>()); + } + + TEST_F(unordered_set, emittingAsYamlShouldOutputAYamlListContainingAllValues) { + std::unordered_set 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 diff --git a/src/tests/main.cpp b/src/tests/main.cpp index cd471d52..0ce51e9c 100644 --- a/src/tests/main.cpp +++ b/src/tests/main.cpp @@ -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"