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:received/3 API for easier history checking #23

Merged
merged 3 commits into from
Mar 30, 2011
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
19 changes: 19 additions & 0 deletions src/meck.erl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
-export([validate/1]).
-export([unload/0]).
-export([unload/1]).
-export([called/3]).

%% Callback exports
-export([init/1]).
Expand Down Expand Up @@ -218,6 +219,16 @@ unload() -> lists:foldl(fun unload_if_mocked/2, [], registered()).
unload(Mod) when is_atom(Mod) -> call(Mod, stop), wait_for_exit(Mod);
unload(Mods) when is_list(Mods) -> [unload(Mod) || Mod <- Mods], ok.

%% @spec called(Mod:: atom(), Fun:: atom(), Args:: list(term())) -> boolean()
%% @doc Returns whether `Mod:Func` has been called with `Args`.
%%
%% This will check the history for the module, Mod, to determine
%% whether the function, Fun, was called with arguments, Args. If so,
%% this function returns true, otherwise false.
-spec called(Mod::atom(), Fun::atom(), Args::list()) -> boolean().
called(Mod, Fun, Args) ->
has_call({Mod, Fun, Args}, meck:history(Mod)).

%%==============================================================================
%% Callback functions
%%==============================================================================
Expand Down Expand Up @@ -328,6 +339,14 @@ unload_if_mocked(P, L) when length(P) > 5 ->
unload_if_mocked(_P, L) ->
L.

has_call({_M, _F, _A}, []) -> false;
has_call({M, F, A}, [{{M, F, A}, _Result} | _Rest]) ->
true;
has_call({M, F, A}, [{{M, F, A}, _ExType, _Exception, _Stack} | _Rest]) ->
true;
has_call({M, F, A}, [_Call | Rest]) ->
has_call({M, F, A}, Rest).

%% --- Mock handling -----------------------------------------------------------

init_expects(Mod, Options) ->
Expand Down
81 changes: 80 additions & 1 deletion test/meck_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,16 @@ meck_test_() ->
fun shortcut_expect_negative_arity_/1,
fun shortcut_call_return_value_/1,
fun shortcut_call_argument_/1,
fun delete_/1]]}.
fun delete_/1,
fun received_false_no_args_/1,
fun received_true_no_args_/1,
fun received_false_one_arg_/1,
fun received_true_one_arg_/1,
fun received_false_few_args_/1,
fun received_true_few_args_/1,
fun received_false_error_/1,
fun received_true_error_/1
]]}.

setup() ->
% Uncomment to run tests with dbg:
Expand Down Expand Up @@ -283,6 +292,76 @@ delete_(Mod) ->
?assertError(undef, Mod:test(a, b)),
?assert(meck:validate(Mod)).

received_false_no_args_(Mod) ->
Args = [],
ok = meck:expect(Mod, test, length(Args), ok),
?assertEqual(false, meck:called(Mod, test, Args)),
?assert(meck:validate(Mod)).

received_true_no_args_(Mod) ->
Args = [],
ok = meck:expect(Mod, test, length(Args), ok),
ok = Mod:test(),
?assertEqual(true, meck:called(Mod, test, Args)),
?assert(meck:validate(Mod)).

received_false_one_arg_(Mod) ->
Arg = "hello",
Args = [Arg],
ok = meck:expect(Mod, test, length(Args), ok),
?assertEqual(false, meck:called(Mod, test, [Arg])),
?assert(meck:validate(Mod)).

received_true_one_arg_(Mod) ->
Arg = "hello",
Args = [Arg],
ok = meck:expect(Mod, test, length(Args), ok),
ok = Mod:test(Arg),
?assertEqual(true, meck:called(Mod, test, [Arg])),
?assert(meck:validate(Mod)).

received_false_few_args_(Mod) ->
Arg1 = one,
Arg2 = 2,
Arg3 = {three, 3},
Arg4 = "four",
Args = [Arg1, Arg2, Arg3, Arg4],
ok = meck:expect(Mod, test, length(Args), ok),
?assertEqual(false, meck:called(Mod, test, Args)),
?assert(meck:validate(Mod)).

received_true_few_args_(Mod) ->
Arg1 = one,
Arg2 = 2,
Arg3 = {three, 3},
Arg4 = "four",
Args = [Arg1, Arg2, Arg3, Arg4],
ok = meck:expect(Mod, test, length(Args), ok),
ok = Mod:test(Arg1, Arg2, Arg3, Arg4),
?assertEqual(true, meck:called(Mod, test, Args)),
?assert(meck:validate(Mod)).

received_false_error_(Mod) ->
Arg1 = one,
Arg2 = "two",
Arg3 = {3, 3},
Args = [Arg1, Arg2, Arg3],
TestFun = fun (_, _, _) -> meck:exception(error, my_error) end,
ok = meck:expect(Mod, test, TestFun),
?assertEqual(false, meck:called(Mod, test, Args)),
?assert(meck:validate(Mod)).

received_true_error_(Mod) ->
Arg1 = one,
Arg2 = "two",
Arg3 = {3, 3},
Args = [Arg1, Arg2, Arg3],
TestFun = fun (_, _, _) -> meck:exception(error, my_error) end,
ok = meck:expect(Mod, test, TestFun),
catch Mod:test(Arg1, Arg2, Arg3),
?assertEqual(true, meck:called(Mod, test, Args)),
?assert(meck:validate(Mod)).

%% --- Tests with own setup ----------------------------------------------------

call_original_test() ->
Expand Down