Skip to content

Commit

Permalink
Add testing scenarios for quotes in unescaped fields
Browse files Browse the repository at this point in the history
  • Loading branch information
beatrichartz committed Mar 17, 2019
1 parent 6181134 commit a4b8395
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/csv/decoding/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ defmodule CSV.Decoding.Parser do
{:delimiter, _} ->
parse(row, field, tokens, :unescaped, options)

{:double_quote, content} ->
parse(row, field <> content, tokens, :inline_quote, options)
{:double_quote, _} ->
parse(row, field, tokens, :inline_quote, options)
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/csv_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ defmodule CSVTest do
).message,
error:
CSV.StrayQuoteError.exception(
field: "j\"",
field: "j",
line: 2
).message,
ok: ["k", "l"]
Expand Down
7 changes: 4 additions & 3 deletions test/decoding/baseline_exceptions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@ defmodule DecodingTests.BaselineExceptionsTest do
end

test "includes an error for rows with unescaped quotes" do
stream = ["a\",\"be", "\"c,d"] |> to_stream
stream = ["a\",\"be", "\"c,d", "\"e,f\"g\",h"] |> to_stream
errors = stream |> Decoder.decode() |> Enum.to_list()

assert errors == [
{:error, StrayQuoteError, "a\"", 0},
{:error, EscapeSequenceError, "c,d", 1}
{:error, StrayQuoteError, "a", 0},
{:error, EscapeSequenceError, "c,d", 1},
{:error, StrayQuoteError, "e,f", 2}
]
end

Expand Down
7 changes: 7 additions & 0 deletions test/decoding/baseline_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ defmodule DecodingTests.BaselineTest do
assert result == [ok: ["a", "be"], ok: ["c\"", "d"]]
end

test "parses strings that contain escaped double quotes" do
stream = ["a,be", "c\"\",d"] |> to_stream
result = Decoder.decode(stream) |> Enum.to_list()

assert result == [ok: ["a", "be"], ok: ["c\"", "d"]]
end

test "parses strings that contain multi-byte unicode characters" do
stream = ["a,b", "c,ಠ_ಠ"] |> to_stream
result = Decoder.decode(stream) |> Enum.to_list()
Expand Down
6 changes: 3 additions & 3 deletions test/decoding/parsing/exceptions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ defmodule DecodingTests.ParsingTests.ExceptionsTest do
{:double_quote, "\""}
], 1},
{[
{:content, "c"},
{:double_quote, "\""},
{:delimiter, "\r\n"},
{:content, "c"},
{:separator, ","},
{:content, "d"}
Expand All @@ -96,8 +96,8 @@ defmodule DecodingTests.ParsingTests.ExceptionsTest do
)

assert parsed == [
{:error, StrayQuoteError, "a\"", 1},
{:error, EscapeSequenceError, "\r\nc,d", 2}
{:error, StrayQuoteError, "a", 1},
{:error, StrayQuoteError, "c", 2}
]
end

Expand Down

0 comments on commit a4b8395

Please sign in to comment.