Search⌘ K

Implementing Specials

Explore how to implement specials within property-based testing by counting items, applying special pricing rules, and calculating totals in Erlang. Understand helper functions and their roles, and see how to test the implementation with coverage and negative testing strategies.

The implementation

A simple method is to count how many of each item is in the list. Account for the specials first, reducing the count every time the specials apply, and then run over the list of items that are not on sale.

-type item() :: string().
-type price() :: integer().
-type special() :: {item(), pos_integer(), price()}.

-spec total([item()], [{item(), price()}], [special()]) -> price(). 
total(ItemList, PriceList, Specials) ->
    Counts = count_seen(ItemList),
    {CountsLeft, Prices} = apply_specials(Counts, Specials), 
    Prices + apply_regular(CountsLeft, PriceList).

Here, ...