Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
goccy committed Dec 23, 2024
1 parent 161b252 commit d3d0468
Showing 1 changed file with 139 additions and 0 deletions.
139 changes: 139 additions & 0 deletions yaml_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package yaml_test

import (
"strings"
"testing"

"github.com/goccy/go-yaml"
)

func TestAutoAnchor(t *testing.T) {
var data = `
a: &a [_,_,_,_,_,_,_,_,_,_,_,_,_,_,_]
b: &b [*a,*a,*a,*a,*a,*a,*a,*a,*a,*a]
c: &c [*b,*b,*b,*b,*b,*b,*b,*b,*b,*b]
d: &d [*c,*c,*c,*c,*c,*c,*c,*c,*c,*c]
e: &e [*d,*d,*d,*d,*d,*d,*d,*d,*d,*d]
f: &f [*e,*e,*e,*e,*e,*e,*e,*e,*e,*e]
g: &g [*f,*f,*f,*f,*f,*f,*f,*f,*f,*f]
h: &h [*g,*g,*g,*g,*g,*g,*g,*g,*g,*g]
i: &i [*h,*h,*h,*h,*h,*h,*h,*h,*h,*h]
`
var v any
if err := yaml.Unmarshal([]byte(data), &v); err != nil {
t.Fatal(err)
}
got, err := yaml.MarshalWithOptions(v, yaml.UseAutoAnchor())
if err != nil {
t.Fatal(err)
}
expected := `
a: &a
- _
- _
- _
- _
- _
- _
- _
- _
- _
- _
- _
- _
- _
- _
- _
b: &b
- *a
- *a
- *a
- *a
- *a
- *a
- *a
- *a
- *a
- *a
c: &c
- *b
- *b
- *b
- *b
- *b
- *b
- *b
- *b
- *b
- *b
d: &d
- *c
- *c
- *c
- *c
- *c
- *c
- *c
- *c
- *c
- *c
e: &e
- *d
- *d
- *d
- *d
- *d
- *d
- *d
- *d
- *d
- *d
f: &f
- *e
- *e
- *e
- *e
- *e
- *e
- *e
- *e
- *e
- *e
g: &g
- *f
- *f
- *f
- *f
- *f
- *f
- *f
- *f
- *f
- *f
h: &h
- *g
- *g
- *g
- *g
- *g
- *g
- *g
- *g
- *g
- *g
i:
- *h
- *h
- *h
- *h
- *h
- *h
- *h
- *h
- *h
- *h
`
if strings.TrimPrefix(expected, "\n") != string(got) {
t.Fatalf("failed to encode: %s", string(got))
}
}

0 comments on commit d3d0468

Please sign in to comment.