diff options
author | Hiroshi SHIBATA <hsbt@ruby-lang.org> | 2022-08-26 14:54:17 +0900 |
---|---|---|
committer | nagachika <nagachika@ruby-lang.org> | 2022-09-03 15:54:07 +0900 |
commit | eed5970b6477910f11630458bb4649c0b94cd4fb (patch) | |
tree | 7534a3b1fe944287deabeb799a0f4b88df3dc745 /test | |
parent | cd0c2a67c482c441ac7f0a07c0f81573d6b6072f (diff) |
Merge csv-3.2.5
Diffstat (limited to 'test')
-rw-r--r-- | test/csv/interface/test_write.rb | 9 | ||||
-rw-r--r-- | test/csv/parse/test_convert.rb | 102 |
2 files changed, 58 insertions, 53 deletions
diff --git a/test/csv/interface/test_write.rb b/test/csv/interface/test_write.rb index 02c2c5c5ce..0cd39a7663 100644 --- a/test/csv/interface/test_write.rb +++ b/test/csv/interface/test_write.rb @@ -85,6 +85,15 @@ testrow LINE end + def test_generate_lines + lines = CSV.generate_lines([["foo", "bar"], [1, 2], [3, 4]]) + assert_equal(<<-LINES, lines) +foo,bar +1,2 +3,4 + LINES + end + def test_headers_detection headers = ["a", "b", "c"] CSV.open(@output.path, "w", headers: true) do |csv| diff --git a/test/csv/parse/test_convert.rb b/test/csv/parse/test_convert.rb index 2ac255695e..c9195c71d9 100644 --- a/test/csv/parse/test_convert.rb +++ b/test/csv/parse/test_convert.rb @@ -15,6 +15,22 @@ class TestCSVParseConvert < Test::Unit::TestCase @time = Time.utc(2018, 12, 30, 6, 41, 29) @windows_safe_time_data = @time.strftime("%a %b %d %H:%M:%S %Y") + + @preserving_converter = lambda do |field, info| + f = field.encode(CSV::ConverterEncoding) + return f if info.quoted? + begin + Integer(f, 10) + rescue + f + end + end + + @quoted_header_converter = lambda do |field, info| + f = field.encode(CSV::ConverterEncoding) + return f if info.quoted? + f.to_sym + end end def test_integer @@ -108,62 +124,42 @@ class TestCSVParseConvert < Test::Unit::TestCase CSV.parse_line(',"",a', empty_value: "empty")) end - sub_test_case("#quoted?") do - def setup - @preserving_converter = lambda do |field, info| - f = field.encode(CSV::ConverterEncoding) - return f if info.quoted? - begin - Integer(f, 10) - rescue - f - end - end - - @quoted_header_converter = lambda do |field, info| - f = field.encode(CSV::ConverterEncoding) - return f if info.quoted? - f.to_sym - end - end - - def test_parse_line - row = CSV.parse_line('1,"2",3', converters: @preserving_converter) - assert_equal([1, "2", 3], row) - end + def test_quoted_parse_line + row = CSV.parse_line('1,"2",3', converters: @preserving_converter) + assert_equal([1, "2", 3], row) + end - def test_parse - expected = [["quoted", "unquoted"], ["109", 1], ["10A", 2]] - rows = CSV.parse(<<~CSV, converters: @preserving_converter) - "quoted",unquoted - "109",1 - "10A",2 - CSV - assert_equal(expected, rows) - end + def test_quoted_parse + expected = [["quoted", "unquoted"], ["109", 1], ["10A", 2]] + rows = CSV.parse(<<~CSV, converters: @preserving_converter) + "quoted",unquoted + "109",1 + "10A",2 + CSV + assert_equal(expected, rows) + end - def test_alternating_quote - row = CSV.parse_line('"1",2,"3"', converters: @preserving_converter) - assert_equal(['1', 2, '3'], row) - end + def test_quoted_alternating_quote + row = CSV.parse_line('"1",2,"3"', converters: @preserving_converter) + assert_equal(['1', 2, '3'], row) + end - def test_parse_headers - expected = [["quoted", :unquoted], ["109", "1"], ["10A", "2"]] - table = CSV.parse(<<~CSV, headers: true, header_converters: @quoted_header_converter) - "quoted",unquoted - "109",1 - "10A",2 - CSV - assert_equal(expected, table.to_a) - end + def test_quoted_parse_headers + expected = [["quoted", :unquoted], ["109", "1"], ["10A", "2"]] + table = CSV.parse(<<~CSV, headers: true, header_converters: @quoted_header_converter) + "quoted",unquoted + "109",1 + "10A",2 + CSV + assert_equal(expected, table.to_a) + end - def test_parse_with_string_headers - expected = [["quoted", :unquoted], %w[109 1], %w[10A 2]] - table = CSV.parse(<<~CSV, headers: '"quoted",unquoted', header_converters: @quoted_header_converter) - "109",1 - "10A",2 - CSV - assert_equal(expected, table.to_a) - end + def test_quoted_parse_with_string_headers + expected = [["quoted", :unquoted], %w[109 1], %w[10A 2]] + table = CSV.parse(<<~CSV, headers: '"quoted",unquoted', header_converters: @quoted_header_converter) + "109",1 + "10A",2 + CSV + assert_equal(expected, table.to_a) end end |