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:20 UTC

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

Add `meck_history:result/5'

`meck_history:result/5' returns the result value of a particular function


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

Branch: refs/heads/master
Commit: e5bdc72728ffc7101e725ebf777c1c72d3ebec76
Parents: c772db1
Author: amutake <am...@gmail.com>
Authored: Wed Jun 29 18:13:57 2016 +0900
Committer: Adam Lindberg <he...@alind.io>
Committed: Mon Dec 19 11:31:41 2016 +0100

----------------------------------------------------------------------
 src/meck_history.erl        | 16 ++++++++
 test/meck_history_tests.erl | 86 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 102 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-meck/blob/e5bdc727/src/meck_history.erl
----------------------------------------------------------------------
diff --git a/src/meck_history.erl b/src/meck_history.erl
index e8c22d2..c73db00 100644
--- a/src/meck_history.erl
+++ b/src/meck_history.erl
@@ -30,6 +30,7 @@
 -export([get_history/2]).
 -export([num_calls/4]).
 -export([capture/6]).
+-export([result/5]).
 -export([new_filter/3]).
 
 %%%============================================================================
@@ -95,6 +96,21 @@ capture(Occur, OptCallerPid, Mod, Func, OptArgsSpec, ArgNum) ->
             lists:nth(ArgNum, Args)
     end.
 
+-spec result(Occur::pos_integer(), opt_pid(), Mod::atom(), Func::atom(),
+             meck_args_matcher:opt_args_spec()) -> ResultValue::any().
+result(Occur, OptCallerPid, Mod, Func, OptArgsSpec) ->
+    ArgsMatcher = meck_args_matcher:new(OptArgsSpec),
+    Filter = new_filter(OptCallerPid, Func, ArgsMatcher),
+    Filtered = lists:filter(Filter, meck_proc:get_history(Mod)),
+    case nth_record(Occur, Filtered) of
+        not_found ->
+            erlang:error(not_found);
+        {_CallerPid, _MFA, Result} ->
+            Result;
+        {_CallerPid, _MFA, Class, Reason, Trace} ->
+            erlang:raise(Class, Reason, Trace)
+    end.
+
 -spec new_filter(opt_pid(), opt_func(), meck_args_matcher:args_matcher()) ->
         fun((history_record()) -> boolean()).
 new_filter(TheCallerPid, TheFunc, ArgsMatcher) ->

http://git-wip-us.apache.org/repos/asf/couchdb-meck/blob/e5bdc727/test/meck_history_tests.erl
----------------------------------------------------------------------
diff --git a/test/meck_history_tests.erl b/test/meck_history_tests.erl
index 104a642..7da0419 100644
--- a/test/meck_history_tests.erl
+++ b/test/meck_history_tests.erl
@@ -81,3 +81,89 @@ capture_different_args_specs_test() ->
     ?assertMatch(2008, meck:capture(first, test, foo, ['_', '_', meck:is(hamcrest_matchers:greater_than(3006))], 2)),
     %% Clean
     meck:unload().
+
+result_different_positions_test() ->
+    %% Given
+    meck:new(test, [non_strict]),
+    meck:expect(test, foo, fun(_, A, _) -> A end),
+    meck:expect(test, foo, 4, ok),
+    meck:expect(test, bar, 3, ok),
+    %% When
+    test:foo(1001, 2001, 3001, 4001),
+    test:bar(1002, 2002, 3002),
+    test:foo(1003, 2003, 3003),
+    test:bar(1004, 2004, 3004),
+    test:foo(1005, 2005, 3005),
+    test:foo(1006, 2006, 3006),
+    test:bar(1007, 2007, 3007),
+    test:foo(1008, 2008, 3008),
+    %% Then
+    ?assertMatch(2003, meck_history:result(first, '_', test, foo, ['_', '_', '_'])),
+    ?assertMatch(2008, meck_history:result(last, '_', test, foo, ['_', '_', '_'])),
+    ?assertMatch(2006, meck_history:result(3, '_', test, foo, ['_', '_', '_'])),
+    ?assertError(not_found, meck_history:result(5, '_', test, foo, ['_', '_', '_'])),
+    %% Clean
+    meck:unload().
+
+result_different_args_specs_test() ->
+    %% Given
+    meck:new(test, [non_strict]),
+    meck:expect(test, foo, fun(_, A) -> A end),
+    meck:expect(test, foo, fun(_, A, _) -> A end),
+    meck:expect(test, foo, fun(_, A, _, _) -> A end),
+    meck:expect(test, bar, 3, ok),
+    %% When
+    test:foo(1001, 2001, 3001, 4001),
+    test:bar(1002, 2002, 3002),
+    test:foo(1003, 2003, 3003),
+    test:bar(1004, 2004, 3004),
+    test:foo(1005, 2005),
+    test:foo(1006, 2006, 3006),
+    test:bar(1007, 2007, 3007),
+    test:foo(1008, 2008, 3008),
+    %% Then
+    ?assertMatch(2001, meck_history:result(first, '_', test, foo, '_')),
+    ?assertMatch(2003, meck_history:result(first, '_', test, foo, 3)),
+    ?assertMatch(2005, meck_history:result(first, '_', test, foo, ['_', '_'])),
+    ?assertMatch(2006, meck_history:result(first, '_', test, foo, [1006, '_', '_'])),
+    Matcher = meck:is(hamcrest_matchers:greater_than(3006)),
+    ?assertMatch(2008, meck_history:result(first, '_', test, foo, ['_', '_', Matcher])),
+    %% Clean
+    meck:unload().
+
+result_exception_test() ->
+    %% Given
+    meck:new(test, [non_strict]),
+    meck:expect(test, error, fun(R) -> erlang:error(R) end),
+    meck:expect(test, throw, fun(R) -> throw(R) end),
+    meck:expect(test, exit, fun(R) -> exit(R) end),
+    %% When
+    catch test:error(foo),
+    catch test:throw(bar),
+    catch test:exit(baz),
+    %% Then
+    ?assertException(error, foo, meck_history:result(first, '_', test, error, 1)),
+    ?assertException(throw, bar, meck_history:result(first, '_', test, throw, 1)),
+    ?assertException(exit, baz, meck_history:result(first, '_', test, exit, 1)),
+    ?assertError(not_found, meck_history:result(first, '_', test, foo, 0)),
+    %% Clean
+    meck:unload().
+
+result_different_caller_test() ->
+    %% Given
+    meck:new(test, [non_strict]),
+    meck:expect(test, foo, fun(_, A, _) -> A end),
+    %% When
+    test:foo(1001, 2001, 3001),
+    Self = self(),
+    Pid = spawn(fun() ->
+                        test:foo(1002, 2002, 3002),
+                        Self ! {self(), done}
+                end),
+    test:foo(1003, 2003, 3003),
+    receive {Pid, done} -> ok end,
+    %% Then
+    ?assertMatch(2003, meck_history:result(2, self(), test, foo, 3)),
+    ?assertMatch(2002, meck_history:result(last, Pid, test, foo, 3)),
+    %% Clean
+    meck:unload().