Sha256: ed2a0575af9e9f563157202882047d2b1d5c55f262c2d88b7c39286d06b1cdd5

Contents?: true

Size: 1.48 KB

Versions: 17

Compression:

Stored size: 1.48 KB

Contents

require 'spec_helper'

fixture_path = 'spec/fixtures'

describe 'loading file with quoted fields' do

  it 'leaving the quotes in the data' do
    options = {}
    data = SmarterCSV.process("#{fixture_path}/quoted.csv", options)
    data.flatten.size.should == 4
    data[1][:model].should eq 'Venture "Extended Edition"'
    data[1][:description].should be_nil
    data[2][:model].should eq 'Venture "Extended Edition, Very Large"'
    data[2][:description].should be_nil
    data.each do |h|
      h[:year].class.should eq Fixnum
      h[:make].should_not be_nil
      h[:model].should_not be_nil
      h[:price].class.should eq Float
    end
  end


  it 'removes quotes around quoted fields, but not inside data' do
    options = {}
    data = SmarterCSV.process("#{fixture_path}/quote_char.csv", options)

    data.length.should eq 6
    data[1][:first_name].should eq "Jam\ne\nson\""
    data[2][:first_name].should eq "\"Jean"
  end


  # NOTE: quotes inside headers need to be escaped by doubling them
  #       e.g. 'correct ""EXAMPLE""'
  #       this escaping is illegal: 'incorrect \"EXAMPLE\"' <-- this caused CSV parsing error
  #  in case of CSV parsing errirs, use :user_provided_headers, or key_mapping
  #
  it 'removes quotes around headers and extra quotes inside headers' do
    options = {}
    data = SmarterCSV.process("#{fixture_path}/quoted2.csv", options)

    data.length.should eq 3
    data.first.keys[2].should eq :isbn
    data.first.keys[3].should eq :discounted_price
  end

end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
smarter_csv-1.5.2 spec/smarter_csv/quoted_spec.rb
smarter_csv-1.5.1 spec/smarter_csv/quoted_spec.rb
smarter_csv-1.5.0 spec/smarter_csv/quoted_spec.rb
smarter_csv-1.4.2 spec/smarter_csv/quoted_spec.rb
smarter_csv-1.4.0 spec/smarter_csv/quoted_spec.rb
smarter_csv-1.3.0 spec/smarter_csv/quoted_spec.rb
smarter_csv-1.2.8 spec/smarter_csv/quoted_spec.rb
smarter_csv-1.2.7 spec/smarter_csv/quoted_spec.rb
pokedex-terminal-0.2.8 vendor/bundle/ruby/2.7.0/gems/smarter_csv-1.2.6/spec/smarter_csv/quoted_spec.rb
pokedex-terminal-0.2.7 vendor/bundle/ruby/2.7.0/gems/smarter_csv-1.2.6/spec/smarter_csv/quoted_spec.rb
pokedex-terminal-0.2.6 vendor/bundle/ruby/2.7.0/gems/smarter_csv-1.2.6/spec/smarter_csv/quoted_spec.rb
pokedex-terminal-0.2.5 vendor/bundle/ruby/2.7.0/gems/smarter_csv-1.2.6/spec/smarter_csv/quoted_spec.rb
pokedex-terminal-0.2.4 vendor/bundle/ruby/2.7.0/gems/smarter_csv-1.2.6/spec/smarter_csv/quoted_spec.rb
smarter_csv-1.2.6 spec/smarter_csv/quoted_spec.rb
smarter_csv-1.2.5 spec/smarter_csv/quoted_spec.rb
smarter_csv-1.2.4 spec/smarter_csv/quoted_spec.rb
smarter_csv-1.2.3 spec/smarter_csv/quoted_spec.rb