Imposing Restrictions

Learn the importance of imposing restrictions and their advantages over transformations.

Why impose restrictions?

A common trait of all default generators is that they’re pretty broad in the data they generate, and from time to time, we’ll want to exclude specific counterexamples. In fact, we already needed to do that when we used the non_empty() generator to remove empty lists or binaries from the generated data set. Such a filter generator can be implemented with the ?SUCHTHAT(InstanceOfType, TypeGenerator, BooleanExp) macro.

The such_that macro

The macro works in a similar manner as let. The type_generator is bound to instance_of_type, which can then be used in boolean_exp. However, unlike ?such_that macros, ?LET requires that boolean_exp be a boolean expression, returning true or false. If the value is true, the data generated is kept and allowed to go through. If the value is false, the data is prevented from being passed to the test. The non_empty() filter can in fact be implemented as the following in the following code:

Get hands-on with 1400+ tech skills courses.