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
84 lines
3.4 KiB
Go
84 lines
3.4 KiB
Go
package resolver
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/zzet/gortex/internal/graph"
|
|
)
|
|
|
|
func kmpEdgeBetween(g graph.Store, from, to string, kind graph.EdgeKind) *graph.Edge {
|
|
for _, e := range g.GetOutEdges(from) {
|
|
if e.To == to && e.Kind == kind && e.Meta != nil {
|
|
if v, _ := e.Meta["via"].(string); v == kmpExpectActualVia {
|
|
return e
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func kmpNode(g graph.Store, id, name, file, role string, kind graph.NodeKind) {
|
|
g.AddNode(&graph.Node{ID: id, Kind: kind, Name: name, FilePath: file, StartLine: 2, Language: "kotlin", Meta: map[string]any{"kmp_role": role}})
|
|
}
|
|
|
|
func TestResolveKMPExpectActual_PairsExpectToActual(t *testing.T) {
|
|
g := graph.New()
|
|
kmpNode(g, "common.kt::platformName", "platformName", "common.kt", "expect", graph.KindFunction)
|
|
kmpNode(g, "android.kt::platformName", "platformName", "android.kt", "actual", graph.KindFunction)
|
|
kmpNode(g, "ios.kt::platformName", "platformName", "ios.kt", "actual", graph.KindFunction)
|
|
kmpNode(g, "common.kt::Platform", "Platform", "common.kt", "expect", graph.KindType)
|
|
kmpNode(g, "android.kt::Platform", "Platform", "android.kt", "actual", graph.KindType)
|
|
|
|
// 2 actuals for platformName + 1 for Platform = 3 pairs.
|
|
assert.Equal(t, 3, ResolveKMPExpectActual(g))
|
|
|
|
// Each actual implements the expect (find_implementations on the expect).
|
|
require.NotNil(t, kmpEdgeBetween(g, "android.kt::platformName", "common.kt::platformName", graph.EdgeImplements), "android actual → expect")
|
|
require.NotNil(t, kmpEdgeBetween(g, "ios.kt::platformName", "common.kt::platformName", graph.EdgeImplements), "ios actual → expect")
|
|
// Reverse navigation edge.
|
|
require.NotNil(t, kmpEdgeBetween(g, "common.kt::platformName", "android.kt::platformName", graph.EdgeReferences), "expect → actual")
|
|
// Type pairing.
|
|
require.NotNil(t, kmpEdgeBetween(g, "android.kt::Platform", "common.kt::Platform", graph.EdgeImplements), "actual type → expect type")
|
|
|
|
e := kmpEdgeBetween(g, "android.kt::platformName", "common.kt::platformName", graph.EdgeImplements)
|
|
assert.Equal(t, SynthKMPExpectActual, e.Meta[MetaSynthesizedBy])
|
|
}
|
|
|
|
func TestResolveKMPExpectActual_ExpectWithoutActualNoEdge(t *testing.T) {
|
|
g := graph.New()
|
|
kmpNode(g, "common.kt::lonely", "lonely", "common.kt", "expect", graph.KindFunction)
|
|
assert.Equal(t, 0, ResolveKMPExpectActual(g))
|
|
}
|
|
|
|
func TestResolveKMPExpectActual_KindMismatchNoPair(t *testing.T) {
|
|
g := graph.New()
|
|
// Same name but different kinds must not pair.
|
|
kmpNode(g, "common.kt::Thing", "Thing", "common.kt", "expect", graph.KindType)
|
|
kmpNode(g, "android.kt::Thing", "Thing", "android.kt", "actual", graph.KindFunction)
|
|
assert.Equal(t, 0, ResolveKMPExpectActual(g))
|
|
}
|
|
|
|
func TestResolveKMPExpectActual_Idempotent(t *testing.T) {
|
|
g := graph.New()
|
|
kmpNode(g, "common.kt::platformName", "platformName", "common.kt", "expect", graph.KindFunction)
|
|
kmpNode(g, "android.kt::platformName", "platformName", "android.kt", "actual", graph.KindFunction)
|
|
first := ResolveKMPExpectActual(g)
|
|
second := ResolveKMPExpectActual(g)
|
|
assert.Equal(t, first, second)
|
|
|
|
count := 0
|
|
for _, kind := range []graph.EdgeKind{graph.EdgeImplements, graph.EdgeReferences} {
|
|
for e := range g.EdgesByKind(kind) {
|
|
if e != nil && e.Meta != nil {
|
|
if v, _ := e.Meta["via"].(string); v == kmpExpectActualVia {
|
|
count++
|
|
}
|
|
}
|
|
}
|
|
}
|
|
assert.Equal(t, 2, count)
|
|
}
|