mirror-to-gitea/vendor/github.com/onsi/gomega/matchers/have_prefix_matcher.go
vendor/github.com/onsi/gomega/matchers/have_prefix_matcher.goRaw
1package matchers
2
3import (
4 "fmt"
5
6 "github.com/onsi/gomega/format"
7)
8
9type HavePrefixMatcher struct {
10 Prefix string
11 Args []interface{}
12}
13
14func (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
23func (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
30func (matcher *HavePrefixMatcher) FailureMessage(actual interface{}) (message string) {
31 return format.Message(actual, "to have prefix", matcher.prefix())
32}
33
34func (matcher *HavePrefixMatcher) NegatedFailureMessage(actual interface{}) (message string) {
35 return format.Message(actual, "not to have prefix", matcher.prefix())
36}