Home / Learn / 3 Text and conditions

Release v0.4.0Edition 0.2

3.6 Testing it

Page 7 of 10

3.6.1 Examples for each rule

Every function of the program but main gives back a value and prints nothing, so each one can have examples, in the form of section 1.7: do a call, then expect what must hold. The examples aim at the edges, where values go wrong:

day-to-day/src/labels.unathe first five examplesuna
example "a SKU loses its spaces and is written in capitals"
  do clean_sku("  lamp-01 ")
  expect result == "LAMP-01"

example "a SKU with a dash inside it looks right"
  do valid_sku("RUG-07")
  expect result

example "a SKU without a dash does not"
  do valid_sku("MUG 4")
  expect not result

example "a SKU that ends with its dash does not either"
  do valid_sku("LAMP-")
  expect not result

example "a long name is cut to 24 characters"
  do fit_name("Wool rug, 120 by 180 cm, natural")
  expect result.len == 24

The other examples cover the rest: a price whose cents are under ten, a discount over 90 percent, the rounding of a sale, a product not sold by weight, a sale's price text, an empty shelf, and a label for a SKU that does not look right.

3.6.2 expect takes a condition

The line after expect is a condition, a Bool, and the example passes when it is true. expect result == "LAMP-01" is a comparison, as in chapter 1. When the function itself gives a Bool, the result is already the condition: expect result says that valid_sku("RUG-07") must be true, and expect not result that valid_sku("MUG 4") must be false. Write them this way, not expect result == true, as you write domestic and not domestic == true.

3.6.3 Running the examples

In the day-to-day directory, una test . prints:

Output
example labels "a SKU loses its spaces and is written in capitals" ... ok
example labels "a SKU with a dash inside it looks right" ... ok
example labels "a SKU without a dash does not" ... ok
example labels "a SKU that ends with its dash does not either" ... ok
example labels "a long name is cut to 24 characters" ... ok
example labels "a short name is kept as it is" ... ok
example labels "an amount keeps the zero of its cents" ... ok
example labels "a discount over 90 percent is not a sale" ... ok
example labels "a discount rounds down to a whole cent" ... ok
example labels "a product not sold by weight has no price per kilogram" ... ok
example labels "a sale shows the old and the new price" ... ok
example labels "an empty shelf is sold out" ... ok
example labels "a SKU that does not look right asks for a check" ... ok

13 examples: 13 passed, 0 failed

Next:3.7 Common mistakes