MCPcopy Create free account
hub / github.com/gavv/httpexpect / Match

Method Match

string.go:874–909  ·  view source on GitHub ↗

Match matches the string with given regexp and returns a new Match instance with found submatches. If regexp is invalid or string doesn't match regexp, Match fails and returns empty (but non-nil) instance. regexp.Compile is used to construct regexp, and Regexp.FindStringSubmatch is used to construc

(re string)

Source from the content-addressed store, hash-verified

872// m.NamedSubmatch("host").IsEqual("example.com")
873// m.NamedSubmatch("user").IsEqual("john")
874func (s *String) Match(re string) *Match {
875 opChain := s.chain.enter("Match()")
876 defer opChain.leave()
877
878 if opChain.failed() {
879 return newMatch(opChain, nil, nil)
880 }
881
882 rx, err := regexp.Compile(re)
883 if err != nil {
884 opChain.fail(AssertionFailure{
885 Type: AssertValid,
886 Actual: &AssertionValue{re},
887 Errors: []error{
888 errors.New("expected: valid regexp"),
889 err,
890 },
891 })
892 return newMatch(opChain, nil, nil)
893 }
894
895 match := rx.FindStringSubmatch(s.value)
896 if match == nil {
897 opChain.fail(AssertionFailure{
898 Type: AssertMatchRegexp,
899 Actual: &AssertionValue{s.value},
900 Expected: &AssertionValue{re},
901 Errors: []error{
902 errors.New("expected: string matches regexp"),
903 },
904 })
905 return newMatch(opChain, nil, nil)
906 }
907
908 return newMatch(opChain, match, rx.SubexpNames())
909}
910
911// NotMatch succeeds if the string doesn't match to given regexp.
912//

Callers 4

TestString_MatchFunction · 0.95
GlobMethod · 0.80
TestString_FailedChainFunction · 0.80
TestIrisThingsFunction · 0.80

Calls 5

newMatchFunction · 0.85
enterMethod · 0.80
leaveMethod · 0.80
failedMethod · 0.80
failMethod · 0.80

Tested by 3

TestString_MatchFunction · 0.76
TestString_FailedChainFunction · 0.64
TestIrisThingsFunction · 0.64