[erlang-questions] How to use erl_parse, erl_scan, erl_eval?

Max Lapshin max.lapshin@REDACTED
Tue Apr 1 16:50:19 CEST 2014


Hi.

I want to write a test for a function that returns list of some tuples:

my_logic:handle_event(Event, State1) -> {ok, Requests, State2}



My idea is to write such test helper:

step(Event, RequiredRequests) ->
  State1 = get(remembered_state),
  {ok, Requests, State2} = my_logic:handle_event(Event, State1),
  put(remembered_state, State2),
  true = lists:all(fun(Match) ->
     case lists:any(fun(Request) ->
          case Request of
              Match -> true;
              _ -> false
          end
       end, Requests), RequiredRequests),
  ok.

and use this function like:

step(playlist_ready,  [{set_timer, download_playlist, _}])   %% This test
should check if new timer is set with some not very important time

Problem here is that I cannot pass a match into function "step".


I have two ideas:

1)  wrap each match into macros:

-define(P(Match),  fun(Request) -> case Request of Match -> true; _ ->
false end end).

and use it like:

step(playlist_ready,  [?P({set_timer, download_playlist, _})])


2) make a macro:

-define(step(Event, RequiredRequests), step(Event, ??RequiredRequests)).

Now it will be:

?step(playlist_ready,  [{set_timer, download_playlist, _}])

but I cannot find any guide how to use those  erl_parse, erl_scan, erl_eval
to split string  "[{set_timer, download_playlist, _}]" into some tokens and
create a list of matches from it.




Should I don't try to make new problems and just use first option?
Or there is some possibility to scan/parse/eval string with matches in
runtime?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20140401/6abd96be/attachment.htm>


More information about the erlang-questions mailing list