Skip to content

Instantly share code, notes, and snippets.

@bleis-tift
Forked from Gab-km/TestBuilder.fs
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 bleis-tift/a7a78d8122b99c083f1a to your computer and use it in GitHub Desktop.
Save bleis-tift/a7a78d8122b99c083f1a to your computer and use it in GitHub Desktop.
type NonEmptyList<'T> = 'T * 'T list
type AssertResult<'T> =
| Failure of NonEmptyList<string>
| Success of 'T
type TestBuilder(description: string) =
// これを解除するとコンパイルエラーになってしまう・・・
// member __.Bind(x, f) =
// match x with
// | Success res -> f res
// | Failure res -> Failure res
member __.Bind(x, f) =
match x with
| Success () -> f ()
| Failure (res1, rest1) ->
match f () with
| Success _ -> Failure (res1, rest1)
| Failure (res2, rest2) ->
Failure (res1, rest1@(res2::rest2))
member __.Return x = Success x
member __.Zero () = Success ()
let assertEquals x y =
if x = y then Success ()
else Failure (sprintf "Expect: %A\nActual: %A" x y, [])
let test description = TestBuilder(description)
test "hogehoge" {
do! assertEquals 1 2
do! assertEquals 2 3
do! assertEquals 2 2
do! assertEquals 3 4
}
(* => val it : AssertResult<unit> =
Failure
("Expect: 1
Actual: 2", ["Expect: 2
Actual: 3"; "Expect: 3
Actual: 4"]) *)
@bleis-tift
Copy link
Author

BindじゃなくてCombineに頼る方向性のほうがやっぱりいいのかも?

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