Path: blob/main/vendor/github.com/onsi/gomega/matchers/match_regexp_matcher.go
2880 views
package matchers12import (3"fmt"4"regexp"56"github.com/onsi/gomega/format"7)89type MatchRegexpMatcher struct {10Regexp string11Args []any12}1314func (matcher *MatchRegexpMatcher) Match(actual any) (success bool, err error) {15actualString, ok := toString(actual)16if !ok {17return false, fmt.Errorf("RegExp matcher requires a string or stringer.\nGot:%s", format.Object(actual, 1))18}1920match, err := regexp.Match(matcher.regexp(), []byte(actualString))21if err != nil {22return false, fmt.Errorf("RegExp match failed to compile with error:\n\t%s", err.Error())23}2425return match, nil26}2728func (matcher *MatchRegexpMatcher) FailureMessage(actual any) (message string) {29return format.Message(actual, "to match regular expression", matcher.regexp())30}3132func (matcher *MatchRegexpMatcher) NegatedFailureMessage(actual any) (message string) {33return format.Message(actual, "not to match regular expression", matcher.regexp())34}3536func (matcher *MatchRegexpMatcher) regexp() string {37re := matcher.Regexp38if len(matcher.Args) > 0 {39re = fmt.Sprintf(matcher.Regexp, matcher.Args...)40}41return re42}434445