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
|
|
|
|
* 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
|
|
|
require "timeout"
|
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
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
|
|
|
# Following tests are my interpretation of the
|
|
|
|
# {CSV RCF}[http://www.ietf.org/rfc/rfc4180.txt]. I only deviate from that
|
2007-12-24 21:46:26 -05:00
|
|
|
# document in one place (intentionally) and that is to make the default row
|
|
|
|
# separator <tt>$/</tt>.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2019-01-25 01:49:59 -05:00
|
|
|
class TestCSVParseGeneral < Test::Unit::TestCase
|
2010-12-25 21:15:55 -05:00
|
|
|
extend DifferentOFS
|
2010-12-25 08:49:14 -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
|
|
|
BIG_DATA = "123456789\n" * 1024
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_mastering_regex_example
|
|
|
|
ex = %Q{Ten Thousand,10000, 2710 ,,"10,000","It's ""10 Grand"", baby",10K}
|
|
|
|
assert_equal( [ "Ten Thousand", "10000", " 2710 ", nil, "10,000",
|
|
|
|
"It's \"10 Grand\", baby", "10K" ],
|
|
|
|
CSV.parse_line(ex) )
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# Old Ruby 1.8 CSV library tests.
|
|
|
|
def test_std_lib_csv
|
|
|
|
[ ["\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"]],
|
|
|
|
[";,;", [";", ";"]] ].each do |csv_test|
|
|
|
|
assert_equal(csv_test.last, CSV.parse_line(csv_test.first))
|
|
|
|
end
|
|
|
|
|
|
|
|
[ ["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"]] ].each do |csv_test|
|
|
|
|
assert_equal(csv_test.last, CSV.parse_line(csv_test.first))
|
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# From: http://ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-core/6496
|
|
|
|
def test_aras_edge_cases
|
|
|
|
[ [%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""",\nc}, ["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 |edge_case|
|
|
|
|
assert_equal(edge_case.last, CSV.parse_line(edge_case.first))
|
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_james_edge_cases
|
|
|
|
# A read at eof? should return nil.
|
|
|
|
assert_equal(nil, CSV.parse_line(""))
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2007-12-24 21:46:26 -05:00
|
|
|
# With Ruby 1.8 CSV it's impossible to tell an empty line from a line
|
|
|
|
# containing a single +nil+ field. The old CSV library returns
|
|
|
|
# <tt>[nil]</tt> in these cases, but <tt>Array.new</tt> makes more sense to
|
|
|
|
# me.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_equal(Array.new, CSV.parse_line("\n1,2,3\n"))
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2010-06-24 22:59:20 -04:00
|
|
|
def test_rob_edge_cases
|
|
|
|
[ [%Q{"a\nb"}, ["a\nb"]],
|
|
|
|
[%Q{"\n\n\n"}, ["\n\n\n"]],
|
|
|
|
[%Q{a,"b\n\nc"}, ['a', "b\n\nc"]],
|
|
|
|
[%Q{,"\r\n"}, [nil,"\r\n"]],
|
|
|
|
[%Q{,"\r\n."}, [nil,"\r\n."]],
|
|
|
|
[%Q{"a\na","one newline"}, ["a\na", 'one newline']],
|
|
|
|
[%Q{"a\n\na","two newlines"}, ["a\n\na", 'two newlines']],
|
|
|
|
[%Q{"a\r\na","one CRLF"}, ["a\r\na", 'one CRLF']],
|
|
|
|
[%Q{"a\r\n\r\na","two CRLFs"}, ["a\r\n\r\na", 'two CRLFs']],
|
|
|
|
[%Q{with blank,"start\n\nfinish"\n}, ['with blank', "start\n\nfinish"]],
|
|
|
|
].each do |edge_case|
|
|
|
|
assert_equal(edge_case.last, CSV.parse_line(edge_case.first))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-23 10:59:25 -04:00
|
|
|
def test_non_regex_edge_cases
|
|
|
|
# An early version of the non-regex parser fails this test
|
|
|
|
[ [ "foo,\"foo,bar,baz,foo\",\"foo\"",
|
|
|
|
["foo", "foo,bar,baz,foo", "foo"] ] ].each do |edge_case|
|
|
|
|
assert_equal(edge_case.last, CSV.parse_line(edge_case.first))
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise(CSV::MalformedCSVError) do
|
|
|
|
CSV.parse_line("1,\"23\"4\"5\", 6")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-23 02:00:35 -05:00
|
|
|
def test_malformed_csv_cr_first_line
|
|
|
|
error = assert_raise(CSV::MalformedCSVError) do
|
2008-10-10 11:09:34 -04:00
|
|
|
CSV.parse_line("1,2\r,3", row_sep: "\n")
|
2007-12-24 21:46:26 -05:00
|
|
|
end
|
2019-04-14 17:01:51 -04:00
|
|
|
assert_equal("Unquoted fields do not allow new line <\"\\r\"> in line 1.",
|
2018-12-23 02:00:35 -05:00
|
|
|
error.message)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2018-12-23 02:00:35 -05:00
|
|
|
def test_malformed_csv_cr_middle_line
|
|
|
|
csv = <<-CSV
|
2018-09-05 09:33:21 -04:00
|
|
|
line,1,abc
|
|
|
|
line,2,"def\nghi"
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2018-09-05 09:33:21 -04:00
|
|
|
line,4,some\rjunk
|
|
|
|
line,5,jkl
|
|
|
|
CSV
|
2018-12-23 02:00:35 -05:00
|
|
|
|
|
|
|
error = assert_raise(CSV::MalformedCSVError) do
|
|
|
|
CSV.parse(csv)
|
2007-12-24 21:46:26 -05:00
|
|
|
end
|
2019-04-14 17:01:51 -04:00
|
|
|
assert_equal("Unquoted fields do not allow new line <\"\\r\"> in line 4.",
|
2018-12-23 02:00:35 -05:00
|
|
|
error.message)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2018-12-23 02:00:35 -05:00
|
|
|
def test_malformed_csv_unclosed_quote
|
|
|
|
error = assert_raise(CSV::MalformedCSVError) do
|
|
|
|
CSV.parse_line('1,2,"3...')
|
|
|
|
end
|
|
|
|
assert_equal("Unclosed quoted field in line 1.",
|
|
|
|
error.message)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2018-12-23 02:00:35 -05:00
|
|
|
def test_malformed_csv_illegal_quote_middle_line
|
|
|
|
csv = <<-CSV
|
2018-09-05 09:33:21 -04:00
|
|
|
line,1,abc
|
|
|
|
line,2,"def\nghi"
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2018-09-05 09:33:21 -04:00
|
|
|
line,4,8'10"
|
|
|
|
line,5,jkl
|
|
|
|
CSV
|
2018-12-23 02:00:35 -05:00
|
|
|
|
|
|
|
error = assert_raise(CSV::MalformedCSVError) do
|
|
|
|
CSV.parse(csv)
|
* 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
|
2018-12-23 02:00:35 -05:00
|
|
|
assert_equal("Illegal quoting in line 4.",
|
|
|
|
error.message)
|
* 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
|
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_the_parse_fails_fast_when_it_can_for_unquoted_fields
|
|
|
|
assert_parse_errors_out('valid,fields,bad start"' + BIG_DATA)
|
|
|
|
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_the_parse_fails_fast_when_it_can_for_unescaped_quotes
|
|
|
|
assert_parse_errors_out('valid,fields,"bad start"unescaped' + BIG_DATA)
|
|
|
|
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_field_size_limit_controls_lookahead
|
|
|
|
assert_parse_errors_out( 'valid,fields,"' + BIG_DATA + '"',
|
2008-10-10 11:09:34 -04:00
|
|
|
field_size_limit: 2048 )
|
* 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
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2017-01-23 00:59:28 -05:00
|
|
|
def test_field_size_limit_in_extended_column_not_exceeding
|
|
|
|
data = <<~DATA
|
|
|
|
"a","b"
|
|
|
|
"
|
|
|
|
2
|
|
|
|
",""
|
|
|
|
DATA
|
|
|
|
assert_nothing_raised(CSV::MalformedCSVError) do
|
|
|
|
CSV.parse(data, field_size_limit: 4)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_field_size_limit_in_extended_column_exceeding
|
|
|
|
data = <<~DATA
|
|
|
|
"a","b"
|
|
|
|
"
|
|
|
|
2345
|
|
|
|
",""
|
|
|
|
DATA
|
|
|
|
assert_parse_errors_out(data, field_size_limit: 5)
|
|
|
|
end
|
|
|
|
|
2018-12-23 02:00:35 -05:00
|
|
|
def test_row_sep_auto_cr
|
|
|
|
assert_equal([["a"]], CSV.parse("a\r"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_row_sep_auto_lf
|
|
|
|
assert_equal([["a"]], CSV.parse("a\n"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_row_sep_auto_cr_lf
|
|
|
|
assert_equal([["a"]], CSV.parse("a\r\n"))
|
|
|
|
end
|
|
|
|
|
2019-10-12 01:03:21 -04:00
|
|
|
def test_seeked_string_io
|
|
|
|
input_with_bom = StringIO.new("\ufeffあ,い,う\r\na,b,c\r\n")
|
|
|
|
input_with_bom.read(3)
|
|
|
|
assert_equal([
|
|
|
|
["あ", "い", "う"],
|
|
|
|
["a", "b", "c"],
|
|
|
|
],
|
|
|
|
CSV.new(input_with_bom).each.to_a)
|
|
|
|
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
|
|
|
private
|
2019-10-12 01:03:21 -04:00
|
|
|
def assert_parse_errors_out(data, **options)
|
* 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_raise(CSV::MalformedCSVError) do
|
2020-02-25 20:16:32 -05:00
|
|
|
timeout = 0.2
|
2021-01-14 01:36:45 -05:00
|
|
|
if defined?(RubyVM::JIT.enabled?) and RubyVM::JIT.enabled?
|
2020-02-25 20:16:32 -05:00
|
|
|
timeout = 5 # for --jit-wait
|
|
|
|
end
|
2020-02-25 12:27:11 -05:00
|
|
|
Timeout.timeout(timeout) do
|
2019-10-12 01:03:21 -04:00
|
|
|
CSV.parse(data, **options)
|
* 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
|
|
|
fail("Parse didn't error out")
|
|
|
|
end
|
2007-12-24 21:46:26 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|