Sha256: 880a491dabbdde483a7c9c707495c3728e6e8df5796f4865d9a74fd9474b3652

Contents?: true

Size: 1.81 KB

Versions: 8

Compression:

Stored size: 1.81 KB

Contents

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

describe 'metaprogramming' do
  describe 'class_attribute' do
    before do
      @klass = Class.new { class_attribute :setting }
      @sub = Class.new(@klass)
    end

    it 'does not have an effect if already provided by another lib.'

    it 'defaults to nil' do
      @klass.setting.should be_nil
      @sub.setting.should be_nil
    end

    it 'is inheritable' do
      @klass.setting = 1
      @sub.setting == 1
    end

    it 'is overridable' do
      @sub.setting = 1
      @klass.setting.should be_nil

      @klass.setting = 2
      @sub.setting.should == 1

      Class.new(@sub).setting.should == 1
    end

    it 'creates a query? method' do
      @klass.setting?.should == false
      @klass.setting = 1
      @klass.setting?.should == true
    end

    it 'instance reader delegates to class' do
      @klass.new.setting.should be_nil

      @klass.setting = 1
      @klass.new.setting.should == 1
    end

    it 'instance override' do
      object = @klass.new
      object.setting = 1
      @klass.setting.should be_nil
      @klass.setting = 2
      object.setting.should == 1
    end

    it 'instance query' do
      object = @klass.new
      object.setting?.should == false
      object.setting = 1
      object.setting?.should == true
    end

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

    it 'works well with singleton classes' do
      object = @klass.new
      object.singleton_class.setting = 'foo'
      object.setting.should == 'foo'
    end

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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
gorillib-0.1.7 spec/metaprogramming/class_attribute_spec.rb
gorillib-0.1.6 spec/metaprogramming/class_attribute_spec.rb
gorillib-0.1.5 spec/metaprogramming/class_attribute_spec.rb
gorillib-0.1.4 spec/metaprogramming/class_attribute_spec.rb
gorillib-0.1.3 spec/metaprogramming/class_attribute_spec.rb
gorillib-0.1.2 spec/metaprogramming/class_attribute_spec.rb
gorillib-0.1.1 spec/metaprogramming/class_attribute_spec.rb
gorillib-0.1.0 spec/metaprogramming/class_attribute_spec.rb