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