How to use textFromCallExpr method of outline Package

Best Ginkgo code snippet using outline.textFromCallExpr

ginkgo.go

Source:ginkgo.go Github

copy

Full Screen

...190}191// textOrAltFromCallExpr tries to derive the "text" of a Ginkgo spec or192// container. If it cannot derive it, it returns the alt text.193func textOrAltFromCallExpr(ce *ast.CallExpr, alt string) string {194	text, defined := textFromCallExpr(ce)195	if !defined {196		return alt197	}198	return text199}200// textFromCallExpr tries to derive the "text" of a Ginkgo spec or container. If201// it cannot derive it, it returns false.202func textFromCallExpr(ce *ast.CallExpr) (string, bool) {203	if len(ce.Args) < 1 {204		return "", false205	}206	text, ok := ce.Args[0].(*ast.BasicLit)207	if !ok {208		return "", false209	}210	switch text.Kind {211	case token.CHAR, token.STRING:212		// For token.CHAR and token.STRING, Value is quoted213		unquoted, err := strconv.Unquote(text.Value)214		if err != nil {215			// If unquoting fails, just use the raw Value216			return text.Value, true...

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

textFromCallExpr

Using AI Code Generation

copy

Full Screen

1import (2func main() {3	f, err := parser.ParseFile(fset, "1.go", nil, parser.ParseComments)4	if err != nil {5		fmt.Println(err)6	}7	for _, s := range f.Imports {8		fmt.Println(s.Path.Value)9	}10	for _, c := range f.Comments {11		fmt.Println(c.Text())12	}13	ast.Inspect(f, func(n ast.Node) bool {14		switch x := n.(type) {15			fmt.Println(x.List)16			fmt.Println(x.Text)17		}18	})19	ast.Inspect(f, func(n ast.Node) bool {20		switch x := n.(type) {21			fmt.Println(x.Name)22			fmt.Println(x.List)23			fmt.Println(x.Text)24		}25	})26	ast.Inspect(f, func(n ast.Node) bool {27		switch x := n.(type) {28			fmt.Println(x.List)29		}30	})31	ast.Inspect(f, func(n ast.Node) bool {32		switch x := n.(type) {33			fmt.Println(x.Text)34		}35	})36	ast.Inspect(f, func(n ast.Node) bool {37		switch x := n.(type) {38			fmt.Println(x.Name)39		}40	})41	ast.Inspect(f, func(n ast.Node) bool {42		switch x := n.(type) {43			fmt.Println(x.Name)

Full Screen

Full Screen

textFromCallExpr

Using AI Code Generation

copy

Full Screen

1import (2func main() {3	f, err := parser.ParseFile(fset, "1.go", nil, parser.ParseComments)4	if err != nil {5		log.Fatal(err)6	}7	fmt.Println("Imports:")8	for _, s := range f.Imports {9		fmt.Println(textFromCallExpr(fset, s))10	}11}12func textFromCallExpr(fset *token.FileSet, x ast.Node) string {13	return fset.Position(x.Pos()).String() + "-" + fset.Position(x.End()).String()14}15import (16func main() {17	f, err := parser.ParseFile(fset, "1.go", nil, parser.ParseComments)18	if err != nil {19		log.Fatal(err)20	}21	fmt.Println("Imports:")22	for _, s := range f.Imports {23		fmt.Println(textFromCallExpr(fset, s))24	}25}26func textFromCallExpr(fset *token.FileSet, x ast.Node) string {27	return fset.Position(x.Pos()).String() + "-" + fset.Position(x.End()).String()28}29import (

Full Screen

Full Screen

textFromCallExpr

Using AI Code Generation

copy

Full Screen

1import (2func main() {3	if len(os.Args) > 1 {4		f, err = parser.ParseFile(fset, os.Args[1], nil, parser.ParseComments)5	} else {6		f, err = parser.ParseFile(fset, "1.go", nil, parser.ParseComments)7	}8	if err != nil {9		panic(err)10	}11	fmt.Println("Comments:")12	for _, s := range f.Comments {13		fmt.Println(s.Text())14	}15	fmt.Println("Functions:")16	for _, s := range f.Decls {17		switch x := s.(type) {18			fmt.Println(x.Name.Name)

Full Screen

Full Screen

textFromCallExpr

Using AI Code Generation

copy

Full Screen

1func main() {2  println(c)3}4func main() {5  println(c)6}7func main() {8  println(c)9}10func main() {11  println(c)12}13func main() {14  println(c)15}16func main() {17  println(c)18}19func main() {20  println(c)21}22func main() {23  println(c)24}25func main() {26  println(c)27}

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