Files
ConfigKit/internal/engine/resolution_index_test.go

74 lines
2.2 KiB
Go

package engine
import (
"testing"
"gitea.maximumdirect.net/eric/ConfigKit/internal/document"
)
func TestResolutionCoverageIndexMatchesOrderedScan(t *testing.T) {
paths := []Path{
Keys("group"),
Keys("group", "field"),
Keys("group", "field"),
Keys("group", "field", "nested"),
Keys("other"),
}
index := newResolutionCoverageIndex(paths)
queries := []Path{
Path{},
Keys("group"),
Keys("group", "field"),
Keys("group", "field", "nested", "leaf"),
Keys("group", "missing"),
Keys("other", "child"),
Keys("group").Index(-1),
}
for _, query := range queries {
wantID, wantDepth, wantFound := 0, -1, false
for id, candidate := range paths {
if pathContains(candidate, query) && (!wantFound || len(candidate.segments) > wantDepth) {
wantID, wantDepth, wantFound = id, len(candidate.segments), true
}
}
gotID, gotFound := index.deepestCoveringID(query)
if gotID != wantID || gotFound != wantFound {
t.Errorf("deepestCoveringID(%s) = %d, %v; want %d, %v", query.String(), gotID, gotFound, wantID, wantFound)
}
}
}
func TestDocumentLookupMatchesPathScan(t *testing.T) {
name := &document.Node{Kind: document.String, Text: "item"}
sequence := &document.Node{Kind: document.Sequence, Sequence: []*document.Node{
{Kind: document.String, Text: "zero"},
{Kind: document.Mapping, Mapping: []document.Entry{{Key: "name", Value: name}}},
}}
root := &document.Node{Kind: document.Mapping, Mapping: []document.Entry{
{Key: "0", Value: &document.Node{Kind: document.String, Text: "numeric key"}},
{Key: "01", Value: &document.Node{Kind: document.String, Text: "leading zero key"}},
{Key: "items", Value: sequence},
}}
lookup := buildDocumentLookup(root)
paths := []Path{
Path{},
Keys("0"),
Keys("01"),
Keys("items"),
Keys("items").Index(0),
Keys("items").Index(1).Key("name"),
Keys("items").Index(2),
Keys("items").Key("name"),
Keys("missing"),
Keys("items").Index(-1),
(Path{}).Index(0),
}
for _, path := range paths {
wantNode, wantFound := documentAtPath(root, path)
gotNode, gotFound := documentAtPathWithLookup(root, path, lookup)
if gotNode != wantNode || gotFound != wantFound {
t.Errorf("indexed lookup %s = (%p, %v), want (%p, %v)", path.String(), gotNode, gotFound, wantNode, wantFound)
}
}
}