Home / Learn / 11 Packages and modules
11.5 Testing it
Page 6 of 9
11.5.1 One command for every module
Each module carries the examples of its own functions, and una test . runs every example of every module, module by module. It checks the whole package first: when any module has a diagnostic, it reports them all and runs no example, since the package as a whole is what the examples test.
$ una test .
example store.invoice "a known SKU is found" ... ok
example store.invoice "a line shows the members' price of one unit" ... ok
example store.invoice "an unknown SKU is UnknownSku" ... ok
example store.money "two digits after the point" ... ok
example store.money "a leading zero is kept" ... ok
example store.money "a refund keeps its sign" ... ok
example store.pricing "members pay 10 percent less" ... ok
example store.pricing "the discount rounds down" ... ok
8 examples: 8 passed, 0 failedEach line names the module, then the example. Two modules may use the same example name, since the module tells them apart; within one module, each name is used once (U1002).
11.5.2 Examples of private functions
An example sits in the module whose function it tests, so it can call the module's private functions too: "the discount rounds down" tests discount, which no other module can see. An example is never pub: it belongs to its module.
11.5.3 Examples that bring their own catalog
Two of the invoice's examples look products up, and each passes the catalog it needs, as a list:
example "a known SKU is found"
do product_of(OrderLine(sku: "LAMP-01", quantity: 2), catalog: [Product(sku: "LAMP-01", name: "Desk lamp", cents: 1990)])
expect result.name == "Desk lamp"
example "a line shows the members' price of one unit"
do describe(OrderLine(sku: "LAMP-01", quantity: 2), product: Product(sku: "LAMP-01", name: "Desk lamp", cents: 1990))
expect result == "2 x Desk lamp at 19.90 (members 17.91): 39.80"
example "an unknown SKU is UnknownSku"
do product_of(OrderLine(sku: "CHAIR-09", quantity: 1), catalog: [])
expect fails UnknownSkuThe first example passes a catalog of one product and expects it back; the third passes the empty list, [], so every SKU is unknown, and expects the failure, expect fails UnknownSku, as section 7.8 showed. Since the catalog is a parameter, an example needs nothing but values. The Product it builds is the record that store.invoice imports from store.catalog, so the examples use the same import as the code. When a function uses a service outside the program in place of a parameter, an example gives it a stand-in for that service, which section 14.3 shows.
11.5.4 An entry module with no examples
store.app has no examples of its own. Everything it does is a call to a function that another module tests. An example of main would need a stand-in for the console (section 14.3), and would only show that something was printed. Keep an entry module this thin: building the values, passing them to functions, and printing what they return. The less it decides, the less goes untested.
Next:11.6 Common mistakes