Add padding to config file generated output

pull/1186/head
Stephen Shelton 4 years ago
parent 1273f11ce8
commit 14e7789847
No known key found for this signature in database
GPG Key ID: EE4BADACCE8B631C

@ -136,7 +136,7 @@ Configuration::generateINIConfig(bool useValues)
visitSections([&](const std::string& section, const DefinitionMap&) { visitSections([&](const std::string& section, const DefinitionMap&) {
if (sectionsVisited > 0) if (sectionsVisited > 0)
oss << "\n"; oss << "\n\n";
// TODO: this will create empty objects as a side effect of map's operator[] // TODO: this will create empty objects as a side effect of map's operator[]
// TODO: this also won't handle sections which have no definition // TODO: this also won't handle sections which have no definition
@ -148,6 +148,7 @@ Configuration::generateINIConfig(bool useValues)
oss << "[" << section << "]\n"; oss << "[" << section << "]\n";
visitDefinitions(section, [&](const std::string& name, const ConfigDefinition_ptr& def) { visitDefinitions(section, [&](const std::string& name, const ConfigDefinition_ptr& def) {
oss << "\n";
// TODO: as above, this will create empty objects // TODO: as above, this will create empty objects
// TODO: as above (but more important): this won't handle definitions with no entries // TODO: as above (but more important): this won't handle definitions with no entries
@ -164,9 +165,10 @@ Configuration::generateINIConfig(bool useValues)
else else
{ {
if (not def->required) if (not def->required)
oss << "# "; oss << "#";
oss << name << "=" << def->defaultValueAsString() << "\n"; oss << name << "=" << def->defaultValueAsString() << "\n";
} }
}); });
sectionsVisited++; sectionsVisited++;

@ -23,13 +23,20 @@ TEST_CASE("Configuration simple generate test", "[config]")
std::string output = config.generateINIConfig(); std::string output = config.generateINIConfig();
CHECK(output == R"raw([foo] CHECK(output == R"raw([foo]
bar=1 bar=1
# baz=2
#baz=2
quux=hello quux=hello
[argle] [argle]
bar=3 bar=3
# baz=4
#baz=4
quux=the quick brown fox quux=the quick brown fox
)raw"); )raw");
} }
@ -41,14 +48,14 @@ TEST_CASE("Configuration useValue test", "[config]")
config.defineOption(std::make_unique<llarp::ConfigDefinition<int>>( config.defineOption(std::make_unique<llarp::ConfigDefinition<int>>(
"foo", "bar", true, 1)); "foo", "bar", true, 1));
constexpr auto expected = "[foo]\nbar=1\n"; constexpr auto expected = "[foo]\n\nbar=1\n";
CHECK(config.generateINIConfig(false) == expected); CHECK(config.generateINIConfig(false) == expected);
CHECK(config.generateINIConfig(true) == expected); CHECK(config.generateINIConfig(true) == expected);
config.addConfigValue("foo", "bar", "2"); config.addConfigValue("foo", "bar", "2");
constexpr auto expectedWhenValueProvided = "[foo]\nbar=2\n"; constexpr auto expectedWhenValueProvided = "[foo]\n\nbar=2\n";
CHECK(config.generateINIConfig(false) == expected); CHECK(config.generateINIConfig(false) == expected);
CHECK(config.generateINIConfig(true) == expectedWhenValueProvided); CHECK(config.generateINIConfig(true) == expectedWhenValueProvided);
@ -68,6 +75,7 @@ TEST_CASE("Configuration section comments test")
CHECK(output == R"raw(# test comment CHECK(output == R"raw(# test comment
# test comment 2 # test comment 2
[foo] [foo]
bar=1 bar=1
)raw"); )raw");
} }
@ -84,6 +92,7 @@ TEST_CASE("Configuration option comments test")
std::string output = config.generateINIConfig(); std::string output = config.generateINIConfig();
CHECK(output == R"raw([foo] CHECK(output == R"raw([foo]
# test comment 1 # test comment 1
# test comment 2 # test comment 2
bar=1 bar=1
@ -107,6 +116,7 @@ TEST_CASE("Configuration empty comments test")
CHECK(output == R"raw(# section comment CHECK(output == R"raw(# section comment
# #
[foo] [foo]
# option comment # option comment
# #
bar=1 bar=1
@ -131,8 +141,10 @@ TEST_CASE("Configuration multi option comments")
CHECK(output == R"raw(# foo section comment CHECK(output == R"raw(# foo section comment
[foo] [foo]
# foo bar option comment # foo bar option comment
bar=1 bar=1
# foo baz option comment # foo baz option comment
baz=1 baz=1
)raw"); )raw");
@ -150,6 +162,7 @@ TEST_CASE("Configuration should print comments for missing keys")
CHECK(output == R"raw(# foo section comment CHECK(output == R"raw(# foo section comment
[foo] [foo]
# foo bar option comment # foo bar option comment
bar= bar=
)raw"); )raw");

Loading…
Cancel
Save