Add private sequence-item placement
This commit is contained in:
@@ -2,6 +2,7 @@ package engine
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/ConfigKit/internal/document"
|
||||
@@ -39,6 +40,29 @@ func TestNormalizeDestinationUsesLiteralMappingKeys(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMappingPlacementModeMatchesZeroValue(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
body := &document.Node{
|
||||
Kind: document.Mapping,
|
||||
Mapping: []document.Entry{{
|
||||
Key: "name", Value: &document.Node{Kind: document.String, Text: "entry"},
|
||||
}},
|
||||
}
|
||||
destination := Keys("catalog", "entry")
|
||||
path, zero, err := placeDocument(body, destination, body.Location, 5, 32)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
explicitPath, explicit, err := placeDocumentWithMode(body, destination, placementModeMapping, body.Location, 5, 32)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if path.String() != explicitPath.String() || !reflect.DeepEqual(zero, explicit) {
|
||||
t.Fatalf("zero placement (%q, %#v) differs from mapping mode (%q, %#v)", path.String(), zero, explicitPath.String(), explicit)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppendDestinationCopiesLiteralSegments(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -204,3 +228,60 @@ func TestPlaceDocumentRootAndBounds(t *testing.T) {
|
||||
t.Fatalf("absent body should not consume placement limits: node %p, error %v", absent, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlaceDocumentSequenceItemAndMountedBounds(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
root := document.Location{SourceID: "item", Line: 1, Column: 1}
|
||||
body := &document.Node{Kind: document.Mapping, Location: root}
|
||||
destination := Keys("catalog", "entries")
|
||||
placedPath, placed, err := placeDocumentWithMode(body, destination, placementModeSequenceItem, root, 3, 6)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if placedPath.String() != "/catalog/entries" || placed.Kind != document.Mapping {
|
||||
t.Fatalf("placed root = %q %#v", placedPath.String(), placed)
|
||||
}
|
||||
container, ok := documentAtPath(placed, destination)
|
||||
if !ok || container.Kind != document.Sequence || len(container.Sequence) != 1 || container.Sequence[0] != body {
|
||||
t.Fatalf("item destination = %#v, present %v", container, ok)
|
||||
}
|
||||
if got, want := countInputNodes(placed), countInputNodes(body)+destinationWrapperNodeCount(destination)+1; got != want {
|
||||
t.Fatalf("item node count = %d, want %d", got, want)
|
||||
}
|
||||
if container.Location != root || container.Sequence[0].Location != root {
|
||||
t.Fatal("synthetic sequence or authored mapping lost source coordinates")
|
||||
}
|
||||
|
||||
if _, _, err := placeDocumentWithMode(body, Path{}, placementModeSequenceItem, root, 3, 6); !errors.Is(err, errInvalidDestination) {
|
||||
t.Fatalf("root item placement error = %v, want invalid destination", err)
|
||||
}
|
||||
if _, _, err := placeDocumentWithMode(&document.Node{Kind: document.String}, destination, placementModeSequenceItem, root, 3, 6); !errors.Is(err, errInvalidDestination) {
|
||||
t.Fatalf("non-mapping item placement error = %v, want invalid destination", err)
|
||||
}
|
||||
if _, _, err := placeDocumentWithMode(body, destination, placementMode(255), root, 3, 6); !errors.Is(err, errInvalidDestination) {
|
||||
t.Fatalf("unknown placement mode error = %v, want invalid destination", err)
|
||||
}
|
||||
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
depth int
|
||||
nodes int
|
||||
wantAccept bool
|
||||
}{
|
||||
{name: "exact allowance", depth: 3, nodes: 6, wantAccept: true},
|
||||
{name: "one too shallow", depth: 2, nodes: 6},
|
||||
{name: "one node short", depth: 3, nodes: 5},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, _, err := placeDocumentWithMode(body, destination, placementModeSequenceItem, root, test.depth, test.nodes)
|
||||
if test.wantAccept && err != nil {
|
||||
t.Fatalf("exact bounds rejected: %v", err)
|
||||
}
|
||||
if !test.wantAccept && !errors.Is(err, errDestinationLimit) {
|
||||
t.Fatalf("over-bound placement error = %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user