func makeGraphData(t testing.TB, n int) graph.Output { data := graph.Output{} if n > 0 { data.Defs = make([]*graph.Def, n) data.Refs = make([]*graph.Ref, n) } for i := 0; i < n; i++ { data.Defs[i] = &graph.Def{ DefKey: graph.DefKey{Path: fmt.Sprintf("def-path-%d", i)}, Name: fmt.Sprintf("def-name-%d", i), Kind: "mykind", DefStart: uint32((i % 53) * 37), DefEnd: uint32((i%53)*37 + (i % 20)), File: fmt.Sprintf("dir%d/subdir%d/subsubdir%d/file-%d.foo", i%5, i%3, i%7, i%5), Exported: i%5 == 0, Local: i%3 == 0, Data: []byte(`"` + strings.Repeat("abcd", 50) + `"`), } data.Refs[i] = &graph.Ref{ DefPath: fmt.Sprintf("ref-path-%d", i), Def: i%5 == 0, Start: uint32((i % 51) * 39), End: uint32((i%51)*37 + (int(i) % 18)), File: fmt.Sprintf("dir%d/subdir%d/subsubdir%d/file-%d.foo", i%3, i%5, i%7, i%5), } if i%3 == 0 { data.Refs[i].DefUnit = fmt.Sprintf("def-unit-%d", i%17) data.Refs[i].DefUnitType = fmt.Sprintf("def-unit-type-%d", i%3) if i%7 == 0 { data.Refs[i].DefRepo = fmt.Sprintf("def-repo-%d", i%13) } } } return data }
func testUnitStore_Refs_ByDef(t *testing.T, us UnitStoreImporter) { refsByDef := map[string][]*graph.Ref{ "p1": { {File: "f1", Start: 0, End: 5}, }, "p2": { {File: "f1", Start: 0, End: 5}, {File: "f2", Start: 5, End: 10}, }, "p3": { {File: "f3", Start: 0, End: 5}, {File: "f1", Start: 5, End: 10}, {File: "f1", Start: 10, End: 15}, }, } var data graph.Output for defPath, refs := range refsByDef { for _, ref := range refs { ref.DefPath = defPath } data.Refs = append(data.Refs, refs...) } if err := us.Import(data); err != nil { t.Errorf("%s: Import(data): %s", us, err) } for defPath, wantRefs := range refsByDef { c_defRefsIndex_getByDef = 0 refs, err := us.Refs(ByRefDef(graph.RefDefKey{DefPath: defPath})) if err != nil { t.Fatalf("%s: Refs(ByDefs %s): %s", us, defPath, err) } sort.Sort(refsByFileStartEnd(refs)) sort.Sort(refsByFileStartEnd(wantRefs)) if want := wantRefs; !reflect.DeepEqual(refs, want) { t.Errorf("%s: Refs(ByDefs %s): got refs %v, want %v", us, defPath, refs, want) } if isIndexedStore(us) { if want := 1; c_defRefsIndex_getByDef != want { t.Errorf("%s: Refs(ByDefs %s): got %d index hits, want %d", us, defPath, c_defRefsIndex_getByDef, want) } } } }
func testUnitStore_Refs_ByFiles(t *testing.T, us UnitStoreImporter) { refsByFile := map[string][]*graph.Ref{ "f1": { {DefPath: "p1", Start: 0, End: 5}, }, "f2": { {DefPath: "p1", Start: 0, End: 5}, {DefPath: "p2", Start: 5, End: 10}, }, "f3": { {DefPath: "p1", Start: 0, End: 5}, {DefPath: "p2", Start: 5, End: 10}, {DefPath: "p3", Start: 10, End: 15}, }, } var data graph.Output for file, refs := range refsByFile { for _, ref := range refs { ref.File = file } data.Refs = append(data.Refs, refs...) } if err := us.Import(data); err != nil { t.Errorf("%s: Import(data): %s", us, err) } for file, wantRefs := range refsByFile { c_refFileIndex_getByFile = 0 refs, err := us.Refs(ByFiles(file)) if err != nil { t.Fatalf("%s: Refs(ByFiles %s): %s", us, file, err) } sort.Sort(refsByFileStartEnd(refs)) sort.Sort(refsByFileStartEnd(wantRefs)) if want := wantRefs; !reflect.DeepEqual(refs, want) { t.Errorf("%s: Refs(ByFiles %s): got refs %v, want %v", us, file, refs, want) } if isIndexedStore(us) { if want := 1; c_refFileIndex_getByFile != want { t.Errorf("%s: Refs(ByFiles %s): got %d index hits, want %d", us, file, c_refFileIndex_getByFile, want) } } } }
func (c *GraphContext) transform(raw *RawOutput, unit *unit.SourceUnit) *graph.Output { var out graph.Output for _, def := range raw.Defs { out.Defs = append(out.Defs, c.transformDef(def)) if doc := c.transformDefDoc(def); doc != nil { out.Docs = append(out.Docs, doc) } } for _, ref := range raw.Refs { if outRef, err := c.transformRef(ref); err == nil { out.Refs = append(out.Refs, outRef) } else { log.Printf("Could not transform ref %v: %s", ref, err) } } return &out }
func Graph(unit *unit.SourceUnit) (*graph.Output, error) { pkg, err := UnitDataAsBuildPackage(unit) if err != nil { return nil, err } o, err := doGraph(pkg) if err != nil { return nil, err } o2 := graph.Output{} for _, gs := range o.Defs { d, err := convertGoDef(gs) if err != nil { return nil, err } if d != nil { o2.Defs = append(o2.Defs, d) } } for _, gr := range o.Refs { r, err := convertGoRef(gr) if err != nil { return nil, err } if r != nil { o2.Refs = append(o2.Refs, r) } } for _, gd := range o.Docs { d, err := convertGoDoc(gd) if err != nil { return nil, err } if d != nil { o2.Docs = append(o2.Docs, d) } } return &o2, nil }
func testTreeStore_Refs_ByFiles(t *testing.T, ts TreeStoreImporter) { refsByUnitByFile := map[string]map[string][]*graph.Ref{ "u1": { "f1": { {DefPath: "p1", Start: 0, End: 5}, }, "f2": { {DefPath: "p1", Start: 0, End: 5}, {DefPath: "p2", Start: 5, End: 10}, }, }, "u2": { "f1": { {DefPath: "p1", Start: 5, End: 10}, }, }, } refsByFile := map[string][]*graph.Ref{} for unitName, refsByFile0 := range refsByUnitByFile { u := &unit.SourceUnit{Key: unit.Key{Type: "t", Name: unitName}} var data graph.Output for file, refs := range refsByFile0 { u.Files = append(u.Files, file) for _, ref := range refs { ref.File = file } data.Refs = append(data.Refs, refs...) refsByFile[file] = append(refsByFile[file], refs...) } if err := ts.Import(u, data); err != nil { t.Errorf("%s: Import(%v, data): %s", ts, u, err) } } if ts, ok := ts.(TreeIndexer); ok { if err := ts.Index(); err != nil { t.Fatalf("%s: Index: %s", ts, err) } } for file, wantRefs := range refsByFile { c_unitStores_Refs_last_numUnitsQueried.set(0) c_refFileIndex_getByFile.set(0) refs, err := ts.Refs(ByFiles(false, file)) if err != nil { t.Fatalf("%s: Refs(ByFiles %s): %s", ts, file, err) } distinctRefUnits := map[string]struct{}{} for _, ref := range refs { distinctRefUnits[ref.Unit] = struct{}{} } // for test equality sort.Sort(refsByFileStartEnd(refs)) sort.Sort(refsByFileStartEnd(wantRefs)) cleanForImport(&graph.Output{Refs: refs}, "", "t", "u1") cleanForImport(&graph.Output{Refs: refs}, "", "t", "u2") if want := wantRefs; !deepEqual(refs, want) { t.Errorf("%s: Refs(ByFiles %s): got refs %v, want %v", ts, file, refs, want) } if isIndexedStore(ts) { if want := len(distinctRefUnits); c_refFileIndex_getByFile.get() != want { t.Errorf("%s: Refs(ByFiles %s): got %d index hits, want %d", ts, file, c_refFileIndex_getByFile.get(), want) } if want := len(distinctRefUnits); c_unitStores_Refs_last_numUnitsQueried.get() != want { t.Errorf("%s: Refs(ByFiles %s): got %d units queried, want %d", ts, file, c_unitStores_Refs_last_numUnitsQueried.get(), want) } } } }