From 226708cb820dc655070e395193a77e12cf0080e7 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Thu, 28 Feb 2019 14:48:46 -0700 Subject: [PATCH] Abstract Matcher#label getter There's no need to store a value for this. Some matchers have a static label. --- src/spectator/matchers/empty_matcher.cr | 6 +++--- src/spectator/matchers/matcher.cr | 6 +----- src/spectator/matchers/nil_matcher.cr | 6 +++--- src/spectator/matchers/predicate_matcher.cr | 6 +++--- src/spectator/matchers/type_matcher.cr | 8 ++++---- src/spectator/matchers/value_matcher.cr | 12 ++++++++---- 6 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/spectator/matchers/empty_matcher.cr b/src/spectator/matchers/empty_matcher.cr index 2d2276d..1f9db96 100644 --- a/src/spectator/matchers/empty_matcher.cr +++ b/src/spectator/matchers/empty_matcher.cr @@ -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. diff --git a/src/spectator/matchers/matcher.cr b/src/spectator/matchers/matcher.cr index 44416a3..8586918 100644 --- a/src/spectator/matchers/matcher.cr +++ b/src/spectator/matchers/matcher.cr @@ -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. diff --git a/src/spectator/matchers/nil_matcher.cr b/src/spectator/matchers/nil_matcher.cr index 67a67b5..ccc6627 100644 --- a/src/spectator/matchers/nil_matcher.cr +++ b/src/spectator/matchers/nil_matcher.cr @@ -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. diff --git a/src/spectator/matchers/predicate_matcher.cr b/src/spectator/matchers/predicate_matcher.cr index fe9dc20..d5a7f2c 100644 --- a/src/spectator/matchers/predicate_matcher.cr +++ b/src/spectator/matchers/predicate_matcher.cr @@ -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. diff --git a/src/spectator/matchers/type_matcher.cr b/src/spectator/matchers/type_matcher.cr index e7048bd..b670369 100644 --- a/src/spectator/matchers/type_matcher.cr +++ b/src/spectator/matchers/type_matcher.cr @@ -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. diff --git a/src/spectator/matchers/value_matcher.cr b/src/spectator/matchers/value_matcher.cr index 920beb6..510741e 100644 --- a/src/spectator/matchers/value_matcher.cr +++ b/src/spectator/matchers/value_matcher.cr @@ -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