Sha256: a30e8198a8f500d9a791dc8d2ff6992b75bae92f7d260fe60174f7781ff94612

Contents?: true

Size: 1.65 KB

Versions: 6

Compression:

Stored size: 1.65 KB

Contents

require File.dirname(__FILE__)+'/../abstract_unit'
require 'gorillib/metaprogramming/class_attribute'

class ClassAttributeTest < ActiveSupport::TestCase
  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 'instance reader delegates to class' do
    assert_nil @klass.new.setting

    @klass.setting = 1
    assert_equal 1, @klass.new.setting
  end

  test 'instance override' do
    object = @klass.new
    object.setting = 1
    assert_nil @klass.setting
    @klass.setting = 2
    assert_equal 1, object.setting
  end

  test 'instance query' do
    object = @klass.new
    assert_equal false, object.setting?
    object.setting = 1
    assert_equal true, object.setting?
  end

  test 'disabling instance writer' do
    object = Class.new { class_attribute :setting, :instance_writer => false }.new
    assert_raise(NoMethodError) { object.setting = 'boom' }
  end

  test 'works well with singleton classes' do
    object = @klass.new
    object.singleton_class.setting = 'foo'
    assert_equal 'foo', object.setting
  end

  test 'setter returns set value' do
    val = @klass.send(:setting=, 1)
    assert_equal 1, val
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
gorillib-0.0.7 test/metaprogramming/class_attribute_test.rb
gorillib-0.0.6 test/metaprogramming/class_attribute_test.rb
gorillib-0.0.5 test/metaprogramming/class_attribute_test.rb
gorillib-0.0.4 test/metaprogramming/class_attribute_test.rb
gorillib-0.0.3 test/metaprogramming/class_attribute_test.rb
gorillib-0.0.2 test/metaprogramming/class_attribute_test.rb