github.com/onsi/gomega@v1.32.0/matchers/panic_matcher_test.go (about) 1 package matchers_test 2 3 import ( 4 . "github.com/onsi/ginkgo/v2" 5 . "github.com/onsi/gomega" 6 . "github.com/onsi/gomega/matchers" 7 ) 8 9 var _ = Describe("Panic", func() { 10 When("passed something that's not a function that takes zero arguments and returns nothing", func() { 11 It("should error", func() { 12 success, err := (&PanicMatcher{}).Match("foo") 13 Expect(success).To(BeFalse()) 14 Expect(err).To(HaveOccurred()) 15 16 success, err = (&PanicMatcher{}).Match(nil) 17 Expect(success).To(BeFalse()) 18 Expect(err).To(HaveOccurred()) 19 20 success, err = (&PanicMatcher{}).Match(func(foo string) {}) 21 Expect(success).To(BeFalse()) 22 Expect(err).To(HaveOccurred()) 23 24 success, err = (&PanicMatcher{}).Match(func() string { return "bar" }) 25 Expect(success).To(BeFalse()) 26 Expect(err).To(HaveOccurred()) 27 }) 28 }) 29 30 When("passed a function of the correct type", func() { 31 It("should call the function and pass if the function panics", func() { 32 Expect(func() { panic("ack!") }).To(Panic()) 33 Expect(func() {}).NotTo(Panic()) 34 }) 35 }) 36 37 When("assertion fails", func() { 38 It("prints the object passed to panic() when negative", func() { 39 failuresMessages := InterceptGomegaFailures(func() { 40 Expect(func() { panic("ack!") }).NotTo(Panic()) 41 }) 42 Expect(failuresMessages).To(ConsistOf(ContainSubstring("not to panic, but panicked with\n <string>: ack!"))) 43 }) 44 45 It("prints simple message when positive", func() { 46 failuresMessages := InterceptGomegaFailures(func() { 47 Expect(func() {}).To(Panic()) 48 }) 49 Expect(failuresMessages).To(ConsistOf(MatchRegexp("Expected\n\\s+<func\\(\\)>: .+\nto panic"))) 50 }) 51 }) 52 }) 53 54 var _ = Describe("PanicWith", func() { 55 When("a specific panic value is expected", func() { 56 matcher := PanicWith("ack!") 57 58 When("no panic occurs", func() { 59 actual := func() {} 60 61 It("prints a message that includes the expected value", func() { 62 failuresMessages := InterceptGomegaFailures(func() { 63 Expect(actual).To(matcher) 64 }) 65 Expect(failuresMessages).To(ConsistOf( 66 MatchRegexp("Expected\n\\s+<func\\(\\)>: .+\nto panic with\\s+<string>: ack!"), 67 )) 68 }) 69 70 It("passes when negated", func() { 71 Expect(actual).NotTo(matcher) 72 }) 73 }) 74 75 When("the panic value matches", func() { 76 actual := func() { panic("ack!") } 77 78 It("passes", func() { 79 Expect(actual).To(matcher) 80 }) 81 82 It("prints a message that includes the (un)expected value when negated", func() { 83 failuresMessages := InterceptGomegaFailures(func() { 84 Expect(actual).NotTo(matcher) 85 }) 86 Expect(failuresMessages).To(ConsistOf( 87 MatchRegexp("Expected\n\\s+<func\\(\\)>: .+\nnot to panic with\\s+<string>: ack!"), 88 )) 89 }) 90 }) 91 92 When("the panic value does not match", func() { 93 actual := func() { panic("unexpected!") } 94 95 It("prints a message that includes both the actual and expected values", func() { 96 failuresMessages := InterceptGomegaFailures(func() { 97 Expect(actual).To(matcher) 98 }) 99 Expect(failuresMessages).To(ConsistOf( 100 MatchRegexp("Expected\n\\s+<func\\(\\)>: .+\nto panic with\\s+<string>: ack!\nbut panicked with\n\\s+<string>: unexpected!"), 101 )) 102 }) 103 104 It("passes when negated", func() { 105 Expect(actual).NotTo(matcher) 106 }) 107 }) 108 }) 109 110 When("the expected value is actually a matcher", func() { 111 matcher := PanicWith(MatchRegexp("ack")) 112 113 When("no panic occurs", func() { 114 actual := func() {} 115 116 It("prints a message that includes the expected value", func() { 117 failuresMessages := InterceptGomegaFailures(func() { 118 Expect(actual).To(matcher) 119 }) 120 Expect(failuresMessages).To(ConsistOf( 121 MatchRegexp("Expected\n\\s+<func\\(\\)>: .+\nto panic with a value matching\n.+MatchRegexpMatcher.+ack"), 122 )) 123 }) 124 125 It("passes when negated", func() { 126 Expect(actual).NotTo(matcher) 127 }) 128 }) 129 130 When("the panic value matches", func() { 131 actual := func() { panic("ack!") } 132 133 It("passes", func() { 134 Expect(actual).To(matcher) 135 }) 136 137 It("prints a message that includes the (un)expected value when negated", func() { 138 failuresMessages := InterceptGomegaFailures(func() { 139 Expect(actual).NotTo(matcher) 140 }) 141 Expect(failuresMessages).To(ConsistOf( 142 MatchRegexp("Expected\n\\s+<func\\(\\)>: .+\nnot to panic with a value matching\n.+MatchRegexpMatcher.+ack"), 143 )) 144 }) 145 }) 146 147 When("the panic value does not match", func() { 148 actual := func() { panic("unexpected!") } 149 150 It("prints a message that includes both the actual and expected values", func() { 151 failuresMessages := InterceptGomegaFailures(func() { 152 Expect(actual).To(matcher) 153 }) 154 Expect(failuresMessages).To(ConsistOf( 155 MatchRegexp("Expected\n\\s+<func\\(\\)>: .+\nto panic with a value matching\n.+MatchRegexpMatcher.+ack.+\nbut panicked with\n\\s+<string>: unexpected!"), 156 )) 157 }) 158 159 It("passes when negated", func() { 160 Expect(actual).NotTo(matcher) 161 }) 162 }) 163 }) 164 })