lib/rspec/expectations/expectation_target.rb in rspec-expectations-3.10.2 vs lib/rspec/expectations/expectation_target.rb in rspec-expectations-3.11.0

- old
+ new

@@ -40,11 +40,11 @@ end BlockExpectationTarget.new(block) elsif block raise ArgumentError, "You cannot pass both an argument and a block to `expect`." else - new(value) + ValueExpectationTarget.new(value) end end # Defines instance {ExpectationTarget} instance methods. These are defined # in a module so we can include it in `Minitest::Expectation` when @@ -86,9 +86,47 @@ "so you must pass a matcher to `##{verb}`." end end include InstanceMethods + end + + # @private + # Validates the provided matcher to ensure it supports block + # expectations, in order to avoid user confusion when they + # use a block thinking the expectation will be on the return + # value of the block rather than the block itself. + class ValueExpectationTarget < ExpectationTarget + def to(matcher=nil, message=nil, &block) + enforce_value_expectation(matcher) + super + end + + def not_to(matcher=nil, message=nil, &block) + enforce_value_expectation(matcher) + super + end + + private + + def enforce_value_expectation(matcher) + return if supports_value_expectations?(matcher) + + RSpec.deprecate( + "expect(value).to #{RSpec::Support::ObjectFormatter.format(matcher)}", + :message => + "The implicit block expectation syntax is deprecated, you should pass " \ + "a block rather than an argument to `expect` to use the provided " \ + "block expectation matcher or the matcher must implement " \ + "`supports_value_expectations?`. e.g `expect { value }.to " \ + "#{RSpec::Support::ObjectFormatter.format(matcher)}` not " \ + "`expect(value).to #{RSpec::Support::ObjectFormatter.format(matcher)}`" + ) + end + + def supports_value_expectations?(matcher) + !matcher.respond_to?(:supports_value_expectations?) || matcher.supports_value_expectations? + end end # @private # Validates the provided matcher to ensure it supports block # expectations, in order to avoid user confusion when they