initial commit
[govpp.git] / vendor / github.com / onsi / gomega / matchers / match_regexp_matcher.go
1 package matchers
2
3 import (
4         "fmt"
5         "github.com/onsi/gomega/format"
6         "regexp"
7 )
8
9 type MatchRegexpMatcher struct {
10         Regexp string
11         Args   []interface{}
12 }
13
14 func (matcher *MatchRegexpMatcher) Match(actual interface{}) (success bool, err error) {
15         actualString, ok := toString(actual)
16         if !ok {
17                 return false, fmt.Errorf("RegExp matcher requires a string or stringer.\nGot:%s", format.Object(actual, 1))
18         }
19
20         match, err := regexp.Match(matcher.regexp(), []byte(actualString))
21         if err != nil {
22                 return false, fmt.Errorf("RegExp match failed to compile with error:\n\t%s", err.Error())
23         }
24
25         return match, nil
26 }
27
28 func (matcher *MatchRegexpMatcher) FailureMessage(actual interface{}) (message string) {
29         return format.Message(actual, "to match regular expression", matcher.regexp())
30 }
31
32 func (matcher *MatchRegexpMatcher) NegatedFailureMessage(actual interface{}) (message string) {
33         return format.Message(actual, "not to match regular expression", matcher.regexp())
34 }
35
36 func (matcher *MatchRegexpMatcher) regexp() string {
37         re := matcher.Regexp
38         if len(matcher.Args) > 0 {
39                 re = fmt.Sprintf(matcher.Regexp, matcher.Args...)
40         }
41         return re
42 }