63 lines
2.0 KiB
Go
63 lines
2.0 KiB
Go
package configkit_test
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
configkit "gitea.maximumdirect.net/eric/ConfigKit"
|
|
)
|
|
|
|
func TestPublicHistoricalPathsEnumerateContextualShapes(t *testing.T) {
|
|
loaded, err := configkit.Load[map[string]any](configkit.Bytes("main", []byte(
|
|
"retired: {child: 1}\nshape: {'0': base}\n'a/b~': {'': 2}\n",
|
|
)), configkit.Options[map[string]any]{Overrides: []configkit.Override{
|
|
{Path: configkit.Keys("retired"), Value: nil, Flag: "--retired"},
|
|
{Path: configkit.Keys("shape"), Value: []any{"replacement"}, Flag: "--shape"},
|
|
}})
|
|
if err != nil {
|
|
t.Fatalf("Load: %v", err)
|
|
}
|
|
|
|
metadata := loaded.Metadata()
|
|
paths := metadata.HistoricalPaths()
|
|
got := make([]string, len(paths))
|
|
for index, path := range paths {
|
|
got[index] = path.String()
|
|
if len(metadata.History(path)) == 0 {
|
|
t.Errorf("enumerated path %s has no history", path)
|
|
}
|
|
}
|
|
want := []string{"", "/a~1b~0", "/a~1b~0/", "/retired", "/retired/child", "/shape", "/shape/0"}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("historical paths = %v, want sorted unique pointers %v", got, want)
|
|
}
|
|
if _, ok := metadata.Lookup(paths[4]); ok {
|
|
t.Fatal("removed descendant remains effective")
|
|
}
|
|
if _, ok := metadata.Lookup(paths[6]); !ok {
|
|
t.Fatal("enumerated numeric path did not follow the effective sequence shape")
|
|
}
|
|
if len(metadata.History(paths[6])) == 0 {
|
|
t.Fatal("enumerated numeric path lost history")
|
|
}
|
|
if _, ok := metadata.Lookup(configkit.Keys("shape", "0")); ok {
|
|
t.Fatal("mapping-key path resolved against the effective sequence")
|
|
}
|
|
if len((configkit.Metadata{}).HistoricalPaths()) != 0 {
|
|
t.Fatal("zero metadata contains historical paths")
|
|
}
|
|
|
|
paths[1] = configkit.Keys("mutated")
|
|
if again := metadata.HistoricalPaths(); !reflect.DeepEqual(pathStrings(again), want) {
|
|
t.Fatalf("returned path slice mutated retained metadata: %v", pathStrings(again))
|
|
}
|
|
}
|
|
|
|
func pathStrings(paths []configkit.Path) []string {
|
|
result := make([]string, len(paths))
|
|
for index, path := range paths {
|
|
result[index] = path.String()
|
|
}
|
|
return result
|
|
}
|