92 lines
2.9 KiB
Go
92 lines
2.9 KiB
Go
package configkit_test
|
|
|
|
import (
|
|
"bytes"
|
|
"reflect"
|
|
"testing"
|
|
|
|
configkit "go.maximumdirect.net/configkit"
|
|
)
|
|
|
|
func TestInspectionViewScopeAndQueries(t *testing.T) {
|
|
result, err := configkit.Load[map[string]any](configkit.Bytes("view.yml", []byte("a: {child: one}\nab: two\nitems: [{name: first}]\n'a/b': {'': value}\n")))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
view, ok := result.InspectSubtree(configkit.Keys("a"))
|
|
if !ok {
|
|
t.Fatal("view unavailable")
|
|
}
|
|
var paths []string
|
|
for _, path := range view.HistoricalPaths() {
|
|
paths = append(paths, path.String())
|
|
}
|
|
if !reflect.DeepEqual(paths, []string{"/a", "/a/child"}) {
|
|
t.Fatalf("scope paths = %#v", paths)
|
|
}
|
|
if _, ok := view.Metadata().Lookup(configkit.Keys("ab")); !ok {
|
|
t.Fatal("metadata filtered")
|
|
}
|
|
if _, ok := view.Explain(configkit.Keys("ab")); !ok {
|
|
t.Fatal("native query filtered")
|
|
}
|
|
var output bytes.Buffer
|
|
if err := view.WriteExplanation(&output, configkit.Keys("ab")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(view.Sources()) != len(result.Sources()) || len(view.Sources()) == 0 {
|
|
t.Fatal("sources filtered")
|
|
}
|
|
if _, ok := view.Source(view.Sources()[0].ID); !ok {
|
|
t.Fatal("source unavailable")
|
|
}
|
|
if _, ok := result.InspectSubtree(configkit.Keys("absent")); !ok {
|
|
t.Fatal("absent scope rejected")
|
|
}
|
|
if _, ok := result.InspectSubtree(configkit.Path{}.Index(-1)); ok {
|
|
t.Fatal("invalid scope accepted")
|
|
}
|
|
keyScope, _ := result.InspectSubtree(configkit.Keys("items", "0"))
|
|
indexScope, _ := result.InspectSubtree(configkit.Keys("items").Index(0))
|
|
if !reflect.DeepEqual(keyScope.HistoricalPaths(), indexScope.HistoricalPaths()) {
|
|
t.Fatal("numeric scope differs")
|
|
}
|
|
escaped, _ := result.InspectSubtree(configkit.Keys("a/b", ""))
|
|
if got := escaped.HistoricalPaths(); len(got) != 1 || got[0].String() != "/a~1b/" {
|
|
t.Fatalf("escaped scope = %#v", got)
|
|
}
|
|
root, _ := result.InspectSubtree(configkit.Path{})
|
|
if !reflect.DeepEqual(root.HistoricalPaths(), result.Metadata().HistoricalPaths()) {
|
|
t.Fatal("root scope filtered")
|
|
}
|
|
}
|
|
|
|
func TestInspectionViewUnavailable(t *testing.T) {
|
|
var result *configkit.Result[map[string]any]
|
|
if _, ok := result.InspectSubtree(configkit.Path{}); ok {
|
|
t.Fatal("nil result accepted")
|
|
}
|
|
if _, ok := (&configkit.Result[map[string]any]{}).InspectSubtree(configkit.Path{}); ok {
|
|
t.Fatal("manual result accepted")
|
|
}
|
|
var view configkit.Inspection
|
|
if len(view.HistoricalPaths()) != 0 || view.Sources() != nil {
|
|
t.Fatal("zero view returned records")
|
|
}
|
|
if _, ok := view.Metadata().Lookup(configkit.Path{}); ok {
|
|
t.Fatal("zero metadata")
|
|
}
|
|
if _, ok := view.Source("source-1"); ok {
|
|
t.Fatal("zero source")
|
|
}
|
|
if _, ok := view.SourceContext("source-1"); ok {
|
|
t.Fatal("zero source context")
|
|
}
|
|
if _, ok := view.Explain(configkit.Path{}); ok {
|
|
t.Fatal("zero explanation")
|
|
}
|
|
if err := view.WriteExplanation(&bytes.Buffer{}, configkit.Path{}); err == nil {
|
|
t.Fatal("zero explanation rendering")
|
|
}
|
|
}
|