...
1 package internal_test
2
3 import (
4 "runtime"
5
6 . "github.com/onsi/ginkgo/v2"
7 . "github.com/onsi/gomega"
8 "github.com/onsi/gomega/internal"
9 )
10
11 var _ = Describe("Gomega", func() {
12 It("is mostly tested in assertion_test and async_assertion_test", func() {
13
14 })
15 Describe("when initialized", func() {
16 var g *internal.Gomega
17
18 BeforeEach(func() {
19 g = internal.NewGomega(internal.DurationBundle{})
20 Ω(g.Fail).Should(BeNil())
21 Ω(g.THelper).Should(BeNil())
22 })
23
24 It("should be registered as unconfigured", func() {
25 Ω(g.IsConfigured()).Should(BeFalse())
26 })
27
28 Context("when configured with a fail handler", func() {
29 It("registers the fail handler and a no-op helper", func() {
30 var capturedMessage string
31 g.ConfigureWithFailHandler(func(message string, skip ...int) {
32 capturedMessage = message
33 })
34 Ω(g.IsConfigured()).Should(BeTrue())
35
36 g.Fail("hi bob")
37 Ω(capturedMessage).Should(Equal("hi bob"))
38 Ω(g.THelper).ShouldNot(Panic())
39 })
40 })
41
42 Context("when configured with a T", func() {
43 It("registers a fail handler an the T's helper", func() {
44 fake := &FakeGomegaTestingT{}
45 g.ConfigureWithT(fake)
46 Ω(g.IsConfigured()).Should(BeTrue())
47
48 g.Fail("hi bob")
49 Ω(fake.CalledHelper).Should(BeTrue())
50 Ω(fake.CalledFatalf).Should(Equal("\nhi bob"))
51
52 fake.CalledHelper = false
53 g.THelper()
54 Ω(fake.CalledHelper).Should(BeTrue())
55 })
56 })
57 })
58
59 Describe("Offset", func() {
60 It("computes the correct offsets", func() {
61 doubleNested := func(g Gomega, eventually bool) {
62 func() {
63 if eventually {
64 g.Eventually(true, "10ms", "5ms").WithOffset(2).Should(BeFalse())
65 } else {
66 g.Expect(true).WithOffset(2).To(BeFalse())
67 }
68 }()
69 }
70
71 reportedFile, reportedLine := "", 0
72 _, thisFile, anchorLine, _ := runtime.Caller(0)
73 g := NewGomega(func(message string, skip ...int) {
74 _, reportedFile, reportedLine, _ = runtime.Caller(skip[0] + 1)
75 })
76 g.Expect(true).To(BeFalse())
77 Ω(reportedFile).Should(Equal(thisFile))
78 Ω(reportedLine - anchorLine).Should(Equal(4))
79 doubleNested(g, false)
80 Ω(reportedFile).Should(Equal(thisFile))
81 Ω(reportedLine - anchorLine).Should(Equal(7))
82 g.Eventually(true, "10ms", "5ms").Should(BeFalse())
83 Ω(reportedFile).Should(Equal(thisFile))
84 Ω(reportedLine - anchorLine).Should(Equal(10))
85 doubleNested(g, true)
86 Ω(reportedFile).Should(Equal(thisFile))
87 Ω(reportedLine - anchorLine).Should(Equal(13))
88 })
89 })
90 })
91
View as plain text