summaryrefslogtreecommitdiff
path: root/test/csv/test_csv_parsing.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/csv/test_csv_parsing.rb')
-rwxr-xr-xtest/csv/test_csv_parsing.rb75
1 files changed, 45 insertions, 30 deletions
diff --git a/test/csv/test_csv_parsing.rb b/test/csv/test_csv_parsing.rb
index e65bbad..3fe1bd7 100755
--- a/test/csv/test_csv_parsing.rb
+++ b/test/csv/test_csv_parsing.rb
@@ -143,55 +143,52 @@ class TestCSV::Parsing < TestCSV
end
end
- def test_malformed_csv
- assert_raise(CSV::MalformedCSVError) do
+ def test_malformed_csv_cr_first_line
+ error = assert_raise(CSV::MalformedCSVError) do
CSV.parse_line("1,2\r,3", row_sep: "\n")
end
+ assert_equal("Unquoted fields do not allow \\r or \\n in line 1.",
+ error.message)
+ end
- bad_data = <<-CSV
+ def test_malformed_csv_cr_middle_line
+ csv = <<-CSV
line,1,abc
line,2,"def\nghi"
line,4,some\rjunk
line,5,jkl
CSV
- lines = bad_data.lines.to_a
- assert_equal(6, lines.size)
- assert_match(/\Aline,4/, lines.find { |l| l =~ /some\rjunk/ })
-
- csv = CSV.new(bad_data)
- begin
- loop do
- assert_not_nil(csv.shift)
- assert_send([csv.lineno, :<, 4])
- end
- rescue CSV::MalformedCSVError
- assert_equal( "Unquoted fields do not allow \\r or \\n in line 4.",
- $!.message )
+
+ error = assert_raise(CSV::MalformedCSVError) do
+ CSV.parse(csv)
end
+ assert_equal("Unquoted fields do not allow \\r or \\n in line 4.",
+ error.message)
+ end
- assert_raise(CSV::MalformedCSVError) { CSV.parse_line('1,2,"3...') }
+ 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
- bad_data = <<-CSV
+ def test_malformed_csv_illegal_quote_middle_line
+ csv = <<-CSV
line,1,abc
line,2,"def\nghi"
line,4,8'10"
line,5,jkl
CSV
- lines = bad_data.lines.to_a
- assert_equal(6, lines.size)
- assert_match(/\Aline,4/, lines.find { |l| l =~ /8'10"/ })
-
- csv = CSV.new(bad_data)
- begin
- loop do
- assert_not_nil(csv.shift)
- assert_send([csv.lineno, :<, 4])
- end
- rescue CSV::MalformedCSVError
- assert_equal("Illegal quoting in line 4.", $!.message)
+
+ error = assert_raise(CSV::MalformedCSVError) do
+ CSV.parse(csv)
end
+ assert_equal("Illegal quoting in line 4.",
+ error.message)
end
def test_the_parse_fails_fast_when_it_can_for_unquoted_fields
@@ -239,6 +236,24 @@ line,5,jkl
CSV.parse("a b d", col_sep: " "))
end
+ 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
+
+ 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)