Simplify negated usage and expose value

This commit is contained in:
Michael Miller 2019-03-07 15:34:55 -07:00
parent ee7a91c3dd
commit 37f2ce99a3
2 changed files with 17 additions and 7 deletions

View file

@ -2,9 +2,6 @@ module Spectator::Matchers
# Selects a value based on whether the value is negated. # Selects a value based on whether the value is negated.
# This is used when a matcher is negated. # This is used when a matcher is negated.
private class AlternativeValue(T1, T2) private class AlternativeValue(T1, T2)
# Negatable value.
getter value
# Creates the wrapper. # Creates the wrapper.
def initialize(@value1 : T1, @value2 : T2) def initialize(@value1 : T1, @value2 : T2)
@negated = false @negated = false
@ -15,14 +12,19 @@ module Spectator::Matchers
@negated = !@negated @negated = !@negated
end end
# Returns the correct value based on the negated status.
def value
@negated ? @value1 : @value2
end
# Produces a stringified value. # Produces a stringified value.
def to_s(io) def to_s(io)
io << @negated ? @value1 : @value2 io << value
end end
# Produces a stringified value with additional information. # Produces a stringified value with additional information.
def inspect(io) def inspect(io)
(@negated ? @value1 : @value2).inspect(io) value.inspect(io)
end end
end end
end end

View file

@ -2,6 +2,9 @@ module Spectator::Matchers
# Wraps a prefixed value that can be negated. # Wraps a prefixed value that can be negated.
# This is used when a matcher is negated. # This is used when a matcher is negated.
private class NegatablePrefixedValue(T) private class NegatablePrefixedValue(T)
# Negatable value.
getter value
# Creates the wrapper. # Creates the wrapper.
def initialize(@positive_prefix : String, @negative_prefix : String, @value : T) def initialize(@positive_prefix : String, @negative_prefix : String, @value : T)
@negated = false @negated = false
@ -12,15 +15,20 @@ module Spectator::Matchers
@negated = !@negated @negated = !@negated
end end
# Returns the correct prefix based on the negated status.
private def prefix
@negated ? @negative_prefix : @positive_prefix
end
# Produces a stringified value. # Produces a stringified value.
def to_s(io) def to_s(io)
io << @negated ? @negative_prefix : @positive_prefix io << prefix
io << @value io << @value
end end
# Produces a stringified value with additional information. # Produces a stringified value with additional information.
def inspect(io) def inspect(io)
io << @negated ? @negative_prefix : @positive_prefix io << prefix
@value.inspect(io) @value.inspect(io)
end end
end end