start writing ParsePage, fails the tests

This commit is contained in:
Nico 2020-11-12 13:18:44 +00:00
parent 4f40ff17b6
commit b6e1c2ac1e
2 changed files with 56 additions and 3 deletions

View File

@ -16,7 +16,6 @@ const (
PREFORMATTED_TEXT
HEADING
LIST
LISTMARKER // Marks the start/end of a list. Not a real gemtext object but useful for conversion.
QUOTE
)
@ -97,4 +96,19 @@ func ParseLine(line string, isPreformatted bool) (GemtextObject, error) {
}
// ParsePage takes a string containing the contents of a gemtext page and returns a GemtextPage.
func ParsePage(p string) {}
func ParsePage(p string) (GemtextPage, error) { // TODO fix preformatted text
preformatted := false
lines := strings.Split(p, "\n")
var page GemtextPage
for _, line := range lines {
l, err := ParseLine(line, preformatted)
if err != nil {
return GemtextPage{}, err
}
if l.Type == PREFORMATTED_TOGGLE {
preformatted = !preformatted
}
page = append(page, l)
}
return page, nil
}

View File

@ -1,12 +1,15 @@
package gemtext
import "testing"
import (
"testing"
)
type LineCase struct {
Str string
Preformatted bool
Want GemtextObject
}
// These tests suck.
func TestParseLine(t *testing.T) {
cases := []LineCase{
@ -146,6 +149,14 @@ func TestParseLine(t *testing.T) {
Text: "link",
Path: "https://example.com"},
},
LineCase{
Str: "\t",
Preformatted: true,
Want: GemtextObject{
Type: PREFORMATTED_TEXT,
Literal: "\t",
Text: "\t"},
},
LineCase{
Str: "=>https://example.com link",
Preformatted: false,
@ -179,3 +190,31 @@ func TestParseLine(t *testing.T) {
}
}
}
// We test each line in ParseLine()
// so all we have to test for ParsePage() is blank lines and preformatting.
func TestParsePage(t *testing.T) {
input := "# Document\n\n* List Item 1\n* List Item 2\n```preformatted stuff\nthis should be preformatted.\n=> https://example.com Not a link\n```\n* A single list item\n```\none line of pre\n```\n```\n```" // TODO make this readable
want := GemtextPage{
GemtextObject{Type: HEADING, Literal: "# Document", Level: 1, Text: "Document"},
GemtextObject{Type: TEXT, Literal: "", Text: ""},
GemtextObject{Type: LIST, Literal: "* List Item 1", Text: "List Item 1"},
GemtextObject{Type: LIST, Literal: "* List Item 2", Text: "List Item 2"},
GemtextObject{Type: PREFORMATTED_TOGGLE, Literal:"```preformatted stuff", Text:"preformatted stuff"},
GemtextObject{Type: PREFORMATTED_TEXT, Literal: "this should be preformatted.", Text:"this should be preformatted."},
GemtextObject{Type: PREFORMATTED_TEXT, Literal: "=> https://example.com Not a link", Text: "=> https://example.com Not a link"},
GemtextObject{Type: PREFORMATTED_TOGGLE, Literal:"```"},
GemtextObject{Type: LIST, Literal: "* A single list item", Text: "A single list item"},
GemtextObject{Type: PREFORMATTED_TOGGLE, Literal:"```"},
GemtextObject{Type: PREFORMATTED_TEXT, Literal:"one line of pre", Text:"one line of pre"},
GemtextObject{Type: PREFORMATTED_TOGGLE, Literal:"```"},
GemtextObject{Type: PREFORMATTED_TOGGLE, Literal:"```"},
GemtextObject{Type: PREFORMATTED_TOGGLE, Literal:"```"},
}
output, _ := ParsePage(input)
for i, _ := range output {
if output[i] != want[i] {
t.Errorf("Wanted: %#v, got: %#v on line %d", want[i], output[i], i)
}
}
}