在 FsCheck 中生成自定义数据

O.F*_*.F. 4 f# fscheck

我有一个FsCheck问题:
我有以下记录类型(我事先说,我被告知我的单案例 DU 可能是一种矫枉过正,但我​​发现它们描述了域,因此是必要的,我会除非必须,否则不要删除它们):

type Name = Name of string
type Quality = Quality of int
type ShelfLife = Days of int
type Style = Plain | Aged | Legendary

type Item = {
    Name: Name
    Quality: Quality
    ShelfLife: ShelfLife
    Style: Style
}
Run Code Online (Sandbox Code Playgroud)

假设我已经定义了函数repeat: Int -> ('a -> 'a) -> 'adecreaseQuality: Item -> Item,我想编写一个FsCheck检查不变量的测试:任何样式为 OTHER THAN Legendary 的项目,在 100 天过去后,质量为 0

我的问题是我不知道以下内容FsCheck
1. 如何定义一个自定义生成器来生成样式不是传奇的项目?相比之下,我如何定义Legendary 类型的项目(以测试两种类型)?

我调查过:

 let itemGenerator = Arb.generate<Item>
 Gen.sample 80 5 itemGenerator
Run Code Online (Sandbox Code Playgroud)

但是创建是纯怪异,作为物品size的控制器,在该示例中,80,还控制的长度Name of string(由于of string),并且还产生QualityShelfLife因为它们都定义是不能接受的我的结构域(即负)值作为... of int其尺寸也控制。
(我也研究过Gen.oneof...,但结果证明这也是个废话)。

  1. 我什至如何定义一个只测试记录的 Quality 属性的测试,即使假设我找到了一种生成自定义数据的方法?

谢谢!

rmu*_*unn 6

一旦您知道如何使用gen { }计算表达式以达到最大效果,您想要的大部分内容都会变得简单。

首先,我将解决如何生成一个Style非传奇的问题。您可以使用Gen.oneOf,但在这种情况下,我认为使用更简单Gen.elements,因为oneOf需要使用一系列生成器,但elements只需要一个项目列表并从该列表中生成一个项目。所以要生成一个Style不是传奇的,我会使用Gen.elements [Plain; Aged]. (并产生一个Style传说,我只是不使用发电机,并且只分配到传奇相应的记录领域,但后来更多。)

至于名称太长,为了将生成的字符串的大小限制为最多 15 个字符,我会使用:

let genString15 = Gen.sized (fun s -> Gen.resize (min s 15) Arb.generate<string>)
// Note: "min" is not a typo. We want either s or 15, whichever is SMALLER
Gen.sample 80 5 genString15
// Never produces any strings longer than 15 characters
Run Code Online (Sandbox Code Playgroud)

但这仍然可以生成null字符串,所以我可能会在我的最终版本中使用它:

let genString15 =
    Gen.sized (fun s -> Gen.resize (min s 15) Arb.generate<NonNull<string>>)
    |> Gen.map (fun (NonNull x) -> x)  // Unwrap
Gen.sample 80 5 genString15
// Never produces any strings longer than 15 characters, AND never produces null
Run Code Online (Sandbox Code Playgroud)

现在,由于QualityShelfLife两者都不能为负,我会使用PositiveInt(其中不允许为 0)或NonNegativeInt(允许为 0)。FsCheck 文档中都没有详细记录,但它们的工作方式如下:

let x = Arb.generate<NonNegativeInt>
Gen.sample 80 5 x
// Produces [NonNegativeInt 79; NonNegativeInt 75; NonNegativeInt 0;
//           NonNegativeInt 69; NonNegativeInt 16] which is hard to deal with
let y = Arb.generate<NonNegativeInt> |> Gen.map (fun (NonNegativeInt n) -> n)
Gen.sample 80 5 y
// Much better: [79; 75; 0; 69; 16]
Run Code Online (Sandbox Code Playgroud)

为了避免在Qualityand 的生成器之间重复代码Days,我会写如下内容:

let genNonNegativeOf (f : int -> 'a) = gen {
    let! (NonNegativeInt n) = Arb.generate<NonNegativeInt>
    return (f n)
}
Gen.sample 80 5 (genNonNegativeOf Quality)
// Produces: [Quality 79; Quality 35; Quality 2; Quality 42; Quality 73]
Gen.sample 80 5 (genNonNegativeOf Days)
// Produces: [Days 60; Days 27; Days 50; Days 22; Days 23]
Run Code Online (Sandbox Code Playgroud)

最后,让我们以一种漂亮、优雅的方式与gen { }CE结合在一起:

let genNonLegendaryItem = gen {
    let! name = genString15 |> Gen.map Name
    let! quality = genNonNegativeOf Quality
    let! shelfLife = genNonNegativeOf Days
    let! style = Gen.elements [Plain; Aged]
    return {
        Name = name
        Quality = quality
        ShelfLife = shelfLife
        Style = style
    }
}
let genLegendaryItem =
    // This is the simplest way to avoid code duplication
    genNonLegendaryItem
    |> Gen.map (fun item -> { item with Style = Legendary })
Run Code Online (Sandbox Code Playgroud)

然后,一旦你这样做了,要在你的测试中实际使用它,你需要注册生成器,正如 Tarmil 在他的回答中提到的那样。我可能会在这里使用单例 DU,以便轻松编写测试,如下所示:

type LegendaryItem = LegendaryItem of Item
type NonLegendaryItem = NonLegendaryItem of Item
Run Code Online (Sandbox Code Playgroud)

然后,您可以通过将genLegendaryItemgenNonLegendaryItem生成器(Non)LegendaryItem通过Gen.map. 然后您的测试用例将如下所示(我将在这里使用Expecto作为示例):

[<Tests>]
let tests =
    testList "Item expiration" [
        testProperty "Non-legendary items expire after 100 days" <| fun (NonLegendaryItem item) ->
            let itemAfter100Days = item |> repeat 100 decreaseQuality
            itemAfter100Days.Quality = Quality 0
        testProperty "Legendary items never expire" <| fun (LegendaryItem item) ->
            let itemAfter100Days = item |> repeat 100 decreaseQuality
            itemAfter100Days.Quality > Quality 0
    ]
Run Code Online (Sandbox Code Playgroud)

请注意,使用这种方法,您基本上必须自己编写收缩器,而Arb.convert按照 Tarmil 的建议使用将“免费”获得收缩器。不要低估收缩器的价值,但如果你发现没有它们你也能活下去,我喜欢gen { }计算表达式的漂亮、干净的性质,以及阅读结果代码是多么容易。