adoc/formatter/html_test.go

167 lines
3.2 KiB
Go
Raw Normal View History

2022-06-11 09:30:22 +02:00
package formatter_test
2022-03-22 06:53:01 +01:00
import (
"strings"
"testing"
2024-09-15 12:07:22 +02:00
"go-mod.ewintr.nl/adoc/element"
"go-mod.ewintr.nl/adoc/formatter"
"go-mod.ewintr.nl/adoc/parser"
"go-mod.ewintr.nl/go-kit/test"
2022-03-22 06:53:01 +01:00
)
func TestHTML(t *testing.T) {
input := `= A Title
Some document
With some text.`
exp := `<!DOCTYPE html>
<html>
<head>
<title>A Title</title>
</head>
<body>
<p>Some document</p>
<p>With some text.</p>
</body>
</html>
`
doc := parser.New(strings.NewReader(input)).Parse()
2022-06-11 09:30:22 +02:00
test.Equals(t, exp, formatter.NewHTML().Format(doc))
2022-03-22 06:53:01 +01:00
}
func TestHTMLFragment(t *testing.T) {
for _, tc := range []struct {
name string
input element.Element
exp string
}{
{
name: "whitespace",
input: element.WhiteSpace("\n"),
exp: " ",
},
{
name: "word",
input: element.Word("word"),
exp: "word",
},
{
name: "word with html",
input: element.Word("<h1>hi</h1>"),
exp: "&lt;h1&gt;hi&lt;/h1&gt;",
},
{
name: "pararaphs",
input: element.Paragraph{
Elements: []element.Element{
element.Word("a"),
element.WhiteSpace(" "),
element.Word("word"),
},
},
exp: "<p>a word</p>\n",
},
{
name: "strong",
input: element.Strong{
element.Word("something"),
element.WhiteSpace(" "),
element.Word("strong"),
},
exp: "<strong>something strong</strong>",
},
{
name: "nested",
input: element.Paragraph{
Elements: []element.Element{
element.Word("normal"),
element.WhiteSpace(" "),
element.Word("text"),
element.Strong{
element.WhiteSpace(" "),
element.Word("and"),
element.WhiteSpace(" "),
element.Word("strong"),
},
element.WhiteSpace(" "),
element.Word("too"),
},
},
exp: "<p>normal text<strong> and strong</strong> too</p>\n",
},
{
name: "emphasis",
input: element.Emphasis{
element.Word("yes"),
},
exp: "<em>yes</em>",
},
{
name: "code",
input: element.Code{
element.Word("simple"),
},
exp: "<code>simple</code>",
},
{
name: "link",
input: element.Link{
URL: "http://example.com",
Title: "an example",
},
exp: `<a href="http://example.com">an example</a>`,
},
{
name: "list",
input: element.List{
element.ListItem{
element.Word("item"),
element.WhiteSpace(" "),
element.Word("1"),
},
element.ListItem{
element.Word("item"),
element.WhiteSpace(" "),
element.Word("2"),
},
},
exp: `<ul>
<li>item 1</li>
<li>item 2</li>
</ul>
`,
},
{
name: "code block",
input: element.CodeBlock{
element.Word("some"),
element.WhiteSpace(" "),
element.Word("text"),
element.WhiteSpace("\n"),
2022-03-31 06:56:26 +02:00
element.Word("<p>with</p>"),
2022-03-22 06:53:01 +01:00
element.WhiteSpace("\t"),
element.Word("formatting"),
},
exp: `<pre><code>some text
2022-03-31 06:56:26 +02:00
&lt;p&gt;with&lt;/p&gt; formatting</code></pre>`,
2022-03-22 06:53:01 +01:00
},
{
name: "subtitle",
input: element.SubTitle("a subtitle"),
exp: "<h2 id=\"a-subtitle\">a subtitle</h2>\n",
},
{
name: "subsubtitle",
input: element.SubSubTitle("a subsubtitle"),
exp: "<h3 id=\"a-subsubtitle\">a subsubtitle</h3>\n",
},
} {
t.Run(tc.name, func(t *testing.T) {
2022-06-11 09:30:22 +02:00
test.Equals(t, tc.exp, formatter.NewHTML().FormatFragments(tc.input))
2022-03-22 06:53:01 +01:00
})
}
}