...
1
2
3 package matchers
4
5 import (
6 "fmt"
7 "strings"
8
9 "github.com/onsi/gomega/format"
10 )
11
12 type ContainSubstringMatcher struct {
13 Substr string
14 Args []interface{}
15 }
16
17 func (matcher *ContainSubstringMatcher) Match(actual interface{}) (success bool, err error) {
18 actualString, ok := toString(actual)
19 if !ok {
20 return false, fmt.Errorf("ContainSubstring matcher requires a string or stringer. Got:\n%s", format.Object(actual, 1))
21 }
22
23 return strings.Contains(actualString, matcher.stringToMatch()), nil
24 }
25
26 func (matcher *ContainSubstringMatcher) stringToMatch() string {
27 stringToMatch := matcher.Substr
28 if len(matcher.Args) > 0 {
29 stringToMatch = fmt.Sprintf(matcher.Substr, matcher.Args...)
30 }
31 return stringToMatch
32 }
33
34 func (matcher *ContainSubstringMatcher) FailureMessage(actual interface{}) (message string) {
35 return format.Message(actual, "to contain substring", matcher.stringToMatch())
36 }
37
38 func (matcher *ContainSubstringMatcher) NegatedFailureMessage(actual interface{}) (message string) {
39 return format.Message(actual, "not to contain substring", matcher.stringToMatch())
40 }
41
View as plain text