lokinet/test/config/test_llarp_config_ini.cpp

57 lines
1.3 KiB
C++
Raw Normal View History

#include <config/ini.hpp>
2019-01-22 14:13:26 +00:00
2021-03-01 21:07:32 +00:00
#include <catch2/catch.hpp>
TEST_CASE("ConfigParser", "[config]")
2019-02-24 02:45:40 +00:00
{
2019-01-22 14:13:26 +00:00
llarp::ConfigParser parser;
2021-03-01 21:07:32 +00:00
SECTION("Parse empty")
2019-01-22 14:13:26 +00:00
{
2021-03-01 21:07:32 +00:00
REQUIRE(parser.LoadFromStr(""));
2019-01-22 14:13:26 +00:00
}
2021-03-01 21:07:32 +00:00
SECTION("Parse one section")
{
llarp::ConfigParser::SectionValues_t sect;
// this is an anti pattern don't write this kind of code with configpaser
auto assertVisit = [&sect](const auto& section) -> bool {
sect = section;
return true;
};
REQUIRE(parser.LoadFromStr("[test]\nkey=val \n"));
REQUIRE(parser.VisitSection("test", assertVisit));
auto itr = sect.find("notfound");
REQUIRE(itr == sect.end());
itr = sect.find("key");
REQUIRE(itr != sect.end());
REQUIRE(itr->second == "val");
}
2019-01-22 14:13:26 +00:00
2021-03-01 21:07:32 +00:00
SECTION("Parse section duplicate keys")
{
REQUIRE(parser.LoadFromStr("[test]\nkey1=val1\nkey1=val2"));
size_t num = 0;
auto visit = [&num](const auto& section) -> bool {
num = section.count("key1");
return true;
};
REQUIRE(parser.VisitSection("test", visit));
REQUIRE(num == size_t(2));
}
2019-01-22 14:13:26 +00:00
2021-03-01 21:07:32 +00:00
SECTION("No key")
{
REQUIRE_FALSE(parser.LoadFromStr("[test]\n=1090\n"));
}
2019-01-22 14:13:26 +00:00
2021-03-01 21:07:32 +00:00
SECTION("Parse invalid")
{
REQUIRE_FALSE(
parser.LoadFromStr("srged5ghe5\nf34wtge5\nw34tgfs4ygsd5yg=4;\n#"
"g4syhgd5\n"));
}
2021-03-01 21:07:32 +00:00
parser.Clear();
}