2016-05-20 02:09:22 +01:00
|
|
|
#include <GameConfig.hpp>
|
2017-02-17 01:17:50 +01:00
|
|
|
|
|
|
|
#include <boost/filesystem.hpp>
|
2016-09-09 21:13:22 +01:00
|
|
|
#include <boost/test/unit_test.hpp>
|
2017-02-17 01:17:50 +01:00
|
|
|
|
2016-05-20 02:09:22 +01:00
|
|
|
#include <fstream>
|
2017-02-17 01:17:50 +01:00
|
|
|
#include <map>
|
|
|
|
|
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
|
2017-02-17 01:58:49 +01:00
|
|
|
typedef std::map<std::string, std::map<std::string, std::string>> simpleConfig_t;
|
2017-02-17 01:17:50 +01:00
|
|
|
|
|
|
|
fs::path getRandomFilePath() {
|
|
|
|
return fs::unique_path(fs::temp_directory_path() /= "openrw_test_%%%%%%%%%%%%%%%%");
|
|
|
|
}
|
|
|
|
|
2017-02-17 01:58:49 +01:00
|
|
|
simpleConfig_t getValidConfig() {
|
|
|
|
simpleConfig_t result;
|
|
|
|
// 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.";
|
2017-02-17 01:17:50 +01:00
|
|
|
result["input"]["invert_y"] = "1 #values != 0 enable input inversion. Optional.";
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-02-17 01:58:49 +01:00
|
|
|
std::ostream &writeConfig(std::ostream &os, const simpleConfig_t &config) {
|
2017-02-17 01:17:50 +01:00
|
|
|
for (auto §ion : config) {
|
|
|
|
os << "[" << section.first << "]" << "\n";
|
|
|
|
for (auto &keyValue : section.second) {
|
|
|
|
os << keyValue.first << "=" << keyValue.second << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2016-05-20 02:09:22 +01:00
|
|
|
BOOST_AUTO_TEST_SUITE(ConfigTests)
|
|
|
|
|
2017-02-17 01:17:50 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(test_config_valid) {
|
2017-02-17 01:58:49 +01:00
|
|
|
// Test reading a valid configuration file
|
2017-02-17 01:17:50 +01:00
|
|
|
auto cfg = getValidConfig();
|
|
|
|
auto configPath = getRandomFilePath();
|
2017-01-15 03:16:46 +01:00
|
|
|
|
2017-02-17 01:17:50 +01:00
|
|
|
std::ofstream ofs(configPath.string());
|
|
|
|
writeConfig(ofs, cfg);
|
|
|
|
ofs.close();
|
2016-05-20 02:09:22 +01:00
|
|
|
|
2017-02-17 01:17:50 +01:00
|
|
|
GameConfig config(configPath.filename().string(),
|
|
|
|
configPath.parent_path().string());
|
2016-05-20 02:09:22 +01:00
|
|
|
|
2016-09-09 21:13:22 +01:00
|
|
|
BOOST_CHECK(config.isValid());
|
2016-05-20 02:09:22 +01:00
|
|
|
|
2016-09-09 21:13:22 +01:00
|
|
|
BOOST_CHECK_EQUAL(config.getGameDataPath(), "/dev/test");
|
2017-01-06 22:45:25 +01:00
|
|
|
BOOST_CHECK_EQUAL(config.getGameLanguage(), "american");
|
2017-02-17 01:17:50 +01:00
|
|
|
BOOST_CHECK_EQUAL(config.getInputInvertY(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_config_valid_modified) {
|
2017-02-17 01:58:49 +01:00
|
|
|
// Test reading a valid modified configuration file
|
2017-02-17 01:17:50 +01:00
|
|
|
auto cfg = getValidConfig();
|
2017-02-17 01:58:49 +01:00
|
|
|
cfg["game"]["path"] = "Liberty City";
|
2017-02-17 01:17:50 +01:00
|
|
|
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());
|
|
|
|
|
2017-01-15 03:16:46 +01:00
|
|
|
BOOST_CHECK_EQUAL(config.getInputInvertY(), false);
|
2017-02-17 01:58:49 +01:00
|
|
|
BOOST_CHECK_EQUAL(config.getGameDataPath(), "Liberty City");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_config_save) {
|
|
|
|
// Test saving a configuration file
|
|
|
|
auto cfg = getValidConfig();
|
|
|
|
cfg["game"]["path"] = "Liberty City";
|
|
|
|
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());
|
|
|
|
|
|
|
|
fs::remove(configPath);
|
|
|
|
|
|
|
|
BOOST_CHECK(!fs::exists(configPath));
|
|
|
|
BOOST_CHECK(config.saveConfig());
|
|
|
|
BOOST_CHECK(fs::exists(configPath));
|
|
|
|
|
|
|
|
GameConfig config2(configPath.filename().string(),
|
|
|
|
configPath.parent_path().string());
|
|
|
|
|
|
|
|
BOOST_CHECK_EQUAL(config2.getGameDataPath(), "Liberty City");
|
2016-05-20 02:09:22 +01:00
|
|
|
}
|
|
|
|
|
2017-02-17 01:17:50 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(test_config_invalid_duplicate) {
|
2017-02-17 01:58:49 +01:00
|
|
|
// Test duplicate keys in invalid configuration file
|
2017-02-17 01:17:50 +01:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2017-02-17 01:58:49 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(test_config_invalid_required_missing) {
|
|
|
|
// Test missing required keys in invalid configuration file
|
|
|
|
auto cfg = getValidConfig();
|
|
|
|
cfg["game"].erase("path");
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2017-02-17 01:17:50 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(test_config_invalid_empty) {
|
2017-02-17 01:58:49 +01:00
|
|
|
// Test reading empty configuration file
|
|
|
|
simpleConfig_t cfg;
|
2017-02-17 01:17:50 +01:00
|
|
|
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());
|
|
|
|
}
|
2017-01-06 22:45:25 +01:00
|
|
|
|
2017-02-17 01:58:49 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(test_config_invalid_nonexisting) {
|
|
|
|
// Test reading non-existing configuration file
|
|
|
|
auto configPath = getRandomFilePath();
|
|
|
|
|
|
|
|
GameConfig config(configPath.filename().string(),
|
|
|
|
configPath.parent_path().string());
|
|
|
|
|
|
|
|
BOOST_CHECK(!config.isValid());
|
|
|
|
}
|
|
|
|
|
2016-05-20 02:09:22 +01:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|