* 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_csv_writing.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 TestCSVWriting < Test::Unit::TestCase
|
|
|
|
def test_writing
|
|
|
|
[ ["\t", ["\t"]],
|
|
|
|
["foo,\"\"\"\"\"\",baz", ["foo", "\"\"", "baz"]],
|
|
|
|
["foo,\"\"\"bar\"\"\",baz", ["foo", "\"bar\"", "baz"]],
|
|
|
|
["\"\"\"\n\",\"\"\"\n\"", ["\"\n", "\"\n"]],
|
|
|
|
["foo,\"\r\n\",baz", ["foo", "\r\n", "baz"]],
|
|
|
|
["\"\"", [""]],
|
|
|
|
["foo,\"\"\"\",baz", ["foo", "\"", "baz"]],
|
|
|
|
["foo,\"\r.\n\",baz", ["foo", "\r.\n", "baz"]],
|
|
|
|
["foo,\"\r\",baz", ["foo", "\r", "baz"]],
|
|
|
|
["foo,\"\",baz", ["foo", "", "baz"]],
|
|
|
|
["\",\"", [","]],
|
|
|
|
["foo", ["foo"]],
|
|
|
|
[",,", [nil, nil, nil]],
|
|
|
|
[",", [nil, nil]],
|
|
|
|
["foo,\"\n\",baz", ["foo", "\n", "baz"]],
|
|
|
|
["foo,,baz", ["foo", nil, "baz"]],
|
|
|
|
["\"\"\"\r\",\"\"\"\r\"", ["\"\r", "\"\r"]],
|
|
|
|
["\",\",\",\"", [",", ","]],
|
|
|
|
["foo,bar,", ["foo", "bar", nil]],
|
|
|
|
[",foo,bar", [nil, "foo", "bar"]],
|
|
|
|
["foo,bar", ["foo", "bar"]],
|
|
|
|
[";", [";"]],
|
|
|
|
["\t,\t", ["\t", "\t"]],
|
|
|
|
["foo,\"\r\n\r\",baz", ["foo", "\r\n\r", "baz"]],
|
|
|
|
["foo,\"\r\n\n\",baz", ["foo", "\r\n\n", "baz"]],
|
|
|
|
["foo,\"foo,bar\",baz", ["foo", "foo,bar", "baz"]],
|
|
|
|
[";,;", [";", ";"]],
|
|
|
|
["foo,\"\"\"\"\"\",baz", ["foo", "\"\"", "baz"]],
|
|
|
|
["foo,\"\"\"bar\"\"\",baz", ["foo", "\"bar\"", "baz"]],
|
|
|
|
["foo,\"\r\n\",baz", ["foo", "\r\n", "baz"]],
|
|
|
|
["\"\"", [""]],
|
|
|
|
["foo,\"\"\"\",baz", ["foo", "\"", "baz"]],
|
|
|
|
["foo,\"\r.\n\",baz", ["foo", "\r.\n", "baz"]],
|
|
|
|
["foo,\"\r\",baz", ["foo", "\r", "baz"]],
|
|
|
|
["foo,\"\",baz", ["foo", "", "baz"]],
|
|
|
|
["foo", ["foo"]],
|
|
|
|
[",,", [nil, nil, nil]],
|
|
|
|
[",", [nil, nil]],
|
|
|
|
["foo,\"\n\",baz", ["foo", "\n", "baz"]],
|
|
|
|
["foo,,baz", ["foo", nil, "baz"]],
|
|
|
|
["foo,bar", ["foo", "bar"]],
|
|
|
|
["foo,\"\r\n\n\",baz", ["foo", "\r\n\n", "baz"]],
|
|
|
|
["foo,\"foo,bar\",baz", ["foo", "foo,bar", "baz"]],
|
|
|
|
[%Q{a,b}, ["a", "b"]],
|
|
|
|
[%Q{a,"""b"""}, ["a", "\"b\""]],
|
|
|
|
[%Q{a,"""b"}, ["a", "\"b"]],
|
|
|
|
[%Q{a,"b"""}, ["a", "b\""]],
|
|
|
|
[%Q{a,"\nb"""}, ["a", "\nb\""]],
|
|
|
|
[%Q{a,"""\nb"}, ["a", "\"\nb"]],
|
|
|
|
[%Q{a,"""\nb\n"""}, ["a", "\"\nb\n\""]],
|
|
|
|
[%Q{a,"""\nb\n""",}, ["a", "\"\nb\n\"", nil]],
|
|
|
|
[%Q{a,,,}, ["a", nil, nil, nil]],
|
|
|
|
[%Q{,}, [nil, nil]],
|
|
|
|
[%Q{"",""}, ["", ""]],
|
|
|
|
[%Q{""""}, ["\""]],
|
|
|
|
[%Q{"""",""}, ["\"",""]],
|
|
|
|
[%Q{,""}, [nil,""]],
|
|
|
|
[%Q{,"\r"}, [nil,"\r"]],
|
|
|
|
[%Q{"\r\n,"}, ["\r\n,"]],
|
|
|
|
[%Q{"\r\n,",}, ["\r\n,", nil]] ].each do |test_case|
|
|
|
|
assert_equal(test_case.first + $/, CSV.generate_line(test_case.last))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_col_sep
|
|
|
|
assert_equal( "a;b;;c\n", CSV.generate_line( ["a", "b", nil, "c"],
|
2008-10-10 11:09:34 -04:00
|
|
|
col_sep: ";" ) )
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_equal( "a\tb\t\tc\n", CSV.generate_line( ["a", "b", nil, "c"],
|
2008-10-10 11:09:34 -04:00
|
|
|
col_sep: "\t" ) )
|
2007-12-24 21:46:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_row_sep
|
|
|
|
assert_equal( "a,b,,c\r\n", CSV.generate_line( ["a", "b", nil, "c"],
|
2008-10-10 11:09:34 -04:00
|
|
|
row_sep: "\r\n" ) )
|
2007-12-24 21:46:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_force_quotes
|
|
|
|
assert_equal( %Q{"1","b","","already ""quoted"""\n},
|
|
|
|
CSV.generate_line( [1, "b", nil, %Q{already "quoted"}],
|
2008-10-10 11:09:34 -04:00
|
|
|
force_quotes: true ) )
|
2007-12-24 21:46:26 -05:00
|
|
|
end
|
|
|
|
end
|