Update formatters to handle new labeled values

This commit is contained in:
Michael Miller 2019-03-22 11:00:00 -06:00
parent 92b407380b
commit a19d9f00aa
4 changed files with 9 additions and 8 deletions

View file

@ -1,5 +1,5 @@
module Spectator::Formatting
# A single key-value pair from the `Spectator::Matchers::MatchData#value` method.
# A single labeled value from the `Spectator::Matchers::MatchData#value` method.
private struct MatchDataValuePair(T)
# Creates the pair formatter.
def initialize(@key : Symbol, @value : T, @padding : Int32)

View file

@ -1,22 +1,23 @@
module Spectator::Formatting
# Produces a `MatchDataValuePair` for each key-value pair
# from `Spectator::Matchers::MatchData#values`.
private struct MatchDataValues(T)
private struct MatchDataValues
include Enumerable(MatchDataValuePair)
@max_key_length : Int32
# Creates the values mapper.
def initialize(@values : T)
@max_key_length = @values.keys.map(&.to_s.size).max
def initialize(@values : Array(Spectator::Matchers::MatchDataLabeledValue))
@max_key_length = @values.map(&.label.to_s.size).max
end
# Yields pairs that can be printed to output.
def each
@values.each do |key, value|
@values.each do |labeled_value|
key = labeled_value.label
key_length = key.to_s.size
padding = @max_key_length - key_length
yield MatchDataValuePair.new(key, value, padding)
yield MatchDataValuePair.new(key, labeled_value.value, padding)
end
end
end

View file

@ -1,6 +1,6 @@
module Spectator::Matchers
# A value from match data with a label.
private struct MatchDataLabeledValue
struct MatchDataLabeledValue
# Label tied to the value.
# This annotates what the value is.
getter label : Symbol

View file

@ -3,7 +3,7 @@ require "./match_data_value"
module Spectator::Matchers
# Wraps an expected value that can be negated.
# This is used when a matcher is negated.
private class NegatableMatchDataValue(T) < MatchDataValue
private struct NegatableMatchDataValue(T) < MatchDataValue
# Negatable value.
getter value