Class: Nicetest::TestFinder::RangeFinder

Inherits:
Prism::Compiler
  • Object
show all
Defined in:
lib/nicetest/test_finder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ RangeFinder

Returns a new instance of RangeFinder.



43
44
45
46
47
48
49
50
# File 'lib/nicetest/test_finder.rb', line 43

def initialize(filename)
  super()
  @namespace_stack = []
  @class_scopes = []
  @tests = []
  @program_nodes = []
  @scopes = []
end

Instance Attribute Details

#scopesObject (readonly)

Returns the value of attribute scopes.



41
42
43
# File 'lib/nicetest/test_finder.rb', line 41

def scopes
  @scopes
end

Class Method Details

.parse_file(filename) ⇒ Object



35
36
37
38
# File 'lib/nicetest/test_finder.rb', line 35

def parse_file(filename)
  node = Prism.parse_file(filename)
  new(filename).populate(node.value)
end

Instance Method Details

#nearest_test_for_line(lineno) ⇒ Object



59
60
61
62
63
64
# File 'lib/nicetest/test_finder.rb', line 59

def nearest_test_for_line(lineno)
  found = @scopes.find do |range, _test_name|
    range.cover?(lineno)
  end
  found&.last
end

#populate(node) ⇒ Object



52
53
54
55
56
57
# File 'lib/nicetest/test_finder.rb', line 52

def populate(node)
  visit(node)
  @scopes += @tests
  @scopes += @class_scopes
  self
end

#test_id_for(test_name) ⇒ Object



104
105
106
# File 'lib/nicetest/test_finder.rb', line 104

def test_id_for(test_name)
  "#{@namespace_stack.map(&:name).join("::")}##{test_name}"
end

#test_range_for(location) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/nicetest/test_finder.rb', line 108

def test_range_for(location)
  if @tests.empty?
    current_nesting = @namespace_stack.map(&:name).join("::")
    @class_scopes << [0..(location.start_line - 1), current_nesting]
  end

  last_scope = @tests.last || @class_scopes.last
  last_scope_start = last_scope[0].begin
  last_scope[0] = last_scope_start..(location.start_line - 1)

  location.start_line..location.end_line
end

#visit_call_node(node) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/nicetest/test_finder.rb', line 96

def visit_call_node(node)
  return super unless node.name == :test

  test_name = "test_#{node.arguments.arguments.first.unescaped}"
  test_name.gsub!(/\s+/, "_")
  @tests << [test_range_for(node.location), test_id_for(test_name)]
end

#visit_class_node(node) ⇒ Object



83
84
85
86
87
# File 'lib/nicetest/test_finder.rb', line 83

def visit_class_node(node)
  @namespace_stack.push(node)
  super
  @namespace_stack.pop
end

#visit_def_node(node) ⇒ Object



89
90
91
92
93
94
# File 'lib/nicetest/test_finder.rb', line 89

def visit_def_node(node)
  node_name = node.name.to_s
  return super unless node_name.start_with?("test_")

  @tests << [test_range_for(node.location), test_id_for(node_name)]
end

#visit_module_node(node) ⇒ Object



77
78
79
80
81
# File 'lib/nicetest/test_finder.rb', line 77

def visit_module_node(node)
  @namespace_stack.push(node)
  super
  @namespace_stack.pop
end

#visit_program_node(node) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/nicetest/test_finder.rb', line 66

def visit_program_node(node)
  @program_nodes << node
  ret = super

  if (last_scope = @class_scopes.last)
    @class_scopes << [last_scope[0].end..node.location.end_line, last_scope[1]]
  end

  ret
end