github.com/onsi/ginkgo@v1.16.6-0.20211118180735-4e1925ba4c95/internal/tree_test.go (about)

     1  package internal_test
     2  
     3  import (
     4  	. "github.com/onsi/ginkgo"
     5  	. "github.com/onsi/gomega"
     6  
     7  	"github.com/onsi/ginkgo/internal"
     8  )
     9  
    10  var _ = Describe("Trees (TreeNode and TreeNodes)", func() {
    11  	Describe("TreeNodes methods", func() {
    12  		var n1, n2, n3 Node
    13  		var childNode Node
    14  		var treeNodes TreeNodes
    15  		BeforeEach(func() {
    16  			n1, n2, n3 = N(), N(), N()
    17  			childNode = N()
    18  			treeNodes = TreeNodes{
    19  				TN(n1,
    20  					TN(childNode),
    21  				),
    22  				TN(n2),
    23  				TN(n3),
    24  			}
    25  		})
    26  
    27  		Describe("treenodes.Nodes", func() {
    28  			It("returns the root node of each node in the treenodes slice", func() {
    29  				Ω(treeNodes.Nodes()).Should(Equal(Nodes{n1, n2, n3}))
    30  			})
    31  		})
    32  
    33  		Describe("treenodes.WithId", func() {
    34  			Context("when a tree with a root node with a matching id is found", func() {
    35  				It("returns that tree", func() {
    36  					Ω(treeNodes.WithID(n2.ID)).Should(Equal(TN(n2)))
    37  				})
    38  			})
    39  
    40  			Context("when the id matches a child node's id", func() {
    41  				It("returns an empty tree as children are not included in the match", func() {
    42  					Ω(treeNodes.WithID(childNode.ID)).Should(BeNil())
    43  				})
    44  			})
    45  
    46  			Context("when the id cannot be found", func() {
    47  				It("returns an empty tree", func() {
    48  					Ω(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 ;)
    49  				})
    50  			})
    51  		})
    52  
    53  		Describe("AppendChild", func() {
    54  			It("appends the passed in child treenode to the parent's children and sets the child's parent", func() {
    55  				existingChildNode1 := N()
    56  				existingChildNode2 := N()
    57  				treeNode := TN(N(),
    58  					TN(existingChildNode1),
    59  					TN(existingChildNode2),
    60  				)
    61  				newChildNode := N()
    62  				childTreeNode := &TreeNode{Node: newChildNode}
    63  				treeNode.AppendChild(childTreeNode)
    64  				Ω(treeNode.Children.Nodes()).Should(Equal(Nodes{existingChildNode1, existingChildNode2, newChildNode}))
    65  				Ω(childTreeNode.Parent).Should(Equal(treeNode))
    66  			})
    67  		})
    68  
    69  		Describe("ParentChain", func() {
    70  			It("returns the chain of parent nodes", func() {
    71  				grandparent := N()
    72  				parent := N()
    73  				aunt := N()
    74  				child := N()
    75  				sibling := N()
    76  				tree := TN(Node{}, TN(
    77  					grandparent,
    78  					TN(parent, TN(child), TN(sibling)),
    79  					TN(aunt),
    80  				))
    81  				childTree := tree.Children[0].Children[0].Children[0]
    82  				Ω(childTree.Node).Should(Equal(child))
    83  				Ω(childTree.AncestorNodeChain()).Should(Equal(Nodes{grandparent, parent, child}))
    84  			})
    85  		})
    86  	})
    87  
    88  	Describe("GenerateSpecsFromTreeRoot", func() {
    89  		var tree *TreeNode
    90  		BeforeEach(func() {
    91  			tree = &TreeNode{}
    92  		})
    93  
    94  		Context("when the tree is empty", func() {
    95  			It("returns an empty set of tests", func() {
    96  				Ω(internal.GenerateSpecsFromTreeRoot(tree)).Should(BeEmpty())
    97  			})
    98  		})
    99  
   100  		Context("when the tree has no Its", func() {
   101  			BeforeEach(func() {
   102  				tree = TN(Node{},
   103  					TN(N(ntBef)),
   104  					TN(N(ntCon),
   105  						TN(N(ntBef)),
   106  						TN(N(ntAf)),
   107  					),
   108  					TN(N(ntCon),
   109  						TN(N(ntCon),
   110  							TN(N(ntBef)),
   111  							TN(N(ntAf)),
   112  						),
   113  					),
   114  					TN(N(ntAf)),
   115  				)
   116  			})
   117  
   118  			It("returns an empty set of tests", func() {
   119  				Ω(internal.GenerateSpecsFromTreeRoot(tree)).Should(BeEmpty())
   120  			})
   121  		})
   122  
   123  		Context("when the tree has nodes in it", func() {
   124  			var tests Specs
   125  			BeforeEach(func() {
   126  				tree = TN(Node{},
   127  					TN(N(ntBef, "Bef #0")),
   128  					TN(N(ntIt, "It #1")),
   129  					TN(N(ntCon, "Container #1"),
   130  						TN(N(ntBef, "Bef #1")),
   131  						TN(N(ntAf, "Af #1")),
   132  						TN(N(ntIt, "It #2")),
   133  					),
   134  					TN(N(ntCon, "Container #2"),
   135  						TN(N(ntBef, "Bef #2")),
   136  						TN(N(ntCon, "Nested Container"),
   137  							TN(N(ntBef, "Bef #4")),
   138  							TN(N(ntIt, "It #3")),
   139  							TN(N(ntIt, "It #4")),
   140  							TN(N(ntAf, "Af #2")),
   141  						),
   142  						TN(N(ntIt, "It #5")),
   143  						TN(N(ntCon, "A Container With No Its"),
   144  							TN(N(ntBef, "Bef #5")),
   145  						),
   146  						TN(N(ntAf, "Af #3")),
   147  					),
   148  					TN(N(ntIt, "It #6")),
   149  					TN(N(ntAf, "Af #4")),
   150  				)
   151  
   152  				tests = internal.GenerateSpecsFromTreeRoot(tree)
   153  			})
   154  
   155  			It("constructs a flattened set of tests", func() {
   156  				Ω(tests).Should(HaveLen(6))
   157  				expectedTexts := [][]string{
   158  					{"Bef #0", "It #1", "Af #4"},
   159  					{"Bef #0", "Container #1", "Bef #1", "Af #1", "It #2", "Af #4"},
   160  					{"Bef #0", "Container #2", "Bef #2", "Nested Container", "Bef #4", "It #3", "Af #2", "Af #3", "Af #4"},
   161  					{"Bef #0", "Container #2", "Bef #2", "Nested Container", "Bef #4", "It #4", "Af #2", "Af #3", "Af #4"},
   162  					{"Bef #0", "Container #2", "Bef #2", "It #5", "Af #3", "Af #4"},
   163  					{"Bef #0", "It #6", "Af #4"},
   164  				}
   165  				for i, expectedText := range expectedTexts {
   166  					Ω(tests[i].Nodes.Texts()).Should(Equal(expectedText))
   167  				}
   168  			})
   169  
   170  			It("ensures each node as the correct nesting level", func() {
   171  				extpectedNestingLevels := [][]int{
   172  					{0, 0, 0},
   173  					{0, 0, 1, 1, 1, 0},
   174  					{0, 0, 1, 1, 2, 2, 2, 1, 0},
   175  					{0, 0, 1, 1, 2, 2, 2, 1, 0},
   176  					{0, 0, 1, 1, 1, 0},
   177  					{0, 0, 0},
   178  				}
   179  				for i, expectedNestingLevels := range extpectedNestingLevels {
   180  					for j, expectedNestingLevel := range expectedNestingLevels {
   181  						Ω(tests[i].Nodes[j].NestingLevel).Should(Equal(expectedNestingLevel))
   182  					}
   183  				}
   184  			})
   185  		})
   186  	})
   187  })