Skip to content

Commit

Permalink
Correct typespec for decode
Browse files Browse the repository at this point in the history
  • Loading branch information
beatrichartz committed Nov 26, 2023
1 parent 29bc8b7 commit 85baadb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/csv.ex
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,10 @@ defmodule CSV do
| {:validate_row_length, boolean()}
| {:escape_character, char()}
| {:escape_max_lines, integer()}
| ({:redact_errors, boolean()}
| {:unredact_exceptions, boolean()})

@spec decode(Enumerable.t(), [decode_options() | {:redact_errors, boolean()}]) :: Enumerable.t()
@spec decode(Enumerable.t(), [decode_options()]) :: Enumerable.t()
def decode(stream, options \\ []) do
stream |> Decoder.decode(options) |> inline_errors!(options)
end
Expand Down Expand Up @@ -288,8 +290,7 @@ defmodule CSV do
"""

@spec decode!(Enumerable.t(), [decode_options() | {:unredact_exceptions, boolean()}]) ::
Enumerable.t()
@spec decode!(Enumerable.t(), [decode_options()]) :: Enumerable.t()
def decode!(stream, options \\ []) do
stream |> Decoder.decode(options) |> raise_errors!(options)
end
Expand Down
28 changes: 28 additions & 0 deletions test/dialyzer/decode_typespectest.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
defmodule DecodeTypespectest do
@moduledoc "Test decoding typespecs"

def test_decode_no_options do
["dummy,input"] |> CSV.decode() |> Enum.to_list()
end

def test_decode_option_escape_character do
["dummy,input"] |> CSV.decode(escape_character: ?.) |> Enum.to_list()
end
Expand Down Expand Up @@ -32,4 +36,28 @@ defmodule DecodeTypespectest do
def test_decode_option_headers_atom_list do
["dummy,input"] |> CSV.decode(headers: [:a, :b]) |> Enum.to_list()
end

def test_decode_combined do
["dummy,input"] |> CSV.decode(headers: [:a, :b], separator: ?,, validate_row_length: true) |> Enum.to_list()
end

def test_decode_option_combine_with_redact_errors do
["dummy,input"] |> CSV.decode(headers: [:a, :b], redact_errors: false) |> Enum.to_list()
end

def test_decode_strict_no_options do
["dummy,input"] |> CSV.decode!() |> Enum.to_list()
end

def test_decode_strict_headers do
["dummy,input"] |> CSV.decode!(headers: [:a, :b]) |> Enum.to_list()
end

def test_decode_strict_combined do
["dummy,input"] |> CSV.decode!(headers: [:a, :b], separator: ?,, validate_row_length: true) |> Enum.to_list()
end

def test_decode_strict_option_combine_with_unredact_exceptions do
["dummy,input"] |> CSV.decode!(headers: [:a, :b], unredact_exceptions: true) |> Enum.to_list()
end
end

0 comments on commit 85baadb

Please sign in to comment.