Skip to content

Commit

Permalink
Use Ruby 1.9 hash syntax
Browse files Browse the repository at this point in the history
Fixes #537
  • Loading branch information
herwinw authored and floehopper committed Dec 25, 2022
1 parent e242033 commit 8bc0ad2
Show file tree
Hide file tree
Showing 34 changed files with 219 additions and 223 deletions.
4 changes: 0 additions & 4 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ Style/ExpandPathArguments:
Style/FormatStringToken:
EnforcedStyle: unannotated

# ruby19 style has only been supported since Ruby v1.9
Style/HashSyntax:
EnforcedStyle: hash_rockets

# I'm not keen on this cop, because it's easy to miss the conditional
# I think the results are particularly unhelpful when Metrics/LineLength is big
Style/IfUnlessModifier:
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def benchmark_test_case(klass, iterations)
unless @silent_option
begin
load 'test/unit/ui/console/outputlevel.rb' unless defined?(Test::Unit::UI::Console::OutputLevel::SILENT)
@silent_option = { :output_level => Test::Unit::UI::Console::OutputLevel::SILENT }
@silent_option = { output_level: Test::Unit::UI::Console::OutputLevel::SILENT }
rescue LoadError
@silent_option = Test::Unit::UI::SILENT
end
Expand Down
14 changes: 7 additions & 7 deletions lib/mocha/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ def self.configuration
class Configuration
# @private
DEFAULTS = {
:stubbing_method_unnecessarily => :allow,
:stubbing_method_on_non_mock_object => :allow,
:stubbing_non_existent_method => :allow,
:stubbing_non_public_method => :allow,
:stubbing_method_on_nil => :prevent,
:display_matching_invocations_on_failure => false,
:strict_keyword_argument_matching => false
stubbing_method_unnecessarily: :allow,
stubbing_method_on_non_mock_object: :allow,
stubbing_non_existent_method: :allow,
stubbing_non_public_method: :allow,
stubbing_method_on_nil: :prevent,
display_matching_invocations_on_failure: false,
strict_keyword_argument_matching: false
}.freeze

attr_reader :options
Expand Down
6 changes: 3 additions & 3 deletions lib/mocha/integration/test_unit/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ def self.description

# @private
def self.included(mod)
mod.setup :mocha_setup, :before => :prepend
mod.setup :mocha_setup, before: :prepend

mod.exception_handler(:handle_mocha_expectation_error)

mod.cleanup :after => :append do
mod.cleanup after: :append do
assertion_counter = Integration::AssertionCounter.new(self)
mocha_verify(assertion_counter)
end

mod.teardown :mocha_teardown, :after => :append
mod.teardown :mocha_teardown, after: :append
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/mocha/parameter_matchers/equivalent_uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def mocha_inspect
# @private
def explode(uri)
query_hash = CGI.parse(uri.query || '')
URI::Generic::COMPONENT.inject({}) { |h, k| h.merge(k => uri.__send__(k)) }.merge(:query => query_hash)
URI::Generic::COMPONENT.inject({}) { |h, k| h.merge(k => uri.__send__(k)) }.merge(query: query_hash)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions test/acceptance/expectations_on_multiple_methods_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def my_instance_method_2
end.new
test_result = run_as_test do
instance.expects(
:my_instance_method_1 => :new_return_value_1,
:my_instance_method_2 => :new_return_value_2
my_instance_method_1: :new_return_value_1,
my_instance_method_2: :new_return_value_2
)
assert_equal :new_return_value_1, instance.my_instance_method_1
assert_equal :new_return_value_2, instance.my_instance_method_2
Expand All @@ -44,8 +44,8 @@ def my_instance_method_2
end.new
test_result = run_as_test do
instance.stubs(
:my_instance_method_1 => :new_return_value_1,
:my_instance_method_2 => :new_return_value_2
my_instance_method_1: :new_return_value_1,
my_instance_method_2: :new_return_value_2
)
assert_equal :new_return_value_1, instance.my_instance_method_1
assert_equal :new_return_value_2, instance.my_instance_method_2
Expand Down
4 changes: 2 additions & 2 deletions test/acceptance/issue_524_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def teardown
def test_expects_returns_last_expectation
test_result = run_as_test do
object = mock
object.expects(:method_1 => 1, :method_2 => 2).twice
object.expects(method_1: 1, method_2: 2).twice
object.method_1
object.method_2
object.method_2
Expand All @@ -25,7 +25,7 @@ def test_expects_returns_last_expectation
def test_stubs_returns_last_expectation
test_result = run_as_test do
object = mock
object.stubs(:method_1 => 1, :method_2 => 2).twice
object.stubs(method_1: 1, method_2: 2).twice
object.method_1
object.method_2
object.method_2
Expand Down
64 changes: 32 additions & 32 deletions test/acceptance/keyword_argument_matching_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ def test_should_match_hash_parameter_with_keyword_args
test_name = __method__
test_result = run_as_test do
mock = mock()
mock.expects(:method).with(:key => 42); execution_point = ExecutionPoint.current
mock.expects(:method).with(key: 42); execution_point = ExecutionPoint.current
DeprecationDisabler.disable_deprecations do
mock.method({ :key => 42 }) # rubocop:disable Style/BracesAroundHashParameters
mock.method({ key: 42 }) # rubocop:disable Style/BracesAroundHashParameters
end
if Mocha::RUBY_V27_PLUS
location = "#{execution_point.file_name}:#{execution_point.line_number}:in `block in #{test_name}'"
Expand All @@ -36,9 +36,9 @@ def test_should_match_hash_parameter_with_keyword_args
def test_should_not_match_hash_parameter_with_keyword_args_when_strict_keyword_matching_is_enabled
test_result = run_as_test do
mock = mock()
mock.expects(:method).with(:key => 42)
Mocha::Configuration.override(:strict_keyword_argument_matching => true) do
mock.method({ :key => 42 }) # rubocop:disable Style/BracesAroundHashParameters
mock.expects(:method).with(key: 42)
Mocha::Configuration.override(strict_keyword_argument_matching: true) do
mock.method({ key: 42 }) # rubocop:disable Style/BracesAroundHashParameters
end
end
assert_failed(test_result)
Expand All @@ -49,9 +49,9 @@ def test_should_match_hash_parameter_with_splatted_keyword_args
test_name = __method__
test_result = run_as_test do
mock = mock()
mock.expects(:method).with(**{ :key => 42 }); execution_point = ExecutionPoint.current
mock.expects(:method).with(**{ key: 42 }); execution_point = ExecutionPoint.current
DeprecationDisabler.disable_deprecations do
mock.method({ :key => 42 }) # rubocop:disable Style/BracesAroundHashParameters
mock.method({ key: 42 }) # rubocop:disable Style/BracesAroundHashParameters
end
if Mocha::RUBY_V27_PLUS
location = "#{execution_point.file_name}:#{execution_point.line_number}:in `block in #{test_name}'"
Expand All @@ -66,9 +66,9 @@ def test_should_match_hash_parameter_with_splatted_keyword_args
def test_should_not_match_hash_parameter_with_splatted_keyword_args_when_strict_keyword_matching_is_enabled
test_result = run_as_test do
mock = mock()
mock.expects(:method).with(**{ :key => 42 })
Mocha::Configuration.override(:strict_keyword_argument_matching => true) do
mock.method({ :key => 42 }) # rubocop:disable Style/BracesAroundHashParameters
mock.expects(:method).with(**{ key: 42 })
Mocha::Configuration.override(strict_keyword_argument_matching: true) do
mock.method({ key: 42 }) # rubocop:disable Style/BracesAroundHashParameters
end
end
assert_failed(test_result)
Expand All @@ -78,17 +78,17 @@ def test_should_not_match_hash_parameter_with_splatted_keyword_args_when_strict_
def test_should_match_splatted_hash_parameter_with_keyword_args
test_result = run_as_test do
mock = mock()
mock.expects(:method).with(:key => 42)
mock.method(**{ :key => 42 })
mock.expects(:method).with(key: 42)
mock.method(**{ key: 42 })
end
assert_passed(test_result)
end

def test_should_match_splatted_hash_parameter_with_splatted_hash
test_result = run_as_test do
mock = mock()
mock.expects(:method).with(**{ :key => 42 })
mock.method(**{ :key => 42 })
mock.expects(:method).with(**{ key: 42 })
mock.method(**{ key: 42 })
end
assert_passed(test_result)
end
Expand All @@ -97,9 +97,9 @@ def test_should_match_positional_and_keyword_args_with_last_positional_hash
test_name = __method__
test_result = run_as_test do
mock = mock()
mock.expects(:method).with(1, { :key => 42 }); execution_point = ExecutionPoint.current # rubocop:disable Style/BracesAroundHashParameters
mock.expects(:method).with(1, { key: 42 }); execution_point = ExecutionPoint.current # rubocop:disable Style/BracesAroundHashParameters
DeprecationDisabler.disable_deprecations do
mock.method(1, :key => 42)
mock.method(1, key: 42)
end
if Mocha::RUBY_V27_PLUS
location = "#{execution_point.file_name}:#{execution_point.line_number}:in `block in #{test_name}'"
Expand All @@ -114,9 +114,9 @@ def test_should_match_positional_and_keyword_args_with_last_positional_hash
def test_should_not_match_positional_and_keyword_args_with_last_positional_hash_when_strict_keyword_args_is_enabled
test_result = run_as_test do
mock = mock()
mock.expects(:method).with(1, { :key => 42 }) # rubocop:disable Style/BracesAroundHashParameters
Mocha::Configuration.override(:strict_keyword_argument_matching => true) do
mock.method(1, :key => 42)
mock.expects(:method).with(1, { key: 42 }) # rubocop:disable Style/BracesAroundHashParameters
Mocha::Configuration.override(strict_keyword_argument_matching: true) do
mock.method(1, key: 42)
end
end
assert_failed(test_result)
Expand All @@ -127,9 +127,9 @@ def test_should_match_last_positional_hash_with_keyword_args
test_name = __method__
test_result = run_as_test do
mock = mock()
mock.expects(:method).with(1, :key => 42); execution_point = ExecutionPoint.current
mock.expects(:method).with(1, key: 42); execution_point = ExecutionPoint.current
DeprecationDisabler.disable_deprecations do
mock.method(1, { :key => 42 }) # rubocop:disable Style/BracesAroundHashParameters
mock.method(1, { key: 42 }) # rubocop:disable Style/BracesAroundHashParameters
end
if Mocha::RUBY_V27_PLUS
location = "#{execution_point.file_name}:#{execution_point.line_number}:in `block in #{test_name}'"
Expand All @@ -144,9 +144,9 @@ def test_should_match_last_positional_hash_with_keyword_args
def test_should_not_match_last_positional_hash_with_keyword_args_when_strict_keyword_args_is_enabled
test_result = run_as_test do
mock = mock()
mock.expects(:method).with(1, :key => 42)
Mocha::Configuration.override(:strict_keyword_argument_matching => true) do
mock.method(1, { :key => 42 }) # rubocop:disable Style/BracesAroundHashParameters
mock.expects(:method).with(1, key: 42)
Mocha::Configuration.override(strict_keyword_argument_matching: true) do
mock.method(1, { key: 42 }) # rubocop:disable Style/BracesAroundHashParameters
end
end
assert_failed(test_result)
Expand All @@ -156,8 +156,8 @@ def test_should_not_match_last_positional_hash_with_keyword_args_when_strict_key
def test_should_match_positional_and_keyword_args_with_keyword_args
test_result = run_as_test do
mock = mock()
mock.expects(:method).with(1, :key => 42)
mock.method(1, :key => 42)
mock.expects(:method).with(1, key: 42)
mock.method(1, key: 42)
end
assert_passed(test_result)
end
Expand All @@ -166,7 +166,7 @@ def test_should_match_hash_parameter_with_hash_matcher
test_result = run_as_test do
mock = mock()
mock.expects(:method).with(has_key(:key))
mock.method({ :key => 42 }) # rubocop:disable Style/BracesAroundHashParameters
mock.method({ key: 42 }) # rubocop:disable Style/BracesAroundHashParameters
end
assert_passed(test_result)
end
Expand All @@ -175,7 +175,7 @@ def test_should_match_splatted_hash_parameter_with_hash_matcher
test_result = run_as_test do
mock = mock()
mock.expects(:method).with(has_key(:key))
mock.method(**{ :key => 42 })
mock.method(**{ key: 42 })
end
assert_passed(test_result)
end
Expand All @@ -184,7 +184,7 @@ def test_should_match_positional_and_keyword_args_with_hash_matcher
test_result = run_as_test do
mock = mock()
mock.expects(:method).with(1, has_key(:key))
mock.method(1, :key => 42)
mock.method(1, key: 42)
end
assert_passed(test_result)
end
Expand All @@ -193,7 +193,7 @@ def test_should_match_last_positional_hash_with_hash_matcher
test_result = run_as_test do
mock = mock()
mock.expects(:method).with(1, has_key(:key))
mock.method(1, { :key => 42 }) # rubocop:disable Style/BracesAroundHashParameters
mock.method(1, { key: 42 }) # rubocop:disable Style/BracesAroundHashParameters
end
assert_passed(test_result)
end
Expand All @@ -202,8 +202,8 @@ def test_should_match_last_positional_hash_with_hash_matcher
def test_should_not_match_non_hash_args_with_keyword_args
test_result = run_as_test do
mock = mock()
mock.expects(:method).with(**{ :key => 1 })
Mocha::Configuration.override(:strict_keyword_argument_matching => true) do
mock.expects(:method).with(**{ key: 1 })
Mocha::Configuration.override(strict_keyword_argument_matching: true) do
mock.method([2])
end
end
Expand Down
18 changes: 9 additions & 9 deletions test/acceptance/mock_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def test_should_build_symbol_named_mock_and_explicitly_add_an_expectation_which_

def test_should_build_mock_incorporating_two_expectations_which_are_satisifed
test_result = run_as_test do
foo = mock(:bar => 'bar', :baz => 'baz')
foo = mock(bar: 'bar', baz: 'baz')
foo.bar
foo.baz
end
Expand All @@ -74,23 +74,23 @@ def test_should_build_mock_incorporating_two_expectations_which_are_satisifed

def test_should_build_mock_incorporating_two_expectations_the_first_of_which_is_not_satisifed
test_result = run_as_test do
foo = mock(:bar => 'bar', :baz => 'baz')
foo = mock(bar: 'bar', baz: 'baz')
foo.baz
end
assert_failed(test_result)
end

def test_should_build_mock_incorporating_two_expectations_the_second_of_which_is_not_satisifed
test_result = run_as_test do
foo = mock(:bar => 'bar', :baz => 'baz')
foo = mock(bar: 'bar', baz: 'baz')
foo.bar
end
assert_failed(test_result)
end

def test_should_build_string_named_mock_incorporating_two_expectations_which_are_satisifed
test_result = run_as_test do
foo = mock('foo', :bar => 'bar', :baz => 'baz')
foo = mock('foo', bar: 'bar', baz: 'baz')
foo.bar
foo.baz
end
Expand All @@ -99,7 +99,7 @@ def test_should_build_string_named_mock_incorporating_two_expectations_which_are

def test_should_build_symbol_named_mock_incorporating_two_expectations_which_are_satisifed
test_result = run_as_test do
foo = mock(:foo, :bar => 'bar', :baz => 'baz')
foo = mock(:foo, bar: 'bar', baz: 'baz')
foo.bar
foo.baz
end
Expand All @@ -108,31 +108,31 @@ def test_should_build_symbol_named_mock_incorporating_two_expectations_which_are

def test_should_build_string_named_mock_incorporating_two_expectations_the_first_of_which_is_not_satisifed
test_result = run_as_test do
foo = mock('foo', :bar => 'bar', :baz => 'baz')
foo = mock('foo', bar: 'bar', baz: 'baz')
foo.baz
end
assert_failed(test_result)
end

def test_should_build_symbol_named_mock_incorporating_two_expectations_the_first_of_which_is_not_satisifed
test_result = run_as_test do
foo = mock(:foo, :bar => 'bar', :baz => 'baz')
foo = mock(:foo, bar: 'bar', baz: 'baz')
foo.baz
end
assert_failed(test_result)
end

def test_should_build_string_named_mock_incorporating_two_expectations_the_second_of_which_is_not_satisifed
test_result = run_as_test do
foo = mock('foo', :bar => 'bar', :baz => 'baz')
foo = mock('foo', bar: 'bar', baz: 'baz')
foo.bar
end
assert_failed(test_result)
end

def test_should_build_symbol_named_mock_incorporating_two_expectations_the_second_of_which_is_not_satisifed
test_result = run_as_test do
foo = mock(:foo, :bar => 'bar', :baz => 'baz')
foo = mock(:foo, bar: 'bar', baz: 'baz')
foo.bar
end
assert_failed(test_result)
Expand Down
4 changes: 2 additions & 2 deletions test/acceptance/multiple_yielding_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ def test_yields_values_multiple_times_when_stubbed_method_is_invoked
def test_yields_values_multiple_times_when_multiple_yields_arguments_are_not_arrays
test_result = run_as_test do
m = mock('m')
m.stubs(:foo).multiple_yields(1, { :b => 2 }, '3')
m.stubs(:foo).multiple_yields(1, { b: 2 }, '3')
yielded = []
m.foo { |*args| yielded << args }
assert_equal [[1], [{ :b => 2 }], ['3']], yielded
assert_equal [[1], [{ b: 2 }], ['3']], yielded
end
assert_passed(test_result)
end
Expand Down

0 comments on commit 8bc0ad2

Please sign in to comment.