#include "gtest/gtest.h" #include "xml/xml_parser.h" #include using std::string; static string s_log; static void xml_gen_on_start(XmlBuilder* thiz, const char* tag, const char** attrs) { uint32_t i = 0; s_log += "<" + string(tag); while (attrs[i] != NULL) { if (i == 0) { s_log += ' '; } s_log += attrs[i]; s_log += string("=\"") + attrs[i + 1] + string("\" "); i += 2; } s_log += ">"; return; } static void xml_gen_on_end(XmlBuilder* thiz, const char* tag) { (void)thiz; s_log += ""; return; } static void xml_gen_on_text(XmlBuilder* thiz, const char* text, size_t length) { char str[1024]; assert(length < sizeof(str)); memset(str, 0x00, sizeof(str)); strncpy(str, text, length); s_log += str; return; } static void xml_gen_destroy(XmlBuilder* thiz) { (void)thiz; return; } static XmlBuilder* builder_init(XmlBuilder& b) { b.on_start = xml_gen_on_start; b.on_end = xml_gen_on_end; b.on_text = xml_gen_on_text; b.destroy = xml_gen_destroy; return &(b); } static void test_str(XmlParser* p, const char* str) { s_log = ""; xml_parser_parse(p, str, strlen(str)); ASSERT_EQ(s_log, str); } static void test_str_ex(XmlParser* p, const char* str, const char* expected) { s_log = ""; xml_parser_parse(p, str, strlen(str)); ASSERT_EQ(s_log, expected); } TEST(XmlParser, cdata) { XmlBuilder b; XmlParser* p = xml_parser_create(); xml_parser_set_builder(p, builder_init(b)); test_str_ex(p, "", "123"); test_str_ex(p, "]]>", ""); test_str_ex(p, "123]]>", "123"); test_str_ex(p, "123]]>", "123"); test_str_ex(p, "123]]>1", "1231"); test_str_ex(p, "123]]>123", "123123"); test_str_ex(p, " ", "123"); test_str_ex(p, " \n ", "123"); test_str_ex(p, " \n \n ", "123"); xml_parser_destroy(p); } TEST(XmlParser, basic) { XmlBuilder b; XmlParser* p = xml_parser_create(); xml_parser_set_builder(p, builder_init(b)); test_str(p, ""); test_str_ex(p, " ", ""); test_str_ex(p, " \n ", ""); test_str_ex(p, " \n123\n ", "123"); test_str(p, "test"); test_str(p, "test"); test_str(p, "testtest1"); xml_parser_destroy(p); } TEST(XmlParser, max_attrs) { XmlBuilder b; XmlParser* p = xml_parser_create(); xml_parser_set_builder(p, builder_init(b)); test_str_ex(p, "", ""); xml_parser_destroy(p); }