spec/csv2hash/yaml_coercer_spec.rb in csv2hash-0.7.3 vs spec/csv2hash/yaml_coercer_spec.rb in csv2hash-0.7.5

- old
+ new

@@ -1,27 +1,118 @@ require 'spec_helper' module Csv2hash module Coercers describe YamlCoercer do - context 'when position is a string' do - let(:rules) { {:position=>"Mobile phone number", :key=>"mobile_phone", :allow_blank=>true} } - let(:result_rules) { {:position=>/\A(Mobile phone number)\z/, :key=>"mobile_phone", :allow_blank=>true} } + let(:base_rules) {{ key: 'mobile_phone', allow_blank: true }} + let(:ignore_case) { false } + let(:exact_matching) { false } + + context 'deserialize!' do subject { YamlCoercer.new(rules) } + before do + Csv2hash.configure do |conf| + conf.ignore_case = ignore_case + conf.exact_matching = exact_matching + end + subject.deserialize! + end - before { subject.deserialize! } - - it{expect(subject.rules).to eql(result_rules)} - end + after do + Csv2hash.configure do |conf| + conf.ignore_case = false + conf.exact_matching = false + end + end - context 'when position is a array' do - let(:rules) { {:position=>[[1,"Mobile phone number"],2], :key=>"mobile_phone", :allow_blank=>true} } - let(:result_rules) { {:position=>[[1, /Mobile phone number/], 2], :key=>"mobile_phone", :allow_blank=>true} } - subject { YamlCoercer.new(rules) } + context 'regular' do + context 'from string' do + context 'when position is a string' do + let(:rules) { base_rules.merge(position: 'Mobile phone number') } + let(:result_rules) { base_rules.merge(position: /Mobile phone number/) } - before { subject.deserialize! } - - it{expect(subject.rules).to eql(result_rules)} - end + it { expect(subject.rules).to eql(result_rules) } + end + + context 'when position is a array' do + let(:rules) { base_rules.merge(position: [[1, 'Mobile phone number'], 2]) } + let(:result_rules) { base_rules.merge(position: [[1, /Mobile phone number/], 2]) } + + it { expect(subject.rules).to eql(result_rules) } + end + end + context 'from regex' do + context 'when position is a string' do + let(:rules) { base_rules.merge(position: /Mobile phone number/) } + let(:result_rules) { base_rules.merge(position: /Mobile phone number/) } + + it { expect(subject.rules).to eql(result_rules) } + end + + context 'when position is a array' do + let(:rules) { base_rules.merge(position: [[1, /Mobile phone number/], 2]) } + let(:result_rules) { base_rules.merge(position: [[1, /Mobile phone number/], 2]) } + + it { expect(subject.rules).to eql(result_rules) } + end + end + end + + context 'ignore_case' do + let(:ignore_case) { true } + + context 'when position is a string' do + let(:rules) { base_rules.merge(position: 'Mobile phone number') } + let(:result_rules) { base_rules.merge(position: /Mobile phone number/i) } + + it { expect(subject.rules).to eql(result_rules) } + end + + context 'when position is a array' do + let(:rules) { base_rules.merge(position: [[1, 'Mobile phone number'], 2]) } + let(:result_rules) { base_rules.merge(position: [[1, /Mobile phone number/i], 2]) } + + it { expect(subject.rules).to eql(result_rules) } + end + end + + context 'exact_matching' do + let(:exact_matching) { true } + + context 'when position is a string' do + let(:rules) { base_rules.merge(position: 'Mobile phone number') } + let(:result_rules) { base_rules.merge(position: /\A(Mobile phone number)\z/) } + + it { expect(subject.rules).to eql(result_rules) } + end + + context 'when position is a array' do + let(:rules) { base_rules.merge(position: [[1, 'Mobile phone number'], 2]) } + let(:result_rules) { base_rules.merge(position: [[1, /\A(Mobile phone number)\z/], 2]) } + + it { expect(subject.rules).to eql(result_rules) } + end + end + + context 'exact_matching and ignore_case' do + let(:ignore_case) { true } + let(:exact_matching) { true } + + context 'when position is a string' do + let(:rules) { base_rules.merge(position: 'Mobile phone number') } + let(:result_rules) { base_rules.merge(position: /\A(Mobile phone number)\z/i) } + + it { expect(subject.rules).to eql(result_rules) } + end + + context 'when position is a array' do + let(:rules) { base_rules.merge(position: [[1, 'Mobile phone number'], 2]) } + let(:result_rules) { base_rules.merge(position: [[1, /\A(Mobile phone number)\z/i], 2]) } + + it { expect(subject.rules).to eql(result_rules) } + end + end + + end end end end