Lawful-classes-*

Many classes we define come with laws defining how functions provided by instances of the class ought to behave, or interact among one another. These can be declared in documentation, but it’s always nice to test them. Even more, wouldn’t it be nice to define these tests next to the class definition, for them to be reused in tests of instances?

Of course, we don’t want to add arbitrary test framework or exemplar generation dependencies to any library which defines some class. Also, tests may want to discard some inputs, but to achieve this, a dependency on QuickCheck or similar may not be desired.

Luckily, we can get pretty far with pure base types and functions, which is what the lawful-classes packages bring: a way to define laws using nothing but base, and then validate instances against such sets of laws using Hedgehog or QuickCheck in a Tasty environment (though it doesn’t depend on Tasty, could easily be used with other test runners).

The result is at GitHub - NicolasT/lawful-classes: Classes with laws lead to more lawful instances with some example code in the README. The resulting packages are lawful-classes-types (some type aliases and super simple utility functions, not required as a dependency to define laws), lawful-classes-hedgehog and lawful-classes-quickcheck.

Hope this is useful to some of you at some point.

3 Likes