# FsUnit for MsTest

The FsUnit library for MsTest can be installed from NuGet:
PM> Install-Package Fs30Unit.MsTest
Sample FsUnit tests for MsTest can be installed from NuGet:
PM> Install-Package Fs30Unit.MsTest.Sample

## Euler - Problem 1

module Project Euler - Problem 1 =
open Microsoft.VisualStudio.TestTools.UnitTesting
open FsUnit.MsTest

let GetSumOfMultiplesOf3And5 max =
seq{3..max-1} |> Seq.fold(fun acc number ->
(if (number % 3 = 0 || number % 5 = 0) then
acc + number else acc)) 0

[<TestMethod>]
let When getting sum of multiples of 3 and 5 to a max number of 10 it should return a sum of 23 () =
GetSumOfMultiplesOf3And5(10) |> should equal 23


## LightBulb

module LightBulb Tests =
open Microsoft.VisualStudio.TestTools.UnitTesting
open FsUnit.MsTest

type LightBulb(state) =
member x.On = state
override x.ToString() =
match x.On with
| true  -> "On"
| false -> "Off"

[<TestClass>]
type Given a LightBulb that has had its state set to true ()=
let lightBulb = new LightBulb(true)

[<TestMethod>] member x.
when I ask whether it is On it answers true. ()=
lightBulb.On |> should be True

[<TestMethod>] member x.
when I convert it to a string it becomes "On". ()=
string lightBulb |> should equal "On"

[<TestClass>]
type Given a LightBulb that has had its state set to false ()=
let lightBulb = new LightBulb(false)

[<TestMethod>] member x.
when I ask whether it is On it answers false. ()=
lightBulb.On |> should be False

[<TestMethod>] member x.
when I convert it to a string it becomes "Off". ()=
string lightBulb |> should equal "Off"


## BowlingGame

Thanks to Keith Nicholas and Julian from hubFS for this example!

module BowlingGame A game of bowling =
open Microsoft.VisualStudio.TestTools.UnitTesting
open FsUnit.MsTest

let (|EndOfGame|IncompleteStrike|Strike|Normal|Other|) (l, frame) =
match l with
| _ when frame = 11            -> EndOfGame(0)
| [10;s]                       -> IncompleteStrike(10+s+s)
| 10::s::n::tail               -> Strike(10+s+n, s::n::tail)
|  f::s::n::tail when f+s = 10 -> Normal(f+s+n,  n::tail)
|  f::s::n::tail               -> Normal(f+s,    n::tail)
| ls                           -> Other(List.fold (+) 0 ls)

let scoreBowls bowls =
let rec scoreBowls' frame l current_score =
let nextframe = scoreBowls' (frame+1)
match (l, frame) with
| EndOfGame(score)        -> current_score + score
| IncompleteStrike(score) -> current_score + score
| Strike(score, l)        -> nextframe l (current_score + score)
| Normal(score, l)        -> nextframe l (current_score + score)
| Other(score)            -> current_score + score
scoreBowls' 1 bowls 0

[<TestMethod>]
let with simple scores should get the expected score. () =
scoreBowls [1;2;3] |> should equal 6

[<TestMethod>]
let with a spare should get the expected score (spare). () =
scoreBowls [2;8;1] |> should equal 12

[<TestMethod>]
let with a strike should get the expected score (strike). () =
scoreBowls [10;1;2] |> should equal 16

[<TestMethod>]
let that is perfect should get a score of 300.() =
scoreBowls [for i in 1..18 -> 10] |> should equal 300

[<TestMethod>]
let with spares in the last frame should get the expected score (spare in last frame). () =
scoreBowls ([for i in 1..18 -> 0] @ [2;8;1]) |> should equal 11

[<TestMethod>]
let with a strike in the last frame should get the expected score (strike in last frame). () =
scoreBowls ([for i in 1..18 -> 0] @ [10;10;1]) |> should equal 21

[<TestMethod>]
let with double strikes should add the score of the first strike to the score of the second. () =
scoreBowls [10;10;1] |> should equal 33

[<TestMethod>]
let that looks like an average bowler's game should get the expected score (example game). () =
scoreBowls [1;4;4;5;6;4;5;5;10;0;1;7;3;6;4;10;2;8;6] |> should equal 133

namespace Microsoft
namespace Microsoft.VisualStudio
namespace Microsoft.VisualStudio.TestTools
namespace Microsoft.VisualStudio.TestTools.UnitTesting
namespace FsUnit
module MsTest

from FsUnit
val GetSumOfMultiplesOf3And5 : max:int -> int
val max : int
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>
module Seq

from Microsoft.FSharp.Collections
val fold : folder:('State -> 'T -> 'State) -> state:'State -> source:seq<'T> -> 'State
val acc : int
val number : int
Multiple items
type TestMethodAttribute =
inherit Attribute
new : unit -> TestMethodAttribute + 1 overload
member DisplayName : string with get, set
member Execute : testMethod:ITestMethod -> TestResult[]

--------------------
TestMethodAttribute() : TestMethodAttribute
TestMethodAttribute(displayName: string) : TestMethodAttribute
val ( When getting sum of multiples of 3 and 5 to a max number of 10 it should return a sum of 23 ) : unit -> unit
val should : f:('a -> #NHamcrest.IMatcher<obj>) -> x:'a -> y:obj -> unit
val equal : expected:'a -> NHamcrest.Core.CustomMatcher<obj>
Multiple items
type LightBulb =
new : state:bool -> LightBulb
override ToString : unit -> string
member On : bool

--------------------
new : state:bool -> LightBulb
val state : bool
val x : LightBulb
property LightBulb.On: bool with get
Multiple items
type TestClassAttribute =
inherit Attribute
new : unit -> TestClassAttribute
member GetTestMethodAttribute : testMethodAttribute:TestMethodAttribute -> TestMethodAttribute

--------------------
TestClassAttribute() : TestClassAttribute
val lightBulb : LightBulb
val x : Given a LightBulb that has had its state set to true
val be : ('a -> 'a)
val True : NHamcrest.Core.CustomMatcher<obj>
Multiple items
val string : value:'T -> string

--------------------
type string = System.String
val x : Given a LightBulb that has had its state set to false
val False : NHamcrest.Core.CustomMatcher<obj>
val l : int list
val frame : int
val s : int
val n : int
val tail : int list
val f : int
val ls : int list
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
| ( [] )
| ( :: ) of Head: 'T * Tail: 'T list
interface IReadOnlyList<'T>
interface IReadOnlyCollection<'T>
interface IEnumerable
interface IEnumerable<'T>
member GetSlice : startIndex:int option * endIndex:int option -> 'T list
member Head : 'T
member IsEmpty : bool
member Item : index:int -> 'T with get
member Length : int
member Tail : 'T list
...
val fold : folder:('State -> 'T -> 'State) -> state:'State -> list:'T list -> 'State
val scoreBowls : bowls:int list -> int
val bowls : int list
val scoreBowls' : (int -> int list -> int -> int)
val current_score : int
val nextframe : (int list -> int -> int)
active recognizer EndOfGame: int list * int -> Choice<int,int,(int * int list),(int * int list),int>
val score : int
active recognizer IncompleteStrike: int list * int -> Choice<int,int,(int * int list),(int * int list),int>
active recognizer Strike: int list * int -> Choice<int,int,(int * int list),(int * int list),int>
active recognizer Normal: int list * int -> Choice<int,int,(int * int list),(int * int list),int>
active recognizer Other: int list * int -> Choice<int,int,(int * int list),(int * int list),int>
val ( with simple scores should get the expected score. ) : unit -> unit
val ( with a spare should get the expected score (spare). ) : unit -> unit
val ( with a strike should get the expected score (strike). ) : unit -> unit
val ( that is perfect should get a score of 300. ) : unit -> unit
val i : int
val ( with spares in the last frame should get the expected score (spare in last frame). ) : unit -> unit
val ( with a strike in the last frame should get the expected score (strike in last frame). ) : unit -> unit
val ( with double strikes should add the score of the first strike to the score of the second. ) : unit -> unit
val ( that looks like an average bowler's game should get the expected score (example game). ) : unit -> unit 