Replace MatchDataValue types with a single LabeledValue

This commit is contained in:
Michael Miller 2019-08-01 13:49:08 -06:00
parent 43ba4de202
commit ae43c930bf
7 changed files with 10 additions and 165 deletions

View file

@ -1,32 +0,0 @@
require "./match_data_value"
module Spectator::Matchers
# Selects a value based on whether the value is negated.
# This is used when a matcher is negated.
private class AlternativeMatchDataValue(T1, T2) < MatchDataValue
# Creates the wrapper.
def initialize(@value1 : T1, @value2 : T2)
@negated = false
end
# Negates (toggles) the value.
def negate
@negated = !@negated
end
# Returns the correct value based on the negated status.
def value
@negated ? @value2 : @value1
end
# Produces a stringified value.
def to_s(io)
io << value
end
# Produces a stringified value with additional information.
def inspect(io)
io << value
end
end
end

View file

@ -1,23 +0,0 @@
require "./match_data_value"
module Spectator::Matchers
# Wraps a value for used in match data.
private class GenericMatchDataValue(T) < MatchDataValue
# Underlying value.
getter value
# Creates the wrapper.
def initialize(@value : T)
end
# Stringifies the value.
def to_s(io)
@value.inspect(io)
end
# Inspects the value.
def inspect(io)
@value.inspect(io)
end
end
end

View file

@ -0,0 +1,10 @@
module Spectator::Matchers
struct LabeledValue
getter label : Symbol
getter value : String
def initialize(@label, @value)
end
end
end

View file

@ -1,10 +0,0 @@
module Spectator::Matchers
# Abstract base for all match data values.
# All sub-classes are expected to implement their own `#to_s`.
private abstract class MatchDataValue
# Placeholder for negating the value.
def negate
# ...
end
end
end

View file

@ -1,34 +0,0 @@
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
# Negatable value.
getter value
# Creates the wrapper.
def initialize(@value : T)
@negated = false
end
# Negates (toggles) the value.
def negate
@negated = !@negated
end
# Produces a stringified value.
# The string will be prefixed with "Not" when negated.
def to_s(io)
io << "Not " if @negated
@value.inspect(io)
end
# Produces a stringified value with additional information.
# The string will be prefixed with "Not" when negated.
def inspect(io)
io << "Not " if @negated
@value.inspect(io)
end
end
end

View file

@ -1,39 +0,0 @@
require "./match_data_value"
module Spectator::Matchers
# Wraps a prefixed value that can be negated.
# This is used when a matcher is negated.
private class NegatablePrefixedMatchDataValue(T) < MatchDataValue
# Negatable value.
getter value
# Creates the wrapper.
def initialize(@positive_prefix : String, @negative_prefix : String, @value : T)
@negated = false
end
# Negates (toggles) the value.
def negate
@negated = !@negated
end
# Returns the correct prefix based on the negated status.
private def prefix
@negated ? @negative_prefix : @positive_prefix
end
# Produces a stringified value.
def to_s(io)
io << prefix
io << ' '
@value.inspect(io)
end
# Produces a stringified value with additional information.
def inspect(io)
io << prefix
io << ' '
@value.inspect(io)
end
end
end

View file

@ -1,27 +0,0 @@
require "./match_data_value"
module Spectator::Matchers
# Prefixes (for output formatting) an actual or expected value.
private class PrefixedMatchDataValue(T) < MatchDataValue
# Value being prefixed.
getter value : T
# Creates the prefixed value.
def initialize(@prefix : String, @value : T)
end
# Outputs the formatted value with a prefix.
def to_s(io)
io << @prefix
io << ' '
@value.inspect(io)
end
# Outputs details of the formatted value with a prefix.
def inspect(io)
io << @prefix
io << ' '
@prefix.inspect(io)
end
end
end