File: failer_test.go

package info (click to toggle)
golang-github-onsi-ginkgo-v2 2.15.0-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 4,112 kB
  • sloc: javascript: 59; sh: 14; makefile: 7
file content (170 lines) | stat: -rw-r--r-- 4,438 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package internal_test

import (
	. "github.com/onsi/ginkgo/v2"
	"github.com/onsi/ginkgo/v2/internal"
	"github.com/onsi/ginkgo/v2/types"
	. "github.com/onsi/gomega"
)

var _ = Describe("Failer", func() {
	var failer *internal.Failer
	var clA types.CodeLocation
	var clB types.CodeLocation

	BeforeEach(func() {
		clA = CL("file_a.go")
		clB = CL("file_b.go")
		failer = internal.NewFailer()
	})

	Context("with no failures", func() {
		It("should return success when drained", func() {
			state, failure := failer.Drain()
			Ω(state).Should(Equal(types.SpecStatePassed))
			Ω(failure).Should(BeZero())
		})
	})

	Describe("when told of a failure", func() {
		BeforeEach(func() {
			failer.Fail("something failed", clA)
		})

		It("should record the failure", func() {
			state, failure := failer.Drain()
			Ω(state).Should(Equal(types.SpecStateFailed))
			Ω(failure).Should(Equal(types.Failure{
				Message:  "something failed",
				Location: clA,
			}))
		})

		Context("when told of anotehr failure", func() {
			It("discards the second failure, preserving the original", func() {
				failer.Fail("something else failed", clB)

				state, failure := failer.Drain()
				Ω(state).Should(Equal(types.SpecStateFailed))
				Ω(failure).Should(Equal(types.Failure{
					Message:  "something failed",
					Location: clA,
				}))
			})
		})
	})

	Describe("when told to skip", func() {
		Context("when no failure has occurred", func() {
			It("registers the test as skipped", func() {
				failer.Skip("something skipped", clA)
				state, failure := failer.Drain()
				Ω(state).Should(Equal(types.SpecStateSkipped))
				Ω(failure).Should(Equal(types.Failure{
					Message:  "something skipped",
					Location: clA,
				}))
			})
		})

		Context("when a failure has already occurred", func() {
			BeforeEach(func() {
				failer.Fail("something failed", clA)
			})

			It("does not modify the failure", func() {
				failer.Skip("something skipped", clB)
				state, failure := failer.Drain()
				Ω(state).Should(Equal(types.SpecStateFailed))
				Ω(failure).Should(Equal(types.Failure{
					Message:  "something failed",
					Location: clA,
				}))
			})
		})
	})

	Describe("when told to abort", func() {
		Context("when no failure has occurred", func() {
			It("registers the test as aborted", func() {
				failer.AbortSuite("something aborted", clA)
				state, failure := failer.Drain()
				Ω(state).Should(Equal(types.SpecStateAborted))
				Ω(failure).Should(Equal(types.Failure{
					Message:  "something aborted",
					Location: clA,
				}))
			})
		})

		Context("when a failure has already occurred", func() {
			BeforeEach(func() {
				failer.Fail("something failed", clA)
			})

			It("does not modify the failure", func() {
				failer.AbortSuite("something aborted", clA)
				state, failure := failer.Drain()
				Ω(state).Should(Equal(types.SpecStateFailed))
				Ω(failure).Should(Equal(types.Failure{
					Message:  "something failed",
					Location: clA,
				}))
			})
		})
	})

	Describe("when told to panic", func() {
		BeforeEach(func() {
			failer.Panic(clA, 17)
		})

		It("should record the panic", func() {
			state, failure := failer.Drain()
			Ω(state).Should(Equal(types.SpecStatePanicked))
			Ω(failure).Should(Equal(types.Failure{
				Message:        "Test Panicked",
				Location:       clA,
				ForwardedPanic: "17",
			}))
		})

		Context("when told of another panic", func() {
			It("discards the second panic, preserving the original", func() {
				failer.Panic(clB, 23)

				state, failure := failer.Drain()
				Ω(state).Should(Equal(types.SpecStatePanicked))
				Ω(failure).Should(Equal(types.Failure{
					Message:        "Test Panicked",
					Location:       clA,
					ForwardedPanic: "17",
				}))
			})
		})
	})

	Context("when drained", func() {
		BeforeEach(func() {
			failer.Fail("something failed", clA)
			state, _ := failer.Drain()
			Ω(state).Should(Equal(types.SpecStateFailed))
		})

		It("resets the failer such that subsequent drains pass", func() {
			state, failure := failer.Drain()
			Ω(state).Should(Equal(types.SpecStatePassed))
			Ω(failure).Should(BeZero())
		})

		It("allows subsequent failures to be recorded", func() {
			failer.Fail("something else failed", clB)
			state, failure := failer.Drain()
			Ω(state).Should(Equal(types.SpecStateFailed))
			Ω(failure).Should(Equal(types.Failure{
				Message:  "something else failed",
				Location: clB,
			}))
		})
	})
})