Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add ?WITH_MOCKED_SETUP and start using eunit_addons for tests

  • Loading branch information...
commit ca8d09ddc6c2c0d570595a97aa2bac3616726828 1 parent cc8df75
@klajo authored
Showing with 65 additions and 69 deletions.
  1. +27 −0 include/mockgyver.hrl
  2. +4 −2 rebar.config
  3. +34 −67 test/mockgyver_tests.erl
View
27 include/mockgyver.hrl
@@ -1,8 +1,35 @@
-ifndef(MOCKGYVER_HRL).
-define(MOCKGYVER_HRL, true).
+-include_lib("eunit_addons/include/eunit_addons.hrl").
+
-compile({parse_transform, mockgyver_xform}).
+%% run tests with a mock
+-define(WITH_MOCKED_SETUP(SetupFun, CleanupFun, ForAllTimeout, PerTcTimeout,
+ Tests),
+ ?WITH_FUN(fun(__MockTest) ->
+ ?MOCK(fun() ->
+ Env = SetupFun(),
+ try
+ apply(?MODULE, __MockTest, [Env])
+ after
+ CleanupFun(Env)
+ end
+ end)
+ end,
+ ForAllTimeout,
+ PerTcTimeout,
+ Tests)).
+
+-define(WITH_MOCKED_SETUP(SetupFun, CleanupFun, ForAllTimeout, PerTcTimeout),
+ ?WITH_MOCKED_SETUP(SetupFun, CleanupFun, ForAllTimeout, PerTcTimeout,
+ eunit_addons:get_tests_with_setup(?MODULE))).
+
+-define(WITH_MOCKED_SETUP(SetupFun, CleanupFun),
+ ?WITH_MOCKED_SETUP(SetupFun, CleanupFun,
+ ?FOR_ALL_TIMEOUT, ?PER_TC_TIMEOUT)).
+
-define(WRAP(Type, Expr),
{'$mock', Type, Expr}).
View
6 rebar.config
@@ -1,6 +1,8 @@
%% Tell emacs to use -*- erlang -*- mode for this file
{lib_dirs,["deps"]}.
-{deps,[{parse_trans, ".*", {git, "git://github.com/esl/parse_trans.git", ""}}
- ]}.
+{deps,
+ [{parse_trans, ".*", {git, "git://github.com/esl/parse_trans.git", ""}},
+ {eunit_addons, ".*", {git, "git://github.com/klajo/eunit_addons.git", ""}}
+ ]}.
%%{edoc_opts, [{doclet, edown_doclet}]}.
View
101 test/mockgyver_tests.erl
@@ -8,48 +8,15 @@
-record('DOWN', {mref, type, obj, info}).
-%% was_called_test() ->
-%% mockgyver:start_link(),
-%% mockgyver:start_session([{x, test, 1}]),
-%% x:test(1),
-%% x:test(2),
-%% proc_lib:spawn(fun() -> x:test(2) end),
-%% % timer:sleep(100),
-%% ok = mockgyver:was_called({x, test, dbg:fun2ms(fun([1]) -> ok end)}, once),
-%% ok = mockgyver:was_called({x, test, dbg:fun2ms(fun([2]) -> ok end)}, {times, 2}),
-%% ok = mockgyver:was_called({x, test, dbg:fun2ms(fun([2]) -> ok end)}, {at_least, 2}),
-%% ok = mockgyver:was_called({x, test, dbg:fun2ms(fun([3]) -> ok end)}, never).
-
mock_test_() ->
code:add_patha(filename:dirname(code:which(?MODULE))),
- Ts = [fun traces_single_arg/0,
- fun traces_multi_args/0,
- fun traces_in_separate_process/0,
- fun traces_in_separate_process/0,
- fun was_called_defaults_to_once/0,
- fun matches_called_arguments/0,
- fun allows_was_called_guards/0,
- fun allows_was_called_guards_with_variables_not_used_in_args_list/0,
- fun returns_called_arguments/0,
- fun allows_variables_in_criteria/0,
- fun returns_error_on_invalid_criteria/0,
- fun checks_that_two_different_functions_are_called/0,
- fun knows_the_difference_in_arities/0,
- fun can_verify_both_mocked_and_non_mocked_modules/0,
- fun returns_immediately_if_waiters_criteria_already_fulfilled/0,
- fun waits_until_waiters_criteria_fulfilled/0,
- fun returns_other_value/0,
- fun can_change_return_value/0,
- fun inherits_variables_from_outer_scope/0,
- fun can_use_params/0,
- fun can_use_multi_clause_functions/0,
- fun fails_gracefully_when_mocking_a_bif/0,
- fun can_call_original_module/0,
- fun can_make_new_module/0,
- fun can_make_new_function_in_existing_module/0,
- fun counts_calls/0,
- fun returns_calls/0],
- [{spawn, fun() -> ?MOCK(T) end} || T <- Ts].
+ ?WITH_MOCKED_SETUP(fun setup/0, fun cleanup/1).
+
+setup() ->
+ ok.
+
+cleanup(_) ->
+ ok.
only_allows_one_mock_at_a_time_test() ->
NumMockers = 10,
@@ -88,7 +55,7 @@ check_no_simultaneous_mockers_outside([]) ->
check_no_simultaneous_mockers_inside([{mock_end, Pid} | Msgs], Pid) ->
check_no_simultaneous_mockers_outside(Msgs).
-traces_single_arg() ->
+traces_single_arg_test(_) ->
1 = mockgyver_dummy:return_arg(1),
2 = mockgyver_dummy:return_arg(2),
2 = mockgyver_dummy:return_arg(2),
@@ -96,7 +63,7 @@ traces_single_arg() ->
?WAS_CALLED(mockgyver_dummy:return_arg(2), {times, 2}),
?WAS_CALLED(mockgyver_dummy:return_arg(_), {times, 3}).
-traces_multi_args() ->
+traces_multi_args_test(_) ->
{a, 1} = mockgyver_dummy:return_arg(a, 1),
{a, 2} = mockgyver_dummy:return_arg(a, 2),
{b, 2} = mockgyver_dummy:return_arg(b, 2),
@@ -106,19 +73,19 @@ traces_multi_args() ->
?WAS_CALLED(mockgyver_dummy:return_arg(_, 1), {times, 1}),
?WAS_CALLED(mockgyver_dummy:return_arg(_, 2), {times, 2}).
-traces_in_separate_process() ->
+traces_in_separate_process_test(_) ->
Pid = proc_lib:spawn_link(fun() -> mockgyver_dummy:return_arg(1) end),
MRef = erlang:monitor(process, Pid),
receive {'DOWN',MRef,_,_,_} -> ok end,
?WAS_CALLED(mockgyver_dummy:return_arg(_), once).
-was_called_defaults_to_once() ->
+was_called_defaults_to_once_test(_) ->
mockgyver_dummy:return_arg(1),
?WAS_CALLED(mockgyver_dummy:return_arg(_)),
mockgyver_dummy:return_arg(1),
?assertError(_, ?WAS_CALLED(mockgyver_dummy:return_arg(_))).
-matches_called_arguments() ->
+matches_called_arguments_test(_) ->
N = 42,
O = 1,
mockgyver_dummy:return_arg(1),
@@ -128,26 +95,26 @@ matches_called_arguments() ->
?WAS_CALLED(mockgyver_dummy:return_arg({_,N}), never),
?WAS_CALLED(mockgyver_dummy:return_arg({N,O}), once).
-allows_was_called_guards() ->
+allows_was_called_guards_test(_) ->
mockgyver_dummy:return_arg(1),
?WAS_CALLED(mockgyver_dummy:return_arg(N) when N == 1, once),
?WAS_CALLED(mockgyver_dummy:return_arg(N) when N == 2, never).
-allows_was_called_guards_with_variables_not_used_in_args_list() ->
+allows_was_called_guards_with_variables_not_used_in_args_list_test(_) ->
W = 2,
mockgyver_dummy:return_arg(1),
?WAS_CALLED(mockgyver_dummy:return_arg(_) when W == 2, once).
-returns_called_arguments() ->
+returns_called_arguments_test(_) ->
mockgyver_dummy:return_arg(1),
mockgyver_dummy:return_arg(2),
[[1], [2]] = ?WAS_CALLED(mockgyver_dummy:return_arg(N), {times, 2}).
-allows_variables_in_criteria() ->
+allows_variables_in_criteria_test(_) ->
C = {times, 0},
?WAS_CALLED(mockgyver_dummy:return_arg(_), C).
-returns_error_on_invalid_criteria() ->
+returns_error_on_invalid_criteria_test(_) ->
lists:foreach(
fun(C) ->
?assertError({invalid_criteria, C},
@@ -159,25 +126,25 @@ returns_error_on_invalid_criteria() ->
{at_most, x},
{times, x}]).
-checks_that_two_different_functions_are_called() ->
+checks_that_two_different_functions_are_called_test(_) ->
mockgyver_dummy:return_arg(1),
mockgyver_dummy:return_arg2(1),
?WAS_CALLED(mockgyver_dummy:return_arg(1), once),
?WAS_CALLED(mockgyver_dummy:return_arg2(1), once).
-knows_the_difference_in_arities() ->
+knows_the_difference_in_arities_test(_) ->
mockgyver_dummy:return_arg(1),
?WAS_CALLED(mockgyver_dummy:return_arg(_), once),
?WAS_CALLED(mockgyver_dummy:return_arg(_, _), never).
-can_verify_both_mocked_and_non_mocked_modules() ->
+can_verify_both_mocked_and_non_mocked_modules_test(_) ->
?WHEN(mockgyver_dummy:return_arg(_) -> [3, 2, 1]),
[3, 2, 1] = lists:reverse([1, 2, 3]),
[3, 2, 1] = mockgyver_dummy:return_arg(1),
?WAS_CALLED(lists:reverse(_)),
?WAS_CALLED(mockgyver_dummy:return_arg(_)).
-handles_all_criterias() ->
+handles_all_criterias_test(_) ->
%% never
{error, _} = mockgyver:check_criteria(never, -1),
ok = mockgyver:check_criteria(never, 0),
@@ -200,22 +167,22 @@ handles_all_criterias() ->
{error, _} = mockgyver:check_criteria({times, 0}, 1),
ok.
-returns_immediately_if_waiters_criteria_already_fulfilled() ->
+returns_immediately_if_waiters_criteria_already_fulfilled_test(_) ->
mockgyver_dummy:return_arg(1),
?WAIT_CALLED(mockgyver_dummy:return_arg(N), once).
-waits_until_waiters_criteria_fulfilled() ->
+waits_until_waiters_criteria_fulfilled_test(_) ->
spawn(fun() -> timer:sleep(50), mockgyver_dummy:return_arg(1) end),
?WAIT_CALLED(mockgyver_dummy:return_arg(N), once).
-returns_other_value() ->
+returns_other_value_test(_) ->
1 = mockgyver_dummy:return_arg(1),
2 = mockgyver_dummy:return_arg(2),
?WHEN(mockgyver_dummy:return_arg(1) -> 42),
42 = mockgyver_dummy:return_arg(1),
?assertError(function_clause, mockgyver_dummy:return_arg(2)).
-can_change_return_value() ->
+can_change_return_value_test(_) ->
1 = mockgyver_dummy:return_arg(1),
?WHEN(mockgyver_dummy:return_arg(1) -> 42),
42 = mockgyver_dummy:return_arg(1),
@@ -224,22 +191,22 @@ can_change_return_value() ->
43 = mockgyver_dummy:return_arg(1),
43 = mockgyver_dummy:return_arg(2).
-inherits_variables_from_outer_scope() ->
+inherits_variables_from_outer_scope_test(_) ->
NewVal = 42,
?WHEN(mockgyver_dummy:return_arg(_) -> NewVal),
42 = mockgyver_dummy:return_arg(1).
-can_use_params() ->
+can_use_params_test(_) ->
?WHEN(mockgyver_dummy:return_arg(N) -> N+1),
2 = mockgyver_dummy:return_arg(1).
-can_use_multi_clause_functions() ->
+can_use_multi_clause_functions_test(_) ->
?WHEN(mockgyver_dummy:return_arg(N) when N >= 0 -> positive;
mockgyver_dummy:return_arg(_N) -> negative),
positive = mockgyver_dummy:return_arg(1),
negative = mockgyver_dummy:return_arg(-1).
-fails_gracefully_when_mocking_a_bif() ->
+fails_gracefully_when_mocking_a_bif_test(_) ->
%% pi/0 should be successfully mocked -- it's a regular function
?WHEN(math:pi() -> 4),
4 = math:pi(),
@@ -248,22 +215,22 @@ fails_gracefully_when_mocking_a_bif() ->
%% mocking the bif should fail gracefully
?assertError({cannot_mock_bif, {math, cos, 1}}, ?WHEN(math:cos(_) -> 0)).
-can_call_original_module() ->
+can_call_original_module_test(_) ->
?WHEN(mockgyver_dummy:return_arg(N) -> 2*'mockgyver_dummy^':return_arg(N)),
6 = mockgyver_dummy:return_arg(3).
-can_make_new_module() ->
+can_make_new_module_test(_) ->
?WHEN(mockgyver_extra_dummy:return_arg(N) -> N),
?WHEN(mockgyver_extra_dummy:return_arg(M, N) -> {M, N}),
1 = mockgyver_extra_dummy:return_arg(1),
{1, 2} = mockgyver_extra_dummy:return_arg(1, 2).
-can_make_new_function_in_existing_module() ->
+can_make_new_function_in_existing_module_test(_) ->
?WHEN(mockgyver_dummy:inc_arg_by_two(N) -> 2*N),
1 = mockgyver_dummy:return_arg(1),
2 = mockgyver_dummy:inc_arg_by_two(1).
-counts_calls() ->
+counts_calls_test(_) ->
mockgyver_dummy:return_arg(1),
mockgyver_dummy:return_arg(2),
mockgyver_dummy:return_arg(2),
@@ -272,7 +239,7 @@ counts_calls() ->
2 = ?NUM_CALLS(mockgyver_dummy:return_arg(2)),
3 = ?NUM_CALLS(mockgyver_dummy:return_arg(_)).
-returns_calls() ->
+returns_calls_test(_) ->
mockgyver_dummy:return_arg(1),
mockgyver_dummy:return_arg(2),
mockgyver_dummy:return_arg(2),
Please sign in to comment.
Something went wrong with that request. Please try again.