Home / Learn / 13 Limits on authority
13.5 Testing it
Page 6 of 9
13.5.1 The price rule, at its edges
sale_price is pure, so its tests are the simple ones of chapter 1, written test in edition 0.3: a call and its result. They cover an ordinary discount, the two ends of Percent, and a discount that is not a whole number of cents:
test "20 percent off a desk lamp"
do sale_price(1990, percent: 20)
expect result == 1592
test "0 percent off keeps the price"
do sale_price(4500, percent: 0)
expect result == 4500
test "100 percent off gives the item away"
do sale_price(4500, percent: 100)
expect result == 0
test "a discount that is not whole cents is rounded down"
do sale_price(999, percent: 15)
expect result == 85013.5.2 The dry run, with a stand-in price list
apply_sale uses the price list, so a test of it needs a price list to call. given @prices = ignored() installs the handler ignored, which keeps nothing, for one test, as in chapter 12. Both tests check the price that comes back, in a dry run and in a real one:
test "a dry run gives the sale price"
given @prices = ignored()
do apply_sale(Item(sku: "LAMP-01", name: "Desk lamp", cents: 1990), percent: 20, dry_run: true)
expect result == 1592
test "a real run gives the same sale price"
given @prices = ignored()
do apply_sale(Item(sku: "LAMP-01", name: "Desk lamp", cents: 1990), percent: 20, dry_run: false)
expect result == 1592The tests pin down the price itself, which no condition states: a dry run must report the same price that the real run sets.
13.5.3 What must not happen: proved, not tested
That a dry run sets no price is not a test's job here. The condition on set_price puts the rule in the header, and the verifier proves it on every path, for every item and every percent, before any test runs. If the guard goes, una check . reports U5001, as Setting a price without the guard shows, and una test . fails with it. A test can also check which operations a call performed, a tool of section 14.3.
13.5.4 What the tests leave out: run_sale and the tags
run_sale prints, and its tests would need a stand-in for the console too (chapter 14). A test outside the boundary also cannot call print_tag: a test's do line takes no with, so it can call into a boundary only a function with no uses. The text that a dry run prints for each item is the pure planned_line, which has its own test:
test "the line of a dry run names the old and the new price"
do planned_line(Item(sku: "RUG-07", name: "Wool rug", cents: 4500), sale: 3600)
expect result == " RUG-07 would go from 4500 to 3600"13.5.5 Running the tests
test store.sale "20 percent off a desk lamp" ... ok
test store.sale "0 percent off keeps the price" ... ok
test store.sale "100 percent off gives the item away" ... ok
test store.sale "a discount that is not whole cents is rounded down" ... ok
test store.sale "a dry run gives the sale price" ... ok
test store.sale "a real run gives the same sale price" ... ok
test store.sale "the line of a dry run names the old and the new price" ... ok
7 tests: 7 passed, 0 failedNext:13.6 Common mistakes