Skip to content

Instantly share code, notes, and snippets.

@lepinay
Last active August 29, 2015 14:16
Show Gist options
  • Save lepinay/90836b560b8da7f6df2d to your computer and use it in GitHub Desktop.
Save lepinay/90836b560b8da7f6df2d to your computer and use it in GitHub Desktop.
Unit testing in F# interactive
open System
open System.IO
Environment.CurrentDirectory <- __SOURCE_DIRECTORY__
if not (File.Exists "paket.exe") then
let url = "https://github.com/fsprojects/Paket/releases/download/0.26.3/paket.exe"
use wc = new Net.WebClient() in let tmp = Path.GetTempFileName() in wc.DownloadFile(url, tmp); File.Move(tmp,Path.GetFileName url)
#r "paket.exe"
Paket.Dependencies.Install """
source https://nuget.org/api/v2
nuget FsUnit
nuget FsUnit.Xunit
nuget XUnit
""";;
#r @"packages\xunit\lib\net20\Xunit.dll"
#r @"packages\FsUnit.xUnit\Lib\net40\NHamcrest.dll"
#r @"packages\FsUnit.xUnit\Lib\net40\FsUnit.CustomMatchers.dll"
#r @"packages\FsUnit.xUnit\Lib\net40\FsUnit.Xunit.dll"
open FsUnit.Xunit
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
scoreBowls [1;2;3] |> should equal 6
scoreBowls [2;8;1] |> should equal 12
scoreBowls [10;1;2] |> should equal 16
scoreBowls [for i in 1..18 -> 10] |> should equal 300
scoreBowls ([for i in 1..18 -> 0] @ [2;8;1]) |> should equal 11
scoreBowls ([for i in 1..18 -> 0] @ [10;10;1]) |> should equal 21
scoreBowls [10;10;1] |> should equal 33
scoreBowls [1;4;4;5;6;4;5;5;10;0;1;7;3;6;4;10;2;8;6] |> should equal 133
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment