1 |
# spec/oil-expr-compare |
2 |
|
3 |
#### Exact equality with === and !== |
4 |
shopt -s oil:all |
5 |
|
6 |
if (3 === 3) { |
7 |
echo 'ok' |
8 |
} |
9 |
if (3 === '3') { |
10 |
echo 'FAIL' |
11 |
} |
12 |
|
13 |
if (3 !== 3) { |
14 |
echo 'FAIL' |
15 |
} |
16 |
if (3 !== '3') { |
17 |
echo 'ok' |
18 |
} |
19 |
|
20 |
# dicts |
21 |
var d1 = {'a': 1, 'b': 2} |
22 |
var d2 = {'a': 1, 'b': 2} |
23 |
var d3 = {'a': 1, 'b': 3} |
24 |
if (d1 === d2) { |
25 |
echo 'ok' |
26 |
} |
27 |
if (d1 === d3) { |
28 |
echo 'FAIL' |
29 |
} |
30 |
if (d1 !== d3) { |
31 |
echo 'ok' |
32 |
} |
33 |
|
34 |
## STDOUT: |
35 |
ok |
36 |
ok |
37 |
ok |
38 |
ok |
39 |
## END |
40 |
|
41 |
#### Approximate equality of Str x {Str, Int, Bool} with ~== |
42 |
shopt -s oil:all |
43 |
|
44 |
# Note: for now there's no !~== operator. Use: not (a ~== b) |
45 |
|
46 |
if (' foo ' ~== 'foo') { |
47 |
echo Str-Str |
48 |
} |
49 |
if (' BAD ' ~== 'foo') { |
50 |
echo FAIL |
51 |
} |
52 |
|
53 |
if ('3 ' ~== 3) { |
54 |
echo Str-Int |
55 |
} |
56 |
if ('4 ' ~== '3') { |
57 |
echo FAIL |
58 |
} |
59 |
|
60 |
if (' true ' ~== true) { |
61 |
echo Str-Bool |
62 |
} |
63 |
if (' true ' ~== false) { |
64 |
echo FAIL |
65 |
} |
66 |
|
67 |
const matrix = [ |
68 |
' TRue ' ~== true, # case insentiive |
69 |
' FALse ' ~== false, |
70 |
] |
71 |
|
72 |
# = matrix |
73 |
if (matrix === [true, true]) { |
74 |
echo 'bool matrix' |
75 |
} |
76 |
|
77 |
## STDOUT: |
78 |
Str-Str |
79 |
Str-Int |
80 |
Str-Bool |
81 |
bool matrix |
82 |
## END |
83 |
|
84 |
#### Wrong Types with ~== |
85 |
shopt -s oil:all |
86 |
|
87 |
# The LHS side should be a string |
88 |
|
89 |
echo one |
90 |
if (['1'] ~== ['1']) { |
91 |
echo bad |
92 |
} |
93 |
echo two |
94 |
|
95 |
if (3 ~== 3) { |
96 |
echo bad |
97 |
} |
98 |
|
99 |
## status: 1 |
100 |
## STDOUT: |
101 |
one |
102 |
## END |
103 |
|
104 |
|
105 |
#### Equality of ~== with Float (deferred) |
106 |
shopt -s oil:all |
107 |
|
108 |
if (42 ~== 42.0) { |
109 |
echo int-float |
110 |
} |
111 |
if (42 ~== 43.0) { |
112 |
echo FAIL |
113 |
} |
114 |
|
115 |
if ('42' ~== 42.0) { |
116 |
echo str-float |
117 |
} |
118 |
if ('42' ~== 43.0) { |
119 |
echo FAIL |
120 |
} |
121 |
|
122 |
if (42 ~== '42.0') { |
123 |
echo int-str-float |
124 |
} |
125 |
if (42 ~== '43.0') { |
126 |
echo FAIL |
127 |
} |
128 |
## STDOUT: |
129 |
## END |
130 |
|
131 |
|
132 |
#### Comparison of Int |
133 |
shopt -s oil:upgrade |
134 |
|
135 |
if (1 < 2) { |
136 |
echo '<' |
137 |
} |
138 |
if (2 <= 2) { |
139 |
echo '<=' |
140 |
} |
141 |
if (5 > 4) { |
142 |
echo '>' |
143 |
} |
144 |
if (5 >= 5) { |
145 |
echo '>=' |
146 |
} |
147 |
|
148 |
if (2 < 1) { |
149 |
echo no |
150 |
} |
151 |
|
152 |
## STDOUT: |
153 |
< |
154 |
<= |
155 |
> |
156 |
>= |
157 |
## END |
158 |
|
159 |
#### Comparison of Str does conversion to Int |
160 |
shopt -s oil:upgrade |
161 |
|
162 |
if ('2' < '11') { |
163 |
echo '<' |
164 |
} |
165 |
if ('2' <= '2') { |
166 |
echo '<=' |
167 |
} |
168 |
if ('11' > '2') { |
169 |
echo '>' |
170 |
} |
171 |
if ('5' >= '5') { |
172 |
echo '>=' |
173 |
} |
174 |
|
175 |
if ('2' < '1') { |
176 |
echo no |
177 |
} |
178 |
|
179 |
## STDOUT: |
180 |
< |
181 |
<= |
182 |
> |
183 |
>= |
184 |
## END |
185 |
|
186 |
|
187 |
#### Mixed Type Comparison does conversion to Int |
188 |
shopt -s oil:upgrade |
189 |
|
190 |
if (2 < '11') { |
191 |
echo '<' |
192 |
} |
193 |
if (2 <= '2') { |
194 |
echo '<=' |
195 |
} |
196 |
if (11 > '2') { |
197 |
echo '>' |
198 |
} |
199 |
if (5 >= '5') { |
200 |
echo '>=' |
201 |
} |
202 |
|
203 |
if (2 < '1') { |
204 |
echo no |
205 |
} |
206 |
|
207 |
## STDOUT: |
208 |
< |
209 |
<= |
210 |
> |
211 |
>= |
212 |
## END |
213 |
|
214 |
|
215 |
#### Invalid String is an error |
216 |
shopt -s oil:upgrade |
217 |
|
218 |
if ('3' < 'bar') { |
219 |
echo no |
220 |
} |
221 |
echo 'should not get here' |
222 |
|
223 |
## status: 3 |
224 |
## STDOUT: |
225 |
## END |
226 |
|
227 |
|
228 |
#### Bool conversion -- explicit allowed, implicit not allowed |
229 |
|
230 |
shopt -s oil:upgrade |
231 |
|
232 |
if (Int(false) < Int(true)) { |
233 |
echo '<' |
234 |
} |
235 |
|
236 |
if (Int(false) <= Int(false) ) { |
237 |
echo '<=' |
238 |
} |
239 |
|
240 |
# JavaScript and Python both have this, but Oil prefers being explicit |
241 |
|
242 |
if (true < false) { |
243 |
echo 'BAD' |
244 |
} |
245 |
echo 'should not get here' |
246 |
|
247 |
## status: 3 |
248 |
## STDOUT: |
249 |
< |
250 |
<= |
251 |
## END |
252 |
|
253 |
|
254 |
#### Chained Comparisons |
255 |
shopt -s oil:upgrade |
256 |
if (1 < 2 < 3) { |
257 |
echo '123' |
258 |
} |
259 |
if (1 < 2 <= 2 <= 3 < 4) { |
260 |
echo '123' |
261 |
} |
262 |
|
263 |
if (1 < 2 < 2) { |
264 |
echo '123' |
265 |
} else { |
266 |
echo 'no' |
267 |
} |
268 |
## STDOUT: |
269 |
123 |
270 |
123 |
271 |
no |
272 |
## END |
273 |
|
274 |
#### Tuple comparison is not allowed |
275 |
|
276 |
# TODO: |
277 |
# - tuples aren't hashable, so I think they shouldn't be comparable |
278 |
# - and probably should just be removed from the language. JS doesn't have |
279 |
# them |
280 |
# - I think records and destructuring would be better |
281 |
|
282 |
shopt -s oil:upgrade |
283 |
|
284 |
var t1 = 3, 0 |
285 |
var t2 = 4, 0 |
286 |
var t3 = 3, 1 |
287 |
|
288 |
if (t2 > t1) { echo yes1 } |
289 |
if (t3 > t1) { echo yes2 } |
290 |
if ( (0,0) > t1) { echo yes3 } |
291 |
|
292 |
# NOTE: ((0,0) causes problems -- it looks like a shell statement! |
293 |
#if ( (0,0) > t1) { echo yes3 } |
294 |
## STDOUT: |
295 |
yes1 |
296 |
yes2 |
297 |
## END |
298 |
|
299 |
#### Collection membership |
300 |
shopt -s oil:upgrade |
301 |
|
302 |
var l = [3, 17, 1, {'foo': 'bar'}, 'bloop'] |
303 |
var d = {'a': 1, 'b': false, 'c': 'hmmm'} |
304 |
|
305 |
if (17 in l) { |
306 |
echo Int-in-List |
307 |
} |
308 |
if (4 in l) { |
309 |
echo FAIL |
310 |
} |
311 |
if ('bloop' in l) { |
312 |
echo Str-in-List |
313 |
} |
314 |
if ({'foo': 'bar'} in l) { |
315 |
echo Dict-in-List |
316 |
} |
317 |
if ({'foo': 'baz'} in l) { |
318 |
echo FAIL |
319 |
} |
320 |
if ('a' in d) { |
321 |
echo Str-in-Dict |
322 |
} |
323 |
if ('d' in d) { |
324 |
echo FAIL |
325 |
} |
326 |
|
327 |
## STDOUT: |
328 |
Int-in-List |
329 |
Str-in-List |
330 |
Dict-in-List |
331 |
Str-in-Dict |
332 |
## END |