Current section
Files
Jump to
Current section
Files
src/mockth.erl
-module(mockth).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([unload_all/0, mocked/0, validate/1, unload/1, history/1, expect/3, with_mock/4]).
-export_type([history/0]).
-type history() :: {history,
gleam@erlang@process:pid_(),
binary(),
binary(),
list(gleam@dynamic:dynamic_())}.
-spec module_atoms_to_strings(list(gleam@erlang@atom:atom_())) -> list(binary()).
module_atoms_to_strings(Module_atoms) ->
_pipe = Module_atoms,
gleam@list:map(_pipe, fun erlang:atom_to_binary/1).
-spec unload_all() -> list(binary()).
unload_all() ->
_pipe = meck:unload(),
module_atoms_to_strings(_pipe).
-spec mocked() -> list(binary()).
mocked() ->
_pipe = meck:mocked(),
module_atoms_to_strings(_pipe).
-spec is_ok_atom(gleam@erlang@atom:atom_()) -> {ok, boolean()} |
{error, binary()}.
is_ok_atom(A) ->
Ok = gleam_erlang_ffi:atom_from_string(<<"ok"/utf8>>),
case Ok of
{ok, Ok@1} ->
case Ok@1 =:= A of
true ->
{ok, true};
false ->
{error, <<"Failed to expect"/utf8>>}
end;
{error, _} ->
{error, <<"Failed to expect"/utf8>>}
end.
-spec to_function_atom(binary()) -> {ok, gleam@erlang@atom:atom_()} |
{error, binary()}.
to_function_atom(Function) ->
_pipe = gleam_erlang_ffi:atom_from_string(Function),
gleam@result:map_error(
_pipe,
fun(_) -> <<"Failed to find function "/utf8, Function/binary>> end
).
-spec to_module_atom(binary()) -> {ok, gleam@erlang@atom:atom_()} |
{error, binary()}.
to_module_atom(Module) ->
_pipe = gleam_erlang_ffi:atom_from_string(Module),
gleam@result:map_error(
_pipe,
fun(_) -> <<"Failed to find module "/utf8, Module/binary>> end
).
-spec validate(binary()) -> boolean().
validate(Module) ->
case to_module_atom(Module) of
{ok, Module@1} ->
meck:validate(Module@1);
{error, _} ->
false
end.
-spec unload(binary()) -> {ok, boolean()} | {error, binary()}.
unload(Module) ->
_pipe = Module,
_pipe@1 = to_module_atom(_pipe),
gleam@result:then(_pipe@1, fun(Module@1) -> _pipe@2 = meck:unload(Module@1),
is_ok_atom(_pipe@2) end).
-spec history(binary()) -> {ok, list(history())} | {error, binary()}.
history(Module) ->
_pipe = Module,
_pipe@1 = to_module_atom(_pipe),
_pipe@2 = gleam@result:map(
_pipe@1,
fun(Module@1) -> meck:history(Module@1) end
),
gleam@result:then(_pipe@2, fun(Histories) -> _pipe@3 = Histories,
_pipe@4 = gleam@list:map(
_pipe@3,
fun(Histoy) ->
{Pid, Mfa, _} = Histoy,
{Module@2, Function, Arguments} = Mfa,
Module@3 = erlang:atom_to_binary(Module@2),
Function@1 = erlang:atom_to_binary(Function),
{history, Pid, Module@3, Function@1, Arguments}
end
),
{ok, _pipe@4} end).
-spec load_expect_atoms(binary(), binary()) -> {ok,
{gleam@erlang@atom:atom_(), gleam@erlang@atom:atom_()}} |
{error, binary()}.
load_expect_atoms(Module, Function) ->
gleam@result:then(
to_module_atom(Module),
fun(Module@1) ->
gleam@result:map(
to_function_atom(Function),
fun(Function@1) -> {Module@1, Function@1} end
)
end
).
-spec expect(binary(), binary(), any()) -> {ok, boolean()} | {error, binary()}.
expect(Module, Function, Fun) ->
gleam@result:then(
load_expect_atoms(Module, Function),
fun(_use0) ->
{Module@1, Function@1} = _use0,
_pipe = meck:expect(Module@1, Function@1, Fun),
is_ok_atom(_pipe)
end
).
-spec with_mock(binary(), binary(), any(), fun((binary()) -> any())) -> list(binary()).
with_mock(Module, Function, Fun, F) ->
_assert_subject = expect(Module, Function, Fun),
{ok, _} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"mockth"/utf8>>,
function => <<"with_mock"/utf8>>,
line => 151})
end,
_pipe = validate(Module),
gleeunit@should:be_true(_pipe),
_pipe@1 = mocked(),
_pipe@2 = gleam@list:contains(_pipe@1, Module),
gleeunit@should:be_true(_pipe@2),
F(Module),
unload_all().