Skip to content

Instantly share code, notes, and snippets.

@pocketberserker
Created December 20, 2012 04:30
Show Gist options
  • Save pocketberserker/4342971 to your computer and use it in GitHub Desktop.
Save pocketberserker/4342971 to your computer and use it in GitHub Desktop.
NaturalSpecとunquoteを併用したら出力がリッチにならないかな、と思ったなれの果て
// NaturalSpec: https://github.com/forki/NaturalSpec
// unquote: http://code.google.com/p/unquote/
open NaturalSpec.Utils
open NaturalSpec
open NUnit.Framework
open Swensen.Unquote
let check (assertType,a,b,value) =
let outputFail f message =
let s =
f
|> reduceFully
|> List.map decompile
|> List.fold (fun s x -> s + sprintf "%s\r\n" x) message
s |> toSpec
s |> Assert.Fail
let evalA = eval a
let evalB = eval b
match assertType with
| Equality -> if evalA <> evalB then outputFail <@ %b = %a @> "\r\nElements are not equal.\r\n"
| Inequality -> if evalA = evalB then outputFail <@ %b <> %a @> "\r\nElements should not be equal.\r\n"
| IsTrue -> test <@ %b = %a @>
| IsFalse -> test <@ %b <> %a @>
value
let should f x y =
toSpec "should "
f <@ x @> y |> check
let shouldn't f x y =
toSpec "sholdn't "
f <@ x @> y |> not' |> check
let have (f:Quotations.Expr<'a -> bool>) value =
toSpec "have "
IsTrue,<@ true @>,<@ value |> %f @>,value
let removing x seq = <@ Seq.filter ((<>) x) seq @>
let length n seq =
printMethod n
let l = seq |> eval |> Seq.length
if n <> l then
<@ %seq |> Seq.length = n @>
|> reduceFully
|> List.map decompile
|> List.fold (fun s x -> s + sprintf "%s\r\n" x) ""
|> Assert.Fail
true
let contain x (seq:Quotations.Expr<'a seq>) =
let x = eval x
printMethod x
IsTrue,<@ true @>,<@ Seq.exists ((=) x) %seq @>,seq
let duplicates (seq:Quotations.Expr<'a seq>) =
printMethod ""
let dict = new System.Collections.Generic.HashSet<'a>()
seq |> eval |> Seq.fold (fun d i -> d || (not (dict.Add i))) false
[<Scenario>]
let ``divide test``() =
Given (1,1)
||> When (fun x y -> <@ x / y @>)
|> It should equal 2
|> Verify
[<Scenario>]
let ``list test``() =
Given [1;2;3;4;5]
|> When removing 3
|> It shouldn't contain 3
|> It should contain 4
|> It should have (length 4)
|> It shouldn't have duplicates
|> Verify
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment