Create text_test.go — your safety net
Now assemble the real thing: one test file for your app's text helpers. Create a file named text_test.go next to your main.go (same folder, same package main). It tests the three functions you already wrote in lesson 7 — charCount, truncateRunes and validateTitle — with no changes to those functions at all.
Three table-driven tests, one per function. Each lists a few cases, then loops and reports mismatches with a got/want message:
TestCharCount— includes{"ąžuolas", 7}, the case that fails the moment anyone reverts to counting bytes.TestTruncateRunes— one short input (unchanged) and one long Lithuanian input (cut cleanly on a character, not mid-byte).TestValidateTitle— a valid title (empty error), a spaces-only title (rejected), and a Lithuanian title that's fine by character count.
Run the whole file:
go test -v
=== RUN TestCharCount
--- PASS: TestCharCount (0.00s)
=== RUN TestTruncateRunes
--- PASS: TestTruncateRunes (0.00s)
=== RUN TestValidateTitle
--- PASS: TestValidateTitle (0.00s)
PASS
ok yourapp 0.5s
Three functions, nine cases, checked in half a second — and you can rerun it after every future change without typing a single line of input.
Note — the test file doesn't run your main. go test compiles the package, runs the Test… functions, and ignores main. So your menu loop and its readLine calls don't get in the way: tests exercise the pure functions directly. That's exactly why, back in lessons 7 and 9, we kept the logic in small functions that take input and return a result — they're the parts worth testing.
Gotcha (the test file won't compile). Two classic slips: (1) the file must end in
_test.go— name ittext.goandgo testwon't see your tests; name ittexttest.goand Go tries to build it into your app. (2)import "testing"must be there, and every test takest *testing.T. Ifgo testsays "undefined: charCount", your test file is in a different folder or package than the function — keep them together inpackage main.