/
did_you_mean_spec.cr
266 lines (218 loc) · 4.63 KB
/
did_you_mean_spec.cr
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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
require "../../spec_helper"
describe "Semantic: did you mean" do
it "says did you mean for one mistake in short word in instance method" do
assert_error "
class Foo
def bar
end
end
Foo.new.baz
",
"Did you mean 'bar'?"
end
it "says did you mean for two mistakes in long word in instance method" do
assert_error "
class Foo
def barbara
end
end
Foo.new.bazbaza
",
"Did you mean 'barbara'?"
end
it "says did you mean for global method with parenthesis" do
assert_error "
def bar
end
baz()
",
"Did you mean 'bar'?"
end
it "says did you mean for global method without parenthesis" do
assert_error "
def bar
end
baz
",
"Did you mean 'bar'?"
end
it "says did you mean for variable" do
assert_error "
bar = 1
baz
",
"Did you mean 'bar'?"
end
it "says did you mean for class" do
assert_error "
class Foo
end
Fog.new
",
"Did you mean 'Foo'?"
end
it "says did you mean for nested class" do
assert_error "
class Foo
class Bar
end
end
Foo::Baz.new
",
"Did you mean 'Foo::Bar'?"
end
it "says did you mean finds most similar in def" do
assert_error "
def barbaza
end
def barbara
end
barbarb
",
"Did you mean 'barbara'?"
end
it "says did you mean finds most similar in type" do
assert_error "
class Barbaza
end
class Barbara
end
Barbarb
",
"Did you mean 'Barbara'?"
end
it "doesn't suggest for operator" do
error = assert_error <<-CRYSTAL
class Foo
def +
end
end
Foo.new.a
CRYSTAL
error.to_s.should_not contain("Did you mean")
end
it "says did you mean for named argument" do
assert_error "
def foo(barbara = 1)
end
foo bazbaza: 1
",
"Did you mean 'barbara'?"
end
it "says did you mean for instance var" do
assert_error %(
class Foo
def initialize
@barbara = 1
end
def foo
@bazbaza.abs
end
end
Foo.new.foo
),
"Did you mean '@barbara'?"
end
it "says did you mean for instance var in subclass" do
assert_error %(
class Foo
def initialize
@barbara = 1
end
end
class Bar < Foo
def foo
@bazbaza.abs
end
end
Bar.new.foo
),
"Did you mean '@barbara'?"
end
it "doesn't suggest when declaring var with suffix if and using it (#946)" do
assert_error %(
a if a = 1
),
"If you declared 'a' in a suffix if, declare it in a regular if for this to work"
end
it "doesn't suggest when declaring var inside macro (#466)" do
assert_error %(
macro foo
a = 1
end
foo
a
),
"If the variable was declared in a macro it's not visible outside it"
end
it "suggest that there might be a typo for an initialize method" do
assert_error %(
class Foo
def intialize(x)
end
end
Foo.new(1)
),
"do you maybe have a typo in this 'intialize' method?"
end
it "suggest that there might be a typo for an initialize method in inherited class" do
assert_error %(
class Foo
def initialize
end
end
class Bar < Foo
def intialize(x)
end
end
Bar.new(1)
),
"do you maybe have a typo in this 'intialize' method?"
end
it "suggest that there might be a typo for an initialize method with overload" do
assert_error %(
class Foo
def initialize(x : Int32)
end
def intialize(y : Float64)
end
end
Foo.new(1.0)
),
"do you maybe have a typo in this 'intialize' method?"
end
it "suggests for class variable" do
assert_error %(
class Foo
@@foobar = 1
@@fooobar
end
), "Did you mean '@@foobar'?"
end
it "suggests a better alternative to logical operators (#2715)" do
ex = assert_error %(
def rand(x : Int32)
end
class String
def bytes
self
end
end
if "a".bytes and 1
1
end
),
"undefined method 'and' for top-level"
ex.to_s.should contain "Did you mean '&&'?"
end
it "says did you mean in instance var declaration" do
assert_error %(
class FooBar
end
class Foo
@x : FooBaz
end
),
"Did you mean 'FooBar'?"
end
end