71 lines
2.3 KiB
Go
71 lines
2.3 KiB
Go
|
|
package domainrefs
|
||
|
|
|
||
|
|
import (
|
||
|
|
"errors"
|
||
|
|
"reflect"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"go.maximumdirect.net/configkit"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestLoadEquivalentStructuredReferences(t *testing.T) {
|
||
|
|
yaml := []byte("name: Example\nnumber: 42\nfraction: 1.5\ninstruction: data\nnested: {enabled: true}\n")
|
||
|
|
json := []byte(`{"name":"Example","number":42,"fraction":1.5,"instruction":"data","nested":{"enabled":true}}`)
|
||
|
|
want, err := Load("example.yml", "application/yaml", yaml)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
for _, input := range []struct {
|
||
|
|
media string
|
||
|
|
data []byte
|
||
|
|
}{
|
||
|
|
{"application/x-yaml", yaml}, {"application/json", json},
|
||
|
|
} {
|
||
|
|
got, err := Load("example", input.media, input.data)
|
||
|
|
if err != nil || !reflect.DeepEqual(got, want) {
|
||
|
|
t.Fatalf("Load(%s) = %#v, %v; want %#v", input.media, got, err, want)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if want["number"] != int64(42) || want["fraction"] != float64(1.5) || want["instruction"] != "data" {
|
||
|
|
t.Fatalf("unexpected native values: %#v", want)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestLoadPreservesNativeFailure(t *testing.T) {
|
||
|
|
_, err := Load("roster.yml", "application/yaml", []byte("name: one\nname: two\n"))
|
||
|
|
var native *configkit.LoadError
|
||
|
|
if !errors.As(err, &native) || !strings.Contains(err.Error(), "roster.yml") {
|
||
|
|
t.Fatalf("expected contextual native error, got %v", err)
|
||
|
|
}
|
||
|
|
if _, err := Load("roster.json", "application/json", []byte("name: value")); err == nil {
|
||
|
|
t.Fatal("JSON media accepted YAML syntax")
|
||
|
|
}
|
||
|
|
_, err = Load("roster.json", "application/json", []byte(`{"name":"one","name":"two"}`))
|
||
|
|
if !errors.As(err, &native) {
|
||
|
|
t.Fatalf("expected native error for duplicate JSON keys, got %v", err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestLoadNativeStructuralControls(t *testing.T) {
|
||
|
|
got, err := Load("reference.yml", "application/yaml", []byte(`entries:
|
||
|
|
parent: {name: inherited}
|
||
|
|
child: {extends: parent, instruction: ordinary}
|
||
|
|
`))
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
child := got["entries"].(map[string]any)["child"].(map[string]any)
|
||
|
|
if child["name"] != "inherited" || child["instruction"] != "ordinary" {
|
||
|
|
t.Fatalf("unexpected resolved child: %#v", child)
|
||
|
|
}
|
||
|
|
if _, ok := child["extends"]; ok {
|
||
|
|
t.Fatalf("inheritance control was not consumed: %#v", child)
|
||
|
|
}
|
||
|
|
_, err = Load("reference.yml", "application/yaml", []byte("config: {imports: [{path: missing.yml}]}\n"))
|
||
|
|
var native *configkit.LoadError
|
||
|
|
if !errors.As(err, &native) {
|
||
|
|
t.Fatalf("expected native disabled-import error, got %v", err)
|
||
|
|
}
|
||
|
|
}
|