• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/tests/custom/00_syntax/28_in_operator

  1 The "in" operator allows testing whether a given value is an item of
  2 a specified array or whether a given key is present in a specified
  3 dictionary.
  4 
  5 
  6 1. The `in` operator returns true if the given element is an item of
  7 the specified array. Strict equality tests are performed.
  8 
  9 -- Expect stdout --
 10 [
 11         true,
 12         false,
 13         true,
 14         false,
 15         true,
 16         false,
 17         true,
 18         false
 19 ]
 20 -- End --
 21 
 22 -- Testcase --
 23 {%
 24         let o = {};
 25         let a = [ o, {}, "", null, false ];
 26 
 27         printf("%.J\n", [
 28                 o in a,
 29                 {} in a,
 30                 "" in a,
 31                 "test" in a,
 32                 null in a,
 33                 [] in a,
 34                 false in a,
 35                 true in a
 36         ]);
 37 %}
 38 -- End --
 39 
 40 2. Strict equality when testing array membership should rule out implict
 41 type coercion.
 42 
 43 -- Expect stdout --
 44 [
 45         true,
 46         false,
 47         false,
 48         false,
 49         true,
 50         false,
 51         false
 52 ]
 53 -- End --
 54 
 55 -- Testcase --
 56 {%
 57         let a = [ "", true ];
 58 
 59         printf("%.J\n", [
 60                 "" in a,
 61                 0 in a,
 62                 false in a,
 63                 null in a,
 64                 true in a,
 65                 1 in a,
 66                 1.0 in a
 67         ]);
 68 %}
 69 -- End --
 70 
 71 3. While there is the rule that `(NaN === NaN) == false`, testing for NaN
 72 in a given array containing NaN should yield `true`.
 73 
 74 -- Expect stdout --
 75 [
 76         true
 77 ]
 78 -- End --
 79 
 80 -- Testcase --
 81 {%
 82         let a = [ NaN ];
 83 
 84         printf("%.J\n", [
 85                 NaN in a
 86         ]);
 87 %}
 88 -- End --
 89 
 90 4. When the `in` operator is applied to an object, it tests whether the given
 91 string value is a key of the specified object. 
 92 
 93 -- Expect stdout --
 94 [
 95         true,
 96         true,
 97         true,
 98         false,
 99         false,
100         false,
101         false,
102         false
103 ]
104 -- End --
105 
106 -- Testcase --
107 {%
108         let o = { 
109                 "1": true,
110                 "test": false,
111                 "empty": null,
112                 "false": 0,
113                 "true": 1,
114                 "[ ]": "array",
115                 "{ }": "object"
116         };
117 
118         printf("%.J\n", [
119                 "1" in o,
120                 "test" in o,
121                 "empty" in o,
122                 1 in o,        // not implicitly converted to "1"
123                 false in o,    // not implicitly converted to "false"
124                 true in o,     // not implicitly converted to "true"
125                 [] in o,       // not implicitly converted to "[ ]"
126                 {} in o        // not implicitly converted to "{ }"
127         ]);
128 %}
129 -- End --

This page was automatically generated by LXR 0.3.1.  •  OpenWrt