Module: Omnitest::ErrorSource

Defined in:
lib/omnitest/errors.rb

Instance Method Summary collapse

Instance Method Details

#error_sourceObject



61
62
63
64
65
66
67
# File 'lib/omnitest/errors.rb', line 61

def error_source
  if backtrace_locations
    source_from_backtrace(backtrace_locations)
  elsif original && original.backtrace_locations
    source_from_backtrace(original.backtrace_locations)
  end
end

#get_dedented_block(source_text, target_lineno) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/omnitest/errors.rb', line 76

def get_dedented_block(source_text, target_lineno)
  block = []
  lines = source_text.lines
  target_indent = lines[target_lineno][/\A */].size
  lines[0...target_lineno].reverse.each do |line|
    indent = line[/\A */].size
    block.prepend line
    break if indent < target_indent
  end
  lines[target_lineno..lines.size].each do |line|
    indent = line[/\A */].size
    block.push line
    break if indent < target_indent
  end
  block.join
end

#source_from_backtrace(backtrace_locations) ⇒ Object



69
70
71
72
73
74
# File 'lib/omnitest/errors.rb', line 69

def source_from_backtrace(backtrace_locations)
  error_location = backtrace_locations.delete_if { |l| l.absolute_path =~ /gems\/rspec-/ }.first
  error_source = File.read(error_location.absolute_path)
  error_lineno = error_location.lineno - 1 # lineno counts from 1
  get_dedented_block(error_source, error_lineno)
end