github.com/onsi/gomega@v1.32.0/internal/dsl_test.go (about) 1 package internal_test 2 3 import ( 4 "errors" 5 "runtime" 6 "time" 7 8 . "github.com/onsi/ginkgo/v2" 9 . "github.com/onsi/gomega" 10 "github.com/onsi/gomega/internal" 11 ) 12 13 func getGlobalDurationBundle() internal.DurationBundle { 14 return Default.(*internal.Gomega).DurationBundle 15 } 16 17 func setGlobalDurationBundle(bundle internal.DurationBundle) { 18 SetDefaultEventuallyTimeout(bundle.EventuallyTimeout) 19 SetDefaultEventuallyPollingInterval(bundle.EventuallyPollingInterval) 20 SetDefaultConsistentlyDuration(bundle.ConsistentlyDuration) 21 SetDefaultConsistentlyPollingInterval(bundle.ConsistentlyPollingInterval) 22 } 23 24 var _ = Describe("Gomega DSL", func() { 25 var globalDurationBundle internal.DurationBundle 26 27 BeforeEach(func() { 28 globalDurationBundle = getGlobalDurationBundle() 29 }) 30 31 AfterEach(func() { 32 RegisterFailHandler(Fail) 33 setGlobalDurationBundle(globalDurationBundle) 34 }) 35 36 Describe("The Default, global, Gomega", func() { 37 It("exists", func() { 38 Ω(Default).ShouldNot(BeNil()) 39 }) 40 41 It("is wired up via the global DSL", func() { 42 counter := 0 43 Eventually(func() int { 44 counter += 1 45 return counter 46 }).Should(Equal(5)) 47 Ω(counter).Should(Equal(5)) 48 }) 49 }) 50 51 Describe("NewGomega", func() { 52 It("creates and configures a new Gomega, using the global duration bundle", func() { 53 bundle := internal.DurationBundle{ 54 EventuallyTimeout: time.Minute, 55 EventuallyPollingInterval: 2 * time.Minute, 56 ConsistentlyDuration: 3 * time.Minute, 57 ConsistentlyPollingInterval: 4 * time.Minute, 58 } 59 setGlobalDurationBundle(bundle) 60 61 var calledWith string 62 g := NewGomega(func(message string, skip ...int) { 63 calledWith = message 64 }) 65 66 gAsStruct := g.(*internal.Gomega) 67 Ω(gAsStruct.DurationBundle).Should(Equal(bundle)) 68 69 g.Ω(true).Should(BeFalse()) 70 Ω(calledWith).Should(Equal("Expected\n <bool>: true\nto be false")) 71 }) 72 }) 73 74 Describe("NewWithT", func() { 75 It("creates and configure a new Gomega with the passed-in T, using the global duration bundle", func() { 76 bundle := internal.DurationBundle{ 77 EventuallyTimeout: time.Minute, 78 EventuallyPollingInterval: 2 * time.Minute, 79 ConsistentlyDuration: 3 * time.Minute, 80 ConsistentlyPollingInterval: 4 * time.Minute, 81 } 82 setGlobalDurationBundle(bundle) 83 84 fakeT := &FakeGomegaTestingT{} 85 g := NewWithT(fakeT) 86 87 Ω(g.DurationBundle).Should(Equal(bundle)) 88 89 g.Ω(true).Should(BeFalse()) 90 Ω(fakeT.CalledFatalf).Should(Equal("\nExpected\n <bool>: true\nto be false")) 91 Ω(fakeT.CalledHelper).Should(BeTrue()) 92 }) 93 }) 94 95 Describe("RegisterFailHandler", func() { 96 It("overrides the global fail handler", func() { 97 var calledWith string 98 RegisterFailHandler(func(message string, skip ...int) { 99 calledWith = message 100 }) 101 102 Ω(true).Should(BeFalse()) 103 104 RegisterFailHandler(Fail) 105 Ω(calledWith).Should(Equal("Expected\n <bool>: true\nto be false")) 106 }) 107 }) 108 109 Describe("RegisterTestingT", func() { 110 It("overrides the global fail handler", func() { 111 fakeT := &FakeGomegaTestingT{} 112 RegisterTestingT(fakeT) 113 114 Ω(true).Should(BeFalse()) 115 RegisterFailHandler(Fail) 116 Ω(fakeT.CalledFatalf).Should(Equal("\nExpected\n <bool>: true\nto be false")) 117 Ω(fakeT.CalledHelper).Should(BeTrue()) 118 }) 119 }) 120 121 Describe("InterceptGomegaFailures", func() { 122 Context("when no failures occur", func() { 123 It("returns an empty array", func() { 124 Expect(InterceptGomegaFailures(func() { 125 Expect("hi").To(Equal("hi")) 126 })).To(BeEmpty()) 127 }) 128 }) 129 130 Context("when failures occur", func() { 131 It("does not stop execution and returns all the failures as strings", func() { 132 Expect(InterceptGomegaFailures(func() { 133 Expect("hi").To(Equal("bye")) 134 Expect(3).To(Equal(2)) 135 })).To(Equal([]string{ 136 "Expected\n <string>: hi\nto equal\n <string>: bye", 137 "Expected\n <int>: 3\nto equal\n <int>: 2", 138 })) 139 140 }) 141 }) 142 }) 143 144 Describe("InterceptGomegaFailure", func() { 145 Context("when no failures occur", func() { 146 It("returns nil", func() { 147 Expect(InterceptGomegaFailure(func() { 148 Expect("hi").To(Equal("hi")) 149 })).To(BeNil()) 150 }) 151 }) 152 153 Context("when failures occur", func() { 154 It("returns the first failure and stops execution", func() { 155 gotThere := false 156 Expect(InterceptGomegaFailure(func() { 157 Expect("hi").To(Equal("bye")) 158 gotThere = true 159 Expect(3).To(Equal(2)) 160 })).To(Equal(errors.New("Expected\n <string>: hi\nto equal\n <string>: bye"))) 161 Expect(gotThere).To(BeFalse()) 162 }) 163 }) 164 165 Context("when the function panics", func() { 166 It("panics", func() { 167 Expect(func() { 168 InterceptGomegaFailure(func() { 169 panic("boom") 170 }) 171 }).To(PanicWith("boom")) 172 }) 173 }) 174 }) 175 176 Context("Making an assertion without a registered fail handler", func() { 177 It("should panic", func() { 178 defer func() { 179 e := recover() 180 RegisterFailHandler(Fail) 181 if e == nil { 182 Fail("expected a panic to have occurred") 183 } 184 }() 185 186 RegisterFailHandler(nil) 187 Expect(true).Should(BeTrue()) 188 }) 189 }) 190 191 Describe("specifying default durations globally", func() { 192 It("should update the durations on the Default gomega", func() { 193 bundle := internal.DurationBundle{ 194 EventuallyTimeout: time.Minute, 195 EventuallyPollingInterval: 2 * time.Minute, 196 ConsistentlyDuration: 3 * time.Minute, 197 ConsistentlyPollingInterval: 4 * time.Minute, 198 } 199 200 SetDefaultEventuallyTimeout(bundle.EventuallyTimeout) 201 SetDefaultEventuallyPollingInterval(bundle.EventuallyPollingInterval) 202 SetDefaultConsistentlyDuration(bundle.ConsistentlyDuration) 203 SetDefaultConsistentlyPollingInterval(bundle.ConsistentlyPollingInterval) 204 205 Ω(Default.(*internal.Gomega).DurationBundle).Should(Equal(bundle)) 206 }) 207 }) 208 209 Describe("Offsets", func() { 210 AfterEach(func() { 211 RegisterFailHandler(Fail) 212 }) 213 214 It("computes the correct offsets", func() { 215 doubleNested := func(eventually bool) { 216 func() { 217 if eventually { 218 Eventually(true, "10ms", "5ms").WithOffset(2).Should(BeFalse()) 219 } else { 220 Expect(true).WithOffset(2).To(BeFalse()) 221 } 222 }() 223 } 224 225 reportedFile, reportedLine := "", 0 226 captureLocation := func(message string, skip ...int) { 227 _, reportedFile, reportedLine, _ = runtime.Caller(skip[0] + 1) 228 } 229 230 _, thisFile, anchorLine, _ := runtime.Caller(0) // 0 231 RegisterFailHandler(captureLocation) // 1 232 Expect(true).To(BeFalse()) // *2* 233 RegisterFailHandler(Fail) // 3 234 Ω(reportedFile).Should(Equal(thisFile)) // 4 235 Ω(reportedLine - anchorLine).Should(Equal(2)) // 5 236 RegisterFailHandler(captureLocation) // 6 237 doubleNested(false) // *7* 238 RegisterFailHandler(Fail) // 8 239 Ω(reportedFile).Should(Equal(thisFile)) // 9 240 Ω(reportedLine - anchorLine).Should(Equal(7)) // 10 241 RegisterFailHandler(captureLocation) // 11 242 Eventually(true, "10ms", "5ms").Should(BeFalse()) // *12* 243 RegisterFailHandler(Fail) // 13 244 Ω(reportedFile).Should(Equal(thisFile)) // 14 245 Ω(reportedLine - anchorLine).Should(Equal(12)) // 15 246 RegisterFailHandler(captureLocation) // 16 247 doubleNested(true) // *17* 248 RegisterFailHandler(Fail) // 18 249 Ω(reportedFile).Should(Equal(thisFile)) // 19 250 Ω(reportedLine - anchorLine).Should(Equal(17)) // 20 251 }) 252 }) 253 })