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

102 lines
2.5 KiB
Go

package languages
import (
"testing"
"github.com/zzet/gortex/internal/graph"
)
func swiftRoleOf(nodes []*graph.Node, name string) (string, bool) {
for _, n := range nodes {
if n.Kind == graph.KindType && n.Name == name && n.Meta != nil {
if r, ok := n.Meta["swiftui_role"].(string); ok {
return r, true
}
}
}
return "", false
}
func swiftEntryPoint(nodes []*graph.Node, name string) bool {
for _, n := range nodes {
if n.Kind == graph.KindType && n.Name == name && n.Meta != nil {
b, _ := n.Meta["entry_point"].(bool)
return b
}
}
return false
}
func TestSwiftUI_ViewAndAppEntryClassification(t *testing.T) {
src := []byte(`import SwiftUI
@main
struct MyApp: App {
var body: some Scene { WindowGroup { ContentView() } }
}
struct ContentView: View {
var body: some View { Text("hi") }
}
struct Plain {
let x: Int
}
`)
res, err := NewSwiftExtractor().Extract("App/MyApp.swift", []byte(src))
if err != nil {
t.Fatal(err)
}
if r, _ := swiftRoleOf(res.Nodes, "ContentView"); r != "component" {
t.Errorf("ContentView swiftui_role = %q (want component)", r)
}
if r, _ := swiftRoleOf(res.Nodes, "MyApp"); r != "app_entry" {
t.Errorf("MyApp swiftui_role = %q (want app_entry)", r)
}
if !swiftEntryPoint(res.Nodes, "MyApp") {
t.Errorf("@main App should be marked entry_point")
}
// A plain struct carries no SwiftUI role.
if r, ok := swiftRoleOf(res.Nodes, "Plain"); ok {
t.Errorf("plain struct should have no swiftui_role, got %q", r)
}
}
func TestSwiftUI_FluentModelClassification(t *testing.T) {
src := []byte(`import Fluent
final class User: Model {
static let schema = "users"
}
`)
res, err := NewSwiftExtractor().Extract("Sources/App/Models/User.swift", []byte(src))
if err != nil {
t.Fatal(err)
}
for _, n := range res.Nodes {
if n.Kind == graph.KindType && n.Name == "User" {
if b, _ := n.Meta["fluent_model"].(bool); !b {
t.Errorf("`final class User: Model` should carry Meta[fluent_model]=true")
}
return
}
}
t.Fatalf("User type node not found")
}
func TestSwiftUI_AppWithoutMainNotEntry(t *testing.T) {
// Conformance to App without @main is not classified as the app entry.
src := []byte(`struct Helper: App {
var body: some Scene { WindowGroup { } }
}
`)
res, err := NewSwiftExtractor().Extract("App/Helper.swift", []byte(src))
if err != nil {
t.Fatal(err)
}
if r, ok := swiftRoleOf(res.Nodes, "Helper"); ok {
t.Errorf("App conformance without @main should not be app_entry, got %q", r)
}
}