aboutsummaryrefslogtreecommitdiff
path: root/t/test_3.c
blob: 5cb2b0c1868bfb844a67c88a3b93bec570ea1623 (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
73
74
75
76
77
78
#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!";
char c_2_3[] = "Render error: Variable \"loop1\" was not bound to a value\n";

#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,t,e)\
	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{\
		if(strcmp(TMPL_err(t,NULL),e) != 0){\
			fprintf(stderr, "Error in test 3.%d.%d\n",n,n2);\
			printf("Error was: '%s'\nShould have been: '%s'\n",TMPL_err(t,NULL),e);\
			return -1;\
		}\
		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 tests3\n");
	struct TMPL_templates* t;
	struct TMPL_varlist *vl;
	struct TMPL_loop *l1;
	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,t,c_2_3);
	TMPL_free_template(t);
	TMPL_free_varlist(vl);
	log("Test 3 complete\n");

	return 0;
}