From 306dbf6da2e9f0aa23107068464af739cb785cea Mon Sep 17 00:00:00 2001 From: Hector Espert Date: Tue, 2 Feb 2021 23:11:40 +0100 Subject: [PATCH] Add xml parser test to run as a unit test --- tests/unit-tests/lib/test_parse.cpp | 63 +++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/tests/unit-tests/lib/test_parse.cpp b/tests/unit-tests/lib/test_parse.cpp index 1a63546377..a0134d5eab 100644 --- a/tests/unit-tests/lib/test_parse.cpp +++ b/tests/unit-tests/lib/test_parse.cpp @@ -60,4 +60,67 @@ namespace test_parse { EXPECT_EQ(test, answer); } + TEST_F(test_parse, XML_PARSER) { + + MIOFILE mf; + + XML_PARSER xp(&mf); + + mf.init_buf_read("\n" +"\n" +" \n" +" asdlfkj\n" +" fj\n" +" \n" +" blah\n" +" 6\n" +" \n" +" 6.555\n" +" 0\n" +""); + + EXPECT_TRUE(xp.parse_start("blah")); + + int success = false; + int expects = 0; + + char name[64]; + strcpy(name, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); + + int val; + + double x; + + bool flag; + + while (!xp.get_tag()) { + if (!xp.is_tag) { + continue; + } + if (xp.match_tag("/blah")) { + success = true; + } else if (xp.parse_str("str", name, 64)) { + EXPECT_STREQ(name, "blah"); + expects++; + } else if (xp.parse_int("int", val)) { + EXPECT_EQ(val, 6); + expects++; + } else if (xp.parse_double("double", x)) { + EXPECT_EQ(x, 6.555); + expects++; + } else if (xp.parse_bool("bool", flag)) { + EXPECT_FALSE(flag); + expects++; + } else { + xp.skip_unexpected(false, "xml test"); + EXPECT_STREQ(xp.parsed_tag, "x"); + expects++; + } + } + + EXPECT_TRUE(success); + EXPECT_EQ(expects, 5); + + } + } // namespace