Release v0.4.0Edition 0.2
16.6 Testing it
Page 7 of 10
16.6.1 Reaching apply through a copy
An example's do line cannot pass edit: it is not a statement, and it has no editable binding of its own. So the examples reach apply through a small private function, applied, which copies the ledger into a let edit binding, applies the movement and returns the copy. lamps builds the ledger the examples start from.
day-to-day/src/stockcount.unathe examples
--- A north ledger that counts `units` of LAMP-01 and nothing else, for the examples.
lamps(units: Int) -> Ledger = Ledger(site: "north", counts: empty_map().with(key: "LAMP-01", value: units), applied: 0)
--- `apply` through a copy, since a `do` line cannot pass `edit`.
applied(ledger: Ledger, movement: Movement) -> Ledger
fails CountError
=
let edit copy = ledger
let receipt = apply(edit copy, movement: movement)
if error pass
copy
example "a sale lowers the count of its SKU"
do applied(lamps(4), movement: sale("LAMP-01", units: 1))
expect result.counts.get(key: "LAMP-01") == Some(3)
expect result.applied == 1
example "selling more than is counted is Oversold"
do applied(lamps(4), movement: sale("LAMP-01", units: 9))
expect fails Oversold
example "a rejected movement leaves the counts as they were"
do close_day(Day(site: "south", opening: empty_map().with(key: "LAMP-01", value: 2), movements: [sale("LAMP-01", units: 5)]))
expect result.closing == empty_map().with(key: "LAMP-01", value: 2)
expect result.summary == "south: 0 applied, 1 rejected"
example "closing every day at once gives the reports in site order"
do close_all([north_day(), south_day()])
expect result == [close_day(north_day()), close_day(south_day())]16.6.2 What each example checks
Each example checks one fact of the chapter:
- "a sale lowers the count of its SKU" checks
applythroughapplied: the count and the number of applied movements. - "selling more than is counted is Oversold" expects the failure with
expect fails Oversold.appliedpasses the failure on withif error pass, so the example sees it. - "a rejected movement leaves the counts as they were" closes a day whose only movement fails, and checks that the closing counts are the opening counts.
- "closing every day at once gives the reports in site order" compares
close_allwith closing the days one after another. This is the guarantee oftogether for(section 15.3), stated as an example.
16.6.3 No fakes needed
None of the examples needs a fake service: the program has no capability but the console, and the console is used only in main. Every function under test takes its input as values and returns its result, which is what makes each one easy to check on its own.
16.6.4 Running the examples
una test . prints:
Output
example stockcount "a sale lowers the count of its SKU" ... ok
example stockcount "selling more than is counted is Oversold" ... ok
example stockcount "a rejected movement leaves the counts as they were" ... ok
example stockcount "closing every day at once gives the reports in site order" ... ok
4 examples: 4 passed, 0 failedNext:16.7 Common mistakes