1542 lines
32 KiB
Go
1542 lines
32 KiB
Go
package textfmt_test
|
|
|
|
import (
|
|
"bytes"
|
|
"code.squareroundforest.org/arpio/textfmt"
|
|
"testing"
|
|
)
|
|
|
|
func TestHTML(t *testing.T) {
|
|
t.Run("invalid", func(t *testing.T) {
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, textfmt.Doc(textfmt.Entry{})); err == nil {
|
|
t.Fatal("failed to fail")
|
|
}
|
|
})
|
|
|
|
t.Run("empty", func(t *testing.T) {
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, textfmt.Doc()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if b.String() != "" {
|
|
t.Fatal(b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("example", func(t *testing.T) {
|
|
doc := textfmt.Doc(
|
|
textfmt.Title(0, "Example Text"),
|
|
|
|
textfmt.Wrap(
|
|
textfmt.Indent(
|
|
textfmt.Paragraph(textfmt.Text("Below you can find some test text, with various text items.")),
|
|
0,
|
|
8,
|
|
),
|
|
30,
|
|
),
|
|
|
|
textfmt.Title(1, "Document syntax:"),
|
|
|
|
textfmt.Indent(
|
|
textfmt.Syntax(
|
|
textfmt.Symbol("textfmt.Doc"),
|
|
textfmt.Symbol("("),
|
|
textfmt.ZeroOrMore(textfmt.Symbol("Entry")),
|
|
textfmt.Symbol(")"),
|
|
),
|
|
0,
|
|
8,
|
|
),
|
|
|
|
textfmt.Title(1, "Entries:"),
|
|
|
|
textfmt.Paragraph(textfmt.Text("textfmt supports the following entries:")),
|
|
|
|
textfmt.List(
|
|
textfmt.Item(textfmt.Text("CodeBlock")),
|
|
textfmt.Item(textfmt.Text("DefinitionList")),
|
|
textfmt.Item(textfmt.Text("List")),
|
|
textfmt.Item(textfmt.Text("NumberedDefinitionList")),
|
|
textfmt.Item(textfmt.Text("NumberedList")),
|
|
textfmt.Item(textfmt.Text("Paragraph")),
|
|
textfmt.Item(textfmt.Text("Syntax")),
|
|
textfmt.Item(textfmt.Text("Table")),
|
|
textfmt.Item(textfmt.Text("Title")),
|
|
),
|
|
|
|
textfmt.Title(1, "Entry explanations:"),
|
|
|
|
textfmt.Wrap(
|
|
textfmt.DefinitionList(
|
|
textfmt.Definition(
|
|
textfmt.Text("CodeBlock"),
|
|
textfmt.Text("a multiline block of code"),
|
|
),
|
|
textfmt.Definition(
|
|
textfmt.Text("DefinitionList"),
|
|
textfmt.Text("a list of definitions like this one"),
|
|
),
|
|
textfmt.Definition(
|
|
textfmt.Text("List"),
|
|
textfmt.Text("a list of items"),
|
|
),
|
|
textfmt.Definition(
|
|
textfmt.Text("NumberedDefinitionList"),
|
|
textfmt.Text("numbered definitions"),
|
|
),
|
|
textfmt.Definition(
|
|
textfmt.Text("NumberedList"),
|
|
textfmt.Text("numbered list"),
|
|
),
|
|
textfmt.Definition(
|
|
textfmt.Text("Paragraph"),
|
|
textfmt.Text("paragraph of text"),
|
|
),
|
|
textfmt.Definition(
|
|
textfmt.Text("Syntax"),
|
|
textfmt.Text("a syntax expression"),
|
|
),
|
|
textfmt.Definition(
|
|
textfmt.Text("Table"),
|
|
textfmt.Text("a table"),
|
|
),
|
|
textfmt.Definition(
|
|
textfmt.Text("Title"),
|
|
textfmt.Text("a title"),
|
|
),
|
|
),
|
|
48,
|
|
),
|
|
)
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTML(&b, doc, "en"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
const expect = `
|
|
<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>Example Text</title>
|
|
</head>
|
|
<body>
|
|
<h1>Example Text</h1>
|
|
<p>Below you can find some test text, with various text items.</p>
|
|
<h2>Document syntax:</h2>
|
|
<pre>
|
|
textfmt.Doc ( [Entry]... )
|
|
</pre>
|
|
<h2>Entries:</h2>
|
|
<p>textfmt supports the following entries:</p>
|
|
<ul>
|
|
<li>CodeBlock</li>
|
|
<li>DefinitionList</li>
|
|
<li>List</li>
|
|
<li>NumberedDefinitionList</li>
|
|
<li>NumberedList</li>
|
|
<li>Paragraph</li>
|
|
<li>Syntax</li>
|
|
<li>Table</li>
|
|
<li>Title</li>
|
|
</ul>
|
|
<h2>Entry explanations:</h2>
|
|
<dl>
|
|
<dt>CodeBlock</dt>
|
|
<dd>a multiline block of code</dd>
|
|
<dt>DefinitionList</dt>
|
|
<dd>a list of definitions like this one</dd>
|
|
<dt>List</dt>
|
|
<dd>a list of items</dd>
|
|
<dt>NumberedDefinitionList</dt>
|
|
<dd>numbered definitions</dd>
|
|
<dt>NumberedList</dt>
|
|
<dd>numbered list</dd>
|
|
<dt>Paragraph</dt>
|
|
<dd>paragraph of text</dd>
|
|
<dt>Syntax</dt>
|
|
<dd>a syntax expression</dd>
|
|
<dt>Table</dt>
|
|
<dd>a table</dd>
|
|
<dt>Title</dt>
|
|
<dd>a title</dd>
|
|
</dl>
|
|
</body>
|
|
</html>
|
|
`
|
|
|
|
if "\n"+b.String() != expect {
|
|
t.Log("\n" + b.String())
|
|
t.Log(expect)
|
|
logBytes(t, "\n"+b.String())
|
|
logBytes(t, expect)
|
|
t.Fatal()
|
|
}
|
|
})
|
|
|
|
t.Run("escape", func(t *testing.T) {
|
|
var b bytes.Buffer
|
|
doc := textfmt.Doc(textfmt.Paragraph(textfmt.Text("Some sample text... with invalid chars.")))
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
const expect = "<p>Some sample text... with invalid chars.</p>\n"
|
|
if b.String() != expect {
|
|
logBytes(t, b.String())
|
|
logBytes(t, expect)
|
|
t.Log(b.String())
|
|
t.Log(expect)
|
|
t.Fatal()
|
|
}
|
|
})
|
|
|
|
t.Run("styling", func(t *testing.T) {
|
|
doc := textfmt.Doc(
|
|
textfmt.Paragraph(
|
|
textfmt.Cat(
|
|
textfmt.Italic(textfmt.Text("Some sample text... with")),
|
|
textfmt.Bold(textfmt.Text(" some ")),
|
|
textfmt.Italic(textfmt.Text("styling.")),
|
|
),
|
|
),
|
|
)
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if b.String() != "<p><i>Some sample text... with</i><b> some </b><i>styling.</i></p>\n" {
|
|
t.Fatal(b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("single line by default", func(t *testing.T) {
|
|
var b bytes.Buffer
|
|
doc := textfmt.Doc(textfmt.Paragraph(textfmt.Text("Some sample text...\n\non multiple lines.")))
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if b.String() != "<p>Some sample text... on multiple lines.</p>\n" {
|
|
t.Fatal(b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("wrap", func(t *testing.T) {
|
|
var b bytes.Buffer
|
|
doc := textfmt.Doc(
|
|
textfmt.Wrap(textfmt.Paragraph(textfmt.Text("Some sample text...\n on multiple lines.")), 15),
|
|
)
|
|
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
const expect = `
|
|
<p>Some sample
|
|
text...
|
|
on multiple
|
|
lines.</p>
|
|
`
|
|
|
|
if "\n"+b.String() != expect {
|
|
t.Fatal("\n" + b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("failing writer", func(t *testing.T) {
|
|
w := &failingWriter{failAfter: 15}
|
|
doc := textfmt.Doc(
|
|
textfmt.Paragraph(textfmt.Text("Some sample text...\non multiple lines.")),
|
|
)
|
|
|
|
if err := textfmt.HTMLFragment(w, doc); err == nil {
|
|
t.Fatal("failed to fail")
|
|
}
|
|
})
|
|
|
|
t.Run("concatenate", func(t *testing.T) {
|
|
doc := textfmt.Doc(
|
|
textfmt.Paragraph(
|
|
textfmt.Cat(
|
|
textfmt.Text("Text from"),
|
|
textfmt.Text(" multiple"),
|
|
textfmt.Text(" pieces."),
|
|
),
|
|
),
|
|
)
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if b.String() != "<p>Text from multiple pieces.</p>\n" {
|
|
t.Fatal(b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("link", func(t *testing.T) {
|
|
t.Run("without label", func(t *testing.T) {
|
|
var b bytes.Buffer
|
|
doc := textfmt.Doc(textfmt.Paragraph(textfmt.Link("", "https://sqrndfst.org")))
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if b.String() != "<p><a href=\"https://sqrndfst.org\"></a></p>\n" {
|
|
t.Fatal(b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("with label", func(t *testing.T) {
|
|
var b bytes.Buffer
|
|
doc := textfmt.Doc(textfmt.Paragraph(textfmt.Link("a link", "https://sqrndfst.org")))
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if b.String() != "<p><a href=\"https://sqrndfst.org\">a link</a></p>\n" {
|
|
t.Fatal(b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("newline in link", func(t *testing.T) {
|
|
var b bytes.Buffer
|
|
doc := textfmt.Doc(textfmt.Paragraph(textfmt.Link("a\nlink", "https://sqrndfst.org\n/foo")))
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if b.String() != "<p><a href=\"https://sqrndfst.org\n/foo\">a link</a></p>\n" {
|
|
t.Fatal(b.String())
|
|
}
|
|
})
|
|
})
|
|
|
|
t.Run("title", func(t *testing.T) {
|
|
var b bytes.Buffer
|
|
doc := textfmt.Doc(textfmt.Title(0, "This is a title"))
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if b.String() != "<h1>This is a title</h1>\n" {
|
|
t.Fatal(b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("paragraph", func(t *testing.T) {
|
|
t.Run("unwrapped", func(t *testing.T) {
|
|
var b bytes.Buffer
|
|
doc := textfmt.Doc(textfmt.Paragraph(textfmt.Text("This is a paragraph.")))
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if b.String() != "<p>This is a paragraph.</p>\n" {
|
|
t.Fatal(b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("wrapped", func(t *testing.T) {
|
|
var b bytes.Buffer
|
|
doc := textfmt.Doc(
|
|
textfmt.Wrap(textfmt.Paragraph(textfmt.Text("This is a paragraph.")), 12),
|
|
)
|
|
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if b.String() != "<p>This is a\nparagraph.\n</p>\n" {
|
|
t.Fatal(b.String())
|
|
}
|
|
})
|
|
})
|
|
|
|
t.Run("list", func(t *testing.T) {
|
|
t.Run("unwrapped", func(t *testing.T) {
|
|
doc := textfmt.Doc(
|
|
textfmt.List(
|
|
textfmt.Item(textfmt.Text("this is an item")),
|
|
textfmt.Item(textfmt.Text("this is another item")),
|
|
textfmt.Item(textfmt.Text("this is a third item")),
|
|
),
|
|
)
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
const expect = `
|
|
<ul>
|
|
<li>this is an item</li>
|
|
<li>this is another item</li>
|
|
<li>this is a third item</li>
|
|
</ul>
|
|
`
|
|
|
|
if "\n"+b.String() != expect {
|
|
t.Fatal("\n" + b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("wrapped", func(t *testing.T) {
|
|
doc := textfmt.Doc(
|
|
textfmt.Wrap(
|
|
textfmt.List(
|
|
textfmt.Item(textfmt.Text("this is an item")),
|
|
textfmt.Item(textfmt.Text("this is another item")),
|
|
textfmt.Item(textfmt.Text("this is a third item")),
|
|
),
|
|
18,
|
|
),
|
|
)
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
const expect = `
|
|
<ul>
|
|
<li>this
|
|
is an item
|
|
</li>
|
|
<li>this
|
|
is another
|
|
item</li>
|
|
<li>this
|
|
is a third
|
|
item</li>
|
|
</ul>
|
|
`
|
|
|
|
if "\n"+b.String() != expect {
|
|
t.Fatal("\n" + b.String())
|
|
}
|
|
})
|
|
})
|
|
|
|
t.Run("numbered list", func(t *testing.T) {
|
|
t.Run("unwrapped", func(t *testing.T) {
|
|
doc := textfmt.Doc(
|
|
textfmt.NumberedList(
|
|
textfmt.Item(textfmt.Text("this is an item")),
|
|
textfmt.Item(textfmt.Text("this is another item")),
|
|
textfmt.Item(textfmt.Text("this is a third item")),
|
|
),
|
|
)
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
const expect = `
|
|
<ol>
|
|
<li>this is an item</li>
|
|
<li>this is another item</li>
|
|
<li>this is a third item</li>
|
|
</ol>
|
|
`
|
|
|
|
if "\n"+b.String() != expect {
|
|
t.Fatal("\n" + b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("wrapped", func(t *testing.T) {
|
|
doc := textfmt.Doc(
|
|
textfmt.Wrap(
|
|
textfmt.NumberedList(
|
|
textfmt.Item(textfmt.Text("this is an item")),
|
|
textfmt.Item(textfmt.Text("this is another item")),
|
|
textfmt.Item(textfmt.Text("this is a third item")),
|
|
),
|
|
18,
|
|
),
|
|
)
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
const expect = `
|
|
<ol>
|
|
<li>this
|
|
is an item
|
|
</li>
|
|
<li>this
|
|
is another
|
|
item</li>
|
|
<li>this
|
|
is a third
|
|
item</li>
|
|
</ol>
|
|
`
|
|
|
|
if "\n"+b.String() != expect {
|
|
t.Fatal("\n" + b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("long numbered list", func(t *testing.T) {
|
|
doc := textfmt.Doc(
|
|
textfmt.NumberedList(
|
|
textfmt.Item(textfmt.Text("this is an item")),
|
|
textfmt.Item(textfmt.Text("this is another item")),
|
|
textfmt.Item(textfmt.Text("this is the third item")),
|
|
textfmt.Item(textfmt.Text("this is the fourth item")),
|
|
textfmt.Item(textfmt.Text("this is the fifth item")),
|
|
textfmt.Item(textfmt.Text("this is the sixth item")),
|
|
textfmt.Item(textfmt.Text("this is the seventh item")),
|
|
textfmt.Item(textfmt.Text("this is the eighth item")),
|
|
textfmt.Item(textfmt.Text("this is the nineth item")),
|
|
textfmt.Item(textfmt.Text("this is the tenth item")),
|
|
textfmt.Item(textfmt.Text("this is the eleventh item")),
|
|
textfmt.Item(textfmt.Text("this is the twelfth item")),
|
|
),
|
|
)
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
const expect = `
|
|
<ol>
|
|
<li>this is an item</li>
|
|
<li>this is another item</li>
|
|
<li>this is the third item</li>
|
|
<li>this is the fourth item</li>
|
|
<li>this is the fifth item</li>
|
|
<li>this is the sixth item</li>
|
|
<li>this is the seventh item</li>
|
|
<li>this is the eighth item</li>
|
|
<li>this is the nineth item</li>
|
|
<li>this is the tenth item</li>
|
|
<li>this is the eleventh item</li>
|
|
<li>this is the twelfth item</li>
|
|
</ol>
|
|
`
|
|
|
|
if "\n"+b.String() != expect {
|
|
t.Fatal("\n" + b.String())
|
|
}
|
|
})
|
|
})
|
|
|
|
t.Run("definition list", func(t *testing.T) {
|
|
t.Run("unwrapped", func(t *testing.T) {
|
|
doc := textfmt.Doc(
|
|
textfmt.DefinitionList(
|
|
textfmt.Definition(textfmt.Text("red"), textfmt.Text("looks like strawberry")),
|
|
textfmt.Definition(textfmt.Text("green"), textfmt.Text("looks like grass")),
|
|
textfmt.Definition(textfmt.Text("blue"), textfmt.Text("looks like sky")),
|
|
),
|
|
)
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
const expect = `
|
|
<dl>
|
|
<dt>red</dt>
|
|
<dd>looks like strawberry</dd>
|
|
<dt>green</dt>
|
|
<dd>looks like grass</dd>
|
|
<dt>blue</dt>
|
|
<dd>looks like sky</dd>
|
|
</dl>
|
|
`
|
|
|
|
if "\n"+b.String() != expect {
|
|
t.Fatal("\n" + b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("wrapped", func(t *testing.T) {
|
|
doc := textfmt.Doc(
|
|
textfmt.Wrap(
|
|
textfmt.DefinitionList(
|
|
textfmt.Definition(textfmt.Text("red"), textfmt.Text("looks like strawberry")),
|
|
textfmt.Definition(textfmt.Text("green"), textfmt.Text("looks like grass")),
|
|
textfmt.Definition(textfmt.Text("blue"), textfmt.Text("looks like sky")),
|
|
),
|
|
24,
|
|
),
|
|
)
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
const expect = `
|
|
<dl>
|
|
<dt>red</dt>
|
|
<dd>looks like
|
|
strawberry</dd>
|
|
<dt>green</dt>
|
|
<dd>looks like
|
|
grass</dd>
|
|
<dt>blue</dt>
|
|
<dd>looks like
|
|
sky</dd>
|
|
</dl>
|
|
`
|
|
|
|
if "\n"+b.String() != expect {
|
|
t.Fatal("\n" + b.String())
|
|
}
|
|
})
|
|
})
|
|
|
|
t.Run("numbered definition list", func(t *testing.T) {
|
|
t.Run("unwrapped", func(t *testing.T) {
|
|
doc := textfmt.Doc(
|
|
textfmt.NumberedDefinitionList(
|
|
textfmt.Definition(textfmt.Text("red"), textfmt.Text("looks like strawberry")),
|
|
textfmt.Definition(textfmt.Text("green"), textfmt.Text("looks like grass")),
|
|
textfmt.Definition(textfmt.Text("blue"), textfmt.Text("looks like sky")),
|
|
),
|
|
)
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
const expect = `
|
|
<dl>
|
|
<dt>1. red</dt>
|
|
<dd>looks like strawberry</dd>
|
|
<dt>2. green</dt>
|
|
<dd>looks like grass</dd>
|
|
<dt>3. blue</dt>
|
|
<dd>looks like sky</dd>
|
|
</dl>
|
|
`
|
|
|
|
if "\n"+b.String() != expect {
|
|
t.Fatal("\n" + b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("wrapped", func(t *testing.T) {
|
|
doc := textfmt.Doc(
|
|
textfmt.Wrap(
|
|
textfmt.NumberedDefinitionList(
|
|
textfmt.Definition(textfmt.Text("red"), textfmt.Text("looks like strawberry")),
|
|
textfmt.Definition(textfmt.Text("green"), textfmt.Text("looks like grass")),
|
|
textfmt.Definition(textfmt.Text("blue"), textfmt.Text("looks like sky")),
|
|
),
|
|
24,
|
|
),
|
|
)
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
const expect = `
|
|
<dl>
|
|
<dt>1. red</dt>
|
|
<dd>looks like
|
|
strawberry</dd>
|
|
<dt>2. green
|
|
</dt>
|
|
<dd>looks like
|
|
grass</dd>
|
|
<dt>3. blue</dt>
|
|
<dd>looks like
|
|
sky</dd>
|
|
</dl>
|
|
`
|
|
|
|
if "\n"+b.String() != expect {
|
|
t.Fatal("\n" + b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("long numbered definition list", func(t *testing.T) {
|
|
doc := textfmt.Doc(
|
|
textfmt.NumberedDefinitionList(
|
|
textfmt.Definition(textfmt.Text("one"), textfmt.Text("this is an item")),
|
|
textfmt.Definition(textfmt.Text("two"), textfmt.Text("this is another item")),
|
|
textfmt.Definition(textfmt.Text("three"), textfmt.Text("this is the third item")),
|
|
textfmt.Definition(textfmt.Text("four"), textfmt.Text("this is the fourth item")),
|
|
textfmt.Definition(textfmt.Text("five"), textfmt.Text("this is the fifth item")),
|
|
textfmt.Definition(textfmt.Text("six"), textfmt.Text("this is the sixth item")),
|
|
textfmt.Definition(textfmt.Text("seven"), textfmt.Text("this is the seventh item")),
|
|
textfmt.Definition(textfmt.Text("eight"), textfmt.Text("this is the eighth item")),
|
|
textfmt.Definition(textfmt.Text("nine"), textfmt.Text("this is the nineth item")),
|
|
textfmt.Definition(textfmt.Text("ten"), textfmt.Text("this is the tenth item")),
|
|
textfmt.Definition(textfmt.Text("eleven"), textfmt.Text("this is the eleventh item")),
|
|
textfmt.Definition(textfmt.Text("twelve"), textfmt.Text("this is the twelfth item")),
|
|
),
|
|
)
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
const expect = `
|
|
<dl>
|
|
<dt>1. one</dt>
|
|
<dd>this is an item</dd>
|
|
<dt>2. two</dt>
|
|
<dd>this is another item</dd>
|
|
<dt>3. three</dt>
|
|
<dd>this is the third item</dd>
|
|
<dt>4. four</dt>
|
|
<dd>this is the fourth item</dd>
|
|
<dt>5. five</dt>
|
|
<dd>this is the fifth item</dd>
|
|
<dt>6. six</dt>
|
|
<dd>this is the sixth item</dd>
|
|
<dt>7. seven</dt>
|
|
<dd>this is the seventh item</dd>
|
|
<dt>8. eight</dt>
|
|
<dd>this is the eighth item</dd>
|
|
<dt>9. nine</dt>
|
|
<dd>this is the nineth item</dd>
|
|
<dt>10. ten</dt>
|
|
<dd>this is the tenth item</dd>
|
|
<dt>11. eleven</dt>
|
|
<dd>this is the eleventh item</dd>
|
|
<dt>12. twelve</dt>
|
|
<dd>this is the twelfth item</dd>
|
|
</dl>
|
|
`
|
|
|
|
if "\n"+b.String() != expect {
|
|
t.Fatal("\n" + b.String())
|
|
}
|
|
})
|
|
})
|
|
|
|
t.Run("table", func(t *testing.T) {
|
|
t.Run("now rows", func(t *testing.T) {
|
|
var b bytes.Buffer
|
|
doc := textfmt.Doc(textfmt.Table())
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if b.String() != "<table></table>\n" {
|
|
t.Fatal(b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("no columns", func(t *testing.T) {
|
|
doc := textfmt.Doc(
|
|
textfmt.Table(
|
|
textfmt.Row(),
|
|
textfmt.Row(),
|
|
textfmt.Row(),
|
|
),
|
|
)
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if b.String() != "<table>\n\t<tr></tr>\n\t<tr></tr>\n\t<tr></tr>\n</table>\n" {
|
|
t.Fatal(b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("basic", func(t *testing.T) {
|
|
doc := textfmt.Doc(
|
|
textfmt.Table(
|
|
textfmt.Row(
|
|
textfmt.Cell(textfmt.Text("1")), textfmt.Cell(textfmt.Text("2")), textfmt.Cell(textfmt.Text("3")),
|
|
),
|
|
textfmt.Row(
|
|
textfmt.Cell(textfmt.Text("4")), textfmt.Cell(textfmt.Text("5")), textfmt.Cell(textfmt.Text("6")),
|
|
),
|
|
textfmt.Row(
|
|
textfmt.Cell(textfmt.Text("7")), textfmt.Cell(textfmt.Text("8")), textfmt.Cell(textfmt.Text("9")),
|
|
),
|
|
),
|
|
)
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
const expect = `
|
|
<table>
|
|
<tr>
|
|
<td>1</td>
|
|
<td>2</td>
|
|
<td>3</td>
|
|
</tr>
|
|
<tr>
|
|
<td>4</td>
|
|
<td>5</td>
|
|
<td>6</td>
|
|
</tr>
|
|
<tr>
|
|
<td>7</td>
|
|
<td>8</td>
|
|
<td>9</td>
|
|
</tr>
|
|
</table>
|
|
`
|
|
|
|
if "\n"+b.String() != expect {
|
|
t.Fatal("\n" + b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("basic with header", func(t *testing.T) {
|
|
doc := textfmt.Doc(
|
|
textfmt.Table(
|
|
textfmt.Header(
|
|
textfmt.Cell(textfmt.Text("1")), textfmt.Cell(textfmt.Text("-1")), textfmt.Cell(textfmt.Text("0")),
|
|
),
|
|
textfmt.Row(
|
|
textfmt.Cell(textfmt.Text("1")), textfmt.Cell(textfmt.Text("2")), textfmt.Cell(textfmt.Text("3")),
|
|
),
|
|
textfmt.Row(
|
|
textfmt.Cell(textfmt.Text("4")), textfmt.Cell(textfmt.Text("5")), textfmt.Cell(textfmt.Text("6")),
|
|
),
|
|
textfmt.Row(
|
|
textfmt.Cell(textfmt.Text("7")), textfmt.Cell(textfmt.Text("8")), textfmt.Cell(textfmt.Text("9")),
|
|
),
|
|
),
|
|
)
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
const expect = `
|
|
<table>
|
|
<tr>
|
|
<th>1</th>
|
|
<th>-1</th>
|
|
<th>0</th>
|
|
</tr>
|
|
<tr>
|
|
<td>1</td>
|
|
<td>2</td>
|
|
<td>3</td>
|
|
</tr>
|
|
<tr>
|
|
<td>4</td>
|
|
<td>5</td>
|
|
<td>6</td>
|
|
</tr>
|
|
<tr>
|
|
<td>7</td>
|
|
<td>8</td>
|
|
<td>9</td>
|
|
</tr>
|
|
</table>
|
|
`
|
|
|
|
if "\n"+b.String() != expect {
|
|
t.Fatal("\n" + b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("header without rows", func(t *testing.T) {
|
|
doc := textfmt.Doc(
|
|
textfmt.Table(
|
|
textfmt.Header(
|
|
textfmt.Cell(textfmt.Text("foo")),
|
|
textfmt.Cell(textfmt.Text("bar")),
|
|
textfmt.Cell(textfmt.Text("baz")),
|
|
),
|
|
),
|
|
)
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
const expect = `
|
|
<table>
|
|
<tr>
|
|
<th>foo</th>
|
|
<th>bar</th>
|
|
<th>baz</th>
|
|
</tr>
|
|
</table>
|
|
`
|
|
|
|
if "\n"+b.String() != expect {
|
|
t.Fatal("\n" + b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("single row", func(t *testing.T) {
|
|
doc := textfmt.Doc(
|
|
textfmt.Table(
|
|
textfmt.Row(
|
|
textfmt.Cell(textfmt.Text("1")),
|
|
textfmt.Cell(textfmt.Text("2")),
|
|
textfmt.Cell(textfmt.Text("3")),
|
|
),
|
|
),
|
|
)
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
const expect = `
|
|
<table>
|
|
<tr>
|
|
<td>1</td>
|
|
<td>2</td>
|
|
<td>3</td>
|
|
</tr>
|
|
</table>
|
|
`
|
|
|
|
if "\n"+b.String() != expect {
|
|
t.Fatal("\n" + b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("single row with header", func(t *testing.T) {
|
|
doc := textfmt.Doc(
|
|
textfmt.Table(
|
|
textfmt.Header(
|
|
textfmt.Cell(textfmt.Text("foo")),
|
|
textfmt.Cell(textfmt.Text("bar")),
|
|
textfmt.Cell(textfmt.Text("baz")),
|
|
),
|
|
textfmt.Row(
|
|
textfmt.Cell(textfmt.Text("1")),
|
|
textfmt.Cell(textfmt.Text("2")),
|
|
textfmt.Cell(textfmt.Text("3")),
|
|
),
|
|
),
|
|
)
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
const expect = `
|
|
<table>
|
|
<tr>
|
|
<th>foo</th>
|
|
<th>bar</th>
|
|
<th>baz</th>
|
|
</tr>
|
|
<tr>
|
|
<td>1</td>
|
|
<td>2</td>
|
|
<td>3</td>
|
|
</tr>
|
|
</table>
|
|
`
|
|
|
|
if "\n"+b.String() != expect {
|
|
t.Fatal("\n" + b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("single column", func(t *testing.T) {
|
|
doc := textfmt.Doc(
|
|
textfmt.Table(
|
|
textfmt.Row(
|
|
textfmt.Cell(textfmt.Text("1")),
|
|
),
|
|
textfmt.Row(
|
|
textfmt.Cell(textfmt.Text("4")),
|
|
),
|
|
textfmt.Row(
|
|
textfmt.Cell(textfmt.Text("7")),
|
|
),
|
|
),
|
|
)
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
const expect = `
|
|
<table>
|
|
<tr>
|
|
<td>1</td>
|
|
</tr>
|
|
<tr>
|
|
<td>4</td>
|
|
</tr>
|
|
<tr>
|
|
<td>7</td>
|
|
</tr>
|
|
</table>
|
|
`
|
|
|
|
if "\n"+b.String() != expect {
|
|
t.Fatal("\n" + b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("single column with header", func(t *testing.T) {
|
|
doc := textfmt.Doc(
|
|
textfmt.Table(
|
|
textfmt.Header(
|
|
textfmt.Cell(textfmt.Text("foo")),
|
|
),
|
|
textfmt.Row(
|
|
textfmt.Cell(textfmt.Text("1")),
|
|
),
|
|
textfmt.Row(
|
|
textfmt.Cell(textfmt.Text("4")),
|
|
),
|
|
textfmt.Row(
|
|
textfmt.Cell(textfmt.Text("7")),
|
|
),
|
|
),
|
|
)
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
const expect = `
|
|
<table>
|
|
<tr>
|
|
<th>foo</th>
|
|
</tr>
|
|
<tr>
|
|
<td>1</td>
|
|
</tr>
|
|
<tr>
|
|
<td>4</td>
|
|
</tr>
|
|
<tr>
|
|
<td>7</td>
|
|
</tr>
|
|
</table>
|
|
`
|
|
|
|
if "\n"+b.String() != expect {
|
|
t.Fatal("\n" + b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("single row and single column", func(t *testing.T) {
|
|
doc := textfmt.Doc(
|
|
textfmt.Table(
|
|
textfmt.Row(
|
|
textfmt.Cell(textfmt.Text("1")),
|
|
),
|
|
),
|
|
)
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
const expect = `
|
|
<table>
|
|
<tr>
|
|
<td>1</td>
|
|
</tr>
|
|
</table>
|
|
`
|
|
|
|
if "\n"+b.String() != expect {
|
|
t.Fatal("\n" + b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("single row and single column with header", func(t *testing.T) {
|
|
doc := textfmt.Doc(
|
|
textfmt.Table(
|
|
textfmt.Header(
|
|
textfmt.Cell(textfmt.Text("foo")),
|
|
),
|
|
textfmt.Row(
|
|
textfmt.Cell(textfmt.Text("1")),
|
|
),
|
|
),
|
|
)
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
const expect = `
|
|
<table>
|
|
<tr>
|
|
<th>foo</th>
|
|
</tr>
|
|
<tr>
|
|
<td>1</td>
|
|
</tr>
|
|
</table>
|
|
`
|
|
|
|
if "\n"+b.String() != expect {
|
|
t.Fatal("\n" + b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("unequal number of row cells", func(t *testing.T) {
|
|
doc := textfmt.Doc(
|
|
textfmt.Table(
|
|
textfmt.Row(
|
|
textfmt.Cell(textfmt.Text("1")), textfmt.Cell(textfmt.Text("2")),
|
|
),
|
|
textfmt.Row(
|
|
textfmt.Cell(textfmt.Text("4")),
|
|
),
|
|
textfmt.Row(
|
|
textfmt.Cell(textfmt.Text("7")), textfmt.Cell(textfmt.Text("8")), textfmt.Cell(textfmt.Text("9")),
|
|
),
|
|
),
|
|
)
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
const expect = `
|
|
<table>
|
|
<tr>
|
|
<td>1</td>
|
|
<td>2</td>
|
|
<td></td>
|
|
</tr>
|
|
<tr>
|
|
<td>4</td>
|
|
<td></td>
|
|
<td></td>
|
|
</tr>
|
|
<tr>
|
|
<td>7</td>
|
|
<td>8</td>
|
|
<td>9</td>
|
|
</tr>
|
|
</table>
|
|
`
|
|
|
|
if "\n"+b.String() != expect {
|
|
t.Fatal("\n" + b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("unequal number of row cells with header", func(t *testing.T) {
|
|
doc := textfmt.Doc(
|
|
textfmt.Table(
|
|
textfmt.Header(
|
|
textfmt.Cell(textfmt.Text("1")), textfmt.Cell(textfmt.Text("-1")),
|
|
),
|
|
textfmt.Row(
|
|
textfmt.Cell(textfmt.Text("1")), textfmt.Cell(textfmt.Text("2")), textfmt.Cell(textfmt.Text("3")),
|
|
),
|
|
textfmt.Row(
|
|
textfmt.Cell(textfmt.Text("4")),
|
|
),
|
|
textfmt.Row(
|
|
textfmt.Cell(textfmt.Text("")), textfmt.Cell(textfmt.Text("8")), textfmt.Cell(textfmt.Text("9")),
|
|
),
|
|
),
|
|
)
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
const expect = `
|
|
<table>
|
|
<tr>
|
|
<th>1</th>
|
|
<th>-1</th>
|
|
<th></th>
|
|
</tr>
|
|
<tr>
|
|
<td>1</td>
|
|
<td>2</td>
|
|
<td>3</td>
|
|
</tr>
|
|
<tr>
|
|
<td>4</td>
|
|
<td></td>
|
|
<td></td>
|
|
</tr>
|
|
<tr>
|
|
<td></td>
|
|
<td>8</td>
|
|
<td>9</td>
|
|
</tr>
|
|
</table>
|
|
`
|
|
|
|
if "\n"+b.String() != expect {
|
|
t.Fatal("\n" + b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("all empty cells", func(t *testing.T) {
|
|
doc := textfmt.Doc(
|
|
textfmt.Wrap(
|
|
textfmt.Table(
|
|
textfmt.Header(textfmt.Cell(textfmt.Text("")), textfmt.Cell(textfmt.Text(""))),
|
|
textfmt.Row(textfmt.Cell(textfmt.Text("")), textfmt.Cell(textfmt.Text(""))),
|
|
textfmt.Row(textfmt.Cell(textfmt.Text("")), textfmt.Cell(textfmt.Text(""))),
|
|
),
|
|
72,
|
|
),
|
|
)
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
const expect = `
|
|
<table>
|
|
<tr>
|
|
<th></th>
|
|
<th></th>
|
|
</tr>
|
|
<tr>
|
|
<td></td>
|
|
<td></td>
|
|
</tr>
|
|
<tr>
|
|
<td></td>
|
|
<td></td>
|
|
</tr>
|
|
</table>
|
|
`
|
|
|
|
if "\n"+b.String() != expect {
|
|
t.Fatal("\n" + b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("wrap", func(t *testing.T) {
|
|
doc := textfmt.Doc(
|
|
textfmt.Wrap(
|
|
textfmt.Table(
|
|
textfmt.Header(
|
|
textfmt.Cell(textfmt.Text("one")),
|
|
textfmt.Cell(textfmt.Text("two")),
|
|
textfmt.Cell(textfmt.Text("three")),
|
|
),
|
|
textfmt.Row(
|
|
textfmt.Cell(textfmt.Text("Walking through the mixed forests of Brandenburg in early autumn")),
|
|
textfmt.Cell(textfmt.Text("one notices the dominant presence of Scots pine (Pinus sylvestris)")),
|
|
textfmt.Cell(textfmt.Text("interspersed with sessile oak (Quercus petraea)")),
|
|
),
|
|
textfmt.Row(
|
|
textfmt.Cell(textfmt.Text("and silver birch (Betula pendula)")),
|
|
textfmt.Cell(textfmt.Text("their canopies creating a mosaic of light")),
|
|
textfmt.Cell(textfmt.Text("and shadow on the forest floor")),
|
|
),
|
|
),
|
|
72,
|
|
),
|
|
)
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
const expect = `
|
|
<table>
|
|
<tr>
|
|
<th>one</th>
|
|
<th>two</th>
|
|
<th>three</th>
|
|
</tr>
|
|
<tr>
|
|
<td>Walking through the mixed forests of Brandenburg in
|
|
early autumn</td>
|
|
<td>one notices the dominant presence of Scots pine
|
|
(Pinus sylvestris)</td>
|
|
<td>interspersed with sessile oak (Quercus petraea)</td>
|
|
</tr>
|
|
<tr>
|
|
<td>and silver birch (Betula pendula)</td>
|
|
<td>their canopies creating a mosaic of light</td>
|
|
<td>and shadow on the forest floor</td>
|
|
</tr>
|
|
</table>
|
|
`
|
|
|
|
if "\n"+b.String() != expect {
|
|
logBytes(t, expect)
|
|
logBytes(t, "\n"+b.String())
|
|
t.Log("\n" + expect)
|
|
t.Fatal("\n" + "\n" + b.String())
|
|
}
|
|
})
|
|
})
|
|
|
|
t.Run("code", func(t *testing.T) {
|
|
t.Run("simple", func(t *testing.T) {
|
|
const code = `func() textfmt.Document {
|
|
return textfmt.Document(
|
|
textfmt.Paragraph(textfmt.Text("Hello, world!")),
|
|
)
|
|
}`
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, textfmt.Doc(textfmt.CodeBlock(code))); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if b.String() != "<pre>\n<code>"+code+"</code>\n</pre>\n" {
|
|
t.Fatal(b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("wrap has no effect", func(t *testing.T) {
|
|
const code = `func() textfmt.Document {
|
|
return textfmt.Document(
|
|
textfmt.Paragraph(textfmt.Text("Hello, world!")),
|
|
)
|
|
}`
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, textfmt.Doc(textfmt.Wrap(textfmt.CodeBlock(code), 12))); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if b.String() != "<pre>\n<code>"+code+"</code>\n</pre>\n" {
|
|
t.Fatal(b.String())
|
|
}
|
|
})
|
|
})
|
|
|
|
t.Run("syntax", func(t *testing.T) {
|
|
t.Run("symbol", func(t *testing.T) {
|
|
doc := textfmt.Doc(textfmt.Syntax(textfmt.Symbol("foo")))
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if b.String() != "<pre>\nfoo\n</pre>\n" {
|
|
t.Fatal(b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("zero or more symbols", func(t *testing.T) {
|
|
doc := textfmt.Doc(textfmt.Syntax(textfmt.ZeroOrMore(textfmt.Symbol("foo"))))
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if b.String() != "<pre>\n[foo]...\n</pre>\n" {
|
|
t.Fatal(b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("one or more", func(t *testing.T) {
|
|
doc := textfmt.Doc(textfmt.Syntax(textfmt.OneOrMore(textfmt.Symbol("foo"))))
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if b.String() != "<pre>\n<foo>...\n</pre>\n" {
|
|
t.Fatal(b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("required symbol", func(t *testing.T) {
|
|
doc := textfmt.Doc(textfmt.Syntax(textfmt.Required(textfmt.Symbol("foo"))))
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if b.String() != "<pre>\n<foo>\n</pre>\n" {
|
|
t.Fatal(b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("optional symbol", func(t *testing.T) {
|
|
doc := textfmt.Doc(textfmt.Syntax(textfmt.Optional(textfmt.Symbol("foo"))))
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if b.String() != "<pre>\n[foo]\n</pre>\n" {
|
|
t.Fatal(b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("sequence implicit", func(t *testing.T) {
|
|
doc := textfmt.Doc(
|
|
textfmt.Syntax(
|
|
textfmt.Symbol("foo"),
|
|
textfmt.Symbol("bar"),
|
|
textfmt.Symbol("baz"),
|
|
),
|
|
)
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if b.String() != "<pre>\nfoo bar baz\n</pre>\n" {
|
|
t.Fatal(b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("sequence", func(t *testing.T) {
|
|
doc := textfmt.Doc(
|
|
textfmt.Syntax(
|
|
textfmt.Sequence(
|
|
textfmt.Symbol("foo"),
|
|
textfmt.Symbol("bar"),
|
|
textfmt.Symbol("baz"),
|
|
),
|
|
),
|
|
)
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if b.String() != "<pre>\nfoo bar baz\n</pre>\n" {
|
|
t.Fatal(b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("subsequence", func(t *testing.T) {
|
|
doc := textfmt.Doc(
|
|
textfmt.Syntax(
|
|
textfmt.Symbol("corge"),
|
|
textfmt.Sequence(
|
|
textfmt.Symbol("foo"),
|
|
textfmt.Symbol("bar"),
|
|
textfmt.Symbol("baz"),
|
|
),
|
|
textfmt.Symbol("garply"),
|
|
),
|
|
)
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if b.String() != "<pre>\ncorge (foo bar baz) garply\n</pre>\n" {
|
|
t.Fatal(b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("top level choice", func(t *testing.T) {
|
|
doc := textfmt.Doc(
|
|
textfmt.Syntax(
|
|
textfmt.Choice(
|
|
textfmt.Symbol("foo"),
|
|
textfmt.Symbol("bar"),
|
|
textfmt.Symbol("baz"),
|
|
),
|
|
),
|
|
)
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if b.String() != "<pre>\nfoo\nbar\nbaz\n</pre>\n" {
|
|
t.Fatal(b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("choice", func(t *testing.T) {
|
|
doc := textfmt.Doc(
|
|
textfmt.Syntax(
|
|
textfmt.Symbol("corge"),
|
|
textfmt.Choice(
|
|
textfmt.Symbol("foo"),
|
|
textfmt.Symbol("bar"),
|
|
textfmt.Symbol("baz"),
|
|
),
|
|
textfmt.Symbol("garply"),
|
|
),
|
|
)
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if b.String() != "<pre>\ncorge (foo|bar|baz) garply\n</pre>\n" {
|
|
t.Fatal(b.String())
|
|
}
|
|
})
|
|
|
|
t.Run("example", func(t *testing.T) {
|
|
doc := textfmt.Doc(
|
|
textfmt.Syntax(
|
|
textfmt.Symbol("foo"),
|
|
textfmt.ZeroOrMore(textfmt.Symbol("options")),
|
|
textfmt.Required(textfmt.Symbol("filename")),
|
|
textfmt.ZeroOrMore(
|
|
textfmt.Choice(
|
|
textfmt.Symbol("string"),
|
|
textfmt.Symbol("number"),
|
|
),
|
|
),
|
|
),
|
|
)
|
|
|
|
var b bytes.Buffer
|
|
if err := textfmt.HTMLFragment(&b, doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if b.String() != "<pre>\nfoo [options]... <filename> [string|number]...\n</pre>\n" {
|
|
t.Fatal(b.String())
|
|
}
|
|
})
|
|
})
|
|
}
|