summaryrefslogtreecommitdiff
path: root/test/mock_genserver.erl
blob: cde41ff52b2f5b940175a38bdebf195999b731c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
%%%-------------------------------------------------------------------
%%% File:      mock_genserver.erl
%%% @author    Cliff Moon <> []
%%% @copyright 2009 Cliff Moon
%%% @doc
%%%
%%% @end
%%%
%%% @since 2009-01-02 by Cliff Moon
%%%-------------------------------------------------------------------
-module(mock_genserver).
-author('cliff@powerset.com').

-behaviour(gen_server).

-include_lib("eunit/include/eunit.hrl").

%% API
-export([start_link/1, stub_call/3, expects_call/3, expects_call/4, stop/1]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
         terminate/2, code_change/3]).

-record(state, {call_stubs=[], call_expects=[], cast_expectations, info_expectations}).

%%====================================================================
%% API
%%====================================================================
%%--------------------------------------------------------------------
%% @spec start_link(Reference::atom()) -> {ok,Pid} | ignore | {error,Error}
%% @doc Starts the server
%% @end
%%--------------------------------------------------------------------
start_link(Reference) ->
  gen_server:start_link(Reference, ?MODULE, [], []).

stub_call(Server, Sym, Fun) when is_function(Fun) ->
  gen_server:call(Server, {mock_stub_call, Sym, Fun}).

expects_call(Server, Args, Fun) when is_function(Fun) ->
  gen_server:call(Server, {mock_expects_call, Args, Fun}).

expects_call(Server, Args, Fun, Times) when is_function(Fun) ->
  gen_server:call(Server, {mock_expects_call, Args, Fun, Times}).

stop(Server) ->
  gen_server:call(Server, mock_stop).

%%====================================================================
%% gen_server callbacks
%%====================================================================

%%--------------------------------------------------------------------
%% @spec init(Args) -> {ok, State} |
%%                         {ok, State, Timeout} |
%%                         ignore               |
%%                         {stop, Reason}
%% @doc Initiates the server
%% @end
%%--------------------------------------------------------------------
init([]) ->
    {ok, #state{}}.

%%--------------------------------------------------------------------
%% @spec
%% handle_call(Request, From, State) -> {reply, Reply, State} |
%%                                      {reply, Reply, State, Timeout} |
%%                                      {noreply, State} |
%%                                      {noreply, State, Timeout} |
%%                                      {stop, Reason, Reply, State} |
%%                                      {stop, Reason, State}
%% @doc Handling call messages
%% @end
%%--------------------------------------------------------------------
handle_call({mock_stub_call, Sym, Fun}, _From, State = #state{call_stubs=Stubs}) ->
  {reply, ok, State#state{call_stubs=[{Sym, Fun}|Stubs]}};

handle_call({mock_expects_call, Args, Fun}, _From, State = #state{call_expects=Expects}) ->
  {reply, ok, State#state{call_expects=add_expectation(Args, Fun, at_least_once, Expects)}};

handle_call({mock_expects_call, Args, Fun, Times}, _From, State = #state{call_expects=Expects}) ->
  {reply, ok, State#state{call_expects=add_expectation(Args, Fun, Times, Expects)}};

handle_call(mock_stop, _From, State) ->
  {stop, normal, ok, State};

handle_call(Request, _From, State = #state{call_stubs=Stubs,call_expects=Expects}) ->
  % expectations have a higher priority
  case find_expectation(Request, Expects) of
    {found, {_, Fun, Time}, NewExpects} -> {reply, Fun(Request, Time), State#state{call_expects=NewExpects}};
    not_found -> % look for a stub
      case find_stub(Request, Stubs) of
        {found, {_, Fun}} -> {reply, Fun(Request), State};
        not_found ->
          {stop, {unexpected_call, Request}, State}
      end
  end.

%%--------------------------------------------------------------------
%% @spec handle_cast(Msg, State) -> {noreply, State} |
%%                                      {noreply, State, Timeout} |
%%                                      {stop, Reason, State}
%% @doc Handling cast messages
%% @end
%%--------------------------------------------------------------------
handle_cast(_Msg, State) ->
    {noreply, State}.

%%--------------------------------------------------------------------
%% @spec handle_info(Info, State) -> {noreply, State} |
%%                                       {noreply, State, Timeout} |
%%                                       {stop, Reason, State}
%% @doc Handling all non call/cast messages
%% @end
%%--------------------------------------------------------------------
handle_info(_Info, State) ->
    {noreply, State}.

%%--------------------------------------------------------------------
%% @spec terminate(Reason, State) -> void()
%% @doc This function is called by a gen_server when it is about to
%% terminate. It should be the opposite of Module:init/1 and do any necessary
%% cleaning up. When it returns, the gen_server terminates with Reason.
%% The return value is ignored.
%% @end
%%--------------------------------------------------------------------
terminate(_Reason, _State) ->
    ok.

%%--------------------------------------------------------------------
%% @spec code_change(OldVsn, State, Extra) -> {ok, NewState}
%% @doc Convert process state when code is changed
%% @end
%%--------------------------------------------------------------------
code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

%%--------------------------------------------------------------------
%%% Internal functions
%%--------------------------------------------------------------------


add_expectation(Args, Fun, Times, Expects) ->
  Expects ++ [{Args, Fun, Times}].

find_expectation(Request, Expects) ->
  find_expectation(Request, Expects, []).

find_expectation(_Request, [], _Rest) ->
  not_found;

find_expectation(Request, [{Args, Fun, Times}|Expects], Rest) ->
  MatchFun = generate_match_fun(Args),
  case MatchFun(Request) of
    true ->
      if
        Times == at_least_once -> {found, {Args, Fun, Times}, lists:reverse(Rest) ++ [{Args, Fun, Times}] ++ Expects};
        Times == 1 -> {found, {Args, Fun, Times}, lists:reverse(Rest) ++ Expects};
        true -> {found, {Args, Fun, Times}, lists:reverse(Rest) ++ [{Args, Fun, Times-1}] ++ Expects}
      end;
    false -> find_expectation(Request, Expects, [{Args, Fun, Times}|Rest])
  end.

find_stub(Request, Stub) when is_tuple(Request) ->
  Sym = element(1, Request),
  find_stub(Sym, Stub);

find_stub(_Sym, []) ->
  not_found;

find_stub(Sym, _Stubs) when not is_atom(Sym) ->
  not_found;

find_stub(Sym, [{Sym, Fun}|_Stubs]) ->
  {found, {Sym, Fun}};

find_stub(Sym, [_Stub|Stubs]) ->
  find_stub(Sym, Stubs).

generate_match_fun(Args) when is_tuple(Args) ->
  generate_match_fun(tuple_to_list(Args));

generate_match_fun(Args) when not is_list(Args) ->
  generate_match_fun([Args]);

generate_match_fun(Args) when is_list(Args) ->
  Src = generate_match_fun("fun({", Args),
  {ok, Tokens, _} = erl_scan:string(Src),
  {ok, [Form]} = erl_parse:parse_exprs(Tokens),
  {value, Fun, _} = erl_eval:expr(Form, erl_eval:new_bindings()),
  Fun.

generate_match_fun(Src, []) ->
  Src ++ "}) -> true; (_) -> false end.";

% unbound atom means you don't care about an arg
generate_match_fun(Src, [unbound|Args]) ->
  if
    length(Args) > 0 -> generate_match_fun(Src ++ "_,", Args);
    true -> generate_match_fun(Src ++ "_", Args)
  end;

generate_match_fun(Src, [Bound|Args]) ->
  Term = lists:flatten(io_lib:format("~w", [Bound])),
  if
    length(Args) > 0 -> generate_match_fun(Src ++ Term ++ ",", Args);
    true -> generate_match_fun(Src ++ Term, Args)
  end.