Skip to content

test: check json's errors by errors.Is and errors.AsType, never by their text - #10

Merged
MarkRosemaker merged 2 commits into
masterfrom
fix/null-test-wording
Nov 15, 2026
Merged

MarkRosemaker merged 2 commits into
masterfrom
fix/null-test-wording

Conversation

@MarkRosemaker

@MarkRosemaker MarkRosemaker commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #9.

Cause: encoding/json/v2 deliberately words its errors with either "cannot" or "unable to", picked at random by iterating a map (errorModalVerb in encoding/json/v2/errors.go), so that no caller depends on its text. TestNull, added in #8, matched json: cannot unmarshal JSON null into Go *apitest_test.item: no, so it failed whenever json picked the other phrase.

Fix: tests check the errors themselves, never json's wording:

  • fakeT now keeps the error values reported to it, besides the formatted messages.
  • A test unmarshaler returns the sentinel errRefused. TestNull and TestRoundTrip check that decoding failed with it, using errors.Is, and that json wrapped it in a *json.SemanticError, using errors.AsType. TestMustDecode checks for the *json.SemanticError too.
  • Text is still matched only where apitest writes the message itself, such as null: or "decoded as another variant".

Also: TestRoundTrip failed under -count greater than 1, because drifting counts its encodings in a package variable and the test expects "encoded 1, then 2". It now resets that counter first.

Verified with Go 1.27.4: 30 repeated, shuffled runs of the package pass, and so do separate shuffled runs. make ready passes.

🤖 Generated with Claude Code

https://claude.ai/code/session_019QCb1AfnATvpBQCH3PrX7Y

encoding/json/v2 words an error with cannot or unable to, picked from a map
at random, so that no caller matches its text. TestNull matched it, and
failed whenever json picked the other. It now checks only what Null and the
unmarshaler say.

Fixes #9

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019QCb1AfnATvpBQCH3PrX7Y
…eir text

fakeT keeps the errors reported, so a test checks that decoding failed with
the unmarshaler's own error, by errors.Is, and with json's *SemanticError, by
errors.AsType, rather than matching words json picks at random. TestRoundTrip
resets the count of drifting's encodings, so that it passes with -count too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019QCb1AfnATvpBQCH3PrX7Y
@MarkRosemaker MarkRosemaker changed the title test: TestNull no longer matches the wording of json's errors test: check json's errors by errors.Is and errors.AsType, never by their text Nov 15, 2026
@MarkRosemaker
MarkRosemaker merged commit beac396 into master Nov 15, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tests fail on main: TestNull

2 participants