From 59cf9395362060504eb6d220a9646bbbccc34557 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Fri, 19 Jul 2019 12:07:50 -0600 Subject: [PATCH] Don't store initial value in matcher --- src/spectator/matchers/change_from_matcher.cr | 27 +++++++++++-------- src/spectator/matchers/change_matcher.cr | 25 ++++++++--------- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/src/spectator/matchers/change_from_matcher.cr b/src/spectator/matchers/change_from_matcher.cr index 858fa6a..b41fdf3 100644 --- a/src/spectator/matchers/change_from_matcher.cr +++ b/src/spectator/matchers/change_from_matcher.cr @@ -2,31 +2,36 @@ require "./value_matcher" module Spectator::Matchers # Matcher that tests whether an expression changed from a specific value. - struct ChangeFromMatcher(ExpressionType, FromType) < ValueMatcher(ExpressionType) + struct ChangeFromMatcher(ExpressionType, FromType) < 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 : String + # Determines whether the matcher is satisfied with the partial given to it. # `MatchData` is returned that contains information about the match. def match(partial) - partial.actual # Invoke action that might change the expression's value. - after = @expression.call # Retrieve the expression's value. - if expected != @actual_before + before = @expression.call # Retrieve the expression's initial value. + partial.actual # Invoke action that might change the expression's value. + after = @expression.call # Retrieve the expression's value again. + if @expected_before != before # Initial value isn't what was expected. - InitialMatchData.new(expected, @actual_before, after, partial.label, label) + InitialMatchData.new(@expected_before, before, after, partial.label, label) else # Check if the expression's value changed. - matched = expected != after - ChangeMatchData.new(matched, expected, @actual_before, after, partial.label, label) + same = before == after + ChangeMatchData.new(!same, @expected_before, before, after, partial.label, label) end end # Creates a new change matcher with a custom label. - def initialize(expression_label : String, expected_before : FromType, @actual_before : ExpressionType, &expression : -> ExpressionType) - super(expected_before, expression_label) + def initialize(@label, @expected_before : FromType, &expression : -> ExpressionType) @expression = expression end # Creates a new change matcher. - def initialize(expected_before : FromType, @actual_before : ExpressionType, &expression : -> ExpressionType) - super(expected_before, expression.to_s) + def initialize(@expected_before : FromType, &expression : -> ExpressionType) + @label = expression.to_s @expression = expression end diff --git a/src/spectator/matchers/change_matcher.cr b/src/spectator/matchers/change_matcher.cr index 64dda8e..72aa1f0 100644 --- a/src/spectator/matchers/change_matcher.cr +++ b/src/spectator/matchers/change_matcher.cr @@ -3,35 +3,36 @@ require "./value_matcher" module Spectator::Matchers # Matcher that tests whether an expression changed. - struct ChangeMatcher(ExpressionType) < ValueMatcher(ExpressionType) - # Determines whether the matcher is satisfied with the value given to it. - private def match?(after) - expected != after - end + struct ChangeMatcher(ExpressionType) < 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 : String # Determines whether the matcher is satisfied with the partial given to it. # `MatchData` is returned that contains information about the match. def match(partial) - partial.actual # Invoke action that might change the expression's value. - after = @expression.call # Retrieve the expression's value. - MatchData.new(match?(after), expected, after, partial.label, label) + before = @expression.call # Retrieve the expression's initial value. + partial.actual # Invoke action that might change the expression's value. + after = @expression.call # Retrieve the expression's value again. + same = before == after # Did the value change? + MatchData.new(!same, before, after, partial.label, label) end # Creates a new change matcher with a custom label. - def initialize(label : String, &expression : -> ExpressionType) - super(yield, label) + def initialize(@label, &expression : -> ExpressionType) @expression = expression end # Creates a new change matcher. def initialize(&expression : -> ExpressionType) - super(yield, expression.to_s) + @label = expression.to_s @expression = expression end # Specifies what the initial value of the expression must be. def from(value : T) forall T - ChangeFromMatcher.new(label, value, expected, &@expression) + ChangeFromMatcher.new(label, value, &@expression) end # Match data specific to this matcher.