Sha256: 307c4d1a50a03ce9bb5c812fdcaaceb120586c54325761b923b75e6314b91345

Contents?: true

Size: 1.86 KB

Versions: 43

Compression:

Stored size: 1.86 KB

Contents

module Spec
  module Runner
    module Formatter
      # This class extracts code snippets by looking at the backtrace of the passed error
      class SnippetExtractor #:nodoc:
        class NullConverter; def convert(code, pre); code; end; end #:nodoc:
        begin; require 'syntax/convertors/html'; @@converter = Syntax::Convertors::HTML.for_syntax "ruby"; rescue LoadError => e; @@converter = NullConverter.new; end
        
        def snippet(error)
          raw_code, line = snippet_for(error.backtrace[0])
          highlighted = @@converter.convert(raw_code, false)
          highlighted << "\n<span class=\"comment\"># gem install syntax to get syntax highlighting</span>" if @@converter.is_a?(NullConverter)
          post_process(highlighted, line)
        end
        
        def snippet_for(error_line)
          if error_line =~ /(.*):(\d+)/
            file = $1
            line = $2.to_i
            [lines_around(file, line), line]
          else
            ["# Couldn't get snippet for #{error_line}", 1]
          end
        end
        
        def lines_around(file, line)
          if File.file?(file)
            lines = File.open(file).read.split("\n")
            min = [0, line-3].max
            max = [line+1, lines.length-1].min
            selected_lines = []
            selected_lines.join("\n")
            lines[min..max].join("\n")
          else
            "# Couldn't get snippet for #{file}"
          end
        end
        
        def post_process(highlighted, offending_line)
          new_lines = []
          highlighted.split("\n").each_with_index do |line, i|
            new_line = "<span class=\"linenum\">#{offending_line+i-2}</span>#{line}"
            new_line = "<span class=\"offending\">#{new_line}</span>" if i == 2
            new_lines << new_line
          end
          new_lines.join("\n")
        end
        
      end
    end
  end
end

Version data entries

43 entries across 43 versions & 12 rubygems

Version Path
dchelimsky-rspec-1.1.99.13 lib/spec/runner/formatter/snippet_extractor.rb
dchelimsky-rspec-1.1.99.9 lib/spec/runner/formatter/snippet_extractor.rb
rspec-instructure-1.3.3 lib/spec/runner/formatter/snippet_extractor.rb
radiant-1.0.0 ruby-debug/ruby/1.8/gems/rspec-1.3.2/lib/spec/runner/formatter/snippet_extractor.rb
rspec-1.3.2 lib/spec/runner/formatter/snippet_extractor.rb
rspec-1.3.1 lib/spec/runner/formatter/snippet_extractor.rb
rspec-1.3.1.rc lib/spec/runner/formatter/snippet_extractor.rb
rspec-1.3.0 lib/spec/runner/formatter/snippet_extractor.rb
hubbub-0.0.11 lib/vendor/plugins/rspec/lib/spec/runner/formatter/snippet_extractor.rb
hubbub-0.0.10 lib/vendor/plugins/rspec/lib/spec/runner/formatter/snippet_extractor.rb
hubbub-0.0.9 lib/vendor/plugins/rspec/lib/spec/runner/formatter/snippet_extractor.rb
hubbub-0.0.8 lib/vendor/plugins/rspec/lib/spec/runner/formatter/snippet_extractor.rb
hubbub-0.0.6 lib/vendor/plugins/rspec/lib/spec/runner/formatter/snippet_extractor.rb
simple-templater-0.0.1.4 gems/gems/rspec-1.2.9/lib/spec/runner/formatter/snippet_extractor.rb
media-path-0.1.2 vendor/rspec/lib/spec/runner/formatter/snippet_extractor.rb
simple-templater-0.0.1.3 vendor/rspec/lib/spec/runner/formatter/snippet_extractor.rb
pupu-0.0.2.pre vendor/rspec/lib/spec/runner/formatter/snippet_extractor.rb
media-path-0.1.1.pre vendor/rspec/lib/spec/runner/formatter/snippet_extractor.rb
simple-templater-0.0.1.2 vendor/rspec/lib/spec/runner/formatter/snippet_extractor.rb
media-path-0.1.1 vendor/rspec/lib/spec/runner/formatter/snippet_extractor.rb