Skip to content

Commit

Permalink
WIP change matchers to new interface
Browse files Browse the repository at this point in the history
Fix various issues.
  • Loading branch information
icy-arctic-fox committed Nov 28, 2024
1 parent dc268dc commit 22678de
Show file tree
Hide file tree
Showing 28 changed files with 148 additions and 331 deletions.
4 changes: 2 additions & 2 deletions src/spectator/formatters/common_text_output.cr
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ module Spectator::Formatters
end
printer.puts
printer.puts
if match_data = error.match_data
match_data.format(printer)
if match_failure = error.match_failure
match_failure.format(printer)
printer.puts
else
printer.puts error.message
Expand Down
37 changes: 10 additions & 27 deletions src/spectator/matchers/built_in/be_a_matcher.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,18 @@ module Spectator::Matchers::BuiltIn
actual_value.is_a?(T)
end

print_messages

def failure_message(printer : FormattingPrinter, actual_value) : Nil
printer << " Expected: "
printer.description_of(actual_value)
printer.puts

printer << " to be a: "
printer.description_of(T)
printer.puts

printer << "but was a: "
printer.description_of(actual_value.class)
def print_failure_message(printer : Formatters::Printer, actual_value) : Nil
printer << " Expected: " << description_of(actual_value) << EOL
printer << " to be a: " << description_of(T) << EOL
printer << "but was a: " << description_of(actual_value.class)
end

def negated_failure_message(printer : FormattingPrinter, actual_value) : Nil
printer << " Expected: "
printer.description_of(actual_value)
printer.puts

printer << "not to be a: "
printer.description_of(T)

return if actual_value.class == T
printer.puts

printer.description_of(actual_value.class)
printer << " is a sub-type of "
printer.description_of(T)
def print_negated_failure_message(printer : Formatters::Printer, actual_value) : Nil
printer << " Expected: " << description_of(actual_value) << EOL
printer << "not to be a: " << description_of(T)
if actual_value.class < T
printer << EOL << description_of(actual_value.class) << " is a sub-type of " << description_of(T)
end
end
end
end
26 changes: 6 additions & 20 deletions src/spectator/matchers/built_in/be_between_matcher.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,15 @@ module Spectator::Matchers::BuiltIn
end
end

print_messages

def failure_message(printer : FormattingPrinter, actual_value) : Nil
printer << " Expected: "
printer.description_of(actual_value)
printer.puts

printer << "to be between: "
printer.description_of(@min)
printer << " and "
printer.description_of(@max)
def print_failure_message(printer : Formatters::Printer, actual_value) : Nil
printer << " Expected: " << description_of(actual_value) << EOL
printer << "to be between: " << description_of(@min) << " and " << description_of(@max)
printer << " (" << (@exclusive ? "exclusive" : "inclusive") << ')'
end

def negated_failure_message(printer : FormattingPrinter, actual_value) : Nil
printer << " Expected: "
printer.description_of(actual_value)
printer.puts

printer << "to be outside: "
printer.description_of(@min)
printer << " and "
printer.description_of(@max)
def print_negated_failure_message(printer : Formatters::Printer, actual_value) : Nil
printer << " Expected: " << description_of(actual_value) << EOL
printer << "to be outside: " << description_of(@min) << " and " << description_of(@max)
printer << " (" << (@exclusive ? "exclusive" : "inclusive") << ')'
end

Expand Down
16 changes: 5 additions & 11 deletions src/spectator/matchers/built_in/be_blank_matcher.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,16 @@ module Spectator::Matchers::BuiltIn
actual_value.responds_to?(:blank?) && actual_value.blank?
end

print_messages

def failure_message(printer : FormattingPrinter, actual_value) : Nil
printer << "Expected "
printer.description_of(actual_value)
printer << " to be blank"
def print_failure_message(printer : Formatters::Printer, actual_value) : Nil
printer << "Expected " << description_of(actual_value) << " to be blank"
end

def negated_failure_message(printer : FormattingPrinter, actual_value : String) : Nil
def print_negated_failure_message(printer : Formatters::Printer, actual_value : String) : Nil
printer << "Expected string not to be blank"
end

def negated_failure_message(printer : FormattingPrinter, actual_value) : Nil
printer << "Expected "
printer.description_of(actual_value)
printer << " not to be blank"
def print_negated_failure_message(printer : Formatters::Printer, actual_value) : Nil
printer << "Expected " << description_of(actual_value) << " not to be blank"
end
end
end
26 changes: 6 additions & 20 deletions src/spectator/matchers/built_in/be_close_matcher.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,14 @@ module Spectator::Matchers::BuiltIn
actual_value >= @expected_value - @delta
end

print_messages

def failure_message(printer : FormattingPrinter, actual_value) : Nil
printer << " Expected: "
printer.description_of(actual_value)
printer.puts

printer << "to be within: "
printer.description_of(@expected_value)
printer << " ± "
printer.description_of(@delta)
def print_failure_message(printer : Formatters::Printer, actual_value) : Nil
printer << " Expected: " << description_of(actual_value) << EOL
printer << "to be within: " << description_of(@expected_value) << " ± " << description_of(@delta)
end

def negated_failure_message(printer : FormattingPrinter, actual_value) : Nil
printer << " Expected: "
printer.description_of(actual_value)
printer.puts

printer << "to be outside: "
printer.description_of(@expected_value)
printer << " ± "
printer.description_of(@delta)
def print_negated_failure_message(printer : Formatters::Printer, actual_value) : Nil
printer << " Expected: " << description_of(actual_value) << EOL
printer << "to be outside: " << description_of(@expected_value) << " ± " << description_of(@delta)
end
end
end
14 changes: 4 additions & 10 deletions src/spectator/matchers/built_in/be_empty_matcher.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,12 @@ module Spectator::Matchers::BuiltIn
(actual_value.responds_to?(:size) && actual_value.size == 0)
end

print_messages

def failure_message(printer : FormattingPrinter, actual_value) : Nil
printer << "Expected "
printer.description_of(actual_value)
printer << " to be empty"
def print_failure_message(printer : Formatters::Printer, actual_value) : Nil
printer << "Expected " << description_of(actual_value) << " to be empty"
end

def negated_failure_message(printer : FormattingPrinter, actual_value) : Nil
printer << "Expected "
printer.description_of(actual_value)
printer << " not to be empty"
def print_negated_failure_message(printer : Formatters::Printer, actual_value) : Nil
printer << "Expected " << description_of(actual_value) << " not to be empty"
end
end
end
22 changes: 6 additions & 16 deletions src/spectator/matchers/built_in/be_in_matcher.cr
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,14 @@ module Spectator::Matchers::BuiltIn
actual_value.in?(@expected_value)
end

print_messages

def failure_message(printer : FormattingPrinter, actual_value) : Nil
printer << "Expected: "
printer.description_of(actual_value)
printer.puts

printer << "to be in: "
printer.description_of(@expected_value)
def print_failure_message(printer : Formatters::Printer, actual_value) : Nil
printer << "Expected: " << description_of(actual_value) << EOL
printer << "to be in: " << description_of(@expected_value)
end

def negated_failure_message(printer : FormattingPrinter, actual_value) : Nil
printer << " Expected: "
printer.description_of(actual_value)
printer.puts

printer << "not to be in: "
printer.description_of(@expected_value)
def print_negated_failure_message(printer : Formatters::Printer, actual_value) : Nil
printer << " Expected: " << description_of(actual_value) << EOL
printer << "not to be in: " << description_of(@expected_value)
end
end
end
14 changes: 4 additions & 10 deletions src/spectator/matchers/built_in/be_infinite_matcher.cr
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,12 @@ module Spectator::Matchers::BuiltIn
actual_value != Float64::INFINITY && actual_value != -Float64::INFINITY)
end

print_messages

def failure_message(printer : FormattingPrinter, actual_value) : Nil
printer << "Expected "
printer.description_of(actual_value)
printer << " to be infinite"
def print_failure_message(printer : Formatters::Printer, actual_value) : Nil
printer << "Expected " << description_of(actual_value) << " to be infinite"
end

def negated_failure_message(printer : FormattingPrinter, actual_value) : Nil
printer << "Expected "
printer.description_of(actual_value)
printer << " to be finite"
def print_negated_failure_message(printer : Formatters::Printer, actual_value) : Nil
printer << "Expected " << description_of(actual_value) << " to be finite"
end
end
end
47 changes: 11 additions & 36 deletions src/spectator/matchers/built_in/be_instance_of_matcher.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,26 @@ module Spectator::Matchers::BuiltIn
actual_value.class == T
end

print_messages

def failure_message(printer : FormattingPrinter, actual_value) : Nil
def print_failure_message(printer : Formatters::Printer, actual_value) : Nil
if actual_value.is_a?(T)
printer << " Expected: "
printer.description_of(actual_value)
printer.puts

printer << " to be a: "
printer.description_of(T)
printer.puts

printer << "but was a: "
printer.description_of(actual_value.class)
printer.puts
printer.puts
printer << " Expected: " << description_of(actual_value) << EOL
printer << " to be a: " << description_of(T) << EOL
printer << "but was a: " << description_of(actual_value.class) << EOL << EOL

printer.description_of(actual_value.class)
printer << " is a sub-type of "
printer.description_of(T)
printer.puts '.'
printer << description_of(actual_value.class) << " is a sub-type of " << description_of(T) << EOL

printer.puts "Using `be_instance_of` ensures the type matches EXACTLY."
printer.print "If you want to match sub-types, use `be_a` instead."
else
printer << " Expected: "
printer.description_of(actual_value)
printer.puts

printer << " to be a: "
printer.description_of(T)
printer.puts

printer << "but was a: "
printer.description_of(actual_value.class)
printer << " Expected: " << description_of(actual_value) << EOL
printer << " to be a: " << description_of(T) << EOL
printer << "but was a: " << description_of(actual_value.class)
end
end

def negated_failure_message(printer : FormattingPrinter, actual_value) : Nil
printer << " Expected: "
printer.description_of(actual_value)
printer.puts

printer << "not to be a: "
printer.description_of(T)
def print_negated_failure_message(printer : Formatters::Printer, actual_value) : Nil
printer << " Expected: " << description_of(actual_value) << EOL
printer << "not to be a: " << description_of(T)
end
end
end
12 changes: 4 additions & 8 deletions src/spectator/matchers/built_in/be_matcher.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,13 @@ module Spectator::Matchers::BuiltIn
end
end

print_messages

def failure_message(printer : FormattingPrinter, actual_value) : Nil
printer << "Expected: " << stringify(actual_value)
printer.puts
def print_failure_message(printer : Formatters::Printer, actual_value) : Nil
printer << "Expected: " << stringify(actual_value) << EOL
printer << " to be: " << stringify(@expected_value)
end

def negated_failure_message(printer : FormattingPrinter, actual_value) : Nil
printer << " Expected: " << stringify(actual_value)
printer.puts
def print_negated_failure_message(printer : Formatters::Printer, actual_value) : Nil
printer << " Expected: " << stringify(actual_value) << EOL
printer << "not to be: " << stringify(@expected_value)
end

Expand Down
14 changes: 4 additions & 10 deletions src/spectator/matchers/built_in/be_nan_matcher.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,12 @@ module Spectator::Matchers::BuiltIn
actual_value.responds_to?(:nan?) && actual_value.nan?
end

print_messages

def failure_message(printer : FormattingPrinter, actual_value) : Nil
printer << "Expected "
printer.description_of(actual_value)
printer << " to be NaN"
def print_failure_message(printer : Formatters::Printer, actual_value) : Nil
printer << "Expected " << description_of(actual_value) << " to be NaN"
end

def negated_failure_message(printer : FormattingPrinter, actual_value) : Nil
printer << "Expected "
printer.description_of(actual_value)
printer << " not to be NaN"
def print_negated_failure_message(printer : Formatters::Printer, actual_value) : Nil
printer << "Expected " << description_of(actual_value) << " not to be NaN"
end
end
end
14 changes: 4 additions & 10 deletions src/spectator/matchers/built_in/be_negative_matcher.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,12 @@ module Spectator::Matchers::BuiltIn
actual_value.responds_to?(:negative?) && actual_value.negative?
end

print_messages

def failure_message(printer : FormattingPrinter, actual_value) : Nil
printer << "Expected "
printer.description_of(actual_value)
printer << " to be negative"
def print_failure_message(printer : Formatters::Printer, actual_value) : Nil
printer << "Expected " << description_of(actual_value) << " to be negative"
end

def negated_failure_message(printer : FormattingPrinter, actual_value) : Nil
printer << "Expected "
printer.description_of(actual_value)
printer << " not to be negative"
def print_negated_failure_message(printer : Formatters::Printer, actual_value) : Nil
printer << "Expected " << description_of(actual_value) << " not to be negative"
end
end
end
14 changes: 4 additions & 10 deletions src/spectator/matchers/built_in/be_nil_matcher.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,12 @@ module Spectator::Matchers::BuiltIn
actual_value.nil?
end

print_messages

def failure_message(printer : FormattingPrinter, actual_value) : Nil
printer << "Expected "
printer.description_of(actual_value)
printer << " to be nil"
def print_failure_message(printer : Formatters::Printer, actual_value) : Nil
printer << "Expected " << description_of(actual_value) << " to be nil"
end

def negated_failure_message(printer : FormattingPrinter, actual_value) : Nil
printer << "Expected "
printer.description_of(actual_value)
printer << " not to be nil"
def print_negated_failure_message(printer : Formatters::Printer, actual_value) : Nil
printer << "Expected " << description_of(actual_value) << " not to be nil"
end
end
end
Loading

0 comments on commit 22678de

Please sign in to comment.