Abstract Matcher#label getter

There's no need to store a value for this.
Some matchers have a static label.
This commit is contained in:
Michael Miller 2019-02-28 14:48:46 -07:00
parent 8cc66b538f
commit 226708cb82
6 changed files with 22 additions and 22 deletions

View File

@ -4,9 +4,9 @@ module Spectator::Matchers
# Matcher that tests whether a collection is empty.
# The values are checked with the `empty?` method.
struct EmptyMatcher < Matcher
# Creates the matcher.
def initialize
super("empty?")
# Textual representation of what the matcher expects.
def label
"empty?"
end
# Determines whether the matcher is satisfied with the partial given to it.

View File

@ -8,11 +8,7 @@ module Spectator::Matchers
# Textual representation of what the matcher expects.
# This shouldn't be used in the conditional logic,
# but for verbose output to help the end-user.
getter label : String
# Creates the base of the matcher.
def initialize(@label)
end
abstract def label : String
# Determines whether the matcher is satisfied with the value given to it.
# True is returned if the match was successful, false otherwise.

View File

@ -4,9 +4,9 @@ module Spectator::Matchers
# Common matcher that tests whether a value is nil.
# The `Object#nil?` method is used for this.
struct NilMatcher < Matcher
# Creates the matcher.
def initialize
super("nil?")
# Textual representation of what the matcher expects.
def label
"nil?"
end
# Determines whether the matcher is satisfied with the partial given to it.

View File

@ -5,10 +5,10 @@ module Spectator::Matchers
# The `ExpectedType` type param should be a `NamedTuple`.
# Each key in the tuple is a predicate (without the '?') to test.
struct PredicateMatcher(ExpectedType) < Matcher
# Creates the matcher.
# Textual representation of what the matcher expects.
# Constructs the label from the type parameters.
def initialize
super({{ExpectedType.keys.splat.stringify}})
def label
{{ExpectedType.keys.splat.stringify}}
end
# Determines whether the matcher is satisfied with the value given to it.

View File

@ -4,10 +4,10 @@ module Spectator::Matchers
# Matcher that tests a value is of a specified type.
# The values are compared with the `Object#is_a?` method.
struct TypeMatcher(Expected) < Matcher
# Creates the type matcher.
# The `Expected` type param will be used to populate the underlying label.
def initialize
super(Expected.to_s)
# Textual representation of what the matcher expects.
# The `Expected` type param will be used to populate the label.
def label
Expected.to_s
end
# Determines whether the matcher is satisfied with the value given to it.

View File

@ -5,6 +5,11 @@ module Spectator::Matchers
# Matchers of this type expect that a SUT applies to the value in some way.
# Sub-types must implement `Matcher#match?`, `Matcher#message`, and `Matcher#negated_message`.
abstract struct ValueMatcher(ExpectedType) < Matcher
# Textual representation of what the matcher expects.
# This shouldn't be used in the conditional logic,
# but for verbose output to help the end-user.
getter label
# Expected value.
# Sub-types may use this value to test the expectation and generate message strings.
getter expected
@ -12,15 +17,14 @@ module Spectator::Matchers
# Creates the value matcher.
# The label should be a string representation of the expectation.
# The expected value is stored for later use.
def initialize(label : String, @expected : ExpectedType)
super(label)
def initialize(@label : String, @expected : ExpectedType)
end
# Creates the value matcher.
# The label is generated by calling `#to_s` on the expected value.
# The expected value is stored for later use.
def initialize(@expected : ExpectedType)
super(@expected.to_s)
def initialize(expected : ExpectedType)
initialize(expected.to_s, expected)
end
end
end