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

99 lines
2.5 KiB
Go

package languages
import (
"testing"
"github.com/zzet/gortex/internal/graph"
)
func TestBuildSwiftObjCSelector(t *testing.T) {
cases := []struct {
base string
labels []string
want string
}{
{"viewDidLoad", nil, "viewDidLoad"},
{"move", []string{"from", "to"}, "moveFrom:to:"},
{"insertSubview", []string{"", "at"}, "insertSubview:at:"},
{"init", []string{"frame"}, "initWithFrame:"},
{"doThing", []string{"with"}, "doThingWith:"},
{"reset", []string{""}, "reset:"},
}
for _, c := range cases {
if got := buildSwiftObjCSelector(c.base, c.labels); got != c.want {
t.Errorf("buildSwiftObjCSelector(%q, %v) = %q, want %q", c.base, c.labels, got, c.want)
}
}
}
func TestSwiftArgLabels(t *testing.T) {
cases := []struct {
in string
want []string
}{
{"", nil},
{"x: Int", []string{"x"}},
{"from a: Int, to b: Int", []string{"from", "to"}},
{"_ view: UIView, at index: Int", []string{"", "at"}},
{"closure: (Int, String) -> Void, name: String", []string{"closure", "name"}},
{"items: [String], opts: [String: Int]", []string{"items", "opts"}},
}
for _, c := range cases {
got := swiftArgLabels(c.in)
if len(got) != len(c.want) {
t.Errorf("swiftArgLabels(%q) = %v, want %v", c.in, got, c.want)
continue
}
for i := range got {
if got[i] != c.want[i] {
t.Errorf("swiftArgLabels(%q)[%d] = %q, want %q", c.in, i, got[i], c.want[i])
}
}
}
}
// TestSwiftExtract_StampsObjCSelector drives the full extractor and
// asserts each @objc method node carries the computed ObjC selector,
// while a non-@objc method does not.
func TestSwiftExtract_StampsObjCSelector(t *testing.T) {
src := `import Foundation
@objc class Mover: NSObject {
@objc func move(from a: Int, to b: Int) {}
@objc(customMove:) func moveCustom(x: Int) {}
@objc func viewDidLoad() {}
func notExposed(x: Int) {}
}
`
r, err := NewSwiftExtractor().Extract("Mover.swift", []byte(src))
if err != nil {
t.Fatalf("extract: %v", err)
}
got := map[string]string{}
for _, n := range r.Nodes {
if n.Kind != graph.KindMethod {
continue
}
sel := ""
if n.Meta != nil {
sel, _ = n.Meta["objc_selector"].(string)
}
got[n.Name] = sel
}
checks := map[string]string{
"move": "moveFrom:to:",
"moveCustom": "customMove:",
"viewDidLoad": "viewDidLoad",
"notExposed": "",
}
for name, want := range checks {
if _, ok := got[name]; !ok {
t.Errorf("method %q not extracted", name)
continue
}
if got[name] != want {
t.Errorf("method %q objc_selector = %q, want %q", name, got[name], want)
}
}
}