Class: RspecTree::Visitor

Inherits:
Prism::Visitor
  • Object
show all
Defined in:
lib/rspec_tree/visitor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeVisitor

Returns a new instance of Visitor.



14
15
16
17
# File 'lib/rspec_tree/visitor.rb', line 14

def initialize
  @root = RspecTree::Root.new
  super
end

Instance Attribute Details

#rootObject

Returns the value of attribute root.



12
13
14
# File 'lib/rspec_tree/visitor.rb', line 12

def root
  @root
end

Instance Method Details

#block_node?(node) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/rspec_tree/visitor.rb', line 23

def block_node?(node)
  node.is_a?(Prism::BlockNode)
end

#call_node?(node) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/rspec_tree/visitor.rb', line 19

def call_node?(node)
  node.is_a?(Prism::CallNode)
end

#call_nodes_in_block(node) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rspec_tree/visitor.rb', line 80

def call_nodes_in_block(node)
  call_nodes = []
  node.compact_child_nodes.each do |child_node|
    next unless block_node?(child_node)

    call_nodes = child_node.compact_child_nodes.first.compact_child_nodes.select do |grand_child_node|
      call_node?(grand_child_node)
    end
  end
  call_nodes
end

#context_node?(node) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/rspec_tree/visitor.rb', line 35

def context_node?(node)
  node.name == :context
end

#describe_node?(node) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/rspec_tree/visitor.rb', line 31

def describe_node?(node)
  node.name == :describe
end

#example_node?(node) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/rspec_tree/visitor.rb', line 39

def example_node?(node)
  node.name == :it || node.name == :specify || node.name == :example
end

#handle_context_node(node, parent) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rspec_tree/visitor.rb', line 63

def handle_context_node(node, parent)
  parent.title = node.arguments.arguments.first.unescaped
  call_nodes_in_block(node).each do |child_node|
    if context_node?(child_node)
      parent.contexts << RspecTree::Context.new
      handle_context_node(child_node, parent.contexts.last)
    elsif example_node?(child_node)
      parent.examples << RspecTree::Example.new
      handle_example_node(child_node, parent.examples.last)
    end
  end
end

#handle_describe_node(node, parent) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/rspec_tree/visitor.rb', line 53

def handle_describe_node(node, parent)
  parent.title = node.arguments.arguments.first.unescaped
  call_nodes_in_block(node).each do |child_node|
    if context_node?(child_node)
      parent.contexts << RspecTree::Context.new
      handle_context_node(child_node, parent.contexts.last)
    end
  end
end

#handle_example_node(node, parent) ⇒ Object



76
77
78
# File 'lib/rspec_tree/visitor.rb', line 76

def handle_example_node(node, parent)
  parent.title = node.arguments.arguments.first.unescaped
end

#handle_root_describe_node(node) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/rspec_tree/visitor.rb', line 43

def handle_root_describe_node(node)
  @root.title = node.arguments.arguments.first.name
  call_nodes_in_block(node).each do |child_node|
    if describe_node?(child_node)
      @root.descriptions << RspecTree::Description.new
      handle_describe_node(child_node, @root.descriptions.last)
    end
  end
end

#root_describe_node?(node) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/rspec_tree/visitor.rb', line 27

def root_describe_node?(node)
  node&.name == :describe && node.receiver
end

#visit_call_node(node) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rspec_tree/visitor.rb', line 92

def visit_call_node(node)
  if root_describe_node?(node)
    handle_root_describe_node(node)
  elsif describe_node?(node)
    handle_describe_node(node)
  elsif context_node?(node)
    handle_context_node(node)
  elsif example_node?(node)
    handle_example_node(node)
  end
end