aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDanny van Kooten <dannyvankooten@users.noreply.github.com>2020-03-20 12:11:20 +0100
committerDanny van Kooten <dannyvankooten@users.noreply.github.com>2020-03-20 12:11:20 +0100
commit26a1adffd4ac7b4f1390ba0135ede850697c4dfa (patch)
treefb785b007b5e9decd9667c32782b2c17dd2f1f57
parent306f057f985260d334a5e54405273f910120523c (diff)
downloadunja-26a1adffd4ac7b4f1390ba0135ede850697c4dfa.tar.gz
unja-26a1adffd4ac7b4f1390ba0135ede850697c4dfa.zip
add string equality operators
-rw-r--r--src/template.c27
-rw-r--r--tests/test_template.c32
2 files changed, 53 insertions, 6 deletions
diff --git a/src/template.c b/src/template.c
index 9e739bc..a1439c4 100644
--- a/src/template.c
+++ b/src/template.c
@@ -100,7 +100,7 @@ mpc_parser_t *parser_init() {
" | <lexp> <spaces> \"!=\" <spaces> <lexp> "
" | <lexp> <spaces> \"==\" <spaces> <lexp> "
" | <lexp> ;"
- " print : /{{2}-? */ <lexp> / *-?}}/ ;"
+ " print : /{{2}-? */ <expression> / *-?}}/ ;"
" comment : \"{#\" /[^#][^#}]*/ \"#}\" ;"
" statement_open: /{\%-? */;"
" statement_close: / *-?\%}/;"
@@ -366,13 +366,28 @@ struct unja_object *eval_expression_value(mpc_ast_t* node, struct context *ctx)
return &null_object;
}
+struct unja_object *eval_string_infix_expression(struct unja_object *left, char *op, struct unja_object *right) {
+ struct unja_object *result;
+
+ if (strcmp(op, "+") == 0) {
+ result = make_string_object(left->string, right->string);
+ } else if (strcmp(op, "==") == 0) {
+ result = make_int_object(strcmp(left->string, right->string) == 0);
+ } else if(strcmp(op, "!=") == 0) {
+ result = make_int_object(strcmp(left->string, right->string) != 0);
+ } else {
+ errx(EXIT_FAILURE, "invalid string operator: %s", op);
+ }
+
+ object_free(left);
+ object_free(right);
+ return result;
+}
+
struct unja_object *eval_infix_expression(struct unja_object *left, char *op, struct unja_object *right) {
/* if operator is + and either left or right node is of type string: concat */
- if (op[0] == '+' && (left->type == OBJ_STRING && right->type == OBJ_STRING)) {
- struct unja_object *result = make_string_object(left->string, right->string);
- object_free(left);
- object_free(right);
- return result;
+ if (left->type == OBJ_STRING && right->type == OBJ_STRING) {
+ return eval_string_infix_expression(left, op, right);
}
int result;
diff --git a/tests/test_template.c b/tests/test_template.c
index 039e993..d94ddd6 100644
--- a/tests/test_template.c
+++ b/tests/test_template.c
@@ -309,6 +309,22 @@ TEST(expr_eq) {
}
}
+TEST(expr_string_eq) {
+ struct {
+ char *input;
+ char *expected_output;
+ } tests[] = {
+ {"{% if \"foo\" == \"bar\" %}1{% endif %}", ""},
+ {"{% if \"foo\" == \"foo\" %}1{% endif %}", "1"},
+ };
+
+ for (int i=0; i < ARRAY_SIZE(tests); i++) {
+ char *output = template_string(tests[i].input, NULL);
+ assert_str(output, tests[i].expected_output);
+ free(output);
+ }
+}
+
TEST(expr_not_eq) {
struct {
char *input;
@@ -325,6 +341,22 @@ TEST(expr_not_eq) {
}
}
+TEST(expr_string_not_eq) {
+ struct {
+ char *input;
+ char *expected_output;
+ } tests[] = {
+ {"{% if \"foo\" != \"bar\" %}1{% endif %}", "1"},
+ {"{% if \"foo\" != \"foo\" %}1{% endif %}", ""},
+ };
+
+ for (int i=0; i < ARRAY_SIZE(tests); i++) {
+ char *output = template_string(tests[i].input, NULL);
+ assert_str(output, tests[i].expected_output);
+ free(output);
+ }
+}
+
TEST(if_block_whitespace) {
char *input = "\n{%- if 10 > 5 -%}\nOK\n{%- endif -%}\n";
char *output = template_string(input, NULL);