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 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) private struct MatchDataValuePair(T)
# Creates the pair formatter. # Creates the pair formatter.
def initialize(@key : Symbol, @value : T, @padding : Int32) def initialize(@key : Symbol, @value : T, @padding : Int32)

View file

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

View file

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

View file

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