1

如何解释基于属性的测试代码?

我正在努力翻译以下代码段中的说明:

let myProperty = Prop.forAll fiveAndThrees <| fun number ->

    let actual = transform number

    let expected = "jbdhjsdhjdsjhsdglsdjlljh"
    expected = actual

Check.QuickThrowOnFailure myProperty

具体来说,我正在与向后管道运算符(即“<|”)作斗争。

这是测试:

[<Fact>]
let ``FizzBuzz.transform returns FizzBuzz`` () =
    let fiveAndThrees = Arb.generate<int> |> Gen.map ((*) (3 * 5)) 
                                          |> Arb.fromGen

    let myProperty = Prop.forAll fiveAndThrees <| fun number ->

        let actual = transform number

        let expected = "jbdhjsdhjdsjhsdglsdjlljh"
        expected = actual

    Check.QuickThrowOnFailure myProperty

有人可以逐步指导我了解此代码的工作原理吗?

可以使用正向管道运算符(即“|>”)重写吗?

4

1 回答 1

3

这个答案只涵盖了为什么<|使用而不是|>.

这里有 5 个示例,它们致力于使用<|大型函数。第 6 个示例是显示使用|>而不是<|. 关键是使用第 6 个示例,|>您必须查看代码以找到主要功能funThatNeedsListAndFunc,但<|主要功能funThatNeedsListAndFunc是显而易见的。因此,您通常会看到<|最后一个参数是一个函数,并且您希望在主函数之后传入该函数以便于理解。就这样; 不要阅读更多内容。

在阅读了 Mark 的博客后,我还了解到删除around<|很有用。使用示例如示例 7 所示。( )fun( )

let funThatNeedsListAndFunc list func =
    func list

let func = List.sum
let list = Seq.toList { 0 .. 5}

let result1 = funThatNeedsListAndFunc list func

printfn "result1: %A" result1

let result2 = funThatNeedsListAndFunc list <| func

printfn "result2: %A" result2

let result3 = funThatNeedsListAndFunc list <| List.sum

printfn "result3: %A" result3

let result4 = funThatNeedsListAndFunc list <| 
    fun (list : 'a list) -> List.sum list

printfn "result4: %A" result4

let result5 = funThatNeedsListAndFunc list <| 
    fun (list : 'a list) -> 
        // This will be a long function
        let a = 1
        let b = 2
        let c = a * b
        let result = List.sum list
        let d = "more useless lines"
        let (e,f,g) = ("a", 15, 3.0)
        result

printfn "result5: %A" result5

.

let result6 = 
    fun (list : 'a list) -> 
        // This will be a long function
        let a = 1
        let b = 2
        let c = a * b
        let result = List.sum list
        let d = "more useless lines"
        let (e,f,g) = ("a", 15, 3.0)
        result
    |> funThatNeedsListAndFunc list

printfn "result6: %A" result6

.

let result7 = 
    funThatNeedsListAndFunc list (fun (list : 'a list) -> 
        // This will be a long function
        let a = 1
        let b = 2
        let c = a * b
        let result = List.sum list
        let d = "more useless lines"
        let (e,f,g) = ("a", 15, 3.0)
        result)

printfn "result7: %A" result7
于 2016-05-12T14:04:32.417 回答