Skip to content

Commit

Permalink
Scan method body for file
Browse files Browse the repository at this point in the history
  • Loading branch information
kbaum committed Dec 13, 2020
1 parent 6ece3fd commit 44e80ae
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions test/coverband/utils/method_definition_test.rb
@@ -0,0 +1,49 @@
# frozen_string_literal: true

require File.expand_path('../../test_helper', File.dirname(__FILE__))

module Coverband
module Utils
class MethodDefinition
attr_reader :path
def initialize(path)
@path = path
end

def scan
scan_node(RubyVM::AbstractSyntaxTree.parse_file(path))
end

def self.scan(path)
MethodDefinition.new(path).scan
end

private

def scan_node(node)
return [] unless node.is_a?(RubyVM::AbstractSyntaxTree::Node)
definitions = []
if node.type == :DEFN
definitions <<
OpenStruct.new(
first_line_number: node.first_lineno,
last_line_number: node.last_lineno
)
end
definitions +
node.children.flatten.compact.map { |child| scan_node(child) }.flatten
end
end

class MethodDefinitionTest < Minitest::Test
def test_scan
method_definitions = MethodDefinition.scan('./test/dog.rb')
assert(method_definitions)
assert_equal(1, method_definitions.length)
method_definition = method_definitions.first # assert_equal(4, method.first_line)
assert_equal(4, method_definition.first_line_number)
assert_equal(6, method_definition.last_line_number)
end
end
end
end

0 comments on commit 44e80ae

Please sign in to comment.