* 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
|
|
|
#!/usr/bin/env ruby -w
|
|
|
|
# encoding: UTF-8
|
2007-12-24 21:46:26 -05:00
|
|
|
|
|
|
|
# tc_headers.rb
|
|
|
|
#
|
|
|
|
# Created by James Edward Gray II on 2005-10-31.
|
|
|
|
# Copyright 2005 James Edward Gray II. You can redistribute or modify this code
|
|
|
|
# under the terms of Ruby's license.
|
|
|
|
|
|
|
|
require "test/unit"
|
|
|
|
|
|
|
|
require "csv"
|
|
|
|
|
|
|
|
class TestCSVHeaders < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
@data = <<-END_CSV.gsub(/^\s+/, "")
|
|
|
|
first,second,third
|
|
|
|
A,B,C
|
|
|
|
1,2,3
|
|
|
|
END_CSV
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_first_row
|
|
|
|
[:first_row, true].each do |setting| # two names for the same setting
|
|
|
|
# activate headers
|
|
|
|
csv = nil
|
|
|
|
assert_nothing_raised(Exception) do
|
|
|
|
csv = CSV.parse(@data, :headers => setting)
|
|
|
|
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
|
|
|
|
|
|
|
|
def test_array_of_headers
|
|
|
|
# activate headers
|
|
|
|
csv = nil
|
|
|
|
assert_nothing_raised(Exception) do
|
|
|
|
csv = CSV.parse(@data, :headers => [:my, :new, :headers])
|
|
|
|
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])
|
|
|
|
|
|
|
|
# with return and convert
|
|
|
|
assert_nothing_raised(Exception) do
|
|
|
|
csv = CSV.parse(@data, :headers => [:my, :new, :headers],
|
|
|
|
:return_headers => true,
|
|
|
|
:header_converters => lambda { |h| h.to_s } )
|
|
|
|
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)
|
|
|
|
assert(row.header_row?)
|
|
|
|
assert(!row.field_row?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_csv_header_string
|
|
|
|
# activate headers
|
|
|
|
csv = nil
|
|
|
|
assert_nothing_raised(Exception) do
|
|
|
|
csv = CSV.parse(@data, :headers => "my,new,headers")
|
|
|
|
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])
|
|
|
|
|
|
|
|
# with return and convert
|
|
|
|
assert_nothing_raised(Exception) do
|
|
|
|
csv = CSV.parse(@data, :headers => "my,new,headers",
|
|
|
|
:return_headers => true,
|
|
|
|
:header_converters => :symbol )
|
|
|
|
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)
|
|
|
|
assert(row.header_row?)
|
|
|
|
assert(!row.field_row?)
|
|
|
|
end
|
|
|
|
|
* 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
|
|
|
|
assert_nothing_raised(Exception) do
|
|
|
|
csv = CSV.parse( @data.tr(",", "|"), :col_sep => "|",
|
|
|
|
:headers => "my|new|headers" )
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
csv = CSV.parse(@data, :headers => true, :return_headers => true)
|
|
|
|
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 )
|
|
|
|
assert(row.header_row?)
|
|
|
|
assert(!row.field_row?)
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
assert(!row.header_row?)
|
|
|
|
assert(row.field_row?)
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
assert(!row.header_row?)
|
|
|
|
assert(row.field_row?)
|
|
|
|
|
|
|
|
# empty
|
|
|
|
assert_nil(csv[3])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_converters
|
|
|
|
# create test data where headers and fields look alike
|
|
|
|
data = <<-END_MATCHING_CSV.gsub(/^\s+/, "")
|
|
|
|
1,2,3
|
|
|
|
1,2,3
|
|
|
|
END_MATCHING_CSV
|
|
|
|
|
|
|
|
# normal converters do not affect headers
|
|
|
|
csv = CSV.parse( data, :headers => true,
|
|
|
|
:return_headers => true,
|
|
|
|
:converters => :numeric )
|
|
|
|
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])
|
|
|
|
|
|
|
|
# header converters do affect headers (only)
|
|
|
|
assert_nothing_raised(Exception) do
|
|
|
|
csv = CSV.parse( data, :headers => true,
|
|
|
|
:return_headers => true,
|
|
|
|
:converters => :numeric,
|
|
|
|
:header_converters => :symbol )
|
|
|
|
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
|
|
|
|
|
|
|
|
def test_builtin_downcase_converter
|
|
|
|
csv = CSV.parse( "One,TWO Three", :headers => true,
|
|
|
|
:return_headers => true,
|
|
|
|
:header_converters => :downcase )
|
|
|
|
assert_equal(%w{one two\ three}, csv.headers)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_builtin_symbol_converter
|
|
|
|
csv = CSV.parse( "One,TWO Three", :headers => true,
|
|
|
|
:return_headers => true,
|
|
|
|
:header_converters => :symbol )
|
|
|
|
assert_equal([:one, :two_three], csv.headers)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_custom_converter
|
|
|
|
converter = lambda { |header| header.tr(" ", "_") }
|
|
|
|
csv = CSV.parse( "One,TWO Three",
|
|
|
|
:headers => true,
|
|
|
|
:return_headers => true,
|
|
|
|
:header_converters => converter )
|
|
|
|
assert_equal(%w{One TWO_Three}, csv.headers)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_table_support
|
|
|
|
csv = nil
|
|
|
|
assert_nothing_raised(Exception) do
|
|
|
|
csv = CSV.parse(@data, :headers => true)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_instance_of(CSV::Table, csv)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_skip_blanks
|
|
|
|
@data = <<-END_CSV.gsub(/^ +/, "")
|
|
|
|
|
|
|
|
|
|
|
|
A,B,C
|
|
|
|
|
|
|
|
1,2,3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
END_CSV
|
|
|
|
|
|
|
|
expected = [%w[1 2 3]]
|
|
|
|
CSV.parse(@data, :headers => true, :skip_blanks => true) do |row|
|
|
|
|
assert_equal(expected.shift, row.fields)
|
|
|
|
end
|
|
|
|
|
|
|
|
expected = [%w[A B C], %w[1 2 3]]
|
|
|
|
CSV.parse( @data,
|
|
|
|
:headers => true,
|
|
|
|
:return_headers => true,
|
|
|
|
:skip_blanks => true ) do |row|
|
|
|
|
assert_equal(expected.shift, row.fields)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
* 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)
|
|
|
|
|
|
|
|
# headers
|
|
|
|
csv = CSV.new(@data, :headers => true)
|
|
|
|
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
|
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_blank_row_bug_fix
|
|
|
|
@data += "\n#{@data}" # add a blank row
|
|
|
|
|
|
|
|
# ensure that everything returned is a Row object
|
|
|
|
CSV.parse(@data, :headers => true) do |row|
|
|
|
|
assert_instance_of(CSV::Row, row)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|