have_occurred_matcher.go 911 B

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