Last active
August 29, 2015 14:07
-
-
Save Gab-km/86dd730a8d8e407a699f 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 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" |
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
https://gist.github.com/pocketberserker/40006f8da7e195924713