Skip to content
This repository has been archived by the owner on Nov 19, 2023. It is now read-only.

Commit

Permalink
Fix test fails when 2+maps (#23)
Browse files Browse the repository at this point in the history
* fix map error against 2+ maps

* apply format

* update travis
  • Loading branch information
KazuCocoa authored Jul 4, 2018
1 parent aa6b5e5 commit 6c20e24
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ language: elixir
opt_release:
- 19.1
- 20.0
- 21.0

elixir:
- 1.4.5
- 1.5.0
- 1.6.0

sudo: false
27 changes: 23 additions & 4 deletions lib/ex_parameterized/params.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ defmodule ExUnit.Parameterized.Params do
defmacro test_with_params(desc, fun, params_ast) do
ast = Keyword.get(params_ast, :do, nil)

case ast do
# for Map
[{:{}, _, [{:%{}, _, _}]}] ->
case validate_map?(ast) do
true ->
ast |> do_test_with(desc, fun)

_ ->
false ->
try do
{params, _} = Code.eval_quoted(params_ast)
Keyword.get(params, :do, nil) |> do_test_with(desc, fun)
Expand All @@ -21,6 +20,25 @@ defmodule ExUnit.Parameterized.Params do
end
end

defp validate_map?(asts, result \\ [])
defp validate_map?([], result) when is_list(result), do: true
defp validate_map?([], _), do: false
defp validate_map?([{:%{}, _, _}], _), do: true

defp validate_map?(asts, result) when is_list(asts) do
[head | tail] = asts

case head do
{_, _, [{:%{}, _, _}]} ->
tail |> validate_map?([head | result])

_ ->
false
end
end

defp validate_map?(_asts, _result), do: false

defp do_test_with(ast, desc, fun) do
ast
|> param_with_index()
Expand Down Expand Up @@ -54,6 +72,7 @@ defmodule ExUnit.Parameterized.Params do
defp param_with_index(list) when is_list(list) do
Enum.zip(list, 0..Enum.count(list))
end

defp param_with_index(_) do
raise(ArgumentError, message: "Unsupported format")
end
Expand Down
27 changes: 23 additions & 4 deletions lib/ex_parameterized/params_callback.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ defmodule ExUnit.Parameterized.ParamsCallback do
defmacro test_with_params(desc, context, fun, params_ast) do
ast = Keyword.get(params_ast, :do, nil)

case ast do
# for Map
[{:{}, _, [{:%{}, _, _}]}] ->
case validate_map?(ast) do
true ->
ast |> do_test_with(desc, context, fun)

_ ->
false ->
try do
{params, _} = params_ast |> Code.eval_quoted()

Expand All @@ -24,6 +23,25 @@ defmodule ExUnit.Parameterized.ParamsCallback do
end
end

defp validate_map?(asts, result \\ [])
defp validate_map?([], result) when is_list(result), do: true
defp validate_map?([], _), do: false
defp validate_map?([{:%{}, _, _}], _), do: true

defp validate_map?(asts, result) when is_list(asts) do
[head | tail] = asts

case head do
{_, _, [{:%{}, _, _}]} ->
tail |> validate_map?([head | result])

_ ->
false
end
end

defp validate_map?(_asts, _result), do: false

defp do_test_with(ast, desc, context, fun) do
ast
|> param_with_index()
Expand Down Expand Up @@ -57,6 +75,7 @@ defmodule ExUnit.Parameterized.ParamsCallback do
defp param_with_index(list) when is_list(list) do
Enum.zip(list, 0..Enum.count(list))
end

defp param_with_index(_) do
raise(ArgumentError, message: "Unsupported format")
end
Expand Down
20 changes: 20 additions & 0 deletions test/ex_parameterized_callback_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,25 @@ defmodule ExParameterizedParamsCallbackTest do
]
end

test_with_params "with map value", context, fn a ->
assert a.b == 1
assert a.c == 2
end do
[
{%{b: 1, c: 2}}
]
end

test_with_params "with two map values", context, fn a ->
assert a.b == 1
assert a.c == 2
end do
[
{%{b: 1, c: 2}},
{%{b: 1, c: 2}}
]
end

test_with_params "add description for each params with context", context, fn a, b, expected ->
str = a <> " and " <> b
assert str == expected
Expand Down Expand Up @@ -124,6 +143,7 @@ defmodule ExParameterizedParamsCallbackTest do
p
end do
make = fn a -> a + 1 end

[
{make.(1)}
]
Expand Down
13 changes: 12 additions & 1 deletion test/ex_parameterized_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ defmodule ExParameterizedTest do
]
end

test_with_params "with two map values", fn a ->
assert a.b == 1
assert a.c == 2
end do
[
{%{b: 1, c: 2}},
{%{b: 1, c: 2}}
]
end

describe "example with shouldi" do
test_with_params "provide one param", fn a ->
assert a == 1
Expand Down Expand Up @@ -155,9 +165,10 @@ defmodule ExParameterizedTest do
end

test_with_params "with function", fn p ->
p
p
end do
make = fn a -> a + 1 end

[
{make.(1)}
]
Expand Down

0 comments on commit 6c20e24

Please sign in to comment.