Files
wehub-resource-sync 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
chore: import upstream snapshot with attribution
2026-07-13 12:33:42 +08:00

113 lines
2.9 KiB
Go

package graph
import "testing"
func TestEnclosingFromID(t *testing.T) {
cases := []struct {
name string
id string
kind NodeKind
wantID string
wantName string
}{
{
name: "method on type",
id: "pkg/decoder.go::Decoder.Parse",
kind: KindMethod,
wantID: "pkg/decoder.go::Decoder",
wantName: "Decoder",
},
{
name: "field of struct",
id: "pkg/config.go::Config.Timeout",
kind: KindField,
wantID: "pkg/config.go::Config",
wantName: "Config",
},
{
name: "enum member",
id: "pkg/kind.go::NodeKind.Function",
kind: KindEnumMember,
wantID: "pkg/kind.go::NodeKind",
wantName: "NodeKind",
},
{
name: "closure inside function",
id: "pkg/run.go::Execute#closure@42",
kind: KindClosure,
wantID: "pkg/run.go::Execute",
wantName: "Execute",
},
{
name: "nested type method",
id: "pkg/x.go::Outer.Inner.Do",
kind: KindMethod,
wantID: "pkg/x.go::Outer.Inner",
wantName: "Inner",
},
{
name: "top-level function -- no owner",
id: "pkg/x.go::TopLevel",
kind: KindFunction,
wantID: "",
wantName: "",
},
{
name: "plain method ID without owner segment",
id: "pkg/x.go::Bare",
kind: KindMethod,
wantID: "",
wantName: "",
},
{
name: "type is never enclosed",
id: "pkg/x.go::SomeType",
kind: KindType,
wantID: "",
wantName: "",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
gotID, gotName := EnclosingFromID(tc.id, tc.kind)
if gotID != tc.wantID || gotName != tc.wantName {
t.Fatalf("EnclosingFromID(%q, %v) = (%q, %q), want (%q, %q)",
tc.id, tc.kind, gotID, gotName, tc.wantID, tc.wantName)
}
})
}
}
func TestNodeBrief_Enclosing(t *testing.T) {
// A method node surfaces its receiver type in Brief.
method := &Node{ID: "pkg/d.go::Decoder.Parse", Kind: KindMethod, Name: "Parse", FilePath: "pkg/d.go"}
b := method.Brief()
if b["enclosing"] != "Decoder" {
t.Errorf("method Brief enclosing = %v, want Decoder", b["enclosing"])
}
if b["enclosing_id"] != "pkg/d.go::Decoder" {
t.Errorf("method Brief enclosing_id = %v, want pkg/d.go::Decoder", b["enclosing_id"])
}
// A top-level function has no enclosing key at all.
fn := &Node{ID: "pkg/d.go::TopLevel", Kind: KindFunction, Name: "TopLevel", FilePath: "pkg/d.go"}
fb := fn.Brief()
if _, ok := fb["enclosing"]; ok {
t.Error("top-level function Brief should carry no enclosing key")
}
}
func TestEnclosingShortName(t *testing.T) {
cases := map[string]string{
"pkg/x.go::Owner": "Owner",
"Outer.Inner": "Inner",
"Plain": "Plain",
"pkg::A.B.C": "C",
}
for in, want := range cases {
if got := EnclosingShortName(in); got != want {
t.Errorf("EnclosingShortName(%q) = %q, want %q", in, got, want)
}
}
}