1 | ## oils_failures_allowed: 0
|
2 |
|
3 | #### Exact equality with === and !==
|
4 | shopt -s ysh: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 ysh: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 ysh: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 | #### === on float not allowed
|
105 |
|
106 | $SH -c '
|
107 | shopt -s ysh:upgrade
|
108 | pp test_ (1.0 === 2.0)
|
109 | echo ok
|
110 | '
|
111 | echo status=$?
|
112 |
|
113 | $SH -c '
|
114 | shopt -s ysh:upgrade
|
115 | pp test_ (42 === 3.0)
|
116 | echo ok
|
117 | '
|
118 | echo status=$?
|
119 |
|
120 | ## STDOUT:
|
121 | status=3
|
122 | status=3
|
123 | ## END
|
124 |
|
125 |
|
126 | #### floatsEqual()
|
127 |
|
128 | var x = 42.0
|
129 | pp test_ (floatsEqual(42.0, x))
|
130 |
|
131 | pp test_ (floatsEqual(42.0, x + 1))
|
132 |
|
133 | ## STDOUT:
|
134 | (Bool) true
|
135 | (Bool) false
|
136 | ## END
|
137 |
|
138 | #### Comparison converts from Str -> Int or Float
|
139 | echo ' i i' $[1 < 2]
|
140 | echo 'si i' $['1' < 2]
|
141 | echo ' i si' $[1 < '2']
|
142 | echo ---
|
143 |
|
144 | echo ' f f' $[2.5 > 1.5]
|
145 | echo 'sf f' $['2.5' > 1.5]
|
146 | echo ' f sf' $[2.5 > '1.5']
|
147 | echo ---
|
148 |
|
149 | echo ' i f' $[4 <= 1.5]
|
150 | echo 'si f' $['4' <= 1.5]
|
151 | echo ' i sf' $[4 <= '1.5']
|
152 | echo ---
|
153 |
|
154 | echo ' f i' $[5.0 >= 2]
|
155 | echo 'sf i' $['5.0' >= 2]
|
156 | echo ' f si' $[5.0 >= '2']
|
157 |
|
158 | ## STDOUT:
|
159 | i i true
|
160 | si i true
|
161 | i si true
|
162 | ---
|
163 | f f true
|
164 | sf f true
|
165 | f sf true
|
166 | ---
|
167 | i f false
|
168 | si f false
|
169 | i sf false
|
170 | ---
|
171 | f i true
|
172 | sf i true
|
173 | f si true
|
174 | ## END
|
175 |
|
176 | #### Comparison of Int
|
177 | shopt -s oil:upgrade
|
178 |
|
179 | if (1 < 2) {
|
180 | echo '<'
|
181 | }
|
182 | if (2 <= 2) {
|
183 | echo '<='
|
184 | }
|
185 | if (5 > 4) {
|
186 | echo '>'
|
187 | }
|
188 | if (5 >= 5) {
|
189 | echo '>='
|
190 | }
|
191 |
|
192 | if (2 < 1) {
|
193 | echo no
|
194 | }
|
195 |
|
196 | ## STDOUT:
|
197 | <
|
198 | <=
|
199 | >
|
200 | >=
|
201 | ## END
|
202 |
|
203 | #### Comparison of Str does conversion to Int
|
204 | shopt -s oil:upgrade
|
205 |
|
206 | if ('2' < '11') {
|
207 | echo '<'
|
208 | }
|
209 | if ('2' <= '2') {
|
210 | echo '<='
|
211 | }
|
212 | if ('11' > '2') {
|
213 | echo '>'
|
214 | }
|
215 | if ('5' >= '5') {
|
216 | echo '>='
|
217 | }
|
218 |
|
219 | if ('2' < '1') {
|
220 | echo no
|
221 | }
|
222 |
|
223 | ## STDOUT:
|
224 | <
|
225 | <=
|
226 | >
|
227 | >=
|
228 | ## END
|
229 |
|
230 |
|
231 | #### Mixed Type Comparison does conversion to Int
|
232 | shopt -s oil:upgrade
|
233 |
|
234 | if (2 < '11') {
|
235 | echo '<'
|
236 | }
|
237 | if (2 <= '2') {
|
238 | echo '<='
|
239 | }
|
240 | if (11 > '2') {
|
241 | echo '>'
|
242 | }
|
243 | if (5 >= '5') {
|
244 | echo '>='
|
245 | }
|
246 |
|
247 | if (2 < '1') {
|
248 | echo no
|
249 | }
|
250 |
|
251 | ## STDOUT:
|
252 | <
|
253 | <=
|
254 | >
|
255 | >=
|
256 | ## END
|
257 |
|
258 |
|
259 | #### Invalid String is an error
|
260 | shopt -s oil:upgrade
|
261 |
|
262 | if ('3' < 'bar') {
|
263 | echo no
|
264 | }
|
265 | echo 'should not get here'
|
266 |
|
267 | ## status: 3
|
268 | ## STDOUT:
|
269 | ## END
|
270 |
|
271 |
|
272 | #### Bool conversion -- explicit allowed, implicit not allowed
|
273 |
|
274 | shopt -s ysh:upgrade
|
275 |
|
276 | if (int(false) < int(true)) {
|
277 | echo '<'
|
278 | }
|
279 |
|
280 | if (int(false) <= int(false) ) {
|
281 | echo '<='
|
282 | }
|
283 |
|
284 | # JavaScript and Python both have this, but Oil prefers being explicit
|
285 |
|
286 | if (true < false) {
|
287 | echo 'BAD'
|
288 | }
|
289 | echo 'should not get here'
|
290 |
|
291 | ## status: 3
|
292 | ## STDOUT:
|
293 | <
|
294 | <=
|
295 | ## END
|
296 |
|
297 |
|
298 | #### Chained Comparisons
|
299 | shopt -s ysh:upgrade
|
300 |
|
301 | if (1 < 2 < 3) {
|
302 | echo '123'
|
303 | }
|
304 | if (1 < 2 <= 2 <= 3 < 4) {
|
305 | echo '123'
|
306 | }
|
307 |
|
308 | if (1 < 2 < 2) {
|
309 | echo '123'
|
310 | } else {
|
311 | echo 'no'
|
312 | }
|
313 | ## STDOUT:
|
314 | 123
|
315 | 123
|
316 | no
|
317 | ## END
|
318 |
|
319 | #### List / "Tuple" comparison is not allowed
|
320 |
|
321 | shopt -s oil:upgrade
|
322 |
|
323 | var t1 = 3, 0
|
324 | var t2 = 4, 0
|
325 | var t3 = 3, 1
|
326 |
|
327 | if (t2 > t1) { echo yes1 }
|
328 | if (t3 > t1) { echo yes2 }
|
329 | if ( (0,0) > t1) { echo yes3 }
|
330 |
|
331 | ## status: 3
|
332 | ## STDOUT:
|
333 | ## END
|
334 |
|
335 | #### Ternary op behaves like if statement
|
336 | shopt -s ysh:upgrade
|
337 |
|
338 | if ([1]) {
|
339 | var y = 42
|
340 | } else {
|
341 | var y = 0
|
342 | }
|
343 | echo y=$y
|
344 |
|
345 | var x = 42 if [1] else 0
|
346 | echo x=$x
|
347 |
|
348 | ## STDOUT:
|
349 | y=42
|
350 | x=42
|
351 | ## END
|
352 |
|
353 | #### Undefined comparisons
|
354 | shopt -s ysh:all
|
355 |
|
356 | func f() { true }
|
357 | var mydict = {}
|
358 | var myexpr = ^[123]
|
359 |
|
360 | var unimpl = [
|
361 | / [a-z]+ /, # Eggex
|
362 | myexpr, # Expr
|
363 | ^(echo hello), # Block
|
364 | f, # Func
|
365 | mydict->keys, # BoundFunc
|
366 | # These cannot be constructed
|
367 | # - Proc
|
368 | # - Slice
|
369 | # - Range
|
370 | ]
|
371 |
|
372 | for val in (unimpl) {
|
373 | try { = val === val }
|
374 | if (_status !== 3) {
|
375 | exit 1
|
376 | }
|
377 | }
|
378 | ## STDOUT:
|
379 | ## END
|
380 |
|
381 | #### Non-comparable types in case arms
|
382 | var myexpr = ^[123]
|
383 |
|
384 | case (myexpr) {
|
385 | (myexpr) { echo 123; }
|
386 | }
|
387 | ## status: 3
|
388 | ## STDOUT:
|
389 | ## END
|