Skip to content

Commit

Permalink
Merge pull request #96 from horkhe/develop
Browse files Browse the repository at this point in the history
Make `undefined_module` error contain module name
  • Loading branch information
eproxus committed Feb 13, 2013
2 parents 7f26c19 + 21e7f85 commit d06c9e7
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 98 deletions.
29 changes: 0 additions & 29 deletions src/meck.erl
Expand Up @@ -108,7 +108,6 @@
%% It is used in {@link expect/3} and {@link expect/4} to define a function
%% clause of complex multi-clause expectations.


%%%============================================================================
%%% Interface exports
%%%============================================================================
Expand All @@ -120,7 +119,6 @@
new(Mod) when is_atom(Mod) -> new(Mod, []);
new(Mod) when is_list(Mod) -> lists:foreach(fun new/1, Mod), ok.


%% @doc Creates new mocked module(s).
%%
%% This replaces the current version (if any) of the modules in `Mod'
Expand Down Expand Up @@ -180,7 +178,6 @@ new(Mod, Options) when is_list(Mod) ->
lists:foreach(fun(M) -> new(M, Options) end, Mod),
ok.


%% @doc Add expectation for a function `Func' to the mocked modules `Mod'.
%%
%% An expectation is either of the following:
Expand Down Expand Up @@ -214,7 +211,6 @@ expect(Mod, Func, Expectation) when is_atom(Mod), is_atom(Func) ->
Expect = meck_expect:new(Func, Expectation),
check_expect_result(meck_proc:set_expect(Mod, Expect)).


%% @doc Adds an expectation with the supplied arity and return value.
%%
%% This creates an expectation which takes `Arity' number of functions
Expand All @@ -234,7 +230,6 @@ expect(Mod, Func, AriOrArgs, RetSpec) when is_atom(Mod), is_atom(Func) ->
Expect = meck_expect:new(Func, AriOrArgs, RetSpec),
check_expect_result(meck_proc:set_expect(Mod, Expect)).


%% @equiv expect(Mod, Func, Ari, seq(Sequence))
%% @deprecated Please use {@link expect/3} or {@link expect/4} along with
%% {@link ret_spec()} generated by {@link seq/1}.
Expand All @@ -252,7 +247,6 @@ sequence(Mod, Func, Ari, Sequence) when is_list(Mod) ->
lists:foreach(fun(M) -> sequence(M, Func, Ari, Sequence) end, Mod),
ok.


%% @equiv expect(Mod, Func, Ari, loop(Loop))
%% @deprecated Please use {@link expect/3} or {@link expect/4} along with
%% {@link ret_spec()} generated by {@link loop/1}.
Expand All @@ -270,7 +264,6 @@ loop(Mod, Func, Ari, Loop) when is_list(Mod) ->
lists:foreach(fun(M) -> loop(M, Func, Ari, Loop) end, Mod),
ok.


%% @doc Deletes an expectation.
%%
%% Deletes the expectation for the function `Func' with the matching
Expand All @@ -287,7 +280,6 @@ delete(Mod, Func, Ari) when is_list(Mod) ->
lists:foreach(fun(M) -> delete(M, Func, Ari) end, Mod),
ok.


%% @doc Throws an expected exception inside an expect fun.
%%
%% This exception will get thrown without invalidating the mocked
Expand All @@ -301,7 +293,6 @@ delete(Mod, Func, Ari) when is_list(Mod) ->
exception(Class, Reason) when Class == throw; Class == error; Class == exit ->
erlang:throw(meck_ret_spec:raise(Class, Reason)).


%% @doc Calls the original function (if existing) inside an expectation fun.
%%
%% <em>Note: this code should only be used inside an expect fun.</em>
Expand All @@ -312,7 +303,6 @@ passthrough(Args) when is_list(Args) ->
{Mod, Func} = meck_code_gen:get_current_call(),
erlang:apply(meck_util:original_name(Mod), Func, Args).


%% @doc Validate the state of the mock module(s).
%%
%% The function returns `true' if the mocked module(s) has been used
Expand All @@ -330,15 +320,13 @@ validate(Mod) when is_atom(Mod) ->
validate(Mod) when is_list(Mod) ->
not lists:member(false, [validate(M) || M <- Mod]).


%% @doc Return the call history of the mocked module for all processes.
%%
%% @equiv history(Mod, '_')
-spec history(Mod) -> history() when
Mod :: atom().
history(Mod) when is_atom(Mod) -> meck_history:get_history('_', Mod).


%% @doc Return the call history of the mocked module for the specified process.
%%
%% Returns a list of calls to the mocked module and their results for
Expand All @@ -357,7 +345,6 @@ history(Mod, OptCallerPid)
when is_atom(Mod), is_pid(OptCallerPid) orelse OptCallerPid == '_' ->
meck_history:get_history(OptCallerPid, Mod).


%% @doc Unloads all mocked modules from memory.
%%
%% The function returns the list of mocked modules that were unloaded
Expand All @@ -367,7 +354,6 @@ history(Mod, OptCallerPid)
Mod :: atom().
unload() -> lists:foldl(fun unload_if_mocked/2, [], registered()).


%% @doc Unload a mocked module or a list of mocked modules.
%%
%% This will purge and delete the module(s) from the Erlang virtual
Expand All @@ -383,7 +369,6 @@ unload(Mod) when is_atom(Mod) ->
unload(Mods) when is_list(Mods) ->
lists:foreach(fun unload/1, Mods), ok.


%% @doc Returns whether `Mod:Func' has been called with `Args'.
%%
%% @equiv called(Mod, Fun, Args, '_')
Expand All @@ -394,7 +379,6 @@ unload(Mods) when is_list(Mods) ->
called(Mod, OptFun, OptArgs) ->
meck_history:num_calls('_', Mod, OptFun, OptArgs) > 0.


%% @doc Returns whether `Pid' has called `Mod:Func' with `Args'.
%%
%% This will check the history for the module, `Mod', to determine
Expand All @@ -413,7 +397,6 @@ called(Mod, OptFun, OptArgs) ->
called(Mod, OptFun, OptArgs, OptPid) ->
meck_history:num_calls(OptPid, Mod, OptFun, OptArgs) > 0.


%% @doc Returns the number of times `Mod:Func' has been called with `Args'.
%%
%% @equiv num_calls(Mod, Fun, Args, '_')
Expand All @@ -424,7 +407,6 @@ called(Mod, OptFun, OptArgs, OptPid) ->
num_calls(Mod, OptFun, OptArgs) ->
meck_history:num_calls('_', Mod, OptFun, OptArgs).


%% @doc Returns the number of times process `Pid' has called `Mod:Func'
%% with `Args'.
%%
Expand All @@ -441,7 +423,6 @@ num_calls(Mod, OptFun, OptArgs) ->
num_calls(Mod, OptFun, OptArgs, OptPid) ->
meck_history:num_calls(OptPid, Mod, OptFun, OptArgs).


%% @doc Erases the call history for a mocked module or a list of mocked modules.
%%
%% This function will erase all calls made heretofore from the history of the
Expand All @@ -455,7 +436,6 @@ reset(Mod) when is_atom(Mod) ->
reset(Mods) when is_list(Mods) ->
lists:foreach(fun(Mod) -> reset(Mod) end, Mods).


%% @doc Converts a list of terms into {@link ret_spec()} defining a loop of
%% values. It is intended to be in construction of clause specs for the
%% {@link expect/3} function.
Expand All @@ -467,7 +447,6 @@ reset(Mods) when is_list(Mods) ->
Loop :: [ret_spec()].
loop(Loop) -> meck_ret_spec:loop(Loop).


%% @doc Converts a list of terms into {@link ret_spec()} defining a sequence of
%% values. It is intended to be in construction of clause specs for the
%% {@link expect/3} function.
Expand All @@ -479,15 +458,13 @@ loop(Loop) -> meck_ret_spec:loop(Loop).
Sequence :: [ret_spec()].
seq(Sequence) -> meck_ret_spec:seq(Sequence).


%% @doc Converts a term into {@link ret_spec()} defining an individual value.
%% It is intended to be in construction of clause specs for the
%% {@link expect/3} function.
-spec val(Value) -> ret_spec() when
Value :: any().
val(Value) -> meck_ret_spec:val(Value).


%% @doc Creates a {@link ret_spec()} that defines an exception.
%%
%% Calls to an expect, created with {@link ret_spec()} returned by this function,
Expand All @@ -497,7 +474,6 @@ val(Value) -> meck_ret_spec:val(Value).
Reason :: term.
raise(Class, Reason) -> meck_ret_spec:raise(Class, Reason).


%% @doc Creates a {@link ret_spec()} that makes the original module function be
%% called.
%%
Expand All @@ -506,14 +482,12 @@ raise(Class, Reason) -> meck_ret_spec:raise(Class, Reason).
-spec passthrough() -> ret_spec().
passthrough() -> meck_ret_spec:passthrough().


%% @doc Creates a {@link ret_spec()} from a function. Calls to an expect,
%% created with {@link ret_spec()} returned by this function, will be forwarded
%% to the specified function.
-spec exec(fun()) -> ret_spec().
exec(Fun) -> meck_ret_spec:exec(Fun).


%% @doc creates a {@link matcher/0} instance from either `Predicate' or
%% `HamcrestMatcher'.
%% <ul>
Expand All @@ -531,7 +505,6 @@ exec(Fun) -> meck_ret_spec:exec(Fun).
is(MatcherImpl) ->
meck_matcher:new(MatcherImpl).


%%%============================================================================
%%% Internal functions
%%%============================================================================
Expand All @@ -541,7 +514,6 @@ wait_for_exit(Mod) ->
MonitorRef = erlang:monitor(process, meck_util:proc_name(Mod)),
receive {'DOWN', MonitorRef, _Type, _Object, _Info} -> ok end.


-spec unload_if_mocked(Mod::atom() | string(), Unloaded::[atom()]) ->
NewUnloaded::[atom()].
unload_if_mocked(Mod, Unloaded) when is_atom(Mod) ->
Expand All @@ -562,7 +534,6 @@ unload_if_mocked(ModName, Unloaded) when length(ModName) > 5 ->
unload_if_mocked(_P, Unloaded) ->
Unloaded.


-spec check_expect_result(ok | {error, Reason::any()}) -> ok.
check_expect_result(ok) -> ok;
check_expect_result({error, Reason}) -> erlang:error(Reason).
23 changes: 0 additions & 23 deletions src/meck_code_gen.erl
Expand Up @@ -24,34 +24,25 @@
%% Exported to be accessible from generated modules.
-export([exec/4]).


%%%============================================================================
%%% Definitions
%%%============================================================================

-define(CURRENT_CALL, '$meck_call').

-define(call(Module, Function, Arguments),
{call, ?LINE,
{remote, ?LINE, ?atom(Module), ?atom(Function)},
Arguments}).

-define(atom(Atom), {atom, ?LINE, Atom}).

-define(integer(Integer), {integer, ?LINE, Integer}).

-define(var(Name), {var, ?LINE, Name}).

-define(attribute(Attribute, Args), {attribute, ?LINE, Attribute, Args}).

-define(function(Name, Arity, Clauses),
{function, ?LINE, Name, Arity, Clauses}).

-define(clause(Arguments, Body), {clause, ?LINE, Arguments, [], Body}).

-define(tuple(Elements), {tuple, ?LINE, Elements}).


%%%============================================================================
%%% API
%%%============================================================================
Expand All @@ -60,12 +51,10 @@ to_forms(Mod, Expects) ->
{Exports, Functions} = functions(Mod, Expects),
[?attribute(module, Mod)] ++ attributes(Mod) ++ Exports ++ Functions.


-spec get_current_call() -> {Mod::atom(), Func::atom()}.
get_current_call() ->
get(?CURRENT_CALL).


%%%============================================================================
%%% Internal functions
%%%============================================================================
Expand All @@ -79,7 +68,6 @@ attributes(Mod) ->
error:undef -> []
end.


functions(Mod, Expects) ->
dict:fold(fun(Export, Expect, {Exports, Functions}) ->
{[?attribute(export, [Export]) | Exports],
Expand All @@ -88,7 +76,6 @@ functions(Mod, Expects) ->
{[], []},
Expects).


func(Mod, {Func, Arity}, {anon, Arity, Result}) ->
case contains_opaque(Result) of
true ->
Expand All @@ -99,7 +86,6 @@ func(Mod, {Func, Arity}, {anon, Arity, Result}) ->
func(Mod, {Func, Arity}, _Expect) ->
func_exec(Mod, Func, Arity).


func_exec(Mod, Func, Arity) ->
Args = args(Arity),
?function(Func, Arity,
Expand All @@ -110,7 +96,6 @@ func_exec(Mod, Func, Arity) ->
?atom(Func),
list(Args)])])]).


func_native(Mod, Func, Arity, Result) ->
Args = args(Arity),
AbsResult = erl_parse:abstract(Result),
Expand All @@ -127,7 +112,6 @@ func_native(Mod, Func, Arity, Result) ->
AbsResult])])]),
AbsResult])]).


contains_opaque(Term) when is_pid(Term); is_port(Term); is_function(Term);
is_reference(Term) ->
true;
Expand All @@ -138,18 +122,14 @@ contains_opaque(Term) when is_tuple(Term) ->
contains_opaque(_Term) ->
false.


args(0) -> [];
args(Arity) -> [?var(var_name(N)) || N <- lists:seq(1, Arity)].


list([]) -> {nil, ?LINE};
list([H|T]) -> {cons, ?LINE, H, list(T)}.


var_name(A) -> list_to_atom("A"++integer_to_list(A)).


%% @hidden
-spec exec(CallerPid::pid(), Mod::atom(), Func::atom(), Args::[any()]) ->
Result::any().
Expand All @@ -172,7 +152,6 @@ exec(Pid, Mod, Func, Args) ->
end
end.


-spec handle_exception(CallerPid::pid(), Mod::atom(), Func::atom(),
Args::[any()], Class:: exit | error | throw,
Reason::any()) ->
Expand All @@ -186,7 +165,6 @@ handle_exception(Pid, Mod, Func, Args, Class, Reason) ->
raise(Pid, Mod, Func, Args, Class, Reason)
end.


-spec raise(CallerPid::pid(), Mod::atom(), Func::atom(), Args::[any()],
Class:: exit | error | throw, Reason::any()) ->
no_return().
Expand All @@ -195,7 +173,6 @@ raise(Pid, Mod, Func, Args, Class, Reason) ->
meck_proc:add_history(Mod, Pid, Func, Args, {Class, Reason, StackTrace}),
erlang:raise(Class, Reason, StackTrace).


-spec inject(Mod::atom(), Func::atom(), Args::[any()],
meck_history:stack_trace()) ->
NewStackTrace::meck_history:stack_trace().
Expand Down
3 changes: 0 additions & 3 deletions src/meck_history.erl
Expand Up @@ -55,7 +55,6 @@
-type opt_pid() :: pid() | '_'.
-type opt_func() :: atom() | '_'.


%%%============================================================================
%%% API
%%%============================================================================
Expand All @@ -68,7 +67,6 @@ get_history(CallerPid, Mod) ->
lists:filter(new_filter(CallerPid, '_', ArgsMatcher),
meck_proc:get_history(Mod)).


-spec num_calls(opt_pid(), Mod::atom(), opt_func(),
meck_args_matcher:opt_args_spec()) ->
non_neg_integer().
Expand All @@ -78,7 +76,6 @@ num_calls(CallerPid, Mod, OptFunc, OptArgsSpec) ->
Filtered = lists:filter(Filter, meck_proc:get_history(Mod)),
length(Filtered).


%%%============================================================================
%%% Internal functions
%%%============================================================================
Expand Down

0 comments on commit d06c9e7

Please sign in to comment.