2.5 Testing it
Page 6 of 9
2.5.1 One example per function
Every function of the slip gives back a value, so each can have an example. Eight of the nine have one, in the form chapter 1 showed; closing always gives back the same text, which the run shows. The form is do a call, then expect result == the exact value. An example may expect a number as well as a text:
example "a line costs its quantity times the unit price"
do line_cents(2, cents: 1990)
expect result == 3980
example "a line of zero units costs nothing"
do line_cents(0, cents: 1990)
expect result == 0Chapter 14 covers examples in full.
2.5.2 The edges of a computation
A computation can be right for the sample order and wrong elsewhere, so its examples try the edges too:
boxeshas three: an odd number of units, 3, which needs the rounding up; an even number, 4, which must not get a box too many; and 0, no units and no box.- "a line of zero units costs nothing" covers a case the sample order never reaches.
- "an item line is indented and starts with the quantity" pins the two leading spaces, which are easy to lose in an edit and hard to see in the output.
An example that states the whole expected text catches a change of one character.
2.5.3 Running the slip's examples
In the day-to-day directory, una test . prints:
example slip "the title names the order" ... ok
example slip "the address line holds the customer number and the email" ... ok
example slip "an item line is indented and starts with the quantity" ... ok
example slip "a line costs its quantity times the unit price" ... ok
example slip "a line of zero units costs nothing" ... ok
example slip "an odd number of units needs one box more than half of them" ... ok
example slip "an even number of units fills every box" ... ok
example slip "no units need no box" ... ok
example slip "the count line gives units and lines" ... ok
example slip "the box line gives the number of boxes" ... ok
example slip "the total is in cents" ... ok
11 examples: 11 passed, 0 failedEach example takes one line of the report, so when one fails, the report names the line of the slip that changed.
2.5.4 What the examples leave out: main
The examples do not call main. It only names values and prints what the functions give back, and the functions are what the examples cover. Most Una programs take this shape: a short main that prints, and functions with no uses line that hold the computations and their examples.
Next:2.6 Common mistakes