lokinet/test/config/test_llarp_config_ini.cpp

61 lines
1.4 KiB
C++
Raw Normal View History

2019-01-22 14:13:26 +00:00
#include <gtest/gtest.h>
#include <config/ini.hpp>
2019-01-22 14:13:26 +00:00
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)
{
2019-07-17 08:48:13 +00:00
ASSERT_TRUE(parser.LoadFromStr(""));
2019-01-22 14:13:26 +00:00
}
TEST_F(TestINIParser, TestParseOneSection)
{
llarp::ConfigParser::SectionValues_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-07-17 08:48:13 +00:00
ASSERT_TRUE(parser.LoadFromStr("[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-02-24 02:45:40 +00:00
ASSERT_EQ(itr->second, "val");
2019-01-22 14:13:26 +00:00
}
TEST_F(TestINIParser, TestParseSectionDuplicateKeys)
{
2019-07-17 08:48:13 +00:00
ASSERT_TRUE(parser.LoadFromStr("[test]\nkey1=val1\nkey1=val2"));
2019-01-22 14:13:26 +00:00
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, TestNoKey)
{
2019-07-17 08:48:13 +00:00
ASSERT_FALSE(parser.LoadFromStr("[test]\n=1090\n"));
}
2019-01-22 14:13:26 +00:00
TEST_F(TestINIParser, TestParseInvalid)
{
2019-02-24 02:45:40 +00:00
ASSERT_FALSE(
2019-07-17 08:48:13 +00:00
parser.LoadFromStr("srged5ghe5\nf34wtge5\nw34tgfs4ygsd5yg=4;\n#"
"g4syhgd5\n"));
}