18.7 Testing it
Page 8 of 11
18.7.1 Values worked out by hand
The examples cover the price and the tag: a normal discount, the stop at cost, the formatting of cents, and a SKU the catalog does not list, each with a value worked out by hand. The last one installs the catalog with given, as main does with with.
example "a sale takes the percent off the price"
do sale_price(Product(sku: "LAMP-01", price: 1990, cost: 1200), off: 30)
expect result == 1393
example "a sale price stops at cost"
do sale_price(Product(sku: "DESK-02", price: 12900, cost: 11000), off: 30)
expect result == 11000
example "cents are shown with two digits"
do shown(1905)
expect result == "19.05"
example "a SKU the catalog does not list gets no price"
given @catalog = listed(sample())
do tag("CHAIR-09", off: 30)
expect result == "CHAIR-09 not in the catalog"18.7.2 Only the handlers an example installs
una test . runs each example in turn, with only the handlers its given lines install: an example can never reach the real catalog, or the console, by accident. With --json, each example in the document also carries its trace, the operations its do line performed; each expect performed line checks that one operation is in it.
18.7.3 What an example checks that a proof does not
An example and a proof check different things. The verifier proves that sale_price is never below cost, for every product and every percent. The examples check that the numbers are the ones a person expects: that 30 percent off 19.90 is 13.93, which no ensures clause states. A proof says "never below cost"; an example says "13.93". A function with a contract still needs an example of its ordinary case, and a function whose result has no rule to prove, such as shown, needs examples for its edge cases, such as a price with fewer than 10 cents.
18.7.4 Running the examples
una test . prints:
example pricetags "a sale takes the percent off the price" ... ok
example pricetags "a sale price stops at cost" ... ok
example pricetags "cents are shown with two digits" ... ok
example pricetags "a SKU the catalog does not list gets no price" ... ok
4 examples: 4 passed, 0 failedNext:18.8 Common mistakes