class Sus::Output::Backtrace
Print out a backtrace relevant to the given test identity if provided.
def self.extract_stack(exception)
def self.extract_stack(exception) if stack = exception.backtrace_locations return stack elsif stack = exception.backtrace return stack.map do |line| Location.new(*line.split(":", 3)) end else [] end end
def self.first(identity = nil)
def self.first(identity = nil) # This implementation could be a little more efficient. self.new(caller_locations(1), identity&.path, 1) end
def self.for(exception, identity = nil)
def self.for(exception, identity = nil) # I've disabled the root filter here, because partial backtraces are not very useful. # We might want to do something to improve presentation of the backtrace based on the root instead. self.new(extract_stack(exception), identity&.path) end
def filter(root: @root, limit: @limit)
def filter(root: @root, limit: @limit) if root if limit return @stack.lazy.select do |frame| frame.path.start_with?(root) end.first(limit) else return up_to_and_matching(@stack) do |frame| frame.path.start_with?(root) end end elsif limit return @stack.first(limit) else return @stack end end
def initialize(stack, root = nil, limit = nil)
def initialize(stack, root = nil, limit = nil) @stack = stack @root = root @limit = limit end
def print(output)
def print(output) if @limit == 1 filter.each do |frame| output.write " ", :path, frame.path, :line, ":", frame.lineno end else output.indented do filter.each do |frame| output.puts :indent, :path, frame.path, :line, ":", frame.lineno, :reset, " ", frame.label end end end end
def up_to_and_matching(things, &block)
def up_to_and_matching(things, &block) = true select do |thing| face eld(thing) ace = false yield(thing)