forked from michaeledgar/laser
/
raise_type_inference_spec.rb
186 lines (175 loc) · 5.32 KB
/
raise_type_inference_spec.rb
1
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
73
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
require_relative 'spec_helper'
describe 'CFG-based raise type inference' do
it 'should infer types based on specified overloads' do
g = cfg <<-EOF
class RI1
def tap_10
yield 10
10
end
end
EOF
method = ClassRegistry['RI1'].instance_method('tap_10')
method.raise_type_for_types(
Utilities.type_for(ClassRegistry['RI1'])).should equal_type(
Types::UnionType.new([Types::ClassType.new('LocalJumpError', :invariant)]))
end
it 'should infer types based on raising a string' do
g = cfg <<-EOF
class RI2
def raise_string
raise 'foo'
end
end
EOF
method = ClassRegistry['RI2'].instance_method('raise_string')
method.raise_type_for_types(
Utilities.type_for(ClassRegistry['RI2'])).should equal_type(
Types::UnionType.new([Types::ClassType.new('RuntimeError', :invariant)]))
end
it 'should infer types based on raising an Exception class' do
g = cfg <<-EOF
class RI3
def raise_class
raise TypeError
end
end
EOF
method = ClassRegistry['RI3'].instance_method('raise_class')
method.raise_type_for_types(
Utilities.type_for(ClassRegistry['RI3'])).should equal_type(
Types::UnionType.new([Types::ClassType.new('TypeError', :invariant)]))
end
it 'should infer types based on raising an Exception instance' do
g = cfg <<-EOF
class RI4
def raise_instance
raise ArgumentError.new('foo')
end
end
EOF
method = ClassRegistry['RI4'].instance_method('raise_instance')
method.raise_type_for_types(
Utilities.type_for(ClassRegistry['RI4'])).should equal_type(
Types::UnionType.new([Types::ClassType.new('ArgumentError', :invariant)]))
end
it 'should infer raises from #initialize when calling Class.new' do
g = cfg <<-EOF
class RTInfer2
def initialize(x)
raise TypeError.new("I don't like integers") if Integer === x
end
end
def make_rtinfer_2(x)
RTInfer2.new(x)
end
EOF
method = ClassRegistry['Object'].instance_method('make_rtinfer_2')
# call make_rinfer_2 should raise for a fixnum
method.raise_type_for_types(
Types::STRING, # doesn't matter
[Types::FIXNUM],
Types::NILCLASS).should equal_type(ClassRegistry['TypeError'].as_type)
method.raise_type_for_types(
Types::STRING, # doesn't matter
[ClassRegistry['Bignum'].as_type],
Types::NILCLASS).should equal_type(ClassRegistry['TypeError'].as_type)
method.raise_type_for_types(
Types::STRING, # doesn't matter
[Types::STRING],
Types::NILCLASS).should equal_type(Types::EMPTY)
end
it 'should infer a potential raises by argument type' do
g = cfg <<-EOF
class RTInfer3
def initialize(x)
try_to_foo(x) if x != 0
end
def try_to_foo(x)
case x
when Integer
raise ArgumentError.new('no negative numbers') if x < 0
when Float
raise TypeError.new('no floats at all')
else
x.ljust(20)
end
end
end
def make_rtinfer_3(x)
RTInfer3.new(x)
end
EOF
method = ClassRegistry['Object'].instance_method('make_rtinfer_3')
# call make_rinfer_2 should raise for a fixnum
method.raise_type_for_types(
Types::STRING, # doesn't matter
[Types::FLOAT],
Types::NILCLASS).should equal_type(ClassRegistry['TypeError'].as_type)
method.raise_type_for_types(
Types::STRING, # doesn't matter
[ClassRegistry['Bignum'].as_type],
Types::NILCLASS).should equal_type(ClassRegistry['ArgumentError'].as_type)
method.raise_type_for_types(
Types::STRING, # doesn't matter
[Types::ARRAY],
Types::NILCLASS).should equal_type(ClassRegistry['NoMethodError'].as_type)
method.raise_type_for_types(
Types::STRING, # doesn't matter
[Types::STRING],
Types::NILCLASS).should equal_type(Types::EMPTY)
end
it 'can infer raises from calls to the annotated String class' do
g = cfg <<-EOF
class RTInfer4
def silly(x)
x.getbyte(x.size * 2)
end
end
EOF
method = ClassRegistry['RTInfer4'].instance_method('silly')
method.raise_type_for_types(
Utilities.type_for(ClassRegistry['RTInfer4']),
[Types::STRING],
Types::NILCLASS).should equal_type(ClassRegistry['IndexError'].as_type |
ClassRegistry['TypeError'].as_type)
method.raise_type_for_types(
Utilities.type_for(ClassRegistry['RTInfer4']),
[Types::FIXNUM],
Types::NILCLASS).should equal_type(ClassRegistry['NoMethodError'].as_type)
end
it 'can infer a guaranteed NoMethodError from privacy violations' do
g = cfg <<-EOF
class RTInfer5
def foo
end
private :foo
def bar
self.foo # error!
end
end
EOF
method = ClassRegistry['RTInfer5'].instance_method('bar')
method.raise_type_for_types(
Utilities.type_for(ClassRegistry['RTInfer5'])).should(
equal_type(ClassRegistry['NoMethodError'].as_type))
end
it 'can infer a potential lookup failure when a successful one exists' do
g = cfg <<-EOF
class RTInfer6
def bar
if gets.size > 0
x = 'hello'
else
x = 5
end
x.intern # error sometimes
end
end
EOF
method = ClassRegistry['RTInfer6'].instance_method('bar')
method.raise_type_for_types(
Utilities.type_for(ClassRegistry['RTInfer6'])).should(
equal_type(ClassRegistry['NoMethodError'].as_type))
end
end