summaryrefslogtreecommitdiff
path: root/test/csv
diff options
context:
space:
mode:
authorkou <kou@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2019-01-25 06:49:59 +0000
committerkou <kou@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2019-01-25 06:49:59 +0000
commit24b57b102c1992b679f8f8c0fd1a0239289a129b (patch)
treee25055dbfcef70d3b01855c004e57e130b3fac5f /test/csv
parentcdca14e75e98e2657daa40589f18a0ad46846020 (diff)
Upgrade CSV to 3.0.4
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66922 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/csv')
-rw-r--r--test/csv/helper.rb (renamed from test/csv/base.rb)4
-rw-r--r--test/csv/parse/test_column_separator.rb40
-rw-r--r--test/csv/parse/test_convert.rb110
-rw-r--r--test/csv/parse/test_each.rb23
-rw-r--r--[-rwxr-xr-x]test/csv/parse/test_general.rb (renamed from test/csv/test_csv_parsing.rb)28
-rw-r--r--[-rwxr-xr-x]test/csv/parse/test_header.rb (renamed from test/csv/test_headers.rb)33
-rw-r--r--test/csv/parse/test_liberal_parsing.rb93
-rw-r--r--test/csv/parse/test_rewind.rb40
-rw-r--r--test/csv/parse/test_unconverted_fields.rb117
-rwxr-xr-xtest/csv/test_csv_writing.rb95
-rwxr-xr-xtest/csv/test_data_converters.rb242
-rwxr-xr-xtest/csv/test_encodings.rb11
-rwxr-xr-xtest/csv/test_features.rb85
-rwxr-xr-xtest/csv/test_interface.rb11
-rwxr-xr-xtest/csv/test_row.rb30
-rwxr-xr-xtest/csv/test_table.rb20
-rw-r--r--test/csv/ts_all.rb19
-rw-r--r--test/csv/write/test_general.rb228
-rw-r--r--test/csv/write/test_quote_empty.rb70
19 files changed, 788 insertions, 511 deletions
diff --git a/test/csv/base.rb b/test/csv/helper.rb
index a282c7a..4f7b002 100644
--- a/test/csv/base.rb
+++ b/test/csv/helper.rb
@@ -1,9 +1,5 @@
-# frozen_string_literal: false
require "test/unit"
require "csv"
require_relative "../lib/with_different_ofs.rb"
-
-class TestCSV < Test::Unit::TestCase
-end
diff --git a/test/csv/parse/test_column_separator.rb b/test/csv/parse/test_column_separator.rb
new file mode 100644
index 0000000..d6eaa7b
--- /dev/null
+++ b/test/csv/parse/test_column_separator.rb
@@ -0,0 +1,40 @@
+# -*- coding: utf-8 -*-
+# frozen_string_literal: false
+
+require_relative "../helper"
+
+class TestCSVParseColumnSeparator < Test::Unit::TestCase
+ extend DifferentOFS
+
+ def test_comma
+ assert_equal([["a", "b", nil, "d"]],
+ CSV.parse("a,b,,d", col_sep: ","))
+ end
+
+ def test_space
+ assert_equal([["a", "b", nil, "d"]],
+ CSV.parse("a b d", col_sep: " "))
+ end
+
+ def test_tab
+ assert_equal([["a", "b", nil, "d"]],
+ CSV.parse("a\tb\t\td", col_sep: "\t"))
+ end
+
+ def test_multiple_characters_include_sub_separator
+ assert_equal([["a b", nil, "d"]],
+ CSV.parse("a b d", col_sep: " "))
+ end
+
+ def test_multiple_characters_leading_empty_fields
+ data = <<-CSV
+<=><=>A<=>B<=>C
+1<=>2<=>3
+ CSV
+ assert_equal([
+ [nil, nil, "A", "B", "C"],
+ ["1", "2", "3"],
+ ],
+ CSV.parse(data, col_sep: "<=>"))
+ end
+end
diff --git a/test/csv/parse/test_convert.rb b/test/csv/parse/test_convert.rb
new file mode 100644
index 0000000..bfe6ddd
--- /dev/null
+++ b/test/csv/parse/test_convert.rb
@@ -0,0 +1,110 @@
+# -*- coding: utf-8 -*-
+# frozen_string_literal: false
+
+require_relative "../helper"
+
+class TestCSVParseConvert < Test::Unit::TestCase
+ extend DifferentOFS
+
+ def setup
+ super
+ @data = "Numbers,:integer,1,:float,3.015"
+ @parser = CSV.new(@data)
+
+ @custom = lambda {|field| /\A:(\S.*?)\s*\Z/ =~ field ? $1.to_sym : field}
+
+ @time = Time.utc(2018, 12, 30, 6, 41, 29)
+ @windows_safe_time_data = @time.strftime("%a %b %d %H:%M:%S %Y")
+ end
+
+ def test_integer
+ @parser.convert(:integer)
+ assert_equal(["Numbers", ":integer", 1, ":float", "3.015"],
+ @parser.shift)
+ end
+
+ def test_float
+ @parser.convert(:float)
+ assert_equal(["Numbers", ":integer", 1.0, ":float", 3.015],
+ @parser.shift)
+ end
+
+ def test_float_integer
+ @parser.convert(:float)
+ @parser.convert(:integer)
+ assert_equal(["Numbers", ":integer", 1.0, ":float", 3.015],
+ @parser.shift)
+ end
+
+ def test_integer_float
+ @parser.convert(:integer)
+ @parser.convert(:float)
+ assert_equal(["Numbers", ":integer", 1, ":float", 3.015],
+ @parser.shift)
+ end
+
+ def test_numberic
+ @parser.convert(:numeric)
+ assert_equal(["Numbers", ":integer", 1, ":float", 3.015],
+ @parser.shift)
+ end
+
+ def test_all
+ @data << ",#{@windows_safe_time_data}"
+ @parser = CSV.new(@data)
+ @parser.convert(:all)
+ assert_equal(["Numbers", ":integer", 1, ":float", 3.015, @time.to_datetime],
+ @parser.shift)
+ end
+
+ def test_custom
+ @parser.convert do |field|
+ /\A:(\S.*?)\s*\Z/ =~ field ? $1.to_sym : field
+ end
+ assert_equal(["Numbers", :integer, "1", :float, "3.015"],
+ @parser.shift)
+ end
+
+ def test_builtin_custom
+ @parser.convert(:numeric)
+ @parser.convert(&@custom)
+ assert_equal(["Numbers", :integer, 1, :float, 3.015],
+ @parser.shift)
+ end
+
+ def test_custom_field_info_line
+ @parser.convert do |field, info|
+ assert_equal(1, info.line)
+ info.index == 4 ? Float(field).floor : field
+ end
+ assert_equal(["Numbers", ":integer", "1", ":float", 3],
+ @parser.shift)
+ end
+
+ def test_custom_field_info_header
+ headers = ["one", "two", "three", "four", "five"]
+ @parser = CSV.new(@data, headers: headers)
+ @parser.convert do |field, info|
+ info.header == "three" ? Integer(field) * 100 : field
+ end
+ assert_equal(CSV::Row.new(headers,
+ ["Numbers", ":integer", 100, ":float", "3.015"]),
+ @parser.shift)
+ end
+
+ def test_custom_blank_field
+ converter = lambda {|field| field.nil?}
+ row = CSV.parse_line('nil,', converters: converter)
+ assert_equal([false, true], row)
+ end
+
+ def test_nil_value
+ assert_equal(["nil", "", "a"],
+ CSV.parse_line(',"",a', nil_value: "nil"))
+ end
+
+ def test_empty_value
+ assert_equal([nil, "empty", "a"],
+ CSV.parse_line(',"",a', empty_value: "empty"))
+ end
+end
diff --git a/test/csv/parse/test_each.rb b/test/csv/parse/test_each.rb
new file mode 100644
index 0000000..ce0b71d
--- /dev/null
+++ b/test/csv/parse/test_each.rb
@@ -0,0 +1,23 @@
+# -*- coding: utf-8 -*-
+# frozen_string_literal: false
+
+require_relative "../helper"
+
+class TestCSVParseEach < Test::Unit::TestCase
+ extend DifferentOFS
+
+ def test_twice
+ data = <<-CSV
+Ruby,2.6.0,script
+ CSV
+ csv = CSV.new(data)
+ assert_equal([
+ [["Ruby", "2.6.0", "script"]],
+ [],
+ ],
+ [
+ csv.to_a,
+ csv.to_a,
+ ])
+ end
+end
diff --git a/test/csv/test_csv_parsing.rb b/test/csv/parse/test_general.rb
index 3fe1bd7..2f235f1 100755..100644
--- a/test/csv/test_csv_parsing.rb
+++ b/test/csv/parse/test_general.rb
@@ -1,14 +1,9 @@
-#!/usr/bin/env ruby -w
-# encoding: UTF-8
+# -*- coding: utf-8 -*-
# frozen_string_literal: false
-# tc_csv_parsing.rb
-#
-# Created by James Edward Gray II on 2005-10-31.
-
require "timeout"
-require_relative "base"
+require_relative "../helper"
#
# Following tests are my interpretation of the
@@ -16,7 +11,7 @@ require_relative "base"
# document in one place (intentionally) and that is to make the default row
# separator <tt>$/</tt>.
#
-class TestCSV::Parsing < TestCSV
+class TestCSVParseGeneral < Test::Unit::TestCase
extend DifferentOFS
BIG_DATA = "123456789\n" * 1024
@@ -226,16 +221,6 @@ line,5,jkl
assert_parse_errors_out(data, field_size_limit: 5)
end
- def test_col_sep_comma
- assert_equal([["a", "b", nil, "d"]],
- CSV.parse("a,b,,d", col_sep: ","))
- end
-
- def test_col_sep_space
- assert_equal([["a", "b", nil, "d"]],
- CSV.parse("a b d", col_sep: " "))
- end
-
def test_row_sep_auto_cr
assert_equal([["a"]], CSV.parse("a\r"))
end
@@ -248,14 +233,7 @@ line,5,jkl
assert_equal([["a"]], CSV.parse("a\r\n"))
end
- def test_headers_empty_line
- assert_equal(CSV::Table.new([CSV::Row.new(["header1"], [])],
- headers: ["header1"]),
- CSV.parse("\n", headers: "header1"))
- end
-
private
-
def assert_parse_errors_out(*args)
assert_raise(CSV::MalformedCSVError) do
Timeout.timeout(0.2) do
diff --git a/test/csv/test_headers.rb b/test/csv/parse/test_header.rb
index 3ebb5cf..d92d823 100755..100644
--- a/test/csv/test_headers.rb
+++ b/test/csv/parse/test_header.rb
@@ -1,14 +1,9 @@
-#!/usr/bin/env ruby -w
-# encoding: UTF-8
+# -*- coding: utf-8 -*-
# frozen_string_literal: false
-# tc_headers.rb
-#
-# Created by James Edward Gray II on 2005-10-31.
+require_relative "../helper"
-require_relative "base"
-
-class TestCSV::Headers < TestCSV
+class TestCSVHeaders < Test::Unit::TestCase
extend DifferentOFS
def setup
@@ -315,4 +310,26 @@ A
assert_equal([["A"], [nil]],
[row.headers, row.fields])
end
+
+ 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
end
diff --git a/test/csv/parse/test_liberal_parsing.rb b/test/csv/parse/test_liberal_parsing.rb
new file mode 100644
index 0000000..22b1689
--- /dev/null
+++ b/test/csv/parse/test_liberal_parsing.rb
@@ -0,0 +1,93 @@
+# -*- coding: utf-8 -*-
+# frozen_string_literal: false
+
+require_relative "../helper"
+
+class TestCSVParseLiberalParsing < Test::Unit::TestCase
+ extend DifferentOFS
+
+ def test_middle_quote_start
+ input = '"Johnson, Dwayne",Dwayne "The Rock" Johnson'
+ error = assert_raise(CSV::MalformedCSVError) do
+ CSV.parse_line(input)
+ end
+ assert_equal("Illegal quoting in line 1.",
+ error.message)
+ assert_equal(["Johnson, Dwayne", 'Dwayne "The Rock" Johnson'],
+ CSV.parse_line(input, liberal_parsing: true))
+ end
+
+ def test_middle_quote_end
+ input = '"quoted" field'
+ error = assert_raise(CSV::MalformedCSVError) do
+ CSV.parse_line(input)
+ end
+ assert_equal("Do not allow except col_sep_split_separator " +
+ "after quoted fields in line 1.",
+ error.message)
+ assert_equal(['"quoted" field'],
+ CSV.parse_line(input, liberal_parsing: true))
+ end
+
+ def test_quote_after_column_separator
+ error = assert_raise(CSV::MalformedCSVError) do
+ CSV.parse_line('is,this "three," or four,fields', liberal_parsing: true)
+ end
+ assert_equal("Unclosed quoted field in line 1.",
+ error.message)
+ end
+
+ def test_quote_before_column_separator
+ assert_equal(["is", 'this "three', ' or four"', "fields"],
+ CSV.parse_line('is,this "three, or four",fields',
+ liberal_parsing: true))
+ end
+
+ def test_backslash_quote
+ assert_equal([
+ "1",
+ "\"Hamlet says, \\\"Seems",
+ "\\\" madam! Nay it is; I know not \\\"seems.\\\"\"",
+ ],
+ CSV.parse_line('1,' +
+ '"Hamlet says, \"Seems,' +
+ '\" madam! Nay it is; I know not \"seems.\""',
+ liberal_parsing: true))
+ end
+
+ def test_space_quote
+ input = <<~CSV
+ Los Angeles, 34°03'N, 118°15'W
+ New York City, 40°42'46"N, 74°00'21"W
+ Paris, 48°51'24"N, 2°21'03"E
+ CSV
+ assert_equal(
+ [
+ ["Los Angeles", " 34°03'N", " 118°15'W"],
+ ["New York City", " 40°42'46\"N", " 74°00'21\"W"],
+ ["Paris", " 48°51'24\"N", " 2°21'03\"E"],
+ ],
+ CSV.parse(input, liberal_parsing: true))
+ end
+
+ def test_double_quote_outside_quote
+ data = %Q{a,""b""}
+ error = assert_raise(CSV::MalformedCSVError) do
+ CSV.parse(data)
+ end
+ assert_equal("Do not allow except col_sep_split_separator " +
+ "after quoted fields in line 1.",
+ error.message)
+ assert_equal([
+ [["a", %Q{""b""}]],
+ [["a", %Q{"b"}]],
+ ],
+ [
+ CSV.parse(data, liberal_parsing: true),
+ CSV.parse(data,
+ liberal_parsing: {
+ double_quote_outside_quote: true,
+ }),
+ ])
+ end
+end
diff --git a/test/csv/parse/test_rewind.rb b/test/csv/parse/test_rewind.rb
new file mode 100644
index 0000000..73a69e9
--- /dev/null
+++ b/test/csv/parse/test_rewind.rb
@@ -0,0 +1,40 @@
+# -*- coding: utf-8 -*-
+# frozen_string_literal: false
+
+require_relative "../helper"
+
+class TestCSVParseRewind < Test::Unit::TestCase
+ extend DifferentOFS
+
+ def parse(data, options={})
+ csv = CSV.new(data, options)
+ records = csv.to_a
+ csv.rewind
+ [records, csv.to_a]
+ end
+
+ def test_default
+ data = <<-CSV
+Ruby,2.6.0,script
+ CSV
+ assert_equal([
+ [["Ruby", "2.6.0", "script"]],
+ [["Ruby", "2.6.0", "script"]],
+ ],
+ parse(data))
+ end
+
+ def test_have_headers
+ data = <<-CSV
+Language,Version,Type
+Ruby,2.6.0,script
+ CSV
+ assert_equal([
+ [CSV::Row.new(["Language", "Version", "Type"],
+ ["Ruby", "2.6.0", "script"])],
+ [CSV::Row.new(["Language", "Version", "Type"],
+ ["Ruby", "2.6.0", "script"])],
+ ],
+ parse(data, headers: true))
+ end
+end
diff --git a/test/csv/parse/test_unconverted_fields.rb b/test/csv/parse/test_unconverted_fields.rb
new file mode 100644
index 0000000..437124e
--- /dev/null
+++ b/test/csv/parse/test_unconverted_fields.rb
@@ -0,0 +1,117 @@
+# -*- coding: utf-8 -*-
+# frozen_string_literal: false
+
+require_relative "../helper"
+
+class TestCSVParseUnconvertedFields < Test::Unit::TestCase
+ extend DifferentOFS
+
+ def setup
+ super
+ @custom = lambda {|field| /\A:(\S.*?)\s*\Z/ =~ field ? $1.to_sym : field}
+
+ @headers = ["first", "second", "third"]
+ @data = <<-CSV
+first,second,third
+1,2,3
+ CSV
+ end
+
+
+ def test_custom
+ row = CSV.parse_line("Numbers,:integer,1,:float,3.015",
+ converters: [:numeric, @custom],
+ unconverted_fields: true)
+ assert_equal([
+ ["Numbers", :integer, 1, :float, 3.015],
+ ["Numbers", ":integer", "1", ":float", "3.015"],
+ ],
+ [
+ row,
+ row.unconverted_fields,
+ ])
+ end
+
+ def test_no_fields
+ row = CSV.parse_line("\n",
+ converters: [:numeric, @custom],
+ unconverted_fields: true)
+ assert_equal([
+ [],
+ [],
+ ],
+ [
+ row,
+ row.unconverted_fields,
+ ])
+ end
+
+ def test_parsed_header
+ row = CSV.parse_line(@data,
+ converters: :numeric,
+ unconverted_fields: true,
+ headers: :first_row)
+ assert_equal([
+ CSV::Row.new(@headers,
+ [1, 2, 3]),
+ ["1", "2", "3"],
+ ],
+ [
+ row,
+ row.unconverted_fields,
+ ])
+ end
+
+ def test_return_headers
+ row = CSV.parse_line(@data,
+ converters: :numeric,
+ unconverted_fields: true,
+ headers: :first_row,
+ return_headers: true)
+ assert_equal([
+ CSV::Row.new(@headers,
+ @headers),
+ @headers,
+ ],
+ [
+ row,
+ row.unconverted_fields,
+ ])
+ end
+
+ def test_header_converters
+ row = CSV.parse_line(@data,
+ converters: :numeric,
+ unconverted_fields: true,
+ headers: :first_row,
+ return_headers: true,
+ header_converters: :symbol)
+ assert_equal([
+ CSV::Row.new(@headers.collect(&:to_sym),
+ @headers),
+ @headers,
+ ],
+ [
+ row,
+ row.unconverted_fields,
+ ])
+ end
+
+ def test_specified_headers
+ row = CSV.parse_line("\n",
+ converters: :numeric,
+ unconverted_fields: true,
+ headers: %w{my new headers},
+ return_headers: true,
+ header_converters: :symbol)
+ assert_equal([
+ CSV::Row.new([:my, :new, :headers],
+ ["my", "new", "headers"]),
+ [],
+ ],
+ [
+ row,
+ row.unconverted_fields,
+ ])
+ end
+end
diff --git a/test/csv/test_csv_writing.rb b/test/csv/test_csv_writing.rb
deleted file mode 100755
index e1c02c1..0000000
--- a/test/csv/test_csv_writing.rb
+++ /dev/null
@@ -1,95 +0,0 @@
-#!/usr/bin/env ruby -w
-# encoding: UTF-8
-# frozen_string_literal: false
-
-# tc_csv_writing.rb
-#
-# Created by James Edward Gray II on 2005-10-31.
-require_relative "base"
-
-class TestCSV::Writing < TestCSV
- extend DifferentOFS
-
- 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"],
- col_sep: ";" ) )
- assert_equal( "a\tb\t\tc\n", CSV.generate_line( ["a", "b", nil, "c"],
- col_sep: "\t" ) )
- end
-
- def test_row_sep
- assert_equal( "a,b,,c\r\n", CSV.generate_line( ["a", "b", nil, "c"],
- row_sep: "\r\n" ) )
- end
-
- def test_force_quotes
- assert_equal( %Q{"1","b","","already ""quoted"""\n},
- CSV.generate_line( [1, "b", nil, %Q{already "quoted"}],
- force_quotes: true ) )
- end
-end
diff --git a/test/csv/test_data_converters.rb b/test/csv/test_data_converters.rb
index 8b3163d..1620e07 100755
--- a/test/csv/test_data_converters.rb
+++ b/test/csv/test_data_converters.rb
@@ -1,23 +1,13 @@
-#!/usr/bin/env ruby -w
-# encoding: UTF-8
+# -*- coding: utf-8 -*-
# frozen_string_literal: false
-# tc_data_converters.rb
-#
-# Created by James Edward Gray II on 2005-10-31.
+require_relative "helper"
-require_relative "base"
-
-class TestCSV::DataConverters < TestCSV
+class TestCSVDataConverters < Test::Unit::TestCase
extend DifferentOFS
def setup
super
- @data = "Numbers,:integer,1,:float,3.015"
- @parser = CSV.new(@data)
-
- @custom = lambda { |field| field =~ /\A:(\S.*?)\s*\Z/ ? $1.to_sym : field }
-
@win_safe_time_str = Time.now.strftime("%a %b %d %H:%M:%S %Y")
end
@@ -113,230 +103,4 @@ class TestCSV::DataConverters < TestCSV
assert_equal(datetime,
CSV::Converters[:date_time][iso8601_string])
end
-
- def test_convert_with_builtin_integer
- # setup parser...
- assert_respond_to(@parser, :convert)
- assert_nothing_raised(Exception) { @parser.convert(:integer) }
-
- # and use
- assert_equal(["Numbers", ":integer", 1, ":float", "3.015"], @parser.shift)
- end
-
- def test_convert_with_builtin_float
- # setup parser...
- assert_respond_to(@parser, :convert)
- assert_nothing_raised(Exception) { @parser.convert(:float) }
-
- # and use
- assert_equal(["Numbers", ":integer", 1.0, ":float", 3.015], @parser.shift)
- end
-
- def test_convert_order_float_integer
- # floats first, then integers...
- assert_nothing_raised(Exception) do
- @parser.convert(:float)
- @parser.convert(:integer)
- end
-
- # gets us nothing but floats
- assert_equal( [String, String, Float, String, Float],
- @parser.shift.map { |field| field.class } )
- end
-
- def test_convert_order_integer_float
- # integers have precendance...
- assert_nothing_raised(Exception) do
- @parser.convert(:integer)
- @parser.convert(:float)
- end
-
- # gives us proper number conversion
- assert_equal( [String, String, 0.class, String, Float],
- @parser.shift.map { |field| field.class } )
- end
-
- def test_builtin_numeric_combo_converter
- # setup parser...
- assert_nothing_raised(Exception) { @parser.convert(:numeric) }
-
- # and use
- assert_equal( [String, String, 0.class, String, Float],
- @parser.shift.map { |field| field.class } )
- end
-
- def test_builtin_all_nested_combo_converter
- # setup parser...
- @data << ",#{@win_safe_time_str}" # add a DateTime field
- @parser = CSV.new(@data) # reset parser
- assert_nothing_raised(Exception) { @parser.convert(:all) }
-
- # and use
- assert_equal( [String, String, 0.class, String, Float, DateTime],
- @parser.shift.map { |field| field.class } )
- end
-
- def test_convert_with_custom_code
- # define custom converter...
- assert_nothing_raised(Exception) do
- @parser.convert { |field| field =~ /\A:(\S.*?)\s*\Z/ ? $1.to_sym : field }
- end
-
- # and use
- assert_equal(["Numbers", :integer, "1", :float, "3.015"], @parser.shift)
- end
-
- def test_convert_with_custom_code_mix
- # mix built-in and custom...
- assert_nothing_raised(Exception) { @parser.convert(:numeric) }
- assert_nothing_raised(Exception) { @parser.convert(&@custom) }
-
- # and use
- assert_equal(["Numbers", :integer, 1, :float, 3.015], @parser.shift)
- end
-
- def test_convert_with_custom_code_using_field_info
- # define custom converter that uses field information...
- assert_nothing_raised(Exception) do
- @parser.convert do |field, info|
- assert_equal(1, info.line)
- info.index == 4 ? Float(field).floor : field
- end
- end
-
- # and use
- assert_equal(["Numbers", ":integer", "1", ":float", 3], @parser.shift)
- end
-
- def test_convert_with_custom_code_using_field_info_header
- @parser = CSV.new(@data, headers: %w{one two three four five})
-
- # define custom converter that uses field header information...
- assert_nothing_raised(Exception) do
- @parser.convert do |field, info|
- info.header == "three" ? Integer(field) * 100 : field
- end
- end
-
- # and use
- assert_equal( ["Numbers", ":integer", 100, ":float", "3.015"],
- @parser.shift.fields )
- end
-
- def test_custom_converter_with_blank_field
- converter = lambda { |field| field.nil? }
- row = nil
- assert_nothing_raised(Exception) do
- row = CSV.parse_line('nil,', converters: converter)
- end
- assert_equal([false, true], row);
- end
-
- def test_shortcut_interface
- assert_equal( ["Numbers", ":integer", 1, ":float", 3.015],
- CSV.parse_line(@data, converters: :numeric) )
-
- assert_equal( ["Numbers", ":integer", 1, ":float", 3.015],
- CSV.parse_line(@data, converters: [:integer, :float]) )
-
- assert_equal( ["Numbers", :integer, 1, :float, 3.015],
- CSV.parse_line(@data, converters: [:numeric, @custom]) )
- end
-
- def test_unconverted_fields_number
- row = CSV.parse_line(@data,
- converters: [:numeric, @custom],
- unconverted_fields: true)
- assert_equal([
- ["Numbers", :integer, 1, :float, 3.015],
- ["Numbers", ":integer", "1", ":float", "3.015"],
- ],
- [
- row,
- row.unconverted_fields,
- ])
- end
-
- def test_unconverted_fields_empty_line
- row = CSV.parse_line("\n",
- converters: [:numeric, @custom],
- unconverted_fields: true)
- assert_equal([
- [],
- [],
- ],
- [
- row,
- row.unconverted_fields,
- ])
- end
-
- def test_unconverted_fields
- data = <<-CSV
-first,second,third
-1,2,3
- CSV
- row = nil
- assert_nothing_raised(Exception) do
- row = CSV.parse_line( data,
- converters: :numeric,
- unconverted_fields: true,
- headers: :first_row )
- end
- assert_not_nil(row)
- assert_equal([["first", 1], ["second", 2], ["third", 3]], row.to_a)
- assert_respond_to(row, :unconverted_fields)
- assert_equal(%w{1 2 3}, row.unconverted_fields)
-
- assert_nothing_raised(Exception) do
- row = CSV.parse_line( data,
- converters: :numeric,
- unconverted_fields: true,
- headers: :first_row,
- return_headers: true )
- end
- assert_not_nil(row)
- assert_equal( [%w{first first}, %w{second second}, %w{third third}],
- row.to_a )
- assert_respond_to(row, :unconverted_fields)
- assert_equal(%w{first second third}, row.unconverted_fields)
-
- assert_nothing_raised(Exception) do
- row = CSV.parse_line( data,
- converters: :numeric,
- unconverted_fields: true,
- headers: :first_row,
- return_headers: true,
- header_converters: :symbol )
- end
- assert_not_nil(row)
- assert_equal( [[:first, "first"], [:second, "second"], [:third, "third"]],
- row.to_a )
- assert_respond_to(row, :unconverted_fields)
- assert_equal(%w{first second third}, row.unconverted_fields)
-
- assert_nothing_raised(Exception) do
- row = CSV.parse_line( data,
- converters: :numeric,
- unconverted_fields: true,
- headers: %w{my new headers},
- return_headers: true,
- header_converters: :symbol )
- end
- assert_not_nil(row)
- assert_equal( [[:my, "my"], [:new, "new"], [:headers, "headers"]],
- row.to_a )
- assert_respond_to(row, :unconverted_fields)
- assert_equal(Array.new, row.unconverted_fields)
- end
-
- def test_nil_value
- assert_equal(["nil", "", "a"],
- CSV.parse_line(',"",a', nil_value: "nil"))
- end
-
- def test_empty_value
- assert_equal([nil, "empty", "a"],
- CSV.parse_line(',"",a', empty_value: "empty"))
- end
end
diff --git a/test/csv/test_encodings.rb b/test/csv/test_encodings.rb
index fcad90e..01101f1 100755
--- a/test/csv/test_encodings.rb
+++ b/test/csv/test_encodings.rb
@@ -1,14 +1,9 @@
-#!/usr/bin/env ruby -w
-# encoding: UTF-8
+# -*- coding: utf-8 -*-
# frozen_string_literal: false
-# tc_encodings.rb
-#
-# Created by James Edward Gray II on 2005-10-31.
+require_relative "helper"
-require_relative "base"
-
-class TestCSV::Encodings < TestCSV
+class TestCSVEncodings < Test::Unit::TestCase
extend DifferentOFS
def setup
diff --git a/test/csv/test_features.rb b/test/csv/test_features.rb
index 53b513d..0b92776 100755
--- a/test/csv/test_features.rb
+++ b/test/csv/test_features.rb
@@ -1,20 +1,15 @@
-#!/usr/bin/env ruby -w
-# encoding: UTF-8
+# -*- coding: utf-8 -*-
# frozen_string_literal: false
-# tc_features.rb
-#
-# Created by James Edward Gray II on 2005-10-31.
-
begin
require "zlib"
rescue LoadError
end
-require_relative "base"
+require_relative "helper"
require "tempfile"
-class TestCSV::Features < TestCSV
+class TestCSVFeatures < Test::Unit::TestCase
extend DifferentOFS
TEST_CASES = [ [%Q{a,b}, ["a", "b"]],
@@ -168,70 +163,6 @@ line,4,jkl
assert_equal(3, count)
end
- def test_liberal_parsing_middle_quote_start
- input = '"Johnson, Dwayne",Dwayne "The Rock" Johnson'
- error = assert_raise(CSV::MalformedCSVError) do
- CSV.parse_line(input)
- end
- assert_equal("Illegal quoting in line 1.",
- error.message)
- assert_equal(["Johnson, Dwayne", 'Dwayne "The Rock" Johnson'],
- CSV.parse_line(input, liberal_parsing: true))
- end
-
- def test_liberal_parsing_middle_quote_end
- input = '"quoted" field'
- error = assert_raise(CSV::MalformedCSVError) do
- CSV.parse_line(input)
- end
- assert_equal("Do not allow except col_sep_split_separator " +
- "after quoted fields in line 1.",
- error.message)
- assert_equal(['"quoted" field'],
- CSV.parse_line(input, liberal_parsing: true))
- end
-
- def test_liberal_parsing_quote_after_column_separator
- error = assert_raise(CSV::MalformedCSVError) do
- CSV.parse_line('is,this "three," or four,fields', liberal_parsing: true)
- end
- assert_equal("Unclosed quoted field in line 1.",
- error.message)
- end
-
- def test_liberal_parsing_quote_before_column_separator
- assert_equal(["is", 'this "three', ' or four"', "fields"],
- CSV.parse_line('is,this "three, or four",fields',
- liberal_parsing: true))
- end
-
- def test_liberal_parsing_backslash_quote
- assert_equal([
- "1",
- "\"Hamlet says, \\\"Seems",
- "\\\" madam! Nay it is; I know not \\\"seems.\\\"\"",
- ],
- CSV.parse_line('1,' +
- '"Hamlet says, \"Seems,' +
- '\" madam! Nay it is; I know not \"seems.\""',
- liberal_parsing: true))
- end
-
- def test_liberal_parsing_space_quote
- input = <<~CSV
- Los Angeles, 34°03'N, 118°15'W
- New York City, 40°42'46"N, 74°00'21"W
- Paris, 48°51'24"N, 2°21'03"E
- CSV
- assert_equal(
- [
- ["Los Angeles", " 34°03'N", " 118°15'W"],
- ["New York City", " 40°42'46\"N", " 74°00'21\"W"],
- ["Paris", " 48°51'24\"N", " 2°21'03\"E"],
- ],
- CSV.parse(input, liberal_parsing: true))
- end
-
def test_csv_behavior_readers
%w[ unconverted_fields return_headers write_headers
skip_blanks force_quotes ].each do |behavior|
@@ -289,16 +220,6 @@ line,4,jkl
csv.each {|row| assert_predicate row, :header_row?}
end
- # reported by Dave Burt
- def test_leading_empty_fields_with_multibyte_col_sep
- data = <<-CSV
-<=><=>A<=>B<=>C
-1<=>2<=>3
- CSV
- parsed = CSV.parse(data, col_sep: "<=>")
- assert_equal([[nil, nil, "A", "B", "C"], ["1", "2", "3"]], parsed)
- end
-
def test_gzip_reader
zipped = nil
assert_nothing_raised(NoMethodError) do
diff --git a/test/csv/test_interface.rb b/test/csv/test_interface.rb
index 309fbbb..77730fa 100755
--- a/test/csv/test_interface.rb
+++ b/test/csv/test_interface.rb
@@ -1,15 +1,10 @@
-#!/usr/bin/env ruby -w
-# encoding: UTF-8
+# -*- coding: utf-8 -*-
# frozen_string_literal: false
-# tc_interface.rb
-#
-# Created by James Edward Gray II on 2005-10-31.
-
-require_relative "base"
+require_relative "helper"
require "tempfile"
-class TestCSV::Interface < TestCSV
+class TestCSVInterface < Test::Unit::TestCase
extend DifferentOFS
def setup
diff --git a/test/csv/test_row.rb b/test/csv/test_row.rb
index 67ed65c..f709dd3 100755
--- a/test/csv/test_row.rb
+++ b/test/csv/test_row.rb
@@ -1,14 +1,9 @@
-#!/usr/bin/env ruby -w
-# encoding: UTF-8
+# -*- coding: utf-8 -*-
# frozen_string_literal: false
-# tc_row.rb
-#
-# Created by James Edward Gray II on 2005-10-31.
+require_relative "helper"
-require_relative "base"
-
-class TestCSV::Row < TestCSV
+class TestCSVRow < Test::Unit::TestCase
extend DifferentOFS
def setup
@@ -105,6 +100,19 @@ class TestCSV::Row < TestCSV
def test_has_key?
assert_equal(true, @row.has_key?('B'))
assert_equal(false, @row.has_key?('foo'))
+
+ # aliases
+ assert_equal(true, @row.header?('B'))
+ assert_equal(false, @row.header?('foo'))
+
+ assert_equal(true, @row.include?('B'))
+ assert_equal(false, @row.include?('foo'))
+
+ assert_equal(true, @row.member?('B'))
+ assert_equal(false, @row.member?('foo'))
+
+ assert_equal(true, @row.key?('B'))
+ assert_equal(false, @row.key?('foo'))
end
def test_set_field
@@ -261,12 +269,6 @@ class TestCSV::Row < TestCSV
end
def test_queries
- # headers
- assert_send([@row, :header?, "A"])
- assert_send([@row, :header?, "C"])
- assert_not_send([@row, :header?, "Z"])
- assert_send([@row, :include?, "A"]) # alias
-
# fields
assert(@row.field?(4))
assert(@row.field?(nil))
diff --git a/test/csv/test_table.rb b/test/csv/test_table.rb
index c341f17..50edc77 100755
--- a/test/csv/test_table.rb
+++ b/test/csv/test_table.rb
@@ -1,14 +1,9 @@
-#!/usr/bin/env ruby -w
-# encoding: UTF-8
+# -*- coding: utf-8 -*-
# frozen_string_literal: false
-# tc_table.rb
-#
-# Created by James Edward Gray II on 2005-10-31.
+require_relative "helper"
-require_relative "base"
-
-class TestCSV::Table < TestCSV
+class TestCSVTable < Test::Unit::TestCase
extend DifferentOFS
def setup
@@ -21,7 +16,7 @@ class TestCSV::Table < TestCSV
@header_table = CSV::Table.new(
[CSV::Row.new(%w{A B C}, %w{A B C}, true)] + @rows
)
-
+
@header_only_table = CSV::Table.new([], headers: %w{A B C})
end
@@ -69,6 +64,13 @@ class TestCSV::Table < TestCSV
assert_equal(%w[A B C], @header_only_table.headers)
end
+ def test_headers_modified_by_row
+ table = CSV::Table.new([], headers: ["A", "B"])
+ table << ["a", "b"]
+ table.first << {"C" => "c"}
+ assert_equal(["A", "B", "C"], table.headers)
+ end
+
def test_index
##################
### Mixed Mode ###
diff --git a/test/csv/ts_all.rb b/test/csv/ts_all.rb
deleted file mode 100644
index f632c81..0000000
--- a/test/csv/ts_all.rb
+++ /dev/null
@@ -1,19 +0,0 @@
-#!/usr/bin/env ruby -w
-# encoding: UTF-8
-# frozen_string_literal: false
-
-# ts_all.rb
-#
-# Created by James Edward Gray II on 2005-10-31.
-
-require "test/unit"
-
-require "test_csv_parsing"
-require "test_features"
-require "test_interface"
-require "test_csv_writing"
-require "test_data_converters"
-require "test_row"
-require "test_table"
-require "test_headers"
-require "test_encodings"
diff --git a/test/csv/write/test_general.rb b/test/csv/write/test_general.rb
new file mode 100644
index 0000000..c879f54
--- /dev/null
+++ b/test/csv/write/test_general.rb
@@ -0,0 +1,228 @@
+# -*- coding: utf-8 -*-
+# frozen_string_literal: false
+
+require_relative "../helper"
+
+module TestCSVWriteGeneral
+ def test_tab
+ assert_equal("\t#{$INPUT_RECORD_SEPARATOR}",
+ generate_line(["\t"]))
+ end
+
+ def test_quote_character
+ assert_equal(%Q[foo,"""",baz#{$INPUT_RECORD_SEPARATOR}],
+ generate_line(["foo", %Q["], "baz"]))
+ end
+
+ def test_quote_character_double
+ assert_equal(%Q[foo,"""""",baz#{$INPUT_RECORD_SEPARATOR}],
+ generate_line(["foo", %Q[""], "baz"]))
+ end
+
+ def test_quote
+ assert_equal(%Q[foo,"""bar""",baz#{$INPUT_RECORD_SEPARATOR}],
+ generate_line(["foo", %Q["bar"], "baz"]))
+ end
+
+ def test_quote_lf
+ assert_equal(%Q["""\n","""\n"#{$INPUT_RECORD_SEPARATOR}],
+ generate_line([%Q["\n], %Q["\n]]))
+ end
+
+ def test_quote_cr
+ assert_equal(%Q["""\r","""\r"#{$INPUT_RECORD_SEPARATOR}],
+ generate_line([%Q["\r], %Q["\r]]))
+ end
+
+ def test_quote_last
+ assert_equal(%Q[foo,"bar"""#{$INPUT_RECORD_SEPARATOR}],
+ generate_line(["foo", %Q[bar"]]))
+ end
+
+ def test_quote_lf_last
+ assert_equal(%Q[foo,"\nbar"""#{$INPUT_RECORD_SEPARATOR}],
+ generate_line(["foo", %Q[\nbar"]]))
+ end
+
+ def test_quote_lf_value_lf
+ assert_equal(%Q[foo,"""\nbar\n"""#{$INPUT_RECORD_SEPARATOR}],
+ generate_line(["foo", %Q["\nbar\n"]]))
+ end
+
+ def test_quote_lf_value_lf_nil
+ assert_equal(%Q[foo,"""\nbar\n""",#{$INPUT_RECORD_SEPARATOR}],
+ generate_line(["foo", %Q["\nbar\n"], nil]))
+ end
+
+ def test_cr
+ assert_equal(%Q[foo,"\r",baz#{$INPUT_RECORD_SEPARATOR}],
+ generate_line(["foo", "\r", "baz"]))
+ end
+
+ def test_lf
+ assert_equal(%Q[foo,"\n",baz#{$INPUT_RECORD_SEPARATOR}],
+ generate_line(["foo", "\n", "baz"]))
+ end
+
+ def test_cr_lf
+ assert_equal(%Q[foo,"\r\n",baz#{$INPUT_RECORD_SEPARATOR}],
+ generate_line(["foo", "\r\n", "baz"]))
+ end
+
+ def test_cr_dot_lf
+ assert_equal(%Q[foo,"\r.\n",baz#{$INPUT_RECORD_SEPARATOR}],
+ generate_line(["foo", "\r.\n", "baz"]))
+ end
+
+ def test_cr_lf_cr
+ assert_equal(%Q[foo,"\r\n\r",baz#{$INPUT_RECORD_SEPARATOR}],
+ generate_line(["foo", "\r\n\r", "baz"]))
+ end
+
+ def test_cr_lf_lf
+ assert_equal(%Q[foo,"\r\n\n",baz#{$INPUT_RECORD_SEPARATOR}],
+ generate_line(["foo", "\r\n\n", "baz"]))
+ end
+
+ def test_cr_lf_comma
+ assert_equal(%Q["\r\n,"#{$INPUT_RECORD_SEPARATOR}],
+ generate_line(["\r\n,"]))
+ end
+
+ def test_cr_lf_comma_nil
+ assert_equal(%Q["\r\n,",#{$INPUT_RECORD_SEPARATOR}],
+ generate_line(["\r\n,", nil]))
+ end
+
+ def test_comma
+ assert_equal(%Q[","#{$INPUT_RECORD_SEPARATOR}],
+ generate_line([","]))
+ end
+
+ def test_comma_double
+ assert_equal(%Q[",",","#{$INPUT_RECORD_SEPARATOR}],
+ generate_line([",", ","]))
+ end
+
+ def test_comma_and_value
+ assert_equal(%Q[foo,"foo,bar",baz#{$INPUT_RECORD_SEPARATOR}],
+ generate_line(["foo", "foo,bar", "baz"]))
+ end
+
+ def test_one_element
+ assert_equal(%Q[foo#{$INPUT_RECORD_SEPARATOR}],
+ generate_line(["foo"]))
+ end
+
+ def test_nil_values_only
+ assert_equal(%Q[,,#{$INPUT_RECORD_SEPARATOR}],
+ generate_line([nil, nil, nil]))
+ end
+
+ def test_nil_double_only
+ assert_equal(%Q[,#{$INPUT_RECORD_SEPARATOR}],
+ generate_line([nil, nil]))
+ end
+
+ def test_nil_values
+ assert_equal(%Q[foo,,,#{$INPUT_RECORD_SEPARATOR}],
+ generate_line(["foo", nil, nil, nil]))
+ end
+
+ def test_nil_value_first
+ assert_equal(%Q[,foo,baz#{$INPUT_RECORD_SEPARATOR}],
+ generate_line([nil, "foo", "baz"]))
+ end
+
+ def test_nil_value_middle
+ assert_equal(%Q[foo,,baz#{$INPUT_RECORD_SEPARATOR}],
+ generate_line(["foo", nil, "baz"]))
+ end
+
+ def test_nil_value_last
+ assert_equal(%Q[foo,baz,#{$INPUT_RECORD_SEPARATOR}],
+ generate_line(["foo", "baz", nil]))
+ end
+
+ def test_nil_empty
+ assert_equal(%Q[,""#{$INPUT_RECORD_SEPARATOR}],
+ generate_line([nil, ""]))
+ end
+
+ def test_nil_cr
+ assert_equal(%Q[,"\r"#{$INPUT_RECORD_SEPARATOR}],
+ generate_line([nil, "\r"]))
+ end
+
+ def test_values
+ assert_equal(%Q[foo,bar#{$INPUT_RECORD_SEPARATOR}],
+ generate_line(["foo", "bar"]))
+ end
+
+ def test_semi_colon
+ assert_equal(%Q[;#{$INPUT_RECORD_SEPARATOR}],
+ generate_line([";"]))
+ end
+
+ def test_semi_colon_values
+ assert_equal(%Q[;,;#{$INPUT_RECORD_SEPARATOR}],
+ generate_line([";", ";"]))
+ end
+
+ def test_tab_values
+ assert_equal(%Q[\t,\t#{$INPUT_RECORD_SEPARATOR}],
+ generate_line(["\t", "\t"]))
+ end
+
+ def test_col_sep
+ assert_equal(%Q[a;b;;c#{$INPUT_RECORD_SEPARATOR}],
+ generate_line(["a", "b", nil, "c"],
+ col_sep: ";"))
+ assert_equal(%Q[a\tb\t\tc#{$INPUT_RECORD_SEPARATOR}],
+ generate_line(["a", "b", nil, "c"],
+ col_sep: "\t"))
+ end
+
+ def test_row_sep
+ assert_equal(%Q[a,b,,c\r\n],
+ generate_line(["a", "b", nil, "c"],
+ row_sep: "\r\n"))
+ end
+
+ def test_force_quotes
+ assert_equal(%Q["1","b","","already ""quoted"""#{$INPUT_RECORD_SEPARATOR}],
+ generate_line([1, "b", nil, %Q{already "quoted"}],
+ force_quotes: true))
+ end
+
+ def test_encoding_utf8
+ assert_equal(%Q[あ,い,う#{$INPUT_RECORD_SEPARATOR}],
+ generate_line(["あ" , "い", "う"]))
+ end
+
+ def test_encoding_euc_jp
+ row = ["あ", "い", "う"].collect {|field| field.encode("EUC-JP")}
+ assert_equal(%Q[あ,い,う#{$INPUT_RECORD_SEPARATOR}].encode("EUC-JP"),
+ generate_line(row))
+ end
+end
+
+class TestCSVWriteGeneralGenerateLine < Test::Unit::TestCase
+ include TestCSVWriteGeneral
+ extend DifferentOFS
+
+ def generate_line(row, **kwargs)
+ CSV.generate_line(row, **kwargs)
+ end
+end
+
+class TestCSVWriteGeneralGenerate < Test::Unit::TestCase
+ include TestCSVWriteGeneral
+ extend DifferentOFS
+
+ def generate_line(row, **kwargs)
+ CSV.generate(**kwargs) do |csv|
+ csv << row
+ end
+ end
+end
diff --git a/test/csv/write/test_quote_empty.rb b/test/csv/write/test_quote_empty.rb
new file mode 100644
index 0000000..70f73da
--- /dev/null
+++ b/test/csv/write/test_quote_empty.rb
@@ -0,0 +1,70 @@
+# -*- coding: utf-8 -*-
+# frozen_string_literal: false
+
+require_relative "../helper"
+
+module TestCSVWriteQuoteEmpty
+ def test_quote_empty_default
+ assert_equal(%Q["""",""#{$INPUT_RECORD_SEPARATOR}],
+ generate_line([%Q["], ""]))
+ end
+
+ def test_quote_empty_false
+ assert_equal(%Q["""",#{$INPUT_RECORD_SEPARATOR}],
+ generate_line([%Q["], ""],
+ quote_empty: false))
+ end
+
+ def test_empty_default
+ assert_equal(%Q[foo,"",baz#{$INPUT_RECORD_SEPARATOR}],
+ generate_line(["foo", "", "baz"]))
+ end
+
+ def test_empty_false
+ assert_equal(%Q[foo,,baz#{$INPUT_RECORD_SEPARATOR}],
+ generate_line(["foo", "", "baz"],
+ quote_empty: false))
+ end
+
+ def test_empty_only_default
+ assert_equal(%Q[""#{$INPUT_RECORD_SEPARATOR}],
+ generate_line([""]))
+ end
+
+ def test_empty_only_false
+ assert_equal(%Q[#{$INPUT_RECORD_SEPARATOR}],
+ generate_line([""],
+ quote_empty: false))
+ end
+
+ def test_empty_double_default
+ assert_equal(%Q["",""#{$INPUT_RECORD_SEPARATOR}],
+ generate_line(["", ""]))
+ end
+
+ def test_empty_double_false
+ assert_equal(%Q[,#{$INPUT_RECORD_SEPARATOR}],
+ generate_line(["", ""],
+ quote_empty: false))
+ end
+end
+
+class TestCSVWriteQuoteEmptyGenerateLine < Test::Unit::TestCase
+ include TestCSVWriteQuoteEmpty
+ extend DifferentOFS
+
+ def generate_line(row, **kwargs)
+ CSV.generate_line(row, **kwargs)
+ end
+end
+
+class TestCSVWriteQuoteEmptyGenerate < Test::Unit::TestCase
+ include TestCSVWriteQuoteEmpty
+ extend DifferentOFS
+
+ def generate_line(row, **kwargs)
+ CSV.generate(**kwargs) do |csv|
+ csv << row
+ end
+ end
+end