48 lines
1.3 KiB
Go
48 lines
1.3 KiB
Go
package commandkit
|
|
|
|
import (
|
|
"bytes"
|
|
"go.yaml.in/yaml/v4"
|
|
"io"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestEffectiveYAMLFramingPreservesOneNativeDocument(t *testing.T) {
|
|
for _, native := range []string{
|
|
"--- {setting: value}\n", "---\t{setting: value}\n", "--- value\n",
|
|
"--- |\n first\n second\n", "--- # authored\nsetting: value\n",
|
|
"setting: value\n", "%YAML 1.1\n--- {setting: value}\n",
|
|
} {
|
|
t.Run(native, func(t *testing.T) {
|
|
var want any
|
|
if err := yaml.Unmarshal([]byte(native), &want); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
framed := frameEffectiveYAML("app", []byte(native))
|
|
withoutLabel := bytes.Replace(framed, []byte("# scope: app\n"), nil, 1)
|
|
if !strings.HasPrefix(native, "---") && !strings.HasPrefix(native, "%") {
|
|
withoutLabel = bytes.TrimPrefix(withoutLabel, []byte("---\n"))
|
|
}
|
|
if !bytes.Equal(withoutLabel, []byte(native)) {
|
|
t.Fatalf("native content changed: %q", framed)
|
|
}
|
|
if !bytes.Contains(framed, []byte("# scope: app\n")) {
|
|
t.Fatalf("missing label: %q", framed)
|
|
}
|
|
decoder := yaml.NewDecoder(bytes.NewReader(framed))
|
|
var got any
|
|
if err := decoder.Decode(&got); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("got=%#v want=%#v", got, want)
|
|
}
|
|
if err := decoder.Decode(new(any)); err != io.EOF {
|
|
t.Fatalf("extra document: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|