contain_substring_matcher.go 1.1 KB

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