How to use ParseSpecs method of parser Package

Best Gauge code snippet using parser.ParseSpecs

parser_test.go

Source:parser_test.go Github

copy

Full Screen

1package dicon2import (3 "go/ast"4 "go/parser"5 "go/token"6 "testing"7)8var TEST_FILE1 = `9package main10type Ex1 interface {11 Exec() error12}13// +DICON14type Ex2 interface {15 Exec() error16 Exec2(i int) (string, error)17}18`19func TestPackageParser_findDicon(t *testing.T) {20 its, err := findDicon("main", "/tmp/tmp.go", TEST_FILE1, "+DICON")21 if err != nil {22 t.Error(err)23 }24 if len(its) != 1 {25 t.Errorf("must 1 interface but %d\n", len(its))26 }27 funcs := its[0].Funcs28 if len(funcs) != 2 {29 t.Errorf("must 2 functions but %d\n", len(funcs))30 }31 for _, it := range its {32 f := it.Funcs[0]33 // test function names34 if f.Name != "Exec" && f.Name != "Exec2" {35 t.Errorf("function name must be Exec, but : %s", f.Name)36 }37 // test functions38 if f.Name == "Exec" {39 if len(f.ArgumentTypes) != 0 {40 t.Errorf("Exec ArgumentType must be blank but: %d", len(f.ArgumentTypes))41 }42 if got := f.ReturnTypes[0].SimpleName(); got != "error" {43 t.Errorf("Exec return type must be error, but : %s", got)44 }45 }46 if f.Name == "Exec2" {47 if f.ArgumentTypes[0].SimpleName() != "int" {48 t.Errorf("Exec2 argument type must be int, but : %s", f.Name)49 }50 if got := f.ReturnTypes[0].SimpleName(); got != "string" {51 t.Errorf("Exec2 return type must be string, but : %s", got)52 }53 if got := f.ReturnTypes[1].SimpleName(); got != "error" {54 t.Errorf("Exec2 return type must be error, but : %s", got)55 }56 }57 }58}59var TEST_COMPONENT = `60package di61type SampleComponent interface {62 Exec() error63}64type sampleComponent struct {65 dep Dependency66}67func NewSampleComponent(dep Dependency) SampleComponent {68 return &sampleComponent {69 dep: dep,70 }71}72func (s *sampleComponent) Exec() error {73 return nil74}75`76func TestPackageParser_FindConstructors(t *testing.T) {77 fs, _ := findConstructors("test", "/tmp/tmp.go", TEST_COMPONENT, []string{"SampleComponent"})78 if len(fs) != 1 {79 t.Fatalf("must be 1")80 }81 fun := fs[0]82 if len(fun.ReturnTypes) != 1 || fun.ReturnTypes[0].ConvertName("test") != "SampleComponent" {83 t.Errorf("return type: %v wrong", fun.ReturnTypes)84 }85 if len(fun.ArgumentTypes) != 1 || fun.ArgumentTypes[0].ConvertName("test") != "Dependency" {86 t.Errorf("arg type: %v wrong", fun.ArgumentTypes)87 }88 if fun.Name != "SampleComponent" {89 t.Errorf("func name is SampleComponent but %s", fun.Name)90 }91 if fun.PackageName != "test" {92 t.Errorf("package name is test but %s", fun.PackageName)93 }94}95var TEST_COMPONENT_ERRORS = `96package di97type SampleComponent interface {98 Exec() error99}100type sampleComponent struct {101 dep Dependency102}103func NewSampleComponent(dep Dependency) (SampleComponent, error) {104 return &sampleComponent {105 dep: dep,106 }, nil107}108func (s *sampleComponent) Exec() error {109 return nil110}111`112func TestPackageParser_FindConstructorsErrors(t *testing.T) {113 fs, _ := findConstructors("test", "/tmp/tmp.go", TEST_COMPONENT_ERRORS, []string{"SampleComponent"})114 if len(fs) != 1 {115 t.Fatalf("must be 1, but %d", len(fs))116 }117 fun := fs[0]118 if len(fun.ReturnTypes) != 2 || fun.ReturnTypes[0].SimpleName() != "SampleComponent" || fun.ReturnTypes[1].SimpleName() != "error" {119 t.Errorf("return type: %s, %s wrong", fun.ReturnTypes[0].SimpleName(), fun.ReturnTypes[1].SimpleName())120 }121 if len(fun.ArgumentTypes) != 1 || fun.ArgumentTypes[0].SimpleName() != "Dependency" {122 t.Errorf("arg type: %v wrong", fun.ArgumentTypes)123 }124 if fun.Name != "SampleComponent" {125 t.Errorf("func name is SampleComponent but %s", fun.Name)126 }127 if fun.PackageName != "test" {128 t.Errorf("package name is test but %s", fun.PackageName)129 }130}131var TEST_DEPENDENCY = `132package di133type Dependency interface {134 Run() error135}136type dependency struct {}137func NewDependency() Dependency {138 return &dependency{}139}140func (*dependency) Run() error {141 return nil142}143`144func TestPackageParer_parseDependencyFuncs(t *testing.T) {145 ds, _ := parseDependencyFuncs("test", []string{"Dependency"}, "/tmp/tmp.go", TEST_DEPENDENCY)146 if len(ds) != 1 {147 t.Fatalf("dependency function length myst be 1 but %d", len(ds))148 }149 if ds[0].Name != "Dependency" {150 t.Errorf("Dependency name must be Dependency but : %s", ds[0].Name)151 }152 if len(ds[0].Funcs) != 1 {153 t.Fatalf("Dependency func has only Run method")154 }155 if ds[0].Funcs[0].ReturnTypes[0].ConvertName("test") != "error" {156 t.Errorf("Return type must be error")157 }158}159func TestPackageParser_findInterface(t *testing.T) {160 parseSpecs := func(src string) []ast.Spec {161 f, err := parser.ParseFile(token.NewFileSet(), "", "package test\n"+src, parser.AllErrors)162 if err != nil {163 t.Fatal(err)164 }165 return f.Decls[0].(*ast.GenDecl).Specs166 }167 ts := []struct {168 specs []ast.Spec169 packageName string170 expected *InterfaceType171 }{172 {173 specs: parseSpecs(`174type A interface {175 F(a, b int) (int, error)176}177`),178 packageName: "test",179 expected: &InterfaceType{180 Name: "A",181 Funcs: []FuncType{182 {183 Name: "F",184 ArgumentTypes: []ParameterType{185 {DeclaredPackageName: "", src: ast.NewIdent("int")},186 {DeclaredPackageName: "", src: ast.NewIdent("int")},187 },188 ReturnTypes: []ParameterType{189 {DeclaredPackageName: "", src: ast.NewIdent("int")},190 {DeclaredPackageName: "", src: ast.NewIdent("error")},191 },192 },193 },194 },195 },196 {197 specs: parseSpecs(`198type B interface {199 F(a int) error200}201`),202 packageName: "test",203 expected: &InterfaceType{204 Name: "B",205 Funcs: []FuncType{206 {207 Name: "F",208 ArgumentTypes: []ParameterType{209 {DeclaredPackageName: "", src: ast.NewIdent("int")},210 },211 ReturnTypes: []ParameterType{212 {DeclaredPackageName: "", src: ast.NewIdent("error")},213 },214 },215 },216 },217 },218 {219 specs: parseSpecs(`220type C interface {221 F() (w, h int)222}223`),224 packageName: "test",225 expected: &InterfaceType{226 Name: "C",227 Funcs: []FuncType{228 {229 Name: "F",230 ArgumentTypes: []ParameterType{},231 ReturnTypes: []ParameterType{232 {DeclaredPackageName: "", src: ast.NewIdent("int")},233 {DeclaredPackageName: "", src: ast.NewIdent("int")},234 },235 },236 },237 },238 },239 }240 for _, tc := range ts {241 got, ok := findInterface(tc.packageName, tc.specs)242 if ok != (tc.expected != nil) {243 t.Errorf("unexpected result. expected: %v, but got: %v", tc.expected, got)244 continue245 }246 if !ok {247 continue248 }249 if got.Name != tc.expected.Name {250 t.Errorf("unexpected name. expected: %v, but got: %v", tc.expected.Name, got.Name)251 }252 if len(got.Funcs) != len(tc.expected.Funcs) {253 t.Errorf("unexpected len(Funcs). expected: %v, but got: %v", len(tc.expected.Funcs), len(got.Funcs))254 } else {255 for i := range got.Funcs {256 if got.Funcs[i].Name != tc.expected.Funcs[i].Name {257 t.Errorf("unexpected Funcs[%d].Name. expected: %v, but got: %v", i,258 tc.expected.Funcs[i].Name, got.Funcs[i].Name)259 }260 if len(got.Funcs[i].ArgumentTypes) != len(tc.expected.Funcs[i].ArgumentTypes) {261 t.Errorf("unexpected len(Funcs[%d].ArgumentTypes). expected: %v, but got: %v", i,262 len(tc.expected.Funcs[i].ArgumentTypes), len(got.Funcs[i].ArgumentTypes))263 } else {264 for j := range got.Funcs[i].ArgumentTypes {265 if got.Funcs[i].ArgumentTypes[j].SimpleName() != tc.expected.Funcs[i].ArgumentTypes[j].SimpleName() {266 t.Errorf("unexpected Funcs[%d].ArgumentTypes[%d]. expected: %v, but got: %v", i, j,267 tc.expected.Funcs[i].ArgumentTypes[j].SimpleName(),268 got.Funcs[i].ArgumentTypes[j].SimpleName())269 }270 }271 }272 if len(got.Funcs[i].ReturnTypes) != len(tc.expected.Funcs[i].ReturnTypes) {273 t.Errorf("unexpected len(Funcs[%d].ReturnTypes). expected: %v, but got: %v", i,274 len(tc.expected.Funcs[i].ReturnTypes), len(got.Funcs[i].ReturnTypes))275 } else {276 for j := range got.Funcs[i].ReturnTypes {277 if got.Funcs[i].ReturnTypes[j].SimpleName() != tc.expected.Funcs[i].ReturnTypes[j].SimpleName() {278 t.Errorf("unexpected Funcs[%d].ReturnTypes[%d]. expected: %v, but got: %v", i, j,279 tc.expected.Funcs[i].ReturnTypes[j].SimpleName(),280 got.Funcs[i].ReturnTypes[j].SimpleName())281 }282 }283 }284 }285 }286 }287}...

Full Screen

Full Screen

parser.go

Source:parser.go Github

copy

Full Screen

1package parser2import (3 "../model"4 "errors"5 "go/ast"6 _ "go/parser"7 "go/token"8)9func Parse(f *ast.File) ([]*model.Table, error) {10 tables := make([]*model.Table, 0)11 for _, decl := range f.Decls {12 genDecl, ok := decl.(*ast.GenDecl)13 if !ok {14 continue15 }16 if genDecl.Tok != token.TYPE {17 continue18 }19 table, err := parseSpecs(genDecl.Specs)20 if err != nil {21 continue22 }23 tables = append(tables, table)24 }25 return tables, nil26}27func parseSpecs(specs []ast.Spec) (*model.Table, error) {28 for _, spec := range specs {29 typeSpec, ok := spec.(*ast.TypeSpec)30 if !ok {31 continue32 }33 structType, ok := typeSpec.Type.(*ast.StructType)34 if !ok {35 continue36 }37 fields, err := parseFields(structType.Fields)38 if err != nil {39 return nil, err40 }41 return &model.Table{42 Name: typeSpec.Name.Name,43 Fields: fields,44 }, nil45 }46 return nil, errors.New("Failed to find struct")47}48func parseFields(fields *ast.FieldList) ([]*model.Type, error) {49 list := make([]*model.Type, 0)50 for _, field := range fields.List {51 if field.Tag == nil {52 return nil, errors.New("Tag not found")53 }54 tag := newTag(field.Tag.Value)55 tagValue := tag.get("ct")56 identType, ok := field.Type.(*ast.Ident)57 if !ok {58 return nil, errors.New("Type is not *ast.Ident")59 }60 list = append(list, &model.Type{61 Name: field.Names[0].Name,62 Type: identType.Name,63 TableType: tagValue,64 })65 }66 return list, nil67}...

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.

Run Gauge automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful