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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
describe Unparser::Anima do
let(:object) { described_class.new(:foo) }
describe '#attributes_hash' do
let(:value) { double('Value') }
let(:instance) { double(foo: value) }
subject { object.attributes_hash(instance) }
it { should eql(foo: value) }
end
describe '#remove' do
let(:object) { described_class.new(:foo, :bar) }
context 'with single attribute' do
subject { object.remove(:bar) }
it { should eql(described_class.new(:foo)) }
end
context 'with multiple attributes' do
subject { object.remove(:foo, :bar) }
it { should eql(described_class.new) }
end
context 'with inexisting attribute' do
subject { object.remove(:baz) }
it { should eql(object) }
end
end
describe '#add' do
context 'with single attribute' do
subject { object.add(:bar) }
it { should eql(described_class.new(:foo, :bar)) }
end
context 'with multiple attributes' do
subject { object.add(:bar, :baz) }
it { should eql(described_class.new(:foo, :bar, :baz)) }
end
context 'with duplicate attribute ' do
subject { object.add(:foo) }
it { should eql(object) }
end
end
describe '#attributes' do
subject { object.attributes }
it { should eql([Unparser::Anima::Attribute.new(:foo)]) }
it { should be_frozen }
end
describe '#included' do
let(:target) do
object = self.object
Class.new do
include object
end
end
let(:value) { double('Value') }
let(:instance) { target.new(foo: value) }
let(:instance_b) { target.new(foo: value) }
let(:instance_c) { target.new(foo: double('Bar')) }
context 'on instance' do
subject { instance }
it { should eql(instance_b) }
it { should_not eql(instance_c) }
it 'returns expected value' do
expect(instance.foo).to be(value)
end
end
context 'on singleton' do
subject { target }
it 'should define attribute hash reader' do
expect(instance.to_h).to eql(foo: value)
end
specify { expect(subject.anima).to be(object) }
end
end
describe '#initialize_instance' do
let(:object) { Unparser::Anima.new(:foo, :bar) }
let(:target) { Object.new }
let(:foo) { double('Foo') }
let(:bar) { double('Bar') }
subject { object.initialize_instance(target, attribute_hash) }
context 'when all keys are present in attribute hash' do
let(:attribute_hash) { { foo: foo, bar: bar } }
it 'should initialize target instance variables' do
subject
expect(
target
.instance_variables
.map(&:to_sym)
.to_set
).to eql(%i[@foo @bar].to_set)
expect(target.instance_variable_get(:@foo)).to be(foo)
expect(target.instance_variable_get(:@bar)).to be(bar)
end
it_should_behave_like 'a command method'
end
context 'when an extra key is present in attribute hash' do
let(:attribute_hash) { { foo: foo, bar: bar, baz: double('Baz') } }
it 'should raise error' do
expect { subject }.to raise_error(
Unparser::Anima::Error,
Unparser::Anima::Error.new(target.class, [], [:baz]).message
)
end
context 'and the extra key is falsy' do
let(:attribute_hash) { { foo: foo, bar: bar, nil => double('Baz') } }
it 'should raise error' do
expect { subject }.to raise_error(
Unparser::Anima::Error,
Unparser::Anima::Error.new(target.class, [], [nil]).message
)
end
end
end
context 'when a key is missing in attribute hash' do
let(:attribute_hash) { { bar: bar } }
it 'should raise error' do
expect { subject }.to raise_error(
Unparser::Anima::Error.new(target.class, [:foo], []).message
)
end
end
end
describe 'using super in initialize' do
subject { klass.new }
let(:klass) do
Class.new do
include Unparser::Anima.new(:foo)
def initialize(attributes = { foo: :bar })
super
end
end
end
specify { expect(subject.foo).to eql(:bar) }
end
describe '#to_h on an anima infected instance' do
subject { instance.to_h }
let(:instance) { klass.new(params) }
let(:params) { Hash[foo: :bar] }
let(:klass) do
Class.new do
include Unparser::Anima.new(:foo)
end
end
it { should eql(params) }
end
describe '#with' do
subject { object.with(attributes) }
let(:klass) do
Class.new do
include Unparser::Anima.new(:foo, :bar)
end
end
let(:object) { klass.new(foo: 1, bar: 2) }
context 'with empty attributes' do
let(:attributes) { {} }
it { should eql(object) }
end
context 'with updated attribute' do
let(:attributes) { { foo: 3 } }
it { should eql(klass.new(foo: 3, bar: 2)) }
end
end
end
|