Sha256: bac74a97161e77da85273e50a2f7421ff948e15ca25d8228b0ca81df9556c56a

Contents?: true

Size: 1.88 KB

Versions: 12

Compression:

Stored size: 1.88 KB

Contents

require 'spec_helper'

describe Virtus::Attribute, '#set_default_value' do
  let(:object) { described_class.build(String, options.merge(:name => name, :default => default)) }

  let(:model)    { Class.new { def name; 'model'; end; attr_reader :test } }
  let(:name)     { :test }
  let(:instance) { model.new }
  let(:options)  { {} }

  before { object.set_default_value(instance) }

  context 'with a nil' do
    subject { instance }

    let(:default) { nil }

    its(:test) { should be(nil) }
    its(:instance_variables) { should include(:'@test') }
  end

  context 'with a non-clonable object' do
    subject { instance }

    let(:object)  { described_class.build('Boolean', options.merge(:name => name, :default => default)) }
    let(:default) { true }

    its(:test) { should be(true) }
    its(:instance_variables) { should include(:'@test') }
  end

  context 'with a clonable' do
    subject { instance }

    let(:default) { [] }

    its(:test) { should eq(default) }
    its(:test) { should_not be(default) }
  end

  context 'with a callable' do
    subject { instance }

    let(:default) { lambda { |model, attribute| "#{model.name}-#{attribute.name}" } }

    its(:test) { should eq('model-test') }
  end

  context 'with a symbol' do
    subject { instance }

    context 'when it is a method name' do
      let(:default) { :set_test }

      context 'when method is public' do
        let(:model) { Class.new { attr_reader :test; def set_test; @test = 'hello world'; end } }

        its(:test) { should eq('hello world') }
      end

      context 'when method is private' do
        let(:model) { Class.new { attr_reader :test; private; def set_test; @test = 'hello world'; end } }

        its(:test) { should eq('hello world') }
      end
    end

    context 'when it is not a method name' do
      let(:default) { :hello_world }

      its(:test) { should eq('hello_world') }
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
virtus-1.0.3 spec/unit/virtus/attribute/set_default_value_spec.rb
virtus-1.0.2 spec/unit/virtus/attribute/set_default_value_spec.rb
virtus-1.0.1 spec/unit/virtus/attribute/set_default_value_spec.rb
virtus-1.0.0 spec/unit/virtus/attribute/set_default_value_spec.rb
virtus-1.0.0.rc2 spec/unit/virtus/attribute/set_default_value_spec.rb
virtus-1.0.0.rc1 spec/unit/virtus/attribute/set_default_value_spec.rb
virtus-1.0.0.beta8 spec/unit/virtus/attribute/set_default_value_spec.rb
virtus-1.0.0.beta7 spec/unit/virtus/attribute/set_default_value_spec.rb
virtus-1.0.0.beta6 spec/unit/virtus/attribute/set_default_value_spec.rb
virtus-1.0.0.beta5 spec/unit/virtus/attribute/set_default_value_spec.rb
virtus-1.0.0.beta4 spec/unit/virtus/attribute/set_default_value_spec.rb
virtus-1.0.0.beta3 spec/unit/virtus/attribute/set_default_value_spec.rb