diff --git a/src/Makefile_Eina.am b/src/Makefile_Eina.am index 9b85a85a83..91ebe577df 100644 --- a/src/Makefile_Eina.am +++ b/src/Makefile_Eina.am @@ -268,6 +268,8 @@ tests/eina/eina_test_tmpstr.c # tests/eina/eina_test_model.c tests_eina_eina_suite_CPPFLAGS = \ +-DTESTS_WD=\"`pwd`\" \ +-DTESTS_SRC_DIR=\"$(top_srcdir)/src/tests/eina\" \ -DPACKAGE_BUILD_DIR=\"`pwd`/$(top_builddir)/src/tests/eina\" \ -DTESTS_BUILD_DIR=PACKAGE_BUILD_DIR \ @CHECK_CFLAGS@ \ diff --git a/src/tests/eina/eina_test_simple_xml_parser.c b/src/tests/eina/eina_test_simple_xml_parser.c index 7093019e3f..25a42c789c 100644 --- a/src/tests/eina/eina_test_simple_xml_parser.c +++ b/src/tests/eina/eina_test_simple_xml_parser.c @@ -27,12 +27,33 @@ #include "eina_suite.h" #include "Eina.h" +static const char *get_file_full_path(const char *filename) +{ + static char full_path[PATH_MAX] = ""; + struct stat st; + + eina_str_join(full_path, sizeof(full_path), '/', TESTS_SRC_DIR, filename); + + if (stat(full_path, &st) == 0) + return full_path; + + if (full_path[0] != '/') + { + snprintf(full_path, sizeof(full_path), "%s/%s/%s", TESTS_WD, TESTS_SRC_DIR, filename); + + if (stat(full_path, &st) == 0) + return full_path; + } + + return NULL; +} + START_TEST(eina_simple_xml_parser_node_dump) { FILE *f; eina_init(); - f = fopen("sample.gpx", "rb"); + f = fopen(get_file_full_path("sample.gpx"), "rb"); if (f) { long sz; @@ -68,8 +89,185 @@ START_TEST(eina_simple_xml_parser_node_dump) } END_TEST +START_TEST(eina_simple_xml_parser_null_node_dump) +{ + eina_init(); + + char *out = eina_simple_xml_node_dump(NULL, " "); + fail_if(out != NULL); + + eina_shutdown(); +} +END_TEST + +START_TEST(eina_simple_xml_parser_childs_count) +{ + eina_init(); + + const char *buf = "\n" + "I'm a child."; + const int sz = strlen(buf); + Eina_Simple_XML_Node_Root *root = eina_simple_xml_node_load(buf, sz, EINA_TRUE); + fail_if(root == NULL); + fail_if(root->children == NULL); + fail_if(eina_inlist_count(root->children) != 2); + eina_simple_xml_node_root_free(root); + + eina_shutdown(); +} +END_TEST + +enum simple_xml_parser_current_state +{ + simple_xml_parser_current_state_begin, + simple_xml_parser_current_state_gpx, + simple_xml_parser_current_state_metadata, + simple_xml_parser_current_state_link, + simple_xml_parser_current_state_text, + simple_xml_parser_current_state_time, + simple_xml_parser_current_state_trk, + simple_xml_parser_current_state_name, + simple_xml_parser_current_state_trkseg, + simple_xml_parser_current_state_trkpt, + simple_xml_parser_current_state_comment, + simple_xml_parser_current_state_cdata, + simple_xml_parser_current_state_end +}; + +static Eina_Bool +eina_simple_xml_parser_parse_with_custom_callback_tag_cb(void *data, + Eina_Simple_XML_Type type, + const char *content, + unsigned offset, + unsigned length) +{ + int* parse_current_state = (int*) data; + + if (type == EINA_SIMPLE_XML_OPEN) + { + if (!strncmp("gpx ", content, strlen("gpx "))) + { + fail_if(*parse_current_state != simple_xml_parser_current_state_begin); + *parse_current_state = simple_xml_parser_current_state_gpx; + } + else if (!strncmp("metadata>", content, strlen("metadata>"))) + { + fail_if(*parse_current_state != simple_xml_parser_current_state_gpx); + *parse_current_state = simple_xml_parser_current_state_metadata; + } + else if (!strncmp("link ", content, strlen("link "))) + { + fail_if(*parse_current_state != simple_xml_parser_current_state_metadata); + *parse_current_state = simple_xml_parser_current_state_link; + } + else if (!strncmp("text>", content, strlen("text>"))) + { + fail_if(*parse_current_state != simple_xml_parser_current_state_link); + *parse_current_state = simple_xml_parser_current_state_text; + } + else if (!strncmp("time>", content, strlen("time>"))) + { + fail_if(*parse_current_state != simple_xml_parser_current_state_text); + *parse_current_state = simple_xml_parser_current_state_time; + } + else if (!strncmp("trk>", content, strlen("trk>"))) + { + fail_if(*parse_current_state != simple_xml_parser_current_state_time); + *parse_current_state = simple_xml_parser_current_state_trk; + } + else if (!strncmp("name>", content, strlen("name>"))) + { + fail_if(*parse_current_state != simple_xml_parser_current_state_trk); + *parse_current_state = simple_xml_parser_current_state_name; + } + else if (!strncmp("trkseg>", content, strlen("trkseg>"))) + { + fail_if(*parse_current_state != simple_xml_parser_current_state_name); + *parse_current_state = simple_xml_parser_current_state_trkseg; + } + else + { + fail_if(*parse_current_state != simple_xml_parser_current_state_trkpt); + } + } + else if (type == EINA_SIMPLE_XML_OPEN_EMPTY) + { + if (!strncmp("trkpt ", content, strlen("trkpt "))) + { + fail_if(*parse_current_state != simple_xml_parser_current_state_trkseg && + *parse_current_state != simple_xml_parser_current_state_trkpt); + *parse_current_state = simple_xml_parser_current_state_trkpt; + } + } + else if (type == EINA_SIMPLE_XML_COMMENT) + { + fail_if(*parse_current_state != simple_xml_parser_current_state_trkpt); + *parse_current_state = simple_xml_parser_current_state_comment; + } + else if (type == EINA_SIMPLE_XML_CDATA) + { + fail_if(*parse_current_state != simple_xml_parser_current_state_comment); + *parse_current_state = simple_xml_parser_current_state_cdata; + } + else if (type == EINA_SIMPLE_XML_CLOSE) + { + if (!strncmp("gpx", content, strlen("gpx"))) + { + fail_if(*parse_current_state != simple_xml_parser_current_state_cdata); + *parse_current_state = simple_xml_parser_current_state_end; + } + } +} + +START_TEST(eina_simple_xml_parser_parse_with_custom_callback) +{ + FILE *f; + + eina_init(); + f = fopen(get_file_full_path("sample.gpx"), "rb"); + + if (f) + { + long sz; + + fseek(f, 0, SEEK_END); + sz = ftell(f); + + if (sz > 0) + { + char *buf; + + fseek(f, 0, SEEK_SET); + buf = malloc(sz + 1); + + if (buf) + { + if (fread(buf, 1, sz, f)) + { + int parse_current_state = simple_xml_parser_current_state_begin; + eina_simple_xml_parse(buf, + sz, + EINA_TRUE, + eina_simple_xml_parser_parse_with_custom_callback_tag_cb, + &parse_current_state); + free(buf); + fail_if(parse_current_state != simple_xml_parser_current_state_end); + } + } + } + + fclose(f); + } + + eina_shutdown(); +} +END_TEST + void eina_test_simple_xml_parser(TCase *tc) { tcase_add_test(tc, eina_simple_xml_parser_node_dump); + tcase_add_test(tc, eina_simple_xml_parser_null_node_dump); + tcase_add_test(tc, eina_simple_xml_parser_childs_count); + tcase_add_test(tc, eina_simple_xml_parser_parse_with_custom_callback); }