File: code_location_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 (149 lines) | stat: -rw-r--r-- 4,360 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
package types_test

import (
	"fmt"
	"runtime"

	. "github.com/onsi/ginkgo/v2"
	"github.com/onsi/ginkgo/v2/types"
	. "github.com/onsi/gomega"
)

var _ = Describe("CodeLocation", func() {
	clWithSkip := func(skip int) types.CodeLocation {
		return types.NewCodeLocation(skip)
	}

	helperFunction := func() types.CodeLocation {
		GinkgoHelper()
		return types.NewCodeLocation(0)
	}

	Describe("Creating CodeLocations", func() {
		Context("when skip is 0", func() {
			It("returns the location at which types.NewCodeLocation was called", func() {
				_, fname, lnumber, _ := runtime.Caller(0)
				cl := types.NewCodeLocation(0)
				Ω(cl).Should(Equal(types.CodeLocation{
					FileName:   fname,
					LineNumber: lnumber + 1,
				}))
			})
		})

		Context("when skip is > 0", func() {
			It("returns the appropriate location from the stack", func() {
				_, fname, lnumber, _ := runtime.Caller(0)
				cl := clWithSkip(1)
				Ω(cl).Should(Equal(types.CodeLocation{
					FileName:   fname,
					LineNumber: lnumber + 1,
				}))

				_, fname, lnumber, _ = runtime.Caller(0)
				cl = func() types.CodeLocation {
					return clWithSkip(2)
				}() // this is the line that's expected
				Ω(cl).Should(Equal(types.CodeLocation{
					FileName:   fname,
					LineNumber: lnumber + 3,
				}))
			})
		})

		Describe("when a function has been marked as a helper", func() {
			It("does not include that function when generating a code location", func() {
				_, fname, lnumber, _ := runtime.Caller(0)
				cl := helperFunction()
				Ω(cl).Should(Equal(types.CodeLocation{
					FileName:   fname,
					LineNumber: lnumber + 1,
				}))

				_, fname, lnumber, _ = runtime.Caller(0)
				cl = func() types.CodeLocation {
					GinkgoHelper()
					return func() types.CodeLocation {
						types.MarkAsHelper()
						return helperFunction()
					}()
				}() // this is the line that's expected
				Ω(cl).Should(Equal(types.CodeLocation{
					FileName:   fname,
					LineNumber: lnumber + 7,
				}))
			})
		})
	})

	Describe("stringer behavior", func() {
		It("should stringify nicely", func() {
			_, fname, lnumber, _ := runtime.Caller(0)
			cl := types.NewCodeLocation(0)
			Ω(cl.String()).Should(Equal(fmt.Sprintf("%s:%d", fname, lnumber+1)))
		})
	})

	Describe("with a custom message", func() {
		It("emits the custom message", func() {
			cl := types.NewCustomCodeLocation("I'm right here.")
			Ω(cl.String()).Should(Equal("I'm right here."))
		})
	})

	Describe("Fetching the line from the file in question", func() {
		It("works", func() {
			cl := types.NewCodeLocation(0)
			cl.LineNumber = cl.LineNumber - 2
			Ω(cl.ContentsOfLine()).Should(Equal("\tDescribe(\"Fetching the line from the file in question\", func() {"))
		})

		It("returns empty string if the line is not found or is out of bounds", func() {
			cl := types.CodeLocation{
				FileName:   "foo.go",
				LineNumber: 0,
			}
			Ω(cl.ContentsOfLine()).Should(BeZero())

			cl = types.NewCodeLocation(0)
			cl.LineNumber = cl.LineNumber + 1000000
			Ω(cl.ContentsOfLine()).Should(BeZero())
		})
	})

	Describe("PruneStack", func() {
		It("should remove any references to ginkgo and pkg/testing and pkg/runtime", func() {
			// Hard-coded string, loosely based on what debug.Stack() produces.
			input := `Skip: skip()
/Skip/me
Skip: skip()
/Skip/me
Something: Func()
/Users/whoever/gospace/src/github.com/onsi/ginkgo/whatever.go:10 (0x12314)
SomethingInternalToGinkgo: Func()
/Users/whoever/gospace/src/github.com/onsi/ginkgo/whatever_else.go:10 (0x12314)
Oops: BlowUp()
/usr/goroot/pkg/strings/oops.go:10 (0x12341)
MyCode: Func()
/Users/whoever/gospace/src/mycode/code.go:10 (0x12341)
MyCodeTest: Func()
/Users/whoever/gospace/src/mycode/code_test.go:10 (0x12341)
TestFoo: RunSpecs(t, "Foo Suite")
/Users/whoever/gospace/src/mycode/code_suite_test.go:12 (0x37f08)
TestingT: Blah()
/usr/goroot/pkg/testing/testing.go:12 (0x37f08)
Something: Func()
/usr/goroot/pkg/runtime/runtime.go:12 (0x37f08)
`
			prunedStack := types.PruneStack(input, 1)
			Ω(prunedStack).Should(Equal(`Oops: BlowUp()
/usr/goroot/pkg/strings/oops.go:10 (0x12341)
MyCode: Func()
/Users/whoever/gospace/src/mycode/code.go:10 (0x12341)
MyCodeTest: Func()
/Users/whoever/gospace/src/mycode/code_test.go:10 (0x12341)
TestFoo: RunSpecs(t, "Foo Suite")
/Users/whoever/gospace/src/mycode/code_suite_test.go:12 (0x37f08)`))
		})
	})
})