Skip to content

Commit

Permalink
Rename count_calls/3+4 to num_calls
Browse files Browse the repository at this point in the history
  • Loading branch information
eproxus committed Oct 12, 2011
1 parent 283c279 commit 7e642e3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
34 changes: 17 additions & 17 deletions src/meck.erl
Expand Up @@ -39,8 +39,8 @@
-export([unload/1]).
-export([called/3]).
-export([called/4]).
-export([count_calls/3]).
-export([count_calls/4]).
-export([num_calls/3]).
-export([num_calls/4]).

%% Callback exports
-export([init/1]).
Expand Down Expand Up @@ -323,29 +323,29 @@ called(Mod, Fun, Args) ->
called(Pid, Mod, Fun, Args) ->
has_call({Mod, Fun, Args}, meck:history(Pid, Mod)).

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

%% @spec count_calls(Pid:: pid(), Mod:: atom(), Fun:: atom(),
%% @spec num_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())
-spec num_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).
num_calls(Pid, Mod, Fun, Args) ->
i_num_calls({Mod, Fun, Args}, meck:history(Pid, Mod), 0).

%%==============================================================================
%% Callback functions
Expand Down Expand Up @@ -769,14 +769,14 @@ get_history_without_pid(History) ->
remove_first_element(Tuple) when is_tuple(Tuple) ->
list_to_tuple(tl(tuple_to_list(Tuple))).

i_count_calls(_MFA, [], Count) ->
i_num_calls(_MFA, [], Count) ->
Count;
i_count_calls(MFA, [{MFA, _Result} | Rest], Count) ->
i_count_calls(MFA, Rest, Count + 1);
i_count_calls(MFA, [{MFA, _ExType, _Exception, _Stack} | Rest], Count) ->
i_count_calls(MFA, Rest, Count + 1);
i_count_calls(MFA, [_Call | Rest], Count) ->
i_count_calls(MFA, Rest, Count).
i_num_calls(MFA, [{MFA, _Result} | Rest], Count) ->
i_num_calls(MFA, Rest, Count + 1);
i_num_calls(MFA, [{MFA, _ExType, _Exception, _Stack} | Rest], Count) ->
i_num_calls(MFA, Rest, Count + 1);
i_num_calls(MFA, [_Call | Rest], Count) ->
i_num_calls(MFA, Rest, Count).

has_call(FuncMatch, History) ->
[] =/= match_history([{{FuncMatch, '_'}, [], ['$_']},
Expand Down
26 changes: 13 additions & 13 deletions test/meck_tests.erl
Expand Up @@ -73,9 +73,9 @@ meck_test_() ->
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 num_calls_/1,
fun num_calls_error_/1,
fun num_calls_with_pid_no_args_/1,
fun called_wildcard_/1,
fun sequence_/1,
fun sequence_multi_/1,
Expand Down Expand Up @@ -437,28 +437,28 @@ spawn_caller_and_sync(Mod, Func, Args) ->
receive {Pid, done} -> ok end, % sync with the spawned process
Pid.

count_calls_(Mod) ->
num_calls_(Mod) ->
Args = [],
IncorrectArgs = [foo],
ok = meck:expect(Mod, test1, length(Args), ok),
?assertEqual(0, meck:count_calls(Mod, test1, Args)),
?assertEqual(0, meck:num_calls(Mod, test1, Args)),
ok = apply(Mod, test1, Args),
?assertEqual(1, meck:count_calls(Mod, test1, Args)),
?assertEqual(0, meck:count_calls(Mod, test1, IncorrectArgs)).
?assertEqual(1, meck:num_calls(Mod, test1, Args)),
?assertEqual(0, meck:num_calls(Mod, test1, IncorrectArgs)).

count_calls_error_(Mod) ->
num_calls_error_(Mod) ->
Args = [one, "two", {3, 3}],
expect_catch_apply(Mod, test, Args),
?assertEqual(1, meck:count_calls(Mod, test, Args)).
?assertEqual(1, meck:num_calls(Mod, test, Args)).

count_calls_with_pid_no_args_(Mod) ->
num_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)),
?assertEqual(0, meck:num_calls(self(), Mod, test, Args)),
?assertEqual(1, meck:num_calls(Pid, Mod, test, Args)),
ok = apply(Mod, test, Args),
?assertEqual(1, meck:count_calls(self(), Mod, test, Args)).
?assertEqual(1, meck:num_calls(self(), Mod, test, Args)).

expect_apply(Mod, Func, Args) ->
ok = meck:expect(Mod, Func, length(Args), ok),
Expand Down

0 comments on commit 7e642e3

Please sign in to comment.