From 3a771009b380a55cdacaf888cab46a142f9a7c7d Mon Sep 17 00:00:00 2001 From: konn Date: Sat, 19 Apr 2008 07:54:45 +0000 Subject: [PATCH] * Added test code of the tag for parser. git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/modchxj/mod_chxj/branches/RELEASE_0_11_0@2401 1a406e8e-add9-4483-a2c8-d8cac5b7c224 --- src/qs_parse_string.c | 25 +-- test/qs_parse_string/Makefile | 5 + test/qs_parse_string/test_parse_script_tag.c | 266 +++++++++++++++++++++++++++ 3 files changed, 284 insertions(+), 12 deletions(-) create mode 100644 test/qs_parse_string/test_parse_script_tag.c diff --git a/src/qs_parse_string.c b/src/qs_parse_string.c index 77355f32..059f7943 100644 --- a/src/qs_parse_string.c +++ b/src/qs_parse_string.c @@ -49,7 +49,7 @@ static Node *qs_pop_node(Doc *doc, NodeStack stack); static void qs_dump_node_stack(Doc *doc, NodeStack stack); #endif static void qs_free_node_stack(Doc *doc, NodeStack stack); -static void s_error_check(Doc *doc, Node *node, NodeStack node_stack, NodeStack err_stack); +static void s_error_check(Doc *doc, const char *name, int line, NodeStack node_stack, NodeStack err_stack); Node * @@ -208,7 +208,7 @@ qs_parse_string(Doc *doc, const char *src, int srclen) if (STRCASEEQ('s','S',"script",&node->name[1])) { script_flag = 0; } - s_error_check(doc, node, node_stack, err_stack); + s_error_check(doc, &node->name[1], node->line, node_stack, err_stack); } else { /* ignore */ @@ -221,7 +221,7 @@ qs_parse_string(Doc *doc, const char *src, int srclen) if (doc->now_parent_node->parent != NULL) { doc->now_parent_node = doc->now_parent_node->parent; doc->parse_mode = PARSE_MODE_CHTML; - s_error_check(doc, node, node_stack, err_stack); + s_error_check(doc, &node->name[1], node->line, node_stack, err_stack); } } } @@ -278,7 +278,7 @@ qs_parse_string(Doc *doc, const char *src, int srclen) if (STRCASEEQ('s','S',"script",node->name)) { script_flag = 0; } - s_error_check(doc, node, node_stack, err_stack); + s_error_check(doc, node->name, node->line, node_stack, err_stack); } else { /* ignore */ @@ -337,14 +337,14 @@ qs_parse_string(Doc *doc, const char *src, int srclen) static void -s_error_check(Doc *doc, Node *node, NodeStack node_stack, NodeStack err_stack) +s_error_check(Doc *doc, const char *name, int line, NodeStack node_stack, NodeStack err_stack) { Node *prevNode; int err = 0; for (prevNode = qs_pop_node(doc,node_stack); prevNode; prevNode = qs_pop_node(doc, node_stack)) { - if (prevNode && strcasecmp(prevNode->name, &node->name[1]) != 0) { + if (prevNode && strcasecmp(prevNode->name, name) != 0) { qs_push_node(doc, prevNode, err_stack); err++; continue; @@ -355,9 +355,9 @@ s_error_check(Doc *doc, Node *node, NodeStack node_stack, NodeStack err_stack) Node *tmpNode = qs_pop_node(doc,node_stack); if (tmpNode == NULL && err != 1) { if (doc->r) - ERR(doc->r, "tag parse error (perhaps, miss spell). tag_name:[%s] line:[%d]", &node->name[1], node->line); + ERR(doc->r, "tag parse error (perhaps, miss spell). tag_name:[%s] line:[%d]", name, line); else - fprintf(stderr, "error :tag parse error (perhaps, miss spell). tag_name:[%s] line:[%d]\n", &node->name[1], node->line); + fprintf(stderr, "error :tag parse error (perhaps, miss spell). tag_name:[%s] line:[%d]\n", name, line); for (prevNode = qs_pop_node(doc,err_stack); prevNode; prevNode = qs_pop_node(doc, err_stack)) { @@ -502,19 +502,20 @@ s_cut_text(const char* s, int len, int script) } -Node* -qs_init_root_node(Doc* doc) +Node * +qs_init_root_node(Doc *doc) { doc->root_node = (Node*)apr_palloc(doc->pool,sizeof(struct Node)); - if (doc->root_node == NULL) + if (doc->root_node == NULL) { QX_LOGGER_FATAL("Out Of Memory"); + } doc->root_node->next = NULL; doc->root_node->parent = NULL; doc->root_node->child = NULL; doc->root_node->attr = NULL; - doc->root_node->name = (char*)apr_palloc(doc->pool,5); + doc->root_node->name = (char*)apr_palloc(doc->pool, 5); if (doc->root_node->name == NULL) { QX_LOGGER_FATAL("Out Of Memory"); } diff --git a/test/qs_parse_string/Makefile b/test/qs_parse_string/Makefile index 79a4a7f3..91ddfb17 100644 --- a/test/qs_parse_string/Makefile +++ b/test/qs_parse_string/Makefile @@ -2,6 +2,7 @@ .SUFFIXES: .c.o TARGET= test_parse_comment_tag \ + test_parse_script_tag TOP_DIR=../.. @@ -16,10 +17,14 @@ all:test test: $(TARGET) ./test_parse_comment_tag + ./test_parse_script_tag test_parse_comment_tag: test_parse_comment_tag.c gcc -g -o $@ -Wall -lcunit $< $(INC_DIR) -DLINUX=2 -D_REENTRANT -D_GNU_SOURCE -D_LARGEFILE64_SOURCE -lapr-1 +test_parse_script_tag: test_parse_script_tag.c + gcc -g -o $@ -Wall -lcunit $< $(INC_DIR) -DLINUX=2 -D_REENTRANT -D_GNU_SOURCE -D_LARGEFILE64_SOURCE -lapr-1 clean: rm -f ./test_parse_comment_tag + rm -f ./test_parse_script_tag diff --git a/test/qs_parse_string/test_parse_script_tag.c b/test/qs_parse_string/test_parse_script_tag.c new file mode 100644 index 00000000..3fbfdc54 --- /dev/null +++ b/test/qs_parse_string/test_parse_script_tag.c @@ -0,0 +1,266 @@ +#include +#include +#include +#include + +#include "httpd.h" +#include "qs_malloc.c" +#include "qs_log.c" +#include "qs_parse_tag.c" +#include "qs_parse_string.c" +#include "qs_parse_attr.c" +#include "qs_ignore_sp.c" +#include + + +void test_parse_script_tag_001(); +void test_parse_script_tag_002(); +void test_parse_script_tag_003(); +void test_parse_script_tag_004(); +void test_parse_script_tag_005(); +void test_parse_script_tag_006(); +void test_parse_script_tag_007(); +void test_parse_script_tag_008(); +void test_parse_script_tag_009(); +void test_parse_script_tag_010(); + + +void ap_log_rerror(const char *file, int line, int level, apr_status_t status, const request_rec *r, const char *fmt, ...) +{ + va_list ap; + fprintf(stderr, "ERROR LOG %s:%d ", file,line); + va_start(ap, fmt); + vfprintf(stderr, fmt,ap); + va_end(ap); + fprintf(stderr, "\n"); +} + +#define APR_INIT \ + request_rec r; \ + apr_pool_t *p; \ + do { \ + apr_initialize(); \ + apr_pool_create(&p, NULL); \ + r.pool = p; \ + doc.r = &r; \ + } \ + while (0) + +#define APR_TERM \ + do { \ + apr_terminate(); \ + } while (0) + + +int +main() +{ + CU_pSuite script_tag_suite; + CU_initialize_registry(); + script_tag_suite = CU_add_suite("test qs_parse_string()", NULL, NULL); + CU_add_test(script_tag_suite, "test normal pattern", test_parse_script_tag_001); + CU_add_test(script_tag_suite, "test inner tag1", test_parse_script_tag_002); + CU_add_test(script_tag_suite, "test inner tag2", test_parse_script_tag_003); + CU_add_test(script_tag_suite, "test short tag", test_parse_script_tag_004); + CU_add_test(script_tag_suite, "test not close script tag", test_parse_script_tag_005); +#if 0 + CU_add_test(script_tag_suite, "test close only script tag", test_parse_script_tag_006); + CU_add_test(script_tag_suite, "test null string", test_parse_script_tag_007); + CU_add_test(script_tag_suite, "test null", test_parse_script_tag_008); + CU_add_test(script_tag_suite, "test minus len", test_parse_script_tag_009); + CU_add_test(script_tag_suite, "test ambiguos close tag", test_parse_script_tag_010); +#endif +#if 0 + CU_console_run_tests(); + CU_automated_run_tests(); + CU_ErrorCode CU_basic_run_tests(); +#else + CU_basic_run_tests(); +#endif + CU_cleanup_registry(); + + return(0); +} + +static void dump(Node *node, int indent); + +static void dump(Node *node, int indent) +{ + Node *child; + for (child = node->child; + child; + child = child->next) { + fprintf(stderr, "%d:%*.*s:[%s] value:[%s]\n", indent, 10 + (indent * 2), 10 + (indent * 2), "Node", child->name, child->otext); + dump(child, indent+1); + } +} + +void test_parse_script_tag_001() +{ +#define TEST_STRING "" + Doc doc; + Node *node; + Node *node2; + APR_INIT; + + qs_init_malloc(&doc); + qs_init_root_node(&doc); + doc.parse_mode = PARSE_MODE_CHTML; + + node = qs_parse_string(&doc, TEST_STRING, sizeof(TEST_STRING)-1); + + CU_ASSERT(node != NULL); + CU_ASSERT(node->name != NULL); + CU_ASSERT(strcasecmp("ROOT",node->name) == 0); + CU_ASSERT(node->child != NULL); + fprintf(stderr, "====\n"); + dump(node, 1); + fprintf(stderr, "====\n"); + node2 = node->child; + CU_ASSERT(node2->name != NULL); + CU_ASSERT(strcasecmp("script",node2->name) == 0); + CU_ASSERT(node2->child != NULL); + node2 = node2->child; + CU_ASSERT(node2 != NULL); + CU_ASSERT(node2->name != NULL); + CU_ASSERT(strcasecmp("text",node2->name) == 0); + + APR_TERM; +#undef TEST_STRING +} + +void test_parse_script_tag_002() +{ +#define TEST_STRING "" + Doc doc; + Node *node; + Node *node2; + APR_INIT; + + qs_init_malloc(&doc); + qs_init_root_node(&doc); + doc.parse_mode = PARSE_MODE_CHTML; + + node = qs_parse_string(&doc, TEST_STRING, sizeof(TEST_STRING)-1); + + CU_ASSERT(node != NULL); + CU_ASSERT(node->name != NULL); + CU_ASSERT(strcasecmp("ROOT",node->name) == 0); + CU_ASSERT(node->child != NULL); + fprintf(stderr, "====\n"); + dump(node, 1); + fprintf(stderr, "====\n"); + node2 = node->child; + CU_ASSERT(node2->name != NULL); + CU_ASSERT(strcasecmp("script",node2->name) == 0); + CU_ASSERT(node2->child != NULL); + node2 = node2->child; + CU_ASSERT(node2 != NULL); + CU_ASSERT(node2->name != NULL); + CU_ASSERT(strcasecmp("text",node2->name) == 0); + + APR_TERM; +#undef TEST_STRING +} +void test_parse_script_tag_003() +{ +#define TEST_STRING "'" + Doc doc; + Node *node; + Node *node2; + APR_INIT; + + qs_init_malloc(&doc); + qs_init_root_node(&doc); + doc.parse_mode = PARSE_MODE_CHTML; + + node = qs_parse_string(&doc, TEST_STRING, sizeof(TEST_STRING)-1); + + CU_ASSERT(node != NULL); + CU_ASSERT(node->name != NULL); + CU_ASSERT(strcasecmp("ROOT",node->name) == 0); + CU_ASSERT(node->child != NULL); + + fprintf(stderr, "====\n"); + dump(node, 1); + fprintf(stderr, "====\n"); + + node2 = node->child; + CU_ASSERT(node2->name != NULL); + CU_ASSERT(strcasecmp("script",node2->name) == 0); + CU_ASSERT(node2->child != NULL); + + node2 = node2->child; + CU_ASSERT(node2 != NULL); + CU_ASSERT(node2->name != NULL); + CU_ASSERT(strcasecmp("text",node2->name) == 0); + + APR_TERM; +#undef TEST_STRING +} +void test_parse_script_tag_005() +{ +#define TEST_STRING "\"" + Doc doc; + Node *node; + Node *node2; + APR_INIT; + + qs_init_malloc(&doc); + qs_init_root_node(&doc); + doc.parse_mode = PARSE_MODE_CHTML; + + node = qs_parse_string(&doc, TEST_STRING, sizeof(TEST_STRING)-1); + + CU_ASSERT(node != NULL); + CU_ASSERT(node->name != NULL); + CU_ASSERT(strcasecmp("ROOT",node->name) == 0); + CU_ASSERT(node->child != NULL); + + fprintf(stderr, "====\n"); + dump(node, 1); + fprintf(stderr, "====\n"); + + node2 = node->child; + CU_ASSERT(node2->name != NULL); + CU_ASSERT(strcasecmp("script",node2->name) == 0); + CU_ASSERT(node2->child != NULL); + + node2 = node2->child; + CU_ASSERT(node2 != NULL); + CU_ASSERT(node2->name != NULL); + CU_ASSERT(strcasecmp("text",node2->name) == 0); + + APR_TERM; +#undef TEST_STRING +} + -- 2.11.0