DenseCheck is a companion tool for property based testing frameworks like FsCheck, which help you generate test data to validate properties about your code.
- FsCheck generates random instances of your types to test properties
- DenseCheck systematically constructs functions that creates a simple integer to all possible instances of your function, making the entire set of your type denumerable.
- FsCheck's random generation can miss edge cases or important scenarios in your domain model.
- DenseCheck allows you to thoroughly test smaller value spaces in a systematic way random generation cannot.
DenseCheck is a deterministic domain exploration tool for F# that treats your types as sets of the values they inhabit, providing you an infinite and indexable catalog of values you can enumerate and explore.
Start with a domain model like this simple role based access control system:
type Role =
| Admin
| Editor
| Viewer
type AccessRule =
| HasRole of Role
| And of AccessRule * AccessRule
| Or of AccessRule * AccessRule
| Not of AccessRule let getAccessRule = DenseCheck.getIndex<AccessRule> () let terms = [ for i in 0I .. 29I -> getAccessRule i ]
// It will return a list of terms like this:
let expected =
[ HasRole Admin
And(HasRole Admin, HasRole Admin)
Or(HasRole Admin, HasRole Admin)
Not(HasRole Admin)
HasRole Editor
And(HasRole Admin, And(HasRole Admin, HasRole Admin))
Or(HasRole Admin, And(HasRole Admin, HasRole Admin))
Not(And(HasRole Admin, HasRole Admin))
HasRole Viewer
And(And(HasRole Admin, HasRole Admin), HasRole Admin) ] let bigRule = getAccessRule 123987123098123987324987234I
// bigRule will be something like this:
let expected =
Or(
Or(
Or(
Or(HasRole Editor, Not(HasRole Admin)),
Not(Not(And(Or(HasRole Admin, HasRole Admin), Or(HasRole Admin, HasRole Admin))))
),
Not(Or(Or(Not(And(HasRole Admin, HasRole Admin)), HasRole Editor), HasRole Viewer))
),
Or(
Not(Or(HasRole Viewer, HasRole Viewer)),
Or(
And(And(HasRole Admin, And(HasRole Admin, HasRole Admin)), And(HasRole Admin, HasRole Admin)),
Or(Not(Or(HasRole Admin, HasRole Admin)), And(HasRole Admin, HasRole Admin))
)
)
) This is all well and good, but domain models always feel abstract. It would be nice if we could quickly generate some concrete examples of our AccessRule to see what sorts of scenarios our model allows.
Instead of using FsCheck random generation or property based testing, we can use the Instance Catalog to enumerate all the possible AccessRule instances.