mirror of https://github.com/oxen-io/lokinet
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.5 KiB
C++
65 lines
1.5 KiB
C++
5 years ago
|
#include <gtest/gtest.h>
|
||
|
|
||
5 years ago
|
#include <config/ini.hpp>
|
||
5 years ago
|
|
||
5 years ago
|
struct TestINIParser : public ::testing::Test
|
||
|
{
|
||
5 years ago
|
llarp::ConfigParser parser;
|
||
|
|
||
5 years ago
|
void
|
||
|
TearDown()
|
||
5 years ago
|
{
|
||
|
parser.Clear();
|
||
|
}
|
||
|
};
|
||
|
|
||
|
TEST_F(TestINIParser, TestParseEmpty)
|
||
|
{
|
||
|
ASSERT_TRUE(parser.LoadString(""));
|
||
|
}
|
||
|
|
||
|
TEST_F(TestINIParser, TestParseOneSection)
|
||
|
{
|
||
|
llarp::ConfigParser::Section_t sect;
|
||
5 years ago
|
// this is an anti pattern don't write this kind of code with configpaser
|
||
5 years ago
|
auto assertVisit = [§](const auto& section) -> bool {
|
||
5 years ago
|
sect = section;
|
||
|
return true;
|
||
|
};
|
||
5 years ago
|
ASSERT_TRUE(parser.LoadString("[test]\nkey=val \n"));
|
||
5 years ago
|
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());
|
||
5 years ago
|
#if __cplusplus >= 201703L
|
||
5 years ago
|
ASSERT_STREQ(llarp::string_view_string(itr->second).c_str(), "val");
|
||
5 years ago
|
#else
|
||
5 years ago
|
ASSERT_EQ(itr->second, "val");
|
||
5 years ago
|
#endif
|
||
5 years ago
|
}
|
||
|
|
||
|
TEST_F(TestINIParser, TestParseSectionDuplicateKeys)
|
||
|
{
|
||
|
ASSERT_TRUE(parser.LoadString("[test]\nkey1=val1\nkey1=val2"));
|
||
|
size_t num = 0;
|
||
5 years ago
|
auto visit = [&num](const auto& section) -> bool {
|
||
5 years ago
|
num = section.count("key1");
|
||
|
return true;
|
||
|
};
|
||
|
ASSERT_TRUE(parser.VisitSection("test", visit));
|
||
|
ASSERT_EQ(num, size_t(2));
|
||
|
}
|
||
|
|
||
5 years ago
|
TEST_F(TestINIParser, TestNoKey)
|
||
|
{
|
||
|
ASSERT_FALSE(parser.LoadString("[test]\n=1090\n"));
|
||
|
}
|
||
|
|
||
5 years ago
|
TEST_F(TestINIParser, TestParseInvalid)
|
||
|
{
|
||
5 years ago
|
ASSERT_FALSE(
|
||
|
parser.LoadString("srged5ghe5\nf34wtge5\nw34tgfs4ygsd5yg=4;\n#"
|
||
|
"g4syhgd5\n"));
|
||
5 years ago
|
}
|