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

Add meck:expects/1,2 #187

Merged
merged 6 commits into from
Jan 22, 2018
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
29 changes: 29 additions & 0 deletions src/meck.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
-export([loop/4]).
-export([delete/3]).
-export([delete/4]).
-export([expects/1]).
-export([expects/2]).
-export([exception/2]).
-export([passthrough/1]).
-export([history/1]).
Expand Down Expand Up @@ -318,6 +320,33 @@ delete(Mod, Func, Ari, Force) when is_list(Mod) ->
delete(Mod, Func, Ari) ->
delete(Mod, Func, Ari, false).

%% @doc Returns the list of expectations.
%%
%% Returns the list of MFAs that were replaced by expectations
%% If `ExcludePassthrough' is on, only expectations that are not
%% direct passthroughs are returned
-spec expects(Mods, ExcludePassthrough) -> [{Mod, Func, Ari}] when
Mods :: Mod | [Mod],
Mod :: atom(),
Func :: atom(),
Ari :: byte(),
ExcludePassthrough :: boolean().
expects(Mod, ExcludePassthrough) when is_atom(Mod) ->
meck_proc:list_expects(Mod, ExcludePassthrough);
expects(Mods, ExcludePassthrough) when is_list(Mods) ->
[Expect || Mod <- Mods, Expect <- expects(Mod, ExcludePassthrough)].

%% @doc Returns the list of expectations.
%%
%% Returns the list of MFAs that were replaced by expectations
-spec expects(Mods) -> [{Mod, Func, Ari}] when
Mods :: Mod | [Mod],
Mod :: atom(),
Func :: atom(),
Ari :: byte().
expects(Mod) ->
expects(Mod, false).

%% @doc Throws an expected exception inside an expect fun.
%%
%% This exception will get thrown without invalidating the mocked
Expand Down
7 changes: 7 additions & 0 deletions src/meck_expect.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
-export([new_passthrough/1]).
-export([new_dummy/2]).
-export([func_ari/1]).
-export([is_passthrough/1]).
-export([fetch_result/2]).

%%%============================================================================
Expand Down Expand Up @@ -73,6 +74,12 @@ new_dummy({Func, Ari}, RetSpec) ->
func_ari({FuncAri, _Clauses}) ->
FuncAri.

-spec is_passthrough(expect()) -> boolean().
is_passthrough({_FuncAri, [{_Matcher, RetSpec}]}) ->
meck_ret_spec:is_passthrough(RetSpec);
is_passthrough(_) ->
false.

-spec fetch_result(Args::[any()], expect()) ->
{undefined, unchanged} |
{meck_ret_spec:result_spec(), unchanged} |
Expand Down
17 changes: 17 additions & 0 deletions src/meck_proc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
-export([start/2]).
-export([set_expect/2]).
-export([delete_expect/4]).
-export([list_expects/2]).
-export([get_history/1]).
-export([wait/6]).
-export([reset/1]).
Expand Down Expand Up @@ -124,6 +125,11 @@ set_expect(Mod, Expect) ->
delete_expect(Mod, Func, Ari, Force) ->
gen_server(call, Mod, {delete_expect, Func, Ari, Force}).

-spec list_expects(Mod::atom(), ExcludePassthrough::boolean()) ->
[{Mod::atom(), Func::atom(), Ari::byte}].
list_expects(Mod, ExcludePassthrough) ->
gen_server(call, Mod, {list_expects, ExcludePassthrough}).

-spec add_history_exception(
Mod::atom(), CallerPid::pid(), Func::atom(), Args::[any()],
{Class::error|exit|throw, Reason::any(), StackTrace::any()}) ->
Expand Down Expand Up @@ -251,6 +257,17 @@ handle_call({delete_expect, Func, Ari, Force}, From,
do_delete_expect(Mod, {Func, Ari}, Expects, ErasePassThrough),
{noreply, S#state{expects = NewExpects,
reload = {CompilerPid, From}}};
handle_call({list_expects, ExcludePassthrough}, _From, S = #state{mod = Mod, expects = Expects}) ->
Result =
case ExcludePassthrough of
false ->
[{Mod, Func, Ari} || {Func, Ari} <- dict:fetch_keys(Expects)];
true ->
[{Mod, Func, Ari} ||
{{Func, Ari}, Expect} <- dict:to_list(Expects),
not meck_expect:is_passthrough(Expect)]
end,
{reply, Result, S};
handle_call(get_history, _From, S = #state{history = undefined}) ->
{reply, [], S};
handle_call(get_history, _From, S) ->
Expand Down
4 changes: 4 additions & 0 deletions src/meck_ret_spec.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
-export([loop/1]).
-export([raise/2]).
-export([is_meck_exception/1]).
-export([is_passthrough/1]).
-export([retrieve_result/1]).
-export([eval_result/4]).

Expand Down Expand Up @@ -74,6 +75,9 @@ is_meck_exception({meck_raise, MockedClass, MockedReason}) ->
is_meck_exception(_Reason) ->
false.

-spec is_passthrough(ret_spec()) -> boolean().
is_passthrough(RetSpec) -> RetSpec == meck_passthrough.

-spec retrieve_result(RetSpec::ret_spec()) ->
{result_spec(), NewRetSpec::ret_spec() | unchanged}.
retrieve_result(RetSpec) ->
Expand Down
26 changes: 25 additions & 1 deletion test/meck_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ meck_test_() ->
fun shortcut_opaque_/1,
fun shortcut_stacktrace_/1,
fun delete_/1,
fun expects_/1,
fun called_false_no_args_/1,
fun called_true_no_args_/1,
fun called_true_two_functions_/1,
Expand Down Expand Up @@ -406,6 +407,13 @@ delete_(Mod) ->
?assertError(undef, Mod:test(a, b)),
?assert(meck:validate(Mod)).

expects_(Mod) ->
?assertEqual([], meck:expects(Mod)),
ok = meck:expect(Mod, test, 2, ok),
?assertEqual([{Mod, test, 2}], meck:expects(Mod)),
ok = meck:expect(Mod, test2, 0, ok),
?assertEqual([{Mod, test, 2}, {Mod, test2, 0}], lists:sort(meck:expects(Mod))).

called_false_no_args_(Mod) ->
Args = [],
ok = meck:expect(Mod, test, length(Args), ok),
Expand Down Expand Up @@ -1217,6 +1225,14 @@ multi_delete_test() ->
?assert(meck:validate(Mods)),
ok = meck:unload(Mods).

multi_expects_test() ->
Mods = [mod1, mod2, mod3],
ok = meck:new(Mods, [non_strict]),
ok = meck:expect(Mods, test, 0, ok),
?assertEqual([{mod1, test, 0}, {mod2, test, 0}, {mod3, test, 0}],
lists:sort(meck:expects(Mods))),
ok = meck:unload(Mods).

multi_reset_test() ->
% Given
Mods = [mod1, mod2, mod3],
Expand Down Expand Up @@ -1516,7 +1532,8 @@ meck_passthrough_test_() ->
{foreach, fun setup_passthrough/0, fun teardown/1,
[{with, [T]} || T <- [
fun delete_passthrough_/1,
fun delete_passthrough_force_/1
fun delete_passthrough_force_/1,
fun expects_passthrough_/1
]]}.

setup_passthrough() ->
Expand All @@ -1540,6 +1557,13 @@ delete_passthrough_force_(Mod) ->
?assertError(undef, Mod:test(a, b)),
?assert(meck:validate(Mod)).

expects_passthrough_(Mod) ->
ok = meck:expect(Mod, test, 2, ok),
?assertEqual([{Mod, a, 0}, {Mod, b, 0}, {Mod, c, 2}, {Mod, test, 2}],
lists:sort(meck:expects(Mod, false))),
?assertEqual([{Mod, test, 2}], meck:expects(Mod, true)).


%%=============================================================================
%% Internal Functions
%%=============================================================================
Expand Down