Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7731c6d

Browse files
committed
More test coverage for indexing
1 parent 1969510 commit 7731c6d

File tree

2 files changed

+54
-24
lines changed

2 files changed

+54
-24
lines changed

lib/syntax_tree/index.rb

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ def index_file(filepath)
171171

172172
private
173173

174+
def location_for(iseq)
175+
code_location = iseq[4][:code_location]
176+
Location.new(code_location[0], code_location[1])
177+
end
178+
174179
def index_iseq(iseq, file_comments)
175180
results = []
176181
queue = [[iseq, []]]
@@ -192,19 +197,15 @@ def index_iseq(iseq, file_comments)
192197
"singleton class with non-self receiver"
193198
end
194199
elsif flags & VM_DEFINECLASS_TYPE_MODULE > 0
195-
code_location = class_iseq[4][:code_location]
196-
location = Location.new(code_location[0], code_location[1])
197-
200+
location = location_for(class_iseq)
198201
results << ModuleDefinition.new(
199202
current_nesting,
200203
name,
201204
location,
202205
EntryComments.new(file_comments, location)
203206
)
204207
else
205-
code_location = class_iseq[4][:code_location]
206-
location = Location.new(code_location[0], code_location[1])
207-
208+
location = location_for(class_iseq)
208209
results << ClassDefinition.new(
209210
current_nesting,
210211
name,
@@ -215,25 +216,23 @@ def index_iseq(iseq, file_comments)
215216

216217
queue << [class_iseq, current_nesting + [name]]
217218
when :definemethod
218-
_, name, method_iseq = insn
219-
220-
code_location = method_iseq[4][:code_location]
221-
location = Location.new(code_location[0], code_location[1])
222-
results << SingletonMethodDefinition.new(
219+
location = location_for(insn[2])
220+
results << MethodDefinition.new(
223221
current_nesting,
224-
name,
222+
insn[1],
225223
location,
226224
EntryComments.new(file_comments, location)
227225
)
228226
when :definesmethod
229-
_, name, method_iseq = insn
230-
231-
code_location = method_iseq[4][:code_location]
232-
location = Location.new(code_location[0], code_location[1])
227+
if current_iseq[13][index - 1] != [:putself]
228+
raise NotImplementedError,
229+
"singleton method with non-self receiver"
230+
end
233231

234-
results << MethodDefinition.new(
232+
location = location_for(insn[2])
233+
results << SingletonMethodDefinition.new(
235234
current_nesting,
236-
name,
235+
insn[1],
237236
location,
238237
EntryComments.new(file_comments, location)
239238
)
@@ -363,13 +362,13 @@ def index_file(filepath)
363362
defined?(RubyVM::InstructionSequence) ? ISeqBackend : ParserBackend
364363

365364
# This method accepts source code and then indexes it.
366-
def self.index(source)
367-
INDEX_BACKEND.new.index(source)
365+
def self.index(source, backend: INDEX_BACKEND.new)
366+
backend.index(source)
368367
end
369368

370369
# This method accepts a filepath and then indexes it.
371-
def self.index_file(filepath)
372-
INDEX_BACKEND.new.index_file(filepath)
370+
def self.index_file(filepath, backend: INDEX_BACKEND.new)
371+
backend.index_file(filepath)
373372
end
374373
end
375374
end

test/index_test.rb

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,44 @@ def test_method_comments
6767
end
6868
end
6969

70+
def test_singleton_method
71+
index_each("def self.foo; end") do |entry|
72+
assert_equal :foo, entry.name
73+
assert_empty entry.nesting
74+
end
75+
end
76+
77+
def test_singleton_method_nested
78+
index_each("class Foo; def self.foo; end; end") do |entry|
79+
assert_equal :foo, entry.name
80+
assert_equal [:Foo], entry.nesting
81+
end
82+
end
83+
84+
def test_singleton_method_comments
85+
index_each("# comment1\n# comment2\ndef self.foo; end") do |entry|
86+
assert_equal :foo, entry.name
87+
assert_equal ["# comment1", "# comment2"], entry.comments.to_a
88+
end
89+
end
90+
91+
def test_this_file
92+
entries = Index.index_file(__FILE__, backend: Index::ParserBackend.new)
93+
94+
if defined?(RubyVM::InstructionSequence)
95+
entries += Index.index_file(__FILE__, backend: Index::ISeqBackend.new)
96+
end
97+
98+
entries.map { |entry| entry.comments.to_a }
99+
end
100+
70101
private
71102

72103
def index_each(source)
73-
yield SyntaxTree::Index::ParserBackend.new.index(source).last
104+
yield Index.index(source, backend: Index::ParserBackend.new).last
74105

75106
if defined?(RubyVM::InstructionSequence)
76-
yield SyntaxTree::Index::ISeqBackend.new.index(source).last
107+
yield Index.index(source, backend: Index::ISeqBackend.new).last
77108
end
78109
end
79110
end

0 commit comments

Comments
 (0)