summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--tests/00_syntax/13_object_literals51
-rw-r--r--tests/00_syntax/14_array_literals25
-rw-r--r--tests/01_arithmetic/00_value_conversion125
-rw-r--r--tests/01_arithmetic/01_division53
-rw-r--r--tests/01_arithmetic/02_modulo32
5 files changed, 286 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 --
diff --git a/tests/01_arithmetic/00_value_conversion b/tests/01_arithmetic/00_value_conversion
new file mode 100644
index 0000000..c44ad00
--- /dev/null
+++ b/tests/01_arithmetic/00_value_conversion
@@ -0,0 +1,125 @@
+Performing unary plus or minus, or performing arithmetic operations may
+implicitly convert non-numeric values to numeric ones.
+
+If an addition is performed and either operand is a string, the other
+operand is converted into a string as well and the addition result is a
+concatenated string consisting of the two operand values.
+
+-- Expect stdout --
+HelloWorld
+12
+34
+true
+false
+null
+{ "some": "object" }
+[ "some", "array" ]
+function() { ... }
+1.2
+Infinity
+124
+123
+123
+NaN
+NaN
+NaN
+124.2
+Infinity
+1
+0
+0
+NaN
+NaN
+NaN
+1.2
+Infinity
+123
+12.3
+NaN
+-1
+0
+0
+NaN
+NaN
+NaN
+-1.2
+-Infinity
+-123
+-12.3
+NaN
+4.2
+9.6
+-- End --
+
+-- Testcase --
+{%
+ print(join("\n", [
+
+ // Adding two strings concatenates them:
+ "Hello" + "World",
+
+ // Adding a number to a string results in a string:
+ "1" + 2,
+
+ // Adding a string to a number results in a string:
+ 3 + "4",
+
+ // Adding any non-string value to a string or vice versa will
+ // force stringification of the non-string value
+ "" + true,
+ "" + false,
+ "" + null,
+ "" + { some: "object" },
+ "" + [ "some", "array" ],
+ "" + function() {},
+ "" + 1.2,
+ "" + (1 / 0),
+
+ // Adding a numeric value to a non-string, non-numeric value
+ // or vice versa will convert the non-numeric argument to a
+ // number
+ 123 + true,
+ 123 + false,
+ 123 + null,
+ 123 + { some: "object" },
+ 123 + [ "some", "array" ],
+ 123 + function() {},
+ 123 + 1.2,
+ 123 + (1 / 0),
+
+ // The unary "+" operator follows the same logic as adding a
+ // non-numeric, non-string value to a numeric one. Additionally
+ // the unary plus forces conversion of string values into numbers
+ +true,
+ +false,
+ +null,
+ +{ some: "object" },
+ +[ "some", "array" ],
+ +function() {},
+ +1.2,
+ +(1 / 0),
+ +"123",
+ +"12.3",
+ +"this is not a number",
+
+ // The unary "-" operator functions like the unary "+" one and
+ // it additionally returns the negation of the numeric value
+ -true,
+ -false,
+ -null,
+ -{ some: "object" },
+ -[ "some", "array" ],
+ -function() {},
+ -1.2,
+ -(1 / 0),
+ -"123",
+ -"12.3",
+ -"this is not a number",
+
+ // Adding a double to an integer or vice versa will force the
+ // result to a double as well
+ 1.2 + 3,
+ 4 + 5.6,
+
+ "" ]));
+-- End --
diff --git a/tests/01_arithmetic/01_division b/tests/01_arithmetic/01_division
new file mode 100644
index 0000000..d4a2adb
--- /dev/null
+++ b/tests/01_arithmetic/01_division
@@ -0,0 +1,53 @@
+While arithmetic divisions generally follow the value conversion rules
+outlined in the "00_value_conversion" test case, a number of additional
+constraints apply.
+
+-- Expect stdout --
+Division by zero yields Infinity:
+1 / 0 = Infinity
+
+Division by Infinity yields zero:
+1 / Infinity = 0
+
+Dividing Infinity yields Infinity:
+Infinity / 1 = Infinity
+
+Dividing Infinity by Infinity yields NaN:
+Infinity / Infinity = NaN
+
+If either operand is NaN, the result is NaN:
+1 / NaN = NaN
+NaN / 1 = NaN
+
+If both operands are integers, integer division is performed:
+10 / 3 = 3
+
+If either operand is a double, double division is performed:
+10.0 / 3 = 3.33333
+10 / 3.0 = 3.33333
+-- End --
+
+-- Testcase --
+Division by zero yields Infinity:
+1 / 0 = {{ 1 / 0 }}
+
+Division by Infinity yields zero:
+1 / Infinity = {{ 1 / Infinity }}
+
+Dividing Infinity yields Infinity:
+Infinity / 1 = {{ Infinity / 1 }}
+
+Dividing Infinity by Infinity yields NaN:
+Infinity / Infinity = {{ Infinity / Infinity }}
+
+If either operand is NaN, the result is NaN:
+1 / NaN = {{ 1 / NaN }}
+NaN / 1 = {{ NaN / 1 }}
+
+If both operands are integers, integer division is performed:
+10 / 3 = {{ 10 / 3 }}
+
+If either operand is a double, double division is performed:
+10.0 / 3 = {{ 10.0 / 3 }}
+10 / 3.0 = {{ 10 / 3.0 }}
+-- End --
diff --git a/tests/01_arithmetic/02_modulo b/tests/01_arithmetic/02_modulo
new file mode 100644
index 0000000..244d624
--- /dev/null
+++ b/tests/01_arithmetic/02_modulo
@@ -0,0 +1,32 @@
+The utpl language supports modulo divisions, however they're only defined
+for integer values.
+
+-- Expect stdout --
+If both operands are integers or convertible to integers,
+the modulo division yields the remainder:
+10 % 4 = 2
+"10" % 4 = 2
+10 % "4" = 2
+"10" % "4" = 2
+
+If either operand is a double value, the modulo operation
+yields NaN:
+10.0 % 4 = NaN
+10 % 4.0 = NaN
+"10.0" % 4 = NaN
+-- End --
+
+-- Testcase --
+If both operands are integers or convertible to integers,
+the modulo division yields the remainder:
+10 % 4 = {{ 10 % 4 }}
+"10" % 4 = {{ "10" % 4 }}
+10 % "4" = {{ 10 % 4 }}
+"10" % "4" = {{ "10" % "4" }}
+
+If either operand is a double value, the modulo operation
+yields NaN:
+10.0 % 4 = {{ 10.0 % 4 }}
+10 % 4.0 = {{ 10 % 4.0 }}
+"10.0" % 4 = {{ "10.0" % 4 }}
+-- End --