Class Spec::Runner::SpecParser

  1. lib/spec/runner/spec_parser.rb
Parent: Object

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

Attributes

best_match [R]

Public class methods

new (run_options)
[show source]
# File lib/spec/runner/spec_parser.rb, line 7
      def initialize(run_options)
        @best_match = {}
        @run_options = run_options
      end

Public instance methods

spec_name_for (file, line_number)
[show source]
# File lib/spec/runner/spec_parser.rb, line 12
      def spec_name_for(file, line_number)
        best_match.clear
        file = File.expand_path(file)
        @run_options.example_groups.each do |example_group|
          consider_example_group_for_best_match example_group, file, line_number

          example_group.examples.each do |example|
            consider_example_for_best_match example, example_group, file, line_number
          end
        end
        if best_match[:example_group]
          if best_match[:example]
            "#{best_match[:example_group].description} #{best_match[:example].description}"
          else
            best_match[:example_group].description
          end
        else
          nil
        end
      end

Protected instance methods

consider_example_for_best_match (example, example_group, file, line_number)
[show source]
# File lib/spec/runner/spec_parser.rb, line 46
      def consider_example_for_best_match(example, example_group, file, line_number)
        parsed_backtrace = parse_backtrace(example.backtrace)
        parsed_backtrace.each do |example_file, example_line|
          if is_best_match?(file, line_number, example_file, example_line)
            best_match.clear
            best_match[:example_group] = example_group
            best_match[:example] = example
            best_match[:line] = example_line
          end
        end
      end
consider_example_group_for_best_match (example_group, file, line_number)
[show source]
# File lib/spec/runner/spec_parser.rb, line 35
      def consider_example_group_for_best_match(example_group, file, line_number)
        parsed_backtrace = parse_backtrace(example_group.backtrace)
        parsed_backtrace.each do |example_file, example_line|
          if is_best_match?(file, line_number, example_file, example_line)
            best_match.clear
            best_match[:example_group] = example_group
            best_match[:line] = example_line
          end
        end
      end
is_best_match? (file, line_number, example_file, example_line)
[show source]
# File lib/spec/runner/spec_parser.rb, line 58
      def is_best_match?(file, line_number, example_file, example_line)
        file == File.expand_path(example_file) &&
        example_line <= line_number &&
        example_line > best_match[:line].to_i
      end
parse_backtrace (backtrace)
[show source]
# File lib/spec/runner/spec_parser.rb, line 64
      def parse_backtrace(backtrace)
        Array(backtrace).collect do |trace_line|
          trace_line =~ /(.*)\:(\d*)(\:|$)/
          file, number = $1, $2
          [file, Integer(number)]
        end
      end