-
-
Notifications
You must be signed in to change notification settings - Fork 231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add `meck:result/4-5' that returns the result value of a particular function #163
Conversation
Hi! Thanks for a very clean pull request! What do you imagine the use case of this functionality to be? If you have had a need for this is production, would you mind detailing how you've used it? The reason I'm asking is I'd like to avoid polluting the top level Meck API space if possible. 😄 (I think the work makes a lot of sense anyway, so one possibility is to add it to the |
I'm sorry for my late and thank you for your feedback! Usecase1. testcase for functions related to stateOne of my usecases of Our production code has a process monitoring server like the following: -module(meck_result_usecase_server).
-behaviour(gen_server).
%% exported apis
-export([start_link/0, monitor/1]).
%% gen_server callbacks
-export([init/1, handle_cast/2, handle_call/3, handle_info/2, terminate/2, code_change/3]).
-record(state, {monitoring_pids = maps:new() :: maps:map(pid(), reference()), ...(complex record)}).
%% -------------
%% Exported APIs
%% -------------
-spec start_link() ->
{ok, pid()}.
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
-spec monitor(pid()) -> ok.
monitor(Pid) ->
gen_server:cast(?MODULE, {monitor, Pid}).
%% --------------------
%% gen_server callbacks
%% --------------------
init([]) ->
{ok, #state{}}.
handle_cast({monitor, Pid}, State = #state{monitoring_pids = Map}) ->
Ref = erlang:monitor(process, Pid),
{noreply, State#state{monitoring_pids = Map#{Pid => Ref}}};
handle_cast(_Req, State) ->
{noreply, State}.
handle_call(_Req, _From, State) ->
{noreply, State}.
handle_info({'DOWN', Ref, process, Pid, _Reason}, State = #state{monitoring_pids = Map}) ->
case maps:find(Pid, Map) of
{ok, Ref} ->
{noreply, State#state{monitoring_pids = maps:remove(Pid, Map)}};
_ ->
{stop, not_monitored, State}
end;
handle_info(_Info, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}. and eunit: -module(meck_result_usecase_server_tests).
-include_lib("eunit/include/eunit.hrl").
%% eunit
meck_result_usecase_server_test_() ->
{setup,
fun() ->
meck:new(meck_result_usecase_server, [unlink, passthrough]),
{ok, Pid} = meck_result_usecase_server:start_link(),
Pid
end,
fun(Pid) ->
process_flag(trap_exit, true),
exit(Pid, shutdown),
meck:unload(),
ok
end,
fun(_) ->
fun() ->
Pid = spawn(timer, sleep, [infinity]),
ok = meck_result_usecase_server:monitor(Pid),
meck:wait(meck_result_usecase_server, handle_cast, 2, 3000),
exit(Pid, kill),
meck:wait(meck_result_usecase_server, handle_info, 2, 3000),
{Type, _} = meck:result(last, meck_result_usecase_server, handle_info, 2),
?assertEqual(noreply, Type) %% check that `Type' is `noreply', not `stop'
end
end}. I want to check that So I want to use a function like 2. for debuggingTo debug test cases, I often want to confirm the result of a particular function using It is OK to achieve this purpose by toplevel or
|
Sorry for the long silence 🙇 If you move the functions into the history module, I'd be happy accepting the patch. For now, I don't want to add more functions to the main Meck API. |
OK! I have removed the functions from Is it OK? |
Yes, that looks good. One final request: Can you squash everything into one commit detailing only the change to I can also do it while merging, but I'm not sure you'll retain your authorship that way 🙂 |
7b4f48b
to
71cf6db
Compare
`meck_history:result/5' returns the result value of a particular function
71cf6db
to
daf201e
Compare
I've squashed! |
Great work, thanks again! |
Thank you! |
Hi! Thank you for the nice library.
I would like a function that returns the result value of a specified function, like follows.
I implemented
meck:result(Occur, Mod, Func, OptArgsSpec)
andmeck:result(Occur, Mod, Func, OptArgsSpec, OptCallerPid)
. What do you think about this?