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 Case =
| Value
| Unit
type TestResult<'T> =
| Success of 'T
| Failure of 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)
member this.Bind(x, f: 'T -> TestResult<'U>) =
match x with
| Success x, _ -> f x
| Failure x, Unit -> Failure x
| Failure x, Value -> Failure x // AllResult [ f ? |> fst |> TestResult.cast; Failure x ]
member this.Delay(f: unit -> TestResult<_>) = f
member this.Run(f) =
// なんとなくプリントしてるだけ
// 他フレームワークに組み込みたいならコンストラクタでRunnerを受け取り、
// 例外なげるなりなんなり
f () |> printfn "%A"
let test = TestBuilder()
let example1 = test {
return! Success 1
}
let example2 = test {
do! Success ()
}
let example3 = test {
do! Failure "test3"
return! Success 2
}
// 失敗した後に使う引数がTestResultにないからまだ無理
let example4 = test {
let! _ = Failure "test4"
return! Success 2
}
let example5 = test {
do! Success ()
return! Success 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