I am using Erlang's EUnit for unit testing an application.
I want to assert that a certain test value is between 2 and 3. There's no built-in support for this, so I am trying to use a pair of guards, like this:
myTestCase ->
?assertMatch({ok, V} when V>2, V<3,
unitUnderTest() % expected to return 2.54232...
).
This tries to use the guard syntax for andalso
.
However, this does not work, presumably because Erlang's parser can't tell the difference between multiple guards and multiple arguments to assertMatch
. I tried wrapping various things in parentheses, but haven't found anything that works. On the other hand, when I reduce the expression to just one clause, it succeeds.
Is there a way to express multiple clauses here?
You cannot use commas in the syntax, simply because then ?assertMatch is interpreted as a macro with 3 - or more - parameters, and there is no definition for that. But the syntax andalso and orelse works. Why don't you use :
fok() -> {ok,2.7}.
fko() -> {ok,3.7}.
myTestCase(F) ->
F1 = fun() -> apply(?MODULE,F,[]) end,
?assertMatch({ok, V} when V>2 andalso V<3, F1()).
test:
1> c(test).
{ok,test}
2> test:myTestCase(fok).
ok
3> test:myTestCase(fko).
** exception error: {assertMatch,
[{module,test},
{line,30},
{expression,"F1 ( )"},
{pattern,"{ ok , V } when V > 2 andalso V < 3"},
{value,{ok,3.7}}]}
in function test:'-myTestCase/1-fun-1-'/1 (test.erl, line 30)
4>