In F# how do you pass a collection to xUnit's InlineData attribute

前端 未结 6 790
故里飘歌
故里飘歌 2021-01-02 00:28

I would like to be about to use a list, array, and/or seq as a parameter to xUnit\'s InlineData.

In C# I can do this:

using Xunit; //2.1.0

namespace C         


        
6条回答
  •  忘掉有多难
    2021-01-02 01:09

    Building further on @Assassin's brilliant answer -- now we have implicit yields you can put the test cases in an array and dispense with the yields. I would also be tempted to add a cheeky little private operator to handle the object conversions. Thus:

    open System
    open Xunit
    
    let inline private (~~) x = x :> Object
    
    let degreesToRadiansCases =
        [|
            // Degrees; Radians
            [|   ~~0.0;            ~~0.0  |]
            [| ~~360.0; ~~(Math.PI * 2.0) |]
        |]
    
    []
    []
    let ``Convert from degrees to radians`` (degrees, radians) =
        let expected = radians
        let actual = Geodesy.Angle.toRadians degrees
        Assert.Equal(expected, actual)
    
    let stringCases =
        [|
            [| ~~99; ~~"hello1" |] 
            [| ~~99; ~~"hello2" |] 
        |]
    
    []
    []
    let ``tests`` (i, s) =
        printfn "%i %s" i s
        Assert.Equal(s, "hello1")
    

提交回复
热议问题