1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-09-15 06:52:34 +02:00

config: fix TempFile.touch() + TempFile test + extra doc of getValidConfig

This commit is contained in:
Anonymous Maarten 2017-02-18 02:35:28 +01:00 committed by Daniel Evans
parent 90001b11ac
commit e0813e4378
2 changed files with 31 additions and 5 deletions

View File

@ -105,7 +105,7 @@ struct IntTranslator {
return res; return res;
} }
boost::optional<internal_type> put_value(const external_type &i) { boost::optional<internal_type> put_value(const external_type &i) {
return boost::optional<internal_type>(std::to_string(i)); return std::to_string(i);
} }
}; };
@ -190,7 +190,7 @@ bool GameConfig::parseConfig(
success = false; success = false;
break; break;
case ParseType::CONFIG: case ParseType::CONFIG:
//Don't care if success == false // Don't care if success == false
target = sourceValue; target = sourceValue;
break; break;
case ParseType::FILE: case ParseType::FILE:
@ -229,4 +229,3 @@ bool GameConfig::parseConfig(
return success; return success;
} }

View File

@ -12,6 +12,8 @@ typedef std::map<std::string, std::map<std::string, std::string>> simpleConfig_t
simpleConfig_t getValidConfig() { simpleConfig_t getValidConfig() {
simpleConfig_t result; 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. // 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"]["path"] = "\t/dev/test \t \r\n";
result["game"]["\tlanguage\t "] = " american ;american english french german italian spanish."; result["game"]["\tlanguage\t "] = " american ;american english french german italian spanish.";
@ -41,7 +43,7 @@ public:
fs::remove(this->m_path); fs::remove(this->m_path);
} }
void touch() { void touch() {
std::ofstream ofs(this->path()); std::ofstream ofs(this->path(), std::ios::out | std::ios::app);
ofs.close(); ofs.close();
} }
bool exists() { bool exists() {
@ -58,7 +60,9 @@ public:
} }
template<typename T> template<typename T>
void write(T t) { 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 << t;
ofs.close(); ofs.close();
} }
@ -71,6 +75,29 @@ private:
BOOST_AUTO_TEST_SUITE(ConfigTests) 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) { BOOST_AUTO_TEST_CASE(test_config_valid) {
// Test reading a valid configuration file // Test reading a valid configuration file
auto cfg = getValidConfig(); auto cfg = getValidConfig();