Erlang EUnit'中的多个子句;什么是资产匹配?
我使用Erlang对应用程序进行单元测试 我想断言某个测试值介于2和3之间,所以我试着用一对护卫,像这样:Erlang EUnit'中的多个子句;什么是资产匹配?,erlang,eunit,Erlang,Eunit,我使用Erlang对应用程序进行单元测试 我想断言某个测试值介于2和3之间,所以我试着用一对护卫,像这样: myTestCase -> ?assertMatch({ok, V} when V>2, V<3, unitUnderTest() % expected to return 2.54232... ). myTestCase-> ?assertMatch({ok,V}当V>2时,V 然而,这不起作用,大概是因为Erlang的解析器不能
myTestCase ->
?assertMatch({ok, V} when V>2, V<3,
unitUnderTest() % expected to return 2.54232...
).
myTestCase->
?assertMatch({ok,V}当V>2时,V
然而,这不起作用,大概是因为Erlang的解析器不能
告诉您多个保护和多个参数之间的区别
资产匹配
政府明确警告:
assertMatch(GuardedPattern,Expr)
GuardedPattern可以是您可以在左手上书写的任何内容
case子句中->符号的一侧,但不能包含
逗号分隔的保护测试
那么:
-module(my).
-compile(export_all).
-include_lib("eunit/include/eunit.hrl").
f() ->
{ok, 3}.
f_test() ->
?LET({ok, X}, f(), ?assertMatch( true, X>2 andalso X<4 ) ).
-模块(my)。
-编译(全部导出)。
-include_lib(“eunit/include/eunit.hrl”)。
f()->
{好的,3}。
f_测试()->
LET({ok,X},f(),?assertMatch(true,X>2 and Also X您不能在语法中使用逗号,因为?assertMatch被解释为具有3个或更多参数的宏,并且没有定义。但是语法andalso和orelse可以工作。为什么不使用:
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()).
fok()->{ok,2.7}。
fko()->{ok,3.7}。
myTestCase(F)->
F1=fun()->应用(?模块,F,[])结束,
?assertMatch({ok,V}当V>2时,也进行V c(测试)。
{好的,测试}
2> 测试:myTestCase(fok)。
好啊
3> 测试:myTestCase(fko)。
**异常错误:{assertMatch,
[{模块,测试},
{line,30},
{表达式,“F1()”},
{pattern,{ok,V}当V>2并且V<3时,
{value,{ok,3.7}}]}
函数测试中:'-myTestCase/1-fun-1-'/1(test.erl,第30行)
4>
您可以将?assertMatch(true,Exp)
替换为那里的?assert(Exp)
。
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>