Archive
Stateful testing in FsCheck
FsCheck is a great test library for F#. I wrote about it back in 2011 but I really think it deserves more attention than it gets so here is another note. The documentation of FsCheck gives an example of stateful testing and I had a look at it today. Here is my slightly extended example for testing the StringBuilder class in .NET fx.
- open FsCheck
- open System.Text
- let spec =
- let appendString (s : string) =
- { new Commands.ICommand<StringBuilder,string>() with
- member x.RunActual c = c.Append(s)
- member x.RunModel m = m + s
- member x.Post (c,m) = m = c.ToString()
- override x.ToString() = sprintf "appendString(%A)" s
- }
- let appendInt (i : int) =
- {
- new Commands.ICommand<StringBuilder,string>() with
- member x.RunActual c = c.Append(i)
- member x.RunModel m = m + i.ToString()
- member x.Post (c,m) = m = c.ToString()
- override x.ToString() = sprintf "appendInt(%A)" i
- }
- { new Commands.ISpecification<StringBuilder,string> with
- member x.Initial() = (new StringBuilder(), "")
- member x.GenCommand _ =
- let stringGen = gen {
- let! s = Arb.generate<string>
- return appendString s
- }
- let intGen = gen {
- let! i = Arb.generate<int>
- return appendInt i
- }
- Gen.oneof [ stringGen; intGen ]
- }
- Check.Verbose (Commands.asProperty spec);;
Thanks to Paste as Visual studio code plugin to live writer.
Database access from Powershell
System.Data.DataSet is a useful class when retrieving and persisting data to and from a database. However, I have a hard time to remember the syntax. Each time I google for help and struggle a bit to get it working. That seems unnecessary so I created a utility function that opens a database connection and adds two script methods to it; GetDataSet and SaveDataSet.
The code is available a gist.github.com for my, and hopefully others, convenience.