Current section

Files

Jump to
mockth src mockth.erl
Raw

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]).
-export_type([history/0]).
-type history() :: {history,
gleam@erlang@process:pid_(),
binary(),
binary(),
list(gleam@dynamic:dynamic_())}.
-spec module_atom_to_string(gleam@erlang@atom:atom_()) -> binary().
module_atom_to_string(Module) ->
_pipe = Module,
_pipe@1 = erlang:atom_to_binary(_pipe),
gleam@string:replace(_pipe@1, <<"@"/utf8>>, <<"/"/utf8>>).
-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 module_atom_to_string/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) ->
Module@1 = gleam@string:replace(Module, <<"/"/utf8>>, <<"@"/utf8>>),
_pipe = gleam_erlang_ffi:atom_from_string(Module@1),
gleam@result:map_error(
_pipe,
fun(_) -> <<"Failed to find module "/utf8, Module@1/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 = module_atom_to_string(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) ->
_pipe = to_module_atom(Module),
gleam@result:then(
_pipe,
fun(Module@1) -> _pipe@1 = to_function_atom(Function),
gleam@result:map(
_pipe@1,
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
).