2019-01-25 01:49:59 -05:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2007-12-24 21:46:26 -05:00
|
|
|
|
2019-01-25 01:49:59 -05:00
|
|
|
require_relative "../helper"
|
2007-12-24 21:46:26 -05:00
|
|
|
|
2019-01-25 01:49:59 -05:00
|
|
|
class TestCSVHeaders < Test::Unit::TestCase
|
2010-12-25 21:15:55 -05:00
|
|
|
extend DifferentOFS
|
2010-12-25 08:49:14 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def setup
|
2010-12-25 08:41:11 -05:00
|
|
|
super
|
2018-09-05 09:33:21 -04:00
|
|
|
@data = <<-CSV
|
|
|
|
first,second,third
|
|
|
|
A,B,C
|
|
|
|
1,2,3
|
|
|
|
CSV
|
2007-12-24 21:46:26 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_first_row
|
|
|
|
[:first_row, true].each do |setting| # two names for the same setting
|
|
|
|
# activate headers
|
|
|
|
csv = nil
|
2009-03-05 22:56:38 -05:00
|
|
|
assert_nothing_raised(Exception) do
|
2008-10-10 11:09:34 -04:00
|
|
|
csv = CSV.parse(@data, headers: setting)
|
2007-12-24 21:46:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# first data row - skipping headers
|
|
|
|
row = csv[0]
|
|
|
|
assert_not_nil(row)
|
|
|
|
assert_instance_of(CSV::Row, row)
|
|
|
|
assert_equal([%w{first A}, %w{second B}, %w{third C}], row.to_a)
|
|
|
|
|
|
|
|
# second data row
|
|
|
|
row = csv[1]
|
|
|
|
assert_not_nil(row)
|
|
|
|
assert_instance_of(CSV::Row, row)
|
|
|
|
assert_equal([%w{first 1}, %w{second 2}, %w{third 3}], row.to_a)
|
|
|
|
|
|
|
|
# empty
|
|
|
|
assert_nil(csv[2])
|
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_array_of_headers
|
|
|
|
# activate headers
|
|
|
|
csv = nil
|
2009-03-05 22:56:38 -05:00
|
|
|
assert_nothing_raised(Exception) do
|
2008-10-10 11:09:34 -04:00
|
|
|
csv = CSV.parse(@data, headers: [:my, :new, :headers])
|
2007-12-24 21:46:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# first data row - skipping headers
|
|
|
|
row = csv[0]
|
|
|
|
assert_not_nil(row)
|
|
|
|
assert_instance_of(CSV::Row, row)
|
|
|
|
assert_equal( [[:my, "first"], [:new, "second"], [:headers, "third"]],
|
|
|
|
row.to_a )
|
|
|
|
|
|
|
|
# second data row
|
|
|
|
row = csv[1]
|
|
|
|
assert_not_nil(row)
|
|
|
|
assert_instance_of(CSV::Row, row)
|
|
|
|
assert_equal([[:my, "A"], [:new, "B"], [:headers, "C"]], row.to_a)
|
|
|
|
|
|
|
|
# third data row
|
|
|
|
row = csv[2]
|
|
|
|
assert_not_nil(row)
|
|
|
|
assert_instance_of(CSV::Row, row)
|
|
|
|
assert_equal([[:my, "1"], [:new, "2"], [:headers, "3"]], row.to_a)
|
|
|
|
|
|
|
|
# empty
|
|
|
|
assert_nil(csv[3])
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# with return and convert
|
|
|
|
assert_nothing_raised(Exception) do
|
2008-10-10 11:09:34 -04:00
|
|
|
csv = CSV.parse( @data, headers: [:my, :new, :headers],
|
|
|
|
return_headers: true,
|
|
|
|
header_converters: lambda { |h| h.to_s } )
|
2007-12-24 21:46:26 -05:00
|
|
|
end
|
|
|
|
row = csv[0]
|
|
|
|
assert_not_nil(row)
|
|
|
|
assert_instance_of(CSV::Row, row)
|
|
|
|
assert_equal([["my", :my], ["new", :new], ["headers", :headers]], row.to_a)
|
2014-03-01 02:08:19 -05:00
|
|
|
assert_predicate(row, :header_row?)
|
|
|
|
assert_not_predicate(row, :field_row?)
|
2007-12-24 21:46:26 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_csv_header_string
|
|
|
|
# activate headers
|
|
|
|
csv = nil
|
2009-03-05 22:56:38 -05:00
|
|
|
assert_nothing_raised(Exception) do
|
2008-10-10 11:09:34 -04:00
|
|
|
csv = CSV.parse(@data, headers: "my,new,headers")
|
2007-12-24 21:46:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# first data row - skipping headers
|
|
|
|
row = csv[0]
|
|
|
|
assert_not_nil(row)
|
|
|
|
assert_instance_of(CSV::Row, row)
|
|
|
|
assert_equal([%w{my first}, %w{new second}, %w{headers third}], row.to_a)
|
|
|
|
|
|
|
|
# second data row
|
|
|
|
row = csv[1]
|
|
|
|
assert_not_nil(row)
|
|
|
|
assert_instance_of(CSV::Row, row)
|
|
|
|
assert_equal([%w{my A}, %w{new B}, %w{headers C}], row.to_a)
|
|
|
|
|
|
|
|
# third data row
|
|
|
|
row = csv[2]
|
|
|
|
assert_not_nil(row)
|
|
|
|
assert_instance_of(CSV::Row, row)
|
|
|
|
assert_equal([%w{my 1}, %w{new 2}, %w{headers 3}], row.to_a)
|
|
|
|
|
|
|
|
# empty
|
|
|
|
assert_nil(csv[3])
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# with return and convert
|
|
|
|
assert_nothing_raised(Exception) do
|
2008-10-10 11:09:34 -04:00
|
|
|
csv = CSV.parse( @data, headers: "my,new,headers",
|
|
|
|
return_headers: true,
|
|
|
|
header_converters: :symbol )
|
2007-12-24 21:46:26 -05:00
|
|
|
end
|
|
|
|
row = csv[0]
|
|
|
|
assert_not_nil(row)
|
|
|
|
assert_instance_of(CSV::Row, row)
|
|
|
|
assert_equal([[:my, "my"], [:new, "new"], [:headers, "headers"]], row.to_a)
|
2014-03-01 02:08:19 -05:00
|
|
|
assert_predicate(row, :header_row?)
|
|
|
|
assert_not_predicate(row, :field_row?)
|
2007-12-24 21:46:26 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
* lib/csv/csv.rb: Reworked CSV's parser and generator to be m17n. Data
is now parsed in the Encoding it is in without need for translation.
* lib/csv/csv.rb: Improved inspect() messages for better IRb support.
* lib/csv/csv.rb: Fixed header writing bug reported by Dov Murik.
* lib/csv/csv.rb: Use custom separators in parsing header Strings as
suggested by Shmulik Regev.
* lib/csv/csv.rb: Added a :write_headers option for outputting headers.
* lib/csv/csv.rb: Handle open() calls in binary mode whenever we can to
workaround a Windows issue where line-ending translation can cause an
off-by-one error in seeking back to a non-zero starting position after
auto-discovery for :row_sep as suggested by Robert Battle.
* lib/csv/csv.rb: Improved the parser to fail faster when fed some forms
of invalid CSV that can be detected without reading ahead.
* lib/csv/csv.rb: Added a :field_size_limit option to control CSV's
lookahead and prevent the parser from biting off more data than
it can chew.
* lib/csv/csv.rb: Added readers for CSV attributes: col_sep(), row_sep(),
quote_char(), field_size_limit(), converters(), unconverted_fields?(),
headers(), return_headers?(), write_headers?(), header_converters(),
skip_blanks?(), and force_quotes?().
* lib/csv/csv.rb: Cleaned up code syntax to be more inline with
Ruby 1.9 than 1.8.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19441 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-20 20:39:03 -04:00
|
|
|
def test_csv_header_string_inherits_separators
|
|
|
|
# parse with custom col_sep
|
|
|
|
csv = nil
|
2009-03-05 22:56:38 -05:00
|
|
|
assert_nothing_raised(Exception) do
|
2008-10-10 11:09:34 -04:00
|
|
|
csv = CSV.parse( @data.tr(",", "|"), col_sep: "|",
|
|
|
|
headers: "my|new|headers" )
|
* lib/csv/csv.rb: Reworked CSV's parser and generator to be m17n. Data
is now parsed in the Encoding it is in without need for translation.
* lib/csv/csv.rb: Improved inspect() messages for better IRb support.
* lib/csv/csv.rb: Fixed header writing bug reported by Dov Murik.
* lib/csv/csv.rb: Use custom separators in parsing header Strings as
suggested by Shmulik Regev.
* lib/csv/csv.rb: Added a :write_headers option for outputting headers.
* lib/csv/csv.rb: Handle open() calls in binary mode whenever we can to
workaround a Windows issue where line-ending translation can cause an
off-by-one error in seeking back to a non-zero starting position after
auto-discovery for :row_sep as suggested by Robert Battle.
* lib/csv/csv.rb: Improved the parser to fail faster when fed some forms
of invalid CSV that can be detected without reading ahead.
* lib/csv/csv.rb: Added a :field_size_limit option to control CSV's
lookahead and prevent the parser from biting off more data than
it can chew.
* lib/csv/csv.rb: Added readers for CSV attributes: col_sep(), row_sep(),
quote_char(), field_size_limit(), converters(), unconverted_fields?(),
headers(), return_headers?(), write_headers?(), header_converters(),
skip_blanks?(), and force_quotes?().
* lib/csv/csv.rb: Cleaned up code syntax to be more inline with
Ruby 1.9 than 1.8.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19441 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-20 20:39:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# verify headers were recognized
|
|
|
|
row = csv[0]
|
|
|
|
assert_not_nil(row)
|
|
|
|
assert_instance_of(CSV::Row, row)
|
|
|
|
assert_equal([%w{my first}, %w{new second}, %w{headers third}], row.to_a)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_return_headers
|
|
|
|
# activate headers and request they are returned
|
|
|
|
csv = nil
|
|
|
|
assert_nothing_raised(Exception) do
|
2008-10-10 11:09:34 -04:00
|
|
|
csv = CSV.parse(@data, headers: true, return_headers: true)
|
2007-12-24 21:46:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# header row
|
|
|
|
row = csv[0]
|
|
|
|
assert_not_nil(row)
|
|
|
|
assert_instance_of(CSV::Row, row)
|
|
|
|
assert_equal( [%w{first first}, %w{second second}, %w{third third}],
|
|
|
|
row.to_a )
|
2014-03-01 02:08:19 -05:00
|
|
|
assert_predicate(row, :header_row?)
|
|
|
|
assert_not_predicate(row, :field_row?)
|
2007-12-24 21:46:26 -05:00
|
|
|
|
|
|
|
# first data row - skipping headers
|
|
|
|
row = csv[1]
|
|
|
|
assert_not_nil(row)
|
|
|
|
assert_instance_of(CSV::Row, row)
|
|
|
|
assert_equal([%w{first A}, %w{second B}, %w{third C}], row.to_a)
|
2014-03-01 02:08:19 -05:00
|
|
|
assert_not_predicate(row, :header_row?)
|
|
|
|
assert_predicate(row, :field_row?)
|
2007-12-24 21:46:26 -05:00
|
|
|
|
|
|
|
# second data row
|
|
|
|
row = csv[2]
|
|
|
|
assert_not_nil(row)
|
|
|
|
assert_instance_of(CSV::Row, row)
|
|
|
|
assert_equal([%w{first 1}, %w{second 2}, %w{third 3}], row.to_a)
|
2014-03-01 02:08:19 -05:00
|
|
|
assert_not_predicate(row, :header_row?)
|
|
|
|
assert_predicate(row, :field_row?)
|
2007-12-24 21:46:26 -05:00
|
|
|
|
|
|
|
# empty
|
|
|
|
assert_nil(csv[3])
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_converters
|
|
|
|
# create test data where headers and fields look alike
|
2018-09-05 09:33:21 -04:00
|
|
|
data = <<-CSV
|
|
|
|
1,2,3
|
|
|
|
1,2,3
|
|
|
|
CSV
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# normal converters do not affect headers
|
2008-10-10 11:09:34 -04:00
|
|
|
csv = CSV.parse( data, headers: true,
|
|
|
|
return_headers: true,
|
|
|
|
converters: :numeric )
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_equal([%w{1 1}, %w{2 2}, %w{3 3}], csv[0].to_a)
|
|
|
|
assert_equal([["1", 1], ["2", 2], ["3", 3]], csv[1].to_a)
|
|
|
|
assert_nil(csv[2])
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# header converters do affect headers (only)
|
2009-03-05 22:56:38 -05:00
|
|
|
assert_nothing_raised(Exception) do
|
2008-10-10 11:09:34 -04:00
|
|
|
csv = CSV.parse( data, headers: true,
|
|
|
|
return_headers: true,
|
|
|
|
converters: :numeric,
|
|
|
|
header_converters: :symbol )
|
2007-12-24 21:46:26 -05:00
|
|
|
end
|
|
|
|
assert_equal([[:"1", "1"], [:"2", "2"], [:"3", "3"]], csv[0].to_a)
|
|
|
|
assert_equal([[:"1", 1], [:"2", 2], [:"3", 3]], csv[1].to_a)
|
|
|
|
assert_nil(csv[2])
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_builtin_downcase_converter
|
2008-10-10 11:09:34 -04:00
|
|
|
csv = CSV.parse( "One,TWO Three", headers: true,
|
|
|
|
return_headers: true,
|
|
|
|
header_converters: :downcase )
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_equal(%w{one two\ three}, csv.headers)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_builtin_symbol_converter
|
2014-04-01 10:40:48 -04:00
|
|
|
# Note that the trailing space is intentional
|
|
|
|
csv = CSV.parse( "One,TWO Three ", headers: true,
|
|
|
|
return_headers: true,
|
|
|
|
header_converters: :symbol )
|
2017-05-16 05:32:32 -04:00
|
|
|
assert_equal([:one, :two_three], csv.headers)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_builtin_symbol_converter_with_punctuation
|
|
|
|
csv = CSV.parse( "One, Two & Three ($)", headers: true,
|
|
|
|
return_headers: true,
|
|
|
|
header_converters: :symbol )
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_equal([:one, :two_three], csv.headers)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2014-04-01 10:35:03 -04:00
|
|
|
def test_builtin_converters_with_blank_header
|
|
|
|
csv = CSV.parse( "one,,three", headers: true,
|
|
|
|
return_headers: true,
|
|
|
|
header_converters: [:downcase, :symbol] )
|
|
|
|
assert_equal([:one, nil, :three], csv.headers)
|
|
|
|
end
|
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_custom_converter
|
|
|
|
converter = lambda { |header| header.tr(" ", "_") }
|
|
|
|
csv = CSV.parse( "One,TWO Three",
|
2008-10-10 11:09:34 -04:00
|
|
|
headers: true,
|
|
|
|
return_headers: true,
|
|
|
|
header_converters: converter )
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_equal(%w{One TWO_Three}, csv.headers)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_table_support
|
|
|
|
csv = nil
|
2009-03-05 22:56:38 -05:00
|
|
|
assert_nothing_raised(Exception) do
|
2008-10-10 11:09:34 -04:00
|
|
|
csv = CSV.parse(@data, headers: true)
|
2007-12-24 21:46:26 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_instance_of(CSV::Table, csv)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_skip_blanks
|
2018-09-05 09:33:21 -04:00
|
|
|
@data = <<-CSV
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
|
2018-09-05 09:33:21 -04:00
|
|
|
A,B,C
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2018-09-05 09:33:21 -04:00
|
|
|
1,2,3
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
2018-09-05 09:33:21 -04:00
|
|
|
CSV
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
expected = [%w[1 2 3]]
|
2008-10-10 11:09:34 -04:00
|
|
|
CSV.parse(@data, headers: true, skip_blanks: true) do |row|
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_equal(expected.shift, row.fields)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
expected = [%w[A B C], %w[1 2 3]]
|
|
|
|
CSV.parse( @data,
|
2008-10-10 11:09:34 -04:00
|
|
|
headers: true,
|
2009-03-05 22:56:38 -05:00
|
|
|
return_headers: true,
|
2008-10-10 11:09:34 -04:00
|
|
|
skip_blanks: true ) do |row|
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_equal(expected.shift, row.fields)
|
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
* lib/csv/csv.rb: Reworked CSV's parser and generator to be m17n. Data
is now parsed in the Encoding it is in without need for translation.
* lib/csv/csv.rb: Improved inspect() messages for better IRb support.
* lib/csv/csv.rb: Fixed header writing bug reported by Dov Murik.
* lib/csv/csv.rb: Use custom separators in parsing header Strings as
suggested by Shmulik Regev.
* lib/csv/csv.rb: Added a :write_headers option for outputting headers.
* lib/csv/csv.rb: Handle open() calls in binary mode whenever we can to
workaround a Windows issue where line-ending translation can cause an
off-by-one error in seeking back to a non-zero starting position after
auto-discovery for :row_sep as suggested by Robert Battle.
* lib/csv/csv.rb: Improved the parser to fail faster when fed some forms
of invalid CSV that can be detected without reading ahead.
* lib/csv/csv.rb: Added a :field_size_limit option to control CSV's
lookahead and prevent the parser from biting off more data than
it can chew.
* lib/csv/csv.rb: Added readers for CSV attributes: col_sep(), row_sep(),
quote_char(), field_size_limit(), converters(), unconverted_fields?(),
headers(), return_headers?(), write_headers?(), header_converters(),
skip_blanks?(), and force_quotes?().
* lib/csv/csv.rb: Cleaned up code syntax to be more inline with
Ruby 1.9 than 1.8.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19441 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-20 20:39:03 -04:00
|
|
|
def test_headers_reader
|
|
|
|
# no headers
|
|
|
|
assert_nil(CSV.new(@data).headers)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
* lib/csv/csv.rb: Reworked CSV's parser and generator to be m17n. Data
is now parsed in the Encoding it is in without need for translation.
* lib/csv/csv.rb: Improved inspect() messages for better IRb support.
* lib/csv/csv.rb: Fixed header writing bug reported by Dov Murik.
* lib/csv/csv.rb: Use custom separators in parsing header Strings as
suggested by Shmulik Regev.
* lib/csv/csv.rb: Added a :write_headers option for outputting headers.
* lib/csv/csv.rb: Handle open() calls in binary mode whenever we can to
workaround a Windows issue where line-ending translation can cause an
off-by-one error in seeking back to a non-zero starting position after
auto-discovery for :row_sep as suggested by Robert Battle.
* lib/csv/csv.rb: Improved the parser to fail faster when fed some forms
of invalid CSV that can be detected without reading ahead.
* lib/csv/csv.rb: Added a :field_size_limit option to control CSV's
lookahead and prevent the parser from biting off more data than
it can chew.
* lib/csv/csv.rb: Added readers for CSV attributes: col_sep(), row_sep(),
quote_char(), field_size_limit(), converters(), unconverted_fields?(),
headers(), return_headers?(), write_headers?(), header_converters(),
skip_blanks?(), and force_quotes?().
* lib/csv/csv.rb: Cleaned up code syntax to be more inline with
Ruby 1.9 than 1.8.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19441 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-20 20:39:03 -04:00
|
|
|
# headers
|
2008-10-10 11:09:34 -04:00
|
|
|
csv = CSV.new(@data, headers: true)
|
* lib/csv/csv.rb: Reworked CSV's parser and generator to be m17n. Data
is now parsed in the Encoding it is in without need for translation.
* lib/csv/csv.rb: Improved inspect() messages for better IRb support.
* lib/csv/csv.rb: Fixed header writing bug reported by Dov Murik.
* lib/csv/csv.rb: Use custom separators in parsing header Strings as
suggested by Shmulik Regev.
* lib/csv/csv.rb: Added a :write_headers option for outputting headers.
* lib/csv/csv.rb: Handle open() calls in binary mode whenever we can to
workaround a Windows issue where line-ending translation can cause an
off-by-one error in seeking back to a non-zero starting position after
auto-discovery for :row_sep as suggested by Robert Battle.
* lib/csv/csv.rb: Improved the parser to fail faster when fed some forms
of invalid CSV that can be detected without reading ahead.
* lib/csv/csv.rb: Added a :field_size_limit option to control CSV's
lookahead and prevent the parser from biting off more data than
it can chew.
* lib/csv/csv.rb: Added readers for CSV attributes: col_sep(), row_sep(),
quote_char(), field_size_limit(), converters(), unconverted_fields?(),
headers(), return_headers?(), write_headers?(), header_converters(),
skip_blanks?(), and force_quotes?().
* lib/csv/csv.rb: Cleaned up code syntax to be more inline with
Ruby 1.9 than 1.8.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19441 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-20 20:39:03 -04:00
|
|
|
assert_equal(true, csv.headers) # before headers are read
|
|
|
|
csv.shift # set headers
|
|
|
|
assert_equal(%w[first second third], csv.headers) # after headers are read
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2018-09-05 09:33:21 -04:00
|
|
|
def test_blank_row
|
2007-12-24 21:46:26 -05:00
|
|
|
@data += "\n#{@data}" # add a blank row
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# ensure that everything returned is a Row object
|
2008-10-10 11:09:34 -04:00
|
|
|
CSV.parse(@data, headers: true) do |row|
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_instance_of(CSV::Row, row)
|
|
|
|
end
|
|
|
|
end
|
2018-09-05 09:33:21 -04:00
|
|
|
|
|
|
|
def test_nil_row_header
|
|
|
|
@data = <<-CSV
|
|
|
|
A
|
|
|
|
|
|
|
|
1
|
|
|
|
CSV
|
|
|
|
|
|
|
|
csv = CSV.parse(@data, headers: true)
|
|
|
|
|
|
|
|
# ensure nil row creates Row object with headers
|
|
|
|
row = csv[0]
|
|
|
|
assert_equal([["A"], [nil]],
|
|
|
|
[row.headers, row.fields])
|
|
|
|
end
|
2019-01-25 01:49:59 -05:00
|
|
|
|
|
|
|
def test_parse_empty
|
|
|
|
assert_equal(CSV::Table.new([], {}),
|
|
|
|
CSV.parse("", headers: true))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parse_empty_line
|
|
|
|
assert_equal(CSV::Table.new([], {}),
|
|
|
|
CSV.parse("\n", headers: true))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_specified_empty
|
|
|
|
assert_equal(CSV::Table.new([],
|
|
|
|
headers: ["header1"]),
|
|
|
|
CSV.parse("", headers: ["header1"]))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_specified_empty_line
|
|
|
|
assert_equal(CSV::Table.new([CSV::Row.new(["header1"], [])],
|
|
|
|
headers: ["header1"]),
|
|
|
|
CSV.parse("\n", headers: ["header1"]))
|
|
|
|
end
|
2007-12-24 21:46:26 -05:00
|
|
|
end
|