Skip to content

Instantly share code, notes, and snippets.

@pirrmann
Created November 21, 2014 17:59
Show Gist options
  • Save pirrmann/830c0cb3eff3e6e8c6dd to your computer and use it in GitHub Desktop.
Save pirrmann/830c0cb3eff3e6e8c6dd to your computer and use it in GitHub Desktop.
Using a computation expression to produce test cases
module TestCasesBuilder
open Microsoft.FSharp.Reflection
type TestCasesBuilder() =
member x.Zero() = Seq.empty
member x.Yield<'T>(t:'T) =
let value =
if FSharpType.IsTuple(typeof<'T>)
then t |> FSharpValue.GetTupleFields
else [|t :> obj|]
Seq.singleton(value)
member x.Delay(f) = f()
member x.Combine(s1, s2) = Seq.append s1 s2
let testcases = new TestCasesBuilder()
module Tests
open NUnit.Framework
open FsUnit
open TestCasesBuilder
let [<Test>] ``A standard test`` () =
1 |> should equal 1
let source1 = testcases { yield 1 }
let [<TestCaseSource("source1")>] ``A test with a source of int`` (input) =
input |> should equal 1
let source2 = testcases {
yield 1, 1, "2"
yield 4, 5, "9" }
let [<TestCaseSource("source2")>] ``A test with a source of tuples`` (x, y, sumAsString) =
(x + y).ToString() |> should equal sumAsString
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment