How to use ginkgoNodeFromCallExpr method of outline Package

Best Ginkgo code snippet using outline.ginkgoNodeFromCallExpr

ginkgo.go

Source:ginkgo.go Github

copy

Full Screen

...110// end positions.111func absoluteOffsetsForNode(fset *token.FileSet, n ast.Node) (start, end int) {112	return fset.PositionFor(n.Pos(), false).Offset, fset.PositionFor(n.End(), false).Offset113}114// ginkgoNodeFromCallExpr derives an outline entry from a go AST subtree115// corresponding to a Ginkgo container or spec.116func ginkgoNodeFromCallExpr(fset *token.FileSet, ce *ast.CallExpr, ginkgoPackageName, tablePackageName *string) (*ginkgoNode, bool) {117	packageName, identName, ok := packageAndIdentNamesFromCallExpr(ce)118	if !ok {119		return nil, false120	}121	n := ginkgoNode{}122	n.Name = identName123	n.Start, n.End = absoluteOffsetsForNode(fset, ce)124	n.Nodes = make([]*ginkgoNode, 0)125	switch identName {126	case "It", "Measure", "Specify":127		n.Spec = true128		n.Text = textOrAltFromCallExpr(ce, undefinedTextAlt)129		return &n, ginkgoPackageName != nil && *ginkgoPackageName == packageName130	case "Entry":...

Full Screen

Full Screen

outline.go

Source:outline.go Github

copy

Full Screen

1package outline2import (3	"encoding/json"4	"fmt"5	"go/ast"6	"go/token"7	"strconv"8	"strings"9	"golang.org/x/tools/go/ast/inspector"10)11const (12	UndefinedTextAlt = "undefined"13)14type GinkgoMetadata struct {15	Name     string `json:"name"`16	Position string `json:"position"`17	Text     string `json:"text"`18	Spec    bool `json:"spec"`19	Focused bool `json:"focused"`20	Pending bool `json:"pending"`21}22type GinkgoNode struct {23	GinkgoMetadata24	Nodes []*GinkgoNode `json:"nodes,omitempty"`25}26type WalkFunc func(n *GinkgoNode)27func (n *GinkgoNode) Walk(f WalkFunc) {28	f(n)29	for _, m := range n.Nodes {30		m.Walk(f)31	}32}33func GinkgoNodeFromCallExpr(ce *ast.CallExpr, fset *token.FileSet) (*GinkgoNode, bool) {34	id, ok := ce.Fun.(*ast.Ident)35	if !ok {36		return nil, false37	}38	n := GinkgoNode{}39	n.Name = id.Name40	n.Position = fset.Position(ce.Pos()).String()41	// TODO: Handle nodot and alias imports of the ginkgo package.42	// The below assumes dot imports .43	switch id.Name {44	case "It", "Measure", "Specify":45		n.Spec = true46		n.Text = TextOrAltFromCallExpr(ce, UndefinedTextAlt)47	case "FIt", "FMeasure", "FSpecify":48		n.Spec = true49		n.Focused = true50		n.Text = TextOrAltFromCallExpr(ce, UndefinedTextAlt)51	case "PIt", "PMeasure", "PSpecify", "XIt", "XMeasure", "XSpecify":52		n.Spec = true53		n.Pending = true54		n.Text = TextOrAltFromCallExpr(ce, UndefinedTextAlt)55	case "Context", "Describe", "When":56		n.Text = TextOrAltFromCallExpr(ce, UndefinedTextAlt)57	case "FContext", "FDescribe", "FWhen":58		n.Focused = true59		n.Text = TextOrAltFromCallExpr(ce, UndefinedTextAlt)60	case "PContext", "PDescribe", "PWhen", "XContext", "XDescribe", "XWhen":61		n.Pending = true62		n.Text = TextOrAltFromCallExpr(ce, UndefinedTextAlt)63	case "By":64	case "AfterEach", "BeforeEach":65	case "JustAfterEach", "JustBeforeEach":66	case "AfterSuite", "BeforeSuite":67	case "SynchronizedAfterSuite", "SynchronizedBeforeSuite":68	default:69		return nil, false70	}71	return &n, true72}73func TextOrAltFromCallExpr(ce *ast.CallExpr, alt string) string {74	text, defined := TextFromCallExpr(ce)75	if !defined {76		return alt77	}78	return text79}80func TextFromCallExpr(ce *ast.CallExpr) (string, bool) {81	if len(ce.Args) < 1 {82		return "", false83	}84	text, ok := ce.Args[0].(*ast.BasicLit)85	if !ok {86		return "", false87	}88	switch text.Kind {89	case token.CHAR, token.STRING:90		// For token.CHAR and token.STRING, Value is quoted91		unquoted, err := strconv.Unquote(text.Value)92		if err != nil {93			// If unquoting fails, just use the raw Value94			return text.Value, true95		}96		return unquoted, true97	default:98		return text.Value, true99	}100}101func FromASTFile(fset *token.FileSet, src *ast.File) (*Outline, error) {102	root := GinkgoNode{103		Nodes: []*GinkgoNode{},104	}105	stack := []*GinkgoNode{&root}106	ispr := inspector.New([]*ast.File{src})107	ispr.Nodes([]ast.Node{(*ast.CallExpr)(nil)}, func(node ast.Node, push bool) bool {108		ce, ok := node.(*ast.CallExpr)109		if !ok {110			panic(fmt.Errorf("node is not an *ast.CallExpr: %s", fset.Position(node.Pos())))111		}112		gn, ok := GinkgoNodeFromCallExpr(ce, fset)113		if !ok {114			// Not a Ginkgo call, continue115			return true116		}117		// Visiting this node on the way down118		if push {119			parent := stack[len(stack)-1]120			if parent.Pending {121				gn.Pending = true122			}123			// TODO: Update focused based on ginkgo behavior:124			// > Nested programmatically focused specs follow a simple rule: if125			// > a leaf-node is marked focused, any of its ancestor nodes that126			// > are marked focus will be unfocused.127			parent.Nodes = append(parent.Nodes, gn)128			stack = append(stack, gn)129			return true130		}131		// Visiting node on the way up132		stack = stack[0 : len(stack)-1]133		return true134	})135	return &Outline{136		outerNodes: root.Nodes,137	}, nil138}139type Outline struct {140	outerNodes []*GinkgoNode141}142func (o *Outline) MarshalJSON() ([]byte, error) {143	return json.Marshal(o.outerNodes)144}145func (o *Outline) String() string {146	var b strings.Builder147	f := func(n *GinkgoNode) {148		b.WriteString(fmt.Sprintf("%s,%s,%s\n", n.Name, n.Text, n.Position))149	}150	for _, n := range o.outerNodes {151		n.Walk(f)152	}153	return b.String()154}...

Full Screen

Full Screen

ginkgoNodeFromCallExpr

Using AI Code Generation

copy

Full Screen

1import (2type outline struct{}3func (o outline) ginkgoNodeFromCallExpr(node ast.Node) ast.Node {4	callExpr, ok := node.(*ast.CallExpr)5	if !ok {6	}7	selector, ok := callExpr.Fun.(*ast.SelectorExpr)8	if !ok {9	}10	if selector.Sel.Name != "Describe" && selector.Sel.Name != "Context" && selector.Sel.Name != "It" {11	}12}13func main() {14	fset := token.NewFileSet()15	f, err := parser.ParseFile(fset, "1.go", nil, parser.ParseComments)16	if err != nil {17		fmt.Println(err)18	}19	ast.Inspect(f, o.ginkgoNodeFromCallExpr)20}21import (22type outline struct{}23func (o outline) ginkgoNodeFromCallExpr(node ast.Node) ast.Node {24	callExpr, ok := node.(*ast.CallExpr)25	if !ok {26	}27	selector, ok := callExpr.Fun.(*ast.SelectorExpr)28	if !ok {29	}30	if selector.Sel.Name != "Describe" && selector.Sel.Name != "Context" && selector.Sel.Name != "It" {31	}32}33func main() {34	fset := token.NewFileSet()35	f, err := parser.ParseFile(fset, "2.go", nil, parser.ParseComments)36	if err != nil {37		fmt.Println(err)38	}39	ast.Inspect(f, o.ginkgoNodeFromCallExpr)40}41import (42type outline struct{}43func (o outline) ginkgoNodeFromCallExpr(node ast.Node) ast.Node {44	callExpr, ok := node.(*ast.CallExpr)45	if !ok {

Full Screen

Full Screen

ginkgoNodeFromCallExpr

Using AI Code Generation

copy

Full Screen

1import (2func main() {3	err := filepath.Walk(".", func(path string, info os.FileInfo, err error) error {4		if strings.HasSuffix(path, ".go") {5			files = append(files, path)6		}7	})8	if err != nil {9		log.Fatal(err)10	}11	for _, file := range files {12		fset := token.NewFileSet()13		f, err := parser.ParseFile(fset, file, nil, parser.ParseComments)14		if err != nil {15			log.Fatal(err)16		}17		for _, decl := range f.Decls {18			if fn, ok := decl.(*ast.FuncDecl); ok {19				if fn.Recv != nil {20					if len(fn.Recv.List) == 1 {21						if starExpr, ok := fn.Recv.List[0].Type.(*ast.StarExpr); ok {22							if ident, ok := starExpr.X.(*ast.Ident); ok {23								if ident.Name == "GinkgoT" {24									fmt.Printf("%s25								}26							}27						}28					}29				}30			}31		}32	}33}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful