feat(ltm): initial full template compiler/parser
This commit is contained in:
parent
af5e519663
commit
ba320a4250
4 changed files with 237 additions and 145 deletions
|
|
@ -1,3 +1,4 @@
|
|||
#include "ltm/common.h"
|
||||
#include "test.h"
|
||||
|
||||
#include "ltm/template.h"
|
||||
|
|
@ -12,8 +13,8 @@ void test_single_placeholder() {
|
|||
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.s == s);
|
||||
TEST_CHECK(template->blocks.arr[0].len == 7);
|
||||
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);
|
||||
|
||||
|
|
@ -33,14 +34,14 @@ void test_single_placeholder_trailing() {
|
|||
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.s == s);
|
||||
TEST_CHECK(template->blocks.arr[0].len == 7);
|
||||
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.s == s + 18);
|
||||
TEST_CHECK(template->blocks.arr[2].len == 1);
|
||||
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);
|
||||
|
||||
|
|
@ -49,8 +50,63 @@ void test_single_placeholder_trailing() {
|
|||
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 }
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue