3 # Parses a spec file and finds the nearest example for a given line number.
5 def spec_name_for(io, line_number)
7 example_group, example_group_line = example_group_at_line(source, line_number)
8 example, example_line = example_at_line(source, line_number)
9 if example_group && example && (example_group_line < example_line)
10 "#{example_group} #{example}"
20 def example_group_at_line(source, line_number)
21 find_above(source, line_number, /^\s*(context|describe)\s+(.*)\s+do/)
24 def example_at_line(source, line_number)
25 find_above(source, line_number, /^\s*(specify|it)\s+(.*)\s+do/)
28 # Returns the context/describe or specify/it name and the line number
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
36 def lines_above_reversed(source, line_number)
37 lines = source.split("\n")
38 lines[0...line_number].reverse
41 def parse_description(str)
42 return str[1..-2] if str =~ /^['"].*['"]$/
43 if matches = /^['"](.*)['"](,.*)?$/.match(str)
44 return ::Spec::Example::ExampleGroupMethods.description_text(matches[1])
46 if matches = /^(.*)\s*,\s*['"](.*)['"](,.*)?$/.match(str)
47 return ::Spec::Example::ExampleGroupMethods.description_text(matches[1], matches[2])