Class: Rails::TestUnit::TestParser
- Defined in:
- railties/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)
Class Method Summary collapse
-
.definition_for(method_obj) ⇒ Object
Helper to translate a method object into the path and line range where the method was defined.
Instance Method Summary collapse
- #first_arg(arg) ⇒ Object (also: #on_method_add_arg, #on_command, #on_stmts_add, #on_arg_paren, #on_bodystmt)
-
#initialize ⇒ TestParser
constructor
A new instance of TestParser.
- #just_lineno ⇒ Object (also: #on_ident, #on_do_block, #on_stmts_new, #on_brace_block)
- #on_args_add(parts, part) ⇒ Object
- #on_args_add_block(args, *rest) ⇒ Object
- #on_args_new ⇒ Object
- #on_command_call(begin_lineno, _args) ⇒ Object
-
#on_def(begin_line) ⇒ Object
method test e.g.
-
#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.
- #parse ⇒ Object
Constructor Details
#initialize ⇒ TestParser
Returns a new instance of TestParser.
19 20 21 22 23 |
# File 'railties/lib/rails/test_unit/test_parser.rb', line 19 def initialize(*) # A hash mapping the 1-indexed line numbers that tests start on to where they end. @begins_to_ends = {} super end |
Class Method Details
.definition_for(method_obj) ⇒ Object
Helper to translate a method object into the path and line range where the method was defined.
12 13 14 15 16 17 |
# File 'railties/lib/rails/test_unit/test_parser.rb', line 12 def self.definition_for(method_obj) path, begin_line = method_obj.source_location begins_to_ends = new(File.read(path), path).parse return unless end_line = begins_to_ends[begin_line] [path, (begin_line..end_line)] 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
56 57 58 |
# File 'railties/lib/rails/test_unit/test_parser.rb', line 56 def first_arg(arg, *) arg end |
#just_lineno ⇒ Object Also known as: on_ident, on_do_block, on_stmts_new, on_brace_block
60 61 62 |
# File 'railties/lib/rails/test_unit/test_parser.rb', line 60 def just_lineno(*) lineno end |
#on_args_add(parts, part) ⇒ Object
79 80 81 |
# File 'railties/lib/rails/test_unit/test_parser.rb', line 79 def on_args_add(parts, part) parts << part end |
#on_args_add_block(args, *rest) ⇒ Object
83 84 85 |
# File 'railties/lib/rails/test_unit/test_parser.rb', line 83 def on_args_add_block(args, *rest) args.first end |
#on_args_new ⇒ Object
75 76 77 |
# File 'railties/lib/rails/test_unit/test_parser.rb', line 75 def on_args_new [] end |
#on_command_call(begin_lineno, _args) ⇒ Object
52 53 54 |
# File 'railties/lib/rails/test_unit/test_parser.rb', line 52 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.
34 35 36 |
# File 'railties/lib/rails/test_unit/test_parser.rb', line 34 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.
46 47 48 49 50 |
# File 'railties/lib/rails/test_unit/test_parser.rb', line 46 def on_method_add_block(begin_line, end_line) if begin_line && end_line @begins_to_ends[begin_line] = end_line end end |
#parse ⇒ Object
25 26 27 28 |
# File 'railties/lib/rails/test_unit/test_parser.rb', line 25 def parse super @begins_to_ends end |