Class: Spec::Runner::SpecParser

Inherits:
Object
  • Object
show all
Defined in:
lib/spec/runner/spec_parser.rb

Overview

Parses a spec file and finds the nearest example for a given line number.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(run_options) ⇒ SpecParser

Returns a new instance of SpecParser.



7
8
9
10
# File 'lib/spec/runner/spec_parser.rb', line 7

def initialize(run_options)
  @best_match = {}
  @run_options = run_options
end

Instance Attribute Details

#best_matchObject (readonly)

Returns the value of attribute best_match.



5
6
7
# File 'lib/spec/runner/spec_parser.rb', line 5

def best_match
  @best_match
end

Instance Method Details

#spec_name_for(file, line_number) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/spec/runner/spec_parser.rb', line 12

def spec_name_for(file, line_number)
  best_match.clear
  file = File.expand_path(file)
  @run_options.example_groups.each do |example_group|
    consider_example_group_for_best_match example_group, file, line_number

    example_group.examples.each do |example|
      consider_example_for_best_match example, example_group, file, line_number
    end
  end
  if best_match[:example_group]
    if best_match[:example]
      "#{best_match[:example_group].description} #{best_match[:example].description}"
    else
      best_match[:example_group].description
    end
  else
    nil
  end
end