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

     1  package internal_test
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	. "github.com/onsi/ginkgo"
     8  	"github.com/onsi/ginkgo/types"
     9  	. "github.com/onsi/gomega"
    10  
    11  	"github.com/onsi/ginkgo/internal"
    12  )
    13  
    14  func TestInternal(t *testing.T) {
    15  	RegisterFailHandler(Fail)
    16  	RunSpecs(t, "Internal Suite")
    17  }
    18  
    19  type Node = internal.Node
    20  type Nodes = internal.Nodes
    21  type NodeType = types.NodeType
    22  type TreeNode = internal.TreeNode
    23  type TreeNodes = internal.TreeNodes
    24  type Spec = internal.Spec
    25  type Specs = internal.Specs
    26  
    27  var ntIt = types.NodeTypeIt
    28  var ntCon = types.NodeTypeContainer
    29  var ntAf = types.NodeTypeAfterEach
    30  var ntBef = types.NodeTypeBeforeEach
    31  var ntJusAf = types.NodeTypeJustAfterEach
    32  var ntJusBef = types.NodeTypeJustBeforeEach
    33  
    34  type NestingLevel int
    35  
    36  // convenience helper to quickly make nodes
    37  // assumes they are correctly configured and no errors occur
    38  func N(args ...interface{}) Node {
    39  	nodeType, text, nestingLevel, hasBody := types.NodeTypeIt, "", -1, false
    40  	remainingArgs := []interface{}{cl}
    41  	for _, arg := range args {
    42  		switch t := reflect.TypeOf(arg); {
    43  		case t == reflect.TypeOf(NestingLevel(1)):
    44  			nestingLevel = int(arg.(NestingLevel))
    45  		case t == reflect.TypeOf(text):
    46  			text = arg.(string)
    47  		case t == reflect.TypeOf(nodeType):
    48  			nodeType = arg.(types.NodeType)
    49  		case t.Kind() == reflect.Func:
    50  			hasBody = true
    51  			remainingArgs = append(remainingArgs, arg)
    52  		default:
    53  			remainingArgs = append(remainingArgs, arg)
    54  		}
    55  	}
    56  	//the hasBody dance is necessary to (a) make sure internal.NewNode is happy (it requires a body) and (b) to then nil out the resulting body to ensure node comparisons work
    57  	//as reflect.DeepEqual cannot compare functions.  Even by pointer.  'Cause.  You know.
    58  	if !hasBody {
    59  		remainingArgs = append(remainingArgs, func() {})
    60  	}
    61  	node, errors := internal.NewNode(nil, nodeType, text, remainingArgs...)
    62  	if nestingLevel != -1 {
    63  		node.NestingLevel = nestingLevel
    64  	}
    65  	ExpectWithOffset(1, errors).Should(BeEmpty())
    66  	if !hasBody {
    67  		node.Body = nil
    68  	}
    69  	return node
    70  }
    71  
    72  // convenience helper to quickly make tree nodes
    73  func TN(node Node, children ...*TreeNode) *TreeNode {
    74  	tn := &TreeNode{Node: node}
    75  	for _, child := range children {
    76  		tn.AppendChild(child)
    77  	}
    78  	return tn
    79  }
    80  
    81  // convenience helper to quickly make specs
    82  func S(nodes ...Node) Spec {
    83  	return Spec{Nodes: nodes}
    84  }
    85  
    86  // convenience helper to quickly make code locations
    87  func CL(options ...interface{}) types.CodeLocation {
    88  	cl = types.NewCodeLocation(0)
    89  	for _, option := range options {
    90  		if reflect.TypeOf(option).Kind() == reflect.String {
    91  			cl.FileName = option.(string)
    92  		} else if reflect.TypeOf(option).Kind() == reflect.Int {
    93  			cl.LineNumber = option.(int)
    94  		}
    95  	}
    96  	return cl
    97  }
    98  
    99  func mustFindNodeWithText(tree *TreeNode, text string) Node {
   100  	node := findNodeWithText(tree, text)
   101  	ExpectWithOffset(1, node).ShouldNot(BeZero(), "Failed to find node in tree with text '%s'", text)
   102  	return node
   103  }
   104  
   105  func findNodeWithText(tree *TreeNode, text string) Node {
   106  	if tree.Node.Text == text {
   107  		return tree.Node
   108  	}
   109  	for _, tn := range tree.Children {
   110  		n := findNodeWithText(tn, text)
   111  		if !n.IsZero() {
   112  			return n
   113  		}
   114  	}
   115  	return Node{}
   116  }
   117  
   118  var cl types.CodeLocation
   119  var _ = BeforeEach(func() {
   120  	cl = types.NewCodeLocation(0)
   121  })