summaryrefslogtreecommitdiffstats
path: root/test/cepis/csv_test.exs
blob: 719de6632e117c21b72d12fc769d0cce851a3e9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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