github.com/onsi/gomega@v1.32.0/matchers/succeed_matcher_test.go (about) 1 package matchers_test 2 3 import ( 4 "errors" 5 "regexp" 6 "runtime" 7 "time" 8 9 . "github.com/onsi/ginkgo/v2" 10 . "github.com/onsi/gomega" 11 . "github.com/onsi/gomega/matchers" 12 ) 13 14 func Erroring() error { 15 return errors.New("bam") 16 } 17 18 func NotErroring() error { 19 return nil 20 } 21 22 type AnyType struct{} 23 24 func Invalid() *AnyType { 25 return nil 26 } 27 28 type formattedGomegaError struct { 29 message string 30 } 31 32 func (e formattedGomegaError) Error() string { 33 return "NOT THIS ERROR" 34 } 35 36 func (e formattedGomegaError) FormattedGomegaError() string { 37 return e.message 38 } 39 40 var _ = Describe("Succeed", func() { 41 It("should succeed if the function succeeds", func() { 42 Expect(NotErroring()).Should(Succeed()) 43 }) 44 45 It("should succeed (in the negated) if the function errored", func() { 46 Expect(Erroring()).ShouldNot(Succeed()) 47 }) 48 49 It("should not if passed a non-error", func() { 50 success, err := (&SucceedMatcher{}).Match(Invalid()) 51 Expect(success).Should(BeFalse()) 52 Expect(err).Should(MatchError("Expected an error-type. Got:\n <*matchers_test.AnyType | 0x0>: nil")) 53 }) 54 55 It("doesn't support non-error type", func() { 56 success, err := (&SucceedMatcher{}).Match(AnyType{}) 57 Expect(success).Should(BeFalse()) 58 Expect(err).Should(MatchError("Expected an error-type. Got:\n <matchers_test.AnyType>: {}")) 59 }) 60 61 It("doesn't support non-error pointer type", func() { 62 success, err := (&SucceedMatcher{}).Match(&AnyType{}) 63 Expect(success).Should(BeFalse()) 64 Expect(err).Should(MatchError(MatchRegexp(`Expected an error-type. Got:\n <*matchers_test.AnyType | 0x[[:xdigit:]]+>: {}`))) 65 }) 66 67 It("should not succeed with pointer types that conform to error interface", func() { 68 err := &CustomErr{"ohai"} 69 Expect(err).ShouldNot(Succeed()) 70 }) 71 72 It("should succeed with nil pointers to types that conform to error interface", func() { 73 var err *CustomErr = nil 74 Expect(err).Should(Succeed()) 75 }) 76 77 It("builds failure message", func() { 78 actual := Succeed().FailureMessage(errors.New("oops")) 79 actual = regexp.MustCompile(" 0x.*>").ReplaceAllString(actual, " 0x00000000>") 80 Expect(actual).To(Equal("Expected success, but got an error:\n <*errors.errorString | 0x00000000>: \n oops\n {s: \"oops\"}")) 81 }) 82 83 It("simply returns .Error() for the failure message if the error is an AsyncPolledActualError", func() { 84 actual := Succeed().FailureMessage(formattedGomegaError{message: "this is already formatted appropriately"}) 85 Expect(actual).To(Equal("this is already formatted appropriately")) 86 }) 87 88 It("operates correctly when paired with an Eventually that receives a Gomega", func() { 89 _, file, line, _ := runtime.Caller(0) 90 failureMessage := InterceptGomegaFailure(func() { 91 Eventually(func(g Gomega) { 92 g.Expect(true).To(BeFalse()) 93 }).WithTimeout(time.Millisecond * 10).Should(Succeed()) 94 }).Error() 95 Ω(failureMessage).Should(HavePrefix("Timed out after")) 96 Ω(failureMessage).Should(HaveSuffix("The function passed to Eventually failed at %s:%d with:\nExpected\n <bool>: true\nto be false", file, line+3)) 97 }) 98 99 It("builds negated failure message", func() { 100 actual := Succeed().NegatedFailureMessage(123) 101 Expect(actual).To(Equal("Expected failure, but got no error.")) 102 }) 103 })