have_occurred_matcher.go 936 B

123456789101112131415161718192021222324252627282930313233343536
  1. // untested sections: 2
  2. package matchers
  3. import (
  4. "fmt"
  5. "github.com/onsi/gomega/format"
  6. )
  7. type HaveOccurredMatcher struct {
  8. }
  9. func (matcher *HaveOccurredMatcher) Match(actual interface{}) (success bool, err error) {
  10. // is purely nil?
  11. if actual == nil {
  12. return false, nil
  13. }
  14. // must be an 'error' type
  15. if !isError(actual) {
  16. return false, fmt.Errorf("Expected an error-type. Got:\n%s", format.Object(actual, 1))
  17. }
  18. // must be non-nil (or a pointer to a non-nil)
  19. return !isNil(actual), nil
  20. }
  21. func (matcher *HaveOccurredMatcher) FailureMessage(actual interface{}) (message string) {
  22. return fmt.Sprintf("Expected an error to have occurred. Got:\n%s", format.Object(actual, 1))
  23. }
  24. func (matcher *HaveOccurredMatcher) NegatedFailureMessage(actual interface{}) (message string) {
  25. return fmt.Sprintf("Unexpected error:\n%s\n%s\n%s", format.Object(actual, 1), format.IndentString(actual.(error).Error(), 1), "occurred")
  26. }