Class: Rails::TestUnit::TestParser

Inherits:
Ripper
  • Object
show all
Defined in:
lib/rails/test_unit/test_parser.rb

Overview

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)

Instance Method Summary collapse

Constructor Details

#initializeTestParser

Returns a new instance of TestParser.



64
65
66
67
68
# File 'lib/rails/test_unit/test_parser.rb', line 64

def initialize(*)
  # A hash mapping the 1-indexed line numbers that tests start on to where they end.
  @begins_to_ends = {}
  super
end

Instance Method Details

#first_arg(arg) ⇒ Object Also known as: on_method_add_arg, on_command, on_stmts_add, on_arg_paren, on_bodystmt



101
102
103
# File 'lib/rails/test_unit/test_parser.rb', line 101

def first_arg(arg, *)
  arg
end

#just_linenoObject Also known as: on_ident, on_do_block, on_stmts_new, on_brace_block



105
106
107
# File 'lib/rails/test_unit/test_parser.rb', line 105

def just_lineno(*)
  lineno
end

#on_args_add(parts, part) ⇒ Object



124
125
126
# File 'lib/rails/test_unit/test_parser.rb', line 124

def on_args_add(parts, part)
  parts << part
end

#on_args_add_block(args, *rest) ⇒ Object



128
129
130
# File 'lib/rails/test_unit/test_parser.rb', line 128

def on_args_add_block(args, *rest)
  args.first
end

#on_args_newObject



120
121
122
# File 'lib/rails/test_unit/test_parser.rb', line 120

def on_args_new
  []
end

#on_command_call(begin_lineno, _args) ⇒ Object



97
98
99
# File 'lib/rails/test_unit/test_parser.rb', line 97

def on_command_call(*, begin_lineno, _args)
  begin_lineno
end

#on_def(begin_line) ⇒ Object

method test e.g. ‘def test_some_description` This event’s first argument gets the ‘ident` node containing the method name, which we have overridden to return the line number of the ident instead.



79
80
81
# File 'lib/rails/test_unit/test_parser.rb', line 79

def on_def(begin_line, *)
  @begins_to_ends[begin_line] = lineno
end

#on_method_add_block(begin_line, end_line) ⇒ Object

Everything past this point is to support declarative tests, which require more work to get right because of the many different ways methods can be invoked in ruby, all of which are parsed differently.

The approach is just to store the current line number when the “test” method is called and pass it up the tree so it’s available at the point when we also know the line where the associated block ends.



91
92
93
94
95
# File 'lib/rails/test_unit/test_parser.rb', line 91

def on_method_add_block(begin_line, end_line)
  if begin_line && end_line
    @begins_to_ends[begin_line] = end_line
  end
end

#parseObject



70
71
72
73
# File 'lib/rails/test_unit/test_parser.rb', line 70

def parse
  super
  @begins_to_ends
end