比较受歧视的工会
我是 F# 的新手,正在尝试 FParsec。我会使用 FParsec 生成 AST。我想使用 FsUnit 围绕解析器的各个部分编写一些测试,以确保正确运行。
我在语法上遇到了一些麻烦(抱歉,确切的代码正在工作,我可以稍后发布一个具体的示例),那么如何准确地比较两个可区分的联合(一个是预期的,另一个是实际的结果)?有人可以提供一个使用 FsUnit(或 NUnit)的小代码示例吗?
一个歧视联盟的例子(非常简单)
type AST =
| Variable of string
| Class of string
| Number of int
I'm a newbie to F# and I'm playing around with FParsec. I would use FParsec to generate an AST. I would like to use FsUnit to write some tests around the various parts of the parser to ensure correct operation.
I'm having a bit of trouble with the syntax (sorry, the exact code is at work, I can post a specific example later) so how exactly could one compare two discriminated unions (one the expected, the other the actual result)? Could someone provide a tiny code example using FsUnit (or NUnit), please?
An example discriminated union (very simple)
type AST =
| Variable of string
| Class of string
| Number of int
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 Brian 指出的,由于 F# 联合具有结构平等,因此使用您喜欢的任何单元测试框架都很容易。
FsUnit 是构建在 NUnit 之上的 F# 特定库。我个人最喜欢的 F# 特定单元测试库是 Unquote,;),它与框架无关,与 NUnit、xUnit.net、MbUnit...甚至在 FSI 内都能很好地工作。您可能对这个比较感兴趣FsUnit。
那么,如何使用 NUnit + Unquote 来做到这一点呢?这是一个完整的工作示例:
然后使用 TestDriven.NET 运行测试,输出如下:
Since, as Brian pointed out, F# unions have structural equality, this is easy using whichever unit testing framework you are fond of.
FsUnit is an F# specific library built on top of NUnit. My personal favorite F# specific unit testing library is Unquote, ;), which is framework agnostic, working very well with NUnit, xUnit.net, MbUnit, ... or even within FSI. You may be interested in this comparison with FsUnit.
So, how would you do this with NUnit + Unquote? Here's a full working example:
Then running the tests using TestDriven.NET, the output is as follows:
一个例子 - 如果你想检查类型而不是内容
请注意,你需要为可区分联合的每个元素使用不同的函数
如果你想比较相等性,你可以用标准方式来做,比如
或
An example - if you want to check the type but not the contents
Note here that you need a different function for each element of the discriminated union
If you want to compare equality, you can just do it in the standard way, like
or