Allow actual collection to be enumerable, not just array

Force expected collection to an array.
This commit is contained in:
Michael Miller 2019-06-12 15:36:09 -06:00
parent 19d52ff02b
commit 3c7bbe4e42
2 changed files with 10 additions and 8 deletions

View file

@ -4,12 +4,13 @@ require "./unordered_array_matcher"
module Spectator::Matchers
# Matcher for checking that the contents of one array (or similar type)
# has the exact same contents as another and in the same order.
struct ArrayMatcher(ExpectedType) < ValueMatcher(Array(ExpectedType))
struct ArrayMatcher(ExpectedType) < ValueMatcher(Enumerable(ExpectedType))
# Determines whether the matcher is satisfied with the partial given to it.
# `MatchData` is returned that contains information about the match.
def match(partial)
actual = partial.actual.to_a
values = ExpectedActual.new(expected, label, actual, partial.label)
expected_elements = expected.to_a
values = ExpectedActual.new(expected_elements, label, actual, partial.label)
if values.expected.size == values.actual.size
index = 0
values.expected.zip(values.actual) do |expected, element|
@ -25,14 +26,14 @@ module Spectator::Matchers
# Creates the value matcher.
# The label should be a string representation of the expectation.
# The expected value is stored for later use.
def initialize(expected : Array(ExpectedType), label : String)
def initialize(expected : Enumerable(ExpectedType), label : String)
super
end
# Creates the value matcher.
# The label is generated by calling `#to_s` on the expected value.
# The expected value is stored for later use.
def initialize(expected : Array(ExpectedType))
def initialize(expected : Enumerable(ExpectedType))
super
end

View file

@ -3,14 +3,15 @@ require "./value_matcher"
module Spectator::Matchers
# Matcher for checking that the contents of one array (or similar type)
# has the exact same contents as another, but in any order.
struct UnorderedArrayMatcher(ExpectedType) < ValueMatcher(Array(ExpectedType))
struct UnorderedArrayMatcher(ExpectedType) < ValueMatcher(Enumerable(ExpectedType))
# Determines whether the matcher is satisfied with the partial given to it.
# `MatchData` is returned that contains information about the match.
def match(partial)
expected_elements = expected.to_a
actual = partial.actual.to_a
missing, extra = array_diff(expected, actual)
values = ExpectedActual.new(expected, label, actual, partial.label)
values = ExpectedActual.new(expected_elements, label, actual, partial.label)
if missing.empty? && extra.empty?
IdenticalMatchData.new(values)
else
@ -40,14 +41,14 @@ module Spectator::Matchers
# Creates the value matcher.
# The label should be a string representation of the expectation.
# The expected value is stored for later use.
def initialize(expected : Array(ExpectedType), label : String)
def initialize(expected : Enumerable(ExpectedType), label : String)
super
end
# Creates the value matcher.
# The label is generated by calling `#to_s` on the expected value.
# The expected value is stored for later use.
def initialize(expected : Array(ExpectedType))
def initialize(expected : Enumerable(ExpectedType))
super
end