func TestParsePackage(t *testing.T) { pkg, err := parser.ParsePackage(".") if err != nil { t.Fatal(err) } t.Logf("%+v", pkg) }
func TestParsePackage_Fields(t *testing.T) { pkg, err := parser.ParsePackage("./testdata/fields") if err != nil { t.Fatal(err) } fields := pkg.Classes[0].Fields table := []struct { Name string Type string }{ {"uint8", "uint8"}, {"xi8", "int8"}, {"yembed.int16", "*int16"}, {"yembed.xi32", "[]int32"}, {"yembed.yi32", "[]int32"}, {"yembed.znested.i64", "int64"}, {"yyembed.int16", "*int16"}, {"yyembed.xi32", "[]int32"}, {"yyembed.yi32", "[]int32"}, {"yyembed.znested.i64", "int64"}, {"zembed.bool", "*bool"}, {"zzembed", "[]struct{int8, u8 uint8, uchar uint8}"}, // TODO: parse more? } for i, v := range table { field := fields[i] if field.Name != v.Name { t.Fatalf("%d Name want:%v but:%v", i, v.Name, field.Name) } if field.Type != v.Type { t.Fatalf("%d Type want:%v but:%v", i, v.Type, field.Type) } } }
func TestParsePackage_IO(t *testing.T) { pkg, err := parser.ParsePackage("io") if err != nil { t.Fatal(err) } b, _ := json.MarshalIndent(pkg, "", "\t") t.Logf("%s", strings.SplitAfterN(string(b), `",`, 2)[0]) }
func TestParsePackage_BuildTags(t *testing.T) { pkg, err := parser.ParsePackage("./testdata/buildtags") if err != nil { t.Fatal(err) } if len(pkg.Classes) != 1 || pkg.Classes[0].Name != "ok" { t.Fatalf("it should contain ok but:%+v", pkg.Classes) } }
func Complete(scope *ast.Scope) error { packageRefs := map[string]map[string]bool{} for _, pkg := range scope.Packages { for _, class := range pkg.Classes { for _, rel := range class.Relations { addReference(packageRefs, rel) } } for _, iface := range pkg.Interfaces { for _, rel := range iface.Relations { addReference(packageRefs, rel) } } } for _, pkg := range scope.Packages { delete(packageRefs, pkg.Name) } for path, refs := range packageRefs { if path == "" { pkg := ast.NewPackage("") pkg.Interfaces = append(pkg.Interfaces, &ast.Interface{Name: "error", Relations: []*ast.Relation{}}) scope.Packages[path] = pkg continue } pkg, err := parser.ParsePackage(path) if err != nil { log.Printf("%#v", err) continue } newclasses := make([]*ast.Class, 0, len(pkg.Classes)) for _, class := range pkg.Classes { if refs[path+"."+class.Name] { // TODO: hide details, really? class.Fields = []*ast.Field{} class.Methods = []*ast.Method{} class.Relations = []*ast.Relation{} newclasses = append(newclasses, class) } } pkg.Classes = newclasses newifaces := make([]*ast.Interface, 0, len(pkg.Interfaces)) for _, iface := range pkg.Interfaces { if refs[path+"."+iface.Name] { iface.Methods = []*ast.Method{} iface.Relations = []*ast.Relation{} newifaces = append(newifaces, iface) } } pkg.Interfaces = newifaces scope.Packages[path] = pkg } return nil }
func TestParsePackage_StdLibs(t *testing.T) { for _, name := range []string{"go/ast", "go/token", "reflect", "database/sql", "image", "image/color", "os", "net", "net/http"} { pkg, err := parser.ParsePackage(name) if err != nil { t.Fatal(err) } b, _ := json.MarshalIndent(pkg, "", "\t") t.Logf("%s", strings.SplitAfterN(string(b), `",`, 2)[0]) } }
func TestOracleCGoImport(t *testing.T) { pkg, err := parser.ParsePackage("github.com/t-yuki/godoc2puml/annotator/testdata/cgoimport") if err != nil { t.Fatal(err) } err = annotator.Oracle(pkg) if err != nil { t.Fatal(err) } b, _ := json.MarshalIndent(pkg, "", "\t") t.Logf("%s", b) }
func TestOracleGoAST(t *testing.T) { name := "go/ast" pkg, err := parser.ParsePackage(name) if err != nil { t.Fatal(err) } err = annotator.Oracle(pkg) if err != nil { t.Fatal(err) } b, _ := json.MarshalIndent(pkg, "", "\t") t.Logf("%s", b) }
func TestOracleStdLibs(t *testing.T) { for _, name := range []string{"io", "net", "net/http"} { pkg, err := parser.ParsePackage(name) if err != nil { t.Fatal(err) } err = annotator.Oracle(pkg) if err != nil { t.Fatal(err) } b, _ := json.MarshalIndent(pkg, "", "\t") t.Logf("%s", b) } }
func TestParsePackage_Relations(t *testing.T) { path := "./testdata/relations" pkg, err := parser.ParsePackage(path) if err != nil { t.Fatal(err) } relations := pkg.Classes[0].Relations table := []struct { Target string Label string RelType ast.RelationType Multiplicity string }{ {"struct1", "", ast.Composition, ""}, {"iface1", "", ast.Composition, ""}, // even if iface1 is interface, it can't be detected on parse {"struct1", "xassociation", ast.Association, ""}, {"struct1", "yembed.struct1", ast.Association, ""}, {"iface1", "yembed.iface1", ast.Association, ""}, {"iface1", "yembed.nested.if1", ast.Association, "*"}, {"struct1", "zembed.struct1", ast.Association, ""}, {"iface1", "zembed.iface1", ast.Association, ""}, {"iface1", "zembed.nested.if1", ast.Association, "*"}, } for i, v := range table { rel := relations[i] if rel.Label != v.Label { t.Fatalf("%d Label want:%v but:%v", i, v.Label, rel.Label) } if rel.Target != path+"."+v.Target { t.Fatalf("%d Target want:%v but:%v", i, path+"."+v.Target, rel.Target) } if rel.RelType != v.RelType { t.Fatalf("%d RelType want:%v but:%v", i, v.RelType, rel.RelType) } if rel.Multiplicity != v.Multiplicity { t.Fatalf("%d Multiplicity want:%v but:%v", i, v.Multiplicity, rel.Multiplicity) } } }
func TestParsePackage_NoFiles(t *testing.T) { pkg, err := parser.ParsePackage("./testdata/nofiles") if err == nil { t.Fatalf("it should be error %+v", pkg) } }