From 3c7bbe4e425a1805ecf209e7a75603019d979afa Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Wed, 12 Jun 2019 15:36:09 -0600 Subject: [PATCH] Allow actual collection to be enumerable, not just array Force expected collection to an array. --- src/spectator/matchers/array_matcher.cr | 9 +++++---- src/spectator/matchers/unordered_array_matcher.cr | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/spectator/matchers/array_matcher.cr b/src/spectator/matchers/array_matcher.cr index 834d8c8..df29b9a 100644 --- a/src/spectator/matchers/array_matcher.cr +++ b/src/spectator/matchers/array_matcher.cr @@ -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 diff --git a/src/spectator/matchers/unordered_array_matcher.cr b/src/spectator/matchers/unordered_array_matcher.cr index c58443a..d84adad 100644 --- a/src/spectator/matchers/unordered_array_matcher.cr +++ b/src/spectator/matchers/unordered_array_matcher.cr @@ -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