Class: Sus::Output::Backtrace

Inherits:
Object
  • Object
show all
Defined in:
lib/sus/output/backtrace.rb

Overview

Print out a backtrace relevant to the given test identity if provided.

Defined Under Namespace

Classes: Location

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stack, root = nil, limit = nil) ⇒ Backtrace

Returns a new instance of Backtrace.



35
36
37
38
39
# File 'lib/sus/output/backtrace.rb', line 35

def initialize(stack, root = nil, limit = nil)
	@stack = stack
	@root = root
	@limit = limit
end

Instance Attribute Details

#limitObject (readonly)

Returns the value of attribute limit.



43
44
45
# File 'lib/sus/output/backtrace.rb', line 43

def limit
  @limit
end

#rootObject (readonly)

Returns the value of attribute root.



42
43
44
# File 'lib/sus/output/backtrace.rb', line 42

def root
  @root
end

#stackObject (readonly)

Returns the value of attribute stack.



41
42
43
# File 'lib/sus/output/backtrace.rb', line 41

def stack
  @stack
end

Class Method Details

.extract_stack(exception) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sus/output/backtrace.rb', line 23

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

.first(identity = nil) ⇒ Object



10
11
12
13
# File 'lib/sus/output/backtrace.rb', line 10

def self.first(identity = nil)
	# This implementation could be a little more efficient.
	self.new(caller_locations(1), identity&.path, 1)
end

.for(exception, identity = nil) ⇒ Object



15
16
17
18
19
# File 'lib/sus/output/backtrace.rb', line 15

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

Instance Method Details

#filter(root: @root, limit: @limit) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sus/output/backtrace.rb', line 45

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


63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/sus/output/backtrace.rb', line 63

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