113 lines
3.9 KiB
C
113 lines
3.9 KiB
C
#include "ltm/common.h"
|
|
#include "test.h"
|
|
|
|
#include "ltm/template.h"
|
|
#include "ltm/template_internal.h"
|
|
|
|
void test_single_placeholder() {
|
|
const char *s = "Hello, {{ world }}";
|
|
|
|
ltm_template *template;
|
|
TEST_CHECK(ltm_template_compile(&template, s) == ltm_err_ok);
|
|
|
|
TEST_CHECK(template->blocks.len == 2);
|
|
|
|
TEST_CHECK(template->blocks.arr[0].type == ltm_template_block_type_literal);
|
|
TEST_CHECK(template->blocks.arr[0].data.ptr == s);
|
|
TEST_CHECK(template->blocks.arr[0].data.len == 7);
|
|
|
|
TEST_CHECK(template->blocks.arr[1].type == ltm_template_block_type_var);
|
|
|
|
TEST_CHECK(template->names.len == 1);
|
|
|
|
TEST_CHECK(template->names.arr[0].name.s == s + 10);
|
|
TEST_CHECK(template->names.arr[0].name.len == 5);
|
|
TEST_CHECK(template->names.arr[0].index == 1);
|
|
}
|
|
|
|
void test_single_placeholder_trailing() {
|
|
const char *s = "Hello, {{ world }}!";
|
|
|
|
ltm_template *template;
|
|
TEST_CHECK(ltm_template_compile(&template, s) == ltm_err_ok);
|
|
|
|
TEST_CHECK(template->blocks.len == 3);
|
|
|
|
TEST_CHECK(template->blocks.arr[0].type == ltm_template_block_type_literal);
|
|
TEST_CHECK(template->blocks.arr[0].data.ptr == s);
|
|
TEST_CHECK(template->blocks.arr[0].data.len == 7);
|
|
|
|
TEST_CHECK(template->blocks.arr[1].type == ltm_template_block_type_var);
|
|
|
|
TEST_CHECK(template->blocks.arr[2].type == ltm_template_block_type_literal);
|
|
TEST_CHECK(template->blocks.arr[2].data.ptr == s + 18);
|
|
TEST_CHECK(template->blocks.arr[2].data.len == 1);
|
|
|
|
TEST_CHECK(template->names.len == 1);
|
|
|
|
TEST_CHECK(template->names.arr[0].name.s == s + 10);
|
|
TEST_CHECK(template->names.arr[0].name.len == 5);
|
|
TEST_CHECK(template->names.arr[0].index == 1);
|
|
}
|
|
|
|
void test_single_loop() {
|
|
const char *s = "abc {{ l start }}some content {{ var }} {{ l end }}";
|
|
|
|
ltm_template *template;
|
|
TEST_CHECK(ltm_template_compile(&template, s) == ltm_err_ok);
|
|
|
|
TEST_CHECK(template->names.len == 1);
|
|
TEST_CHECK(template->names.arr[0].name.s == s + 7);
|
|
TEST_CHECK(template->names.arr[0].name.len == 1);
|
|
TEST_CHECK(template->names.arr[0].index == 1);
|
|
|
|
TEST_CHECK(template->blocks.len == 2);
|
|
|
|
TEST_CHECK(template->blocks.arr[0].type == ltm_template_block_type_literal);
|
|
TEST_CHECK(template->blocks.arr[0].data.ptr == s);
|
|
TEST_CHECK(template->blocks.arr[0].data.len == 4);
|
|
|
|
TEST_CHECK(template->blocks.arr[1].type == ltm_template_block_type_loop);
|
|
ltm_template *loop_template = template->blocks.arr[1].data.ptr;
|
|
|
|
TEST_CHECK(loop_template->blocks.len == 3);
|
|
TEST_CHECK(loop_template->blocks.arr[0].type == ltm_template_block_type_literal);
|
|
TEST_CHECK(loop_template->blocks.arr[0].data.ptr == s + 17);
|
|
TEST_CHECK(loop_template->blocks.arr[0].data.len == 13);
|
|
TEST_CHECK(loop_template->blocks.arr[1].type == ltm_template_block_type_var);
|
|
TEST_CHECK(loop_template->blocks.arr[2].type == ltm_template_block_type_literal);
|
|
TEST_CHECK(loop_template->blocks.arr[2].data.ptr == s + 39);
|
|
TEST_CHECK(loop_template->blocks.arr[2].data.len == 1);
|
|
|
|
TEST_CHECK(loop_template->names.len == 1);
|
|
TEST_CHECK(loop_template->names.arr[0].name.s == s + 33);
|
|
TEST_CHECK(loop_template->names.arr[0].name.len == 3);
|
|
TEST_CHECK(loop_template->names.arr[0].index == 1);
|
|
}
|
|
|
|
void test_unclosed_placeholder() {
|
|
ltm_template *template;
|
|
|
|
const char *s = "abc {{ var }";
|
|
TEST_CHECK(ltm_template_compile(&template, s) == ltm_err_invalid_template);
|
|
|
|
s = "abc {{ var ";
|
|
TEST_CHECK(ltm_template_compile(&template, s) == ltm_err_invalid_template);
|
|
}
|
|
|
|
void test_unclosed_loop() {
|
|
ltm_template *template;
|
|
|
|
const char *s = "abc {{ var start }} {{ hello }}";
|
|
TEST_CHECK(ltm_template_compile(&template, s) == ltm_err_invalid_template);
|
|
}
|
|
|
|
TEST_LIST = {
|
|
{ "template single placeholder", test_single_placeholder },
|
|
{ "template single placeholder trailing", test_single_placeholder_trailing },
|
|
{ "template single loop", test_single_loop },
|
|
{ "template unclosed placeholder", test_unclosed_placeholder },
|
|
{ "template unclosed loop", test_unclosed_loop },
|
|
{ NULL, NULL }
|
|
};
|