From ed185f25268d8b689d110590a33a9a4a04841930 Mon Sep 17 00:00:00 2001 From: Anonymous Maarten Date: Fri, 17 Feb 2017 01:17:50 +0100 Subject: [PATCH] config: add tests on reading good and bad configuration files --- rwgame/GameConfig.cpp | 3 +- tests/CMakeLists.txt | 2 +- tests/test_config.cpp | 96 ++++++++++++++++++++++++++++++++++++++----- 3 files changed, 89 insertions(+), 12 deletions(-) diff --git a/rwgame/GameConfig.cpp b/rwgame/GameConfig.cpp index 32550434..8b483eaf 100644 --- a/rwgame/GameConfig.cpp +++ b/rwgame/GameConfig.cpp @@ -183,7 +183,8 @@ bool GameConfig::parseConfig( auto deft = StringTranslator(); auto boolt = BoolTranslator(); - //Add new configuration parameters here. + // Add new configuration parameters here. + // Additionally, add them to the unit test. // @todo Don't allow path separators and relative directories read_config("game.path", this->m_gamePath, "/opt/games/Grand Theft Auto 3", deft, false); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 35b9a32e..839dc190 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -8,7 +8,7 @@ else() add_definitions(-DRW_TEST_WITH_DATA=1) endif() -find_package(Boost COMPONENTS unit_test_framework REQUIRED) +find_package(Boost COMPONENTS filesystem unit_test_framework REQUIRED) set(TEST_SOURCES "main.cpp" diff --git a/tests/test_config.cpp b/tests/test_config.cpp index d49ac66e..3ec87ebc 100644 --- a/tests/test_config.cpp +++ b/tests/test_config.cpp @@ -1,28 +1,104 @@ #include + +#include #include + #include +#include + +namespace fs = boost::filesystem; + +typedef std::map> config_t; + +fs::path getRandomFilePath() { + return fs::unique_path(fs::temp_directory_path() /= "openrw_test_%%%%%%%%%%%%%%%%"); +} + +config_t getValidConfig() { + config_t result; + result["game"]["\tpath "] = "\t/dev/test \t \r\n"; + result["game"]["language\t "] = " american ;american english french german italian spanish."; + result["input"]["invert_y"] = "1 #values != 0 enable input inversion. Optional."; + return result; +} + +std::ostream &writeConfig(std::ostream &os, const config_t &config) { + for (auto §ion : config) { + os << "[" << section.first << "]" << "\n"; + for (auto &keyValue : section.second) { + os << keyValue.first << "=" << keyValue.second << "\n"; + } + } + return os; +} + +#include BOOST_AUTO_TEST_SUITE(ConfigTests) +BOOST_AUTO_TEST_CASE(test_config_valid) { + auto cfg = getValidConfig(); + auto configPath = getRandomFilePath(); -BOOST_AUTO_TEST_CASE(test_loading) { - // Write out a temporary file - std::ofstream test_config("/tmp/openrw_test.ini"); - test_config << "[game]\n" - << "\tpath=\t/dev/test\n" - << " language = american ;Comment\n" - << ";lineComment\n" - << "nonexistingkey=somevalue" - << std::endl; + std::ofstream ofs(configPath.string()); + writeConfig(ofs, cfg); + ofs.close(); - GameConfig config("openrw_test.ini", "/tmp"); + GameConfig config(configPath.filename().string(), + configPath.parent_path().string()); BOOST_CHECK(config.isValid()); BOOST_CHECK_EQUAL(config.getGameDataPath(), "/dev/test"); BOOST_CHECK_EQUAL(config.getGameLanguage(), "american"); + BOOST_CHECK_EQUAL(config.getInputInvertY(), true); +} + +BOOST_AUTO_TEST_CASE(test_config_valid_modified) { + auto cfg = getValidConfig(); + cfg["input"]["invert_y"] = "0"; + auto configPath = getRandomFilePath(); + + std::ofstream ofs(configPath.string()); + writeConfig(ofs, cfg); + ofs.close(); + + GameConfig config(configPath.filename().string(), + configPath.parent_path().string()); + + BOOST_CHECK(config.isValid()); + BOOST_CHECK_EQUAL(config.getInputInvertY(), false); } +BOOST_AUTO_TEST_CASE(test_config_invalid_duplicate) { + auto cfg = getValidConfig(); + cfg["input"]["invert_y "] = "0"; + auto configPath = getRandomFilePath(); + + std::ofstream ofs(configPath.string()); + writeConfig(ofs, cfg); + ofs.close(); + + GameConfig config(configPath.filename().string(), + configPath.parent_path().string()); + + BOOST_CHECK(!config.isValid()); +} + +BOOST_AUTO_TEST_CASE(test_config_invalid_empty) { + config_t cfg; + auto configPath = getRandomFilePath(); + std::cout << configPath.string() << "\n"; + + std::ofstream ofs(configPath.string()); + writeConfig(ofs, cfg); + ofs.close(); + + GameConfig config(configPath.filename().string(), + configPath.parent_path().string()); + + BOOST_CHECK(!config.isValid()); +} BOOST_AUTO_TEST_SUITE_END()