From 6c20e24e8b5e752a9075e8db8239a2be645e3fb9 Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Wed, 4 Jul 2018 23:05:55 +0900 Subject: [PATCH] Fix test fails when 2+maps (#23) * fix map error against 2+ maps * apply format * update travis --- .travis.yml | 2 ++ lib/ex_parameterized/params.ex | 27 +++++++++++++++++++++---- lib/ex_parameterized/params_callback.ex | 27 +++++++++++++++++++++---- test/ex_parameterized_callback_test.exs | 20 ++++++++++++++++++ test/ex_parameterized_test.exs | 13 +++++++++++- 5 files changed, 80 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9fc0a33..15525d2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/lib/ex_parameterized/params.ex b/lib/ex_parameterized/params.ex index a651677..c72b5a3 100644 --- a/lib/ex_parameterized/params.ex +++ b/lib/ex_parameterized/params.ex @@ -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) @@ -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() @@ -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 diff --git a/lib/ex_parameterized/params_callback.ex b/lib/ex_parameterized/params_callback.ex index 3c3c05a..9605fd3 100644 --- a/lib/ex_parameterized/params_callback.ex +++ b/lib/ex_parameterized/params_callback.ex @@ -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() @@ -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() @@ -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 diff --git a/test/ex_parameterized_callback_test.exs b/test/ex_parameterized_callback_test.exs index ccc515d..4058bad 100644 --- a/test/ex_parameterized_callback_test.exs +++ b/test/ex_parameterized_callback_test.exs @@ -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 @@ -124,6 +143,7 @@ defmodule ExParameterizedParamsCallbackTest do p end do make = fn a -> a + 1 end + [ {make.(1)} ] diff --git a/test/ex_parameterized_test.exs b/test/ex_parameterized_test.exs index 9d1c258..094b012 100644 --- a/test/ex_parameterized_test.exs +++ b/test/ex_parameterized_test.exs @@ -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 @@ -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)} ]