Skip to content

Regexp accepts interface{} #1585

@kevinburkesegment

Description

@kevinburkesegment

The signature for assert.Regexp (and associated methods) is:

func Regexp(t TestingT, rx interface{}, str interface{}, msgAndArgs ...interface{}) bool {

However, these methods immediately call matchRegexp, which is this:

func matchRegexp(rx interface{}, str interface{}) bool {

	var r *regexp.Regexp
	if rr, ok := rx.(*regexp.Regexp); ok {
		r = rr
	} else {
		r = regexp.MustCompile(fmt.Sprint(rx))
	}

	return (r.FindStringIndex(fmt.Sprint(str)) != nil)

}

Given this - wouldn't it make more sense for the signature to just be *regexp.Regexp and string ? The latter cast would likely help prevent unexpected behavior around the fmt.Sprint call.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions