Skip to content

Instantly share code, notes, and snippets.

@pocketberserker
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pocketberserker/40006f8da7e195924713 to your computer and use it in GitHub Desktop.
Save pocketberserker/40006f8da7e195924713 to your computer and use it in GitHub Desktop.
type NonEmptyList<'T> = 'T * 'T list
type Case<'T> =
| Unit
| Value of 'T
type TestResult<'T> =
| Success of 'T
| Failure of NonEmptyList<string>
type TestBuilder() =
member this.Return(()) = Success ()
member this.ReturnFrom(x, _) = x
member this.Source(x: TestResult<unit>) = (x, Unit)
member this.Source<'T>(x: TestResult<'T>) = (x, Value (Unchecked.defaultof<'T>))
member this.Bind(x, f: _ -> TestResult<_>) =
match x with
| (Success x, _) -> f x
| (Failure xs, Unit) -> Failure xs
| (Failure (res1, rest1), Value v) ->
match f v with
| Success _ -> Failure (res1, rest1)
| Failure (res2, rest2) -> Failure (res1, rest1@(res2::rest2))
member this.Delay(f: unit -> TestResult<_>) = f
member this.Run(f) =
// なんとなくプリントしてるだけ
// 他フレームワークに組み込みたいならコンストラクタでRunnerを受け取り、
// 例外なげるなりなんなり
f () |> printfn "%A"
let test = TestBuilder()
let assertEqualsWith v x y =
if x = y then Success v
else Failure (sprintf "Expect: %A\nActual: %A" x y, [])
let check x y = assertEqualsWith true x y
let assertEquals x y = assertEqualsWith () x y
let example1 = test {
let! _ = check 1 2
let! _ = check 2 3
let! _ = check 2 2
return! check 3 4
}
(*
Failure ("Expect: 1
Actual: 2", ["Expect: 2
Actual: 3"; "Expect: 3
Actual: 4"])
val it : unit = ()
*)
let example2 = test {
do! assertEquals 1 2
do! assertEquals 2 3
do! assertEquals 2 2
do! assertEquals 3 4
}
(*
Failure ("Expect: 1
Actual: 2", [])
*)
@pocketberserker
Copy link
Author

https://gist.github.com/bleis-tift/a7a78d8122b99c083f1a を参考にがんばってみました

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment