aboutsummaryrefslogtreecommitdiff
path: root/t/test_1.c
blob: 67461cca49e9c0d0d7e0069fa788b6a61f7d711f (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/* Test some good inputs
 * Test 1: Basic, var
 * Test 2: var w/ default value
 */

#include <stdio.h>

#include <ctemplates.h>

char t_1[] = "This is a <TMPL_VAR name=\"what\">!";
char c_1_1[] = "This is a template!";

char t_2[] = "This is a <TMPL_VAR name=\"missing\" default=\"program\">!";
char c_2_1[] = "This is a program!";

char t_3[] = "<TMPL_VAR name=\"what\"> starts with a tag.";
char c_3_1[] = "This starts with a tag.";

char t_4[] = "<TMPL_VAR name=\"missing\" default=\"That\"> starts with a tag.";
char c_4_1[] = "That starts with a tag.";

char t_5[] = "This ends with a <TMPL_VAR name=\"what\">";
char c_5_1[] = "This ends with a tag";

char t_6[] = "This ends with a <TMPL_VAR name=\"missing\" default=\"default tag\">";
char c_6_1[] = "This ends with a default tag";

char t_7[] = "Test loop<TMPL_LOOP name=\"loop\"><TMPL_VAR name=\"var\"><TMPL_END>!";
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:<TMPL_IF name=\"var\" value=\"correct\"> Works!<TMPL_END>";
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:<TMPL_IF name=\"var\"> Exists!<TMPL_END>";
char c_9_1[] = "Test if for existance: Exists!";
char c_9_2[] = "Test if for existance:";

char t_10[] = "Test else:<TMPL_IF name=\"var\"> One<TMPL_ELSE> Two<TMPL_END>";
char c_10_1[] = "Test else: One";
char c_10_2[] = "Test else: Two";

char t_11[] = "Test else2:<TMPL_IF name=\"var\" value=\"thing\"> One<TMPL_ELSE> Two<TMPL_END>";
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:";

char t_13[] = "Test else4:<TMPL_IF name=\"var\" value=\"one\"> One<TMPL_ELSEIF name=\"var2\" value=\"two\"> Two<TMPL_ELSE> Three<TMPL_END>";
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:<TMPL_IF name=\"var\" value=\"\"> Correct!<TMPL_END>";
char c_14_1[] = "Test if with empty value: Correct!";

char t_15[] = "Test elseif with empty value:<TMPL_IF name=\"missing\"> Wrong<TMPL_ELSEIF name=\"var\" value=\"\"> Correct!<TMPL_END>";
char c_15_1[] = "Test elseif with empty value: Correct!";

int main(){
	struct TMPL_templates* t;
	struct TMPL_varlist* vl;
	char* ret;
	size_t dummy;
	
	/*Test 1: Variable*/
	t = TMPL_alloc_template(t_1);
	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);

	/*Test 2: Variable with default parameter*/
	t = TMPL_alloc_template(t_2);
	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);

	/*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");
	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);

	/*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);
	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);

	/*Test 5: Ends with a tag*/
	t = TMPL_alloc_template(t_5);
	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);

	/*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);
	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);

	/*Test 7: Simple loop*/
	t = TMPL_alloc_template(t_7);
	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_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();
		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_1,ret);
		return -1;
	}
	TMPL_free_varlist(vl);
	TMPL_free_template(t);
	
	/*Test 8: Simple if*/
	t = TMPL_alloc_template(t_8);
	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);

	/*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");
	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);

	/*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");
	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);

	/*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);
	
	/*Test 13: If with elseif, no else*/
	t = TMPL_alloc_template(t_13);
	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);
	
	/*Test 14: If with with empty value*/
	t = TMPL_alloc_template(t_14);
	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);

	/*Test 15: Elseif with with empty value*/
	t = TMPL_alloc_template(t_15);
	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);
	
	
	
	return 0;
}