Search⌘ K
AI Features

Generating Specials

Explore how to generate lists of specials and regular items using custom recursive data generators in PropEr. Understand how to design generators that produce valid and varied test cases, including handling special price rules and ensuring realistic item lists. By mastering this technique, you will improve testing accuracy and detect edge cases in property-based testing with Erlang.

Generating the list of specials

We’ll start with the list of specials since it’s conceptually simpler and a prerequisite of other generators. The method here is to first extract the list of all item names from the price list, and then create a matching table of specials. Let’s take a look at how it can be done.

special_list(PriceList) ->
    Items = [Name || {Name, _} <- PriceList],
    ?LET(Specials, list({elements(Items), choose(2,5), integer()}),
         lists:ukeysort(1, Specials)). % no dupes

The generated list of specials contains the name of the item, how many of them are required for the special to apply, and then the price for the whole group. The specials are generated from randomly selected entries ...