PageRenderTime 38ms CodeModel.GetById 15ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/bundle/jruby/2.1/gems/rspec-core-2.14.8/lib/rspec/core/formatters/snippet_extractor.rb

https://github.com/delowong/logstash
Ruby | 92 lines | 53 code | 6 blank | 33 comment | 2 complexity | ea205b04af8cab8767bd58dfa709d35e MD5 | raw file
 1module RSpec
 2  module Core
 3    module Formatters
 4      # @api private
 5      #
 6      # Extracts code snippets by looking at the backtrace of the passed error and applies synax highlighting and line numbers using html.
 7      class SnippetExtractor
 8        class NullConverter; def convert(code, pre); code; end; end
 9
10        begin
11          require 'syntax/convertors/html'
12          @@converter = Syntax::Convertors::HTML.for_syntax "ruby"
13        rescue LoadError
14          @@converter = NullConverter.new
15        end
16
17        # @api private
18        #
19        # Extract lines of code corresponding to  a backtrace.
20        #
21        # @param [String] backtrace the backtrace from a test failure
22        # @return [String] highlighted code snippet indicating where the test failure occured
23        #
24        # @see #post_process
25        def snippet(backtrace)
26          raw_code, line = snippet_for(backtrace[0])
27          highlighted = @@converter.convert(raw_code, false)
28          highlighted << "\n<span class=\"comment\"># gem install syntax to get syntax highlighting</span>" if @@converter.is_a?(NullConverter)
29          post_process(highlighted, line)
30        end
31
32        # @api private
33        #
34        # Create a snippet from a line of code.
35        #
36        # @param [String] error_line file name with line number (i.e. 'foo_spec.rb:12')
37        # @return [String] lines around the target line within the file
38        #
39        # @see #lines_around
40        def snippet_for(error_line)
41          if error_line =~ /(.*):(\d+)/
42            file = $1
43            line = $2.to_i
44            [lines_around(file, line), line]
45          else
46            ["# Couldn't get snippet for #{error_line}", 1]
47          end
48        end
49
50        # @api private
51        #
52        # Extract lines of code centered around a particular line within a source file.
53        #
54        # @param [String] file filename
55        # @param [Fixnum] line line number
56        # @return [String] lines around the target line within the file (2 above and 1 below).
57        def lines_around(file, line)
58          if File.file?(file)
59            lines = File.read(file).split("\n")
60            min = [0, line-3].max
61            max = [line+1, lines.length-1].min
62            selected_lines = []
63            selected_lines.join("\n")
64            lines[min..max].join("\n")
65          else
66            "# Couldn't get snippet for #{file}"
67          end
68        rescue SecurityError
69          "# Couldn't get snippet for #{file}"
70        end
71
72        # @api private
73        #
74        # Adds line numbers to all lines and highlights the line where the failure occurred using html `span` tags.
75        #
76        # @param [String] highlighted syntax-highlighted snippet surrounding the offending line of code
77        # @param [Fixnum] offending_line line where failure occured
78        # @return [String] completed snippet
79        def post_process(highlighted, offending_line)
80          new_lines = []
81          highlighted.split("\n").each_with_index do |line, i|
82            new_line = "<span class=\"linenum\">#{offending_line+i-2}</span>#{line}"
83            new_line = "<span class=\"offending\">#{new_line}</span>" if i == 2
84            new_lines << new_line
85          end
86          new_lines.join("\n")
87        end
88
89      end
90    end
91  end
92end