Skip to content

Commit

Permalink
Merge pull request #47376 from seanpdoyle/with-options-hash-like
Browse files Browse the repository at this point in the history
Add test coverage for `Object#with_options` with `Hash`-like argument
  • Loading branch information
guilleiguaran committed Feb 15, 2023
2 parents 26e21fc + 286fa40 commit ad71489
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
Expand Up @@ -5,9 +5,9 @@
class Object
# An elegant way to factor duplication out of options passed to a series of
# method calls. Each method called in the block, with the block variable as
# the receiver, will have its options merged with the default +options+ hash
# provided. Each method called on the block variable must take an options
# hash as its final argument.
# the receiver, will have its options merged with the default +options+
# <tt>Hash</tt> or <tt>Hash</tt>-like object provided. Each method called on
# the block variable must take an options hash as its final argument.
#
# Without <tt>with_options</tt>, this code contains duplication:
#
Expand Down
15 changes: 15 additions & 0 deletions activesupport/test/option_merger_test.rb
Expand Up @@ -114,6 +114,21 @@ def test_option_merger_implicit_receiver
assert_equal expected, @options
end

def test_with_options_hash_like
hash_like = Class.new do
delegate :to_hash, :deep_merge, to: :@hash

def initialize(hash)
@hash = hash
end
end
local_options = { "cool" => true }
scope = with_options(hash_like.new(@options))

assert_equal local_options, method_with_options(local_options)
assert_equal @options.merge(local_options), scope.method_with_options(local_options)
end

def test_with_options_no_block
local_options = { "cool" => true }
scope = with_options(@options)
Expand Down

0 comments on commit ad71489

Please sign in to comment.