Sha256: 870f74240c937431cb30ea77f604a3aacebb8ad0e79b8dbd73b9767d03b88192
Contents?: true
Size: 1.91 KB
Versions: 2
Compression:
Stored size: 1.91 KB
Contents
module RSpec module Core module Formatters # This class extracts code snippets by looking at the backtrace of the passed error class SnippetExtractor class NullConverter; def convert(code, pre); code; end; end begin require 'syntax/convertors/html' @@converter = Syntax::Convertors::HTML.for_syntax "ruby" rescue LoadError @@converter = NullConverter.new end def snippet(backtrace) raw_code, line = snippet_for(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.read(file).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 rescue SecurityError "# Couldn't get snippet for #{file}" 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
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
rspec-core-2.12.1 | lib/rspec/core/formatters/snippet_extractor.rb |
rspec-core-2.12.0 | lib/rspec/core/formatters/snippet_extractor.rb |