Sha256: 6cb8c7240dd37c55245d7ad5b74978b23dd552ce052e70d547b45bf2bcc9b551

Contents?: true

Size: 1.53 KB

Versions: 4

Compression:

Stored size: 1.53 KB

Contents

module Spec
  module Runner
    # Parses a spec file and finds the nearest example for a given line number.
    class SpecParser
      def spec_name_for(io, line_number)
        source  = io.read
        behaviour, behaviour_line = behaviour_at_line(source, line_number)
        example, example_line = example_at_line(source, line_number)
        if behaviour && example && (behaviour_line < example_line)
          "#{behaviour} #{example}"
        elsif behaviour
          behaviour
        else
          nil
        end
      end

    protected

      def behaviour_at_line(source, line_number)
        find_above(source, line_number, /^\s*(context|describe)\s+(.*)\s+do/)
      end

      def example_at_line(source, line_number)
        find_above(source, line_number, /^\s*(specify|it)\s+(.*)\s+do/)
      end

      # Returns the context/describe or specify/it name and the line number
      def find_above(source, line_number, pattern)
        lines_above_reversed(source, line_number).each_with_index do |line, n|
          return [parse_description($2), line_number-n] if line =~ pattern
        end
        nil
      end

      def lines_above_reversed(source, line_number)
        lines = source.split("\n")
      	lines[0...line_number].reverse
      end
      
      def parse_description(str)
        return str[1..-2] if str =~ /^['"].*['"]$/
        if matches = /^(.*)\s*,\s*['"](.*)['"]$/.match(str)
          return ::Spec::DSL::Description.generate_description(matches[1], matches[2])
        end
        return str
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
has_finder-0.1.1 spec/rails/vendor/plugins/rspec/lib/spec/runner/spec_parser.rb
has_finder-0.1.2 spec/rails/vendor/plugins/rspec/lib/spec/runner/spec_parser.rb
has_finder-0.1.3 spec/rails/vendor/plugins/rspec/lib/spec/runner/spec_parser.rb
rspec-1.0.5 lib/spec/runner/spec_parser.rb