lokinet/test/util/test_llarp_util_ini.cpp

60 lines
1.4 KiB
C++
Raw Normal View History

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 = [&sect](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());
#if __cplusplus >= 201703L
2019-02-05 20:33:49 +00:00
ASSERT_STREQ(llarp::string_view_string(itr->second).c_str(), "val");
#else
2019-02-24 02:45:40 +00:00
ASSERT_EQ(itr->second, "val");
#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"));
}