chore: copy over project files
This commit is contained in:
commit
7c63b3db1d
15 changed files with 3138 additions and 0 deletions
44
test/instance.c
Normal file
44
test/instance.c
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
#include "test.h"
|
||||
|
||||
#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_ASSERT(ltm_template_compile(&template, s) == ltm_err_ok);
|
||||
|
||||
ltm_instance *instance;
|
||||
TEST_CHECK(ltm_template_instantiate(&instance, template) == ltm_err_ok);
|
||||
|
||||
TEST_CHECK(ltm_instance_block_add_var(instance, "world", ltm_instance_block_type_buf, "World", 5) == ltm_err_ok);
|
||||
|
||||
TEST_CHECK(ltm_instance_size(instance) == 13);
|
||||
|
||||
char buf[13];
|
||||
|
||||
size_t written = 0;
|
||||
TEST_CHECK(ltm_instance_write(&written, buf, 5, instance) == ltm_err_ok);
|
||||
TEST_CHECK(written == 5);
|
||||
|
||||
written = 0;
|
||||
TEST_CHECK(ltm_instance_write(&written, buf + 5, 5, instance) == ltm_err_ok);
|
||||
TEST_CHECK(written == 5);
|
||||
|
||||
written = 0;
|
||||
TEST_CHECK(ltm_instance_write(&written, buf + 10, 5, instance) == ltm_err_done);
|
||||
TEST_CHECK(written == 3);
|
||||
|
||||
TEST_CHECK(strncmp(buf, "Hello, World!", 13) == 0);
|
||||
TEST_DUMP("buf", buf, 13);
|
||||
|
||||
ltm_instance_free(instance);
|
||||
ltm_template_free(template);
|
||||
}
|
||||
|
||||
TEST_LIST = {
|
||||
{ "instance single placeholder", test_single_placeholder },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
118
test/template_compile.c
Normal file
118
test/template_compile.c
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
#include "test.h"
|
||||
|
||||
#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);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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 },
|
||||
{ "template single nested", test_single_nested },
|
||||
{ "template unclosed placeholder", test_unclosed_placeholder },
|
||||
{ "template unclosed nested", test_unclosed_nested },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
1839
test/test.h
Normal file
1839
test/test.h
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue