From d3d0468955b9a7a9c9f36e4a568fed2d2b1c7ed3 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Mon, 23 Dec 2024 15:32:31 +0900 Subject: [PATCH] add test case --- yaml_test.go | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 yaml_test.go diff --git a/yaml_test.go b/yaml_test.go new file mode 100644 index 0000000..d6d264c --- /dev/null +++ b/yaml_test.go @@ -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)) + } +}