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

Commit

Permalink
Fix private attribute (#15)
Browse files Browse the repository at this point in the history
* implement private attribute case

* update

* update some unquote/quote

* for callback
  • Loading branch information
KazuCocoa authored Jan 1, 2018
1 parent 1e6bd29 commit a289778
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 16 deletions.
14 changes: 13 additions & 1 deletion lib/ex_parameterized/params.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 ->
Expand Down
52 changes: 37 additions & 15 deletions lib/ex_parameterized/params_callback.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions test/ex_parameterized_callback_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 16 additions & 0 deletions test/ex_parameterized_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a289778

Please sign in to comment.