summaryrefslogtreecommitdiffhomepage
path: root/tests/custom/00_syntax
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2024-03-13 21:05:04 +0100
committerJo-Philipp Wich <jo@mein.io>2024-03-13 23:08:37 +0100
commitbe767ae197babd656d4f5d9c2d5013e39ddbe656 (patch)
tree37b790dae8f45a02da5b5cbe7266875e9bc1f540 /tests/custom/00_syntax
parentba3855ae3775197f3594fc2615cac539075bd2fb (diff)
vm: rework `in` operator semantics
- Ensure that testing for array membership does strict equality tests - Ensure that `(NaN in [ NaN ]) == true` - Do not perform implicit value conversion when testing for object keys, to avoid nonsensical results such as `([] in { "[ ]": true }) == true` - Add test cases for the `in` operator Fixes: #193 Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'tests/custom/00_syntax')
-rw-r--r--tests/custom/00_syntax/28_in_operator129
1 files changed, 129 insertions, 0 deletions
diff --git a/tests/custom/00_syntax/28_in_operator b/tests/custom/00_syntax/28_in_operator
new file mode 100644
index 0000000..7d58a43
--- /dev/null
+++ b/tests/custom/00_syntax/28_in_operator
@@ -0,0 +1,129 @@
+The "in" operator allows testing whether a given value is an item of
+a specified array or whether a given key is present in a specified
+dictionary.
+
+
+1. The `in` operator returns true if the given element is an item of
+the specified array. Strict equality tests are performed.
+
+-- Expect stdout --
+[
+ true,
+ false,
+ true,
+ false,
+ true,
+ false,
+ true,
+ false
+]
+-- End --
+
+-- Testcase --
+{%
+ let o = {};
+ let a = [ o, {}, "", null, false ];
+
+ printf("%.J\n", [
+ o in a,
+ {} in a,
+ "" in a,
+ "test" in a,
+ null in a,
+ [] in a,
+ false in a,
+ true in a
+ ]);
+%}
+-- End --
+
+2. Strict equality when testing array membership should rule out implict
+type coercion.
+
+-- Expect stdout --
+[
+ true,
+ false,
+ false,
+ false,
+ true,
+ false,
+ false
+]
+-- End --
+
+-- Testcase --
+{%
+ let a = [ "", true ];
+
+ printf("%.J\n", [
+ "" in a,
+ 0 in a,
+ false in a,
+ null in a,
+ true in a,
+ 1 in a,
+ 1.0 in a
+ ]);
+%}
+-- End --
+
+3. While there is the rule that `(NaN === NaN) == false`, testing for NaN
+in a given array containing NaN should yield `true`.
+
+-- Expect stdout --
+[
+ true
+]
+-- End --
+
+-- Testcase --
+{%
+ let a = [ NaN ];
+
+ printf("%.J\n", [
+ NaN in a
+ ]);
+%}
+-- End --
+
+4. When the `in` operator is applied to an object, it tests whether the given
+string value is a key of the specified object.
+
+-- Expect stdout --
+[
+ true,
+ true,
+ true,
+ false,
+ false,
+ false,
+ false,
+ false
+]
+-- End --
+
+-- Testcase --
+{%
+ let o = {
+ "1": true,
+ "test": false,
+ "empty": null,
+ "false": 0,
+ "true": 1,
+ "[ ]": "array",
+ "{ }": "object"
+ };
+
+ printf("%.J\n", [
+ "1" in o,
+ "test" in o,
+ "empty" in o,
+ 1 in o, // not implicitly converted to "1"
+ false in o, // not implicitly converted to "false"
+ true in o, // not implicitly converted to "true"
+ [] in o, // not implicitly converted to "[ ]"
+ {} in o // not implicitly converted to "{ }"
+ ]);
+%}
+-- End --