63 lines
1.8 KiB
Go
63 lines
1.8 KiB
Go
|
|
package validation
|
||
|
|
|
||
|
|
import (
|
||
|
|
"reflect"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestNativeHandoff(t *testing.T) {
|
||
|
|
value, err := LoadDomain("synthetic.yml", []byte(`name: "Weather 🌧"
|
||
|
|
count: 9223372036854775807
|
||
|
|
amount: 0.123456789012345678901234567890
|
||
|
|
aliases: null
|
||
|
|
entries: []
|
||
|
|
context: {}
|
||
|
|
`))
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
want := map[string]any{"name": "Weather 🌧", "count": int64(9223372036854775807), "amount": float64(0.12345678901234568), "aliases": nil, "entries": []any{}, "context": map[string]any{}}
|
||
|
|
if !reflect.DeepEqual(value, want) {
|
||
|
|
t.Fatalf("native result %#v differs from %#v", value, want)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestNativeFailures(t *testing.T) {
|
||
|
|
for _, input := range []string{
|
||
|
|
"a: 9223372036854775808", "a: 1e500", "a: yes", "a: TRUE", "a: 0x10",
|
||
|
|
"a: !!float 1", "a: &x value\nb: *x", "a: 1\na: 2", "1: value", `"<<": value`,
|
||
|
|
`{"name":"Weather \ud83c\udf27"}`, "config: {note: ordinary}",
|
||
|
|
} {
|
||
|
|
t.Run(input, func(t *testing.T) {
|
||
|
|
if _, err := LoadDomain("synthetic.yml", []byte(input)); err == nil || !strings.HasPrefix(err.Error(), "load:") {
|
||
|
|
t.Fatalf("want native failure, got %v", err)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestNativeControls(t *testing.T) {
|
||
|
|
value, err := LoadDomain("synthetic.yml", []byte(`records:
|
||
|
|
base: {name: Base}
|
||
|
|
child: {extends: base}
|
||
|
|
context:
|
||
|
|
extends: null
|
||
|
|
config: {note: ordinary}
|
||
|
|
`))
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
records := value["records"].(map[string]any)
|
||
|
|
if !reflect.DeepEqual(records["base"], records["child"]) {
|
||
|
|
t.Fatalf("inheritance did not resolve: %#v", records)
|
||
|
|
}
|
||
|
|
context := value["context"].(map[string]any)
|
||
|
|
if _, found := context["extends"]; found {
|
||
|
|
t.Fatal("extends survived as ordinary data")
|
||
|
|
}
|
||
|
|
if !reflect.DeepEqual(context["config"], map[string]any{"note": "ordinary"}) {
|
||
|
|
t.Fatalf("nested config data changed: %#v", context)
|
||
|
|
}
|
||
|
|
}
|