Simplify match data values by using named tuples

This commit is contained in:
Michael Miller 2019-08-06 23:13:33 -06:00
parent e7b19ee591
commit 436c10cad6
10 changed files with 62 additions and 66 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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