github.com/onsi/gomega@v1.32.0/matchers/have_occurred_matcher_test.go (about) 1 package matchers_test 2 3 import ( 4 "errors" 5 6 . "github.com/onsi/ginkgo/v2" 7 . "github.com/onsi/gomega" 8 . "github.com/onsi/gomega/matchers" 9 ) 10 11 type CustomErr struct { 12 msg string 13 } 14 15 func (e *CustomErr) Error() string { 16 return e.msg 17 } 18 19 var _ = Describe("HaveOccurred", func() { 20 It("should succeed if matching an error", func() { 21 Expect(errors.New("Foo")).Should(HaveOccurred()) 22 }) 23 24 It("should not succeed with nil", func() { 25 Expect(nil).ShouldNot(HaveOccurred()) 26 }) 27 28 It("should only support errors and nil", func() { 29 success, err := (&HaveOccurredMatcher{}).Match("foo") 30 Expect(success).Should(BeFalse()) 31 Expect(err).Should(HaveOccurred()) 32 33 success, err = (&HaveOccurredMatcher{}).Match("") 34 Expect(success).Should(BeFalse()) 35 Expect(err).Should(HaveOccurred()) 36 }) 37 38 It("doesn't support non-error type", func() { 39 success, err := (&HaveOccurredMatcher{}).Match(AnyType{}) 40 Expect(success).Should(BeFalse()) 41 Expect(err).Should(MatchError("Expected an error-type. Got:\n <matchers_test.AnyType>: {}")) 42 }) 43 44 It("doesn't support non-error pointer type", func() { 45 success, err := (&HaveOccurredMatcher{}).Match(&AnyType{}) 46 Expect(success).Should(BeFalse()) 47 Expect(err).Should(MatchError(MatchRegexp(`Expected an error-type. Got:\n <*matchers_test.AnyType | 0x[[:xdigit:]]+>: {}`))) 48 }) 49 50 It("should succeed with pointer types that conform to error interface", func() { 51 err := &CustomErr{"ohai"} 52 Expect(err).Should(HaveOccurred()) 53 }) 54 55 It("should not succeed with nil pointers to types that conform to error interface", func() { 56 var err *CustomErr = nil 57 Expect(err).ShouldNot(HaveOccurred()) 58 }) 59 })