#700·goconvey

Consider add assertion `ShouldMatchRegex`

Author: 1ncludeStevenCreated Oct 9, 2025Updated Oct 9, 2025

add below code in assertions/strings.go

go
func ShouldMatchRegex(actual interface{}, expected ...interface{}) string {
	if fail := need(1, expected); fail != success {
		return fail
	}

	obs, valueIsString := actual.(string)
	pattern, expecIsString := expected[0].(string)

	if !valueIsString || !expecIsString {
		return fmt.Sprintf(shouldBothBeStrings, reflect.TypeOf(actual), reflect.TypeOf(expected[0]))
	}
	matched, err := regexp.MatchString(pattern, obs)
	if err != nil {
		return fmt.Sprintf("error from regex.MatchString: '%v' in trying to match observed '%s' against expected pattern '%s'", err, obs, pattern)
	}
	if !matched {
		return fmt.Sprintf("failed to match observed '%s' against expected pattern '%s'", obs, pattern)
	}
	return success
}

then add ShouldMatchRegex in convey/assertions.go

go
ShouldMatchRegex  = assertions.ShouldMatchRegex