aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ctemplates.c22
-rw-r--r--t/test_1.c75
2 files changed, 78 insertions, 19 deletions
diff --git a/ctemplates.c b/ctemplates.c
index e79b676..8d4bbd6 100644
--- a/ctemplates.c
+++ b/ctemplates.c
@@ -522,17 +522,21 @@ parse_elseif(struct TMPL_token* head, struct TMPL_buf* errbuf){
t->TMPL_tag.ifelse.varname = name;
int testval_offset = kmp(start_of_attribs,head->length, ATTRIBUTE_VALUE,ATTRIBUTE_VALUE_LENGTH);
- char* start_of_value = start_of_attribs + testval_offset + ATTRIBUTE_VALUE_LENGTH;
- size_t value_length = get_quoted_string(start_of_value,head->length);
- char* value = (char*)malloc(sizeof(char)*value_length);
- memcpy(value,start_of_value,value_length);
- value[value_length] = '\0';
- t->TMPL_tag.ifelse.testval = value;
+ if(testval_offset == -1){
+ t->TMPL_tag.ifelse.testval = NULL;
+ }else{
+ char* start_of_value = start_of_attribs + testval_offset + ATTRIBUTE_VALUE_LENGTH;
+ size_t value_length = get_quoted_string(start_of_value,head->length);
+ char* value = (char*)malloc(sizeof(char)*value_length);
+ memcpy(value,start_of_value,value_length);
+ value[value_length] = '\0';
+ t->TMPL_tag.ifelse.testval = value;
+ }
struct TMPL_token* cursor = head->next;
int nest_level = 0;
struct TMPL_token* fstart = NULL;
while(nest_level > 0 || cursor->type != tag_end){
- if(nest_level == 0 && fstart == NULL){
+ if(fstart == NULL){
if(cursor->type == tag_elseif){
fstart = cursor;
}else if(cursor->type == tag_else){
@@ -559,8 +563,8 @@ parse_elseif(struct TMPL_token* head, struct TMPL_buf* errbuf){
}
t->TMPL_tag.ifelse.tbranch = parse(head->next,errbuf);
- t->next = NULL;
-
+ t->next = parse(cursor->next,errbuf);
+
t->type = tag_elseif;
return t;
diff --git a/t/test_1.c b/t/test_1.c
index aa72213..fc1550f 100644
--- a/t/test_1.c
+++ b/t/test_1.c
@@ -47,13 +47,18 @@ char t_11[] = "Test else2:<TMPL_IF name=\"var\" value=\"thing\"> One<TMPL_ELSE>
char c_11_1[] = "Test else2: One";
char c_11_2[] = "Test else2: Two";
+char t_12[] = "Test else3:<TMPL_IF name=\"var\"> One<TMPL_ELSEIF name=\"var2\"> Two<TMPL_END>";
+char c_12_1[] = "Test else3: One";
+char c_12_2[] = "Test else3: Two";
+char c_12_3[] = "Test else3:";
+
int main(){
struct TMPL_templates* t;
struct TMPL_varlist* vl;
char* ret;
size_t dummy;
- /*Test 1*/
+ /*Test 1: Variable*/
t = TMPL_alloc_template(t_1);
vl = TMPL_alloc_varlist();
TMPL_add_var_to_varlist(vl,"what","template");
@@ -66,7 +71,7 @@ int main(){
TMPL_free_template(t);
TMPL_free_varlist(vl);
- /*Test 2*/
+ /*Test 2: Variable with default parameter*/
t = TMPL_alloc_template(t_2);
vl = TMPL_alloc_varlist();
ret = TMPL_render(t,vl,&dummy);
@@ -78,7 +83,7 @@ int main(){
TMPL_free_template(t);
TMPL_free_varlist(vl);
- /*Test 3*/
+ /*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");
@@ -91,7 +96,7 @@ int main(){
TMPL_free_template(t);
TMPL_free_varlist(vl);
- /*Test 4*/
+ /*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);
@@ -103,7 +108,7 @@ int main(){
TMPL_free_template(t);
TMPL_free_varlist(vl);
- /*Test 5*/
+ /*Test 5: Ends with a tag*/
t = TMPL_alloc_template(t_5);
vl = TMPL_alloc_varlist();
TMPL_add_var_to_varlist(vl,"what","tag");
@@ -116,7 +121,7 @@ int main(){
TMPL_free_template(t);
TMPL_free_varlist(vl);
- /*Test 6*/
+ /*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);
@@ -128,7 +133,7 @@ int main(){
TMPL_free_template(t);
TMPL_free_varlist(vl);
- /*Test 7*/
+ /*Test 7: Simple loop*/
t = TMPL_alloc_template(t_7);
vl = TMPL_alloc_varlist();
{
@@ -180,7 +185,7 @@ int main(){
TMPL_free_varlist(vl);
TMPL_free_template(t);
- /*Test 8*/
+ /*Test 8: Simple if*/
t = TMPL_alloc_template(t_8);
vl = TMPL_alloc_varlist();
TMPL_add_var_to_varlist(vl,"var","correct");
@@ -210,7 +215,7 @@ int main(){
TMPL_free_template(t);
TMPL_free_varlist(vl);
- /*Test 9*/
+ /*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");
@@ -231,7 +236,7 @@ int main(){
TMPL_free_varlist(vl);
TMPL_free_template(t);
- /*Test 10*/
+ /*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");
@@ -252,7 +257,57 @@ int main(){
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);
+
return 0;
}