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 ?SUCHTHAT macro

The macro works in a similar manner as ?LET. The TypeGenerator is bound to InstanceOfType, which can then be used in BooleanExp. However, unlike ?SUCHTHAT macros, ?LET requires that BooleanExp 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 code:

Get hands-on with 1400+ tech skills courses.