diff options
author | Danny van Kooten <dannyvankooten@users.noreply.github.com> | 2020-03-15 20:49:20 +0100 |
---|---|---|
committer | Danny van Kooten <dannyvankooten@users.noreply.github.com> | 2020-03-15 20:49:20 +0100 |
commit | 8fcc38ce404f16b2809a73de7bdf778bb84becb4 (patch) | |
tree | eb3da105ae8dfb4df697e972b95d0b10abd134e9 /tests/test_template.c | |
parent | 74a547aa407ff8555f40dc38b18dee0bdb708836 (diff) | |
download | unja-8fcc38ce404f16b2809a73de7bdf778bb84becb4.tar.gz unja-8fcc38ce404f16b2809a73de7bdf778bb84becb4.zip |
add basic infix expressions
Diffstat (limited to 'tests/test_template.c')
-rw-r--r-- | tests/test_template.c | 75 |
1 files changed, 67 insertions, 8 deletions
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); } |