From e0813e4378db9392f9c4822736b4df38337a2509 Mon Sep 17 00:00:00 2001 From: Anonymous Maarten Date: Sat, 18 Feb 2017 02:35:28 +0100 Subject: [PATCH] config: fix TempFile.touch() + TempFile test + extra doc of getValidConfig --- rwgame/GameConfig.cpp | 5 ++--- tests/test_config.cpp | 31 +++++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/rwgame/GameConfig.cpp b/rwgame/GameConfig.cpp index 12f451b7..de6f48ab 100644 --- a/rwgame/GameConfig.cpp +++ b/rwgame/GameConfig.cpp @@ -105,7 +105,7 @@ struct IntTranslator { return res; } boost::optional put_value(const external_type &i) { - return boost::optional(std::to_string(i)); + return std::to_string(i); } }; @@ -190,7 +190,7 @@ bool GameConfig::parseConfig( success = false; break; case ParseType::CONFIG: - //Don't care if success == false + // Don't care if success == false target = sourceValue; break; case ParseType::FILE: @@ -229,4 +229,3 @@ bool GameConfig::parseConfig( return success; } - diff --git a/tests/test_config.cpp b/tests/test_config.cpp index ecbce384..8f52cfd9 100644 --- a/tests/test_config.cpp +++ b/tests/test_config.cpp @@ -12,6 +12,8 @@ typedef std::map> simpleConfig_t simpleConfig_t getValidConfig() { simpleConfig_t result; + // Some values and subkeys are surrounded by whitespace + // to test the robustness of the INI parser. // Don't change game.path and input.invert_y keys. Tests depend on them. result["game"]["path"] = "\t/dev/test \t \r\n"; result["game"]["\tlanguage\t "] = " american ;american english french german italian spanish."; @@ -41,7 +43,7 @@ public: fs::remove(this->m_path); } void touch() { - std::ofstream ofs(this->path()); + std::ofstream ofs(this->path(), std::ios::out | std::ios::app); ofs.close(); } bool exists() { @@ -58,7 +60,9 @@ public: } template void write(T t) { - std::ofstream ofs(this->path()); + // Append argument at the end of the file. + // File is open/closes repeatedly. Not optimal. + std::ofstream ofs(this->path(), std::ios::out | std::ios::app); ofs << t; ofs.close(); } @@ -71,6 +75,29 @@ private: BOOST_AUTO_TEST_SUITE(ConfigTests) +BOOST_AUTO_TEST_CASE(test_TempFile) { + // Check the behavior of TempFile + TempFile tempFile; + BOOST_CHECK_EQUAL(tempFile.exists(), false); + tempFile.touch(); + BOOST_CHECK_EQUAL(tempFile.exists(), true); + tempFile.remove(); + BOOST_CHECK_EQUAL(tempFile.exists(), false); + + tempFile.touch(); + BOOST_CHECK_EQUAL(tempFile.exists(), true); + tempFile.remove(); + + tempFile.write("abc"); + tempFile.write("def"); + BOOST_CHECK_EQUAL(tempFile.exists(), true); + tempFile.touch(); + std::ifstream ifs(tempFile.path()); + std::string line; + std::getline(ifs, line); + BOOST_CHECK_EQUAL(line, "abcdef"); +} + BOOST_AUTO_TEST_CASE(test_config_valid) { // Test reading a valid configuration file auto cfg = getValidConfig();