Fix misaligned indentation in comments (#734)

This commit is contained in:
Shuhei Kitagawa 2025-05-29 06:34:24 +02:00 committed by GitHub
parent 6a6c998c2f
commit f1c23f747b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 58 additions and 4 deletions

View file

@ -1452,8 +1452,9 @@ foo:
func TestComment(t *testing.T) {
tests := []struct {
name string
yaml string
name string
yaml string
expected string
}{
{
name: "map with comment",
@ -1541,6 +1542,52 @@ foo: > # comment
# This comment is in its own document
---
a: b
`,
},
{
name: "map with misaligned indentation in comments",
yaml: `
# commentA
a: #commentB
# commentC
b: c # commentD
# commentE
d: e # commentF
# commentG
`,
expected: `
# commentA
a: #commentB
# commentC
b: c # commentD
# commentE
d: e # commentF
# commentG
`,
},
{
name: "sequence with misaligned indentation in comments",
yaml: `
# commentA
- a # commentB
# commentC
- b: # commentD
# commentE
- d # commentF
# commentG
- e # commentG
# commentH
`,
expected: `
# commentA
- a # commentB
# commentC
- b: # commentD
# commentE
- d # commentF
# commentG
- e # commentG
# commentH
`,
},
}
@ -1551,8 +1598,12 @@ a: b
t.Fatalf("%+v", err)
}
got := "\n" + f.String()
if test.yaml != got {
t.Fatalf("expected:%s\ngot:%s", test.yaml, got)
expected := test.yaml
if test.expected != "" {
expected = test.expected
}
if expected != got {
t.Fatalf("expected:%s\ngot:%s", expected, got)
}
})
}