You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by va...@apache.org on 2017/09/28 21:02:17 UTC

[09/50] meck commit: updated refs/heads/master to 3544aca

Fix #88


Project: http://git-wip-us.apache.org/repos/asf/couchdb-meck/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-meck/commit/77f63dac
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-meck/tree/77f63dac
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-meck/diff/77f63dac

Branch: refs/heads/master
Commit: 77f63dac7b8de1de995beff1cb6605935312e5f5
Parents: f6f885c
Author: yutopp <yu...@gmail.com>
Authored: Mon Jun 27 18:03:14 2016 +0900
Committer: yutopp <yu...@gmail.com>
Committed: Mon Jun 27 20:49:54 2016 +0900

----------------------------------------------------------------------
 src/meck.erl        | 29 +++++++++++++++++++++++------
 src/meck_proc.erl   | 34 ++++++++++++++++++++++++----------
 test/meck_tests.erl | 29 +++++++++++++++++++++++++++++
 3 files changed, 76 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-meck/blob/77f63dac/src/meck.erl
----------------------------------------------------------------------
diff --git a/src/meck.erl b/src/meck.erl
index 2d70315..0ed87ec 100644
--- a/src/meck.erl
+++ b/src/meck.erl
@@ -34,6 +34,7 @@
 -export([sequence/4]).
 -export([loop/4]).
 -export([delete/3]).
+-export([delete/4]).
 -export([exception/2]).
 -export([passthrough/1]).
 -export([history/1]).
@@ -289,18 +290,34 @@ loop(Mod, Func, Ari, Loop) when is_list(Mod) ->
 %%
 %% Deletes the expectation for the function `Func' with the matching
 %% arity `Arity'.
--spec delete(Mods, Func, Ari) -> ok when
+%% `Force' is a flag to delete the function even if it is passthrough.
+-spec delete(Mods, Func, Ari, Force) -> ok when
       Mods :: Mod | [Mod],
       Mod :: atom(),
       Func :: atom(),
-      Ari :: byte().
-delete(Mod, Func, Ari)
+      Ari :: byte(),
+      Force :: boolean().
+delete(Mod, Func, Ari, Force)
   when is_atom(Mod), is_atom(Func), Ari >= 0 ->
-    meck_proc:delete_expect(Mod, Func, Ari);
-delete(Mod, Func, Ari) when is_list(Mod) ->
-    lists:foreach(fun(M) -> delete(M, Func, Ari) end, Mod),
+    meck_proc:delete_expect(Mod, Func, Ari, Force);
+delete(Mod, Func, Ari, Force) when is_list(Mod) ->
+    lists:foreach(fun(M) -> delete(M, Func, Ari, Force) end, Mod),
     ok.
 
+%% @doc Deletes an expectation.
+%%
+%% Deletes the expectation for the function `Func' with the matching
+%% arity `Arity'.
+%% If the mock has passthrough enabled, this function restores the
+%% expectation to the original function. See {@link delete/4}.
+-spec delete(Mods, Func, Ari) -> ok when
+      Mods :: Mod | [Mod],
+      Mod :: atom(),
+      Func :: atom(),
+      Ari :: byte().
+delete(Mod, Func, Ari) ->
+    delete(Mod, Func, Ari, false).
+
 %% @doc Throws an expected exception inside an expect fun.
 %%
 %% This exception will get thrown without invalidating the mocked

http://git-wip-us.apache.org/repos/asf/couchdb-meck/blob/77f63dac/src/meck_proc.erl
----------------------------------------------------------------------
diff --git a/src/meck_proc.erl b/src/meck_proc.erl
index cfb3050..2bd8169 100644
--- a/src/meck_proc.erl
+++ b/src/meck_proc.erl
@@ -22,7 +22,7 @@
 %% API
 -export([start/2]).
 -export([set_expect/2]).
--export([delete_expect/3]).
+-export([delete_expect/4]).
 -export([get_history/1]).
 -export([wait/6]).
 -export([reset/1]).
@@ -61,6 +61,7 @@
                 original :: term(),
                 was_sticky = false :: boolean(),
                 merge_expects = false :: boolean(),
+                passthrough = false :: boolean(),
                 reload :: {Compiler::pid(), {From::pid(), Tag::any()}} |
                           undefined,
                 trackers = [] :: [tracker()]}).
@@ -119,9 +120,9 @@ set_expect(Mod, Expect) ->
             end
     end.
 
--spec delete_expect(Mod::atom(), Func::atom(), Ari::byte()) -> ok.
-delete_expect(Mod, Func, Ari) ->
-    gen_server(call, Mod, {delete_expect, Func, Ari}).
+-spec delete_expect(Mod::atom(), Func::atom(), Ari::byte(), Force::boolean()) -> ok.
+delete_expect(Mod, Func, Ari, Force) ->
+    gen_server(call, Mod, {delete_expect, Func, Ari, Force}).
 
 -spec add_history_exception(
         Mod::atom(), CallerPid::pid(), Func::atom(), Args::[any()],
@@ -201,6 +202,7 @@ init([Mod, Options]) ->
     NoPassCover = proplists:get_bool(no_passthrough_cover, Options),
     MergeExpects = proplists:get_bool(merge_expects, Options),
     EnableOnLoad = proplists:get_bool(enable_on_load, Options),
+    Passthrough = proplists:get_bool(passthrough, Options),
     Original = backup_original(Mod, NoPassCover, EnableOnLoad),
     NoHistory = proplists:get_bool(no_history, Options),
     History = if NoHistory -> undefined; true -> [] end,
@@ -216,6 +218,7 @@ init([Mod, Options]) ->
                     original = Original,
                     was_sticky = WasSticky,
                     merge_expects = MergeExpects,
+                    passthrough = Passthrough,
                     history = History}}
     catch
         exit:{error_loading_module, Mod, sticky_directory} ->
@@ -239,10 +242,13 @@ handle_call({set_expect, Expect}, From,
         {error, Reason} ->
             {reply, {error, Reason}, S}
     end;
-handle_call({delete_expect, Func, Ari}, From,
-            S = #state{mod = Mod, expects = Expects}) ->
+handle_call({delete_expect, Func, Ari, Force}, From,
+            S = #state{mod = Mod, expects = Expects,
+                       passthrough = PassThrough}) ->
     check_if_being_reloaded(S),
-    {NewExpects, CompilerPid} = do_delete_expect(Mod, {Func, Ari}, Expects),
+    ErasePassThrough = Force orelse (not PassThrough),
+    {NewExpects, CompilerPid} =
+        do_delete_expect(Mod, {Func, Ari}, Expects, ErasePassThrough),
     {noreply, S#state{expects = NewExpects,
                       reload = {CompilerPid, From}}};
 handle_call(get_history, _From, S = #state{history = undefined}) ->
@@ -503,10 +509,18 @@ store_expect(Mod, FuncAri, Expect, Expects, false) ->
     NewExpects =  dict:store(FuncAri, Expect, Expects),
     compile_expects(Mod, NewExpects).
 
--spec do_delete_expect(Mod::atom(), meck_expect:func_ari(), Expects::meck_dict()) ->
+-spec do_delete_expect(Mod::atom(), meck_expect:func_ari(),
+                       Expects::meck_dict(), ErasePassThrough::boolean()) ->
         {NewExpects::meck_dict(), CompilerPid::pid()}.
-do_delete_expect(Mod, FuncAri, Expects) ->
-    NewExpects = dict:erase(FuncAri, Expects),
+do_delete_expect(Mod, FuncAri, Expects, ErasePassThrough) ->
+    NewExpects = case ErasePassThrough of
+                     true  ->
+                         dict:erase(FuncAri, Expects);
+                     false ->
+                         dict:store(FuncAri,
+                                    meck_expect:new_passthrough(FuncAri),
+                                    Expects)
+                 end,
     compile_expects(Mod, NewExpects).
 
 -spec compile_expects(Mod::atom(), Expects::meck_dict()) ->

http://git-wip-us.apache.org/repos/asf/couchdb-meck/blob/77f63dac/test/meck_tests.erl
----------------------------------------------------------------------
diff --git a/test/meck_tests.erl b/test/meck_tests.erl
index bbf2d75..3ea4de2 100644
--- a/test/meck_tests.erl
+++ b/test/meck_tests.erl
@@ -1488,6 +1488,35 @@ wait_purge_expired_tracker_test() ->
     %% Clean
     meck:unload().
 
+
+meck_passthrough_test_() ->
+    {foreach, fun setup_passthrough/0, fun teardown/1,
+     [{with, [T]} || T <- [
+                           fun ?MODULE:delete_passthrough_/1,
+                           fun ?MODULE:delete_passthrough_force_/1
+                          ]]}.
+
+setup_passthrough() ->
+    % Uncomment to run tests with dbg:
+    % dbg:tracer(),
+    % dbg:p(all, call),
+    % dbg:tpl(meck, []),
+    ok = meck:new(meck_test_module, [passthrough, non_strict]),
+    meck_test_module.
+
+delete_passthrough_(Mod) ->
+    ok = meck:expect(Mod, c, 2, {c, d}),
+    ?assertMatch({c, d}, Mod:c(a, b)),
+    ?assertEqual(ok, meck:delete(Mod, c, 2)),
+    ?assertMatch({a, b}, Mod:c(a, b)),
+    ?assert(meck:validate(Mod)).
+
+delete_passthrough_force_(Mod) ->
+    ok = meck:expect(Mod, c, 2, ok),
+    ?assertEqual(ok, meck:delete(Mod, c, 2, true)),
+    ?assertError(undef, Mod:test(a, b)),
+    ?assert(meck:validate(Mod)).
+
 %%=============================================================================
 %% Internal Functions
 %%=============================================================================