diff options
author | Danny van Kooten <dannyvankooten@users.noreply.github.com> | 2020-03-17 15:12:06 +0100 |
---|---|---|
committer | Danny van Kooten <dannyvankooten@users.noreply.github.com> | 2020-03-17 15:12:06 +0100 |
commit | 92be46c849a53968e469e544f84820e2fedf6dac (patch) | |
tree | 962c43a687808a2fd0e92679784f36c8675bc94d /tests | |
parent | 4624d6d369e8e5ce9a887acfe27be90fedf3d1ed (diff) | |
download | unja-92be46c849a53968e469e544f84820e2fedf6dac.tar.gz unja-92be46c849a53968e469e544f84820e2fedf6dac.zip |
allocate precisely for output buffer
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test.h | 2 | ||||
-rw-r--r-- | tests/test_template.c | 34 |
2 files changed, 26 insertions, 10 deletions
diff --git a/tests/test.h b/tests/test.h index dec4ba4..44fd7fb 100644 --- a/tests/test.h +++ b/tests/test.h @@ -7,7 +7,7 @@ #define END_TESTS } #define TEST(name) strcpy(current_test, #name); #define assert_null(actual) _assert(actual == NULL, __FILE__, __LINE__, "invalid value: expected NULL, got %s", actual) -#define assert_str(actual, expected) _assert(actual != NULL && strcmp(actual, expected) == 0, __FILE__, __LINE__, "invalid string: expected %s, got %s", expected, actual) +#define assert_str(actual, expected) _assert(actual != NULL && strcmp(actual, expected) == 0, __FILE__, __LINE__, "invalid string: expected \"%s\", got \"%s\"", expected, actual) #define assert(assertion, format, ...) _assert(assertion, __FILE__, __LINE__, format, ##__VA_ARGS__) #define ARRAY_SIZE(arr) sizeof arr / sizeof arr[0] diff --git a/tests/test_template.c b/tests/test_template.c index 0567995..c13ef4c 100644 --- a/tests/test_template.c +++ b/tests/test_template.c @@ -114,18 +114,18 @@ TEST(expr_whitespace) { } TEST(for_block) { - char *input = "{% for n in numbers %}{{ n }}, {% endfor %}"; + char *input = "{% for n in names %}{{ n }}, {% endfor %}"; struct hashmap *ctx = hashmap_new(); - struct vector *numbers = vector_new(3); - vector_push(numbers, "1"); - vector_push(numbers, "2"); - vector_push(numbers, "3"); - hashmap_insert(ctx, "numbers", numbers); + struct vector *names = vector_new(9); + vector_push(names, "John"); + vector_push(names, "Sally"); + vector_push(names, "Eric"); + hashmap_insert(ctx, "names", names); char *output = template(input, ctx); - assert_str(output, "1, 2, 3, "); - vector_free(numbers); + assert_str(output, "John, Sally, Eric, "); + vector_free(names); hashmap_free(ctx); free(output); } @@ -201,4 +201,20 @@ TEST(if_else_block) { hashmap_free(ctx); } -END_TESTS +TEST(buffer_alloc) { + /* Output a string so that output buffer is longer than template buffer, + to test dynamic allocation */ + char *input = "{{ n }}"; + struct hashmap *ctx = hashmap_new(); + char *text = "Lorem ipsum dolor sit amet."; + hashmap_insert(ctx, "n", text); + + char *output = template(input, ctx); + assert_str(output, text); + hashmap_free(ctx); + free(output); +} + + + +END_TESTS
\ No newline at end of file |