have_prefix_matcher.go 1005 B

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