Commit f5098c01 authored by Tomek Mrugalski's avatar Tomek Mrugalski 🛰

[3400] Added tests for location and comments in configs

parent 2a77f5bb
......@@ -993,6 +993,40 @@ TEST(Element, preprocessor) {
EXPECT_THROW(Element::fromJSON(dbl_tail_comment), JSONError);
// This test checks whether positions are stored properly is possible to ignore comments. It also checks
// that the comments are ignored only when told to.
TEST(Element, preprocessorLocation) {
string commented = "# this is a comment, ignore me\n"
"{ \"a\": 1,\n"
"# this is a comment, ignore me\n"
" \"b\": 2}\n"
"# this is a comment, ignore me\n";
// This is what we expect in all cases.
ElementPtr exp = Element::fromJSON(commented);
// Let's convert them all and see that the result it the same every time
ElementPtr a = cfg->get("a");
ElementPtr b = cfg->get("b");
EXPECT_TRUE(exp->equals(*Element::fromJSON(head_comment, true)));
EXPECT_TRUE(exp->equals(*Element::fromJSON(mid_comment, true)));
EXPECT_TRUE(exp->equals(*Element::fromJSON(tail_comment, true)));
EXPECT_TRUE(exp->equals(*Element::fromJSON(dbl_head_comment, true)));
EXPECT_TRUE(exp->equals(*Element::fromJSON(dbl_mid_comment, true)));
EXPECT_TRUE(exp->equals(*Element::fromJSON(dbl_tail_comment, true)));
// With preprocessing disabled, it should fail all around
EXPECT_THROW(Element::fromJSON(head_comment), JSONError);
EXPECT_THROW(Element::fromJSON(mid_comment), JSONError);
EXPECT_THROW(Element::fromJSON(tail_comment), JSONError);
EXPECT_THROW(Element::fromJSON(dbl_head_comment), JSONError);
EXPECT_THROW(Element::fromJSON(dbl_mid_comment), JSONError);
EXPECT_THROW(Element::fromJSON(dbl_tail_comment), JSONError);
TEST(Element, getPosition) {
std::istringstream ss("{\n"
" \"a\": 2,\n"
......@@ -1087,4 +1121,59 @@ TEST(Element, getPosition) {
// Tests whether position is returned properly for a commented input JSON text.
TEST(Element, getPositionCommented) {
std::istringstream ss("{\n"
" \"a\": 2,\n"
"# comment\n"
" \"cy\": \"a string\",\n"
" \"dyz\": {\n"
"# another comment\n"
" \"e\": 3,\n"
" \"f\": null\n"
" } }\n");
// Create a JSON string holding different type of values. Some of the
// values in the config string are not aligned, so as we can check that
// the position is set correctly for the elements.
ElementPtr top = Element::fromJSON(ss, "kea.conf");
// Element "a"
ConstElementPtr level1_el = top->get("a");
EXPECT_EQ(2, level1_el->getPosition().line_);
EXPECT_EQ(11, level1_el->getPosition().pos_);
EXPECT_EQ("kea.conf", level1_el->getPosition().file_);
// Element "cy"
level1_el = top->get("cy");
EXPECT_EQ(4, level1_el->getPosition().line_);
EXPECT_EQ(11, level1_el->getPosition().pos_);
EXPECT_EQ("kea.conf", level1_el->getPosition().file_);
// Element "dyz"
level1_el = top->get("dyz");
EXPECT_EQ(5, level1_el->getPosition().line_);
EXPECT_EQ(13, level1_el->getPosition().pos_);
EXPECT_EQ("kea.conf", level1_el->getPosition().file_);
// Element "e" is a sub element of "dyz".
ConstElementPtr level2_el = level1_el->get("e");
EXPECT_EQ(7, level2_el->getPosition().line_);
EXPECT_EQ(12, level2_el->getPosition().pos_);
EXPECT_EQ("kea.conf", level2_el->getPosition().file_);
// Element "f" is also a sub element of "dyz"
level2_el = level1_el->get("f");
EXPECT_EQ(8, level2_el->getPosition().line_);
EXPECT_EQ(14, level2_el->getPosition().pos_);
EXPECT_EQ("kea.conf", level2_el->getPosition().file_);
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment