Skip to content

quote string values in empty assertion failures#1875

Draft
Bojun-Vvibe wants to merge 1 commit intostretchr:masterfrom
Bojun-Vvibe:agent/stretchr_testify-1776829253
Draft

quote string values in empty assertion failures#1875
Bojun-Vvibe wants to merge 1 commit intostretchr:masterfrom
Bojun-Vvibe:agent/stretchr_testify-1776829253

Conversation

@Bojun-Vvibe
Copy link
Copy Markdown

Repo: stretchr/testify (⭐ 23000)
Type: bugfix
Files changed: 2
Lines: +20/-22

What

This change updates assert.Empty and assert.NotEmpty to quote string values in failure messages, including aliased string types. The related assert package tests were updated so whitespace-only, newline-containing, and empty-string cases now assert against explicit, readable output like "\\n" and "" instead of visually ambiguous raw text.

Why

Current failure output for string values uses %v, which makes empty strings and whitespace-heavy strings hard to distinguish from formatting noise. That weakens assertion diagnostics in exactly the cases where users need clarity. Quoting string values keeps behavior unchanged while making failures immediately understandable and resolves the existing TODO-marked test expectations around these messages.

Testing

Ran GOCACHE=$(pwd)/.gocache go test ./assert -run 'Test(Empty|NotEmpty)$' from the repo root. The targeted tests passed.

Risk

Low / Message formatting only changes for string values in Empty and NotEmpty; assertion semantics are unchanged.

Copy link
Copy Markdown
Collaborator

@dolmen dolmen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes should happen in truncatingFormat.

@dolmen dolmen added AI slop Bad quality AI generated. Malus points for AI not being disclosed. pkg-assert Change related to package testify/assert pkg-require Change related to package testify/require enhancement: output format Enhancement related to formatting of messages labels Apr 22, 2026
@Bojun-Vvibe
Copy link
Copy Markdown
Author

Thanks for the review @dolmen. To make sure I move this in the right direction — did you mean: (a) have truncatingFormat itself detect string values and switch the verb to %q automatically (so callers don't need to choose), or (b) keep the per-call-site verb but drop the formatEmptyMessageValue helper and just inline a truncatingFormat("%q", obj) call in Empty/NotEmpty? I lean toward (b) since truncatingFormat is also called with %v and %#v in many places where auto-quoting strings would be a behavior change, but happy to do (a) if you prefer the centralization. Will push the update once you confirm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI slop Bad quality AI generated. Malus points for AI not being disclosed. enhancement: output format Enhancement related to formatting of messages pkg-assert Change related to package testify/assert pkg-require Change related to package testify/require

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants