More on Generation Size
Explore how to dynamically adjust generation size in Elixir property-based testing using StreamData. Understand scaling, capping, and conditional generator selection to create efficient test data tailored to complexity and size constraints. Apply these techniques to practical examples like email domain generation, enhancing your test data variety and relevance.
We'll cover the following...
If we want to apply some modifications to the generation size, we’re looking for the StreamData.scale/2 function. It takes a generator and a function that takes a size and returns a new size, and then it returns a new generator. The new generator uses the generation size returned by the passed function. (That was a mouthful.) Essentially, it modifies the generation size of a generator in a dynamic way, based on the original generation size.
Scaling in generation size
Scaling is especially useful in a couple of scenarios. The first one is when we want to increase or decrease the speed at which the generation size progresses. For example, it might be a good idea to reduce the growth of the generation size for a complex and deeply nested generator. To do that, we can scale the generation size with mathematic functions like square roots:
Executable
Output
iex> generator = StreamData.list_of(StreamData.string(:ascii))
#StreamData<54.17825959/2 in StreamData.list_of/1>
iex> scaled_generator =
...> StreamData.scale(generator, fn size ->
...> round(:math.sqrt(size))
...> end)
#StreamData<76.17825959/2 in StreamData.scale/2>
iex> scaled_generator |> ...