From be767ae197babd656d4f5d9c2d5013e39ddbe656 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Wed, 13 Mar 2024 21:05:04 +0100 Subject: 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 --- tests/custom/00_syntax/28_in_operator | 129 ++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 tests/custom/00_syntax/28_in_operator (limited to 'tests/custom') 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 -- -- cgit v1.2.3