summaryrefslogtreecommitdiffstats
path: root/test/lib/spec/runner/spec_parser.rb
blob: 2cb8518fc56a6d9dbc7d70999d45940dc1ef98f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
module Spec
  module Runner
    # Parses a spec file and finds the nearest spec for a given line number.
    class SpecParser
      def spec_name_for(io, line_number)
        source  = io.read
        context = context_at_line(source, line_number)
        spec    = spec_at_line(source, line_number)
        if context && spec
          "#{context} #{spec}"
        elsif context
          context
        else
          nil
        end
      end

    protected

      def context_at_line(source, line_number)
        find_above(source, line_number, /^\s*context\s+['|"](.*)['|"]/)
      end

      def spec_at_line(source, line_number)
        find_above(source, line_number, /^\s*specify\s+['|"](.*)['|"]/)
      end

      def find_above(source, line_number, pattern)
        lines_above_reversed(source, line_number).each do |line| 
          return $1 if line =~ pattern
        end
        nil
      end

      def lines_above_reversed(source, line_number)
        lines = source.split("\n")
      	lines[0...line_number].reverse
      end
    end
  end
end