aboutsummaryrefslogtreecommitdiff
path: root/t
diff options
context:
space:
mode:
authorAlexander Pickering <alexandermpickering@gmail.com>2019-11-21 18:17:18 -0500
committerAlexander Pickering <alexandermpickering@gmail.com>2019-11-21 18:24:30 -0500
commitd917129d502a753f13bd26ecf47a30d31f738fc1 (patch)
treea31bb887c0b27b47939eb377535388b1fea552c2 /t
parentd8987084b7aa3c47642af30a87c0673a2df01fd0 (diff)
downloadlibctemplates-d917129d502a753f13bd26ecf47a30d31f738fc1.tar.gz
libctemplates-d917129d502a753f13bd26ecf47a30d31f738fc1.tar.bz2
libctemplates-d917129d502a753f13bd26ecf47a30d31f738fc1.zip
Fixed empty loop causeing infinite loop
If a loop was defined in the template, but no loop was defined in the varlist, render() would infinite loop while trying to execute. Fixs that.
Diffstat (limited to 't')
-rw-r--r--t/test_3.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/t/test_3.c b/t/test_3.c
new file mode 100644
index 0000000..dc55aa6
--- /dev/null
+++ b/t/test_3.c
@@ -0,0 +1,72 @@
+#include <stdio.h>
+
+#include <ctemplates.h>
+
+//Nested loops w/ break
+char t_1[] = "What is a loop with no items?<TMPL_LOOP name=\"loop1\">Something<TMPL_END>\nA sad little pile of bits!";
+char c_1_1[] = "What is a loop with no items?\nA sad little pile of bits!";
+char c_1_2[] = "What is a loop with no items?\nA sad little pile of bits!";
+
+#define log(x) printf(x)
+
+#define check(n,n2,a,b)\
+ if(strcmp(a,b) != 0){\
+ fprintf(stderr, "Error in test 3.%d.%d\n",n,n2);\
+ printf("Result should have been '%s'\n was '%s'\n", b, a);\
+ if(t->error)\
+ printf(TMPL_err(t, NULL));\
+ return -1;\
+ }else{\
+ printf("\n\n##########\n##Test %d.%d passed\n##########\n\n\n",n,n2);\
+ }
+
+#define check_isnull(n,n2,a)\
+ if(a != NULL){\
+ fprintf(stderr, "Error in test 3.%d.%d\n",n,n2);\
+ printf("Result should have been NULL\n was '%s'\n", a);\
+ if(t->error)\
+ printf(TMPL_err(t, NULL));\
+ return -1;\
+ }else{\
+ printf("\n\n##########\n##Test %d.%d passed\n##########\n\n\n",n,n2);\
+ }
+
+#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 tests2\n");
+ struct TMPL_templates* t;
+ struct TMPL_varlist *vl,*vl1,*vl2;
+ struct TMPL_loop *l1,*l2;
+ char* ret;
+ size_t dummy;
+ int err;
+
+ /*Test 1: Variable*/
+ template(t_1,t);
+ vl = TMPL_alloc_varlist();
+ l1 = TMPL_alloc_loop();
+ TMPL_add_loop_to_varlist(vl,"loop1",l1);
+ ret = TMPL_render(t,vl,&dummy);
+ check(1,1,ret,c_1_1);
+ TMPL_free_template(t);
+ TMPL_free_varlist(vl);
+ log("Test 1 complete\n");
+
+ template(t_1,t);
+ vl = TMPL_alloc_varlist();
+ l1 = TMPL_alloc_loop();
+ TMPL_add_loop_to_varlist(vl,"notaloop",l1);
+ ret = TMPL_render(t,vl,&dummy);
+ check_isnull(1,2,ret);
+ TMPL_free_template(t);
+ TMPL_free_varlist(vl);
+ log("Test 2 complete\n");
+
+ return 0;
+}