Erlang:如何Assert匹配IoDevice值?

wdebmtf2  于 2022-12-08  发布在  Erlang
关注(0)|答案(1)|浏览(143)

I am trying to write a test for opening a file. File open returns a value of the form:

{ok, IoDevice} | {error, Reason}

IoDevice example value is: <0.941.0>
I have tried the following so far (some of them I didn't believe they would succeed anyway...):

?assertMatch([], theIoDevice).
?assertMatch("[]", theIoDevice).
?assertMatch(<>, theIoDevice). % Won't compile

and several more tests, but I fail.
What is the way to match the IoDevice pattern?
Is there possibility to match to Regular Expression?

liwlm1x9

liwlm1x91#

file:open/2始终返回包含两个元素的tuple

  1. atom ok和处理文件的进程(genserver)的pid。
    或:
    1.原子error和一个字符串。但是因为字符串是创建列表的快捷方式,所以元组实际上包含原子error和一个列表。
    但是theIoDevice是一个原子,它永远不会匹配空列表[],或者两个元素的列表"[]",这是创建列表[91,93]的快捷方式。
    您无法提前知道处理文件的进程的pid,因此无法尝试匹配特定的pid,但可以Assert您从file:open/2获得了{ok, _}。如果您希望获得更细的粒度,可以使用is_pid/1检查第二个元素:
?assert(is_pid(File)).

您可以手动创建一个pid,如下所示:

list_to_pid("<0.4.1>") -> pid()

相关问题