From 92df7d98b62f48cf21cdec522f2e7b34380fd718 Mon Sep 17 00:00:00 2001 From: Sutou Kouhei Date: Sat, 12 Oct 2019 14:03:21 +0900 Subject: Import CSV 3.1.2 (#2547) --- test/csv/parse/test_general.rb | 14 ++++++++++++-- test/csv/parse/test_header.rb | 4 ++-- test/csv/parse/test_rewind.rb | 2 +- test/csv/test_encodings.rb | 16 +++++++++------- test/csv/test_features.rb | 22 ++++++++++++++++++---- test/csv/write/test_general.rb | 26 ++++++++++++++++++++++++++ 6 files changed, 68 insertions(+), 16 deletions(-) (limited to 'test') diff --git a/test/csv/parse/test_general.rb b/test/csv/parse/test_general.rb index f340921854..655bb26560 100644 --- a/test/csv/parse/test_general.rb +++ b/test/csv/parse/test_general.rb @@ -233,11 +233,21 @@ line,5,jkl assert_equal([["a"]], CSV.parse("a\r\n")) end + 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 + private - def assert_parse_errors_out(*args, **options) + def assert_parse_errors_out(data, **options) assert_raise(CSV::MalformedCSVError) do Timeout.timeout(0.2) do - CSV.parse(*args, **options) + CSV.parse(data, **options) fail("Parse didn't error out") end end diff --git a/test/csv/parse/test_header.rb b/test/csv/parse/test_header.rb index 61346c2aac..481c5107c6 100644 --- a/test/csv/parse/test_header.rb +++ b/test/csv/parse/test_header.rb @@ -312,12 +312,12 @@ A end def test_parse_empty - assert_equal(CSV::Table.new([], **{}), + assert_equal(CSV::Table.new([]), CSV.parse("", headers: true)) end def test_parse_empty_line - assert_equal(CSV::Table.new([], **{}), + assert_equal(CSV::Table.new([]), CSV.parse("\n", headers: true)) end diff --git a/test/csv/parse/test_rewind.rb b/test/csv/parse/test_rewind.rb index 43fd8da159..0aa403b756 100644 --- a/test/csv/parse/test_rewind.rb +++ b/test/csv/parse/test_rewind.rb @@ -6,7 +6,7 @@ require_relative "../helper" class TestCSVParseRewind < Test::Unit::TestCase extend DifferentOFS - def parse(data, options={}) + def parse(data, **options) csv = CSV.new(data, **options) records = csv.to_a csv.rewind diff --git a/test/csv/test_encodings.rb b/test/csv/test_encodings.rb index 151ce7048c..acee03db45 100755 --- a/test/csv/test_encodings.rb +++ b/test/csv/test_encodings.rb @@ -268,11 +268,11 @@ class TestCSVEncodings < Test::Unit::TestCase private - def assert_parses(fields, encoding, options = { }) + def assert_parses(fields, encoding, **options) encoding = Encoding.find(encoding) unless encoding.is_a? Encoding orig_fields = fields fields = encode_ary(fields, encoding) - data = ary_to_data(fields, options) + data = ary_to_data(fields, **options) parsed = CSV.parse(data, **options) assert_equal(fields, parsed) parsed.flatten.each_with_index do |field, i| @@ -285,7 +285,9 @@ class TestCSVEncodings < Test::Unit::TestCase end end begin - CSV.open(@temp_csv_path, "rb:#{encoding}:#{__ENCODING__}", **options) do |csv| + CSV.open(@temp_csv_path, + "rb:#{encoding}:#{__ENCODING__}", + **options) do |csv| csv.each_with_index do |row, i| assert_equal(orig_fields[i], row) end @@ -315,7 +317,7 @@ class TestCSVEncodings < Test::Unit::TestCase ary.map { |row| row.map { |field| field.encode(encoding) } } end - def ary_to_data(ary, options = { }) + def ary_to_data(ary, **options) encoding = ary.flatten.first.encoding quote_char = (options[:quote_char] || '"').encode(encoding) col_sep = (options[:col_sep] || ",").encode(encoding) @@ -327,9 +329,9 @@ class TestCSVEncodings < Test::Unit::TestCase }.join('').encode(encoding) end - def encode_for_tests(data, options = { }) - yield ary_to_data(encode_ary(data, "UTF-8"), options) - yield ary_to_data(encode_ary(data, "UTF-16BE"), options) + def encode_for_tests(data, **options) + yield ary_to_data(encode_ary(data, "UTF-8"), **options) + yield ary_to_data(encode_ary(data, "UTF-16BE"), **options) end def each_encoding diff --git a/test/csv/test_features.rb b/test/csv/test_features.rb index 306b880f6f..d6eb2dc13b 100755 --- a/test/csv/test_features.rb +++ b/test/csv/test_features.rb @@ -52,6 +52,20 @@ line,4,jkl assert_equal([",,,", nil], CSV.parse_line(",,,;", col_sep: ";")) end + def test_col_sep_nil + assert_raise_with_message(ArgumentError, + ":col_sep must be 1 or more characters: nil") do + CSV.parse(@sample_data, col_sep: nil) + end + end + + def test_col_sep_empty + assert_raise_with_message(ArgumentError, + ":col_sep must be 1 or more characters: \"\"") do + CSV.parse(@sample_data, col_sep: "") + end + end + def test_row_sep error = assert_raise(CSV::MalformedCSVError) do CSV.parse_line("1,2,3\n,4,5\r\n", row_sep: "\r\n") @@ -110,10 +124,10 @@ line,4,jkl def test_line lines = [ - %Q(abc,def\n), - %Q(abc,"d\nef"\n), - %Q(abc,"d\r\nef"\n), - %Q(abc,"d\ref") + %Q(\u{3000}abc,def\n), + %Q(\u{3000}abc,"d\nef"\n), + %Q(\u{3000}abc,"d\r\nef"\n), + %Q(\u{3000}abc,"d\ref") ] csv = CSV.new(lines.join('')) lines.each do |line| diff --git a/test/csv/write/test_general.rb b/test/csv/write/test_general.rb index c879f54e74..d157b74ba1 100644 --- a/test/csv/write/test_general.rb +++ b/test/csv/write/test_general.rb @@ -205,6 +205,32 @@ module TestCSVWriteGeneral assert_equal(%Q[あ,い,う#{$INPUT_RECORD_SEPARATOR}].encode("EUC-JP"), generate_line(row)) end + + def test_encoding_with_default_internal + with_default_internal(Encoding::UTF_8) do + row = ["あ", "い", "う"].collect {|field| field.encode("EUC-JP")} + assert_equal(%Q[あ,い,う#{$INPUT_RECORD_SEPARATOR}].encode("EUC-JP"), + generate_line(row, encoding: Encoding::EUC_JP)) + end + end + + def test_with_default_internal + with_default_internal(Encoding::UTF_8) do + row = ["あ", "い", "う"].collect {|field| field.encode("EUC-JP")} + assert_equal(%Q[あ,い,う#{$INPUT_RECORD_SEPARATOR}].encode("EUC-JP"), + generate_line(row)) + end + end + + def with_default_internal(encoding) + original = Encoding.default_internal + begin + Encoding.default_internal = encoding + yield + ensure + Encoding.default_internal = original + end + end end class TestCSVWriteGeneralGenerateLine < Test::Unit::TestCase -- cgit v1.2.3