Skip to content

Commit

Permalink
Add test coverage for Object#with_options with Hash-like
Browse files Browse the repository at this point in the history
Add test coverage for existing `Object#with_options` support for
`Hash`-like objects.

The current implementation expects "Hash-like" to mean that the argument
implements both `Hash#deep_merge` and `#to_hash` (to be called
explicitly with `#to_hash` and implicitly with `**`).
  • Loading branch information
seanpdoyle committed Feb 14, 2023
1 parent d6eec53 commit 286fa40
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 286fa40

Please sign in to comment.