Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support strict keyword argument matching #554

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/mocha/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'mocha/ruby_version'

module Mocha
# Allows setting of configuration options. See {Configuration} for the available options.
#
Expand Down Expand Up @@ -43,7 +45,8 @@ class Configuration
:stubbing_non_public_method => :allow,
:stubbing_method_on_nil => :prevent,
:display_matching_invocations_on_failure => false,
:reinstate_undocumented_behaviour_from_v1_9 => true
:reinstate_undocumented_behaviour_from_v1_9 => true,
floehopper marked this conversation as resolved.
Show resolved Hide resolved
:strict_keyword_argument_matching => false
}.freeze

attr_reader :options
Expand Down Expand Up @@ -303,6 +306,17 @@ def reinstate_undocumented_behaviour_from_v1_9?
@options[:reinstate_undocumented_behaviour_from_v1_9]
end

# @private
def strict_keyword_argument_matching=(value)
raise 'Strict keyword argument matching requires Ruby 2.7 and above.' unless Mocha::RUBY_V27_PLUS
@options[:strict_keyword_argument_matching] = value
end

# @private
def strict_keyword_argument_matching?
@options[:strict_keyword_argument_matching]
end

class << self
# Allow the specified +action+.
#
Expand Down
1 change: 1 addition & 0 deletions lib/mocha/ruby_version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'mocha/deprecation'

module Mocha
RUBY_V27_PLUS = Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new('2.7')
end
22 changes: 22 additions & 0 deletions test/unit/configuration_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
require File.expand_path('../../test_helper', __FILE__)
require 'mocha/configuration'
require 'mocha/ruby_version'

class ConfigurationTest < Mocha::TestCase
def teardown
Mocha::Configuration.reset_configuration
end

floehopper marked this conversation as resolved.
Show resolved Hide resolved
def test_allow_temporarily_changes_config_when_given_block
Mocha.configure { |c| c.stubbing_method_unnecessarily = :warn }
yielded = false
Expand Down Expand Up @@ -34,4 +39,21 @@ def test_warn_when_temporarily_changes_config_when_given_block
assert yielded
assert_equal :allow, Mocha.configuration.stubbing_method_unnecessarily
end

def test_strict_keyword_argument_matching_works_is_false_by_default
assert_equal false, Mocha.configuration.strict_keyword_argument_matching?
end

if Mocha::RUBY_V27_PLUS
def test_enabling_strict_keyword_argument_matching_works_in_ruby_2_7_and_above
Mocha.configure { |c| c.strict_keyword_argument_matching = true }
assert_equal true, Mocha.configuration.strict_keyword_argument_matching?
wasabigeek marked this conversation as resolved.
Show resolved Hide resolved
end
else
def test_enabling_strict_keyword_argument_matching_raises_error_if_below_ruby_2_7
assert_raises(RuntimeError, 'Strict keyword argument matching requires Ruby 2.7 and above.') do
floehopper marked this conversation as resolved.
Show resolved Hide resolved
Mocha.configure { |c| c.strict_keyword_argument_matching = true }
end
end
end
end