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. # Matcher that tests whether a collection is empty.
# The values are checked with the `empty?` method. # The values are checked with the `empty?` method.
struct EmptyMatcher < Matcher struct EmptyMatcher < Matcher
# Creates the matcher. # Textual representation of what the matcher expects.
def initialize def label
super("empty?") "empty?"
end end
# Determines whether the matcher is satisfied with the partial given to it. # 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. # Textual representation of what the matcher expects.
# This shouldn't be used in the conditional logic, # This shouldn't be used in the conditional logic,
# but for verbose output to help the end-user. # but for verbose output to help the end-user.
getter label : String abstract def label : String
# Creates the base of the matcher.
def initialize(@label)
end
# Determines whether the matcher is satisfied with the value given to it. # Determines whether the matcher is satisfied with the value given to it.
# True is returned if the match was successful, false otherwise. # 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. # Common matcher that tests whether a value is nil.
# The `Object#nil?` method is used for this. # The `Object#nil?` method is used for this.
struct NilMatcher < Matcher struct NilMatcher < Matcher
# Creates the matcher. # Textual representation of what the matcher expects.
def initialize def label
super("nil?") "nil?"
end end
# Determines whether the matcher is satisfied with the partial given to it. # 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`. # The `ExpectedType` type param should be a `NamedTuple`.
# Each key in the tuple is a predicate (without the '?') to test. # Each key in the tuple is a predicate (without the '?') to test.
struct PredicateMatcher(ExpectedType) < Matcher struct PredicateMatcher(ExpectedType) < Matcher
# Creates the matcher. # Textual representation of what the matcher expects.
# Constructs the label from the type parameters. # Constructs the label from the type parameters.
def initialize def label
super({{ExpectedType.keys.splat.stringify}}) {{ExpectedType.keys.splat.stringify}}
end end
# Determines whether the matcher is satisfied with the value given to it. # 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. # Matcher that tests a value is of a specified type.
# The values are compared with the `Object#is_a?` method. # The values are compared with the `Object#is_a?` method.
struct TypeMatcher(Expected) < Matcher struct TypeMatcher(Expected) < Matcher
# Creates the type matcher. # Textual representation of what the matcher expects.
# The `Expected` type param will be used to populate the underlying label. # The `Expected` type param will be used to populate the label.
def initialize def label
super(Expected.to_s) Expected.to_s
end end
# Determines whether the matcher is satisfied with the value given to it. # 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. # 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`. # Sub-types must implement `Matcher#match?`, `Matcher#message`, and `Matcher#negated_message`.
abstract struct ValueMatcher(ExpectedType) < Matcher 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. # Expected value.
# Sub-types may use this value to test the expectation and generate message strings. # Sub-types may use this value to test the expectation and generate message strings.
getter expected getter expected
@ -12,15 +17,14 @@ module Spectator::Matchers
# Creates the value matcher. # Creates the value matcher.
# The label should be a string representation of the expectation. # The label should be a string representation of the expectation.
# The expected value is stored for later use. # The expected value is stored for later use.
def initialize(label : String, @expected : ExpectedType) def initialize(@label : String, @expected : ExpectedType)
super(label)
end end
# Creates the value matcher. # Creates the value matcher.
# The label is generated by calling `#to_s` on the expected value. # The label is generated by calling `#to_s` on the expected value.
# The expected value is stored for later use. # The expected value is stored for later use.
def initialize(@expected : ExpectedType) def initialize(expected : ExpectedType)
super(@expected.to_s) initialize(expected.to_s, expected)
end end
end end
end end