/* 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"; #define log(x) printf(x) #define template(x,y) \ err = TMPL_alloc_template(x,&y);\ if(err != 0){\ printf("Error: %s\n",TMPL_err(y,&dummy));\ return -1;\ } int main(){ log("Running tests\n"); struct TMPL_templates* t; struct TMPL_varlist* vl; char* ret; size_t dummy; int err; /*Test 1: Variable*/ template(t_1,t); 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); log("Test 1 complete\n"); /*Test 2: Variable with default parameter*/ template(t_2,t); 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); log("Test 2 complete\n"); /*Test 3: Template that starts with a tag*/ template(t_3,t); 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); log("Test 3 complete\n"); /*Test 4: Starts with a tag with a default value*/ template(t_4,t); 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); log("Test 4 complete\n"); /*Test 5: Ends with a tag*/ template(t_5,t); 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); log("Test 5 complete\n"); /*Test 6: Ends with a tag with a default value*/ template(t_6,t); 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); log("Test 6 complete\n"); /*Test 7: Simple loop*/ template(t_7,t); 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); log("Test 7 complete\n"); /*Test 8: Simple if*/ template(t_8,t); 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); log("Test 8 complete\n"); /*Test 9: Use if to check for variable existance*/ template(t_9,t); 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); log("Test 9 complete\n"); /*Test 10: If with else section, if used to check for existance*/ template(t_10,t); 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); log("Test 10 complete\n"); /*Test 11: If with else section, if used for equivalence*/ template(t_11,t); 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); log("Test 11 complete\n"); /*Test 12: If with elseif, no else*/ template(t_12,t); 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); log("Test 12 complete\n"); /*Test 13: If with elseif, no else*/ template(t_13,t); 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); log("Test 13 complete\n"); /*Test 14: If with with empty value*/ template(t_14,t); 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); log("Test 14 complete\n"); /*Test 15: Elseif with with empty value*/ template(t_15,t); 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); log("Test 15 complete\n"); /*Test 16: var with multiple spaces in between*/ template(t_16,t); 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); log("Test 16 complete\n"); /*Test 17: var with multiple spaces in between, and a default*/ template(t_17,t); 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); log("Test 17 complete\n"); /*Test 18: spaces between attribute and value*/ template(t_18,t); vl = TMPL_alloc_varlist(); ret = TMPL_render(t,vl,&dummy); 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); log("Test 18 complete\n"); return 0; }