summaryrefslogtreecommitdiffhomepage
path: root/tests/00_syntax
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2020-09-07 19:28:27 +0200
committerJo-Philipp Wich <jo@mein.io>2020-09-07 19:28:27 +0200
commit42e8fcddf0ec78d81d6733c8a14592df9dcb2381 (patch)
tree62e8e68e408de7a2df99033ae496117b4481a79d /tests/00_syntax
parentd7a4f8d3b7d0ebab6a1b6bef8f342e100ea32f1d (diff)
tests: add object/array literal and arithmetic test cases
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'tests/00_syntax')
-rw-r--r--tests/00_syntax/13_object_literals51
-rw-r--r--tests/00_syntax/14_array_literals25
2 files changed, 76 insertions, 0 deletions
diff --git a/tests/00_syntax/13_object_literals b/tests/00_syntax/13_object_literals
new file mode 100644
index 0000000..4b4f934
--- /dev/null
+++ b/tests/00_syntax/13_object_literals
@@ -0,0 +1,51 @@
+The utpl script language supports declaring objects (dictionaries) using
+either JSON or JavaScript notation.
+
+-- Expect stdout --
+{ }
+{ "name": "Bob", "age": 31, "email": { "work": "bob@example.com", "private": "bob@example.org" } }
+{ "banana": "yellow", "tomato": "red", "broccoli": "green" }
+{ "foo": "bar", "complex key": "qrx" }
+{ "foo": { "bar": true } }
+-- End --
+
+-- Testcase --
+{%
+ // An empty object can be declared using a pair of curly brackets
+ empty_obj = { };
+
+ // It is also possible to use JSON notation to declare an object
+ json_obj = {
+ "name": "Bob",
+ "age": 31,
+ "email": {
+ "work": "bob@example.com",
+ "private": "bob@example.org"
+ }
+ };
+
+ // Declaring an object in JavaScript notation is supported as well
+ another_obj = {
+ banana: "yellow",
+ tomato: "red",
+ broccoli: "green"
+ };
+
+ // Mixing styles is allowed too
+ third_obj = {
+ foo: "bar",
+ "complex key": "qrx"
+ };
+
+ // Important caveat: when nesting objects, ensure that curly brackets
+ // are separated by space or newline to avoid interpretation as
+ // expression block tag!
+ nested_obj = { foo: { bar: true } }; // <-- mind the space in "} }"
+
+ // Printing (or stringifying) objects will return their JSON representation
+ print(empty_obj, "\n");
+ print(json_obj, "\n");
+ print(another_obj, "\n");
+ print(third_obj, "\n");
+ print(nested_obj, "\n");
+-- End --
diff --git a/tests/00_syntax/14_array_literals b/tests/00_syntax/14_array_literals
new file mode 100644
index 0000000..ea5f9c0
--- /dev/null
+++ b/tests/00_syntax/14_array_literals
@@ -0,0 +1,25 @@
+The utpl script language supports declaring arrays using JSON notation.
+
+-- Expect stdout --
+[ ]
+[ "first", "second", 123, [ "a", "nested", "array" ], { "a": "nested object" } ]
+-- End --
+
+-- Testcase --
+{%
+ // An empty array can be declared using a pair of square brackets
+ empty_array = [ ];
+
+ // JSON notation is used to declare an array with contents
+ json_array = [
+ "first",
+ "second",
+ 123,
+ [ "a", "nested", "array" ],
+ { a: "nested object" }
+ ];
+
+ // Printing (or stringifying) arrays will return their JSON representation
+ print(empty_array, "\n");
+ print(json_array, "\n");
+-- End --