From 4aa59f05cc66c693bcd911a05ced7b77c701fc47 Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Fri, 26 Jun 2015 13:22:19 +0100 Subject: [PATCH] Added tests for File class. --- CMakeLists.txt | 5 +- src/tests/backend/metadata/test_file.h | 175 +++++++++++++++++++++++++ src/tests/main.cpp | 1 + 3 files changed, 179 insertions(+), 2 deletions(-) create mode 100644 src/tests/backend/metadata/test_file.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 2fd4e660..508e42f7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -156,7 +156,8 @@ set (LOOT_API_HEADERS ${LOOT_HEADERS} set (LOOT_TESTS_SRC "${CMAKE_SOURCE_DIR}/src/tests/main.cpp") set (LOOT_TESTS_HEADERS "${CMAKE_SOURCE_DIR}/src/tests/fixtures.h" - "${CMAKE_SOURCE_DIR}/src/tests/api/test_api.h") + "${CMAKE_SOURCE_DIR}/src/tests/api/test_api.h" + "${CMAKE_SOURCE_DIR}/src/tests/backend/metadata/test_file.h") source_group("Header Files" FILES ${LOOT_HEADERS} ${LOOT_GUI_HEADERS} ${LOOT_API_HEADERS} ${LOOT_TESTS_HEADERS}) @@ -256,7 +257,7 @@ target_link_libraries (loot${PROJECT_ARCH} ${Boost_LIBRARIES} ${YAML_CPP_LIBRARI IF (${GTEST_FOUND}) # Build tests. - add_executable(tests ${LOOT_TESTS_SRC} ${LOOT_TESTS_HEADERS}) + add_executable(tests ${LOOT_TESTS_SRC} ${LOOT_SRC} ${LOOT_TESTS_HEADERS}) target_link_libraries(tests loot${PROJECT_ARCH} ${Boost_LIBRARIES} ${YAML_CPP_LIBRARIES} ${LOOT_LIBS} ${GTEST_BOTH_LIBRARIES}) ENDIF () diff --git a/src/tests/backend/metadata/test_file.h b/src/tests/backend/metadata/test_file.h new file mode 100644 index 00000000..6977cbd7 --- /dev/null +++ b/src/tests/backend/metadata/test_file.h @@ -0,0 +1,175 @@ +/* LOOT + +A load order optimisation tool for Oblivion, Skyrim, Fallout 3 and +Fallout: New Vegas. + +Copyright (C) 2014-2015 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" + +using loot::File; + +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: condition1}"); + File file = node.as(); + EXPECT_EQ("name1", file.Name()); + EXPECT_EQ("display1", file.DisplayName()); + EXPECT_EQ("condition1", 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: condition1}"); + file = node.as(); + EXPECT_EQ("name1", file.Name()); + EXPECT_EQ("name1", file.DisplayName()); + EXPECT_EQ("condition1", file.Condition()); + + 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 c41c066e..1e50fbfc 100644 --- a/src/tests/main.cpp +++ b/src/tests/main.cpp @@ -23,6 +23,7 @@ */ #include "api/test_api.h" +#include "backend/metadata/test_file.h" int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv);