Home / Learn / 1 Hello, Una
1.7 Testing it
Page 8 of 11
1.7.1 What an example is
An example is a check that lives next to the code: it calls a function and says what the call must give back. una test . runs every example of the package and reports each one. The morning notices have one example per notice:
example "the notices start with the store's name"
do store_name()
expect result == "Harbor Street store"
example "the address gives the street"
do store_address()
expect result == "12 Harbor Street, by the ferry"Each example has three lines:
example "…". The keywordexampleand a sentence that says what the example shows. It names the example in the report.do greeting(). The call to make, indented two spaces.expect result == "Good morning". What must hold afterwards:resultstands for what the call gave back, and==means "is equal to".
This is the simplest form of an example. Chapter 14 covers examples in full.
1.7.2 One example per notice
Every function that gives back a text has an example that states the exact text. An example of the whole text catches a change of one character, such as a missing space, which is easy to miss when you read the output.
1.7.3 Running the examples
In the day-to-day directory, una test . checks the package first, then runs the examples:
example morning "the notices start with the store's name" ... ok
example morning "the address gives the street" ... ok
example morning "the greeting is for the morning" ... ok
example morning "the hours give the opening and the closing time" ... ok
example morning "the cutoff names the hour" ... ok
example morning "the offer names what is on sale" ... ok
example morning "the café keeps its own hours" ... ok
example morning "the help desk says where to go" ... ok
8 examples: 8 passed, 0 failedEach example takes one line of the report. When an example fails, its line says FAILED, with the line of the expect and the value result had; the exercises of Exercises start from such a report.
1.7.4 What the examples leave out: the printing functions
The examples do not call main, section or rule. These functions print, and an example of a function that prints needs a stand-in for the console, which chapter 14 shows. The printing functions here only print what the tested functions give back, so the examples cover what can go wrong: the text of each notice.
Next:1.8 Common mistakes