From 436c10cad6df184805b6205731a8a04ec4da6b94 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Tue, 6 Aug 2019 23:13:33 -0600 Subject: [PATCH] Simplify match data values by using named tuples --- src/spectator/matchers/array_matcher.cr | 28 ++++++++----------- src/spectator/matchers/contain_matcher.cr | 10 +++---- src/spectator/matchers/empty_matcher.cr | 4 +-- src/spectator/matchers/failed_match_data.cr | 6 ++-- .../matchers/greater_than_equal_matcher.cr | 16 +++++------ .../matchers/greater_than_matcher.cr | 16 +++++------ .../matchers/less_than_equal_matcher.cr | 16 +++++------ src/spectator/matchers/less_than_matcher.cr | 16 +++++------ src/spectator/matchers/standard_matcher.cr | 8 +++--- src/spectator/matchers/value_matcher.cr | 8 +++--- 10 files changed, 62 insertions(+), 66 deletions(-) diff --git a/src/spectator/matchers/array_matcher.cr b/src/spectator/matchers/array_matcher.cr index 268d0fe..acbf26d 100644 --- a/src/spectator/matchers/array_matcher.cr +++ b/src/spectator/matchers/array_matcher.cr @@ -1,4 +1,3 @@ -require "../test_value" require "./failed_match_data" require "./matcher" require "./successful_match_data" @@ -61,29 +60,26 @@ module Spectator::Matchers private def failed_size_mismatch(expected_elements, actual_elements, actual_label) FailedMatchData.new("#{actual_label} does not contain exactly #{expected.label} (size mismatch)", - [ - LabeledValue.new(expected_elements.inspect, "expected"), - LabeledValue.new(actual_elements.inspect, "actual"), - LabeledValue.new(expected_elements.size, "expected size"), - LabeledValue.new(actual_elements.size, "actual size"), - ]) + expected: expected_elements.inspect, + actual: actual_elements.inspect, + "expected size": expected_elements.size.to_s, + "actual size": actual_elements.size.to_s + ) end private def failed_content_mismatch(expected_elements, actual_elements, index, actual_label) FailedMatchData.new("#{actual_label} does not contain exactly #{expected.label} (element mismatch)", - [ - LabeledValue.new(expected_elements[index].inspect, "expected"), - LabeledValue.new(actual_elements[index].inspect, "actual"), - LabeledValue.new(index.to_s, "index"), - ]) + expected: expected_elements[index].inspect, + actual: actual_elements[index].inspect, + index: index.to_s + ) end private def failed_content_identical(expected_elements, actual_elements, actual_label) FailedMatchData.new("#{actual.label} contains exactly #{expected.label}", - [ - LabeledValue.new("Not #{expected_elements.inspect}", "expected"), - LabeledValue.new(actual_elements.inspect, "actual"), - ]) + expected: "Not #{expected_elements.inspect}", + actual: actual_elements.inspect + ) end end end diff --git a/src/spectator/matchers/contain_matcher.cr b/src/spectator/matchers/contain_matcher.cr index f707486..fa6d65a 100644 --- a/src/spectator/matchers/contain_matcher.cr +++ b/src/spectator/matchers/contain_matcher.cr @@ -22,11 +22,11 @@ module Spectator::Matchers "#{actual.label} contains #{expected.label}" end - private def values(actual) : Array(LabeledValue) - [ - LabeledValue.new(expected.value.inspect, "subset"), - LabeledValue.new(actual.value.inspect, "superset"), - ] + private def values(actual) + { + subset: expected.value.inspect, + superset: actual.value.inspect, + } end end end diff --git a/src/spectator/matchers/empty_matcher.cr b/src/spectator/matchers/empty_matcher.cr index dcd5582..658cd17 100644 --- a/src/spectator/matchers/empty_matcher.cr +++ b/src/spectator/matchers/empty_matcher.cr @@ -20,8 +20,8 @@ module Spectator::Matchers "#{actual.label} is empty" end - private def values(actual) : Array(LabeledValue) - [LabeledValue.new(actual.value.inspect, "actual")] + private def values(actual) + {actual: actual.value.inspect} end end end diff --git a/src/spectator/matchers/failed_match_data.cr b/src/spectator/matchers/failed_match_data.cr index 250cf64..eef1cb2 100644 --- a/src/spectator/matchers/failed_match_data.cr +++ b/src/spectator/matchers/failed_match_data.cr @@ -1,4 +1,3 @@ -require "../test_value" require "./match_data" module Spectator::Matchers @@ -9,9 +8,10 @@ module Spectator::Matchers getter failure_message : String - getter values : Array(LabeledValue) + getter values : Array(Tuple(Symbol, String)) - def initialize(@failure_message, @values = [] of LabeledValue) + def initialize(@failure_message, **values) + @values = values.to_a end end end diff --git a/src/spectator/matchers/greater_than_equal_matcher.cr b/src/spectator/matchers/greater_than_equal_matcher.cr index 9228a98..1a2ef80 100644 --- a/src/spectator/matchers/greater_than_equal_matcher.cr +++ b/src/spectator/matchers/greater_than_equal_matcher.cr @@ -21,17 +21,17 @@ module Spectator::Matchers end private def values(actual) - [ - LabeledValue.new(">= #{expected.value.inspect}", "expected"), - LabeledValue.new(actual.value.inspect, "actual"), - ] + { + expected: ">= #{expected.value.inspect}", + actual: actual.value.inspect, + } end private def negated_values(actual) - [ - LabeledValue.new("< #{expected.value.inspect}", "expected"), - LabeledValue.new(actual.value.inspect, "actual"), - ] + { + expected: "< #{expected.value.inspect}", + actual: actual.value.inspect, + } end end end diff --git a/src/spectator/matchers/greater_than_matcher.cr b/src/spectator/matchers/greater_than_matcher.cr index c65a038..12ec50f 100644 --- a/src/spectator/matchers/greater_than_matcher.cr +++ b/src/spectator/matchers/greater_than_matcher.cr @@ -21,17 +21,17 @@ module Spectator::Matchers end private def values(actual) - [ - LabeledValue.new("> #{expected.value.inspect}", "expected"), - LabeledValue.new(actual.value.inspect, "actual"), - ] + { + expected: "> #{expected.value.inspect}", + actual: actual.value.inspect, + } end private def negated_values(actual) - [ - LabeledValue.new("<= #{expected.value.inspect}", "expected"), - LabeledValue.new(actual.value.inspect, "actual"), - ] + { + expected: "<= #{expected.value.inspect}", + actual: actual.value.inspect, + } end end end diff --git a/src/spectator/matchers/less_than_equal_matcher.cr b/src/spectator/matchers/less_than_equal_matcher.cr index 2146257..da43df1 100644 --- a/src/spectator/matchers/less_than_equal_matcher.cr +++ b/src/spectator/matchers/less_than_equal_matcher.cr @@ -21,17 +21,17 @@ module Spectator::Matchers end private def values(actual) - [ - LabeledValue.new("<= #{expected.value.inspect}", "expected"), - LabeledValue.new(actual.value.inspect, "actual"), - ] + { + expected: "<= #{expected.value.inspect}", + actual: actual.value.inspect, + } end private def negated_values(actual) - [ - LabeledValue.new("> #{expected.value.inspect}", "expected"), - LabeledValue.new(actual.value.inspect, "actual"), - ] + { + expected: "> #{expected.value.inspect}", + actual: actual.value.inspect, + } end end end diff --git a/src/spectator/matchers/less_than_matcher.cr b/src/spectator/matchers/less_than_matcher.cr index e45f3d6..0c37302 100644 --- a/src/spectator/matchers/less_than_matcher.cr +++ b/src/spectator/matchers/less_than_matcher.cr @@ -21,17 +21,17 @@ module Spectator::Matchers end private def values(actual) - [ - LabeledValue.new("< #{expected.value.inspect}", "expected"), - LabeledValue.new(actual.value.inspect, "actual"), - ] + { + expected: "< #{expected.value.inspect}", + actual: actual.value.inspect, + } end private def negated_values(actual) - [ - LabeledValue.new(">= #{expected.value.inspect}", "expected"), - LabeledValue.new(actual.value.inspect, "actual"), - ] + { + expected: ">= #{expected.value.inspect}", + actual: actual.value.inspect, + } end end end diff --git a/src/spectator/matchers/standard_matcher.cr b/src/spectator/matchers/standard_matcher.cr index 491e21d..63ba640 100644 --- a/src/spectator/matchers/standard_matcher.cr +++ b/src/spectator/matchers/standard_matcher.cr @@ -32,12 +32,12 @@ module Spectator::Matchers !matches?(actual) end - private def values(actual) : Array(LabeledValue) - [LabeledValue.new(actual.value.inspect, "actual")] + private def values(actual) + {actual: actual.value.inspect} end - private def negated_values(actual) : Array(LabeledValue) - values + private def negated_values(actual) + values(actual) end def match(actual) diff --git a/src/spectator/matchers/value_matcher.cr b/src/spectator/matchers/value_matcher.cr index 7c5e0ca..fc00c42 100644 --- a/src/spectator/matchers/value_matcher.cr +++ b/src/spectator/matchers/value_matcher.cr @@ -13,12 +13,12 @@ module Spectator::Matchers def initialize(@expected) end - private def values(actual) : Array(LabeledValue) - super(actual) << LabeledValue.new(expected.value.inspect, "expected") + private def values(actual) + super.merge(expected: expected.value.inspect) end - private def negated_values(actual) : Array(LabeledValue) - super(actual) << LabeledValue.new("Not #{expected.value.inspect}", "expected") + private def negated_values(actual) + super.merge(expected: "Not #{expected.value.inspect}") end end end