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