-
Notifications
You must be signed in to change notification settings - Fork 1.7k
docs(assert/require): clarify allowed rx values for Regexp/NotRegexp (#268, #1793) #1817
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
8014355
039a069
0aca0d7
5c95c10
0908722
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1708,35 +1708,51 @@ func ErrorContains(t TestingT, theError error, contains string, msgAndArgs ...in | |||||||||||||||||||||
| return true | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // matchRegexp return true if a specified regexp matches a string. | ||||||||||||||||||||||
| func matchRegexp(rx interface{}, str interface{}) bool { | ||||||||||||||||||||||
| // matchRegexp returns whether the provided regular expression matches the input string. | ||||||||||||||||||||||
| // If the rx cannot be compiled into a valid regexp, an error is returned. | ||||||||||||||||||||||
| func matchRegexp(rx interface{}, str interface{}) (bool, error) { | ||||||||||||||||||||||
| var r *regexp.Regexp | ||||||||||||||||||||||
| if rr, ok := rx.(*regexp.Regexp); ok { | ||||||||||||||||||||||
| r = rr | ||||||||||||||||||||||
| } else { | ||||||||||||||||||||||
| r = regexp.MustCompile(fmt.Sprint(rx)) | ||||||||||||||||||||||
| // Safely attempt to compile the pattern; on error, report it so callers can fail the assertion. | ||||||||||||||||||||||
| compiled, err := regexp.Compile(fmt.Sprint(rx)) | ||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||
| return false, err | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| r = compiled | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| switch v := str.(type) { | ||||||||||||||||||||||
| case []byte: | ||||||||||||||||||||||
| return r.Match(v) | ||||||||||||||||||||||
| return r.Match(v), nil | ||||||||||||||||||||||
| case string: | ||||||||||||||||||||||
| return r.MatchString(v) | ||||||||||||||||||||||
| return r.MatchString(v), nil | ||||||||||||||||||||||
| default: | ||||||||||||||||||||||
| return r.MatchString(fmt.Sprint(v)) | ||||||||||||||||||||||
| return r.MatchString(fmt.Sprint(v)), nil | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Regexp asserts that a specified regexp matches a string. | ||||||||||||||||||||||
| // | ||||||||||||||||||||||
| // The rx (expression) argument should be a *regexp.Regexp. For backward | ||||||||||||||||||||||
| // compatibility, if rx is any other type, its value will be formatted with | ||||||||||||||||||||||
| // %v and compiled using regexp.Compile. | ||||||||||||||||||||||
|
Comment on lines
1736
to
+1740
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would use godoc links
Suggested change
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the suggestion, @ccoVeille! I’ve updated the doc comments to use GoDoc link syntax for both [regexp.Regexp] and [regexp.Compile] as you recommended. This should make the documentation clearer and more helpful for users. Let me know if you have any other feedback |
||||||||||||||||||||||
| // | ||||||||||||||||||||||
| // Examples: | ||||||||||||||||||||||
| // | ||||||||||||||||||||||
| // assert.Regexp(t, regexp.MustCompile("start"), "it's starting") | ||||||||||||||||||||||
| // assert.Regexp(t, "start...$", "it's not starting") | ||||||||||||||||||||||
| // assert.Regexp(t, "start...$", "it's not starting") // string is compiled | ||||||||||||||||||||||
| func Regexp(t TestingT, rx interface{}, str interface{}, msgAndArgs ...interface{}) bool { | ||||||||||||||||||||||
| if h, ok := t.(tHelper); ok { | ||||||||||||||||||||||
| h.Helper() | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| match := matchRegexp(rx, str) | ||||||||||||||||||||||
| match, err := matchRegexp(rx, str) | ||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||
| Fail(t, fmt.Sprintf("Invalid regular expression: %v", err), msgAndArgs...) | ||||||||||||||||||||||
| return false | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if !match { | ||||||||||||||||||||||
| Fail(t, fmt.Sprintf("Expect \"%v\" to match \"%v\"", str, rx), msgAndArgs...) | ||||||||||||||||||||||
|
|
@@ -1747,13 +1763,23 @@ func Regexp(t TestingT, rx interface{}, str interface{}, msgAndArgs ...interface | |||||||||||||||||||||
|
|
||||||||||||||||||||||
| // NotRegexp asserts that a specified regexp does not match a string. | ||||||||||||||||||||||
| // | ||||||||||||||||||||||
| // The rx (expression) argument should be a *regexp.Regexp. For backward | ||||||||||||||||||||||
| // compatibility, if rx is any other type, its value will be formatted with | ||||||||||||||||||||||
| // %v and compiled using regexp.Compile. | ||||||||||||||||||||||
| // | ||||||||||||||||||||||
| // Examples: | ||||||||||||||||||||||
| // | ||||||||||||||||||||||
| // assert.NotRegexp(t, regexp.MustCompile("starts"), "it's starting") | ||||||||||||||||||||||
| // assert.NotRegexp(t, "^start", "it's not starting") | ||||||||||||||||||||||
| // assert.NotRegexp(t, "^start", "it's not starting") // string is compiled | ||||||||||||||||||||||
| func NotRegexp(t TestingT, rx interface{}, str interface{}, msgAndArgs ...interface{}) bool { | ||||||||||||||||||||||
| if h, ok := t.(tHelper); ok { | ||||||||||||||||||||||
| h.Helper() | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| match := matchRegexp(rx, str) | ||||||||||||||||||||||
| match, err := matchRegexp(rx, str) | ||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||
| Fail(t, fmt.Sprintf("Invalid regular expression: %v", err), msgAndArgs...) | ||||||||||||||||||||||
| return false | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if match { | ||||||||||||||||||||||
| Fail(t, fmt.Sprintf("Expect \"%v\" to NOT match \"%v\"", str, rx), msgAndArgs...) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package assert | ||
|
|
||
| import "testing" | ||
|
|
||
| // Verifies that invalid patterns no longer cause a panic when using Regexp/NotRegexp. | ||
| // Instead, the assertion should fail and return false. | ||
| func TestRegexp_InvalidPattern_NoPanic(t *testing.T) { | ||
| NotPanics(t, func() { | ||
| mockT := new(testing.T) | ||
| False(t, Regexp(mockT, "\\C", "whatever")) | ||
| }) | ||
| } | ||
|
|
||
| func TestNotRegexp_InvalidPattern_NoPanic(t *testing.T) { | ||
| NotPanics(t, func() { | ||
| mockT := new(testing.T) | ||
| False(t, NotRegexp(mockT, "\\C", "whatever")) | ||
| }) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change seems unexpected
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @ccoVeille, thanks for catching that! The comment was updated to clarify that both [Lenf] and [Len] will fail if the object’s type isn’t accepted by Go’s built-in [len()] function. This makes the documentation more explicit for users who might be unsure about which types are supported. If you think the wording could be improved or if it’s redundant, I’m happy to adjust further.