github.com/onsi/ginkgo@v1.16.6-0.20211118180735-4e1925ba4c95/internal/failer_test.go (about) 1 package internal_test 2 3 import ( 4 . "github.com/onsi/ginkgo" 5 "github.com/onsi/ginkgo/internal" 6 "github.com/onsi/ginkgo/types" 7 . "github.com/onsi/gomega" 8 ) 9 10 var _ = Describe("Failer", func() { 11 var failer *internal.Failer 12 var clA types.CodeLocation 13 var clB types.CodeLocation 14 15 BeforeEach(func() { 16 clA = CL("file_a.go") 17 clB = CL("file_b.go") 18 failer = internal.NewFailer() 19 }) 20 21 Context("with no failures", func() { 22 It("should return success when drained", func() { 23 state, failure := failer.Drain() 24 Ω(state).Should(Equal(types.SpecStatePassed)) 25 Ω(failure).Should(BeZero()) 26 }) 27 }) 28 29 Describe("when told of a failure", func() { 30 BeforeEach(func() { 31 failer.Fail("something failed", clA) 32 }) 33 34 It("should record the failure", func() { 35 state, failure := failer.Drain() 36 Ω(state).Should(Equal(types.SpecStateFailed)) 37 Ω(failure).Should(Equal(types.Failure{ 38 Message: "something failed", 39 Location: clA, 40 })) 41 }) 42 43 Context("when told of anotehr failure", func() { 44 It("discards the second failure, preserving the original", func() { 45 failer.Fail("something else failed", clB) 46 47 state, failure := failer.Drain() 48 Ω(state).Should(Equal(types.SpecStateFailed)) 49 Ω(failure).Should(Equal(types.Failure{ 50 Message: "something failed", 51 Location: clA, 52 })) 53 }) 54 }) 55 }) 56 57 Describe("when told to skip", func() { 58 Context("when no failure has occured", func() { 59 It("registers the test as skipped", func() { 60 failer.Skip("something skipped", clA) 61 state, failure := failer.Drain() 62 Ω(state).Should(Equal(types.SpecStateSkipped)) 63 Ω(failure).Should(Equal(types.Failure{ 64 Message: "something skipped", 65 Location: clA, 66 })) 67 }) 68 }) 69 70 Context("when a failure has already occured", func() { 71 BeforeEach(func() { 72 failer.Fail("something failed", clA) 73 }) 74 75 It("does not modify the failure", func() { 76 failer.Skip("something skipped", clB) 77 state, failure := failer.Drain() 78 Ω(state).Should(Equal(types.SpecStateFailed)) 79 Ω(failure).Should(Equal(types.Failure{ 80 Message: "something failed", 81 Location: clA, 82 })) 83 }) 84 }) 85 }) 86 87 Describe("when told to abort", func() { 88 Context("when no failure has occured", func() { 89 It("registers the test as aborted", func() { 90 failer.AbortSuite("something aborted", clA) 91 state, failure := failer.Drain() 92 Ω(state).Should(Equal(types.SpecStateAborted)) 93 Ω(failure).Should(Equal(types.Failure{ 94 Message: "something aborted", 95 Location: clA, 96 })) 97 }) 98 }) 99 100 Context("when a failure has already occured", func() { 101 BeforeEach(func() { 102 failer.Fail("something failed", clA) 103 }) 104 105 It("does not modify the failure", func() { 106 failer.AbortSuite("something aborted", clA) 107 state, failure := failer.Drain() 108 Ω(state).Should(Equal(types.SpecStateFailed)) 109 Ω(failure).Should(Equal(types.Failure{ 110 Message: "something failed", 111 Location: clA, 112 })) 113 }) 114 }) 115 }) 116 117 Describe("when told to panic", func() { 118 BeforeEach(func() { 119 failer.Panic(clA, 17) 120 }) 121 122 It("should record the panic", func() { 123 state, failure := failer.Drain() 124 Ω(state).Should(Equal(types.SpecStatePanicked)) 125 Ω(failure).Should(Equal(types.Failure{ 126 Message: "Test Panicked", 127 Location: clA, 128 ForwardedPanic: "17", 129 })) 130 }) 131 132 Context("when told of another panic", func() { 133 It("discards the second panic, preserving the original", func() { 134 failer.Panic(clB, 23) 135 136 state, failure := failer.Drain() 137 Ω(state).Should(Equal(types.SpecStatePanicked)) 138 Ω(failure).Should(Equal(types.Failure{ 139 Message: "Test Panicked", 140 Location: clA, 141 ForwardedPanic: "17", 142 })) 143 }) 144 }) 145 }) 146 147 Context("when drained", func() { 148 BeforeEach(func() { 149 failer.Fail("something failed", clA) 150 state, _ := failer.Drain() 151 Ω(state).Should(Equal(types.SpecStateFailed)) 152 }) 153 154 It("resets the failer such that subsequent drains pass", func() { 155 state, failure := failer.Drain() 156 Ω(state).Should(Equal(types.SpecStatePassed)) 157 Ω(failure).Should(BeZero()) 158 }) 159 160 It("allows subsequent failures to be recorded", func() { 161 failer.Fail("something else failed", clB) 162 state, failure := failer.Drain() 163 Ω(state).Should(Equal(types.SpecStateFailed)) 164 Ω(failure).Should(Equal(types.Failure{ 165 Message: "something else failed", 166 Location: clB, 167 })) 168 }) 169 }) 170 })