|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# frozen_string_literal: false |
| 3 | + |
| 4 | +require_relative "../helper" |
| 5 | + |
| 6 | +class TestCSVParseUnquotedCR < Test::Unit::TestCase |
| 7 | + extend DifferentOFS |
| 8 | + |
| 9 | + def test_accept_cr_in_unquoted_field_when_row_separator_is_lf_only |
| 10 | + # When row separator is just \n, \r should be allowed in unquoted fields |
| 11 | + data = "field1,field\rwith\rcr,field3\nrow2,data,here\n" |
| 12 | + expected = [ |
| 13 | + ["field1", "field\rwith\rcr", "field3"], |
| 14 | + ["row2", "data", "here"] |
| 15 | + ] |
| 16 | + assert_equal(expected, CSV.parse(data, row_sep: "\n")) |
| 17 | + end |
| 18 | + |
| 19 | + def test_accept_cr_in_unquoted_field_when_row_separator_is_custom |
| 20 | + # When row separator is custom (like "|"), \r should be allowed in unquoted fields |
| 21 | + data = "field1,field\rwith\rcr,field3|row2,data,here|" |
| 22 | + expected = [ |
| 23 | + ["field1", "field\rwith\rcr", "field3"], |
| 24 | + ["row2", "data", "here"] |
| 25 | + ] |
| 26 | + assert_equal(expected, CSV.parse(data, row_sep: "|")) |
| 27 | + end |
| 28 | + |
| 29 | + def test_reject_cr_when_row_separator_includes_cr |
| 30 | + # When row separator includes \r (like \r\n), \r should still be rejected in unquoted fields |
| 31 | + data = "field1,field2,field3\r\nrow2,data,here\r\n" |
| 32 | + expected = [ |
| 33 | + ["field1", "field2", "field3"], |
| 34 | + ["row2", "data", "here"] |
| 35 | + ] |
| 36 | + assert_equal(expected, CSV.parse(data, row_sep: "\r\n")) |
| 37 | + end |
| 38 | + |
| 39 | + def test_reject_cr_when_row_separator_is_cr_only |
| 40 | + # When row separator is just \r, \r should be rejected in unquoted fields |
| 41 | + data = "field1,field2,field3\rrow2,data,here\r" |
| 42 | + expected = [ |
| 43 | + ["field1", "field2", "field3"], |
| 44 | + ["row2", "data", "here"] |
| 45 | + ] |
| 46 | + assert_equal(expected, CSV.parse(data, row_sep: "\r")) |
| 47 | + end |
| 48 | + |
| 49 | + def test_liberal_parsing_with_custom_row_separator |
| 50 | + # Test liberal parsing mode with custom row separator |
| 51 | + data = "field1,field\rwith\rcr,field3|row2,data,here|" |
| 52 | + expected = [ |
| 53 | + ["field1", "field\rwith\rcr", "field3"], |
| 54 | + ["row2", "data", "here"] |
| 55 | + ] |
| 56 | + assert_equal(expected, CSV.parse(data, row_sep: "|", liberal_parsing: true)) |
| 57 | + end |
| 58 | + |
| 59 | + def test_quoted_fields_with_cr_and_custom_row_separator |
| 60 | + # Quoted fields should always allow \r regardless of row separator |
| 61 | + data = "field1,\"field\rwith\rcr\",field3|row2,data,here|" |
| 62 | + expected = [ |
| 63 | + ["field1", "field\rwith\rcr", "field3"], |
| 64 | + ["row2", "data", "here"] |
| 65 | + ] |
| 66 | + assert_equal(expected, CSV.parse(data, row_sep: "|")) |
| 67 | + end |
| 68 | +end |
0 commit comments