-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #194 from jow-/in-operator-semantics
vm: rework `in` operator semantics
- Loading branch information
Showing
2 changed files
with
163 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 -- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters