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/ordered_options"
|
2005-09-30 02:51:42 -04:00
|
|
|
|
2012-01-05 20:12:46 -05:00
|
|
|
class OrderedOptionsTest < ActiveSupport::TestCase
|
2005-09-30 02:51:42 -04:00
|
|
|
def test_usage
|
2008-06-03 14:32:53 -04:00
|
|
|
a = ActiveSupport::OrderedOptions.new
|
2005-09-30 02:51:42 -04:00
|
|
|
|
|
|
|
assert_nil a[:not_set]
|
|
|
|
|
2013-03-30 04:31:42 -04:00
|
|
|
a[:allow_concurrency] = true
|
2005-09-30 02:51:42 -04:00
|
|
|
assert_equal 1, a.size
|
2013-03-30 04:31:42 -04:00
|
|
|
assert a[:allow_concurrency]
|
2005-09-30 02:51:42 -04:00
|
|
|
|
2013-03-30 04:31:42 -04:00
|
|
|
a[:allow_concurrency] = false
|
2005-09-30 02:51:42 -04:00
|
|
|
assert_equal 1, a.size
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not a[:allow_concurrency]
|
2005-09-30 02:51:42 -04:00
|
|
|
|
|
|
|
a["else_where"] = 56
|
|
|
|
assert_equal 2, a.size
|
|
|
|
assert_equal 56, a[:else_where]
|
|
|
|
end
|
2006-07-08 14:14:49 -04:00
|
|
|
|
2005-09-30 03:02:28 -04:00
|
|
|
def test_looping
|
2008-06-03 14:32:53 -04:00
|
|
|
a = ActiveSupport::OrderedOptions.new
|
2005-09-30 03:02:28 -04:00
|
|
|
|
2013-03-30 04:31:42 -04:00
|
|
|
a[:allow_concurrency] = true
|
2005-09-30 03:02:28 -04:00
|
|
|
a["else_where"] = 56
|
2006-07-08 14:14:49 -04:00
|
|
|
|
2013-03-30 04:31:42 -04:00
|
|
|
test = [[:allow_concurrency, true], [:else_where, 56]]
|
2006-07-08 14:14:49 -04:00
|
|
|
|
2005-09-30 03:02:28 -04:00
|
|
|
a.each_with_index do |(key, value), index|
|
|
|
|
assert_equal test[index].first, key
|
|
|
|
assert_equal test[index].last, value
|
|
|
|
end
|
|
|
|
end
|
2006-07-08 14:14:49 -04:00
|
|
|
|
2005-09-30 03:14:37 -04:00
|
|
|
def test_method_access
|
2008-06-03 14:32:53 -04:00
|
|
|
a = ActiveSupport::OrderedOptions.new
|
2005-09-30 03:14:37 -04:00
|
|
|
|
|
|
|
assert_nil a.not_set
|
|
|
|
|
2013-03-30 04:31:42 -04:00
|
|
|
a.allow_concurrency = true
|
2005-09-30 03:14:37 -04:00
|
|
|
assert_equal 1, a.size
|
2013-03-30 04:31:42 -04:00
|
|
|
assert a.allow_concurrency
|
2005-09-30 03:14:37 -04:00
|
|
|
|
2013-03-30 04:31:42 -04:00
|
|
|
a.allow_concurrency = false
|
2005-09-30 03:14:37 -04:00
|
|
|
assert_equal 1, a.size
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not a.allow_concurrency
|
2005-09-30 03:14:37 -04:00
|
|
|
|
|
|
|
a.else_where = 56
|
|
|
|
assert_equal 2, a.size
|
|
|
|
assert_equal 56, a.else_where
|
|
|
|
end
|
2011-03-25 15:48:52 -04:00
|
|
|
|
|
|
|
def test_inheritable_options_continues_lookup_in_parent
|
|
|
|
parent = ActiveSupport::OrderedOptions.new
|
|
|
|
parent[:foo] = true
|
|
|
|
|
|
|
|
child = ActiveSupport::InheritableOptions.new(parent)
|
|
|
|
assert child.foo
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_inheritable_options_can_override_parent
|
|
|
|
parent = ActiveSupport::OrderedOptions.new
|
|
|
|
parent[:foo] = :bar
|
|
|
|
|
|
|
|
child = ActiveSupport::InheritableOptions.new(parent)
|
|
|
|
child[:foo] = :baz
|
|
|
|
|
|
|
|
assert_equal :baz, child.foo
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_inheritable_options_inheritable_copy
|
|
|
|
original = ActiveSupport::InheritableOptions.new
|
|
|
|
copy = original.inheritable_copy
|
|
|
|
|
|
|
|
assert copy.kind_of?(original.class)
|
|
|
|
assert_not_equal copy.object_id, original.object_id
|
|
|
|
end
|
2012-05-05 02:24:57 -04:00
|
|
|
|
|
|
|
def test_introspection
|
|
|
|
a = ActiveSupport::OrderedOptions.new
|
2018-01-24 21:14:10 -05:00
|
|
|
assert_respond_to a, :blah
|
|
|
|
assert_respond_to a, :blah=
|
2012-05-05 02:24:57 -04:00
|
|
|
assert_equal 42, a.method(:blah=).call(42)
|
|
|
|
assert_equal 42, a.method(:blah).call
|
|
|
|
end
|
2015-05-17 06:51:34 -04:00
|
|
|
|
|
|
|
def test_raises_with_bang
|
|
|
|
a = ActiveSupport::OrderedOptions.new
|
|
|
|
a[:foo] = :bar
|
2018-01-24 21:14:10 -05:00
|
|
|
assert_respond_to a, :foo!
|
2015-05-17 06:51:34 -04:00
|
|
|
|
|
|
|
assert_nothing_raised { a.foo! }
|
|
|
|
assert_equal a.foo, a.foo!
|
|
|
|
|
|
|
|
assert_raises(KeyError) do
|
|
|
|
a.foo = nil
|
|
|
|
a.foo!
|
|
|
|
end
|
2015-05-26 12:21:28 -04:00
|
|
|
assert_raises(KeyError) { a.non_existing_key! }
|
2015-05-17 06:51:34 -04:00
|
|
|
end
|
2017-09-23 05:25:16 -04:00
|
|
|
|
|
|
|
def test_inheritable_options_with_bang
|
|
|
|
a = ActiveSupport::InheritableOptions.new(foo: :bar)
|
|
|
|
|
|
|
|
assert_nothing_raised { a.foo! }
|
|
|
|
assert_equal a.foo, a.foo!
|
|
|
|
|
|
|
|
assert_raises(KeyError) do
|
|
|
|
a.foo = nil
|
|
|
|
a.foo!
|
|
|
|
end
|
|
|
|
assert_raises(KeyError) { a.non_existing_key! }
|
|
|
|
end
|
2020-08-22 15:08:55 -04:00
|
|
|
|
|
|
|
def test_inspect
|
|
|
|
a = ActiveSupport::OrderedOptions.new
|
|
|
|
assert_equal "#<ActiveSupport::OrderedOptions {}>", a.inspect
|
|
|
|
|
|
|
|
a.foo = :bar
|
|
|
|
a[:baz] = :quz
|
|
|
|
|
|
|
|
assert_equal "#<ActiveSupport::OrderedOptions {:foo=>:bar, :baz=>:quz}>", a.inspect
|
|
|
|
end
|
2005-09-30 02:51:42 -04:00
|
|
|
end
|