Repository navigation
test: check json's errors by errors.Is and errors.AsType, never by their text - #10
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #9.
Cause:
encoding/json/v2deliberately words its errors with either "cannot" or "unable to", picked at random by iterating a map (errorModalVerbinencoding/json/v2/errors.go), so that no caller depends on its text.TestNull, added in #8, matchedjson: 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:
fakeTnow keeps the error values reported to it, besides the formatted messages.errRefused.TestNullandTestRoundTripcheck that decoding failed with it, usingerrors.Is, and that json wrapped it in a*json.SemanticError, usingerrors.AsType.TestMustDecodechecks for the*json.SemanticErrortoo.apitestwrites the message itself, such asnull:or "decoded as another variant".Also:
TestRoundTripfailed under-countgreater than 1, becausedriftingcounts 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 readypasses.🤖 Generated with Claude Code
https://claude.ai/code/session_019QCb1AfnATvpBQCH3PrX7Y