4.6 Testing it
Page 7 of 10
The examples are the simplest form of section 1.7: a call, and what it must give back. A sum makes it easy to see what to test, since its variants are listed in one place.
4.6.1 One example per variant
Each function that chooses by a variant has an example for each variant: the three zones, the two speeds, the three deliveries. When a variant is added, a when without an else stops checking, and the list of examples shows the variant that has none yet.
example "the door costs extra"
do delivery_charge(AtDoor)
expect result == Charge(item: "to the door", cents: 250)
example "a locker is free, and named"
do delivery_charge(AtLocker("Station 14"))
expect result == Charge(item: "locker Station 14", cents: 0)
example "a store is free, and named"
do delivery_charge(AtStore("Harbour Road"))
expect result == Charge(item: "store Harbour Road", cents: 0)expect result == Charge(…) builds the charge the call must give back, and compares the two field by field, as == compares any two records.
4.6.2 The edges of the weight bands
The bands of weight_charge meet at 500 and 2000 grams, and the examples sit on both sides of each edge: 500 and 501, 2000 and 2001. A mistake such as < in place of <= moves a parcel into the wrong band at exactly one weight, which only an example at that weight catches.
4.6.3 A field of the result
expect result.cents == 300 reads one field of the result. Use it when only one field matters to the rule, as the price of a zone does. The example for the whole parcel, the total adds the four charges, checks the sum that quote prints.
4.6.4 Running the examples
In the day-to-day directory, una test . checks the package, then runs the examples:
example pricing "each zone has its price" ... ok
example pricing "the nearest zone is the cheapest" ... ok
example pricing "abroad costs the most" ... ok
example pricing "500 g is still the lightest band" ... ok
example pricing "501 g is the next band" ... ok
example pricing "2 kg is the last gram of the middle band" ... ok
example pricing "over 2 kg pays the most" ... ok
example pricing "standard speed is free" ... ok
example pricing "express at home costs 500" ... ok
example pricing "express abroad costs 900" ... ok
example pricing "the door costs extra" ... ok
example pricing "a locker is free, and named" ... ok
example pricing "a store is free, and named" ... ok
example pricing "the total adds the four charges" ... ok
example pricing "express changes the speed and nothing else" ... ok
15 examples: 15 passed, 0 failedmain, quote and charge_line have no examples of their own: the first two print, and the third only writes the line that the tested charges give it.
Next:4.7 Common mistakes