Last active
August 29, 2015 14:07
-
-
Save pocketberserker/40006f8da7e195924713 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type NonEmptyList<'T> = 'T * 'T list | |
type FlowControl = Break | Continue | |
type Case<'T> = | |
| Unit | |
| Value of 'T | |
type TestResult<'T> = | |
| Success of 'T | |
| Failure of NonEmptyList<string> | |
type TestBuilder() = | |
member this.Return(()) = ((Success (), Unit), Break) | |
member this.ReturnFrom(x, _) = (x, Break) | |
member this.YieldFrom(x, _) = (x, Continue) | |
member this.Source(x: TestResult<unit>) = ((x, Unit), Continue) | |
member this.Source<'T>(x: TestResult<'T>) = ((x, Value Unchecked.defaultof<'T>), Continue) | |
member this.Bind((x, cont), f: _ -> (TestResult<_> * Case<_>) * FlowControl) = | |
match x with | |
| (Success x, _) -> f x | |
| (Failure xs, Unit) -> ((Failure xs, Unit), cont) | |
| (Failure (res1, rest1), Value v) -> | |
match f v with | |
| ((Success _, v), cont) -> ((Failure (res1, rest1), v), cont) | |
| ((Failure (res2, rest2), v), cont) -> ((Failure (res1, rest1@(res2::rest2)), v), cont) | |
member this.Combine((x: TestResult<_> * Case<_>, cont), rest: unit -> (TestResult<_> * Case<_>) * FlowControl) = | |
match cont with | |
| Break -> (x, Break) | |
| Continue -> | |
match x with | |
| (Success _, _) -> rest () | |
| (Failure (res1, rest1), Unit) -> ((Failure (res1, rest1), Unit), Continue) | |
| (Failure (res1, rest1), Value _) -> | |
match rest () with | |
| ((Success _, c), x) -> ((Failure (res1, rest1), c), x) | |
| ((Failure (res2, rest2), c), x) -> ((Failure (res1, rest1@(res2::rest2)), c), x) | |
member this.Delay(f: unit -> (TestResult<_> * Case<_>) * FlowControl) = f | |
member this.Run(f) = | |
// なんとなくプリントしてるだけ | |
// 他フレームワークに組み込みたいならコンストラクタでRunnerを受け取り、 | |
// 例外なげるなりなんなり | |
//f () |> fst |> printfn "%A" | |
f () |> fst |> fst |> 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"]) | |
*) | |
let example2 = test { | |
do! assertEquals 1 2 | |
do! assertEquals 2 3 | |
do! assertEquals 2 2 | |
do! assertEquals 3 4 | |
} | |
(* | |
Failure ("Expect: 1 | |
Actual: 2", []) | |
*) | |
let example3 = test { | |
yield! check 1 2 | |
yield! check 2 3 | |
yield! check 2 2 | |
yield! check 3 4 | |
} | |
(* | |
Failure ("Expect: 1 | |
Actual: 2", ["Expect: 2 | |
Actual: 3"; "Expect: 3 | |
Actual: 4"]) | |
*) | |
let example4 = test { | |
return! check 1 2 | |
return! check 2 3 | |
return! check 2 2 | |
return! check 3 4 | |
} | |
(* | |
Failure ("Expect: 1 | |
Actual: 2", []) | |
*) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/bleis-tift/a7a78d8122b99c083f1a を参考にがんばってみました