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 ## STDOUT:
21 ok
22 ok
23 ## END
24
25 #### Approximate equality of Str x {Str, Int, Bool} with ~==
26 shopt -s oil:all
27
28 # Note: for now there's no !~== operator. Use: not (a ~== b)
29
30 if (' foo ' ~== 'foo') {
31 echo Str-Str
32 }
33 if (' BAD ' ~== 'foo') {
34 echo FAIL
35 }
36
37 if ('3 ' ~== 3) {
38 echo Str-Int
39 }
40 if ('4 ' ~== '3') {
41 echo FAIL
42 }
43
44 if (' true ' ~== true) {
45 echo Str-Bool
46 }
47 if (' true ' ~== false) {
48 echo FAIL
49 }
50
51 const matrix = [
52 ' TRue ' ~== true, # case insentiive
53 ' FALse ' ~== false,
54
55 # Note this is the opposite of exit codes :-(
56 # Maybe we should encourage 'if try' instead of 'if'
57 ' 1 ' ~== true,
58 ' 0 ' ~== false,
59 ]
60
61 # = matrix
62 if (matrix === [true, true, true, true]) {
63 echo 'bool matrix'
64 }
65
66 ## STDOUT:
67 Str-Str
68 Str-Int
69 Str-Bool
70 bool matrix
71 ## END
72
73 #### Wrong Types with ~==
74 shopt -s oil:all
75
76 # The LHS side should be a string
77
78 echo one
79 if (['1'] ~== ['1']) {
80 echo bad
81 }
82 echo two
83
84 if (3 ~== 3) {
85 echo bad
86 }
87
88 ## status: 1
89 ## STDOUT:
90 one
91 ## END
92
93
94 #### Equality of ~== with Float (deferred)
95 shopt -s oil:all
96
97 if (42 ~== 42.0) {
98 echo int-float
99 }
100 if (42 ~== 43.0) {
101 echo FAIL
102 }
103
104 if ('42' ~== 42.0) {
105 echo str-float
106 }
107 if ('42' ~== 43.0) {
108 echo FAIL
109 }
110
111 if (42 ~== '42.0') {
112 echo int-str-float
113 }
114 if (42 ~== '43.0') {
115 echo FAIL
116 }
117 ## STDOUT:
118 ## END
119
120
121 #### Comparison of Int
122 shopt -s oil:upgrade
123
124 if (1 < 2) {
125 echo '<'
126 }
127 if (2 <= 2) {
128 echo '<='
129 }
130 if (5 > 4) {
131 echo '>'
132 }
133 if (5 >= 5) {
134 echo '>='
135 }
136
137 if (2 < 1) {
138 echo no
139 }
140
141 ## STDOUT:
142 <
143 <=
144 >
145 >=
146 ## END
147
148 #### Comparison of Str does conversion to Int
149 shopt -s oil:upgrade
150
151 if ('2' < '11') {
152 echo '<'
153 }
154 if ('2' <= '2') {
155 echo '<='
156 }
157 if ('11' > '2') {
158 echo '>'
159 }
160 if ('5' >= '5') {
161 echo '>='
162 }
163
164 if ('2' < '1') {
165 echo no
166 }
167
168 ## STDOUT:
169 <
170 <=
171 >
172 >=
173 ## END
174
175
176 #### Mixed Type Comparison does conversion to Int
177 shopt -s oil:upgrade
178
179 if (2 < '11') {
180 echo '<'
181 }
182 if (2 <= '2') {
183 echo '<='
184 }
185 if (11 > '2') {
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
204 #### Invalid String is an error
205 shopt -s oil:upgrade
206
207 if ('3' < 'bar') {
208 echo no
209 }
210 echo 'should not get here'
211
212 ## status: 3
213 ## STDOUT:
214 ## END
215
216
217 #### Bool conversion -- explicit allowed, implicit not allowed
218
219 shopt -s oil:upgrade
220
221 if (Int(false) < Int(true)) {
222 echo '<'
223 }
224
225 if (Int(false) <= Int(false) ) {
226 echo '<='
227 }
228
229 # JavaScript and Python both have this, but Oil prefers being explicit
230
231 if (true < false) {
232 echo 'BAD'
233 }
234 echo 'should not get here'
235
236 ## status: 3
237 ## STDOUT:
238 <
239 <=
240 ## END
241
242
243 #### Chained Comparisons
244 shopt -s oil:upgrade
245 if (1 < 2 < 3) {
246 echo '123'
247 }
248 if (1 < 2 <= 2 <= 3 < 4) {
249 echo '123'
250 }
251
252 if (1 < 2 < 2) {
253 echo '123'
254 } else {
255 echo 'no'
256 }
257 ## STDOUT:
258 123
259 123
260 no
261 ## END
262
263 #### Tuple comparison is not allowed
264
265 # TODO:
266 # - tuples aren't hashable, so I think they shouldn't be comparable
267 # - and probably should just be removed from the language. JS doesn't have
268 # them
269 # - I think records and destructuring would be better
270
271 shopt -s oil:upgrade
272
273 var t1 = 3, 0
274 var t2 = 4, 0
275 var t3 = 3, 1
276
277 if (t2 > t1) { echo yes1 }
278 if (t3 > t1) { echo yes2 }
279 if ( (0,0) > t1) { echo yes3 }
280
281 # NOTE: ((0,0) causes problems -- it looks like a shell statement!
282 #if ( (0,0) > t1) { echo yes3 }
283 ## STDOUT:
284 yes1
285 yes2
286 ## END