3 people like it.

NUnit assertions with pattern matching

A simple utility that allows making assertions using potentially complex pattern matches.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
open NUnit.Framework

let shouldMatch (f : 'a -> bool) (x : 'a) =
    if f x then () 
    else raise <| new AssertionException(sprintf "Unexpected result: %A." x)


[<TestFixture>]
type Test() =

    // concoct some random test scenario
    [<Test>]
    member __.``test scenario``() =
        [1..10]
        |> List.map (fun i -> (i,i % 2 = 0))
        |> List.filter snd
        |> shouldMatch (function _ :: (4,_) :: _ -> true | _ -> false)
        
namespace NUnit
namespace NUnit.Framework
val shouldMatch : f:('a -> bool) -> x:'a -> unit

Full name: Script.shouldMatch
val f : ('a -> bool)
type bool = System.Boolean

Full name: Microsoft.FSharp.Core.bool
val x : 'a
val raise : exn:System.Exception -> 'T

Full name: Microsoft.FSharp.Core.Operators.raise
Multiple items
type AssertionException =
  inherit Exception
  new : message:string -> AssertionException + 1 overload

Full name: NUnit.Framework.AssertionException

--------------------
AssertionException(message: string) : unit
AssertionException(message: string, inner: exn) : unit
val sprintf : format:Printf.StringFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
Multiple items
type TestFixtureAttribute =
  inherit Attribute
  new : unit -> TestFixtureAttribute + 1 overload
  member Arguments : obj[]
  member Categories : IList
  member Category : string with get, set
  member Description : string with get, set
  member Ignore : bool with get, set
  member IgnoreReason : string with get, set
  member TypeArgs : Type[] with get, set

Full name: NUnit.Framework.TestFixtureAttribute

--------------------
TestFixtureAttribute() : unit
TestFixtureAttribute([<System.ParamArray>] arguments: obj []) : unit
Multiple items
type Test =
  new : unit -> Test
  member ( test scenario ) : unit -> unit

Full name: Script.Test

--------------------
type TestAttribute =
  inherit Attribute
  new : unit -> TestAttribute
  member Description : string with get, set

Full name: NUnit.Framework.TestAttribute

--------------------
new : unit -> Test

--------------------
TestAttribute() : unit
member Test.( test scenario ) : unit -> unit

Full name: Script.Test.( test scenario )
Multiple items
type List =
  new : unit -> List
  static member Map : actual:ICollection -> ListMapper

Full name: NUnit.Framework.List

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>

--------------------
List() : unit
val map : mapping:('T -> 'U) -> list:'T list -> 'U list

Full name: Microsoft.FSharp.Collections.List.map
val i : int
val filter : predicate:('T -> bool) -> list:'T list -> 'T list

Full name: Microsoft.FSharp.Collections.List.filter
val snd : tuple:('T1 * 'T2) -> 'T2

Full name: Microsoft.FSharp.Core.Operators.snd

More information

Link:http://fssnip.net/dL
Posted:12 years ago
Author:Eirik Tsarpalis
Tags: nunit , pattern matching