Skip to content

Commit

Permalink
Adding new functions to search in the history for calls by a specific…
Browse files Browse the repository at this point in the history
… pid.

* Added meck:called/4
* Added meck:count_calls/4
* Added meck:wildcard_count_calls/4
  • Loading branch information
daha committed Oct 1, 2011
1 parent 992a476 commit 16fbe86
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 3 deletions.
46 changes: 44 additions & 2 deletions src/meck.erl
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@
-export([unload/0]).
-export([unload/1]).
-export([called/3]).
-export([called/4]).
-export([count_calls/3]).
-export([count_calls/4]).
-export([wildcard_count_calls/3]).
-export([wildcard_count_calls/4]).

%% Callback exports
-export([init/1]).
Expand Down Expand Up @@ -308,6 +311,17 @@ unload(Mods) when is_list(Mods) -> lists:foreach(fun unload/1, Mods), ok.
called(Mod, Fun, Args) ->
has_call({Mod, Fun, Args}, meck:history(Mod)).

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

%% @spec count_calls(Mod:: atom(), Fun:: atom(), Args:: list(term()))
%% -> non_neg_integer()
%% @doc Returns the number of times `Mod:Func' has been called with `Args'.
Expand All @@ -319,10 +333,23 @@ called(Mod, Fun, Args) ->
count_calls(Mod, Fun, Args) ->
i_count_calls({Mod, Fun, Args}, meck:history(Mod), 0).

%% @spec count_calls(Pid:: pid(), Mod:: atom(), Fun:: atom(),
%% Args:: list(term())) -> non_neg_integer()
%% @doc Returns the number of times process `Pid' has called `Mod:Func'
%% with `Args'.
%%
%% This will check the history for the module, `Mod', to determine how
%% many times process `Pid' has called the function, `Fun', with
%% arguments, `Args' and returns the result.
-spec count_calls(Pid::pid(), Mod::atom(), Fun::atom(), Args::list())
-> non_neg_integer().
count_calls(Pid, Mod, Fun, Args) ->
i_count_calls({Mod, Fun, Args}, meck:history(Pid, Mod), 0).

%% @spec wildcard_count_calls(Mod:: atom(), Fun:: atom(),
%% Args::list(term()) | '_') -> non_neg_integer()
%% @doc Returns the number of times `Mod:Func' has been called with
%% wildcards matching of `Args'.
%% @doc Returns the number of times `Mod:Func' has been called
%% with wildcards matching of `Args'.
%%
%% This will check the history for the module, `Mod', to determine
%% whether the function, `Fun', was called with arguments, `Args'. It
Expand All @@ -334,6 +361,21 @@ count_calls(Mod, Fun, Args) ->
wildcard_count_calls(Mod, Fun, Args) ->
i_wildcard_count_calls({Mod, Fun, Args}, meck:history(Mod), 0).

%% @spec wildcard_count_calls(Pid:: pid(), Mod:: atom(), Fun:: atom(),
%% Args::list(term()) | '_') -> non_neg_integer()
%% @doc Returns the number of times process `Pid' has called `Mod:Func'
%% with wildcards matching of `Args'.
%%
%% This will check the history for the module, `Mod', to determine how
%% many times process `Pid' has called the function, `Fun', with
%% arguments, `Args'. It uses the wildcard '_' to match one element
%% in a list or tuple. By setting `Args' to '_' any arguments is
%% matched. It returns the number of matching calls.
-spec wildcard_count_calls(Pid::pid(), Mod::atom(), Fun::atom(), Args::list() | '_' ) ->
non_neg_integer().
wildcard_count_calls(Pid, Mod, Fun, Args) ->
i_wildcard_count_calls({Mod, Fun, Args}, meck:history(Pid, Mod), 0).

%%==============================================================================
%% Callback functions
%%==============================================================================
Expand Down
45 changes: 44 additions & 1 deletion test/meck_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ meck_test_() ->
fun called_true_few_args_/1,
fun called_false_error_/1,
fun called_true_error_/1,
fun called_with_pid_no_args_/1,
fun count_calls_/1,
fun count_calls_error_/1,
fun count_calls_with_pid_no_args_/1,
fun wildcard_count_calls_with_wildcard_on_args_/1,
fun wildcard_count_calls_with_wildcard_on_args_error_/1,
fun wildcard_count_calls_simple_/1,
Expand All @@ -83,6 +85,7 @@ meck_test_() ->
fun wildcard_count_calls_in_tuples_/1,
fun wildcard_count_calls_simple_error_/1,
fun wildcard_count_calls_error_/1,
fun wildcard_count_calls_with_pid_no_args_/1,
fun sequence_/1,
fun sequence_multi_/1,
fun loop_/1,
Expand Down Expand Up @@ -424,6 +427,25 @@ called_true_error_(Mod) ->
expect_catch_apply(Mod, test, Args),
assert_called(Mod, test, Args, true).

called_with_pid_no_args_(Mod) ->
Args = [],
ok = meck:expect(Mod, test, length(Args), ok),
Pid = spawn_caller_and_sync(Mod, test, Args),
assert_called(self(), Mod, test, Args, false),
assert_called(Pid, Mod, test, Args, true),
ok = apply(Mod, test, Args),
assert_called(self(), Mod, test, Args, true).

spawn_caller_and_sync(Mod, Func, Args) ->
TestPid = self(),
Fun = fun() ->
catch apply(Mod, Func, Args),
TestPid ! {self(), done}
end,
Pid = spawn(Fun),
receive {Pid, done} -> ok end, % sync with the spawned process
Pid.

count_calls_(Mod) ->
Args = [],
IncorrectArgs = [foo],
Expand All @@ -438,6 +460,15 @@ count_calls_error_(Mod) ->
expect_catch_apply(Mod, test, Args),
?assertEqual(1, meck:count_calls(Mod, test, Args)).

count_calls_with_pid_no_args_(Mod) ->
Args = [],
ok = meck:expect(Mod, test, length(Args), ok),
Pid = spawn_caller_and_sync(Mod, test, Args),
?assertEqual(0, meck:count_calls(self(), Mod, test, Args)),
?assertEqual(1, meck:count_calls(Pid, Mod, test, Args)),
ok = apply(Mod, test, Args),
?assertEqual(1, meck:count_calls(self(), Mod, test, Args)).

wildcard_count_calls_with_wildcard_on_args_(Mod) ->
Args = [a],
expect_apply(Mod, test1, Args),
Expand Down Expand Up @@ -516,6 +547,15 @@ wildcard_count_calls_error_(Mod) ->
?assertEqual(1, meck:wildcard_count_calls(Mod, test, MatchingArgs2)),
?assertEqual(1, meck:wildcard_count_calls(Mod, test, MatchingArgs3)).

wildcard_count_calls_with_pid_no_args_(Mod) ->
Args = [],
ok = meck:expect(Mod, test, length(Args), ok),
Pid = spawn_caller_and_sync(Mod, test, Args),
?assertEqual(0, meck:wildcard_count_calls(self(), Mod, test, Args)),
?assertEqual(1, meck:wildcard_count_calls(Pid, Mod, test, Args)),
ok = apply(Mod, test, Args),
?assertEqual(1, meck:wildcard_count_calls(self(), Mod, test, Args)).

expect_apply(Mod, Func, Args) ->
ok = meck:expect(Mod, Func, length(Args), ok),
ok = apply(Mod, Func, Args).
Expand All @@ -525,7 +565,6 @@ expect_catch_apply(Mod, Func, Args) ->
ok = meck:expect(Mod, Func, TestFun),
catch apply(Mod, Func, Args).


sequence_(Mod) ->
Sequence = [a, b, c, d, e],
?assertEqual(ok, meck:sequence(Mod, s, 2, Sequence)),
Expand Down Expand Up @@ -881,3 +920,7 @@ cannot_expect_bif_or_autogenerated_test() ->
assert_called(Mod, Function, Args, WasCalled) ->
?assertEqual(WasCalled, meck:called(Mod, Function, Args)),
?assert(meck:validate(Mod)).

assert_called(Pid, Mod, Function, Args, WasCalled) ->
?assertEqual(WasCalled, meck:called(Pid, Mod, Function, Args)),
?assert(meck:validate(Mod)).

0 comments on commit 16fbe86

Please sign in to comment.