1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
# frozen_string_literal: true
require_relative "abstract_unit"
require "active_support/core_ext/object/with_options"
class OptionMergerTest < ActiveSupport::TestCase
def setup
@options = { hello: "world" }
end
def test_method_with_options_merges_string_options
local_options = { "cool" => true }
with_options(@options) do |o|
assert_equal local_options, method_with_options(local_options)
assert_equal @options.merge(local_options), o.method_with_options(local_options)
end
end
def test_method_with_options_merges_options_when_options_are_present
local_options = { cool: true }
with_options(@options) do |o|
assert_equal local_options, method_with_options(local_options)
assert_equal @options.merge(local_options), o.method_with_options(local_options)
assert_equal @options.merge(local_options), o.method_with_kwargs(local_options)
assert_equal @options.merge(local_options), o.method_with_kwargs_only(local_options)
end
end
def test_method_with_options_appends_options_when_options_are_missing
with_options(@options) do |o|
assert_equal Hash.new, method_with_options
assert_equal @options, o.method_with_options
assert_equal @options, o.method_with_kwargs
assert_equal @options, o.method_with_kwargs_only
end
end
def test_method_with_options_copies_options_when_options_are_missing
with_options(@options) do |o|
assert_not_same @options, o.method_with_options
end
end
def test_method_with_options_allows_to_overwrite_options
local_options = { hello: "moon" }
assert_equal @options.keys, local_options.keys
with_options(@options) do |o|
assert_equal local_options, method_with_options(local_options)
assert_equal @options.merge(local_options), o.method_with_options(local_options)
assert_equal local_options, o.method_with_options(local_options)
end
with_options(local_options) do |o|
assert_equal local_options.merge(@options), o.method_with_options(@options)
end
end
def test_nested_method_with_options_containing_hashes_merge
with_options conditions: { method: :get } do |outer|
outer.with_options conditions: { domain: "www" } do |inner|
expected = { conditions: { method: :get, domain: "www" } }
assert_equal expected, inner.method_with_options
end
end
end
def test_nested_method_with_options_containing_hashes_overwrite
with_options conditions: { method: :get, domain: "www" } do |outer|
outer.with_options conditions: { method: :post } do |inner|
expected = { conditions: { method: :post, domain: "www" } }
assert_equal expected, inner.method_with_options
end
end
end
def test_nested_method_with_options_containing_hashes_going_deep
with_options html: { class: "foo", style: { margin: 0, display: "block" } } do |outer|
outer.with_options html: { title: "bar", style: { margin: "1em", color: "#fff" } } do |inner|
expected = { html: { class: "foo", title: "bar", style: { margin: "1em", display: "block", color: "#fff" } } }
assert_equal expected, inner.method_with_options
end
end
end
def test_nested_method_with_options_using_lambda_as_only_argument
local_lambda = lambda { { lambda: true } }
with_options(@options) do |o|
assert_equal @options.merge(local_lambda.call), o.method_with_options(local_lambda).call
end
end
def test_proc_as_first_argument_with_other_options_should_still_merge_options
local_proc = proc { }
local_options = { "cool" => true }
with_options(@options) do |o|
assert_equal [local_proc, @options.merge(local_options)], o.method_with_args(local_proc, local_options)
end
end
# Needed when counting objects with the ObjectSpace
def test_option_merger_class_method
assert_equal ActiveSupport::OptionMerger, ActiveSupport::OptionMerger.new("", "").class
end
def test_option_merger_implicit_receiver
@options.with_options foo: "bar" do
merge! fizz: "buzz"
end
expected = { hello: "world", foo: "bar", fizz: "buzz" }
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)
assert_equal local_options, method_with_options(local_options)
assert_equal @options.merge(local_options), scope.method_with_options(local_options)
end
private
def method_with_args(*args)
args
end
def method_with_options(options = {})
options
end
def method_with_kwargs(*args, **options)
options
end
def method_with_kwargs_only(**options)
options
end
end
|