Skip to content

Commit

Permalink
Fix returning of opaque terms in shortcut expectations
Browse files Browse the repository at this point in the history
  • Loading branch information
eproxus committed Jun 9, 2011
1 parent 9b8934a commit b1904a2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
39 changes: 28 additions & 11 deletions src/meck.erl
Original file line number Diff line number Diff line change
Expand Up @@ -439,12 +439,27 @@ e_fetch(Expects, Func, Arity) ->
e_delete(Expects, Func, Arity) ->
dict:erase({Func, Arity}, Expects).

interface_equal(NewExpects, OldExpects) ->
dict:fetch_keys(NewExpects) == dict:fetch_keys(OldExpects).

%% --- Code generation ---------------------------------------------------------

func(Mod, {Func, Arity}, {anon, Arity, Result}) ->
case contains_opaque(Result) of
true ->
func_exec(Mod, Func, Arity);
false ->
func_native(Mod, Func, Arity, Result)
end;
func(Mod, {Func, Arity}, _Expect) ->
func_exec(Mod, Func, Arity).

func_exec(Mod, Func, Arity) ->
Args = args(Arity),
?function(Func, Arity,
[?clause(Args,
[?call(?MODULE, exec,
[?atom(Mod), ?atom(Func), ?integer(Arity),
list(Args)])])]).

func_native(Mod, Func, Arity, Result) ->
Args = args(Arity),
AbsResult = erl_parse:abstract(Result),
?function(
Expand All @@ -457,14 +472,16 @@ func(Mod, {Func, Arity}, {anon, Arity, Result}) ->
?tuple([?tuple([?atom(Mod), ?atom(Func),
list(Args)]),
AbsResult])])]),
AbsResult])]);
func(Mod, {Func, Arity}, _Expect) ->
Args = args(Arity),
?function(Func, Arity,
[?clause(Args,
[?call(?MODULE, exec,
[?atom(Mod), ?atom(Func), ?integer(Arity),
list(Args)])])]).
AbsResult])]).

contains_opaque(Term) when is_pid(Term); is_port(Term); is_function(Term) ->
true;
contains_opaque(Term) when is_list(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));
contains_opaque(_Term) ->
false.


to_forms(Mod, Expects) ->
Expand Down
5 changes: 5 additions & 0 deletions test/meck_performance_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ run(N) ->
test_avg(meck, expect, [test, shortcut, 0, ok], N)),
io:format("expect/4+args\t~p\t~p\t~p\t~p~n",
test_avg(meck, expect, [test, shortcut_args, 2, ok], N)),

meck:expect(test, shortcut_opaque, 0, self()),

io:format("~n\t\tMin\tMax\tMed\tAvg~n"),
io:format("normal\t\t~p\t~p\t~p\t~p~n",
test_avg(test, normal, [], N)),
Expand All @@ -29,6 +32,8 @@ run(N) ->
test_avg(test, shortcut, [], N)),
io:format("shortcut_args\t~p\t~p\t~p\t~p~n",
test_avg(test, shortcut_args, [a, b], N)),
io:format("shortcut_opaque\t~p\t~p\t~p\t~p~n",
test_avg(test, shortcut_opaque, [], N)),
meck:unload(test),
ok.

Expand Down
5 changes: 5 additions & 0 deletions test/meck_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ meck_test_() ->
fun shortcut_call_return_value_/1,
fun shortcut_call_argument_/1,
fun shortcut_re_add_/1,
fun shortcut_opaque_/1,
fun delete_/1,
fun called_false_no_args_/1,
fun called_true_no_args_/1,
Expand Down Expand Up @@ -298,6 +299,10 @@ shortcut_re_add_(Mod) ->
?assertEqual(new, Mod:test(hest, 1)),
?assertEqual(true, meck:validate(Mod)).

shortcut_opaque_(Mod) ->
ok = meck:expect(Mod, test, 0, self()),
?assertMatch(P when is_pid(P), Mod:test()).

delete_(Mod) ->
ok = meck:expect(Mod, test, 2, ok),
?assertEqual(ok, meck:delete(Mod, test, 2)),
Expand Down

0 comments on commit b1904a2

Please sign in to comment.