diff --git a/CMakeLists.txt b/CMakeLists.txt index 2efc94d4..3bcf781b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -246,7 +246,7 @@ set (LOOT_TESTS_HEADERS "${CMAKE_SOURCE_DIR}/src/tests/base_game_test.h" "${CMAKE_SOURCE_DIR}/src/tests/backend/helpers/yaml_set_helpers_test.h" "${CMAKE_SOURCE_DIR}/src/tests/backend/metadata/condition_grammar_test.h" "${CMAKE_SOURCE_DIR}/src/tests/backend/metadata/conditional_metadata_test.h" - "${CMAKE_SOURCE_DIR}/src/tests/backend/metadata/test_file.h" + "${CMAKE_SOURCE_DIR}/src/tests/backend/metadata/file_test.h" "${CMAKE_SOURCE_DIR}/src/tests/backend/metadata/test_location.h" "${CMAKE_SOURCE_DIR}/src/tests/backend/metadata/test_message.h" "${CMAKE_SOURCE_DIR}/src/tests/backend/metadata/test_message_content.h" diff --git a/src/tests/backend/metadata/file_test.h b/src/tests/backend/metadata/file_test.h new file mode 100644 index 00000000..0b54013e --- /dev/null +++ b/src/tests/backend/metadata/file_test.h @@ -0,0 +1,186 @@ +/* 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_METADATA_FILE +#define LOOT_TEST_BACKEND_METADATA_FILE + +#include "backend/metadata/file.h" + +#include + +namespace loot { + namespace test { + TEST(File, defaultConstructorShouldInitialiseEmptyStrings) { + File file; + + EXPECT_EQ("", file.Name()); + EXPECT_EQ("", file.DisplayName()); + EXPECT_EQ("", file.Condition()); + } + + TEST(File, stringsConstructorShouldStoreGivenStrings) { + File file("name", "display", "condition"); + + EXPECT_EQ("name", file.Name()); + EXPECT_EQ("display", file.DisplayName()); + EXPECT_EQ("condition", file.Condition()); + } + + TEST(File, filesWithCaseInsensitiveEqualNameStringsShouldBeEqual) { + File file1("name", "display1", "condition1"); + File file2("Name", "display2", "condition2"); + + EXPECT_TRUE(file1 == file2); + } + + TEST(File, filesWithDifferentNamesShouldBeUnequal) { + File file1("name1"); + File file2("name2"); + + EXPECT_FALSE(file1 == file2); + } + + TEST(File, lessThanOperatorShouldUseCaseInsensitiveLexicographicalNameComparison) { + File file1("name", "display1", "condition1"); + File file2("Name", "display2", "condition2"); + + EXPECT_FALSE(file1 < file2); + EXPECT_FALSE(file2 < file1); + + file1 = File("name1"); + file2 = File("name2"); + + EXPECT_TRUE(file1 < file2); + EXPECT_FALSE(file2 < file1); + } + + TEST(File, emittingAsYamlShouldSingleQuoteValues) { + File file("name1", "display1", "condition1"); + YAML::Emitter emitter; + emitter << file; + std::string expected = "name: '" + file.Name() + + "'\ncondition: '" + file.Condition() + + "'\ndisplay: '" + file.DisplayName() + "'"; + + EXPECT_EQ(expected, emitter.c_str()); + } + + TEST(File, emittingAsYamlShouldOutputAsAScalarIfOnlyTheNameStringIsNotEmpty) { + File file("name1"); + YAML::Emitter emitter; + emitter << file; + + EXPECT_EQ("'" + file.Name() + "'", emitter.c_str()); + } + + TEST(File, emittingAsYamlShouldOmitDisplayFieldIfItMatchesTheNameField) { + File file("name1", "name1"); + YAML::Emitter emitter; + emitter << file; + + EXPECT_EQ("'" + file.Name() + "'", emitter.c_str()); + } + + TEST(File, emittingAsYamlShouldOmitAnEmptyConditionString) { + File file("name1", "display1"); + YAML::Emitter emitter; + emitter << file; + std::string expected = "name: '" + file.Name() + + "'\ndisplay: '" + file.DisplayName() + "'"; + + EXPECT_EQ(expected, emitter.c_str()); + } + + TEST(File, encodingAsYamlShouldStoreDataCorrectly) { + File file("name1", "display1", "condition1"); + YAML::Node node; + node = file; + + EXPECT_EQ(file.Name(), node["name"].as()); + EXPECT_EQ(file.DisplayName(), node["display"].as()); + EXPECT_EQ(file.Condition(), node["condition"].as()); + } + + TEST(File, encodingAsYamlShouldOmitEmptyFields) { + File file("name1"); + YAML::Node node; + node = file; + + EXPECT_EQ(file.Name(), node["name"].as()); + EXPECT_FALSE(node["display"]); + EXPECT_FALSE(node["condition"]); + } + + TEST(File, encodingAsYamlShouldOmitDisplayFieldIfItMatchesTheNameField) { + File file("name1", "name1"); + YAML::Node node; + node = file; + + EXPECT_EQ(file.Name(), node["name"].as()); + EXPECT_FALSE(node["display"]); + EXPECT_FALSE(node["condition"]); + } + + TEST(File, decodingFromYamlShouldSetDataCorrectly) { + YAML::Node node = YAML::Load("{name: name1, display: display1, condition: 'file(\"Foo.esp\")'}"); + File file = node.as(); + + EXPECT_EQ(node["name"].as(), file.Name()); + EXPECT_EQ(node["display"].as(), file.DisplayName()); + EXPECT_EQ(node["condition"].as(), file.Condition()); + } + + TEST(File, decodingFromYamlWithMissingConditionFieldShouldLeaveConditionStringEmpty) { + YAML::Node node = YAML::Load("{name: name1, display: display1}"); + File file = node.as(); + + EXPECT_EQ(node["name"].as(), file.Name()); + EXPECT_EQ(node["display"].as(), file.DisplayName()); + EXPECT_TRUE(file.Condition().empty()); + } + + TEST(File, decodingFromYamlScalarShouldUseNameValueForDisplayNameAndLeaveConditionEmpty) { + YAML::Node node = YAML::Load("name1"); + File file = node.as(); + + EXPECT_EQ(node.as(), file.Name()); + EXPECT_EQ(node.as(), file.DisplayName()); + EXPECT_TRUE(file.Condition().empty()); + } + + TEST(File, decodingFromYamlShouldThrowIfAnInvalidMapIsGiven) { + YAML::Node node = YAML::Load("{name: name1, condition: invalid}"); + + EXPECT_THROW(node.as(), YAML::RepresentationException); + } + + TEST(File, decodingFromYamlShouldThrowIfAListIsGiven) { + YAML::Node node = YAML::Load("[0, 1, 2]"); + + EXPECT_ANY_THROW(node.as()); + } + } +} + +#endif diff --git a/src/tests/backend/metadata/test_file.h b/src/tests/backend/metadata/test_file.h deleted file mode 100644 index d9addd36..00000000 --- a/src/tests/backend/metadata/test_file.h +++ /dev/null @@ -1,180 +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_METADATA_FILE -#define LOOT_TEST_BACKEND_METADATA_FILE - -#include "backend/metadata/file.h" -#include "tests/fixtures.h" - -namespace loot { - namespace test { - TEST(File, ConstructorsAndDataAccess) { - File file; - EXPECT_EQ("", file.Name()); - EXPECT_EQ("", file.DisplayName()); - EXPECT_EQ("", file.Condition()); - - file = File("name"); - EXPECT_EQ("name", file.Name()); - EXPECT_EQ("name", file.DisplayName()); - EXPECT_EQ("", file.Condition()); - - file = File("name", "display"); - EXPECT_EQ("name", file.Name()); - EXPECT_EQ("display", file.DisplayName()); - EXPECT_EQ("", file.Condition()); - - // Not a valid condition, but not evaluating it in this test. - file = File("name", "display", "condition"); - EXPECT_EQ("name", file.Name()); - EXPECT_EQ("display", file.DisplayName()); - EXPECT_EQ("condition", file.Condition()); - } - - TEST(File, EqualityOperator) { - File file1, file2; - EXPECT_TRUE(file1 == file2); - - // Not valid conditions, but not evaluating them in this test. - file1 = File("name", "display1", "condition1"); - file2 = File("name", "display2", "condition2"); - EXPECT_TRUE(file1 == file2); - - file1 = File("name1"); - file2 = File("name2"); - EXPECT_FALSE(file1 == file2); - } - - TEST(File, LessThanOperator) { - File file1, file2; - EXPECT_FALSE(file1 < file2); - EXPECT_FALSE(file2 < file1); - - file1 = File("name", "display1", "condition1"); - file2 = File("name", "display2", "condition2"); - EXPECT_FALSE(file1 < file2); - EXPECT_FALSE(file2 < file1); - - file1 = File("name1"); - file2 = File("name2"); - EXPECT_TRUE(file1 < file2); - EXPECT_FALSE(file2 < file1); - } - - TEST(File, YamlEmitter) { - File file("name1", "display1", "condition1"); - - YAML::Emitter e1; - e1 << file; - EXPECT_STREQ("name: 'name1'\ncondition: 'condition1'\ndisplay: 'display1'", e1.c_str()); - - file = File("name1"); - YAML::Emitter e2; - e2 << file; - EXPECT_STREQ("'name1'", e2.c_str()); - - file = File("name1", "", "condition1"); - YAML::Emitter e3; - e3 << file; - EXPECT_STREQ("name: 'name1'\ncondition: 'condition1'", e3.c_str()); - - file = File("name1", "display1"); - YAML::Emitter e4; - e4 << file; - EXPECT_STREQ("name: 'name1'\ndisplay: 'display1'", e4.c_str()); - - file = File("name1", "name1"); - YAML::Emitter e5; - e5 << file; - EXPECT_STREQ("'name1'", e5.c_str()); - - file = File("name1", "name1", "condition1"); - YAML::Emitter e6; - e6 << file; - EXPECT_STREQ("name: 'name1'\ncondition: 'condition1'", e6.c_str()); - } - - TEST(File, YamlEncode) { - File file("name1", "display1", "condition1"); - YAML::Node node; - node = file; - EXPECT_EQ("name1", node["name"].as()); - EXPECT_EQ("display1", node["display"].as()); - EXPECT_EQ("condition1", node["condition"].as()); - - file = File("name1"); - node = file; - EXPECT_EQ("name1", node["name"].as()); - EXPECT_FALSE(node["display"]); - EXPECT_FALSE(node["condition"]); - - file = File("name1", "name1"); - node = file; - EXPECT_EQ("name1", node["name"].as()); - EXPECT_FALSE(node["display"]); - EXPECT_FALSE(node["condition"]); - - file = File("name1", "display1"); - node = file; - EXPECT_EQ("name1", node["name"].as()); - EXPECT_EQ("display1", node["display"].as()); - EXPECT_FALSE(node["condition"]); - } - - TEST(File, YamlDecode) { - YAML::Node node = YAML::Load("{name: name1, display: display1, condition: 'file(\"Foo.esp\")'}"); - File file = node.as(); - EXPECT_EQ("name1", file.Name()); - EXPECT_EQ("display1", file.DisplayName()); - EXPECT_EQ("file(\"Foo.esp\")", file.Condition()); - - node = YAML::Load("name1"); - file = node.as(); - EXPECT_EQ("name1", file.Name()); - EXPECT_EQ("name1", file.DisplayName()); - EXPECT_EQ("", file.Condition()); - - node = YAML::Load("{name: name1, display: display1}"); - file = node.as(); - EXPECT_EQ("name1", file.Name()); - EXPECT_EQ("display1", file.DisplayName()); - EXPECT_EQ("", file.Condition()); - - node = YAML::Load("{name: name1, condition: 'file(\"Foo.esp\")'}"); - file = node.as(); - EXPECT_EQ("name1", file.Name()); - EXPECT_EQ("name1", file.DisplayName()); - EXPECT_EQ("file(\"Foo.esp\")", file.Condition()); - - node = YAML::Load("{name: name1, condition: invalid}"); - EXPECT_THROW(node.as(), YAML::RepresentationException); - - node = YAML::Load("[0, 1, 2]"); - EXPECT_ANY_THROW(node.as()); - } - } -} - -#endif diff --git a/src/tests/main.cpp b/src/tests/main.cpp index 2b6621b1..9c03dfdf 100644 --- a/src/tests/main.cpp +++ b/src/tests/main.cpp @@ -52,7 +52,7 @@ #include "backend/helpers/yaml_set_helpers_test.h" #include "backend/metadata/condition_grammar_test.h" #include "backend/metadata/conditional_metadata_test.h" -#include "backend/metadata/test_file.h" +#include "backend/metadata/file_test.h" #include "backend/metadata/test_location.h" #include "backend/metadata/test_message.h" #include "backend/metadata/test_message_content.h"