lander/ltm/test/template_compile.c

119 lines
4.0 KiB
C
Raw Normal View History

#include "test.h"
2023-12-16 20:42:07 +01:00
#include "ltm/common.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);
ltm_template_free(template);
}
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);
ltm_template_free(template);
}
2023-12-16 16:29:08 +01:00
void test_single_nested() {
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);
2023-12-16 16:29:08 +01:00
TEST_CHECK(template->blocks.arr[1].type == ltm_template_block_type_nested);
ltm_template *nested_template = template->blocks.arr[1].data.ptr;
TEST_CHECK(nested_template->blocks.len == 3);
TEST_CHECK(nested_template->blocks.arr[0].type == ltm_template_block_type_literal);
TEST_CHECK(nested_template->blocks.arr[0].data.ptr == s + 17);
TEST_CHECK(nested_template->blocks.arr[0].data.len == 13);
TEST_CHECK(nested_template->blocks.arr[1].type == ltm_template_block_type_var);
TEST_CHECK(nested_template->blocks.arr[2].type == ltm_template_block_type_literal);
TEST_CHECK(nested_template->blocks.arr[2].data.ptr == s + 39);
TEST_CHECK(nested_template->blocks.arr[2].data.len == 1);
TEST_CHECK(nested_template->names.len == 1);
TEST_CHECK(nested_template->names.arr[0].name.s == s + 33);
TEST_CHECK(nested_template->names.arr[0].name.len == 3);
TEST_CHECK(nested_template->names.arr[0].index == 1);
ltm_template_free(template);
}
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);
}
2023-12-16 16:29:08 +01:00
void test_unclosed_nested() {
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 },
2023-12-16 16:29:08 +01:00
{ "template single nested", test_single_nested },
{ "template unclosed placeholder", test_unclosed_placeholder },
2023-12-16 16:29:08 +01:00
{ "template unclosed nested", test_unclosed_nested },
{ NULL, NULL }
};