Recursive Generators
Explore how to build recursive generators using PropEr in Elixir, enabling the creation of complex data structures like paths on a grid. Understand how to control generator size dynamically, avoid infinite loops, and improve property-based tests by generating scalable, realistic test inputs.
We'll cover the following...
Example
Let’s take a look at an example.
Let’s say we have a robot and want to give it directions. We might want to test things such as whether it can go through a room without hitting obstacles, whether it can cover all areas, and so on. But before we can test our bot, we’ll need to be able to create and plan an itinerary for it. Let’s say our robot works on a grid with coordinates and can go left, right, up, or down. A simple generator might look like this:
def path(), do: list(oneof([:left, :right, :up, :down]))
This would be fine if we wanted to eliminate things like “going left then right” or “going up then down” where moves cancel each other out. A let would probably work as well. We’d just need to scan the list, and every time two opposite directions are taken, drop them from the list.
But what if we wanted to generate a path such ...