summaryrefslogtreecommitdiffhomepage
path: root/tests/00_syntax
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2020-11-12 00:02:36 +0100
committerJo-Philipp Wich <jo@mein.io>2020-11-12 00:17:42 +0100
commitc447e58ebf3b04f129009f642d5d1be4ca8cee92 (patch)
tree8ed4788d85cfbf7a870fa748549606cb9c96ce65 /tests/00_syntax
parent6893c896561387fa88f610a2f54ba59867021cd8 (diff)
syntax: implement key/value for-in loop iteration
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'tests/00_syntax')
-rw-r--r--tests/00_syntax/16_for_loop131
1 files changed, 131 insertions, 0 deletions
diff --git a/tests/00_syntax/16_for_loop b/tests/00_syntax/16_for_loop
index 4253b5e..2f8ca9f 100644
--- a/tests/00_syntax/16_for_loop
+++ b/tests/00_syntax/16_for_loop
@@ -164,3 +164,134 @@ Iteration {{ i }}
Item {{ n }}
{% endfor %}
-- End --
+
+
+By specifying two loop variables in for-in loop expressions, keys
+and values can be iterated simultaneously.
+
+-- Expect stdout --
+true
+false
+123
+456
+[ 0, true ]
+[ 1, false ]
+[ 2, 123 ]
+[ 3, 456 ]
+foo
+bar
+baz
+qrx
+[ "foo", true ]
+[ "bar", false ]
+[ "baz", 123 ]
+[ "qrx", 456 ]
+-- End --
+
+-- Testcase --
+{%
+ local arr = [ true, false, 123, 456 ];
+ local obj = { foo: true, bar: false, baz: 123, qrx: 456 };
+
+ // iterating arrays with one loop variable yields the array values
+ for (local x in arr)
+ print(x, "\n");
+
+ // iterating arrays with two loop variables yields the array indexes
+ // and their corresponding values
+ for (local x, y in arr)
+ print([x, y], "\n");
+
+ // iterating objects with one loop variable yields the object keys
+ for (local x in obj)
+ print(x, "\n");
+
+ // iterating objects with two loop variables yields the object keys
+ // and their corresponding values
+ for (local x, y in obj)
+ print([x, y], "\n");
+%}
+-- End --
+
+
+Ensure that for-in loop expressions with more than two variables are
+rejected.
+
+-- Expect stderr --
+Syntax error: Invalid for-in expression
+In line 2, byte 16:
+
+ ` for (local x, y, z in {})`
+ Near here --------^
+
+
+-- End --
+
+-- Testcase --
+{%
+ for (local x, y, z in {})
+ ;
+%}
+-- End --
+
+
+Ensure that assignments in for-in loop expressions are rejected.
+
+-- Expect stderr --
+Syntax error: Unexpected token
+Expecting ','
+In line 2, byte 15:
+
+ ` for (local x = 1, y in {})`
+ Near here -------^
+
+
+-- End --
+
+-- Testcase --
+{%
+ for (local x = 1, y in {})
+ ;
+%}
+-- End --
+
+
+Ensure that too short for-in loop expressions are rejected (1/2).
+
+-- Expect stderr --
+Syntax error: Unexpected token
+Expecting ',' or 'in'
+In line 2, byte 14:
+
+ ` for (local x)`
+ Near here ------^
+
+
+-- End --
+
+-- Testcase --
+{%
+ for (local x)
+ ;
+%}
+-- End --
+
+
+Ensure that too short for-in loop expressions are rejected (2/2).
+
+-- Expect stderr --
+Syntax error: Invalid for-in expression
+In line 2, byte 16:
+
+ ` for (local x, y)`
+ Near here --------^
+
+
+-- End --
+
+-- Testcase --
+{%
+ for (local x, y)
+ ;
+%}
+-- End --