Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
テストをコンピュテーション式で書けないか、という思考実験
// コンピュテーション式中で用いる型
type AssertionResult<'T> =
| NotAsserted of 'T
| Success
| Failure of string
// コンピュテーションビルダー
type TestBuilder() =
member self.Bind(x, f) =
match x with
| NotAsserted(y) -> f y
| z -> z
member self.Return(_) = Success
member self.ReturnFrom(x) = x
// アサート用関数
let assertEqual<'T when 'T : equality> (x: 'T) (y: 'T) : AssertionResult<'T> =
if x = y then
Success
else
Failure(System.String.Format("Expect: {0}\nActual: {1}", x, y))
// テストに名前をつけるためのトリック
let test = fun description -> TestBuilder()
// テスト式
test "We can write good information" {
let! x = NotAsserted(1)
let! y = NotAsserted(2)
return! (assertEqual x y)
}
//=> val it : AssertionResult<int> = Failure "Expect: 1
// Actual: 2"
@Gab-km

This comment has been minimized.

Copy link
Owner Author

Gab-km commented Oct 22, 2014

@Gab-km

This comment has been minimized.

Copy link
Owner Author

Gab-km commented Oct 23, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.