From fbfe506e2e7e2c86763997057d7e10c691c61b32 Mon Sep 17 00:00:00 2001 From: Danny van Kooten Date: Fri, 20 Mar 2020 14:06:18 +0100 Subject: add support for modulo operator --- src/template.c | 1 + tests/test_template.c | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/template.c b/src/template.c index e8fcf36..0d1faa8 100644 --- a/src/template.c +++ b/src/template.c @@ -398,6 +398,7 @@ struct unja_object *eval_infix_expression(struct unja_object *left, char *op, st case '-': result = object_to_int(left) - object_to_int(right); break; case '/': result = object_to_int(left) / object_to_int(right); break; case '*': result = object_to_int(left) * object_to_int(right); break; + case '%': result = object_to_int(left) % object_to_int(right); break; case '>': if (op[1] == '=') { result = object_to_int(left) >= object_to_int(right); diff --git a/tests/test_template.c b/tests/test_template.c index 69ff7fe..758f7fb 100644 --- a/tests/test_template.c +++ b/tests/test_template.c @@ -110,23 +110,30 @@ TEST(expr_op_precedence) { } TEST(expr_subtract) { - char *input = "Hello {{ 5 - 5 }}."; + char *input = "{{ 5 - 5 }}"; char *output = template_string(input, NULL); - assert_str(output, "Hello 0."); + assert_str(output, "0"); free(output); } TEST(expr_divide) { - char *input = "Hello {{ 5 / 5 }}."; + char *input = "{{ 5 / 5 }}"; char *output = template_string(input, NULL); - assert_str(output, "Hello 1."); + assert_str(output, "1"); free(output); } TEST(expr_multiply) { - char *input = "Hello {{ 5 * 5 }}."; + char *input = "{{ 5 * 5 }}"; char *output = template_string(input, NULL); - assert_str(output, "Hello 25."); + assert_str(output, "25"); + free(output); +} + +TEST(expr_modulo) { + char *input = "{{ 5 % 4 }}"; + char *output = template_string(input, NULL); + assert_str(output, "1"); free(output); } -- cgit v1.2.3