Sha256: 62c244ad01c59a565e9bfb9ef6df52d4f0444aafa556f774409a1a80f3f0ba40

Contents?: true

Size: 1.41 KB

Versions: 7

Compression:

Stored size: 1.41 KB

Contents

require 'spec_helper'

describe Virtus::AttributeSet, '#<<' do
  subject { object << attribute }

  let(:attributes) { []                                      }
  let(:parent)     { described_class.new                     }
  let(:object)     { described_class.new(parent, attributes) }
  let(:name)       { :name                                   }

  context 'with a new attribute' do
    let(:attribute) { Virtus::Attribute.build(String, :name => name) }

    it { is_expected.to equal(object) }

    it 'adds an attribute' do
      expect { subject }.to change { object.to_a }.
        from(attributes).
        to([ attribute ])
    end

    it 'indexes the new attribute under its #name property' do
      expect { subject }.to change { object[name] }.
        from(nil).
        to(attribute)
    end

    it 'indexes the new attribute under the string version of its #name property' do
      expect { subject }.to change { object[name.to_s] }.
        from(nil).
        to(attribute)
    end
  end

  context 'with a duplicate attribute' do
    let(:attributes) { [Virtus::Attribute.build(String, :name => name)] }
    let(:attribute)  { Virtus::Attribute.build(String, :name => name) }

    it { is_expected.to equal(object) }

    it "replaces the original attribute object" do
      expect { subject }.to change { object.to_a.map(&:__id__) }.
      from(attributes.map(&:__id__)).
      to([attribute.__id__])
    end
  end
end

Version data entries

7 entries across 7 versions & 3 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/virtus-2.0.0/spec/unit/virtus/attribute_set/append_spec.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/virtus-2.0.0/spec/unit/virtus/attribute_set/append_spec.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/virtus-2.0.0/spec/unit/virtus/attribute_set/append_spec.rb
virtus2-2.1.0 spec/unit/virtus/attribute_set/append_spec.rb
virtus2-2.0.2 spec/unit/virtus/attribute_set/append_spec.rb
virtus2-2.0.1 spec/unit/virtus/attribute_set/append_spec.rb
virtus-2.0.0 spec/unit/virtus/attribute_set/append_spec.rb