spec/lib/danica/variables_holder_spec.rb in danica-2.6.0 vs spec/lib/danica/variables_holder_spec.rb in danica-2.6.1

- old
+ new

@@ -26,15 +26,87 @@ subject.variables = variables end.not_to change { clazz.variables_hash } end end +shared_examples 'a class with mapped variables' do + context 'when initializing with an array' do + subject { clazz.new(1,2,3) } + it 'initialize variables in order of declaration' do + expect(subject.variables_hash).to eq({ + x: Danica::Wrapper::Number.new(1), + y: Danica::Wrapper::Number.new(2), + z: Danica::Wrapper::Number.new(3) + }) + end + end + + context 'when initialized with a hash' do + subject { clazz.new(z: 1, y: 2) } + + it 'initialize variables with the map given' do + expect(subject.variables_hash).to eq({ + x: Danica::Wrapper::Variable.new(name: :x), + y: Danica::Wrapper::Number.new(2), + z: Danica::Wrapper::Number.new(1) + }) + end + end + + context 'when initializing with hashes' do + subject do + clazz.new( + {name: :xis, value: 1}, + {name: :yps, value: 2}, + {name: :zes, value: 3} + ) + end + + it 'initialize variables with the maps given' do + expect(subject.variables_hash).to eq({ + x: Danica::Wrapper::Variable.new(name: :xis, value: 1), + y: Danica::Wrapper::Variable.new(name: :yps, value: 2), + z: Danica::Wrapper::Variable.new(name: :zes, value: 3) + }) + end + end + + context 'when initializing with array of hashes' do + subject do + clazz.new([ + {name: :xis, value: 1}, + {name: :yps, value: 2}, + {name: :zes, value: 3} + ]) + end + + it 'initialize variables with the maps given' do + expect(subject.variables_hash).to eq({ + x: Danica::Wrapper::Variable.new(name: :xis, value: 1), + y: Danica::Wrapper::Variable.new(name: :yps, value: 2), + z: Danica::Wrapper::Variable.new(name: :zes, value: 3) + }) + end + end +end + describe Danica::VariablesHolder do let(:clazz) { described_class::Dummy } subject { clazz.new } + describe '#initialize' do + context 'when using a symbolized key definition' do + it_behaves_like 'a class with mapped variables' + end + + context 'when using a string key definition' do + let(:clazz) { described_class::DummyString } + it_behaves_like 'a class with mapped variables' + end + end + describe 'variables assignement' do it 'creates setters and getters for the variables' do %i(x y z).each do |var| expect(subject).to respond_to(var) expect(subject).to respond_to("#{var}=") @@ -78,11 +150,13 @@ it 'returns the list of variables of both classes merged' do expect(clazz.variables_names).to eq(%i(k z)) end end + end + describe 'variable alias' do context 'when we alias a variable' do let(:clazz) { described_class::DummyAlias } it 'returns the list of variables of both classes merged' do expect(clazz.variables_names).to eq(%i(a y z)) @@ -210,10 +284,10 @@ end it 'does not change the class variables' do expect do subject.z = 2 - end.not_to change { clazz.variables_hash } + end.not_to change(clazz, :variables_hash) end end end describe '#variables_hash' do