package gemtext_test import ( "bytes" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "tildegit.org/tjp/sliderule/gemini/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 =: spartan://foo.bar/baz this should be a spartan prompt > 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, 20, 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.LineTypePrompt, doc[12].Type()) assert.Equal(t, "=: spartan://foo.bar/baz this should be a spartan prompt\n", string(doc[12].Raw())) assert.Equal(t, "spartan://foo.bar/baz", doc[12].(gemtext.PromptLine).URL()) assert.Equal(t, "this should be a spartan prompt", doc[12].(gemtext.PromptLine).Label()) assertEmptyLine(t, doc[13]) assert.Equal(t, gemtext.LineTypeQuote, doc[14].Type()) assert.Equal(t, "> this is a quote\n", string(doc[14].Raw())) assert.Equal(t, " this is a quote", doc[14].(gemtext.QuoteLine).Body()) assert.Equal(t, gemtext.LineTypeQuote, doc[15].Type()) assert.Equal(t, "> -tjp\n", string(doc[15].Raw())) assert.Equal(t, " -tjp", doc[15].(gemtext.QuoteLine).Body()) assertEmptyLine(t, doc[16]) assert.Equal(t, gemtext.LineTypePreformatToggle, doc[17].Type()) assert.Equal(t, "```pre-formatted code\n", string(doc[17].Raw())) assert.Equal(t, "pre-formatted code", doc[17].(gemtext.PreformatToggleLine).AltText()) assert.Equal(t, gemtext.LineTypePreformattedText, doc[18].Type()) assert.Equal(t, "doc := gemtext.Parse(req.Body)\n", string(doc[18].Raw())) assert.Equal(t, gemtext.LineTypePreformatToggle, doc[19].Type()) assert.Equal(t, "```ignored closing alt-text\n", string(doc[19].Raw())) assert.Equal(t, "", doc[19].(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()) }