Skip to content

Instantly share code, notes, and snippets.

@Gab-km
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Gab-km/86dd730a8d8e407a699f to your computer and use it in GitHub Desktop.
Save Gab-km/86dd730a8d8e407a699f to your computer and use it in GitHub Desktop.
テストをコンピュテーション式で書けないか、という思考実験
// コンピュテーション式中で用いる型
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
Copy link
Author

Gab-km commented Oct 22, 2014

@Gab-km
Copy link
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