This repository was archived by the owner on Nov 30, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 385
Factorize common DynamicPredicate base. #1197
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,9 +2,11 @@ module RSpec | |
| module Matchers | ||
| module BuiltIn | ||
| # @api private | ||
| # Provides the implementation for `has_<predicate>`. | ||
| # Not intended to be instantiated directly. | ||
| class Has < BaseMatcher | ||
| # Provides the implementation for dynamic predicate matchers. | ||
| # Not intended to be inherited directly. | ||
| class DynamicPredicate < BaseMatcher | ||
| include BeHelpers | ||
|
|
||
| if RSpec::Support::RubyFeatures.kw_args_supported? | ||
| binding.eval(<<-CODE, __FILE__, __LINE__) | ||
| def initialize(method_name, *args, **kwargs, &block) | ||
|
|
@@ -34,25 +36,25 @@ def does_not_match?(actual, &block) | |
| # @api private | ||
| # @return [String] | ||
| def failure_message | ||
| validity_message || "expected ##{predicate}#{failure_message_args_description} to return true, got false" | ||
| failure_message_expecting(true) | ||
| end | ||
|
|
||
| # @api private | ||
| # @return [String] | ||
| def failure_message_when_negated | ||
| validity_message || "expected ##{predicate}#{failure_message_args_description} to return false, got true" | ||
| failure_message_expecting(false) | ||
| end | ||
|
|
||
| # @api private | ||
| # @return [String] | ||
| def description | ||
| [method_description, args_description].compact.join(' ') | ||
| "#{method_description}#{args_to_sentence}" | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def predicate_accessible? | ||
| !private_predicate? && predicate_exists? | ||
| @actual.respond_to? predicate | ||
| end | ||
JonRowe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # support 1.8.7, evaluate once at load time for performance | ||
|
|
@@ -68,38 +70,87 @@ def private_predicate? | |
| end | ||
| end | ||
|
|
||
| def predicate_exists? | ||
| @actual.respond_to? predicate | ||
| end | ||
|
|
||
| if RSpec::Support::RubyFeatures.kw_args_supported? | ||
| binding.eval(<<-CODE, __FILE__, __LINE__) | ||
| def predicate_matches? | ||
| def predicate_result | ||
| if @kwargs.empty? | ||
| @actual.__send__(predicate, *@args, &@block) | ||
| @predicate_result = actual.__send__(predicate_method_name, *@args, &@block) | ||
| else | ||
| @actual.__send__(predicate, *@args, **@kwargs, &@block) | ||
| @predicate_result = actual.__send__(predicate_method_name, *@args, **@kwargs, &@block) | ||
| end | ||
| end | ||
| CODE | ||
| else | ||
| def predicate_matches? | ||
| @actual.__send__(predicate, *@args, &@block) | ||
| def predicate_result | ||
| @predicate_result = actual.__send__(predicate_method_name, *@args, &@block) | ||
| end | ||
| end | ||
|
|
||
| def predicate | ||
| def predicate_method_name | ||
| predicate | ||
| end | ||
|
Comment on lines
+89
to
+91
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like this should be in the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| def predicate_matches? | ||
| !!predicate_result | ||
| end | ||
|
|
||
| def root | ||
| # On 1.9, there appears to be a bug where String#match can return `false` | ||
| # rather than the match data object. Changing to Regex#match appears to | ||
| # work around this bug. For an example of this bug, see: | ||
| # https://travis-ci.org/rspec/rspec-expectations/jobs/27549635 | ||
| @predicate ||= :"has_#{Matchers::HAS_REGEX.match(@method_name.to_s).captures.first}?" | ||
| self.class::REGEX.match(@method_name.to_s).captures.first | ||
| end | ||
|
|
||
| def method_description | ||
| @method_name.to_s.tr('_', ' ') | ||
| EnglishPhrasing.split_words(@method_name) | ||
| end | ||
|
|
||
| def failure_message_expecting(value) | ||
| validity_message || | ||
| "expected `#{actual_formatted}.#{predicate}#{args_to_s}` to return #{value}, got #{description_of @predicate_result}" | ||
| end | ||
|
|
||
| def validity_message | ||
| return nil if predicate_accessible? | ||
|
|
||
| "expected #{actual_formatted} to respond to `#{predicate}`#{failure_to_respond_explanation}" | ||
| end | ||
|
|
||
| def failure_to_respond_explanation | ||
| if private_predicate? | ||
| " but `#{predicate}` is a private method" | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # @api private | ||
| # Provides the implementation for `has_<predicate>`. | ||
| # Not intended to be instantiated directly. | ||
| class Has < DynamicPredicate | ||
| # :nodoc: | ||
| REGEX = Matchers::HAS_REGEX | ||
|
|
||
| # @api private | ||
| # @return [String] | ||
| def failure_message | ||
| validity_message || "expected ##{predicate}#{failure_message_args_description} to return true, got false" | ||
| end | ||
|
|
||
| # @api private | ||
| # @return [String] | ||
| def failure_message_when_negated | ||
| validity_message || "expected ##{predicate}#{failure_message_args_description} to return false, got true" | ||
| end | ||
|
|
||
| # @api private | ||
| # @return [String] | ||
| def description | ||
| [method_description, args_description].compact.join(' ') | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def args_description | ||
| return nil if @args.empty? | ||
| @args.map { |arg| RSpec::Support::ObjectFormatter.format(arg) }.join(', ') | ||
|
|
@@ -110,12 +161,40 @@ def failure_message_args_description | |
| "(#{desc})" if desc | ||
| end | ||
|
|
||
| def validity_message | ||
| if private_predicate? | ||
| "expected #{@actual} to respond to `#{predicate}` but `#{predicate}` is a private method" | ||
| elsif !predicate_exists? | ||
| "expected #{@actual} to respond to `#{predicate}`" | ||
| end | ||
| def predicate | ||
| @predicate ||= :"has_#{root}?" | ||
| end | ||
| end | ||
|
|
||
| # @api private | ||
| # Provides the implementation of `be_<predicate>`. | ||
| # Not intended to be instantiated directly. | ||
| class BePredicate < DynamicPredicate | ||
| # :nodoc: | ||
| REGEX = Matchers::BE_PREDICATE_REGEX | ||
| private | ||
| def predicate | ||
| @predicate ||= :"#{root}?" | ||
| end | ||
|
|
||
| def predicate_method_name | ||
| actual.respond_to?(predicate) ? predicate : present_tense_predicate | ||
| end | ||
|
|
||
| def failure_to_respond_explanation | ||
| super || if predicate == :true? | ||
| " or perhaps you meant `be true` or `be_truthy`" | ||
| elsif predicate == :false? | ||
| " or perhaps you meant `be false` or `be_falsey`" | ||
| end | ||
| end | ||
|
|
||
| def predicate_accessible? | ||
| super || actual.respond_to?(present_tense_predicate) | ||
| end | ||
|
|
||
| def present_tense_predicate | ||
| :"#{root}s?" | ||
| end | ||
| end | ||
| end | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.