File: element_set_spec.rb

package info (click to toggle)
ruby-virtus 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 660 kB
  • sloc: ruby: 4,378; makefile: 2
file content (64 lines) | stat: -rw-r--r-- 2,144 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
require 'spec_helper'

describe Virtus::AttributeSet, '#[]=' do
  subject { object[name] = 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(attribute) }

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

    it 'allows #[] to access the attribute with a symbol' do
      expect { subject }.to change { object['name'] }.from(nil).to(attribute)
    end

    it 'allows #[] to access the attribute with a string' do
      expect { subject }.to change { object[:name] }.from(nil).to(attribute)
    end

    it 'allows #reset to track overridden attributes' do
      expect { subject }.to change { object.reset.to_a }.from(attributes).to([ attribute ])
    end
  end

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

    it { is_expected.to equal(attribute) }

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

    it 'allows #[] to access the attribute with a string' do
      expect { subject }.to change { object['name'].__id__ }.
      from(original.__id__).
      to(attribute.__id__)
    end

    it 'allows #[] to access the attribute with a symbol' do
      expect { subject }.to change { object[:name].__id__ }.
      from(original.__id__).
      to(attribute.__id__)
    end

    it 'allows #reset to track overridden attributes' do
      expect { subject }.to change { object.reset.to_a.map(&:__id__) }.
      from(attributes.map(&:__id__)).
      to([attribute.__id__])
    end
  end
end