68 lines
2.0 KiB
Go
68 lines
2.0 KiB
Go
|
|
package engine
|
||
|
|
|
||
|
|
import (
|
||
|
|
"reflect"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestResultValueNavigation(t *testing.T) {
|
||
|
|
type key string
|
||
|
|
type embedded struct{ Name string }
|
||
|
|
type root struct {
|
||
|
|
Embedded *embedded `yaml:",inline"`
|
||
|
|
Extra map[key]any `yaml:",inline"`
|
||
|
|
List []int `yaml:"list"`
|
||
|
|
Any any `yaml:"any"`
|
||
|
|
}
|
||
|
|
value := root{Embedded: &embedded{Name: "named"}, Extra: map[key]any{"a/b": nil}, List: []int{3}, Any: map[string]any{"n": []any{"ok"}}}
|
||
|
|
parsed, _ := ParsePath("/any/n/0")
|
||
|
|
for _, tc := range []struct {
|
||
|
|
path Path
|
||
|
|
want any
|
||
|
|
ok bool
|
||
|
|
}{
|
||
|
|
{Keys("name"), "named", true},
|
||
|
|
{Keys("a/b"), nil, true},
|
||
|
|
{Keys("missing"), nil, false},
|
||
|
|
{Keys("list").Index(0), 3, true},
|
||
|
|
{Keys("list").Key("0"), nil, false},
|
||
|
|
{parsed, "ok", true},
|
||
|
|
{Keys("list").Index(2), nil, false},
|
||
|
|
{Path{}.Index(-1), nil, false},
|
||
|
|
} {
|
||
|
|
got, ok := ResultValue(value, tc.path, SecretBinding{})
|
||
|
|
if ok != tc.ok || !reflect.DeepEqual(got, tc.want) {
|
||
|
|
t.Errorf("%s: got %#v, %v; want %#v, %v", tc.path.String(), got, ok, tc.want, tc.ok)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
value.Embedded = nil
|
||
|
|
if _, ok := ResultValue(value, Keys("name"), SecretBinding{}); ok {
|
||
|
|
t.Fatal("nil inline pointer was traversed")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestResultValueTerminalAndPointerCycle(t *testing.T) {
|
||
|
|
type pointer *pointer
|
||
|
|
var cycle pointer
|
||
|
|
cycle = &cycle
|
||
|
|
var nilSlice []string
|
||
|
|
for _, value := range []any{nil, nilSlice, make(chan int), cycle} {
|
||
|
|
got, ok := ResultValue(value, Path{}, SecretBinding{})
|
||
|
|
if !ok || reflect.TypeOf(got) != reflect.TypeOf(value) {
|
||
|
|
t.Fatalf("terminal %#v: got %#v, %v", value, got, ok)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if _, ok := ResultValue(cycle, Keys("x"), SecretBinding{}); ok {
|
||
|
|
t.Fatal("pointer cycle was traversed")
|
||
|
|
}
|
||
|
|
channel := make(chan int)
|
||
|
|
value := struct{ Channel chan int }{Channel: channel}
|
||
|
|
got, ok := ResultValue(value, Keys("channel"), SecretBinding{})
|
||
|
|
if !ok || got != channel {
|
||
|
|
t.Fatalf("unsupported terminal: got %#v, %v", got, ok)
|
||
|
|
}
|
||
|
|
if _, ok := ResultValue(value, Keys("channel", "x"), SecretBinding{}); ok {
|
||
|
|
t.Fatal("unsupported terminal accepted navigation")
|
||
|
|
}
|
||
|
|
}
|