Regex-constrained strings with FsCheck
25 September 2015
Source code available on GitHub.
In Haskell, there’s the quickcheck-regex package, by Audrey (唐鳳) Tang, which allows to write and execute this:
It exports a
matching function that turns a Regular Expression into a DFA/NFA finite-state machine and then into a generator of strings matching that regex:
In F#, a similar generator for FsCheck can be written as:
matching function uses the .NET port of dk.brics.automaton and xeger, and has the signature:
Use in F# Interactive
Here’s a way to generate regex-constrained strings with FsCheck in F# Interactive:
Notice how FsCheck yields different results on each run:
matching takes the size of generated test data into account.
Use in FsCheck with xUnit.net
Finally, here’s a quantified property scenario using FsCheck with xUnit.net: