a06f331eb8
CI / benchmark (push) Has been skipped
install-script / posix-syntax (push) Successful in 6m1s
CI / build-onnx (push) Failing after 6m43s
init-smoke / dry-run (push) Failing after 15m57s
security / govulncheck (push) Has been cancelled
security / trivy-fs (push) Has been cancelled
CI / test (1.26, ubuntu-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
CI / test (1.26, macos-latest) (push) Has been cancelled
CI / build-windows (push) Has been cancelled
CI / lint (push) Has been cancelled
install-script / powershell-syntax (push) Has been cancelled
install-script / install (macos-14) (push) Has been cancelled
install-script / install (ubuntu-latest) (push) Has been cancelled
64 lines
2.3 KiB
Go
64 lines
2.3 KiB
Go
package query
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/zzet/gortex/internal/graph"
|
|
)
|
|
|
|
// An edge carrying Meta["call_sites"] expands into one usage row per site.
|
|
func TestFindUsages_ExpandsCallSites(t *testing.T) {
|
|
g := graph.New()
|
|
target := "h.php::HandlerInterface.handle"
|
|
caller := "app.php::run"
|
|
g.AddNode(&graph.Node{ID: target, Kind: graph.KindMethod, Name: "handle", FilePath: "h.php", StartLine: 3, Language: "php"})
|
|
g.AddNode(&graph.Node{ID: caller, Kind: graph.KindFunction, Name: "run", FilePath: "app.php", StartLine: 2, EndLine: 10, Language: "php"})
|
|
|
|
e := &graph.Edge{From: caller, To: target, Kind: graph.EdgeCalls, FilePath: "app.php", Line: 5, Origin: graph.OriginLSPResolved}
|
|
graph.AppendCallSite(e, "app.php", 7)
|
|
graph.AppendCallSite(e, "app.php", 9)
|
|
g.AddEdge(e)
|
|
|
|
sg := NewEngine(g).FindUsages(target)
|
|
|
|
var lines []int
|
|
for _, ue := range sg.Edges {
|
|
if ue.From == caller && ue.To == target && ue.Kind == graph.EdgeCalls {
|
|
lines = append(lines, ue.Line)
|
|
}
|
|
}
|
|
assert.ElementsMatch(t, []int{5, 7, 9}, lines, "one usage row per call site")
|
|
}
|
|
|
|
// A call_sites entry that coincides with a real per-line edge is not
|
|
// double-counted; the real edge wins.
|
|
func TestFindUsages_CallSitesDedupAgainstRealEdge(t *testing.T) {
|
|
g := graph.New()
|
|
target := "h.php::X.foo"
|
|
caller := "app.php::run"
|
|
g.AddNode(&graph.Node{ID: target, Kind: graph.KindMethod, Name: "foo", FilePath: "h.php", StartLine: 3, Language: "php"})
|
|
g.AddNode(&graph.Node{ID: caller, Kind: graph.KindFunction, Name: "run", FilePath: "app.php", StartLine: 2, EndLine: 10, Language: "php"})
|
|
|
|
g.AddEdge(&graph.Edge{From: caller, To: target, Kind: graph.EdgeCalls, FilePath: "app.php", Line: 7, Origin: graph.OriginASTResolved})
|
|
e5 := &graph.Edge{From: caller, To: target, Kind: graph.EdgeCalls, FilePath: "app.php", Line: 5, Origin: graph.OriginLSPResolved}
|
|
graph.AppendCallSite(e5, "app.php", 7) // duplicates the real edge's site
|
|
g.AddEdge(e5)
|
|
|
|
sg := NewEngine(g).FindUsages(target)
|
|
|
|
countAt7 := 0
|
|
var lines []int
|
|
for _, ue := range sg.Edges {
|
|
if ue.To == target && ue.Kind == graph.EdgeCalls {
|
|
lines = append(lines, ue.Line)
|
|
if ue.Line == 7 {
|
|
countAt7++
|
|
}
|
|
}
|
|
}
|
|
assert.Equal(t, 1, countAt7, "site 7 must not be double-counted")
|
|
assert.ElementsMatch(t, []int{5, 7}, lines)
|
|
}
|