Sha256: d1efec8ea01354993efa6a3d57117083d1d19ba85cb8c5dd5a277d5bfa4b4eca

Contents?: true

Size: 1.76 KB

Versions: 3

Compression:

Stored size: 1.76 KB

Contents

require 'spec_helper'

module Csv2hash
  describe Parser::Mapping do

    let(:definition) do
      Main.generate_definition :foo do
        set_type { Definition::MAPPING }
        mapping do
          cell position: [1,1], key: 'first_name'
          cell position: [2,1], key: 'last_name'
        end
      end
    end

    let(:data_source) do
      [ ['Nickname', 'jo', nil, nil, nil],
        ['FirstName', 'John', nil, nil, nil],
        ['LastName', 'Doe', nil, nil, nil],
        [nil, nil, nil, nil, nil],
        ['Employment', 'CEO', nil, nil, nil],
        ['Post', 'Doe', nil, nil, nil],
        [nil, nil, nil, nil, nil],
        [nil, 'Personal info', 'Age', 26, nil],
        [nil, 'Sex', 'Male', nil, nil],
        [nil, nil, nil, nil, nil]
      ]
    end

    subject do
      Main.new(definition, data_source, ignore_blank_line: false)
    end

    context 'regular way' do
      it { expect { subject.parse! }.to_not raise_error }
      it {
        expect(subject.tap do |csv2hash|
          csv2hash.parse!
        end.data).to eql({ data: [{ 'first_name' => 'John', 'last_name' => 'Doe' }]})
      }
    end

    context 'with nested' do
      before do
        definition.cells << Cell.new({ position: [7,3], key: 'age', nested: 'infos' })
      end
      it {
        expect(subject.tap { |c| c.parse! }.data).to eql(
          { data: [ { 'first_name' => 'John', 'last_name' => 'Doe', 'infos' => { 'age' => 26 } } ] }
        )
      }
    end

    context 'discover' do
      before do
        definition.cells << Cell.new({ position: [[1,/Sex/],2], key: 'sex'})
      end
      it {
        expect(subject.tap { |c| c.parse! }.data).to eql(
          { data: [ { 'first_name' => 'John', 'last_name' => 'Doe', 'sex' => 'Male' } ] }
        )
      }
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
csv2hash-0.6.4 spec/csv2hash/parser/mapping_spec.rb
csv2hash-0.6.3 spec/csv2hash/parser/mapping_spec.rb
csv2hash-0.6.2 spec/csv2hash/parser/mapping_spec.rb