Skip to content

Instantly share code, notes, and snippets.

@kos59125
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 kos59125/238d7d67984f87ce106a to your computer and use it in GitHub Desktop.
Save kos59125/238d7d67984f87ce106a to your computer and use it in GitHub Desktop.
テストコンピュテーション式 (https://gist.github.com/Gab-km/86dd730a8d8e407a699f 参考)
type TestResult =
| Success
| Failure of string
type TestState<'a, 'b, 'c> =
| NotRun
| Context of 'a
| Action of 'b
| Response of 'c
| Result of TestResult
type TestBuilder () =
member __.Yield (x) = Context (x)
member __.For (state, _) = state
member __.Zero () = NotRun
[<CustomOperation("Given", AllowIntoPattern = true)>]
member __.Given (_, condition) = Context (condition)
[<CustomOperation("When", AllowIntoPattern = true, MaintainsVariableSpace = true)>]
member __.When (state, [<ProjectionParameter>] f) =
match state with
| Context (x) -> Action (f x)
| _ -> failwith "No condition is given"
[<CustomOperation("Then", MaintainsVariableSpace = true)>]
member __.Then (state, [<ProjectionParameter>] f) =
match state with
| Action (x) -> Response (f x)
| _ -> failwith "No condition is given"
[<CustomOperation("ShouldEqual")>]
member __.ShouldEqual (state, expected) =
match state with
| Response (actual) -> Result (if actual = expected then Success else Failure (sprintf "Expected %A, but given %A" expected actual))
| _ -> failwith "No condition is given"
let test = TestBuilder ()
// Result Success
test {
Given 0 into x
When (x + 1) into y
Then y
ShouldEqual 1
}
|> printfn "%A"
// Result (Failure "Expected 3, but given 2")
test {
Given 1 into x
When (2 * x) into y
Then y
ShouldEqual 3
}
|> printfn "%A"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment