Skip to content

assert.Regexp: document allowed rx argument types for Regexp/NotRegexp#1897

Closed
SAY-5 wants to merge 1 commit into
stretchr:masterfrom
SAY-5:docs-regexp-rx-arg
Closed

assert.Regexp: document allowed rx argument types for Regexp/NotRegexp#1897
SAY-5 wants to merge 1 commit into
stretchr:masterfrom
SAY-5:docs-regexp-rx-arg

Conversation

@SAY-5

@SAY-5 SAY-5 commented May 24, 2026

Copy link
Copy Markdown

Summary

Document that the rx argument of Regexp/NotRegexp accepts a *regexp.Regexp or any other value.

Changes

  • Add a note to the assert.Regexp and assert.NotRegexp doc comments explaining that rx may be a *regexp.Regexp (used directly) or any other value (stringified with fmt.Sprint and compiled with regexp.MustCompile, which panics on an invalid pattern).
  • Regenerate the require, format and forward variants with go generate ./....

Motivation

Requested in #1793 by @brackendawson: "The documentation for these functions should be changed to explain that the value should be a *regexp.Regexp, but that if any other type is provided" it is compiled at runtime. The current doc comments give examples for both forms but never state the accepted types or the panic on an invalid expression.

This is a documentation-only change; the behavior is unchanged.

Related issues

Closes #1793

…otRegexp

Signed-off-by: SAY-5 <say.apm35@gmail.com>

@dolmen dolmen left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think that it is a bug that we accept anything that can be stringified: this leaves to much space to misuse of the API. We should only accept *regexp.Regexp and strict string.

See also Antonboom/testifylint#292

So the last thing I want is to document this as a feature.

We must only document that we accept *regexp.Regexp and string (but unfortunately leave the implementation as is for compatibility).

@dolmen

dolmen commented May 24, 2026

Copy link
Copy Markdown
Collaborator

As the example already shows usage with *regexp.Regexp and string, I don't think that there is more change needed.

So I'm closing.

@dolmen dolmen closed this May 24, 2026
@dolmen dolmen added documentation pkg-assert Change related to package testify/assert pkg-require Change related to package testify/require assert.Regexp About assert.Regexp labels May 24, 2026
@dolmen dolmen changed the title docs(assert/require): document allowed rx argument types for Regexp/NotRegexp assert.Regexp: document allowed rx argument types for Regexp/NotRegexp May 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

assert.Regexp About assert.Regexp documentation 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.

assert.Regexp: Document allowed rx argument values #268

2 participants