From 7731c6d6721b3a733c6cd9fbf7725d3b3f257427 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Wed, 25 Jan 2023 20:16:58 -0500 Subject: [PATCH] More test coverage for indexing --- lib/syntax_tree/index.rb | 43 ++++++++++++++++++++-------------------- test/index_test.rb | 35 ++++++++++++++++++++++++++++++-- 2 files changed, 54 insertions(+), 24 deletions(-) diff --git a/lib/syntax_tree/index.rb b/lib/syntax_tree/index.rb index 6956ae9c..8b33f785 100644 --- a/lib/syntax_tree/index.rb +++ b/lib/syntax_tree/index.rb @@ -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, []]] @@ -192,9 +197,7 @@ 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, @@ -202,9 +205,7 @@ def index_iseq(iseq, file_comments) 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, @@ -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) ) @@ -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 diff --git a/test/index_test.rb b/test/index_test.rb index 91dfcc76..6bb83881 100644 --- a/test/index_test.rb +++ b/test/index_test.rb @@ -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