jeroldhaas
7/12/2016 - 4:38 PM

There, screw nunit.

There, screw nunit.

exception AssertionException of string

module Assert =

    let isTrue value =
        if not value then
            raise (AssertionException "Expected true but got false.")

    let isFalse value =
        if value then
            raise (AssertionException "Expected false but got true.")

    let inline areEqual expected actual =
        if expected <> actual then
            raise (AssertionException ("Expected value '" + string expected + "' but got '" + string actual + "'."))

    let tests1 (ty : Type) =
        let methods = ty.GetMethods (BindingFlags.Instance ||| BindingFlags.Public)
        let instance = Activator.CreateInstance(ty, [||])
        for meth in methods do
            try meth.Invoke(instance, [||]) |> ignore
            with
            | :? AssertionException as exn -> Console.WriteLine ("Test method '" + ty.FullName + "." + meth.Name + "' failed due to: " + string exn)
            | exn -> Console.WriteLine ("Test method '" + ty.FullName + "." + meth.Name + "' unexpectedly exited with exception: " + string exn)

    let tests<'t> () =
        tests1 (typeof<'t>)