/* Test some good inputs * Test 1: Basic, var * Test 2: var w/ default value */ #include #include char t_1[] = "This is a !"; char c_1_1[] = "This is a template!"; char t_2[] = "This is a !"; char c_2_1[] = "This is a program!"; char t_3[] = " starts with a tag."; char c_3_1[] = "This starts with a tag."; char t_4[] = " starts with a tag."; char c_4_1[] = "That starts with a tag."; char t_5[] = "This ends with a "; char c_5_1[] = "This ends with a tag"; char t_6[] = "This ends with a "; char c_6_1[] = "This ends with a default tag"; char t_7[] = "Test loop!"; 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: Works!"; 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: Exists!"; char c_9_1[] = "Test if for existance: Exists!"; char c_9_2[] = "Test if for existance:"; char t_10[] = "Test else: One Two"; char c_10_1[] = "Test else: One"; char c_10_2[] = "Test else: Two"; char t_11[] = "Test else2: One Two"; char c_11_1[] = "Test else2: One"; char c_11_2[] = "Test else2: Two"; char t_12[] = "Test else3: One Two"; char c_12_1[] = "Test else3: One"; char c_12_2[] = "Test else3: Two"; char c_12_3[] = "Test else3:"; char t_13[] = "Test else4: One Two Three"; char c_13_1[] = "Test else4: One"; char c_13_2[] = "Test else4: Two"; char c_13_3[] = "Test else4: Three"; char t_14[] = "Test if with empty value: Correct!"; char c_14_1[] = "Test if with empty value: Correct!"; char t_15[] = "Test elseif with empty value: Wrong Correct!"; char c_15_1[] = "Test elseif with empty value: Correct!"; char t_16[] = "Test var with spaces:"; char c_16_1[] = "Test var with spaces:Test"; char t_17[] = "Test var with spaces and default:"; char c_17_1[] = "Test var with spaces and default:Correct!"; char t_18[] = "Test var with spaces between attribute and name:"; char c_18_1[] = "Test var with spaces between attribute and name:Correct!"; char c_18_2[] = "Test var with spaces between attribute and name:Blah"; int main(){ struct TMPL_templates* t; struct TMPL_varlist* vl; char* ret; size_t dummy; /*Test 1: Variable*/ 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: Variable with default parameter*/ 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: Template that starts with a tag*/ 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: Starts with a tag with a default value*/ 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: Ends with a tag*/ 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: Ends with a tag with a default value*/ 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 6\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: Simple loop*/ 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 7\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 7\n"); printf("Result should have been '%s'\n was '%s'\n",c_7_2,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 7\n"); printf("Result should have been '%s'\n was '%s'\n",c_7_3,ret); return -1; } TMPL_free_varlist(vl); TMPL_free_template(t); /*Test 8: Simple if*/ 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 8\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 8\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 8\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); /*Test 9: Use if to check for variable existance*/ 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 9\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 9\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); /*Test 10: If with else section, if used to check for existance*/ t = TMPL_alloc_template(t_10); vl = TMPL_alloc_varlist(); TMPL_add_var_to_varlist(vl,"var","True"); ret = TMPL_render(t,vl,&dummy); if(strcmp(ret, c_10_1) != 0){ fprintf(stderr, "Error in test file 1, test 10\n"); printf("Result should have been '%s'\n was '%s'\n",c_10_1,ret); return -1; } TMPL_free_varlist(vl); vl = TMPL_alloc_varlist(); ret = TMPL_render(t,vl,&dummy); if(strcmp(ret, c_10_2) != 0){ fprintf(stderr, "Error in test file 1, test 10\n"); printf("Result should have been '%s'\n was '%s'\n",c_10_2,ret); return -1; } TMPL_free_varlist(vl); TMPL_free_template(t); /*Test 11: If with else section, if used for equivalence*/ t = TMPL_alloc_template(t_11); vl = TMPL_alloc_varlist(); TMPL_add_var_to_varlist(vl,"var","thing"); ret = TMPL_render(t,vl,&dummy); if(strcmp(ret, c_11_1) != 0){ fprintf(stderr, "Error in test file 1, test 11\n"); printf("Result should have been '%s'\n was '%s'\n",c_11_1,ret); return -1; } TMPL_free_varlist(vl); vl = TMPL_alloc_varlist(); ret = TMPL_render(t,vl,&dummy); if(strcmp(ret, c_11_2) != 0){ fprintf(stderr, "Error in test file 1, test 11\n"); printf("Result should have been '%s'\n was '%s'\n",c_11_2,ret); return -1; } TMPL_free_varlist(vl); TMPL_free_template(t); /*Test 12: If with elseif, no else*/ t = TMPL_alloc_template(t_12); vl = TMPL_alloc_varlist(); TMPL_add_var_to_varlist(vl,"var","thing"); ret = TMPL_render(t,vl,&dummy); if(strcmp(ret, c_12_1) != 0){ fprintf(stderr, "Error in test file 1, test 12\n"); printf("Result should have been '%s'\n was '%s'\n",c_12_1,ret); return -1; } TMPL_free_varlist(vl); vl = TMPL_alloc_varlist(); TMPL_add_var_to_varlist(vl,"var2","thing"); ret = TMPL_render(t,vl,&dummy); if(strcmp(ret, c_12_2) != 0){ fprintf(stderr, "Error in test file 1, test 12\n"); printf("Result should have been '%s'\n was '%s'\n",c_12_2,ret); return -1; } TMPL_free_varlist(vl); vl = TMPL_alloc_varlist(); ret = TMPL_render(t,vl,&dummy); if(strcmp(ret, c_12_3) != 0){ fprintf(stderr, "Error in test file 1, test 12\n"); printf("Result should have been '%s'\n was '%s'\n",c_12_3,ret); return -1; } TMPL_free_varlist(vl); TMPL_free_template(t); /*Test 13: If with elseif, no else*/ t = TMPL_alloc_template(t_13); vl = TMPL_alloc_varlist(); TMPL_add_var_to_varlist(vl,"var","one"); ret = TMPL_render(t,vl,&dummy); if(strcmp(ret, c_13_1) != 0){ fprintf(stderr, "Error in test file 1, test 13\n"); printf("Result should have been '%s'\n was '%s'\n",c_13_1,ret); return -1; } TMPL_free_varlist(vl); vl = TMPL_alloc_varlist(); TMPL_add_var_to_varlist(vl,"var2","two"); ret = TMPL_render(t,vl,&dummy); if(strcmp(ret, c_13_2) != 0){ fprintf(stderr, "Error in test file 1, test 13\n"); printf("Result should have been '%s'\n was '%s'\n",c_13_2,ret); return -1; } TMPL_free_varlist(vl); vl = TMPL_alloc_varlist(); ret = TMPL_render(t,vl,&dummy); if(strcmp(ret, c_13_3) != 0){ fprintf(stderr, "Error in test file 1, test 13\n"); printf("Result should have been '%s'\n was '%s'\n",c_13_3,ret); return -1; } TMPL_free_varlist(vl); TMPL_free_template(t); /*Test 14: If with with empty value*/ t = TMPL_alloc_template(t_14); vl = TMPL_alloc_varlist(); TMPL_add_var_to_varlist(vl,"var",""); ret = TMPL_render(t,vl,&dummy); if(strcmp(ret, c_14_1) != 0){ fprintf(stderr, "Error in test file 1, test 14\n"); printf("Result should have been '%s'\n was '%s'\n",c_14_1,ret); return -1; } TMPL_free_varlist(vl); TMPL_free_template(t); /*Test 15: Elseif with with empty value*/ t = TMPL_alloc_template(t_15); vl = TMPL_alloc_varlist(); TMPL_add_var_to_varlist(vl,"var",""); ret = TMPL_render(t,vl,&dummy); if(strcmp(ret, c_15_1) != 0){ fprintf(stderr, "Error in test file 1, test 15\n"); printf("Result should have been '%s'\n was '%s'\n",c_15_1,ret); return -1; } TMPL_free_varlist(vl); TMPL_free_template(t); /*Test 16: var with multiple spaces in between*/ t = TMPL_alloc_template(t_16); vl = TMPL_alloc_varlist(); TMPL_add_var_to_varlist(vl,"test","Test"); ret = TMPL_render(t,vl,&dummy); if(strcmp(ret,c_16_1) != 0){ fprintf(stderr, "Error in test file 1, test 16\n"); printf("Result should have been '%s\n was '%s'\n",c_16_1,ret); return -1; } TMPL_free_varlist(vl); TMPL_free_template(t); /*Test 17: var with multiple spaces in between, and a default*/ t = TMPL_alloc_template(t_17); vl = TMPL_alloc_varlist(); ret = TMPL_render(t,vl,&dummy); if(strcmp(ret,c_17_1) != 0){ fprintf(stderr, "Error in test file 1, test 17\n"); printf("Result should have been '%s\n was '%s'\n",c_17_1,ret); return -1; } TMPL_free_varlist(vl); TMPL_free_template(t); /*printf("Before test 18\n");*/ /*Test 18: spaces between attribute and value*/ t = TMPL_alloc_template(t_18); /*print_ast(t->roottag);*/ /*printf("Alloced template\n");*/ vl = TMPL_alloc_varlist(); /*printf("Made varles\n");*/ ret = TMPL_render(t,vl,&dummy); /*printf("Rendered\n");*/ if(strcmp(ret,c_18_1) != 0){ fprintf(stderr, "Error in test file 1, test 18\n"); printf("Result should have been '%s\n was '%s'\n",c_18_1,ret); return -1; } TMPL_free_varlist(vl); TMPL_free_template(t); return 0; }