diff --git a/lib/ex_parameterized/params.ex b/lib/ex_parameterized/params.ex index d78ce2b..d8786b2 100644 --- a/lib/ex_parameterized/params.ex +++ b/lib/ex_parameterized/params.ex @@ -9,7 +9,19 @@ defmodule ExUnit.Parameterized.Params do # for Map [{:{}, _, [{:%{}, _, _}]}] -> ast |> do_test_with(desc, fun) + {:@, _, [{atom, _, _}]} -> # for @param + quote do + attr = Module.get_attribute(unquote(__CALLER__.module), unquote(atom)) + |> Macro.escape + # [{:test, [], + # ["'bad': number of 0", + # [do: {{:., [], + # [#Function<1.14669326 in file:test/ex_parameterized_test.exs>]}, [], + # [1]}]]}] + do_test_with(attr, unquote(desc), unquote(fun)) + # If we can run the above AST, test will run. + end _ -> try do {params, _} = Code.eval_quoted(params_ast) @@ -21,7 +33,7 @@ defmodule ExUnit.Parameterized.Params do end end - defp do_test_with(ast, desc, fun) do + def do_test_with(ast, desc, fun) do ast |> param_with_index() |> Enum.map(fn param -> diff --git a/lib/ex_parameterized/params_callback.ex b/lib/ex_parameterized/params_callback.ex index 7159e9a..2c0f7e2 100644 --- a/lib/ex_parameterized/params_callback.ex +++ b/lib/ex_parameterized/params_callback.ex @@ -3,25 +3,47 @@ defmodule ExUnit.Parameterized.ParamsCallback do @spec test_with_params(bitstring, any, fun, [tuple]) :: any defmacro test_with_params(desc, context, fun, params_ast) do - try do - {params, _} = params_ast |> Code.eval_quoted() - - Keyword.get(params, :do, nil) - |> param_with_index() - |> Enum.map(fn test_param -> - test_with(desc, context, fun, test_param) - end) - rescue + ast = Keyword.get(params_ast, :do, nil) + + case ast do + # for Map + [{:{}, _, [{:%{}, _, _}]}] -> + ast |> do_test_with(desc, context, fun) + {:@, _, [{atom, _, _}]} -> # for @param + quote do + attr = Module.get_attribute(unquote(__CALLER__.module), unquote(atom)) + |> Macro.escape + + # [{:test, [], + # ["'bad': number of 0", + # [do: {{:., [], + # [#Function<1.14669326 in file:test/ex_parameterized_test.exs>]}, [], + # [1]}]]}] + do_test_with(attr, unquote(desc), unquote(fun)) + # If we can run the above AST, test will run. + end _ -> - params_ast - |> Keyword.get(:do, nil) - |> param_with_index() - |> Enum.map(fn test_param -> - test_with(desc, context, fun, test_param) - end) + try do + {params, _} = params_ast |> Code.eval_quoted() + + params + |> Keyword.get(:do, nil) + |> do_test_with(desc, context, fun) + rescue + _ -> + ast |> do_test_with(desc, context, fun) + end end end + def do_test_with(ast, desc, context, fun) do + ast + |> param_with_index() + |> Enum.map(fn param -> + test_with(desc, context, fun, param) + end) + end + defp test_with(desc, context, fun, {{param_desc, {_, _, values}}, num}) when is_atom(param_desc) do run("'#{desc}': '#{param_desc}': number of #{num}", context, fun, values) end diff --git a/test/ex_parameterized_callback_test.exs b/test/ex_parameterized_callback_test.exs index a405f17..0527a22 100644 --- a/test/ex_parameterized_callback_test.exs +++ b/test/ex_parameterized_callback_test.exs @@ -118,4 +118,20 @@ defmodule ExParameterizedParamsCallbackTest do "description for param2": {"hello", context[:hello], "hello and world"} # with description ] end + + @params [{1}] + test_with_params "bad", context, + fn (a) -> + assert a == 1 + end do + @params + end + + @params2 [{1}, {1}] + test_with_params "bad", context, + fn (a) -> + assert a == 1 + end do + @params2 + end end diff --git a/test/ex_parameterized_test.exs b/test/ex_parameterized_test.exs index 3f6f2cb..b902e40 100644 --- a/test/ex_parameterized_test.exs +++ b/test/ex_parameterized_test.exs @@ -156,4 +156,20 @@ defmodule ExParameterizedTest do end do Enum.map([{["a", "b"]}], fn (x) -> x end) end + + @params [{1}] + test_with_params "bad", + fn (a) -> + assert a == 1 + end do + @params + end + + @params2 [{1}, {1}] + test_with_params "bad", + fn (a) -> + assert a == 1 + end do + @params2 + end end