<div dir="ltr">Hi.<div><br></div><div>I want to write a test for a function that returns list of some tuples:</div><div><br></div><div>my_logic:handle_event(Event, State1) -> {ok, Requests, State2}</div><div><br></div><div>
<br></div><div><br></div><div>My idea is to write such test helper:</div><div><br></div><div>step(Event, RequiredRequests) -></div><div> State1 = get(remembered_state),</div><div> {ok, Requests, State2} = my_logic:handle_event(Event, State1),</div>
<div> put(remembered_state, State2),</div><div> true = lists:all(fun(Match) -></div><div> case lists:any(fun(Request) -></div><div> case Request of</div><div> Match -> true;</div><div>
_ -> false</div><div> end</div><div> end, Requests), RequiredRequests),</div><div> ok.</div><div><br></div><div>and use this function like:</div><div><br></div><div>step(playlist_ready, [{set_timer, download_playlist, _}]) %% This test should check if new timer is set with some not very important time</div>
<div><br></div><div>Problem here is that I cannot pass a match into function "step".</div><div><br></div><div><br></div><div>I have two ideas:</div><div><br></div><div>1) wrap each match into macros:</div><div>
<br></div><div>-define(P(Match), fun(Request) -> case Request of Match -> true; _ -> false end end).</div><div><br></div><div>and use it like:</div><div><br></div><div>step(playlist_ready, [?P({set_timer, download_playlist, _})])<br>
</div><div><br></div><div><br></div><div>2) make a macro:</div><div><br></div><div>-define(step(Event, RequiredRequests), step(Event, ??RequiredRequests)).</div><div><br></div><div>Now it will be:</div><div><br></div><div>
?step(playlist_ready, [{set_timer, download_playlist, _}])</div><div><br></div><div>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.</div>
<div><br></div><div><br></div><div><br></div><div><br></div><div>Should I don't try to make new problems and just use first option?</div><div>Or there is some possibility to scan/parse/eval string with matches in runtime?</div>
<div><br></div></div>