This commit is contained in:
Michael Miller 2021-05-18 20:10:02 -06:00
parent 867c06bd67
commit 453f6a2fce
No known key found for this signature in database
GPG key ID: FB9F12F7C646A4AD

View file

@ -9,8 +9,8 @@ module Spectator::Formatting
class DocumentFormatter < Formatter class DocumentFormatter < Formatter
include Summary include Summary
# Whitespace count per indent. # Identation string.
private INDENT_AMOUNT = 2 private INDENT = " "
# Output stream to write results to. # Output stream to write results to.
private getter io private getter io
@ -34,25 +34,19 @@ module Spectator::Formatting
# Invoked after an example completes successfully. # Invoked after an example completes successfully.
# Produces a successful example line. # Produces a successful example line.
def example_passed(notification) def example_passed(notification)
indent = @previous_hierarchy.size * INDENT_AMOUNT line(notification.example.name.colorize(:green))
indent.times { @io << ' ' }
@io.puts notification.example.name.colorize(:green)
end end
# Invoked after an example is skipped or marked as pending. # Invoked after an example is skipped or marked as pending.
# Produces a pending example line. # Produces a pending example line.
def example_pending(notification) def example_pending(notification)
indent = @previous_hierarchy.size * INDENT_AMOUNT line(notification.example.name.colorize(:yellow))
indent.times { @io << ' ' }
@io.puts notification.example.name.colorize(:yellow)
end end
# Invoked after an example fails. # Invoked after an example fails.
# Produces a failure example line. # Produces a failure example line.
def example_failed(notification) def example_failed(notification)
indent = @previous_hierarchy.size * INDENT_AMOUNT line(notification.example.name.colorize(:red))
indent.times { @io << ' ' }
@io.puts notification.example.name.colorize(:red)
end end
# Invoked after an example fails from an unexpected error. # Invoked after an example fails from an unexpected error.
@ -63,13 +57,14 @@ module Spectator::Formatting
# Produces a list of groups making up the hierarchy for an example. # Produces a list of groups making up the hierarchy for an example.
private def group_hierarchy(example) private def group_hierarchy(example)
hierarchy = [] of Label Array(Label).new.tap do |hierarchy|
group = example.group? group = example.group?
while group && (parent = group.group?) while group && (parent = group.group?)
hierarchy << group.name hierarchy << group.name
group = parent group = parent
end end
hierarchy.reverse hierarchy.reverse!
end
end end
# Generates a difference between two hierarchies. # Generates a difference between two hierarchies.
@ -83,11 +78,16 @@ module Spectator::Formatting
end end
# Displays an indented hierarchy starting partially into the whole hierarchy. # Displays an indented hierarchy starting partially into the whole hierarchy.
private def print_sub_hierarchy(start_index, sub_hierarchy) private def print_sub_hierarchy(start_index, hierarchy)
sub_hierarchy.each_with_index(start_index) do |name, index| hierarchy.each_with_index(start_index) do |name, index|
(index * INDENT_AMOUNT).times { @io << ' ' } line(name, index)
@io.puts name end
end end
# Displays an indented line of text.
private def line(text, level = @previous_hierarchy.size)
level.times { @io << INDENT }
@io.puts text
end end
end end
end end