forked from eproxus/meck
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
-module(mocking). | ||
-compile([export_all]). | ||
|
||
stub(M, F, A, R) -> | ||
stub(M), | ||
meck:expect(M, F, A, R). | ||
|
||
stub_sequence(M, F, A, Rs) -> | ||
stub(M), | ||
meck:sequence(M, F, A, Rs). | ||
|
||
fake(M, F, A, R) -> | ||
fake(M), | ||
meck:expect(M, F, A, R). | ||
|
||
should_never_call(Module) -> | ||
meck:new(Module, [no_link]). | ||
|
||
% ---- INTERNALS ---- | ||
|
||
stub(M) -> | ||
mock(M, stub_options). | ||
|
||
fake(M) -> | ||
mock(M, fake_options). | ||
|
||
mock(M, MeckOptionsFun) -> | ||
case module_already_mocked(M) of | ||
false -> meck:new(M, ?MODULE:MeckOptionsFun(M)); | ||
true -> noop | ||
end. | ||
|
||
module_already_mocked(Module) -> | ||
case erlang:whereis(meck_process_name(Module)) of | ||
undefined -> false; | ||
_ -> true | ||
end. | ||
|
||
meck_process_name(Module) -> | ||
list_to_atom(atom_to_list(Module) ++ "_meck"). | ||
|
||
stub_options(M) -> | ||
{module, M} = code:ensure_loaded(M), | ||
|
||
case code:is_sticky(M) of | ||
true -> | ||
[no_link, unstick]; | ||
false -> | ||
[no_link] | ||
end. | ||
|
||
fake_options(M) -> | ||
false = code:is_loaded(M), | ||
|
||
[no_link]. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
-import(mocking, [stub/4, stub_sequence/4, fake/4, should_never_call/1]). | ||
|
||
-define(MOCK_FIXTURE_NAME, mock_fixture_test_). | ||
|
||
% add eunit test fixture to module which runs all other functions as tests | ||
% wrapped with meck unload | ||
-define(MOCK_FIXTURE(Setup, Teardown), | ||
?MOCK_FIXTURE_NAME() -> | ||
{foreach, | ||
fun() -> Setup() end, | ||
fun(X) -> Teardown(X), meck:unload() end, | ||
[ | ||
{?MODULE, F} || {F, _A} <- ?MODULE:module_info(exports), | ||
F =/= ?MOCK_FIXTURE_NAME | ||
andalso F =/= test | ||
andalso F =/= module_info | ||
andalso F =/= Setup | ||
andalso F =/= Teardown | ||
] | ||
} | ||
). | ||
|
||
-define(MOCK_FIXTURE, ?MOCK_FIXTURE(fun() -> noop end, fun(_) -> noop end)). |