Class: RSpock::BacktraceFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/rspock/backtrace_filter.rb

Instance Method Summary collapse

Constructor Details

#initialize(source_map_provider: ::ASTTransform::SourceMap) ⇒ BacktraceFilter

Constructs a new BacktraceFilter instance.

Parameters:

  • source_map_provider (::ASTTransform::SourceMap) (defaults to: ::ASTTransform::SourceMap)

    The source map provider to be used.



9
10
11
# File 'lib/rspock/backtrace_filter.rb', line 9

def initialize(source_map_provider: ::ASTTransform::SourceMap)
  @source_map_provider = source_map_provider
end

Instance Method Details

#filter_exception(exception) ⇒ void

This method returns an undefined value.

Filters the backtrace of the given exception and applies the filtered backtrace to the exception.

Parameters:

  • exception (Exception)

    The exception to be filtered.



18
19
20
# File 'lib/rspock/backtrace_filter.rb', line 18

def filter_exception(exception)
  exception.set_backtrace(source_mapped_backtrace(exception))
end

#filter_string(location) ⇒ String

Filters the given location.

Parameters:

  • location (String)

    A location string.

Returns:

  • (String)

    The filtered location.



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rspock/backtrace_filter.rb', line 27

def filter_string(location)
  file_path, lineno = location.match(/([\S]+):(\d+)/).captures
  lineno = lineno.to_i
  absolute_path = File.expand_path(file_path)

  source_map = @source_map_provider.for_file_path(absolute_path)
  return location unless source_map

  line_number = source_map.line(lineno) || '?'
  location.gsub(/#{ASTTransform.output_path}\/([\S]+):(\d+)/, "\\1:#{line_number}")
end