diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2010-01-31 18:13:56 -0800 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2010-02-01 02:02:41 -0800 |
commit | 8ae25a8e41168801590fdb95891cc5990b4db21c (patch) | |
tree | de2e639915fa8717dd96d37c6b6997cbf7c6771e /activesupport/test/core_ext/class | |
parent | 209235165266ff39f2d14d02b497d7d703788104 (diff) | |
download | rails-8ae25a8e41168801590fdb95891cc5990b4db21c.tar.gz rails-8ae25a8e41168801590fdb95891cc5990b4db21c.tar.bz2 rails-8ae25a8e41168801590fdb95891cc5990b4db21c.zip |
Introduce class_attribute to declare inheritable class attributes. Writing an attribute on a subclass behaves just like overriding the superclass reader method. Unifies and replaces most usage of cattr_accessor, class_inheritable_attribute, superclass_delegating_attribute, and extlib_inheritable_attribute.
Diffstat (limited to 'activesupport/test/core_ext/class')
-rw-r--r-- | activesupport/test/core_ext/class/attribute_test.rb | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/activesupport/test/core_ext/class/attribute_test.rb b/activesupport/test/core_ext/class/attribute_test.rb new file mode 100644 index 0000000000..ef84b9f255 --- /dev/null +++ b/activesupport/test/core_ext/class/attribute_test.rb @@ -0,0 +1,47 @@ +require 'abstract_unit' +require 'active_support/core_ext/class/attribute' + +class ClassAttributeTest < ActiveSupport::TestCase + class Base + class_attribute :setting + end + + class Subclass < Base + end + + def setup + @klass = Class.new { class_attribute :setting } + @sub = Class.new(@klass) + end + + test 'defaults to nil' do + assert_nil @klass.setting + assert_nil @sub.setting + end + + test 'inheritable' do + @klass.setting = 1 + assert_equal 1, @sub.setting + end + + test 'overridable' do + @sub.setting = 1 + assert_nil @klass.setting + + @klass.setting = 2 + assert_equal 1, @sub.setting + + assert_equal 1, Class.new(@sub).setting + end + + test 'query method' do + assert_equal false, @klass.setting? + @klass.setting = 1 + assert_equal true, @klass.setting? + end + + test 'no instance delegates' do + assert_raise(NoMethodError) { @klass.new.setting } + assert_raise(NoMethodError) { @klass.new.setting? } + end +end |