Search code examples
haskelluuidquickcheck

Arbitrary instance for non-null UUID


I am having trouble creating Arbitrary instance for some record types that have UUID-fields. A smart constructor ensures that the UUID cannot be null. Is there an idiomatic way to modify the UUID Arbitrary instance from Test.QuickCheck.Instances.UUID such that it does not generate null values - that is, the generator should not produce 00000000-0000-0000-0000-000000000000?


Solution

  • The key piece you need is suchThat. When your instance would call arbitrary to get a UUID, call arbitrary `suchThat` (not . Data.UUID.Types.null) instead. Also, if your type defines shrink, when shrinking the UUID, do filter (not . Data.UUID.Types.null) on the list of shrinks.