class Rails::TestUnit::TestParser

Parse a test file to extract the line ranges of all tests in both method-style (def test_foo) and declarative-style (test “foo” do)

Public Class Methods

Helper to translate a method object into the path and line range where the method was defined.

# File railties/lib/rails/test_unit/test_parser.rb, line 18
def self.definition_for(method)
  filepath, start_line = method.source_location
  queue = [Prism.parse_file(filepath).value]

  while (node = queue.shift)
    case node.type
    when :def_node
      if node.location.start_line == start_line
        return [filepath, start_line..node.location.end_line]
      end
    when :call_node
      if node.location.start_line == start_line
        return [filepath, start_line..node.location.end_line]
      end
    end

    queue.concat(node.compact_child_nodes)
  end

  nil
end