Sha256: c125a4a4a0e5cce382faafc91bbebe74ec4ad47374014d32a38fa0bf940c2fb0

Contents?: true

Size: 1.04 KB

Versions: 8

Compression:

Stored size: 1.04 KB

Contents

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

Version data entries

8 entries across 8 versions & 3 rubygems

Version Path
puppet-0.22.4 test/lib/spec/runner/spec_parser.rb
puppet-0.23.0 test/lib/spec/runner/spec_parser.rb
puppet-0.23.1 test/lib/spec/runner/spec_parser.rb
puppet-0.23.2 test/lib/spec/runner/spec_parser.rb
riess-0.0.8 vendor/rspec-0.8.2/lib/spec/runner/spec_parser.rb
rspec-0.8.0 lib/spec/runner/spec_parser.rb
rspec-0.8.1 lib/spec/runner/spec_parser.rb
rspec-0.8.2 lib/spec/runner/spec_parser.rb