diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test.h | 1 | ||||
-rw-r--r-- | tests/test_template.c | 75 |
2 files changed, 68 insertions, 8 deletions
diff --git a/tests/test.h b/tests/test.h index 5c76448..f860ee2 100644 --- a/tests/test.h +++ b/tests/test.h @@ -8,6 +8,7 @@ #define TEST(name) strcpy(current_test, #name); #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] /* used to store the running test name */ char current_test[256] = {'\0'}; diff --git a/tests/test_template.c b/tests/test_template.c index 5c17dd4..61a027f 100644 --- a/tests/test_template.c +++ b/tests/test_template.c @@ -34,22 +34,81 @@ TEST(expr_symbol) { free(output); } -TEST(var_whitespace) { - char *input = "Hello \n{{-name -}}\n."; +TEST(expr_add) { + struct { + char *input; + char *expected_output; + } tests[] = { + {"{{ 5 + 5 }}.", "10."}, + {"{{ 5 + foo }}.", "15."}, + {"{{ \"foo\" + \"bar\" }}", "foobar"}, + {"{{ \"Hello \" + name }}", "Hello Danny"}, + }; + struct hashmap *ctx = hashmap_new(); - hashmap_insert(ctx, "name", "world"); - char *output = template(input, ctx); - assert_str(output, "Helloworld."); + hashmap_insert(ctx, "foo", "10"); + hashmap_insert(ctx, "name", "Danny"); + + for (int i=0; i < ARRAY_SIZE(tests); i++) { + char *output = template(tests[i].input, ctx); + assert_str(output, tests[i].expected_output); + free(output); + } + hashmap_free(ctx); +} + +TEST(expr_subtract) { + char *input = "Hello {{ 5 - 5 }}."; + char *output = template(input, NULL); + assert_str(output, "Hello 0."); + free(output); +} + +TEST(expr_divide) { + char *input = "Hello {{ 5 / 5 }}."; + char *output = template(input, NULL); + assert_str(output, "Hello 1."); + free(output); +} + +TEST(expr_multiply) { + char *input = "Hello {{ 5 * 5 }}."; + char *output = template(input, NULL); + assert_str(output, "Hello 25."); + free(output); +} + +TEST(expr_gt) { + char *input = "Hello {{ 5 > 4 }}."; + char *output = template(input, NULL); + assert_str(output, "Hello 1."); + free(output); + + input = "Hello {{ 5 > 6 }}."; + output = template(input, NULL); + assert_str(output, "Hello 0."); free(output); } -TEST(multiline) { - char *input = "Hello {{name}}.\nL2"; +TEST(expr_lt) { + char *input = "Hello {{ 5 < 4 }}."; + char *output = template(input, NULL); + assert_str(output, "Hello 0."); + free(output); + + input = "Hello {{ 4 < 5 }}."; + output = template(input, NULL); + assert_str(output, "Hello 1."); + free(output); +} + +TEST(expr_whitespace) { + char *input = "Hello \n{{-name -}}\n."; struct hashmap *ctx = hashmap_new(); hashmap_insert(ctx, "name", "world"); char *output = template(input, ctx); - assert_str(output, "Hello world.\nL2"); + assert_str(output, "Helloworld."); hashmap_free(ctx); free(output); } |