Add error block component

This commit is contained in:
Michael Miller 2021-05-16 17:14:09 -06:00
parent ed3ad662d2
commit f81c498aef
No known key found for this signature in database
GPG key ID: FB9F12F7C646A4AD
3 changed files with 52 additions and 3 deletions

View file

@ -0,0 +1,44 @@
require "../../example"
require "../../error_result"
require "./comment"
module Spectator::Formatting::Components
struct ErrorBlock
private INDENT = 2
def initialize(@example : Example, @result : ErrorResult, @index : Int32)
end
def to_s(io)
2.times { io << ' ' }
io << @index
io << ')'
io << ' '
io.puts @example
indent = INDENT + index_digit_count + 2
indent.times { io << ' ' }
error = @result.error
io << "Error: ".colorize(:red)
io.puts error.message
io.puts
indent.times { io << ' ' }
io << error.class
io.puts ':'
indent += INDENT
error.backtrace?.try do |trace|
trace.each do |entry|
indent.times { io << ' ' }
entry = entry.colorize.dim unless entry.starts_with?(/src\/|spec\//)
io.puts entry
end
end
indent -= INDENT
indent.times { io << ' ' }
io.puts Comment.colorize(@example.location) # TODO: Use location of failed expectation.
end
private def index_digit_count
(Math.log(@index.to_f + 1) / Math.log(10)).ceil.to_i
end
end
end

View file

@ -1,12 +1,12 @@
require "../../example"
require "../../fail_result"
require "./comment"
module Spectator::Formatting::Components
struct FailureBlock
private INDENT = 2
def initialize(@example : Example, @index : Int32)
@result = @example.result.as(FailResult)
def initialize(@example : Example, @result : FailResult, @index : Int32)
end
def to_s(io)

View file

@ -1,3 +1,4 @@
require "../fail_result"
require "./components"
module Spectator::Formatting
@ -36,7 +37,11 @@ module Spectator::Formatting
io.puts "Failures:"
io.puts
examples.each_with_index do |example, index|
io.puts Components::FailureBlock.new(example, index + 1)
if result = example.result.as?(ErrorResult)
io.puts Components::ErrorBlock.new(example, result, index + 1)
elsif result = example.result.as?(FailResult)
io.puts Components::FailureBlock.new(example, result, index + 1)
end
end
end