Class Spec::Runner::SpecParser
In: lib/spec/runner/spec_parser.rb
Parent: Object

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

Methods

Public Instance methods

[Source]

    # File lib/spec/runner/spec_parser.rb, line 5
 5:       def spec_name_for(io, line_number)
 6:         source  = io.read
 7:         behaviour, behaviour_line = behaviour_at_line(source, line_number)
 8:         example, example_line = example_at_line(source, line_number)
 9:         if behaviour && example && (behaviour_line < example_line)
10:           "#{behaviour} #{example}"
11:         elsif behaviour
12:           behaviour
13:         else
14:           nil
15:         end
16:       end

Protected Instance methods

[Source]

    # File lib/spec/runner/spec_parser.rb, line 20
20:       def behaviour_at_line(source, line_number)
21:         find_above(source, line_number, /^\s*(context|describe)\s+(.*)\s+do/)
22:       end

[Source]

    # File lib/spec/runner/spec_parser.rb, line 24
24:       def example_at_line(source, line_number)
25:         find_above(source, line_number, /^\s*(specify|it)\s+(.*)\s+do/)
26:       end

Returns the context/describe or specify/it name and the line number

[Source]

    # File lib/spec/runner/spec_parser.rb, line 29
29:       def find_above(source, line_number, pattern)
30:         lines_above_reversed(source, line_number).each_with_index do |line, n|
31:           return [parse_description($2), line_number-n] if line =~ pattern
32:         end
33:         nil
34:       end

[Source]

    # File lib/spec/runner/spec_parser.rb, line 36
36:       def lines_above_reversed(source, line_number)
37:         lines = source.split("\n")
38:         lines[0...line_number].reverse
39:       end

[Source]

    # File lib/spec/runner/spec_parser.rb, line 41
41:       def parse_description(str)
42:         return str[1..-2] if str =~ /^['"].*['"]$/
43:         if matches = /^(.*)\s*,\s*['"](.*)['"]$/.match(str)
44:           return ::Spec::DSL::Description.generate_description(matches[1], matches[2])
45:         end
46:         return str
47:       end

[Validate]