Skip to content

More test coverage for indexing #266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 21 additions & 22 deletions lib/syntax_tree/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ def index_file(filepath)

private

def location_for(iseq)
code_location = iseq[4][:code_location]
Location.new(code_location[0], code_location[1])
end

def index_iseq(iseq, file_comments)
results = []
queue = [[iseq, []]]
Expand All @@ -192,19 +197,15 @@ def index_iseq(iseq, file_comments)
"singleton class with non-self receiver"
end
elsif flags & VM_DEFINECLASS_TYPE_MODULE > 0
code_location = class_iseq[4][:code_location]
location = Location.new(code_location[0], code_location[1])

location = location_for(class_iseq)
results << ModuleDefinition.new(
current_nesting,
name,
location,
EntryComments.new(file_comments, location)
)
else
code_location = class_iseq[4][:code_location]
location = Location.new(code_location[0], code_location[1])

location = location_for(class_iseq)
results << ClassDefinition.new(
current_nesting,
name,
Expand All @@ -215,25 +216,23 @@ def index_iseq(iseq, file_comments)

queue << [class_iseq, current_nesting + [name]]
when :definemethod
_, name, method_iseq = insn

code_location = method_iseq[4][:code_location]
location = Location.new(code_location[0], code_location[1])
results << SingletonMethodDefinition.new(
location = location_for(insn[2])
results << MethodDefinition.new(
current_nesting,
name,
insn[1],
location,
EntryComments.new(file_comments, location)
)
when :definesmethod
_, name, method_iseq = insn

code_location = method_iseq[4][:code_location]
location = Location.new(code_location[0], code_location[1])
if current_iseq[13][index - 1] != [:putself]
raise NotImplementedError,
"singleton method with non-self receiver"
end

results << MethodDefinition.new(
location = location_for(insn[2])
results << SingletonMethodDefinition.new(
current_nesting,
name,
insn[1],
location,
EntryComments.new(file_comments, location)
)
Expand Down Expand Up @@ -363,13 +362,13 @@ def index_file(filepath)
defined?(RubyVM::InstructionSequence) ? ISeqBackend : ParserBackend

# This method accepts source code and then indexes it.
def self.index(source)
INDEX_BACKEND.new.index(source)
def self.index(source, backend: INDEX_BACKEND.new)
backend.index(source)
end

# This method accepts a filepath and then indexes it.
def self.index_file(filepath)
INDEX_BACKEND.new.index_file(filepath)
def self.index_file(filepath, backend: INDEX_BACKEND.new)
backend.index_file(filepath)
end
end
end
35 changes: 33 additions & 2 deletions test/index_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,44 @@ def test_method_comments
end
end

def test_singleton_method
index_each("def self.foo; end") do |entry|
assert_equal :foo, entry.name
assert_empty entry.nesting
end
end

def test_singleton_method_nested
index_each("class Foo; def self.foo; end; end") do |entry|
assert_equal :foo, entry.name
assert_equal [:Foo], entry.nesting
end
end

def test_singleton_method_comments
index_each("# comment1\n# comment2\ndef self.foo; end") do |entry|
assert_equal :foo, entry.name
assert_equal ["# comment1", "# comment2"], entry.comments.to_a
end
end

def test_this_file
entries = Index.index_file(__FILE__, backend: Index::ParserBackend.new)

if defined?(RubyVM::InstructionSequence)
entries += Index.index_file(__FILE__, backend: Index::ISeqBackend.new)
end

entries.map { |entry| entry.comments.to_a }
end

private

def index_each(source)
yield SyntaxTree::Index::ParserBackend.new.index(source).last
yield Index.index(source, backend: Index::ParserBackend.new).last

if defined?(RubyVM::InstructionSequence)
yield SyntaxTree::Index::ISeqBackend.new.index(source).last
yield Index.index(source, backend: Index::ISeqBackend.new).last
end
end
end
Expand Down