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?
1条答案
按热度按时间liwlm1x91#
file:open/2
始终返回包含两个元素的tuple
:ok
和处理文件的进程(genserver)的pid。或:
1.原子
error
和一个字符串。但是因为字符串是创建列表的快捷方式,所以元组实际上包含原子error
和一个列表。但是
theIoDevice
是一个原子,它永远不会匹配空列表[]
,或者两个元素的列表"[]"
,这是创建列表[91,93]
的快捷方式。您无法提前知道处理文件的进程的pid,因此无法尝试匹配特定的pid,但可以Assert您从
file:open/2
获得了{ok, _}
。如果您希望获得更细的粒度,可以使用is_pid/1
检查第二个元素:您可以手动创建一个pid,如下所示: