InlineDataAttribute relies on the C # params mechanism. This is what the default InlineData syntax allows in C #: -
[InlineData(1,2)]
Your version with array construction: -
[InlineData( new object[] {1,2})]
- this is just what complier translates above. The moment you go further, you will encounter the same limitations as what the CLI actually allows - the bottom line is that at the IL level, using attribute constructors, it is understood that at compile time everything should be reduced to constants. The F # equivalent of the specified syntax is simple: [<InlineData(1,2)>] , so a direct answer to your question:
module UsingInlineData = [<Theory>] [<InlineData(1, 2)>] [<InlineData(1, 1)>] let v4 (a : int, b : int) : unit = Assert.NotEqual(a, b)
I was not able to avoid the riff using the @bytebuster example, though :) If we define an assistant: -
type ClassDataBase(generator : obj [] seq) = interface seq<obj []> with member this.GetEnumerator() = generator.GetEnumerator() member this.GetEnumerator() = generator.GetEnumerator() :> System.Collections.IEnumerator
Then (if we want to abandon laziness), we can abuse list to avoid using seq / yield to win the golf code: -
type MyArrays1() = inherit ClassDataBase([ [| 3; 4 |]; [| 32; 42 |] ]) [<Theory>] [<ClassData(typeof<MyArrays1>)>] let v1 (a : int, b : int) : unit = Assert.NotEqual(a, b)
But the original seq syntax can be made clean enough, so you donβt need to use it as above, instead:
let values : obj array seq = seq { yield [| 3; 4 |] yield [| 32; 42 |] } type ValuesAsClassData() = inherit ClassDataBase(values) [<Theory; ClassData(typeof<ValuesAsClassData>)>] let v2 (a : int, b : int) : unit = Assert.NotEqual(a, b)
However, most of the idioms with xUnit v2 for me is to use the direct MemberData (which is similar to xUnit v1 PropertyData , but generalized to work with fields): -
[<Theory; MemberData("values")>] let v3 (a : int, b : int) : unit = Assert.NotEqual(a, b)
The key to getting it right is to place : seq<obj> (or : obj array seq ) in the sequence declaration or xUnit will throw at you.