Module: Pork::ShowSource

Defined in:
lib/pork/extra/show_source.rb

Instance Method Summary collapse

Instance Method Details

#show_source(test, err) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pork/extra/show_source.rb', line 6

def show_source test, err
  source = test.source
  sopath = "#{test.source_location.first}:"
  lowers = test.source_location.last
  uppers = lowers + source.size
  lineno = reject_pork(test, err).find do |backtrace|
    # find the line from the test source, exclude everything else
    next unless backtrace.start_with?(sopath)
    number = backtrace[/(?<=\.rb:)\d+/].to_i
    break number if number >= lowers && number < uppers
  end
  return '' unless lineno # TODO: error might be coming from before block
  lowerl = lineno - lowers
  upperl = MethodSource.expression_at(source, lowerl + 1).
             count("\n") + lowerl
  indent = source[/^\s*/].size
  result = source.each_line.with_index.map do |source_line, index|
    line = source_line[indent..-1] || "\n"
    if index >= lowerl && index < upperl
      highlight_line("  => #{line}")
    else
      backlight_line("     #{line}")
    end
  end.join
  "\n#{result.chomp}"
rescue SyntaxError => e
  "\nPork bug: Cannot parse the source. Please report: #{e}"
end