Skip to content

Commit

Permalink
Add `meck_history:result/5'
Browse files Browse the repository at this point in the history
`meck_history:result/5' returns the result value of a particular function
  • Loading branch information
amutake committed Dec 19, 2016
1 parent eaa0359 commit 71cf6db
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/meck_history.erl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
-export([get_history/2]).
-export([num_calls/4]).
-export([capture/6]).
-export([result/5]).
-export([new_filter/3]).

%%%============================================================================
Expand Down Expand Up @@ -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) ->
Expand Down
86 changes: 86 additions & 0 deletions test/meck_history_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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().

0 comments on commit 71cf6db

Please sign in to comment.