/
source_spec.rb
326 lines (302 loc) · 8.46 KB
/
source_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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
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
describe Solargraph::Source do
it "parses code" do
code = 'class Foo;def bar;end;end'
source = described_class.new(code)
expect(source.code).to eq(code)
expect(Solargraph::Parser.is_ast_node?(source.node)).to be_truthy
expect(source).to be_parsed
end
it "fixes invalid code" do
code = 'class Foo; def bar; x.'
source = described_class.new(code)
expect(source.code).to eq(code)
# @todo Unparsed code is resulting in nil nodes, maybe temporarily.
# See Solargraph::Source#initialize
# expect(source.node).to be_a(Parser::AST::Node)
expect(source).not_to be_parsed
end
it "finds ranges" do
code = %(
class Foo
def bar
end
end
)
source = described_class.new(code)
range = Solargraph::Range.new(Solargraph::Position.new(2, 8), Solargraph::Position.new(2, 15))
expect(source.at(range)).to eq('def bar')
end
it "finds nodes" do
# @todo This test is specific to Parser and breaks with RubyVM.
next if Solargraph::Parser.rubyvm?
code = 'class Foo;def bar;end;end'
source = described_class.new(code)
node = source.node_at(0, 0)
expect(node.type).to eq(:class)
node = source.node_at(0, 10)
expect(node.type).to eq(:def)
end
it "synchronizes from incremental updates" do
code = 'class Foo;def bar;end;end'
source = described_class.new(code)
updater = Solargraph::Source::Updater.new(
nil, 0, [Solargraph::Source::Change.new(
Solargraph::Range.new(
Solargraph::Position.new(0, 9),
Solargraph::Position.new(0, 9)
),
'd'
)]
)
changed = source.synchronize(updater)
expect(changed.code).to start_with('class Food;')
# @todo This test is specific to Parser and breaks with RubyVM.
next if Solargraph::Parser.rubyvm?
expect(changed.node.children[0].children[1]).to eq(:Food)
end
it "synchronizes from full updates" do
code1 = 'class Foo;end'
code2 = 'class Bar;end'
source = described_class.new(code1)
updater = Solargraph::Source::Updater.new(nil, 0, [
Solargraph::Source::Change.new(nil, code2)
])
changed = source.synchronize(updater)
expect(changed.code).to eq(code2)
# @todo This test is specific to Parser and breaks with RubyVM.
next if Solargraph::Parser.rubyvm?
expect(changed.node.children[0].children[1]).to eq(:Bar)
end
it "repairs broken incremental updates" do
code = %(
class Foo
def bar
end
end
)
source = described_class.new(code)
updater = Solargraph::Source::Updater.new(
nil, 0, [Solargraph::Source::Change.new(
Solargraph::Range.new(
Solargraph::Position.new(3, 0),
Solargraph::Position.new(3, 1)
),
'@'
)]
)
changed = source.synchronize(updater)
expect(changed).to be_parsed
expect(changed).to be_repaired
end
it "flags irreparable updates" do
code = 'class Foo;def bar;end;end'
source = described_class.new(code)
updater = Solargraph::Source::Updater.new(nil, 0, [
Solargraph::Source::Change.new(nil, 'end;end')
])
changed = source.synchronize(updater)
expect(changed).to be_parsed
expect(changed).to be_repaired
end
it "finds references" do
source = Solargraph::Source.load_string(%(
class Foo
def bar
end
def bar=
end
end
𐐀 = Foo.new # unicode name to test offset
𐐀.bar
𐐀.bar = 1
))
foos = source.references('Foo')
foobacks = foos.map{|f| source.at(f.range)}
expect(foobacks).to eq(['Foo', 'Foo'])
bars = source.references('bar')
barbacks = bars.map{|b| source.at(b.range)}
expect(barbacks).to eq(['bar', 'bar'])
assign_bars = source.references('bar=')
assign_barbacks = assign_bars.map{|b| source.at(b.range)}
expect(assign_barbacks).to eq(['bar=', 'bar ='])
end
it "allows escape sequences incompatible with UTF-8" do
source = Solargraph::Source.new('
x = " Un bUen café \x92"
puts x
')
expect(source.parsed?).to be(true)
end
it "fixes invalid byte sequences in UTF-8 encoding" do
expect {
Solargraph::Source.load('spec/fixtures/invalid_byte.rb')
}.not_to raise_error
end
it "loads files with Unicode characters" do
expect {
Solargraph::Source.load('spec/fixtures/unicode.rb')
}.not_to raise_error
end
it "updates itself when code does not change" do
original = Solargraph::Source.load_string('x = y', 'test.rb')
updater = Solargraph::Source::Updater.new('test.rb', 1, [])
updated = original.synchronize(updater)
expect(original).to be(updated)
expect(updated.version).to eq(1)
end
it "handles unparseable code" do
source = Solargraph::Source.load_string(%(
100.times do |num|
))
# @todo Unparseable code results in a nil node for now, but that could
# change. See Solargraph::Source#initialize
expect(source.node).to be_nil
expect(source.parsed?).to be(false)
end
it "finds foldable ranges" do
# Of the 7 possible ranges, 2 are too short to be foldable
source = Solargraph::Source.load_string(%(
=begin
Range 1
=end
def range_2
x = y
puts z
end
# Range 3.1
# Range 3.2
# Range 3.3
a = b
# Range 4.1 (too short)
# Range 4.2
c = b
# Range 5.1 (too short)
d = c # inline
# Range 6.1
# Range 6.2
# Range 6.3
e = d # inline
# Range 7.1
# Range 7.2
# Range 7.3
))
expect(source.folding_ranges.length).to eq(5)
end
it 'folds multiline strings' do
source = Solargraph::Source.load_string(%(
a = 1
b = 2
c = 3
d = %(
one
two
three
)
))
expect(source.folding_ranges).to be_one
expect(source.folding_ranges.first.start.line).to eq(4)
end
it 'folds multiline arrays' do
source = Solargraph::Source.load_string(%(
a = 1
b = 2
c = 3
d = [
one,
two,
three
]
))
expect(source.folding_ranges).to be_one
expect(source.folding_ranges.first.start.line).to eq(4)
end
it 'folds multiline hashes' do
source = Solargraph::Source.load_string(%(
a = 1
b = 2
c = 3
d = {
one: 1,
two: 2,
three: 3
}
))
expect(source.folding_ranges).to be_one
expect(source.folding_ranges.first.start.line).to eq(4)
end
it "returns unsynchronized sources for started synchronizations" do
source1 = Solargraph::Source.load_string('x = 1', 'test.rb')
source2 = source1.start_synchronize Solargraph::Source::Updater.new(
'test.rb',
2,
[
Solargraph::Source::Change.new(
Solargraph::Range.from_to(0, 5, 0, 5),
'2'
)
]
)
expect(source2.code).to eq('x = 12')
expect(source2).not_to be_synchronized
end
it "finishes synchronizations for unbalanced lines" do
source1 = Solargraph::Source.load_string('x = 1', 'test.rb')
source2 = source1.start_synchronize Solargraph::Source::Updater.new(
'test.rb',
2,
[
Solargraph::Source::Change.new(
Solargraph::Range.from_to(0, 5, 0, 5),
"\n2"
)
]
)
expect(source2.code).to eq("x = 1\n2")
expect(source2).to be_synchronized
end
it "handles comment arrays that overlap lines" do
# Fixes negative argument error (castwide/solargraph#141)
source = Solargraph::Source.load_string(%(
=begin
=end
y = 1 #foo
))
node = source.node_at(3, 0)
expect {
source.comments_for(node)
}.not_to raise_error
end
it "formats comments with multiple hash prefixes" do
source = Solargraph::Source.load_string(%(
##
# one
# two
class Foo; end
))
node = source.node_at(4, 7)
comments = source.comments_for(node)
expect(comments.lines.map(&:chomp)).to eq(['one', 'two'])
end
it 'does not include inner comments' do
source = Solargraph::Source.load_string(%(
# included
class Foo
# ignored
end
))
node = source.node_at(2, 6)
comments = source.comments_for(node)
expect(comments).to include('included')
expect(comments).not_to include('ignored')
end
it 'handles long squiggly heredocs' do
source = Solargraph::Source.load('spec/fixtures/long_squiggly_heredoc.rb')
expect(source.string_ranges).not_to be_empty
end
it 'handles string array substitutions' do
source = Solargraph::Source.load_string(
'%W[array of words #{\'with a substitution\'}]'
)
expect(source.string_ranges.length).to eq(4)
end
end