File: tree_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 (187 lines) | stat: -rw-r--r-- 5,115 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package internal_test

import (
	. "github.com/onsi/ginkgo/v2"
	. "github.com/onsi/gomega"

	"github.com/onsi/ginkgo/v2/internal"
)

var _ = Describe("Trees (TreeNode and TreeNodes)", func() {
	Describe("TreeNodes methods", func() {
		var n1, n2, n3 Node
		var childNode Node
		var treeNodes TreeNodes
		BeforeEach(func() {
			n1, n2, n3 = N(), N(), N()
			childNode = N()
			treeNodes = TreeNodes{
				TN(n1,
					TN(childNode),
				),
				TN(n2),
				TN(n3),
			}
		})

		Describe("treenodes.Nodes", func() {
			It("returns the root node of each node in the treenodes slice", func() {
				Ω(treeNodes.Nodes()).Should(Equal(Nodes{n1, n2, n3}))
			})
		})

		Describe("treenodes.WithId", func() {
			Context("when a tree with a root node with a matching id is found", func() {
				It("returns that tree", func() {
					Ω(treeNodes.WithID(n2.ID)).Should(Equal(TN(n2)))
				})
			})

			Context("when the id matches a child node's id", func() {
				It("returns an empty tree as children are not included in the match", func() {
					Ω(treeNodes.WithID(childNode.ID)).Should(BeNil())
				})
			})

			Context("when the id cannot be found", func() {
				It("returns an empty tree", func() {
					Ω(treeNodes.WithID(1000000)).Should(BeZero()) //pretty sure it's a safe bet we don't ever get to 1_000_000 nodes in this test ;)
				})
			})
		})

		Describe("AppendChild", func() {
			It("appends the passed in child treenode to the parent's children and sets the child's parent", func() {
				existingChildNode1 := N()
				existingChildNode2 := N()
				treeNode := TN(N(),
					TN(existingChildNode1),
					TN(existingChildNode2),
				)
				newChildNode := N()
				childTreeNode := &TreeNode{Node: newChildNode}
				treeNode.AppendChild(childTreeNode)
				Ω(treeNode.Children.Nodes()).Should(Equal(Nodes{existingChildNode1, existingChildNode2, newChildNode}))
				Ω(childTreeNode.Parent).Should(Equal(treeNode))
			})
		})

		Describe("ParentChain", func() {
			It("returns the chain of parent nodes", func() {
				grandparent := N()
				parent := N()
				aunt := N()
				child := N()
				sibling := N()
				tree := TN(Node{}, TN(
					grandparent,
					TN(parent, TN(child), TN(sibling)),
					TN(aunt),
				))
				childTree := tree.Children[0].Children[0].Children[0]
				Ω(childTree.Node).Should(Equal(child))
				Ω(childTree.AncestorNodeChain()).Should(Equal(Nodes{grandparent, parent, child}))
			})
		})
	})

	Describe("GenerateSpecsFromTreeRoot", func() {
		var tree *TreeNode
		BeforeEach(func() {
			tree = &TreeNode{}
		})

		Context("when the tree is empty", func() {
			It("returns an empty set of tests", func() {
				Ω(internal.GenerateSpecsFromTreeRoot(tree)).Should(BeEmpty())
			})
		})

		Context("when the tree has no Its", func() {
			BeforeEach(func() {
				tree = TN(Node{},
					TN(N(ntBef)),
					TN(N(ntCon),
						TN(N(ntBef)),
						TN(N(ntAf)),
					),
					TN(N(ntCon),
						TN(N(ntCon),
							TN(N(ntBef)),
							TN(N(ntAf)),
						),
					),
					TN(N(ntAf)),
				)
			})

			It("returns an empty set of tests", func() {
				Ω(internal.GenerateSpecsFromTreeRoot(tree)).Should(BeEmpty())
			})
		})

		Context("when the tree has nodes in it", func() {
			var tests Specs
			BeforeEach(func() {
				tree = TN(Node{},
					TN(N(ntBef, "Bef #0")),
					TN(N(ntIt, "It #1")),
					TN(N(ntCon, "Container #1"),
						TN(N(ntBef, "Bef #1")),
						TN(N(ntAf, "Af #1")),
						TN(N(ntIt, "It #2")),
					),
					TN(N(ntCon, "Container #2"),
						TN(N(ntBef, "Bef #2")),
						TN(N(ntCon, "Nested Container"),
							TN(N(ntBef, "Bef #4")),
							TN(N(ntIt, "It #3")),
							TN(N(ntIt, "It #4")),
							TN(N(ntAf, "Af #2")),
						),
						TN(N(ntIt, "It #5")),
						TN(N(ntCon, "A Container With No Its"),
							TN(N(ntBef, "Bef #5")),
						),
						TN(N(ntAf, "Af #3")),
					),
					TN(N(ntIt, "It #6")),
					TN(N(ntAf, "Af #4")),
				)

				tests = internal.GenerateSpecsFromTreeRoot(tree)
			})

			It("constructs a flattened set of tests", func() {
				Ω(tests).Should(HaveLen(6))
				expectedTexts := [][]string{
					{"Bef #0", "It #1", "Af #4"},
					{"Bef #0", "Container #1", "Bef #1", "Af #1", "It #2", "Af #4"},
					{"Bef #0", "Container #2", "Bef #2", "Nested Container", "Bef #4", "It #3", "Af #2", "Af #3", "Af #4"},
					{"Bef #0", "Container #2", "Bef #2", "Nested Container", "Bef #4", "It #4", "Af #2", "Af #3", "Af #4"},
					{"Bef #0", "Container #2", "Bef #2", "It #5", "Af #3", "Af #4"},
					{"Bef #0", "It #6", "Af #4"},
				}
				for i, expectedText := range expectedTexts {
					Ω(tests[i].Nodes.Texts()).Should(Equal(expectedText))
				}
			})

			It("ensures each node as the correct nesting level", func() {
				extpectedNestingLevels := [][]int{
					{0, 0, 0},
					{0, 0, 1, 1, 1, 0},
					{0, 0, 1, 1, 2, 2, 2, 1, 0},
					{0, 0, 1, 1, 2, 2, 2, 1, 0},
					{0, 0, 1, 1, 1, 0},
					{0, 0, 0},
				}
				for i, expectedNestingLevels := range extpectedNestingLevels {
					for j, expectedNestingLevel := range expectedNestingLevels {
						Ω(tests[i].Nodes[j].NestingLevel).Should(Equal(expectedNestingLevel))
					}
				}
			})
		})
	})
})