Re-centering with shrink

Learn how the shrink macro works, and when to use it to better understand the failing cases of programs.

We'll cover the following

shrink

shrink is conceptually the simplest of the two macros that can be used to impact shrinking. It is best used to pick a custom zero-point toward which PropEr will try to shrink data. We can do this mainly by giving the framework a normal generator for normal cases, and then suggesting it uses other simpler generators whenever an error is discovered.

The macro takes the form shrink(default_generator, [alternative_generators]). The default_generator will be used for all passing tests. Once a property fails, however, shrink lets us tell PropEr that any of the alternative generators in the list are interesting ways to get simpler relevant data. We can give hints about how the framework should search for failures. And if the alternative generators are not fruitful, so be it. The shrinking will continue in other ways until no progress can be made.

To make things practical, if we’re generating timestamps or dates, we may be interested in including years from 0 to 9999 to make sure we cover all kinds of weird cases. But if we know that the underlying implementation of our system uses Unix timestamps, then we should consider that its epoch, or starting time, is on January 1, 1970. Since January 1, 1970, is the underlying system’s zero value, picking 1970 as a shrinking target makes more sense than the literal year 0.

Get hands-on with 1200+ tech skills courses.