Newer
Older
Ary Borenszweig
committed
#!/usr/bin/env bin/crystal --run
Ary Borenszweig
committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
require "../../spec_helper"
describe "Codegen: is_a?" do
it "codegens is_a? true for simple type" do
run("1.is_a?(Int)").to_b.should be_true
end
it "codegens is_a? false for simple type" do
run("1.is_a?(Bool)").to_b.should be_false
end
it "codegens is_a? with union gives true" do
run("(1 == 1 ? 1 : 'a').is_a?(Int)").to_b.should be_true
end
it "codegens is_a? with union gives false" do
run("(1 == 1 ? 1 : 'a').is_a?(Char)").to_b.should be_false
end
it "codegens is_a? with union gives false" do
run("(1 == 1 ? 1 : 'a').is_a?(Float)").to_b.should be_false
end
it "codegens is_a? with union gives true" do
run("(1 == 1 ? 1 : 'a').is_a?(Object)").to_b.should be_true
end
it "codegens is_a? with nilable gives true" do
run("(1 == 1 ? nil : Reference.new).is_a?(Nil)").to_b.should be_true
end
it "codegens is_a? with nilable gives false becuase other type 1" do
run("(1 == 1 ? nil : Reference.new).is_a?(Reference)").to_b.should be_false
end
it "codegens is_a? with nilable gives false becuase other type 2" do
run("(1 == 2 ? nil : Reference.new).is_a?(Reference)").to_b.should be_true
end
it "codegens is_a? with nilable gives false becuase no type" do
run("(1 == 2 ? nil : Reference.new).is_a?(String)").to_b.should be_false
end
it "codegens is_a? with nilable gives false becuase no type" do
run("1.is_a?(Object)").to_b.should be_true
end
it "evaluate method on filtered type" do
run("a = 1; a = 'a'; if a.is_a?(Char); a.ord; else; 0; end").to_i.chr.should eq('a')
end
it "evaluate method on filtered type nilable type not-nil" do
run("
class Foo
def foo
1
end
end
a = nil
a = Foo.new
if a.is_a?(Foo)
a.foo
else
2
end
").to_i.should eq(1)
end
it "evaluate method on filtered type nilable type nil" do
run("
Ary Borenszweig
committed
struct Nil
Ary Borenszweig
committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
def foo
1
end
end
class Foo
end
a = Foo.new
a = nil
if a.is_a?(Nil)
a.foo
else
2
end
").to_i.should eq(1)
end
it "evaluates method on filtered union type" do
run("
class Foo
def initialize(x)
@x = x
end
def x
@x
end
end
a = 1
a = Foo.new(2)
if a.is_a?(Reference)
a.x
else
0
end
").to_i.should eq(2)
end
it "evaluates method on filtered union type 2" do
run("
class Foo
def initialize(x)
@x = x
end
def x
@x
end
end
class Bar
def initialize(x)
@x = x
end
def x
@x
end
end
a = 1
a = Foo.new(2)
a = Bar.new(3)
if a.is_a?(Reference)
a.x
else
0
end
").to_i.should eq(3)
end
it "evaluates method on filtered union type 3" do
run("
require \"prelude\"
a = 1
a = [1.1]
a = [5]
if a.is_a?(Enumerable)
a[0]
else
0
end.to_i
").to_i.should eq(5)
end
it "codegens when is_a? is always false but properties are used" do
run("
Ary Borenszweig
committed
require \"prelude\"
Ary Borenszweig
committed
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
class Foo
def obj; 1 end
end
foo = 1
foo.is_a?(Foo) && foo.obj && foo.obj
").to_b.should be_false
end
it "codegens is_a? on right side of and" do
run("
class Foo
def bar
true
end
end
foo = Foo.new || nil
if 1 == 1 && foo.is_a?(Foo) && foo.bar
1
else
2
end
").to_i.should eq(1)
end
it "codegens is_a? with hierarchy" do
run("
class Foo
end
class Bar < Foo
end
foo = Bar.new || Foo.new
foo.is_a?(Bar) ? 1 : 2
").to_i.should eq(1)
end
it "codegens is_a? with hierarchy and nil" do
run("
class Foo
end
class Bar < Foo
end
f = Foo.new || Bar.new || nil
f.is_a?(Foo) ? 1 : 2
").to_i.should eq(1)
end
it "codegens is_a? with hierarchy and module" do
run("
module Bar
end
abstract class FooBase2
end
abstract class FooBase < FooBase2
include Bar
end
class Foo < FooBase
end
class Foo2 < FooBase2
end
f = Foo.new || Foo2.new
f.is_a?(Bar)
").to_b.should be_true
end
it "restricts simple type with union" do
run("
a = 1
if a.is_a?(Int32 | Char)
a + 1
else
0
end
").to_i.should eq(2)
end
it "restricts union with union" do
Ary Borenszweig
committed
struct Char
def +(other : Int32)
other
end
end
Ary Borenszweig
committed
struct Bool
def foo
2
end
end
a = 1 || 'a' || false
if a.is_a?(Int32 | Char)
a + 2
else
a.foo
end
").to_i.should eq(3)
end
Ary Borenszweig
committed
it "codegens is_a? with a Const does comparison and gives true" do
run("
require \"prelude\"
A = 1
1.is_a?(A)
").to_b.should be_true
end
it "codegens is_a? with a Const does comparison and gives false" do
run("
require \"prelude\"
A = 1
2.is_a?(A)
").to_b.should be_false
end
it "gives false if generic type doesn't match exactly" do
run("
class Foo(T)
end
foo = Foo(Int32 | Float64).new
foo.is_a?(Foo(Int32)) ? 1 : 2
").to_i.should eq(2)
end
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
it "does is_a? with more strict hierarchy type" do
run("
class Foo
end
class Bar < Foo
def foo
2
end
end
f = Bar.new || Foo.new
if f.is_a?(Bar)
f.foo
else
1
end
").to_i.should eq(2)
end
it "codegens is_a? casts union to nilable" do
run("
class Foo; end
var = \"hello\" || Foo.new || nil
if var.is_a?(Foo | Nil)
var2 = var
1
else
2
end
").to_i.should eq(2)
end
it "codegens is_a? casts union to nilable in method" do
run("
class Foo; end
def foo(var)
if var.is_a?(Foo | Nil)
var2 = var
1
else
2
end
end
var = \"hello\" || Foo.new || nil
foo(var)
").to_i.should eq(2)
end
it "codegens is_a? from hierarchy type to module" do
run("
module Moo
end
class Foo
end
class Bar < Foo
include Moo
end
class Baz < Foo
include Moo
end
f = Bar.new || Baz.new
if f.is_a?(Moo)
g = f
1
else
2
end
").to_i.should eq(1)
end
Ary Borenszweig
committed
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
it "codegens is_a? from nilable reference union type to nil" do
run("
class Foo
end
class Bar
end
a = Foo.new || Bar.new || nil
if a.is_a?(Nil)
b = a
1
else
2
end
").to_i.should eq(2)
end
it "codegens is_a? from nilable reference union type to type" do
run("
class Foo
end
class Bar
end
a = Foo.new || Bar.new || nil
if a.is_a?(Foo)
b = a
1
else
2
end
").to_i.should eq(1)
end
it "says false for value.is_a?(Class)" do
run("
1.is_a?(Class)
").to_b.should be_false
end