Skip to content

Commit

Permalink
Add tests for failing decode/encode
Browse files Browse the repository at this point in the history
  • Loading branch information
malcolmholmes committed Jan 15, 2024
1 parent b73cb94 commit 1f79f66
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 182 deletions.
182 changes: 0 additions & 182 deletions cmd/dev/main.go

This file was deleted.

114 changes: 114 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2890,3 +2890,117 @@ func TestSameNameInineStruct(t *testing.T) {
t.Fatalf("failed to decode")
}
}

// Test newline in key as encoded by alternative YAML tools:
func TestUnmarshalNewlineInKey(t *testing.T) {
tests := []struct {
name string
spec string
}{
{
name: "multiline-key-with-newline-01",
/* As rendered by this library from this JSON:
{
"a": "a",
"c\nc": "cc",
"d": "d"
}
*/
spec: `a: a
|-
c
c: cc
d: d
`,
},
{
name: "quoted-key-with-newline",
spec: `a: a
"c\nc": "cc"
d: d
`,
},
{
name: "multiline-key-with-newline-02",
// As encoded by alternative online YAML tools (e.g. https://www.json2yaml.com/)
spec: `a: a
? |-
c
c
: cc
d: d
`,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
decoder := yaml.NewDecoder(strings.NewReader(test.spec))
var out map[string]interface{}
for i := 0; ; i++ {
err := decoder.Decode(&out)
if err == io.EOF {
break
}
if err != nil {
t.Fatalf("Error on iteration %d: %v", i, err)
}
}
})
}
}

func TestUnmarshalBackslashInKey(t *testing.T) {
spec := `outer:
"b\\b": b
d : d
`

decoder := yaml.NewDecoder(strings.NewReader(spec))
var out map[string]interface{}
for i := 0; ; i++ {
err := decoder.Decode(&out)
if err == io.EOF {
break
}
if err != nil {
t.Fatalf("Error on iteration %d: %v", i, err)
}
}
}

func TestUnmarshalQuotesInKey(t *testing.T) {
spec := `outer:
"a\"b\"c": a
"d\"e\"f": d
`
decoder := yaml.NewDecoder(strings.NewReader(spec))
var out map[string]interface{}
for i := 0; ; i++ {
err := decoder.Decode(&out)
if err == io.EOF {
break
}
if err != nil {
t.Fatalf("Error on iteration %d: %v", i, err)
}
}
}

func TestUnmarshalCollectedEscapesInKey(t *testing.T) {
spec := `outer:
"a\"b\"c\\d\ne": a
"d\"e\"f": d
`
decoder := yaml.NewDecoder(strings.NewReader(spec))
var out map[string]interface{}
for i := 0; ; i++ {
err := decoder.Decode(&out)
if err == io.EOF {
break
}
if err != nil {
t.Fatalf("Error on iteration %d: %v", i, err)
}
}
}
33 changes: 33 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math"
"reflect"
"strconv"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -1630,3 +1631,35 @@ b:
t.Fatalf("failed to encode. expected %s but got %s", expected, got)
}
}

func TestMarshalNewlineInKey(t *testing.T) {
spec := map[string]string{
"a": "a",
"c\nc": "cc",
"d": "d",
}
expected := []string{
`a: a
"c\nc": "cc"
d: d
`,
`---
a: a
? |-
c
c
: cc
d: d
`}
y, err := yaml.Marshal(spec)
if err != nil {
t.Fatalf("Error marshalling YAML: %v", err)
}
for _, exp := range expected {
if string(y) == exp {
return
}
}

t.Fatalf("testNewlineEncode FAILED\nExpected: '%s'\nGot: '%s'", strings.Join(expected, "\nor\n"), string(y))
}

0 comments on commit 1f79f66

Please sign in to comment.