Skip to content

Commit

Permalink
Merge pull request #77 from norton/norton-develop
Browse files Browse the repository at this point in the history
Two new options for meck
  • Loading branch information
eproxus committed Oct 7, 2012
2 parents c020b25 + 2465860 commit 2d8a995
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/meck.erl
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
-record(state, {mod :: atom(),
expects :: dict(),
valid = true :: boolean(),
history = [] :: history(),
history = [] :: history() | undefined,
original :: term(),
was_sticky :: boolean()}).

Expand Down Expand Up @@ -142,6 +142,12 @@ new(Mod) when is_list(Mod) -> lists:foreach(fun new/1, Mod), ok.
%% This option allows you to disable that
%% feature if it causes problems.
%% </dd>
%% <dt>`{spawn_opt, list()}'</dt><dd>Specify Erlang process spawn options.
%% Typically used to specify non-default,
%% garbage collection options.
%% </dd>
%% <dt>`no_history'</dt> <dd>Do not store history of meck calls.
%% </dd>
%% </dl>
-spec new(Mod:: atom() | [atom()], Options::[term()]) -> ok.
new(Mod, Options) when is_atom(Mod), is_list(Options) ->
Expand Down Expand Up @@ -467,12 +473,14 @@ init([Mod, Options]) ->
end,
NoPassCover = proplists:get_bool(no_passthrough_cover, Options),
Original = backup_original(Mod, NoPassCover),
NoHistory = proplists:get_bool(no_history, Options),
History = if NoHistory -> undefined; true -> [] end,
process_flag(trap_exit, true),
Expects = init_expects(Mod, Options),
try
_Bin = meck_mod:compile_and_load_forms(to_forms(Mod, Expects)),
{ok, #state{mod = Mod, expects = Expects, original = Original,
was_sticky = WasSticky}}
was_sticky = WasSticky, history=History}}
catch
exit:{error_loading_module, Mod, sticky_directory} ->
{stop, module_is_sticky}
Expand All @@ -491,6 +499,8 @@ handle_call({expect, FuncAri, Clauses}, _From, S) ->
handle_call({delete, Func, Arity}, _From, S) ->
NewExpects = delete_expect(S#state.mod, {Func, Arity}, S#state.expects),
{reply, ok, S#state{expects = NewExpects}};
handle_call(history, _From, #state{history=undefined}=S) ->
{reply, [], S};
handle_call(history, _From, S) ->
{reply, lists:reverse(S#state.history), S};
handle_call(reset, _From, S) ->
Expand All @@ -503,6 +513,8 @@ handle_call(stop, _From, S) ->
{stop, normal, ok, S}.

%% @hidden
handle_cast({add_history, _Item}, #state{history=undefined}=S) ->
{noreply, S};
handle_cast({add_history, Item}, S) ->
{noreply, S#state{history = [Item| S#state.history]}};
handle_cast(_Msg, S) ->
Expand Down Expand Up @@ -555,7 +567,8 @@ start(Mod, Options) ->
end.

start(Func, Mod, Options) ->
gen_server:Func({local, proc_name(Mod)}, ?MODULE, [Mod, Options], []).
SpawnOpt = proplists:get_value(spawn_opt, Options, []),
gen_server:Func({local, proc_name(Mod)}, ?MODULE, [Mod, Options], [{spawn_opt, SpawnOpt}]).

cast(Mod, Msg) -> gen_server(cast, Mod, Msg).
call(Mod, Msg) -> gen_server(call, Mod, Msg).
Expand Down

0 comments on commit 2d8a995

Please sign in to comment.