2022-06-11 09:30:22 +02:00
|
|
|
package formatter_test
|
2022-06-10 14:58:36 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2022-06-11 08:50:20 +02:00
|
|
|
"time"
|
2022-06-10 14:58:36 +02:00
|
|
|
|
2022-06-11 09:30:22 +02:00
|
|
|
"ewintr.nl/adoc/document"
|
2022-06-10 14:58:36 +02:00
|
|
|
"ewintr.nl/adoc/element"
|
2022-06-11 09:30:22 +02:00
|
|
|
"ewintr.nl/adoc/formatter"
|
2022-06-10 14:58:36 +02:00
|
|
|
"ewintr.nl/go-kit/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAsciiDoc(t *testing.T) {
|
2022-06-11 09:30:22 +02:00
|
|
|
input := &document.Document{
|
|
|
|
Title: "A Title",
|
|
|
|
Author: "Author",
|
|
|
|
Date: time.Date(2022, time.Month(6), 11, 0, 0, 0, 0, time.UTC),
|
2022-06-11 08:50:20 +02:00
|
|
|
Attributes: map[string]string{
|
|
|
|
"key1": "value 1",
|
|
|
|
"key2": "value 2",
|
|
|
|
},
|
2022-06-11 09:30:22 +02:00
|
|
|
Content: []element.Element{
|
|
|
|
element.Paragraph{
|
|
|
|
Elements: []element.Element{
|
|
|
|
element.Word("some"),
|
|
|
|
element.WhiteSpace(" "),
|
|
|
|
element.Word("text"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-06-11 08:50:20 +02:00
|
|
|
}
|
2022-06-11 09:30:22 +02:00
|
|
|
|
|
|
|
exp := `= A Title
|
|
|
|
Author
|
2022-06-11 08:50:20 +02:00
|
|
|
2022-06-11
|
|
|
|
:key1: value 1
|
|
|
|
:key2: value 2
|
2022-06-11 09:30:22 +02:00
|
|
|
|
|
|
|
some text
|
|
|
|
|
2022-06-11 08:50:20 +02:00
|
|
|
`
|
|
|
|
|
2022-06-11 09:30:22 +02:00
|
|
|
test.Equals(t, exp, formatter.NewAsciiDoc().Format(input))
|
2022-06-11 08:50:20 +02:00
|
|
|
}
|
|
|
|
|
2022-06-10 14:58:36 +02:00
|
|
|
func TestAsciiDocFragment(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: "pararaphs",
|
|
|
|
input: element.Paragraph{
|
|
|
|
Elements: []element.Element{
|
|
|
|
element.Word("a"),
|
|
|
|
element.WhiteSpace(" "),
|
|
|
|
element.Word("word"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
exp: "a word\n\n",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "strong",
|
|
|
|
input: element.Strong{
|
|
|
|
element.Word("something"),
|
|
|
|
element.WhiteSpace(" "),
|
|
|
|
element.Word("strong"),
|
|
|
|
},
|
|
|
|
exp: "*something strong*",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "nested",
|
|
|
|
input: element.Paragraph{
|
|
|
|
Elements: []element.Element{
|
|
|
|
element.Word("normal"),
|
|
|
|
element.WhiteSpace(" "),
|
|
|
|
element.Word("text"),
|
|
|
|
element.WhiteSpace(" "),
|
|
|
|
element.Strong{
|
|
|
|
element.WhiteSpace(" "),
|
|
|
|
element.Word("and"),
|
|
|
|
element.WhiteSpace(" "),
|
|
|
|
element.Word("strong"),
|
|
|
|
},
|
|
|
|
element.WhiteSpace(" "),
|
|
|
|
element.Word("too"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
exp: "normal text * and strong* too\n\n",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "emphasis",
|
|
|
|
input: element.Emphasis{
|
|
|
|
element.Word("yes"),
|
|
|
|
},
|
|
|
|
exp: "_yes_",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "code",
|
|
|
|
input: element.Code{
|
|
|
|
element.Word("simple"),
|
|
|
|
},
|
|
|
|
exp: "`simple`",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "link",
|
|
|
|
input: element.Link{
|
|
|
|
URL: "http://example.com",
|
|
|
|
Title: "an example",
|
|
|
|
},
|
|
|
|
exp: `http://example.com[an example]`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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: `* item 1
|
|
|
|
* item 2
|
|
|
|
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "code block",
|
|
|
|
input: element.CodeBlock{
|
|
|
|
element.Word("some"),
|
|
|
|
element.WhiteSpace(" "),
|
|
|
|
element.Word("text"),
|
|
|
|
element.WhiteSpace("\n"),
|
|
|
|
element.Word("<p>with</p>"),
|
|
|
|
element.WhiteSpace("\t"),
|
|
|
|
element.Word("formatting"),
|
|
|
|
},
|
|
|
|
exp: `----
|
|
|
|
some text
|
|
|
|
<p>with</p> formatting
|
|
|
|
----
|
|
|
|
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "subtitle",
|
|
|
|
input: element.SubTitle("a subtitle"),
|
|
|
|
exp: "== a subtitle\n\n",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "subsubtitle",
|
|
|
|
input: element.SubSubTitle("a subsubtitle"),
|
|
|
|
exp: "=== a subsubtitle\n\n",
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2022-06-11 09:30:22 +02:00
|
|
|
test.Equals(t, tc.exp, formatter.NewAsciiDoc().FormatFragments(tc.input))
|
2022-06-10 14:58:36 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|