-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathhoax_expect.erl
58 lines (52 loc) · 2.34 KB
/
hoax_expect.erl
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
-module(hoax_expect).
-export([
assert_exported/2,
parse/2
]).
-include("hoax_int.hrl").
assert_exported([Expect = #expectation{key={_,F,A}} | Rest], Exports) ->
lists:member({F, A}, Exports) orelse
error({no_such_function_to_mock, {F, A}}),
hoax_tab:insert(Expect),
assert_exported(Rest, Exports);
assert_exported([], _) ->
ok.
parse(Mod, expect_no_interactions) ->
hoax_tab:insert(#expectation{key={Mod,undefined,undefined}, expected_count=0}),
[];
parse(Mod,[]) ->
error({no_expectations_for_mock, Mod});
parse(Mod,Expects) ->
[expectation(Mod, Ex) || Ex <- Expects].
expectation(Mod, {FunctionName, Lambda}) when is_function(Lambda) ->
expectation(Mod, FunctionName, hoax_fun:create_wildcard_for_args(Lambda), Lambda, undefined);
expectation(Mod, {Function, Args}) when is_atom(Mod), is_list(Args) ->
expectation(Mod, Function, Args, default, undefined);
expectation(Mod, {Function, Args, Count}) when is_atom(Mod),
is_list(Args),
is_integer(Count) ->
expectation(Mod, Function, Args, default, Count);
expectation(Mod, {Function, Args, {X,Y}}) when is_atom(Mod),
is_list(Args),
X == return;
X == error;
X == exit;
X == throw ->
expectation(Mod, Function, Args, {X,Y}, undefined);
expectation(Mod, {Function, Args, {X,Y}, Count}) when is_atom(Mod),
is_list(Args),
is_integer(Count),
X == return;
X == error;
X == exit;
X == throw ->
expectation(Mod, Function, Args, {X,Y}, Count);
expectation(_, Other) ->
error({bad_expectation_syntax, Other}).
expectation(Mod, Function, Args, Action, Count) ->
#expectation{
key = {Mod, Function, length(Args)},
expected_args = Args,
action = Action,
expected_count = Count
}.