diff options
Diffstat (limited to 'test/cepis/csv_test.exs')
| -rw-r--r-- | test/cepis/csv_test.exs | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/test/cepis/csv_test.exs b/test/cepis/csv_test.exs new file mode 100644 index 0000000..719de66 --- /dev/null +++ b/test/cepis/csv_test.exs @@ -0,0 +1,49 @@ +defmodule Cepis.CSVTest do + use ExUnit.Case, async: true + + alias Cepis.CSV + + test "parses plain rows" do + assert CSV.parse("a,b\n1,2\n") == [["a", "b"], ["1", "2"]] + end + + test "tolerates a missing final newline" do + assert CSV.parse("a,b\n1,2") == [["a", "b"], ["1", "2"]] + end + + test "tolerates CRLF" do + assert CSV.parse("a,b\r\n1,2\r\n") == [["a", "b"], ["1", "2"]] + end + + test "keeps empty fields" do + assert CSV.parse("a,b,c\n1,,3\n") == [["a", "b", "c"], ["1", "", "3"]] + end + + test "unquotes fields containing commas, quotes and newlines" do + text = ~s(a,b\n"x, y","he said ""hi"""\n) + assert CSV.parse(text) == [["a", "b"], ["x, y", ~s(he said "hi")]] + + assert CSV.parse(~s(a\n"one\ntwo"\n)) == [["a"], ["one\ntwo"]] + end + + test "strips a byte order mark" do + assert CSV.parse("a,b\n1,2\n") == [["a", "b"], ["1", "2"]] + end + + test "keeps multi-byte characters intact" do + assert CSV.parse("a\nSão Paulo\n") == [["a"], ["São Paulo"]] + end + + test "an empty document has no rows" do + assert CSV.parse("") == [] + assert CSV.parse("\n") == [] + end + + test "pairs values with the header, padding short rows" do + assert CSV.parse_with_header("a,b,c\n1,2\n") == [%{"a" => "1", "b" => "2", "c" => ""}] + end + + test "a header alone yields no rows" do + assert CSV.parse_with_header("a,b,c\n") == [] + end +end |