Add test coverage for `Object#with_options` with `Hash`-like

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 `**`).
This commit is contained in:
Sean Doyle 2023-02-13 09:39:56 -05:00
parent d6eec533c1
commit 286fa403d6
2 changed files with 18 additions and 3 deletions

View File

@ -5,9 +5,9 @@ require "active_support/option_merger"
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:
#

View File

@ -114,6 +114,21 @@ class OptionMergerTest < ActiveSupport::TestCase
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)