Skip to content

Check the Project Boundaries

The Course
•
16 min study

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 as Poems with 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.

Run the reading log checks Go
package main import "fmt" func main() { cases := []struct { line string want Session ok bool }{ {" Poems , 1 ", Session{"Poems", 1}, true}, {"Essays,240", Session{"Essays", 240}, true}, {"Essays,241", Session{}, false}, {"Essays,0", Session{}, false}, {"Essays,later", Session{}, false}, {",20", Session{}, false}, {"Essays", Session{}, false}, } for _, c := range cases { got, err := parseSession(c.line) if got != c.want || (err == nil) != c.ok { panic(fmt.Sprintf("bad result for %q: %+v, %v", c.line, got, err)) } } if totalMinutes(nil) != 0 || totalMinutes([]Session{{"Poems", 1}, {"Essays", 240}}) != 241 { panic("bad total") } fmt.Println("Reading log checks passed") } package main import ( "errors" "strconv" "strings" ) type Session struct { Title string Minutes int } func parseSession(line string) (Session, error) { title, rawMinutes, ok := strings.Cut(line, ",") if !ok { return Session{}, errors.New("use title,minutes") } title = strings.TrimSpace(title) if title == "" { return Session{}, errors.New("title is required") } minutes, err := strconv.Atoi(strings.TrimSpace(rawMinutes)) if err != nil || minutes < 1 || minutes > 240 { return Session{}, errors.New("use 1–240 whole minutes") } return Session{Title: title, Minutes: minutes}, nil } func totalMinutes(sessions []Session) int { total := 0 for _, session := range sessions { total += session.Minutes } return total }
Text
Reading log checks passed
Exercise

Break 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 > 240 to minutes >= 240, and the Essays,240 row fails.
  • Change minutes < 1 to minutes < 0, and the Essays,0 row fails.
  • Remove the blank-title check, and the ,20 row fails.
  • Stop trimming the title, and the " Poems , 1 " row fails.
  • Change total += session.Minutes to total = 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.

Was this article helpful?

Your response is saved on this device.