jhn_shadow behaviour (jhn_stdlib v5.10.3)

View Source

A simple lightweight mocking library that allows the mocking of modules and applications and the simplified loading of modules used to mock.

A module can be mocked for some or all of its exported functions, stateless or with a state that can be inspected and interacted with to provide more intricate mocking behaviour. The mocked module must be compiled in the load path but no special flags or preparation of the module is needed.

When the mocking is no longer required the module can be "revealed" and used normally. The original module is loaded.

To be noted is that in the mocked module for unmocked (passthrough) functions any use of the atom of the module name is replaced with hidden_{original module name}.

Summary

Types

app/0

-type app() :: atom().

application_opt/0

-type application_opt() ::
          env() |
          #{env => env(),
            applications => [atom()],
            mod => dummy | {module(), _},
            sup => {module(), args()} | {module(), name(), args()}}.

application_opts/0

-type application_opts() :: [application_opt()].

args/0

-type args() :: [_].

env/0

-type env() :: [{atom(), _}].

fa/0

-type fa() :: {name(), arity()}.

module_opts/0

-type module_opts() :: jhn_server:opts().

name/0

-type name() :: atom().

shadow/0

-type shadow() :: pid().

Callbacks

hide/3

(optional)
-callback hide(module(), name(), args()) -> _.

hide/4

(optional)
-callback hide(module(), name(), args(), State) -> {ok, _, State}.

init/1

(optional)
-callback init(_) -> {ok, _}.

peek/2

(optional)
-callback peek(_, State) -> {ok, _, State}.

Functions

create(Mod, Opts)

-spec create(module(), module_opts()) -> {ok, pid()} | ignore | {error, _}.

destroy(Shadow)

-spec destroy(shadow()) -> ok.

hide(Mod, CB, Funcs)

-spec hide(module(), module() | pid() | {name, name()}, all | [fa()]) -> ok | {error, _} | {error, _, _}.

load_app(Name, Opts)

-spec load_app(app(), application_opts()) -> ok | {error, _}.

load_mod(B)

-spec load_mod(binary() | string()) -> ok | {error, _} | {error, _, _}.

load_mod(Mod, Funs)

-spec load_mod(module(), [binary() | string()]) -> ok | {error, _} | {error, _, _}.

passthrough(Mod, Name, Args)

-spec passthrough(module(), name(), args()) -> _.

peek(Shadow, Arg)

-spec peek(shadow(), _) -> _.

reveal(Mod)

-spec reveal(module()) -> ok | {error, not_hidden}.

start_all_app(Name, Opts)

-spec start_all_app(app(), application_opts()) -> ok | {error, _}.

start_app(Name, Opts)

-spec start_app(app(), application_opts()) -> ok | {error, _}.

stop_app(Name)

-spec stop_app(app()) -> ok | {error, _}.

unload_app(Name)

-spec unload_app(app()) -> ok | {error, not_loaded | not_shadow_application}.

unload_mod(Mod)

-spec unload_mod(module()) -> ok | {error, not_loaded | not_shadow}.