contain_substring_matcher.go 1.1 KB

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