Sha256: 449868bab28d669dd697d7abc7de3ffedd714df5611960cc11cde8d8205994aa

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

require 'spec_helper'

describe Virtus::Attribute::DefaultValue, '#evaluate' do
  subject { object.evaluate(instance) }

  let(:object)    { described_class.build(attribute, value) }
  let(:attribute) { mock('attribute')                       }
  let(:value)     { mock('value')                           }
  let(:instance)  { mock('instance')                        }
  let(:response)  { stub('response')                        }

  context 'when the value is callable' do
    before { value.stub(:call => response) }

    specify { object.should be_instance_of(Virtus::Attribute::DefaultValue::FromCallable) }

    it { should be(response) }

    it 'calls the value with the instance and attribute' do
      value.should_receive(:call).with(instance, attribute)
      subject
    end
  end

  context 'when the value is cloneable' do
    let(:clone) { stub('clone') }

    before { value.stub(:clone => clone) }

    specify { object.should be_instance_of(Virtus::Attribute::DefaultValue::FromClonable) }

    it { should be(clone) }

    it 'clones the value' do
      value.should_receive(:clone).with(no_args)
      subject
    end
  end

  context 'when the value is a method name' do
    let(:instance) { mock('instance', value => retval) }
    let(:value)    { :set_default                      }
    let(:retval)   { stub('retval')                    }

    specify { object.should be_instance_of(Virtus::Attribute::DefaultValue::FromSymbol) }

    it { should be(retval) }

    it 'calls the method' do
      instance.should_receive(value).with(no_args)
      subject
    end
  end

  # smallest number that is Bignum across major ruby impls
  bignum = 0x7fffffffffffffff + 1

  [ nil, true, false, 0, 0.0, bignum, :symbol ].each do |value|
    context "when the value is #{value.inspect} (#{value.class})" do
      let(:value) { value }

      it { should be(value) }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
virtus-0.3.0 spec/unit/virtus/attribute/default_value/evaluate_spec.rb