mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Move more failure block chunks to their own structs
This commit is contained in:
parent
bef3243c6c
commit
e752f901a5
3 changed files with 59 additions and 7 deletions
|
@ -26,6 +26,7 @@ module Spectator::Formatters
|
||||||
# Creates the block of text describing the failure.
|
# Creates the block of text describing the failure.
|
||||||
def to_s(io)
|
def to_s(io)
|
||||||
inner_indent = integer_length(@index) + 2 # +2 for ) and space after number.
|
inner_indent = integer_length(@index) + 2 # +2 for ) and space after number.
|
||||||
|
|
||||||
indent do
|
indent do
|
||||||
title(io)
|
title(io)
|
||||||
indent(inner_indent) do
|
indent(inner_indent) do
|
||||||
|
@ -42,7 +43,7 @@ module Spectator::Formatters
|
||||||
# 1) Example name
|
# 1) Example name
|
||||||
# ```
|
# ```
|
||||||
private def title(io)
|
private def title(io)
|
||||||
line(io, "#{@index}) #{@result.example}")
|
line(io, NumberedItem.new(@index, @result.example))
|
||||||
end
|
end
|
||||||
|
|
||||||
# Produces the message line of the failure block.
|
# Produces the message line of the failure block.
|
||||||
|
@ -53,7 +54,7 @@ module Spectator::Formatters
|
||||||
# The indentation of this line starts directly under
|
# The indentation of this line starts directly under
|
||||||
# the example name from the title line.
|
# the example name from the title line.
|
||||||
private def message(io)
|
private def message(io)
|
||||||
line(io, Color.failure("Failure: #{@result.error}"))
|
line(io, FailureMessage.color(@result))
|
||||||
end
|
end
|
||||||
|
|
||||||
# Produces the values list of the failure block.
|
# Produces the values list of the failure block.
|
||||||
|
|
35
src/spectator/formatters/failure_message.cr
Normal file
35
src/spectator/formatters/failure_message.cr
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
module Spectator::Formatters
|
||||||
|
# Produces a stringified failure or error message.
|
||||||
|
private struct FailureMessage
|
||||||
|
# Creates the failure message.
|
||||||
|
def initialize(@result : FailedResult)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Appends the message to the output.
|
||||||
|
def to_s(io)
|
||||||
|
io << @result.call(Label)
|
||||||
|
io << @result.error
|
||||||
|
end
|
||||||
|
|
||||||
|
# Creates a colorized version of the message.
|
||||||
|
def self.color(result)
|
||||||
|
result.call(Color) { |result| new(result) }
|
||||||
|
end
|
||||||
|
|
||||||
|
# Interface for `Result#call` to invoke.
|
||||||
|
# This is used to get the correct prefix for failures and errors.
|
||||||
|
private module Label
|
||||||
|
extend self
|
||||||
|
|
||||||
|
# Returns the prefix for a failure message.
|
||||||
|
def failure
|
||||||
|
"Failure: "
|
||||||
|
end
|
||||||
|
|
||||||
|
# Returns the prefix for an error message.
|
||||||
|
def error
|
||||||
|
"Error: "
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
16
src/spectator/formatters/numbered_item.cr
Normal file
16
src/spectator/formatters/numbered_item.cr
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
module Spectator::Formatters
|
||||||
|
# Produces a stringified value with a numerical prefix.
|
||||||
|
private struct NumberedItem(T)
|
||||||
|
# Creates the numbered item.
|
||||||
|
def initialize(@number : Int32, @text : T)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Appends the numbered item to the output.
|
||||||
|
def to_s(io)
|
||||||
|
io << @number
|
||||||
|
io << ')'
|
||||||
|
io << ' '
|
||||||
|
io << @text
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Add table
Add a link
Reference in a new issue