aboutsummaryrefslogtreecommitdiffstats
path: root/tests/check_template_parser.c
blob: 94f669b41d8430746990f6d577ab36a22a47ae75 (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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
/*
 * blogc: A blog compiler.
 * Copyright (C) 2015 Rafael G. Martins <rafael@rafaelmartins.eng.br>
 *
 * This program can be distributed under the terms of the BSD License.
 * See the file LICENSE.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */

#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <string.h>
#include "../src/template-parser.h"
#include "../src/error.h"
#include "../src/utils/utils.h"


static void
blogc_assert_template_stmt(b_slist_t *l, const char *value,
    const blogc_template_stmt_type_t type)
{
    blogc_template_stmt_t *stmt = l->data;
    if (value == NULL)
        assert_null(stmt->value);
    else
        assert_string_equal(stmt->value, value);
    assert_int_equal(stmt->type, type);
}


static void
blogc_assert_template_if_stmt(b_slist_t *l, const char *variable,
    blogc_template_stmt_operator_t operator, const char *operand)
{
    blogc_template_stmt_t *stmt = l->data;
    assert_string_equal(stmt->value, variable);
    assert_int_equal(stmt->op, operator);
    assert_string_equal(stmt->value2, operand);
    assert_int_equal(stmt->type, BLOGC_TEMPLATE_IF_STMT);
}


static void
test_template_parse(void **state)
{
    const char *a =
        "Test\n"
        "\n"
        "    {% block entry %}\n"
        "{% ifdef CHUNDA %}\n"
        "bola\n"
        "{% endif %}\n"
        "{% ifndef BOLA %}\n"
        "bolao\n"
        "{% endif %}\n"
        "{% endblock %}\n"
        "{% block listing %}{{ BOLA }}{% endblock %}\n"
        "{% block listing_once %}asd{% endblock %}\n"
        "{% if BOLA == \"1\\\"0\" %}aee{% endif %}";
    blogc_error_t *err = NULL;
    b_slist_t *stmts = blogc_template_parse(a, strlen(a), &err);
    assert_null(err);
    assert_non_null(stmts);
    blogc_assert_template_stmt(stmts, "Test\n\n    ",
        BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(stmts->next, "entry",
        BLOGC_TEMPLATE_BLOCK_STMT);
    blogc_assert_template_stmt(stmts->next->next, "\n",
        BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(stmts->next->next->next, "CHUNDA",
        BLOGC_TEMPLATE_IFDEF_STMT);
    blogc_assert_template_stmt(stmts->next->next->next->next, "\nbola\n",
        BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(stmts->next->next->next->next->next, NULL,
        BLOGC_TEMPLATE_ENDIF_STMT);
    blogc_assert_template_stmt(stmts->next->next->next->next->next->next, "\n",
        BLOGC_TEMPLATE_CONTENT_STMT);
    b_slist_t *tmp = stmts->next->next->next->next->next->next->next;
    blogc_assert_template_stmt(tmp, "BOLA", BLOGC_TEMPLATE_IFNDEF_STMT);
    blogc_assert_template_stmt(tmp->next, "\nbolao\n", BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(tmp->next->next, NULL, BLOGC_TEMPLATE_ENDIF_STMT);
    blogc_assert_template_stmt(tmp->next->next->next, "\n",
        BLOGC_TEMPLATE_CONTENT_STMT);
    tmp = tmp->next->next->next->next;
    blogc_assert_template_stmt(tmp, NULL, BLOGC_TEMPLATE_ENDBLOCK_STMT);
    blogc_assert_template_stmt(tmp->next, "\n", BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(tmp->next->next, "listing",
        BLOGC_TEMPLATE_BLOCK_STMT);
    blogc_assert_template_stmt(tmp->next->next->next, "BOLA",
        BLOGC_TEMPLATE_VARIABLE_STMT);
    blogc_assert_template_stmt(tmp->next->next->next->next,
        NULL, BLOGC_TEMPLATE_ENDBLOCK_STMT);
    blogc_assert_template_stmt(tmp->next->next->next->next->next, "\n",
        BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(tmp->next->next->next->next->next->next,
        "listing_once", BLOGC_TEMPLATE_BLOCK_STMT);
    blogc_assert_template_stmt(tmp->next->next->next->next->next->next->next,
        "asd", BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(tmp->next->next->next->next->next->next->next->next,
        NULL, BLOGC_TEMPLATE_ENDBLOCK_STMT);
    blogc_assert_template_stmt(tmp->next->next->next->next->next->next->next->next->next,
        "\n", BLOGC_TEMPLATE_CONTENT_STMT);
    tmp = tmp->next->next->next->next->next->next->next->next->next->next;
    blogc_assert_template_if_stmt(tmp, "BOLA", BLOGC_TEMPLATE_OP_EQ, "1\\\"0");
    blogc_assert_template_stmt(tmp->next, "aee", BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(tmp->next->next, NULL,
        BLOGC_TEMPLATE_ENDIF_STMT);
    assert_null(tmp->next->next->next);
    blogc_template_free_stmts(stmts);
}


static void
test_template_parse_crlf(void **state)
{
    const char *a =
        "Test\r\n"
        "\r\n"
        "    {% block entry %}\r\n"
        "{% ifdef CHUNDA %}\r\n"
        "bola\r\n"
        "{% endif %}\r\n"
        "{% ifndef BOLA %}\r\n"
        "bolao\r\n"
        "{% endif %}\r\n"
        "{% endblock %}\r\n"
        "{% block listing %}{{ BOLA }}{% endblock %}\r\n"
        "{% block listing_once %}asd{% endblock %}\r\n"
        "{% if BOLA == \"1\\\"0\" %}aee{% endif %}";
    blogc_error_t *err = NULL;
    b_slist_t *stmts = blogc_template_parse(a, strlen(a), &err);
    assert_null(err);
    assert_non_null(stmts);
    blogc_assert_template_stmt(stmts, "Test\r\n\r\n    ",
        BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(stmts->next, "entry",
        BLOGC_TEMPLATE_BLOCK_STMT);
    blogc_assert_template_stmt(stmts->next->next, "\r\n",
        BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(stmts->next->next->next, "CHUNDA",
        BLOGC_TEMPLATE_IFDEF_STMT);
    blogc_assert_template_stmt(stmts->next->next->next->next, "\r\nbola\r\n",
        BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(stmts->next->next->next->next->next, NULL,
        BLOGC_TEMPLATE_ENDIF_STMT);
    blogc_assert_template_stmt(stmts->next->next->next->next->next->next, "\r\n",
        BLOGC_TEMPLATE_CONTENT_STMT);
    b_slist_t *tmp = stmts->next->next->next->next->next->next->next;
    blogc_assert_template_stmt(tmp, "BOLA", BLOGC_TEMPLATE_IFNDEF_STMT);
    blogc_assert_template_stmt(tmp->next, "\r\nbolao\r\n", BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(tmp->next->next, NULL, BLOGC_TEMPLATE_ENDIF_STMT);
    blogc_assert_template_stmt(tmp->next->next->next, "\r\n",
        BLOGC_TEMPLATE_CONTENT_STMT);
    tmp = tmp->next->next->next->next;
    blogc_assert_template_stmt(tmp, NULL, BLOGC_TEMPLATE_ENDBLOCK_STMT);
    blogc_assert_template_stmt(tmp->next, "\r\n", BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(tmp->next->next, "listing",
        BLOGC_TEMPLATE_BLOCK_STMT);
    blogc_assert_template_stmt(tmp->next->next->next, "BOLA",
        BLOGC_TEMPLATE_VARIABLE_STMT);
    blogc_assert_template_stmt(tmp->next->next->next->next,
        NULL, BLOGC_TEMPLATE_ENDBLOCK_STMT);
    blogc_assert_template_stmt(tmp->next->next->next->next->next, "\r\n",
        BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(tmp->next->next->next->next->next->next,
        "listing_once", BLOGC_TEMPLATE_BLOCK_STMT);
    blogc_assert_template_stmt(tmp->next->next->next->next->next->next->next,
        "asd", BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(tmp->next->next->next->next->next->next->next->next,
        NULL, BLOGC_TEMPLATE_ENDBLOCK_STMT);
    blogc_assert_template_stmt(tmp->next->next->next->next->next->next->next->next->next,
        "\r\n", BLOGC_TEMPLATE_CONTENT_STMT);
    tmp = tmp->next->next->next->next->next->next->next->next->next->next;
    blogc_assert_template_if_stmt(tmp, "BOLA", BLOGC_TEMPLATE_OP_EQ, "1\\\"0");
    blogc_assert_template_stmt(tmp->next, "aee", BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(tmp->next->next, NULL,
        BLOGC_TEMPLATE_ENDIF_STMT);
    assert_null(tmp->next->next->next);
    blogc_template_free_stmts(stmts);
}


static void
test_template_parse_html(void **state)
{
    const char *a =
        "<html>\n"
        "    <head>\n"
        "        {% block entry %}\n"
        "        <title>My cool blog >> {{ TITLE }}</title>\n"
        "        {% endblock %}\n"
        "        {% block listing_once %}\n"
        "        <title>My cool blog - Main page</title>\n"
        "        {% endblock %}\n"
        "    </head>\n"
        "    <body>\n"
        "        <h1>My cool blog</h1>\n"
        "        {% block entry %}\n"
        "        <h2>{{ TITLE }}</h2>\n"
        "        {% ifdef DATE %}<h4>Published in: {{ DATE }}</h4>{% endif %}\n"
        "        <pre>{{ CONTENT }}</pre>\n"
        "        {% endblock %}\n"
        "        {% block listing_once %}<ul>{% endblock %}\n"
        "        {% block listing %}<p><a href=\"{{ FILENAME }}.html\">"
        "{{ TITLE }}</a>{% ifdef DATE %} - {{ DATE }}{% endif %}</p>{% endblock %}\n"
        "        {% block listing_once %}</ul>{% endblock %}\n"
        "    </body>\n"
        "</html>\n";
    blogc_error_t *err = NULL;
    b_slist_t *stmts = blogc_template_parse(a, strlen(a), &err);
    assert_null(err);
    assert_non_null(stmts);
    blogc_assert_template_stmt(stmts, "<html>\n    <head>\n        ",
        BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(stmts->next, "entry",
        BLOGC_TEMPLATE_BLOCK_STMT);
    blogc_assert_template_stmt(stmts->next->next,
        "\n        <title>My cool blog >> ", BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(stmts->next->next->next, "TITLE",
        BLOGC_TEMPLATE_VARIABLE_STMT);
    blogc_assert_template_stmt(stmts->next->next->next->next,
        "</title>\n        ", BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(stmts->next->next->next->next->next, NULL,
        BLOGC_TEMPLATE_ENDBLOCK_STMT);
    blogc_assert_template_stmt(stmts->next->next->next->next->next->next,
        "\n        ", BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(stmts->next->next->next->next->next->next->next,
        "listing_once", BLOGC_TEMPLATE_BLOCK_STMT);
    b_slist_t *tmp = stmts->next->next->next->next->next->next->next->next;
    blogc_assert_template_stmt(tmp,
        "\n        <title>My cool blog - Main page</title>\n        ",
        BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(tmp->next, NULL, BLOGC_TEMPLATE_ENDBLOCK_STMT);
    blogc_assert_template_stmt(tmp->next->next,
        "\n    </head>\n    <body>\n        <h1>My cool blog</h1>\n        ",
        BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(tmp->next->next->next, "entry",
        BLOGC_TEMPLATE_BLOCK_STMT);
    blogc_assert_template_stmt(tmp->next->next->next->next,
        "\n        <h2>", BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(tmp->next->next->next->next->next,
        "TITLE", BLOGC_TEMPLATE_VARIABLE_STMT);
    blogc_assert_template_stmt(tmp->next->next->next->next->next->next,
        "</h2>\n        ", BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(tmp->next->next->next->next->next->next->next,
        "DATE", BLOGC_TEMPLATE_IFDEF_STMT);
    tmp = tmp->next->next->next->next->next->next->next->next;
    blogc_assert_template_stmt(tmp, "<h4>Published in: ",
        BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(tmp->next, "DATE", BLOGC_TEMPLATE_VARIABLE_STMT);
    blogc_assert_template_stmt(tmp->next->next, "</h4>",
        BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(tmp->next->next->next, NULL,
        BLOGC_TEMPLATE_ENDIF_STMT);
    blogc_assert_template_stmt(tmp->next->next->next->next, "\n        <pre>",
        BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(tmp->next->next->next->next->next,
        "CONTENT", BLOGC_TEMPLATE_VARIABLE_STMT);
    blogc_assert_template_stmt(tmp->next->next->next->next->next->next,
        "</pre>\n        ", BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(tmp->next->next->next->next->next->next->next,
        NULL, BLOGC_TEMPLATE_ENDBLOCK_STMT);
    tmp = tmp->next->next->next->next->next->next->next->next;
    blogc_assert_template_stmt(tmp, "\n        ", BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(tmp->next, "listing_once",
        BLOGC_TEMPLATE_BLOCK_STMT);
    blogc_assert_template_stmt(tmp->next->next, "<ul>",
        BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(tmp->next->next->next, NULL,
        BLOGC_TEMPLATE_ENDBLOCK_STMT);
    blogc_assert_template_stmt(tmp->next->next->next->next, "\n        ",
        BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(tmp->next->next->next->next->next,
        "listing", BLOGC_TEMPLATE_BLOCK_STMT);
    blogc_assert_template_stmt(tmp->next->next->next->next->next->next,
        "<p><a href=\"", BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(tmp->next->next->next->next->next->next->next,
        "FILENAME", BLOGC_TEMPLATE_VARIABLE_STMT);
    tmp = tmp->next->next->next->next->next->next->next->next;
    blogc_assert_template_stmt(tmp, ".html\">", BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(tmp->next, "TITLE",
        BLOGC_TEMPLATE_VARIABLE_STMT);
    blogc_assert_template_stmt(tmp->next->next, "</a>",
        BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(tmp->next->next->next, "DATE",
        BLOGC_TEMPLATE_IFDEF_STMT);
    blogc_assert_template_stmt(tmp->next->next->next->next, " - ",
        BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(tmp->next->next->next->next->next, "DATE",
        BLOGC_TEMPLATE_VARIABLE_STMT);
    blogc_assert_template_stmt(tmp->next->next->next->next->next->next,
        NULL, BLOGC_TEMPLATE_ENDIF_STMT);
    blogc_assert_template_stmt(tmp->next->next->next->next->next->next->next,
        "</p>", BLOGC_TEMPLATE_CONTENT_STMT);
    tmp = tmp->next->next->next->next->next->next->next->next;
    blogc_assert_template_stmt(tmp, NULL, BLOGC_TEMPLATE_ENDBLOCK_STMT);
    blogc_assert_template_stmt(tmp->next, "\n        ",
        BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(tmp->next->next, "listing_once",
        BLOGC_TEMPLATE_BLOCK_STMT);
    blogc_assert_template_stmt(tmp->next->next->next, "</ul>",
        BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(tmp->next->next->next->next, NULL,
        BLOGC_TEMPLATE_ENDBLOCK_STMT);
    blogc_assert_template_stmt(tmp->next->next->next->next->next,
        "\n    </body>\n</html>\n", BLOGC_TEMPLATE_CONTENT_STMT);
    assert_null(tmp->next->next->next->next->next->next);
    blogc_template_free_stmts(stmts);
}


static void
test_template_parse_ifdef_and_var_outside_block(void **state)
{
    const char *a =
        "{% ifdef GUDA %}bola{% endif %}\n"
        "{{ BOLA }}\n"
        "{% ifndef CHUNDA %}{{ CHUNDA }}{% endif %}\n";
    blogc_error_t *err = NULL;
    b_slist_t *stmts = blogc_template_parse(a, strlen(a), &err);
    assert_null(err);
    assert_non_null(stmts);
    blogc_assert_template_stmt(stmts, "GUDA", BLOGC_TEMPLATE_IFDEF_STMT);
    blogc_assert_template_stmt(stmts->next, "bola",
        BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(stmts->next->next, NULL,
        BLOGC_TEMPLATE_ENDIF_STMT);
    blogc_assert_template_stmt(stmts->next->next->next, "\n",
        BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(stmts->next->next->next->next, "BOLA",
        BLOGC_TEMPLATE_VARIABLE_STMT);
    blogc_assert_template_stmt(stmts->next->next->next->next->next, "\n",
        BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(stmts->next->next->next->next->next->next,
        "CHUNDA", BLOGC_TEMPLATE_IFNDEF_STMT);
    b_slist_t *tmp = stmts->next->next->next->next->next->next->next;
    blogc_assert_template_stmt(tmp, "CHUNDA", BLOGC_TEMPLATE_VARIABLE_STMT);
    blogc_assert_template_stmt(tmp->next, NULL, BLOGC_TEMPLATE_ENDIF_STMT);
    blogc_assert_template_stmt(tmp->next->next, "\n",
        BLOGC_TEMPLATE_CONTENT_STMT);
    assert_null(tmp->next->next->next);
    blogc_template_free_stmts(stmts);
}


static void
test_template_parse_invalid_block_start(void **state)
{
    const char *a = "{% ASD %}\n";
    blogc_error_t *err = NULL;
    b_slist_t *stmts = blogc_template_parse(a, strlen(a), &err);
    assert_non_null(err);
    assert_null(stmts);
    assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER);
    assert_string_equal(err->msg,
        "Invalid statement syntax. Must begin with lowercase letter.\n"
        "Error occurred near line 1, position 4: {% ASD %}");
    blogc_error_free(err);
}


static void
test_template_parse_invalid_block_nested(void **state)
{
    const char *a =
        "{% block entry %}\n"
        "{% block listing %}\n";
    blogc_error_t *err = NULL;
    b_slist_t *stmts = blogc_template_parse(a, strlen(a), &err);
    assert_non_null(err);
    assert_null(stmts);
    assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER);
    assert_string_equal(err->msg,
        "Blocks can't be nested.\n"
        "Error occurred near line 2, position 9: {% block listing %}");
    blogc_error_free(err);
}


static void
test_template_parse_invalid_block_not_open(void **state)
{
    const char *a = "{% endblock %}\n";
    blogc_error_t *err = NULL;
    b_slist_t *stmts = blogc_template_parse(a, strlen(a), &err);
    assert_non_null(err);
    assert_null(stmts);
    assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER);
    assert_string_equal(err->msg,
        "'endblock' statement without an open 'block' statement.\n"
        "Error occurred near line 1, position 12: {% endblock %}");
    blogc_error_free(err);
}


static void
test_template_parse_invalid_endif_not_open(void **state)
{
    const char *a = "{% block listing %}{% endif %}{% endblock %}\n";
    blogc_error_t *err = NULL;
    b_slist_t *stmts = blogc_template_parse(a, strlen(a), &err);
    assert_non_null(err);
    assert_null(stmts);
    assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER);
    assert_string_equal(err->msg,
        "'endif' statement without an open 'ifdef' or 'ifndef' statement.\n"
        "Error occurred near line 1, position 28: "
        "{% block listing %}{% endif %}{% endblock %}");
    blogc_error_free(err);
}


static void
test_template_parse_invalid_block_name(void **state)
{
    const char *a = "{% chunda %}\n";
    blogc_error_t *err = NULL;
    b_slist_t *stmts = blogc_template_parse(a, strlen(a), &err);
    assert_non_null(err);
    assert_null(stmts);
    assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER);
    assert_string_equal(err->msg,
        "Invalid statement type: Allowed types are: 'block', 'endblock', 'ifdef', "
        "'ifndef' and 'endif'.\n"
        "Error occurred near line 1, position 10: {% chunda %}");
    blogc_error_free(err);
}


static void
test_template_parse_invalid_block_type_start(void **state)
{
    const char *a = "{% block ENTRY %}\n";
    blogc_error_t *err = NULL;
    b_slist_t *stmts = blogc_template_parse(a, strlen(a), &err);
    assert_non_null(err);
    assert_null(stmts);
    assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER);
    assert_string_equal(err->msg,
        "Invalid block syntax. Must begin with lowercase letter.\n"
        "Error occurred near line 1, position 10: {% block ENTRY %}");
    blogc_error_free(err);
}


static void
test_template_parse_invalid_block_type(void **state)
{
    const char *a = "{% block chunda %}\n";
    blogc_error_t *err = NULL;
    b_slist_t *stmts = blogc_template_parse(a, strlen(a), &err);
    assert_non_null(err);
    assert_null(stmts);
    assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER);
    assert_string_equal(err->msg,
        "Invalid block type. Allowed types are: 'entry', 'listing' and 'listing_once'.\n"
        "Error occurred near line 1, position 16: {% block chunda %}");
    blogc_error_free(err);
}


static void
test_template_parse_invalid_ifdef_start(void **state)
{
    const char *a = "{% block entry %}{% ifdef guda %}\n";
    blogc_error_t *err = NULL;
    b_slist_t *stmts = blogc_template_parse(a, strlen(a), &err);
    assert_non_null(err);
    assert_null(stmts);
    assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER);
    assert_string_equal(err->msg,
        "Invalid variable name. Must begin with uppercase letter.\n"
        "Error occurred near line 1, position 27: "
        "{% block entry %}{% ifdef guda %}");
    blogc_error_free(err);
}


static void
test_template_parse_invalid_ifdef_variable(void **state)
{
    const char *a = "{% block entry %}{% ifdef BoLA %}\n";
    blogc_error_t *err = NULL;
    b_slist_t *stmts = blogc_template_parse(a, strlen(a), &err);
    assert_non_null(err);
    assert_null(stmts);
    assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER);
    assert_string_equal(err->msg,
        "Invalid variable name. Must be uppercase letter, number or '_'.\n"
        "Error occurred near line 1, position 28: "
        "{% block entry %}{% ifdef BoLA %}");
    blogc_error_free(err);
}


static void
test_template_parse_invalid_if_operator(void **state)
{
    const char *a = "{% block entry %}{% if BOLA = \"asd\" %}\n";
    blogc_error_t *err = NULL;
    b_slist_t *stmts = blogc_template_parse(a, strlen(a), &err);
    assert_non_null(err);
    assert_null(stmts);
    assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER);
    assert_string_equal(err->msg,
        "Invalid 'if' operator. Must be '<', '>', '<=', '>=', '==' or '!='.\n"
        "Error occurred near line 1, position 29: "
        "{% block entry %}{% if BOLA = \"asd\" %}");
    blogc_error_free(err);
}


static void
test_template_parse_invalid_if_operand(void **state)
{
    const char *a = "{% block entry %}{% if BOLA == asd %}\n";
    blogc_error_t *err = NULL;
    b_slist_t *stmts = blogc_template_parse(a, strlen(a), &err);
    assert_non_null(err);
    assert_null(stmts);
    assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER);
    assert_string_equal(err->msg,
        "Invalid 'if' operand. Must be double-quoted static string.\n"
        "Error occurred near line 1, position 32: "
        "{% block entry %}{% if BOLA == asd %}");
    blogc_error_free(err);
}


static void
test_template_parse_invalid_if_operand2(void **state)
{
    const char *a = "{% block entry %}{% if BOLA == \"asd %}\n";
    blogc_error_t *err = NULL;
    b_slist_t *stmts = blogc_template_parse(a, strlen(a), &err);
    assert_non_null(err);
    assert_null(stmts);
    assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER);
    assert_string_equal(err->msg,
        "Found an open double-quoted string.\n"
        "Error occurred near line 1, position 32: "
        "{% block entry %}{% if BOLA == \"asd %}");
    blogc_error_free(err);
}


static void
test_template_parse_invalid_block_end(void **state)
{
    const char *a = "{% block entry }}\n";
    blogc_error_t *err = NULL;
    b_slist_t *stmts = blogc_template_parse(a, strlen(a), &err);
    assert_non_null(err);
    assert_null(stmts);
    assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER);
    assert_string_equal(err->msg,
        "Invalid statement syntax. Must end with '%}'.\n"
        "Error occurred near line 1, position 16: {% block entry }}");
    blogc_error_free(err);
}


static void
test_template_parse_invalid_variable_name(void **state)
{
    const char *a = "{% block entry %}{{ bola }}{% endblock %}\n";
    blogc_error_t *err = NULL;
    b_slist_t *stmts = blogc_template_parse(a, strlen(a), &err);
    assert_non_null(err);
    assert_null(stmts);
    assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER);
    assert_string_equal(err->msg,
        "Invalid variable name. Must begin with uppercase letter.\n"
        "Error occurred near line 1, position 21: "
        "{% block entry %}{{ bola }}{% endblock %}");
    blogc_error_free(err);
}


static void
test_template_parse_invalid_variable_name2(void **state)
{
    const char *a = "{% block entry %}{{ Bola }}{% endblock %}\n";
    blogc_error_t *err = NULL;
    b_slist_t *stmts = blogc_template_parse(a, strlen(a), &err);
    assert_non_null(err);
    assert_null(stmts);
    assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER);
    assert_string_equal(err->msg,
        "Invalid variable name. Must be uppercase letter, number or '_'.\n"
        "Error occurred near line 1, position 22: "
        "{% block entry %}{{ Bola }}{% endblock %}");
    blogc_error_free(err);
}


static void
test_template_parse_invalid_variable_end(void **state)
{
    const char *a = "{% block entry %}{{ BOLA %}{% endblock %}\n";
    blogc_error_t *err = NULL;
    b_slist_t *stmts = blogc_template_parse(a, strlen(a), &err);
    assert_non_null(err);
    assert_null(stmts);
    assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER);
    assert_string_equal(err->msg,
        "Invalid statement syntax. Must end with '}}'.\n"
        "Error occurred near line 1, position 26: "
        "{% block entry %}{{ BOLA %}{% endblock %}");
    blogc_error_free(err);
}


static void
test_template_parse_invalid_close(void **state)
{
    const char *a = "{% block entry %%\n";
    blogc_error_t *err = NULL;
    b_slist_t *stmts = blogc_template_parse(a, strlen(a), &err);
    assert_non_null(err);
    assert_null(stmts);
    assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER);
    assert_string_equal(err->msg,
        "Invalid statement syntax. Must end with '}'.\n"
        "Error occurred near line 1, position 17: {% block entry %%");
    blogc_error_free(err);
}


static void
test_template_parse_invalid_close2(void **state)
{
    const char *a = "{% block entry %}{{ BOLA }%{% endblock %}\n";
    blogc_error_t *err = NULL;
    b_slist_t *stmts = blogc_template_parse(a, strlen(a), &err);
    assert_non_null(err);
    assert_null(stmts);
    assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER);
    assert_string_equal(err->msg,
        "Invalid statement syntax. Must end with '}'.\n"
        "Error occurred near line 1, position 27: "
        "{% block entry %}{{ BOLA }%{% endblock %}");
    blogc_error_free(err);
}


static void
test_template_parse_invalid_if_not_closed(void **state)
{
    const char *a = "{% block entry %}{% ifdef BOLA %}{% endblock %}\n";
    blogc_error_t *err = NULL;
    b_slist_t *stmts = blogc_template_parse(a, strlen(a), &err);
    assert_non_null(err);
    assert_null(stmts);
    assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER);
    assert_string_equal(err->msg, "1 open 'ifdef' and/or 'ifndef' statements "
        "were not closed!");
    blogc_error_free(err);
}


static void
test_template_parse_invalid_block_not_closed(void **state)
{
    const char *a = "{% block entry %}\n";
    blogc_error_t *err = NULL;
    b_slist_t *stmts = blogc_template_parse(a, strlen(a), &err);
    assert_non_null(err);
    assert_null(stmts);
    assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER);
    assert_string_equal(err->msg, "An open block was not closed!");
    blogc_error_free(err);
}


int
main(void)
{
    const UnitTest tests[] = {
        unit_test(test_template_parse),
        unit_test(test_template_parse_crlf),
        unit_test(test_template_parse_html),
        unit_test(test_template_parse_ifdef_and_var_outside_block),
        unit_test(test_template_parse_invalid_block_start),
        unit_test(test_template_parse_invalid_block_nested),
        unit_test(test_template_parse_invalid_block_not_open),
        unit_test(test_template_parse_invalid_endif_not_open),
        unit_test(test_template_parse_invalid_block_name),
        unit_test(test_template_parse_invalid_block_type_start),
        unit_test(test_template_parse_invalid_block_type),
        unit_test(test_template_parse_invalid_ifdef_start),
        unit_test(test_template_parse_invalid_ifdef_variable),
        unit_test(test_template_parse_invalid_if_operator),
        unit_test(test_template_parse_invalid_if_operand),
        unit_test(test_template_parse_invalid_if_operand2),
        unit_test(test_template_parse_invalid_block_end),
        unit_test(test_template_parse_invalid_variable_name),
        unit_test(test_template_parse_invalid_variable_name2),
        unit_test(test_template_parse_invalid_variable_end),
        unit_test(test_template_parse_invalid_close),
        unit_test(test_template_parse_invalid_close2),
        unit_test(test_template_parse_invalid_if_not_closed),
        unit_test(test_template_parse_invalid_block_not_closed),
    };
    return run_tests(tests);
}