mirror of https://github.com/OpenMW/openmw.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
374 lines
11 KiB
C++
374 lines
11 KiB
C++
5 years ago
|
#include <components/settings/parser.hpp>
|
||
7 months ago
|
#include <components/testing/util.hpp>
|
||
5 years ago
|
|
||
3 years ago
|
#include <fstream>
|
||
5 years ago
|
|
||
|
#include <gtest/gtest.h>
|
||
|
|
||
|
namespace
|
||
|
{
|
||
|
using namespace testing;
|
||
|
using namespace Settings;
|
||
|
|
||
|
struct SettingsFileParserTest : Test
|
||
|
{
|
||
|
SettingsFileParser mLoader;
|
||
|
SettingsFileParser mSaver;
|
||
|
|
||
|
template <typename F>
|
||
2 years ago
|
void withSettingsFile(const std::string& content, F&& f)
|
||
5 years ago
|
{
|
||
3 years ago
|
auto path = TestingOpenMW::outputFilePath(
|
||
3 years ago
|
std::string(UnitTest::GetInstance()->current_test_info()->name()) + ".cfg");
|
||
5 years ago
|
|
||
|
{
|
||
3 years ago
|
std::ofstream stream(path);
|
||
5 years ago
|
stream << content;
|
||
|
stream.close();
|
||
|
}
|
||
|
|
||
|
f(path);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
TEST_F(SettingsFileParserTest, load_empty_file)
|
||
|
{
|
||
|
const std::string content;
|
||
|
|
||
2 years ago
|
withSettingsFile(content, [this](const auto& path) {
|
||
5 years ago
|
CategorySettingValueMap map;
|
||
|
mLoader.loadSettingsFile(path, map);
|
||
|
|
||
|
EXPECT_EQ(map, CategorySettingValueMap());
|
||
|
});
|
||
|
}
|
||
|
|
||
|
TEST_F(SettingsFileParserTest, file_with_single_empty_section)
|
||
|
{
|
||
2 years ago
|
const std::string content = "[Section]\n";
|
||
5 years ago
|
|
||
2 years ago
|
withSettingsFile(content, [this](const auto& path) {
|
||
5 years ago
|
CategorySettingValueMap map;
|
||
|
mLoader.loadSettingsFile(path, map);
|
||
|
|
||
|
EXPECT_EQ(map, CategorySettingValueMap());
|
||
|
});
|
||
|
}
|
||
|
|
||
|
TEST_F(SettingsFileParserTest, file_with_single_section_and_key)
|
||
|
{
|
||
2 years ago
|
const std::string content
|
||
|
= "[Section]\n"
|
||
|
"key = value\n";
|
||
5 years ago
|
|
||
2 years ago
|
withSettingsFile(content, [this](const auto& path) {
|
||
5 years ago
|
CategorySettingValueMap map;
|
||
|
mLoader.loadSettingsFile(path, map);
|
||
|
|
||
2 years ago
|
EXPECT_EQ(map, CategorySettingValueMap({ { CategorySetting("Section", "key"), "value" } }));
|
||
5 years ago
|
});
|
||
|
}
|
||
|
|
||
|
TEST_F(SettingsFileParserTest, file_with_single_section_and_key_and_line_comments)
|
||
|
{
|
||
2 years ago
|
const std::string content
|
||
|
= "# foo\n"
|
||
|
"[Section]\n"
|
||
|
"# bar\n"
|
||
|
"key = value\n"
|
||
|
"# baz\n";
|
||
|
|
||
|
withSettingsFile(content, [this](const auto& path) {
|
||
5 years ago
|
CategorySettingValueMap map;
|
||
|
mLoader.loadSettingsFile(path, map);
|
||
|
|
||
2 years ago
|
EXPECT_EQ(map, CategorySettingValueMap({ { CategorySetting("Section", "key"), "value" } }));
|
||
5 years ago
|
});
|
||
|
}
|
||
|
|
||
|
TEST_F(SettingsFileParserTest, file_with_single_section_and_key_file_and_inline_section_comment)
|
||
|
{
|
||
2 years ago
|
const std::string content
|
||
|
= "[Section] # foo\n"
|
||
|
"key = value\n";
|
||
5 years ago
|
|
||
2 years ago
|
withSettingsFile(content, [this](const auto& path) {
|
||
5 years ago
|
CategorySettingValueMap map;
|
||
|
EXPECT_THROW(mLoader.loadSettingsFile(path, map), std::runtime_error);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
TEST_F(SettingsFileParserTest, file_single_section_and_key_and_inline_key_comment)
|
||
|
{
|
||
2 years ago
|
const std::string content
|
||
|
= "[Section]\n"
|
||
|
"key = value # foo\n";
|
||
5 years ago
|
|
||
2 years ago
|
withSettingsFile(content, [this](const auto& path) {
|
||
5 years ago
|
CategorySettingValueMap map;
|
||
|
mLoader.loadSettingsFile(path, map);
|
||
|
|
||
2 years ago
|
EXPECT_EQ(map, CategorySettingValueMap({ { CategorySetting("Section", "key"), "value # foo" } }));
|
||
5 years ago
|
});
|
||
|
}
|
||
|
|
||
|
TEST_F(SettingsFileParserTest, file_with_single_section_and_key_and_whitespaces)
|
||
|
{
|
||
2 years ago
|
const std::string content
|
||
|
= " [ Section ] \n"
|
||
|
" key = value \n";
|
||
5 years ago
|
|
||
2 years ago
|
withSettingsFile(content, [this](const auto& path) {
|
||
5 years ago
|
CategorySettingValueMap map;
|
||
|
mLoader.loadSettingsFile(path, map);
|
||
|
|
||
2 years ago
|
EXPECT_EQ(map, CategorySettingValueMap({ { CategorySetting("Section", "key"), "value" } }));
|
||
5 years ago
|
});
|
||
|
}
|
||
|
|
||
|
TEST_F(SettingsFileParserTest, file_with_quoted_string_value)
|
||
|
{
|
||
2 years ago
|
const std::string content
|
||
|
= "[Section]\n"
|
||
|
R"(key = "value")";
|
||
5 years ago
|
|
||
2 years ago
|
withSettingsFile(content, [this](const auto& path) {
|
||
5 years ago
|
CategorySettingValueMap map;
|
||
|
mLoader.loadSettingsFile(path, map);
|
||
|
|
||
2 years ago
|
EXPECT_EQ(map, CategorySettingValueMap({ { CategorySetting("Section", "key"), R"("value")" } }));
|
||
5 years ago
|
});
|
||
|
}
|
||
|
|
||
|
TEST_F(SettingsFileParserTest, file_with_quoted_string_value_and_eol)
|
||
|
{
|
||
2 years ago
|
const std::string content
|
||
|
= "[Section]\n"
|
||
|
R"(key = "value"\n)";
|
||
5 years ago
|
|
||
2 years ago
|
withSettingsFile(content, [this](const auto& path) {
|
||
5 years ago
|
CategorySettingValueMap map;
|
||
|
mLoader.loadSettingsFile(path, map);
|
||
|
|
||
2 years ago
|
EXPECT_EQ(map, CategorySettingValueMap({ { CategorySetting("Section", "key"), R"("value"\n)" } }));
|
||
5 years ago
|
});
|
||
|
}
|
||
|
|
||
|
TEST_F(SettingsFileParserTest, file_with_empty_value)
|
||
|
{
|
||
2 years ago
|
const std::string content
|
||
|
= "[Section]\n"
|
||
|
"key =\n";
|
||
5 years ago
|
|
||
2 years ago
|
withSettingsFile(content, [this](const auto& path) {
|
||
5 years ago
|
CategorySettingValueMap map;
|
||
|
mLoader.loadSettingsFile(path, map);
|
||
|
|
||
2 years ago
|
EXPECT_EQ(map, CategorySettingValueMap({ { CategorySetting("Section", "key"), "" } }));
|
||
5 years ago
|
});
|
||
|
}
|
||
|
|
||
|
TEST_F(SettingsFileParserTest, file_with_empty_key)
|
||
|
{
|
||
2 years ago
|
const std::string content
|
||
|
= "[Section]\n"
|
||
|
"=\n";
|
||
5 years ago
|
|
||
2 years ago
|
withSettingsFile(content, [this](const auto& path) {
|
||
5 years ago
|
CategorySettingValueMap map;
|
||
|
mLoader.loadSettingsFile(path, map);
|
||
|
|
||
2 years ago
|
EXPECT_EQ(map, CategorySettingValueMap({ { CategorySetting("Section", ""), "" } }));
|
||
5 years ago
|
});
|
||
|
}
|
||
|
|
||
|
TEST_F(SettingsFileParserTest, file_with_multiple_keys)
|
||
|
{
|
||
2 years ago
|
const std::string content
|
||
|
= "[Section]\n"
|
||
|
"key1 = value1\n"
|
||
|
"key2 = value2\n";
|
||
5 years ago
|
|
||
2 years ago
|
withSettingsFile(content, [this](const auto& path) {
|
||
5 years ago
|
CategorySettingValueMap map;
|
||
|
mLoader.loadSettingsFile(path, map);
|
||
|
|
||
2 years ago
|
EXPECT_EQ(map,
|
||
|
CategorySettingValueMap({
|
||
|
{ CategorySetting("Section", "key1"), "value1" },
|
||
|
{ CategorySetting("Section", "key2"), "value2" },
|
||
|
}));
|
||
5 years ago
|
});
|
||
|
}
|
||
|
|
||
|
TEST_F(SettingsFileParserTest, file_with_multiple_sections)
|
||
|
{
|
||
2 years ago
|
const std::string content
|
||
|
= "[Section1]\n"
|
||
|
"key1 = value1\n"
|
||
|
"[Section2]\n"
|
||
|
"key2 = value2\n";
|
||
|
|
||
|
withSettingsFile(content, [this](const auto& path) {
|
||
5 years ago
|
CategorySettingValueMap map;
|
||
|
mLoader.loadSettingsFile(path, map);
|
||
|
|
||
2 years ago
|
EXPECT_EQ(map,
|
||
|
CategorySettingValueMap({
|
||
|
{ CategorySetting("Section1", "key1"), "value1" },
|
||
|
{ CategorySetting("Section2", "key2"), "value2" },
|
||
|
}));
|
||
5 years ago
|
});
|
||
|
}
|
||
|
|
||
|
TEST_F(SettingsFileParserTest, file_with_multiple_sections_and_keys)
|
||
|
{
|
||
2 years ago
|
const std::string content
|
||
|
= "[Section1]\n"
|
||
|
"key1 = value1\n"
|
||
|
"key2 = value2\n"
|
||
|
"[Section2]\n"
|
||
|
"key3 = value3\n"
|
||
|
"key4 = value4\n";
|
||
|
|
||
|
withSettingsFile(content, [this](const auto& path) {
|
||
5 years ago
|
CategorySettingValueMap map;
|
||
|
mLoader.loadSettingsFile(path, map);
|
||
|
|
||
2 years ago
|
EXPECT_EQ(map,
|
||
|
CategorySettingValueMap({
|
||
|
{ CategorySetting("Section1", "key1"), "value1" },
|
||
|
{ CategorySetting("Section1", "key2"), "value2" },
|
||
|
{ CategorySetting("Section2", "key3"), "value3" },
|
||
|
{ CategorySetting("Section2", "key4"), "value4" },
|
||
|
}));
|
||
5 years ago
|
});
|
||
|
}
|
||
|
|
||
|
TEST_F(SettingsFileParserTest, file_with_repeated_sections)
|
||
|
{
|
||
2 years ago
|
const std::string content
|
||
|
= "[Section]\n"
|
||
|
"key1 = value1\n"
|
||
|
"[Section]\n"
|
||
|
"key2 = value2\n";
|
||
|
|
||
|
withSettingsFile(content, [this](const auto& path) {
|
||
5 years ago
|
CategorySettingValueMap map;
|
||
|
mLoader.loadSettingsFile(path, map);
|
||
|
|
||
2 years ago
|
EXPECT_EQ(map,
|
||
|
CategorySettingValueMap({
|
||
|
{ CategorySetting("Section", "key1"), "value1" },
|
||
|
{ CategorySetting("Section", "key2"), "value2" },
|
||
|
}));
|
||
5 years ago
|
});
|
||
|
}
|
||
|
|
||
|
TEST_F(SettingsFileParserTest, file_with_repeated_keys)
|
||
|
{
|
||
2 years ago
|
const std::string content
|
||
|
= "[Section]\n"
|
||
|
"key = value\n"
|
||
|
"key = value\n";
|
||
5 years ago
|
|
||
2 years ago
|
withSettingsFile(content, [this](const auto& path) {
|
||
5 years ago
|
CategorySettingValueMap map;
|
||
|
EXPECT_THROW(mLoader.loadSettingsFile(path, map), std::runtime_error);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
TEST_F(SettingsFileParserTest, file_with_repeated_keys_in_differrent_sections)
|
||
|
{
|
||
2 years ago
|
const std::string content
|
||
|
= "[Section1]\n"
|
||
|
"key = value\n"
|
||
|
"[Section2]\n"
|
||
|
"key = value\n";
|
||
|
|
||
|
withSettingsFile(content, [this](const auto& path) {
|
||
5 years ago
|
CategorySettingValueMap map;
|
||
|
mLoader.loadSettingsFile(path, map);
|
||
|
|
||
2 years ago
|
EXPECT_EQ(map,
|
||
|
CategorySettingValueMap({
|
||
|
{ CategorySetting("Section1", "key"), "value" },
|
||
|
{ CategorySetting("Section2", "key"), "value" },
|
||
|
}));
|
||
5 years ago
|
});
|
||
|
}
|
||
|
|
||
|
TEST_F(SettingsFileParserTest, file_with_unterminated_section)
|
||
|
{
|
||
2 years ago
|
const std::string content = "[Section";
|
||
5 years ago
|
|
||
2 years ago
|
withSettingsFile(content, [this](const auto& path) {
|
||
5 years ago
|
CategorySettingValueMap map;
|
||
|
EXPECT_THROW(mLoader.loadSettingsFile(path, map), std::runtime_error);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
TEST_F(SettingsFileParserTest, file_with_single_empty_section_name)
|
||
|
{
|
||
2 years ago
|
const std::string content = "[]\n";
|
||
5 years ago
|
|
||
2 years ago
|
withSettingsFile(content, [this](const auto& path) {
|
||
5 years ago
|
CategorySettingValueMap map;
|
||
|
mLoader.loadSettingsFile(path, map);
|
||
|
|
||
|
EXPECT_EQ(map, CategorySettingValueMap());
|
||
|
});
|
||
|
}
|
||
|
|
||
|
TEST_F(SettingsFileParserTest, file_with_key_and_without_section)
|
||
|
{
|
||
2 years ago
|
const std::string content = "key = value\n";
|
||
5 years ago
|
|
||
2 years ago
|
withSettingsFile(content, [this](const auto& path) {
|
||
5 years ago
|
CategorySettingValueMap map;
|
||
|
EXPECT_THROW(mLoader.loadSettingsFile(path, map), std::runtime_error);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
TEST_F(SettingsFileParserTest, file_with_key_in_empty_name_section)
|
||
|
{
|
||
2 years ago
|
const std::string content
|
||
|
= "[]"
|
||
|
"key = value\n";
|
||
5 years ago
|
|
||
2 years ago
|
withSettingsFile(content, [this](const auto& path) {
|
||
5 years ago
|
CategorySettingValueMap map;
|
||
|
EXPECT_THROW(mLoader.loadSettingsFile(path, map), std::runtime_error);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
TEST_F(SettingsFileParserTest, file_with_unterminated_key)
|
||
|
{
|
||
2 years ago
|
const std::string content
|
||
|
= "[Section]\n"
|
||
|
"key\n";
|
||
5 years ago
|
|
||
2 years ago
|
withSettingsFile(content, [this](const auto& path) {
|
||
5 years ago
|
CategorySettingValueMap map;
|
||
|
EXPECT_THROW(mLoader.loadSettingsFile(path, map), std::runtime_error);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
TEST_F(SettingsFileParserTest, file_with_empty_lines)
|
||
|
{
|
||
2 years ago
|
const std::string content
|
||
|
= "\n"
|
||
|
"[Section]\n"
|
||
|
"\n"
|
||
|
"key = value\n"
|
||
|
"\n";
|
||
|
|
||
|
withSettingsFile(content, [this](const auto& path) {
|
||
5 years ago
|
CategorySettingValueMap map;
|
||
|
mLoader.loadSettingsFile(path, map);
|
||
|
|
||
2 years ago
|
EXPECT_EQ(map, CategorySettingValueMap({ { CategorySetting("Section", "key"), "value" } }));
|
||
5 years ago
|
});
|
||
|
}
|
||
|
}
|