Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support meck:expect with improper list mock data #102

Merged
merged 2 commits into from Mar 31, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/meck_code_gen.erl
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,22 @@ contains_opaque(Term) when is_pid(Term); is_port(Term); is_function(Term);
is_reference(Term) ->
true;
contains_opaque(Term) when is_list(Term) ->
lists:any(fun contains_opaque/1, Term);
lists_any(fun contains_opaque/1, Term);
contains_opaque(Term) when is_tuple(Term) ->
lists:any(fun contains_opaque/1, tuple_to_list(Term));
lists_any(fun contains_opaque/1, tuple_to_list(Term));
contains_opaque(_Term) ->
false.

%% based on lists.erl but accepts improper lists.
lists_any(Pred, []) when is_function(Pred, 1) -> false;
lists_any(Pred, [Hd|Tail]) ->
case Pred(Hd) of
true -> true;
false -> lists_any(Pred, Tail)
end;
lists_any(Pred, Improper) ->
Pred(Improper).

args(0) -> [];
args(Arity) -> [?var(var_name(N)) || N <- lists:seq(1, Arity)].

Expand Down
7 changes: 7 additions & 0 deletions test/meck_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ meck_test_() ->
fun ?MODULE:expect_/1,
fun ?MODULE:exports_/1,
fun ?MODULE:call_return_value_/1,
fun ?MODULE:call_return_value_improper_list_/1,
fun ?MODULE:call_argument_/1,
fun ?MODULE:call_undef_/1,
fun ?MODULE:call_function_clause_/1,
Expand Down Expand Up @@ -146,6 +147,12 @@ call_return_value_(Mod) ->
?assertEqual(apa, Mod:test()),
?assertEqual(true, meck:validate(Mod)).

call_return_value_improper_list_(Mod) ->
Dict = dict:store(hest, apa, dict:new()),
ok = meck:expect(Mod, test, 0, Dict),
?assertEqual(Dict, Mod:test()),
?assertEqual(true, meck:validate(Mod)).

call_argument_(Mod) ->
ok = meck:expect(Mod, test, fun(hest, 1) -> apa end),
?assertEqual(apa, Mod:test(hest, 1)),
Expand Down