Home / Learn / 6 Functions and their results
6.5 Testing it
Page 6 of 9
6.5.1 Each rule at its edges
Each example calls one pure function and states its result, in the form of section 1.7. The first ones test the rates at their edges: the last weight at the lowest rate, the first weights of a started kilogram above it, and the far zone:
example "a parcel up to 2 kg ships at the lowest rate"
do base_rate(2_000)
expect result == 790
example "each started kilogram above 2 kg adds 250 cents"
do base_rate(3_200)
expect result == 1_290
example "the rest of the world adds 1800 cents"
do surcharge(World)
expect result == 1_8006.5.2 Results built from other results
The quotes are the results the report depends on. One example ships a large order free within the country; the next sends the same order to a neighboring country, where it pays 6890 cents: 6290 for 24 kg and 600 for the zone. Each example builds its order in place, as the argument of quote:
example "a large order within the country ships free"
do quote(Order(number: 1, goods: 15_900, parcel: Parcel(grams: 24_000, zone: Home)))
expect result == 0
example "a large order abroad pays shipping"
do quote(Order(number: 2, goods: 15_900, parcel: Parcel(grams: 24_000, zone: Neighbor)))
expect result == 6_8906.5.3 The offer and the report
The weekend offer has an example for its rounding, and one for a charge of nothing. money and report_line each have one for the case that is easiest to get wrong, an amount with a zero in its cents and an order that ships free:
example "the weekend offer rounds up to a whole cent"
do weekend_price(3_091)
expect result == 1_546
example "the weekend offer of nothing is nothing"
do weekend_price(0)
expect result == 0
example "an amount keeps its zero cents"
do money(705)
expect result == "7.05"
example "a free order says so"
do report_line(Order(number: 1043, goods: 15_900, parcel: Parcel(grams: 24_000, zone: Home)), charge: 0)
expect result == "order 1043: ships free"The examples and the ensures clauses do different jobs. ensures result >= 0 and result <= charge is proved for every charge, but it does not say which value weekend_price gives back. The examples fix exact values, 1546 and 0, for two cases. The examples leave out show and main, the two functions that print: they only print what the tested functions give back.
6.5.4 Running the examples
una test . prints:
example shipping "a parcel up to 2 kg ships at the lowest rate" ... ok
example shipping "each started kilogram above 2 kg adds 250 cents" ... ok
example shipping "the rest of the world adds 1800 cents" ... ok
example shipping "a large order within the country ships free" ... ok
example shipping "a large order abroad pays shipping" ... ok
example shipping "the weekend offer rounds up to a whole cent" ... ok
example shipping "the weekend offer of nothing is nothing" ... ok
example shipping "an amount keeps its zero cents" ... ok
example shipping "a free order says so" ... ok
9 examples: 9 passed, 0 failedNext:6.6 Common mistakes