Skip to content

Commit

Permalink
Pass along and utilize printer
Browse files Browse the repository at this point in the history
  • Loading branch information
icy-arctic-fox committed Aug 5, 2024
1 parent 7c73884 commit c8fbd84
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
14 changes: 12 additions & 2 deletions src/spectator/matchers/formatting.cr
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
require "../formatters/printer"

module Spectator::Matchers
module Formatting
private def description_of(value)
# TODO: Actually format the value.
value.inspect
end

private def format_description_of(io : IO, value) : Nil
value.inspect(io)
private def format_description_of(printer : Formatters::Printer, value) : Nil
printer.print_value do |io|
value.inspect(io)
end
end

private def format_description_of(printer : Formatters::Printer, value : T.class) : Nil forall T
printer.print_type do |io|
value.inspect(io)
end
end
end
end
36 changes: 22 additions & 14 deletions src/spectator/matchers/match_data.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "colorize"
require "../core/location_range"
require "../formatters/plain_printer"
require "../formatters/printer"

module Spectator::Matchers
abstract struct MatchData
Expand All @@ -15,6 +16,8 @@ module Spectator::Matchers
end

abstract def try_raise : Nil

abstract def format(printer : Formatters::Printer) : Nil
end

struct PassedMatchData < MatchData
Expand All @@ -29,25 +32,24 @@ module Spectator::Matchers
def try_raise : Nil
# ...
end

def format(printer : Formatters::Printer) : Nil
# TODO: Print "Passed" string
end
end

struct FailedMatchData < MatchData
getter message : String do
color_enabled = Colorize.enabled?
begin
to_s
ensure
Colorize.enabled = color_enabled
end
to_s
end

@proc : (IO ->)?
@proc : (Formatters::Printer ->)?

def initialize(@message : String, location : Core::LocationRange? = nil)
super(location)
end

def initialize(location : Core::LocationRange? = nil, &block : IO ->)
def initialize(location : Core::LocationRange? = nil, &block : Formatters::Printer ->)
super(location)
@proc = block
end
Expand All @@ -60,15 +62,21 @@ module Spectator::Matchers
raise AssertionFailed.new(self)
end

def to_s(io : IO) : Nil
def format(printer : Formatters::Printer) : Nil
if proc = @proc
proc.call(io)
proc.call(printer)
elsif message = @message
io << message
printer.print_value do |io|
io << message
end
else
io << "Failed"
# TODO: Print "Failed" string
end
end

def to_s(io : IO) : Nil
format(Formatters::PlainPrinter.new(io))
end
end

def self.passed(location : Core::LocationRange? = nil) : MatchData
Expand All @@ -79,7 +87,7 @@ module Spectator::Matchers
FailedMatchData.new(message, location)
end

def self.failed(location : Core::LocationRange? = nil, &block : IO ->) : MatchData
def self.failed(location : Core::LocationRange? = nil, &block : Formatters::Printer ->) : MatchData
FailedMatchData.new(location, &block)
end
end
11 changes: 6 additions & 5 deletions src/spectator/matchers/matchable.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "./formatting"
require "../formatters/printer"
require "../framework_error"
require "./formatting"

module Spectator::Matchers
module Matchable
Expand All @@ -21,16 +22,16 @@ module Spectator::Matchers
"Expected #{description_of actual_value} to #{description}"
end

def format_failure_message(io : IO, actual_value) : Nil
io << failure_message(actual_value)
def format_failure_message(printer : Formatters::Printer, actual_value) : Nil
printer << failure_message(actual_value)
end

def negated_failure_message(actual_value)
"Expected #{description_of actual_value} not to #{description}"
end

def format_negated_failure_message(io : IO, actual_value) : Nil
io << negated_failure_message(actual_value)
def format_negated_failure_message(printer : Formatters::Printer, actual_value) : Nil
printer << negated_failure_message(actual_value)
end

def =~(other)
Expand Down

0 comments on commit c8fbd84

Please sign in to comment.