On Day 6 you wrote table checks for small functions. Here the same idea protects a real program. Because the reading log keeps its rules in log.go, a separate check program can call those rules directly, with inputs chosen to sit exactly on the edges of each rule, and confirm every answer.
This playground replaces the application entry with check.go. It uses the same log.go and calls the same functions. The table checks the parsed fields and whether an error came back. It also checks the total for two sessions and for an empty list.
What the table covers
Each row of the table in check.go is there for a reason. Reading them as a list of claims about the program shows what is protected:
" Poems , 1 "is accepted asPoemswith 1 minute, so spaces around both fields are trimmed and the lower bound is included."Essays,240"is accepted, so the upper bound is included."Essays,241"and"Essays,0"are rejected, so the values just outside the range fail."Essays,later"is rejected, so text that is not a number fails.",20"is rejected, so a blank title fails."Essays"is rejected, so a line without a comma fails.
The total checks add one more claim: an empty list totals 0, and two sessions at the extreme values add up correctly. If any of these claims stops being true, the check panics and names the line that broke it.
Reading log checks passedBreak and restore the upper bound
In log.go, change the rejection from minutes > 240 to minutes >= 240. Run the checks. Explain the failure and restore the original condition.
Hint 1: the idea
The new comparison excludes a value the format promises to accept.
Hint 2: a first step
Find the table row with the largest permitted duration.
Worked solution
The Essays,240 row expects a valid session and no error. The changed comparison rejects it. Restoring the original comparison allows exactly 240 and still rejects 241.
Breaking code on purpose
What you just did has a name: mutation testing. You change the code in a small, plausible way, the kind of slip a person might make, and confirm that at least one check fails. A check that still passes after a real mistake is not protecting anything. The course's own tooling runs these mutations automatically for the reading log, and every one must be caught:
- Change
minutes > 240tominutes >= 240, and theEssays,240row fails. - Change
minutes < 1tominutes < 0, and theEssays,0row fails. - Remove the blank-title check, and the
,20row fails. - Stop trimming the title, and the
" Poems , 1 "row fails. - Change
total += session.Minutestototal = session.Minutes, and the two-session total fails.
Try a few of them yourself, one at a time, and restore the line after each. For each, predict the failing row before you run.
The same table, two ways to run it
The downloadable project contains log_test.go, which holds the same table written for Go's testing package. Locally, go test . runs it and reports every failing row instead of stopping at the first. check.go exists because the browser editor cannot run go test; the cases and the rules they protect are identical.
When a check fails
A failing check is information, not a verdict. Read the message first: it names the input and shows what came back. Then decide which is wrong, the code or the expectation. Most of the time the code is wrong, as in the exercise above. Occasionally the rule has really changed, for example if the log should now accept 300 minutes. Then you update the table and the code together, in the same change, so the checks keep describing what the program promises.
Completion is saved in this browser only. It does not measure mastery.