[erlang-questions] How to call a module's local function from within an external fun

Magnus Klaar magnus.klaar@REDACTED
Fri Nov 25 19:41:49 CET 2011


Hi!

There is a built in module attribute, -import, that can be used to import
functions exported from one module into the local namespace of another
module. If you really want to write a parse transform I would recommend
writing one that adds this module attribute to your test modules. An
alternative solution is to create an include a file containing only an
-import attribute into the test modules.

echo '-import(test_module, [send/2, expect/2]). %% fix arities' >
include/test_module.hrl

After this you will able to use test_module:send/2 and test_module:expect/2
as send/2 and expect/2 in the modules you add the following include
attribute to:

-include_lib("test_module/include/test_module.hrl").

/ Magnus

On Fri, Nov 25, 2011 at 7:29 PM, Francis Joanis <francis.joanis@REDACTED>wrote:

> Hi,
>
> I'm trying to write a test tool that can be used to send and expect
> messages over a custom protocol. I would like the tests themselves to
> be supplied as funs from outside the module so that:
>
> test_module:run_test(TestModulePid, fun() -> send(...), expect(...) end).
>
> would end up calling send() and expect() from test_module, as if the
> fun was executed in the "context" of the module (i.e. as if it were
> defined directly within run_test() and not supplied to it). The
> resulting "API" would look like some kind of DSL specialized to define
> my tests.
>
> In my case I need multiple test_modules to run in parallel, so I can't
> use a single "test_module" registered name. It would be simple to have
> test_module export both send() and expect() so that the following
> works:
>
> test_module:run_test(TestModulePid, fun(TestModulePid) ->
> test_module:send(TestModulePid, ...),
> test_module:expect(TestModulePid, ...) end).
>
> but I don't like the extra verboseness of having to write
> test_module:... all the time.
>
> I _think_ that I might be able to use parse transforms to achieve what
> I want but I wanted to know if there would be an easier way.
>
> Cheers,
> Francis
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20111125/48af7503/attachment.htm>


More information about the erlang-questions mailing list