2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
require "abstract_unit"
|
|
|
|
require "active_support/configurable"
|
2010-04-22 06:00:13 -04:00
|
|
|
|
|
|
|
class ConfigurableActiveSupport < ActiveSupport::TestCase
|
|
|
|
class Parent
|
|
|
|
include ActiveSupport::Configurable
|
|
|
|
config_accessor :foo
|
2012-06-05 11:50:48 -04:00
|
|
|
config_accessor :bar, instance_reader: false, instance_writer: false
|
|
|
|
config_accessor :baz, instance_accessor: false
|
2010-04-22 06:00:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class Child < Parent
|
|
|
|
end
|
|
|
|
|
|
|
|
setup do
|
|
|
|
Parent.config.clear
|
|
|
|
Parent.config.foo = :bar
|
|
|
|
|
|
|
|
Child.config.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
test "adds a configuration hash" do
|
2012-06-05 11:50:48 -04:00
|
|
|
assert_equal({ foo: :bar }, Parent.config)
|
2010-04-22 06:00:13 -04:00
|
|
|
end
|
|
|
|
|
2011-02-28 08:31:29 -05:00
|
|
|
test "adds a configuration hash to a module as well" do
|
|
|
|
mixin = Module.new { include ActiveSupport::Configurable }
|
|
|
|
mixin.config.foo = :bar
|
2012-06-05 11:50:48 -04:00
|
|
|
assert_equal({ foo: :bar }, mixin.config)
|
2011-02-28 08:31:29 -05:00
|
|
|
end
|
|
|
|
|
2010-04-22 06:00:13 -04:00
|
|
|
test "configuration hash is inheritable" do
|
|
|
|
assert_equal :bar, Child.config.foo
|
|
|
|
assert_equal :bar, Parent.config.foo
|
|
|
|
|
|
|
|
Child.config.foo = :baz
|
|
|
|
assert_equal :baz, Child.config.foo
|
|
|
|
assert_equal :bar, Parent.config.foo
|
|
|
|
end
|
|
|
|
|
2012-12-24 16:20:05 -05:00
|
|
|
test "configuration accessors are not available on instance" do
|
2011-05-06 08:32:48 -04:00
|
|
|
instance = Parent.new
|
2012-06-05 11:50:48 -04:00
|
|
|
|
2018-01-24 21:14:10 -05:00
|
|
|
assert_not_respond_to instance, :bar
|
|
|
|
assert_not_respond_to instance, :bar=
|
2012-06-05 11:50:48 -04:00
|
|
|
|
2018-01-24 21:14:10 -05:00
|
|
|
assert_not_respond_to instance, :baz
|
|
|
|
assert_not_respond_to instance, :baz=
|
2011-05-06 08:32:48 -04:00
|
|
|
end
|
|
|
|
|
2012-09-14 15:25:21 -04:00
|
|
|
test "configuration accessors can take a default value" do
|
|
|
|
parent = Class.new do
|
|
|
|
include ActiveSupport::Configurable
|
|
|
|
config_accessor :hair_colors, :tshirt_colors do
|
|
|
|
[:black, :blue, :white]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal [:black, :blue, :white], parent.hair_colors
|
|
|
|
assert_equal [:black, :blue, :white], parent.tshirt_colors
|
|
|
|
end
|
|
|
|
|
2010-04-22 06:00:13 -04:00
|
|
|
test "configuration hash is available on instance" do
|
|
|
|
instance = Parent.new
|
|
|
|
assert_equal :bar, instance.config.foo
|
|
|
|
assert_equal :bar, Parent.config.foo
|
|
|
|
|
|
|
|
instance.config.foo = :baz
|
|
|
|
assert_equal :baz, instance.config.foo
|
|
|
|
assert_equal :bar, Parent.config.foo
|
|
|
|
end
|
2010-09-27 08:50:39 -04:00
|
|
|
|
|
|
|
test "configuration is crystalizeable" do
|
|
|
|
parent = Class.new { include ActiveSupport::Configurable }
|
|
|
|
child = Class.new(parent)
|
|
|
|
|
|
|
|
parent.config.bar = :foo
|
2011-06-29 12:57:59 -04:00
|
|
|
assert_method_not_defined parent.config, :bar
|
|
|
|
assert_method_not_defined child.config, :bar
|
|
|
|
assert_method_not_defined child.new.config, :bar
|
2010-09-27 08:50:39 -04:00
|
|
|
|
2010-09-27 14:42:02 -04:00
|
|
|
parent.config.compile_methods!
|
2010-09-27 08:50:39 -04:00
|
|
|
assert_equal :foo, parent.config.bar
|
|
|
|
assert_equal :foo, child.new.config.bar
|
|
|
|
|
2011-06-29 12:57:59 -04:00
|
|
|
assert_method_defined parent.config, :bar
|
|
|
|
assert_method_defined child.config, :bar
|
|
|
|
assert_method_defined child.new.config, :bar
|
|
|
|
end
|
|
|
|
|
2012-06-05 11:50:48 -04:00
|
|
|
test "should raise name error if attribute name is invalid" do
|
|
|
|
assert_raises NameError do
|
2012-09-14 15:25:21 -04:00
|
|
|
Class.new do
|
2012-06-05 11:50:48 -04:00
|
|
|
include ActiveSupport::Configurable
|
|
|
|
config_accessor "invalid attribute name"
|
|
|
|
end
|
|
|
|
end
|
2013-12-19 14:27:52 -05:00
|
|
|
|
|
|
|
assert_raises NameError do
|
|
|
|
Class.new do
|
|
|
|
include ActiveSupport::Configurable
|
|
|
|
config_accessor "invalid\nattribute"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-19 12:30:01 -05:00
|
|
|
assert_raises NameError do
|
|
|
|
Class.new do
|
|
|
|
include ActiveSupport::Configurable
|
2013-12-19 14:27:52 -05:00
|
|
|
config_accessor "invalid\n"
|
2013-12-19 12:30:01 -05:00
|
|
|
end
|
|
|
|
end
|
2012-06-05 11:50:48 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
test "the config_accessor method should not be publicly callable" do
|
2015-01-20 07:03:27 -05:00
|
|
|
assert_raises NoMethodError do
|
|
|
|
Class.new {
|
|
|
|
include ActiveSupport::Configurable
|
|
|
|
}.config_accessor :foo
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-29 12:57:59 -04:00
|
|
|
def assert_method_defined(object, method)
|
|
|
|
methods = object.public_methods.map(&:to_s)
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes methods, method.to_s, "Expected #{methods.inspect} to include #{method.to_s.inspect}"
|
2011-06-29 12:57:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def assert_method_not_defined(object, method)
|
|
|
|
methods = object.public_methods.map(&:to_s)
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_not_includes methods, method.to_s, "Expected #{methods.inspect} to not include #{method.to_s.inspect}"
|
2010-09-27 08:50:39 -04:00
|
|
|
end
|
2012-09-14 15:25:21 -04:00
|
|
|
end
|