比较受歧视的工会

发布于 2024-12-05 04:49:11 字数 337 浏览 1 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葬花如无物 2024-12-12 04:49:11

正如 Brian 指出的,由于 F# 联合具有结构平等,因此使用您喜欢的任何单元测试框架都很容易。

FsUnit 是构建在 NUnit 之上的 F# 特定库。我个人最喜欢的 F# 特定单元测试库是 Unquote,;),它与框架无关,与 NUnit、xUnit.net、MbUnit...甚至在 FSI 内都能很好地工作。您可能对这个比较感兴趣FsUnit。

那么,如何使用 NUnit + Unquote 来做到这一点呢?这是一个完整的工作示例:

module UnitTests

open NUnit.Framework
open Swensen.Unquote

type AST = 
    | Variable of string
    | Class of string
    | Number of int

let mockFParsec_parseVariable input = Variable(input)

[<Test>]
let ``test variable parse, passing example`` () =
    test <@ mockFParsec_parseVariable "x" = Variable("x") @>

[<Test>]
let ``test variable parse, failing example`` () =
    test <@ mockFParsec_parseVariable "y" = Variable("x") @>

然后使用 TestDriven.NET 运行测试,输出如下:

------ Test started: Assembly: xxx.exe ------

Test 'UnitTests.test variable parse, failing example' failed: 

UnitTests.mockFParsec_parseVariable "y" = Variable("x")
Variable "y" = Variable("x")
false
    C:\xxx\UnitTests.fs(19,0): at UnitTests.test variable parse, failing example()

1 passed, 1 failed, 0 skipped, took 0.80 seconds (NUnit 2.5.10).

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:

module UnitTests

open NUnit.Framework
open Swensen.Unquote

type AST = 
    | Variable of string
    | Class of string
    | Number of int

let mockFParsec_parseVariable input = Variable(input)

[<Test>]
let ``test variable parse, passing example`` () =
    test <@ mockFParsec_parseVariable "x" = Variable("x") @>

[<Test>]
let ``test variable parse, failing example`` () =
    test <@ mockFParsec_parseVariable "y" = Variable("x") @>

Then running the tests using TestDriven.NET, the output is as follows:

------ Test started: Assembly: xxx.exe ------

Test 'UnitTests.test variable parse, failing example' failed: 

UnitTests.mockFParsec_parseVariable "y" = Variable("x")
Variable "y" = Variable("x")
false
    C:\xxx\UnitTests.fs(19,0): at UnitTests.test variable parse, failing example()

1 passed, 1 failed, 0 skipped, took 0.80 seconds (NUnit 2.5.10).
野稚 2024-12-12 04:49:11

一个例子 - 如果你想检查类型而不是内容

let matched x= 
    match x with
    |Variable(_) -> true
    | _ -> false

请注意,你需要为可区分联合的每个元素使用不同的函数

如果你想比较相等性,你可以用标准方式来做,比如

Assert.AreEqual(Variable("hello"),result)

if result = Variable("hello") then stuff()

An example - if you want to check the type but not the contents

let matched x= 
    match x with
    |Variable(_) -> true
    | _ -> false

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

Assert.AreEqual(Variable("hello"),result)

or

if result = Variable("hello") then stuff()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文