diff options
| author | Alexander Pickering <alexandermpickering@gmail.com> | 2018-01-05 20:07:03 -0500 |
|---|---|---|
| committer | Alexander Pickering <alexandermpickering@gmail.com> | 2018-01-05 20:07:03 -0500 |
| commit | 59c84b0460418983b0e655fd9fbba34e19b10291 (patch) | |
| tree | 21dce8b7a1f6c3f4e2eaab3180866f609c5d56d5 | |
| parent | 9c7882627fd28c060f53d86e194fb864302a347c (diff) | |
| download | libctemplates-59c84b0460418983b0e655fd9fbba34e19b10291.tar.gz libctemplates-59c84b0460418983b0e655fd9fbba34e19b10291.tar.bz2 libctemplates-59c84b0460418983b0e655fd9fbba34e19b10291.zip | |
Started writing unit tests
Added some tests for good input.
Also fixed a bug where an empty loop would crash.
| -rw-r--r-- | Makefile | 13 | ||||
| -rw-r--r-- | ctemplates.c | 48 | ||||
| -rw-r--r-- | ctemplates.h | 5 | ||||
| -rw-r--r-- | ctemplates.o | bin | 0 -> 156366 bytes | |||
| -rw-r--r-- | t/test_1.c | 228 |
5 files changed, 282 insertions, 12 deletions
@@ -1,6 +1,10 @@ -CFLAGS = -I. -L. -std=c99 -pedantic -Wall -Werror -O3 +CFLAGS = -I. -L. -std=c99 -pedantic -Wall -Werror -O3 -g CC = gcc LIBNAME = libctemplates.a +BIN_POSTFIX = .exe + + +TEST_1_NAME = t\test_1$(BIN_POSTFIX) $(LIBNAME): ctemplates.o fbuf.o kmp.o hashmap.o ar rc $(LIBNAME) ctemplates.o fbuf.o kmp.o hashmap.o @@ -21,8 +25,11 @@ hashmap.o: hashmap.c hashmap.h clean: rm -f *.o *.a template -test: - cd t; ./test.sh +$(TEST_1_NAME): t\test_1.c $(LIBNAME) + $(CC) $(CFLAGS) -o t\test_1.exe t\test_1.c -lctemplates + +test: $(TEST_1_NAME) + $(TEST_1_NAME) install: $(LIBNAME) cp $(LIBNAME) /usr/local/lib diff --git a/ctemplates.c b/ctemplates.c index d858aa9..e9e36ff 100644 --- a/ctemplates.c +++ b/ctemplates.c @@ -747,7 +747,8 @@ TMPL_free_loop(struct TMPL_loop* tl){ if(tl->next != NULL){ TMPL_free_loop(tl->next); } - TMPL_free_varlist(tl->varlist); + if(tl->varlist != NULL) + TMPL_free_varlist(tl->varlist); free(tl); } @@ -874,6 +875,7 @@ render_text(struct TMPL_templates* t, struct TMPL_tagnode* node, struct TMPL_var int print_pair(any_t indent, any_t b){ struct TMPL_varitem* vi = (struct TMPL_varitem*)b; + printf("Print pair, varitem is %p, type is %d\n",(void*)vi,(int)vi->type); int* ip = (int*)indent; int ind = *ip; if(vi->type == vartype_var){ @@ -885,7 +887,9 @@ print_pair(any_t indent, any_t b){ struct TMPL_loop* cursor; printf("{\n"); ind++; - for(cursor = vi->item.l; cursor != NULL; cursor = cursor->next){ + /*If the loop doesn't have any varlists added, the cursor's varlist will be null*/ + for(cursor = vi->item.l; cursor != NULL && cursor->varlist != NULL; cursor = cursor->next){ + printf("First iteration though, cursor was %p\n",(void*)cursor); print_varlist_helper(cursor->varlist,ind); } printf("}\n"); @@ -911,12 +915,20 @@ render_if(struct TMPL_templates* t, struct TMPL_tagnode* node, struct TMPL_varli struct TMPL_varitem* vi; int err = hashmap_get(varlist->map,varname,(void**)&vi); struct TMPL_tagnode* cursor; - if(err == MAP_OK && strcmp(vi->item.s,testval) == 0){ - cursor = node->TMPL_tag.ifelse.tbranch; - while(cursor != NULL){ - render_any(t,cursor,varlist); - cursor = cursor->next; + if(err == MAP_OK){ + if(testval == NULL){/*Use this if as an existance check*/ + + }else if(strcmp(vi->item.s, testval) == 0){ + cursor = node->TMPL_tag.ifelse.tbranch; + while(cursor != NULL){ + render_any(t,cursor,varlist); + cursor = cursor->next; + } + + }else{ + } + }else if(node->TMPL_tag.ifelse.fbranch != NULL){ cursor = node->TMPL_tag.ifelse.fbranch; while(cursor != NULL){ @@ -934,10 +946,27 @@ render_elseif(struct TMPL_templates* t, struct TMPL_tagnode* node, struct TMPL_v } int +resolve_name(struct TMPL_varlist* varlist, char* name,struct TMPL_varitem** item){ + return hashmap_get(varlist->map,name,(void*)item); + /* + struct TMPL_varlist* cursor = varlist; + int err; + do{ + err = hashmap_get(cursor->map,name,(void*)item); + cursor = cursor->parent; + }while(cursor != NULL && err == MAP_MISSING); + if(err == MAP_MISSING){ + return MAP_MISSING; + } + return MAP_OK; + */ +} + +int render_loop(struct TMPL_templates* t, struct TMPL_tagnode* node, struct TMPL_varlist* varlist){ char* loopname = node->TMPL_tag.loop.loopname; struct TMPL_varitem* loop; - int err = hashmap_get(varlist->map,loopname,(void*)&loop); + int err = resolve_name(varlist,loopname,&loop); if(err != MAP_OK){ return -1; } @@ -946,7 +975,8 @@ render_loop(struct TMPL_templates* t, struct TMPL_tagnode* node, struct TMPL_var nt->errout = t->errout; nt->roottag = node->TMPL_tag.loop.body; struct TMPL_loop* cursor; - for(cursor = loop->item.l; cursor != NULL; cursor = cursor->next){ + /*If the loop has no items, it's varlist will be null*/ + for(cursor = loop->item.l; cursor != NULL && cursor->varlist != NULL; cursor = cursor->next){ if(err != 0){ } TMPL_render_helper(nt,cursor->varlist); diff --git a/ctemplates.h b/ctemplates.h index 3dba429..48237b4 100644 --- a/ctemplates.h +++ b/ctemplates.h @@ -81,6 +81,7 @@ struct TMPL_light_string{ }; enum TMPL_vartype{ + vartype_null = 0, vartype_loop, vartype_var }; @@ -109,6 +110,8 @@ struct TMPL_varitem{ struct TMPL_tagnode{ enum TMPL_tagtype type; struct TMPL_tagnode* next; + unsigned int line; + unsigned int character; union{ /*Text*/ struct{ @@ -172,6 +175,8 @@ struct TMPL_token{ enum TMPL_tagtype type;//The type of token size_t length;//Length of token, should always be end-start struct TMPL_token* next;//The next token + unsigned int line; + unsigned int character; }TMPL_token; void TMPL_add_var_to_varlist(struct TMPL_varlist* vl, char* name, char* value); diff --git a/ctemplates.o b/ctemplates.o Binary files differnew file mode 100644 index 0000000..5536579 --- /dev/null +++ b/ctemplates.o diff --git a/t/test_1.c b/t/test_1.c new file mode 100644 index 0000000..038f7bb --- /dev/null +++ b/t/test_1.c @@ -0,0 +1,228 @@ +/* Test some good inputs + * Test 1: Basic, var + * Test 2: var w/ default value + */ + +#include <stdio.h> + +#include <ctemplates.h> + +char t_1[] = "This is a <TMPL_VAR name=\"what\">!"; +char c_1_1[] = "This is a template!"; + +char t_2[] = "This is a <TMPL_VAR name=\"missing\" default=\"program\">!"; +char c_2_1[] = "This is a program!"; + +char t_3[] = "<TMPL_VAR name=\"what\"> starts with a tag."; +char c_3_1[] = "This starts with a tag."; + +char t_4[] = "<TMPL_VAR name=\"missing\" default=\"That\"> starts with a tag."; +char c_4_1[] = "That starts with a tag."; + +char t_5[] = "This ends with a <TMPL_VAR name=\"what\">"; +char c_5_1[] = "This ends with a tag"; + +char t_6[] = "This ends with a <TMPL_VAR name=\"missing\" default=\"default tag\">"; +char c_6_1[] = "This ends with a default tag"; + +char t_7[] = "Test loop<TMPL_LOOP name=\"loop\"><TMPL_VAR name=\"var\"><TMPL_END>!"; +char c_7_1[] = "Test loop!"; +char c_7_2[] = "Test loop one!"; +char c_7_3[] = "Test loop one two three!"; + +char t_8[] = "Test if as check:<TMPL_IF name=\"var\" value=\"correct\"> Works!<TMPL_END>"; +char c_8_1[] = "Test if as check: Works!"; +char c_8_2[] = "Test if as check:"; +char c_8_3[] = "Test if as check:"; + +char t_9[] = "Test if for existance:<TMPL_IF name=\"var\"> Exists!<TMPL_END>"; +char c_9_1[] = "Test if for existance: Exists!"; +char c_9_2[] = "Test if for existance:"; + +int main(){ + struct TMPL_templates* t; + struct TMPL_varlist* vl; + char* ret; + size_t dummy; + + /*Test 1*/ + t = TMPL_alloc_template(t_1); + vl = TMPL_alloc_varlist(); + TMPL_add_var_to_varlist(vl,"what","template"); + ret = TMPL_render(t,vl,&dummy); + if(strcmp(ret,c_1_1) != 0){ + fprintf(stderr,"Error in test file 1, test 1\n"); + printf("Result should have been '%s'\n was '%s'\n",c_1_1,ret); + return -1; + } + TMPL_free_template(t); + TMPL_free_varlist(vl); + + /*Test 2*/ + t = TMPL_alloc_template(t_2); + vl = TMPL_alloc_varlist(); + ret = TMPL_render(t,vl,&dummy); + if(strcmp(ret,c_2_1) != 0){ + fprintf(stderr,"Error in test file 1, test 2\n"); + printf("Result should have been '%s'\n was '%s'\n",c_2_1,ret); + return -1; + } + TMPL_free_template(t); + TMPL_free_varlist(vl); + + /*Test 3*/ + t = TMPL_alloc_template(t_3); + vl = TMPL_alloc_varlist(); + TMPL_add_var_to_varlist(vl,"what","This"); + ret = TMPL_render(t,vl,&dummy); + if(strcmp(ret,c_3_1) != 0){ + fprintf(stderr,"Error in test file 1, test 3\n"); + printf("Result should have been '%s'\n was '%s'\n",c_3_1,ret); + return -1; + } + TMPL_free_template(t); + TMPL_free_varlist(vl); + + /*Test 4*/ + t = TMPL_alloc_template(t_4); + vl = TMPL_alloc_varlist(); + ret = TMPL_render(t,vl,&dummy); + if(strcmp(ret,c_4_1) != 0){ + fprintf(stderr,"Error in test file 1, test 4\n"); + printf("Result should have been '%s'\n was '%s'\n",c_4_1,ret); + return -1; + } + TMPL_free_template(t); + TMPL_free_varlist(vl); + + /*Test 5*/ + t = TMPL_alloc_template(t_5); + vl = TMPL_alloc_varlist(); + TMPL_add_var_to_varlist(vl,"what","tag"); + ret = TMPL_render(t,vl,&dummy); + if(strcmp(ret,c_5_1) != 0){ + fprintf(stderr, "Error in test file 1, test 5\n"); + printf("Result should have been '%s'\n was '%s'\n",c_5_1,ret); + return -1; + } + TMPL_free_template(t); + TMPL_free_varlist(vl); + + /*Test 6*/ + t = TMPL_alloc_template(t_6); + vl = TMPL_alloc_varlist(); + ret = TMPL_render(t,vl,&dummy); + if(strcmp(ret,c_6_1) != 0){ + fprintf(stderr, "Error in test file 1, test 5\n"); + printf("Result should have been '%s'\n was '%s'\n",c_6_1,ret); + return -1; + } + TMPL_free_template(t); + TMPL_free_varlist(vl); + + /*Test 7*/ + t = TMPL_alloc_template(t_7); + vl = TMPL_alloc_varlist(); + { + struct TMPL_loop* l = TMPL_alloc_loop(); + TMPL_add_loop_to_varlist(vl,"loop",l); + } + ret = TMPL_render(t,vl,&dummy); + if(strcmp(ret,c_7_1) != 0){ + fprintf(stderr, "Error in test file 1, test 5\n"); + printf("Result should have been '%s'\n was '%s'\n",c_7_1,ret); + return -1; + } + TMPL_free_varlist(vl); + vl = TMPL_alloc_varlist(); + { + struct TMPL_loop* l = TMPL_alloc_loop(); + struct TMPL_varlist* vl_2 = TMPL_alloc_varlist(); + TMPL_add_var_to_varlist(vl_2,"var"," one"); + TMPL_add_varlist_to_loop(l,vl_2); + TMPL_add_loop_to_varlist(vl,"loop",l); + } + ret = TMPL_render(t,vl,&dummy); + if(strcmp(ret,c_7_2) != 0){ + fprintf(stderr, "Error in test file 1, test 5\n"); + printf("Result should have been '%s'\n was '%s'\n",c_7_1,ret); + return -1; + } + TMPL_free_varlist(vl); + vl = TMPL_alloc_varlist(); + { + struct TMPL_loop* l = TMPL_alloc_loop(); + struct TMPL_varlist* vl_2 = TMPL_alloc_varlist(); + struct TMPL_varlist* vl_3 = TMPL_alloc_varlist(); + struct TMPL_varlist* vl_4 = TMPL_alloc_varlist(); + TMPL_add_var_to_varlist(vl_2,"var"," one"); + TMPL_add_var_to_varlist(vl_3,"var"," two"); + TMPL_add_var_to_varlist(vl_4,"var"," three"); + TMPL_add_varlist_to_loop(l,vl_2); + TMPL_add_varlist_to_loop(l,vl_3); + TMPL_add_varlist_to_loop(l,vl_4); + TMPL_add_loop_to_varlist(vl,"loop",l); + } + ret = TMPL_render(t,vl,&dummy); + if(strcmp(ret,c_7_3) != 0){ + fprintf(stderr, "Error in test file 1, test 5\n"); + printf("Result should have been '%s'\n was '%s'\n",c_7_1,ret); + return -1; + } + TMPL_free_varlist(vl); + TMPL_free_template(t); + + /*Test 8*/ + t = TMPL_alloc_template(t_8); + vl = TMPL_alloc_varlist(); + TMPL_add_var_to_varlist(vl,"var","correct"); + ret = TMPL_render(t,vl,&dummy); + if(strcmp(ret,c_8_1) != 0){ + fprintf(stderr, "Error in test file 1, test 5\n"); + printf("Result should have been '%s'\n was '%s'\n",c_8_1,ret); + return -1; + } + TMPL_free_varlist(vl); + vl = TMPL_alloc_varlist(); + TMPL_add_var_to_varlist(vl,"var","incorrect"); + ret = TMPL_render(t,vl,&dummy); + if(strcmp(ret,c_8_2) != 0){ + fprintf(stderr, "Error in test file 1, test 5\n"); + printf("Result should have been '%s'\n was '%s'\n",c_8_2,ret); + return -1; + } + TMPL_free_varlist(vl); + vl = TMPL_alloc_varlist(); + ret = TMPL_render(t,vl,&dummy); + if(strcmp(ret,c_8_3) != 0){ + fprintf(stderr, "Error in test file 1, test 5\n"); + printf("Result should have been '%s'\n was '%s'\n",c_8_3,ret); + return -1; + } + TMPL_free_template(t); + TMPL_free_varlist(vl); + + t = TMPL_alloc_template(t_9); + vl = TMPL_alloc_varlist(); + TMPL_add_var_to_varlist(vl,"var","thing"); + ret = TMPL_render(t,vl,&dummy); + if(strcmp(ret,c_9_1) != 0){ + fprintf(stderr, "Error in test file 1, test 5\n"); + printf("Result should have been '%s'\n was '%s'\n",c_9_1,ret); + return -1; + } + TMPL_free_varlist(vl); + vl = TMPL_alloc_varlist(); + ret = TMPL_render(t,vl,&dummy); + if(strcmp(ret,c_9_2) != 0){ + fprintf(stderr, "Error in test file 1, test 5\n"); + printf("Result should have been '%s'\n was '%s'\n",c_9_1,ret); + return -1; + } + TMPL_free_varlist(vl); + TMPL_free_template(t); + + + + return 0; +} |
