package htmlformat
import (
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestFormat(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "missing closing tags are inserted",
input: `
`,
expected: `
`,
},
{
name: "html attribute escaping is normalized",
input: ` - A
- B
`,
expected: `
- A
- B
`,
},
{
name: "bare ampersands are escaped",
input: ` - A
- B
`,
expected: `
- A
- B
`,
},
{
name: "html elements are indented",
input: ` - A
- B
`,
expected: `
- A
- B
`,
},
{
name: "text fragments are supported",
input: `test 123`,
expected: `test 123` + "\n",
},
{
name: "phrasing content element children are kept on the same line, including punctuation",
input: ``,
expected: `
`,
},
{
name: "style content is indented consistently",
input: ``,
expected: `
`,
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
r := strings.NewReader(test.input)
w := new(strings.Builder)
if err := Fragment(w, r); err != nil {
t.Fatalf("failed to format: %v", err)
}
if diff := cmp.Diff(test.expected, w.String()); diff != "" {
t.Error(diff)
}
})
}
}