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: `
    1. A
    2. B
    `, expected: `
    1. A
    2. B
    `, }, { name: "bare ampersands are escaped", input: `
    1. A
    2. B
    `, expected: `
    1. A
    2. B
    `, }, { name: "html elements are indented", input: `
    1. A
    2. B
    `, expected: `
    1. A
    2. B
    `, }, { name: "text fragments are supported", input: `test 123`, expected: `test 123` + "\n", }, { name: "script tags are only indented", input: ``, expected: `` + "\n", }, { name: "phrasing content element children are kept on the same line, including punctuation", input: ``, expected: ` `, }, { name: "style content is indented consistently", input: ``, expected: ` `, }, { name: "space after text node with punctuation is preserved", input: `

    Link. Another

    `, expected: `

    Link. Another

    `, }, { name: "spaces are not added around elements ending with punctuation", input: "
    ", expected: `
    `, }, { name: "HTML entities in pre/code blocks are preserved", input: `
    <tab>
    `, expected: `
    <tab>
    `, }, { name: "multiple HTML entities in pre blocks are preserved", input: `
    <html> & "quotes"
    `, expected: `
    <html> & "quotes"
    `, }, { name: "HTML entities in nested code within pre are preserved", input: `
    Some text <tag> more text
    `, expected: `
    Some text <tag> more text
    `, }, { name: "ampersands in pre blocks are preserved", input: `
    npm install && npm test
    `, expected: `
    npm install && npm test
    `, }, { name: "complex HTML entities in code blocks are preserved", input: `
    const html = "<div class="test">Hello & goodbye</div>";
    `, expected: `
    const html = "<div class="test">Hello & goodbye</div>";
    `, }, { name: "HTML entities in code blocks (no pre) are preserved", input: `const html = "<div class="test">Hello & goodbye</div>";`, expected: `const html = "<div class="test">Hello & goodbye</div>"; `, }, } 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) } }) } } func TestFormatDocument(t *testing.T) { tests := []struct { name string input string expected string }{ { name: "HTML5 doctype is preserved", input: "", expected: "\n\n \n \n \n \n\n", }, } 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 := Document(w, r); err != nil { t.Fatalf("failed to format: %v", err) } if diff := cmp.Diff(test.expected, w.String()); diff != "" { t.Error(diff) } }) } }