Class: Cyperful::TestParser

Inherits:
Object
  • Object
show all
Defined in:
lib/cyperful/test_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(test_class) ⇒ TestParser

Returns a new instance of TestParser.



4
5
6
7
# File 'lib/cyperful/test_parser.rb', line 4

def initialize(test_class)
  @test_class = test_class
  @source_filepath = Object.const_source_location(test_class.name).first
end

Instance Method Details

#steps_per_testObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cyperful/test_parser.rb', line 9

def steps_per_test
  ast = Parser::CurrentRuby.parse(File.read(@source_filepath))

  test_class_name = @test_class.name.to_sym

  system_test_class =
    ast.children.find do |node|
      if node.type == :class
        node.children.find do |c|
          c.type == :const && c.children[1] == test_class_name
        end
      end
    end
  unless system_test_class
    raise "Could not find class #{test_class.name} in #{@source_filepath}"
  end

  (
      # the children of the `class` node are either:
      # - a `begin` node if there's more than 1 child node
      # - or the one 0 or 1 child node
      system_test_class
        .children
        .find { |node| node.type == :begin }
        &.children || [system_test_class.children[2]].compact
    )
    .map do |node|
      # e.g. `test "my test" do ... end`
      if node.type == :block && node.children[0].type == :send &&
           node.children[0].children[1] == :test
        test_string = node.children[0].children[2].children[0]

        # https://github.com/rails/rails/blob/66676ce499a32e4c62220bd05f8ee2cdf0e15f0c/activesupport/lib/active_support/testing/declarative.rb#L14C23-L14C61
        test_method = "test_#{test_string.gsub(/\s+/, "_")}".to_sym

        block_node = node.children[2]
        [test_method, block_node]
      else
        # e.g. `def test_my_test; ... end`
        # TODO
      end
    end
    .compact
    .to_h do |test_method, block_node|
      [
        test_method,
        find_test_steps(block_node)
          # sanity check:
          .uniq { |step| step[:line] },
      ]
    end
end