File tree Expand file tree Collapse file tree 2 files changed +57
-2
lines changed Expand file tree Collapse file tree 2 files changed +57
-2
lines changed Original file line number Diff line number Diff line change 1
1
# frozen_string_literal: true
2
2
3
+ begin
4
+ require "prism"
5
+ rescue LoadError
6
+ # If Prism isn't available (because of using an older Ruby version) then we'll
7
+ # define a fallback parser using ripper.
8
+ end
9
+
10
+ if defined? ( Prism )
11
+ module Rails
12
+ module TestUnit
13
+ # Parse a test file to extract the line ranges of all tests in both
14
+ # method-style (def test_foo) and declarative-style (test "foo" do)
15
+ class TestParser < Prism ::Visitor
16
+ # Helper to translate a method object into the path and line range where
17
+ # the method was defined.
18
+ def self . definition_for ( method )
19
+ filepath , start_line = method . source_location
20
+ Prism . parse_file ( filepath ) . value . accept ( new ( ranges = { } ) )
21
+ ( end_line = ranges [ start_line ] ) && [ filepath , ( start_line ..end_line ) ]
22
+ end
23
+
24
+ def self . definitions_for ( source , filepath )
25
+ Prism . parse ( source , filepath : filepath ) . value . accept ( new ( ranges = { } ) )
26
+ ranges
27
+ end
28
+
29
+ attr_reader :ranges
30
+
31
+ def initialize ( ranges )
32
+ @ranges = ranges
33
+ end
34
+
35
+ def visit_def_node ( node )
36
+ if node . name . start_with? ( "test" )
37
+ ranges [ node . location . start_line ] = node . location . end_line
38
+ end
39
+ super
40
+ end
41
+
42
+ def visit_call_node ( node )
43
+ if node . name == :test
44
+ ranges [ node . location . start_line ] = node . location . end_line
45
+ end
46
+ super
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ # If we have Prism, then we don't need to define the fallback parser using
53
+ # ripper.
54
+ return
55
+ end
56
+
3
57
require "ripper"
4
58
5
59
module Rails
Original file line number Diff line number Diff line change 1
1
# frozen_string_literal: true
2
2
3
+ require "active_support/deprecator"
3
4
require "active_support/test_case"
4
5
require "active_support/testing/autorun"
5
6
require "rails/test_unit/test_parser"
@@ -42,7 +43,7 @@ def test_oneline; assert true; end
42
43
end
43
44
RUBY
44
45
45
- parser = Rails ::TestUnit ::TestParser . new ( example_test , "example_test.rb" )
46
+ actual_map = Rails ::TestUnit ::TestParser . definitions_for ( example_test , "example_test.rb" )
46
47
expected_map = {
47
48
4 => 8 , # test_method
48
49
10 => 10 , # test_oneline
@@ -53,6 +54,6 @@ def test_oneline; assert true; end
53
54
27 => 27 , # declarative oneilne do
54
55
29 => 32 # declarative multiline w/braces
55
56
}
56
- assert_equal expected_map , parser . parse
57
+ assert_equal expected_map , actual_map
57
58
end
58
59
end
You can’t perform that action at this time.
0 commit comments