summaryrefslogtreecommitdiff
path: root/vendor/github.com/onsi/gomega/matchers/have_suffix_matcher.go
blob: 40a3526eb2dac89cd6806cf18c2ca1fef58d7111 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package matchers

import (
	"fmt"

	"github.com/onsi/gomega/format"
)

type HaveSuffixMatcher struct {
	Suffix string
	Args   []interface{}
}

func (matcher *HaveSuffixMatcher) Match(actual interface{}) (success bool, err error) {
	actualString, ok := toString(actual)
	if !ok {
		return false, fmt.Errorf("HaveSuffix matcher requires a string or stringer.  Got:\n%s", format.Object(actual, 1))
	}
	suffix := matcher.suffix()
	return len(actualString) >= len(suffix) && actualString[len(actualString)-len(suffix):] == suffix, nil
}

func (matcher *HaveSuffixMatcher) suffix() string {
	if len(matcher.Args) > 0 {
		return fmt.Sprintf(matcher.Suffix, matcher.Args...)
	}
	return matcher.Suffix
}

func (matcher *HaveSuffixMatcher) FailureMessage(actual interface{}) (message string) {
	return format.Message(actual, "to have suffix", matcher.suffix())
}

func (matcher *HaveSuffixMatcher) NegatedFailureMessage(actual interface{}) (message string) {
	return format.Message(actual, "not to have suffix", matcher.suffix())
}