aboutsummaryrefslogtreecommitdiff
path: root/t/test_3.c
blob: dc55aa6121a6d3369be74ef7d882605a092230c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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;
}