2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2018-09-29 20:50:43 -04:00
|
|
|
require_relative "abstract_unit"
|
2016-08-06 12:03:25 -04:00
|
|
|
require "active_support/core_ext/object/with_options"
|
2005-12-15 15:03:23 -05:00
|
|
|
|
2012-01-05 20:12:46 -05:00
|
|
|
class OptionMergerTest < ActiveSupport::TestCase
|
2005-12-15 15:03:23 -05:00
|
|
|
def setup
|
2016-08-16 03:30:11 -04:00
|
|
|
@options = { hello: "world" }
|
2005-12-15 15:03:23 -05:00
|
|
|
end
|
2006-07-08 14:14:49 -04:00
|
|
|
|
2020-02-05 03:22:25 -05:00
|
|
|
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
|
|
|
|
|
2005-12-15 15:03:23 -05:00
|
|
|
def test_method_with_options_merges_options_when_options_are_present
|
2016-08-16 03:30:11 -04:00
|
|
|
local_options = { cool: true }
|
2006-07-08 14:14:49 -04:00
|
|
|
|
2005-12-15 15:03:23 -05:00
|
|
|
with_options(@options) do |o|
|
|
|
|
assert_equal local_options, method_with_options(local_options)
|
2020-02-05 03:22:25 -05:00
|
|
|
assert_equal @options.merge(local_options), o.method_with_options(local_options)
|
2019-09-13 05:06:37 -04:00
|
|
|
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)
|
2005-12-15 15:03:23 -05:00
|
|
|
end
|
|
|
|
end
|
2006-07-08 14:14:49 -04:00
|
|
|
|
2005-12-15 15:03:23 -05:00
|
|
|
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
|
2019-09-13 05:06:37 -04:00
|
|
|
assert_equal @options, o.method_with_kwargs
|
|
|
|
assert_equal @options, o.method_with_kwargs_only
|
2005-12-15 15:03:23 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-23 17:47:48 -04:00
|
|
|
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
|
|
|
|
|
2006-04-26 17:49:41 -04:00
|
|
|
def test_method_with_options_allows_to_overwrite_options
|
2016-08-16 03:30:11 -04:00
|
|
|
local_options = { hello: "moon" }
|
2006-04-26 17:49:41 -04:00
|
|
|
assert_equal @options.keys, local_options.keys
|
2006-07-08 14:14:49 -04:00
|
|
|
|
2006-04-26 17:49:41 -04:00
|
|
|
with_options(@options) do |o|
|
|
|
|
assert_equal local_options, method_with_options(local_options)
|
2020-02-05 03:22:25 -05:00
|
|
|
assert_equal @options.merge(local_options), o.method_with_options(local_options)
|
2006-04-26 17:49:41 -04:00
|
|
|
assert_equal local_options, o.method_with_options(local_options)
|
|
|
|
end
|
|
|
|
with_options(local_options) do |o|
|
2020-02-05 03:22:25 -05:00
|
|
|
assert_equal local_options.merge(@options), o.method_with_options(@options)
|
2006-04-26 17:49:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-07-13 21:53:41 -04:00
|
|
|
def test_nested_method_with_options_containing_hashes_merge
|
2016-08-06 13:38:33 -04:00
|
|
|
with_options conditions: { method: :get } do |outer|
|
|
|
|
outer.with_options conditions: { domain: "www" } do |inner|
|
|
|
|
expected = { conditions: { method: :get, domain: "www" } }
|
2008-07-13 21:53:41 -04:00
|
|
|
assert_equal expected, inner.method_with_options
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_nested_method_with_options_containing_hashes_overwrite
|
2016-08-06 13:38:33 -04:00
|
|
|
with_options conditions: { method: :get, domain: "www" } do |outer|
|
|
|
|
outer.with_options conditions: { method: :post } do |inner|
|
|
|
|
expected = { conditions: { method: :post, domain: "www" } }
|
2008-07-13 21:53:41 -04:00
|
|
|
assert_equal expected, inner.method_with_options
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_nested_method_with_options_containing_hashes_going_deep
|
2016-08-06 13:38:33 -04:00
|
|
|
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" } } }
|
2008-07-13 21:53:41 -04:00
|
|
|
assert_equal expected, inner.method_with_options
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2011-05-18 15:59:08 -04:00
|
|
|
def test_nested_method_with_options_using_lambda
|
2016-08-06 13:38:33 -04:00
|
|
|
local_lambda = lambda { { lambda: true } }
|
2008-11-15 10:48:14 -05:00
|
|
|
with_options(@options) do |o|
|
2020-02-05 03:22:25 -05:00
|
|
|
assert_equal @options.merge(local_lambda.call), o.method_with_options(local_lambda).call
|
2008-11-15 10:48:14 -05:00
|
|
|
end
|
|
|
|
end
|
2008-07-13 21:53:41 -04:00
|
|
|
|
2006-09-27 14:46:46 -04:00
|
|
|
# Needed when counting objects with the ObjectSpace
|
|
|
|
def test_option_merger_class_method
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal ActiveSupport::OptionMerger, ActiveSupport::OptionMerger.new("", "").class
|
2006-09-27 14:46:46 -04:00
|
|
|
end
|
|
|
|
|
2014-07-29 13:50:56 -04:00
|
|
|
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
|
|
|
|
|
2005-12-15 15:03:23 -05:00
|
|
|
private
|
|
|
|
def method_with_options(options = {})
|
|
|
|
options
|
|
|
|
end
|
2019-09-13 05:06:37 -04:00
|
|
|
|
|
|
|
def method_with_kwargs(*args, **options)
|
|
|
|
options
|
|
|
|
end
|
|
|
|
|
|
|
|
def method_with_kwargs_only(**options)
|
|
|
|
options
|
|
|
|
end
|
2005-12-15 15:03:23 -05:00
|
|
|
end
|