Generators

We have had a detailed discussion of properties, but haven't mentioned yet where the input data for these properties comes from. Let's correct this omission and give generators the care they deserve.

The idea of the generator comes from the general concept of types. In a sense, a type is a specification of possible values complying to that type. In other words, types describe the rules that values must comply to. These rules give us the possibility to generate ranges of data values for given types.

For some types there are more values; for others, there are less. As we already know, there are literal types which contain a single value. The same applies for Unit type with its () value. For Boolean, there are two values that exist: true and false. Two values would also exist for an imaginary equality relation type—equal and non-equal. With the same principle, we can say that full ordering takes one of three values: less than, equal, or greater than.

Properties defined in terms of types with such limited sets of possible values are called provable. This is because it is possible to try out all values of a given type (or combinations, if there are multiple parameters) and prove that the program is correct for all possible inputs.

The other type of properties are falsifiable properties. It is not possible (or does not make sense) to try out all possible values of input parameters, hence it is only possible to tell that the functionality under test works for some subset of all inputs.

To make falsifiable properties more trustworthy, existing ScalaCheck generators for Byte, Short, Int, and Long place additional weight on zero, +1, -1, and both minValue and maxValue for the type.

Let's take a look at which generators are included in the ScalaCheck and how we can use them to create new generators for the data types specific for our code. We'll also briefly touch the topic of gradually reducing the test data for failing cases known as shrinking.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.175.253