105 lines
3.1 KiB
Go
105 lines
3.1 KiB
Go
package gemtext_test
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"tildegit.org/tjp/gus/gemtext"
|
|
)
|
|
|
|
func TestParse(t *testing.T) {
|
|
docBytes := []byte(`
|
|
# top-level header line
|
|
|
|
## subtitle
|
|
|
|
This is some non-blank regular text.
|
|
|
|
* an
|
|
* unordered
|
|
* list
|
|
|
|
=> gemini://google.com/ as if
|
|
|
|
> this is a quote
|
|
> -tjp
|
|
|
|
`[1:] + "```pre-formatted code\ndoc := gemtext.Parse(req.Body)\n```ignored closing alt-text\n")
|
|
|
|
assertEmptyLine := func(t *testing.T, line gemtext.Line) {
|
|
assert.Equal(t, gemtext.LineTypeText, line.Type())
|
|
assert.Equal(t, "\n", string(line.Raw()))
|
|
}
|
|
|
|
doc, err := gemtext.Parse(bytes.NewBuffer(docBytes))
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, 18, len(doc))
|
|
|
|
assert.Equal(t, gemtext.LineTypeHeading1, doc[0].Type())
|
|
assert.Equal(t, "# top-level header line\n", string(doc[0].Raw()))
|
|
assert.Equal(t, "top-level header line", doc[0].(gemtext.HeadingLine).Body())
|
|
|
|
assertEmptyLine(t, doc[1])
|
|
|
|
assert.Equal(t, gemtext.LineTypeHeading2, doc[2].Type())
|
|
assert.Equal(t, "## subtitle\n", string(doc[2].Raw()))
|
|
assert.Equal(t, "subtitle", doc[2].(gemtext.HeadingLine).Body())
|
|
|
|
assertEmptyLine(t, doc[3])
|
|
|
|
assert.Equal(t, gemtext.LineTypeText, doc[4].Type())
|
|
assert.Equal(t, "This is some non-blank regular text.\n", string(doc[4].Raw()))
|
|
|
|
assertEmptyLine(t, doc[5])
|
|
|
|
assert.Equal(t, gemtext.LineTypeListItem, doc[6].Type())
|
|
assert.Equal(t, "an", doc[6].(gemtext.ListItemLine).Body())
|
|
|
|
assert.Equal(t, gemtext.LineTypeListItem, doc[7].Type())
|
|
assert.Equal(t, "unordered", doc[7].(gemtext.ListItemLine).Body())
|
|
|
|
assert.Equal(t, gemtext.LineTypeListItem, doc[8].Type())
|
|
assert.Equal(t, "list", doc[8].(gemtext.ListItemLine).Body())
|
|
|
|
assertEmptyLine(t, doc[9])
|
|
|
|
assert.Equal(t, gemtext.LineTypeLink, doc[10].Type())
|
|
assert.Equal(t, "=> gemini://google.com/ as if\n", string(doc[10].Raw()))
|
|
assert.Equal(t, "gemini://google.com/", doc[10].(gemtext.LinkLine).URL())
|
|
assert.Equal(t, "as if", doc[10].(gemtext.LinkLine).Label())
|
|
|
|
assertEmptyLine(t, doc[11])
|
|
|
|
assert.Equal(t, gemtext.LineTypeQuote, doc[12].Type())
|
|
assert.Equal(t, "> this is a quote\n", string(doc[12].Raw()))
|
|
assert.Equal(t, " this is a quote", doc[12].(gemtext.QuoteLine).Body())
|
|
|
|
assert.Equal(t, gemtext.LineTypeQuote, doc[13].Type())
|
|
assert.Equal(t, "> -tjp\n", string(doc[13].Raw()))
|
|
assert.Equal(t, " -tjp", doc[13].(gemtext.QuoteLine).Body())
|
|
|
|
assertEmptyLine(t, doc[14])
|
|
|
|
assert.Equal(t, gemtext.LineTypePreformatToggle, doc[15].Type())
|
|
assert.Equal(t, "```pre-formatted code\n", string(doc[15].Raw()))
|
|
assert.Equal(t, "pre-formatted code", doc[15].(gemtext.PreformatToggleLine).AltText())
|
|
|
|
assert.Equal(t, gemtext.LineTypePreformattedText, doc[16].Type())
|
|
assert.Equal(t, "doc := gemtext.Parse(req.Body)\n", string(doc[16].Raw()))
|
|
|
|
assert.Equal(t, gemtext.LineTypePreformatToggle, doc[17].Type())
|
|
assert.Equal(t, "```ignored closing alt-text\n", string(doc[17].Raw()))
|
|
assert.Equal(t, "", doc[17].(gemtext.PreformatToggleLine).AltText())
|
|
|
|
// ensure we can rebuild the original doc from all the line.Raw()s
|
|
buf := &bytes.Buffer{}
|
|
for _, line := range doc {
|
|
_, _ = buf.Write(line.Raw())
|
|
}
|
|
assert.Equal(t, string(docBytes), buf.String())
|
|
}
|