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.



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

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

Instance Attribute Details

#scopesObject (readonly)

Returns the value of attribute scopes.



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

def scopes
  @scopes
end

Class Method Details

.parse_file(filename) ⇒ Object



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

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

Instance Method Details

#nearest_test_for_line(lineno) ⇒ Object



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

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

#populate(node) ⇒ Object



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

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

#test_id_for(test_name) ⇒ Object



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

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

#test_range_for(location) ⇒ Object



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

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



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

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



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

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

#visit_def_node(node) ⇒ Object



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

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



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

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

#visit_program_node(node) ⇒ Object



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

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