2019-01-22 14:13:26 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include <util/ini.hpp>
|
|
|
|
|
2019-02-24 02:45:40 +00:00
|
|
|
struct TestINIParser : public ::testing::Test
|
|
|
|
{
|
2019-01-22 14:13:26 +00:00
|
|
|
llarp::ConfigParser parser;
|
|
|
|
|
2019-02-24 02:45:40 +00:00
|
|
|
void
|
|
|
|
TearDown()
|
2019-01-22 14:13:26 +00:00
|
|
|
{
|
|
|
|
parser.Clear();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(TestINIParser, TestParseEmpty)
|
|
|
|
{
|
|
|
|
ASSERT_TRUE(parser.LoadString(""));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TestINIParser, TestParseOneSection)
|
|
|
|
{
|
|
|
|
llarp::ConfigParser::Section_t sect;
|
2019-01-22 14:18:08 +00:00
|
|
|
// this is an anti pattern don't write this kind of code with configpaser
|
2019-02-24 02:45:40 +00:00
|
|
|
auto assertVisit = [§](const auto& section) -> bool {
|
2019-01-22 14:13:26 +00:00
|
|
|
sect = section;
|
|
|
|
return true;
|
|
|
|
};
|
2019-01-22 15:16:35 +00:00
|
|
|
ASSERT_TRUE(parser.LoadString("[test]\nkey=val \n"));
|
2019-01-22 14:13:26 +00:00
|
|
|
ASSERT_TRUE(parser.VisitSection("test", assertVisit));
|
|
|
|
auto itr = sect.find("notfound");
|
|
|
|
ASSERT_EQ(itr, sect.end());
|
|
|
|
itr = sect.find("key");
|
|
|
|
ASSERT_NE(itr, sect.end());
|
2019-01-30 17:24:02 +00:00
|
|
|
#if __cplusplus >= 201703L
|
2019-02-05 20:33:49 +00:00
|
|
|
ASSERT_STREQ(llarp::string_view_string(itr->second).c_str(), "val");
|
2019-01-30 17:24:02 +00:00
|
|
|
#else
|
2019-02-24 02:45:40 +00:00
|
|
|
ASSERT_EQ(itr->second, "val");
|
2019-01-30 17:24:02 +00:00
|
|
|
#endif
|
2019-01-22 14:13:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TestINIParser, TestParseSectionDuplicateKeys)
|
|
|
|
{
|
|
|
|
ASSERT_TRUE(parser.LoadString("[test]\nkey1=val1\nkey1=val2"));
|
|
|
|
size_t num = 0;
|
2019-02-24 02:45:40 +00:00
|
|
|
auto visit = [&num](const auto& section) -> bool {
|
2019-01-22 14:13:26 +00:00
|
|
|
num = section.count("key1");
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
ASSERT_TRUE(parser.VisitSection("test", visit));
|
|
|
|
ASSERT_EQ(num, size_t(2));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TestINIParser, TestParseInvalid)
|
|
|
|
{
|
2019-02-24 02:45:40 +00:00
|
|
|
ASSERT_FALSE(
|
|
|
|
parser.LoadString("srged5ghe5\nf34wtge5\nw34tgfs4ygsd5yg=4;\n#"
|
|
|
|
"g4syhgd5\n"));
|
2019-01-30 17:24:02 +00:00
|
|
|
}
|