Skip to content

Commit

Permalink
Add tests. More parse errors
Browse files Browse the repository at this point in the history
  • Loading branch information
revelaction committed Oct 11, 2023
1 parent 170ceaa commit 15178a0
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 14 deletions.
37 changes: 23 additions & 14 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,17 @@ func (ns *Notes) addFieldName(fieldName string) error {
// after first note:
if ns.numNotes() > 0 {
if "" != fieldName {
return fmt.Errorf("Invalid presence of fields (%q) in note %d.", fieldName, ns.numNotes()+1)
return fmt.Errorf("Invalid presence of fields (%q) in note %d", fieldName, ns.numNotes()+1)
} else {
return nil
}
}

// first note:
if "" == fieldName {
return fmt.Errorf("Missing fields in note %d", ns.numNotes()+1)
}

for _, fn := range ns.fieldNames {
if fn == fieldName {
return fmt.Errorf("Note %d: Field %q already exist", ns.numNotes()+1, fieldName)
Expand All @@ -138,6 +142,7 @@ func (p *Parser) Parse(markdown []byte) (*Notes, error) {
root := p.mdp.Parser().Parse(text.NewReader(markdown))

var fieldBuf bytes.Buffer
var insideNoteField bool = false

nt := newNote()
notes := newNotes()
Expand All @@ -147,17 +152,20 @@ func (p *Parser) Parse(markdown []byte) (*Notes, error) {

if !entering {

if isFieldEnd(n) {
if isFieldEnd(n, insideNoteField) {
nt.addField(Field{Html: fieldBuf.String()})
insideNoteField = false
}

if isNoteEnd(n) {
err = notes.addNote(nt)
if err != nil {
return ast.WalkStop, err
}
insideNoteField = false
}


return ast.WalkSkipChildren, nil
}

Expand All @@ -179,6 +187,7 @@ func (p *Parser) Parse(markdown []byte) (*Notes, error) {
noteNum := len(notes.Notes) + 1
nt.addId(strconv.Itoa(noteNum))


return ast.WalkSkipChildren, nil
}

Expand All @@ -190,13 +199,16 @@ func (p *Parser) Parse(markdown []byte) (*Notes, error) {
}

fieldBuf = bytes.Buffer{}

insideNoteField = true

return ast.WalkSkipChildren, nil
}

// Render the node contents
if err = p.mdp.Renderer().Render(&fieldBuf, markdown, n); err != nil {
return ast.WalkStop, fmt.Errorf("Render error %v", err)
}
// Render the node contents
if err = p.mdp.Renderer().Render(&fieldBuf, markdown, n); err != nil {
return ast.WalkStop, fmt.Errorf("Render error %v", err)
}

return ast.WalkSkipChildren, nil
})
Expand Down Expand Up @@ -273,8 +285,12 @@ func isFieldStart(n ast.Node) bool {
// the field end is determined by either:
// - a following (sibling) h2, but no coming fron preceding h1
// - the following is Document final (not entering)
func isFieldEnd(n ast.Node) bool {
func isFieldEnd(n ast.Node, inside bool) bool {

if !inside{
return false
}

_, ok := n.(*ast.Document)
if ok {
return true
Expand All @@ -284,13 +300,6 @@ func isFieldEnd(n ast.Node) bool {
return false
}

h, ok := n.(*ast.Heading)
if ok {
if 1 == h.Level {
return false
}
}

switch v := n.NextSibling().(type) {
case *ast.Document:
return true
Expand Down
45 changes: 45 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ second note has 3 fields
}
}

// Text after H1 before H2 are allowed
func TestParseContentBetweenH1AndH2(t *testing.T) {

withTextBetween := []byte(`# note1 tag1
Expand All @@ -148,8 +149,52 @@ Text`)
md := mankidown.NewParser()
_, err := md.Parse(withTextBetween)

//t.Logf("Error is %q", err)
if err != nil {
t.Errorf("\ngot %s\nwant nil", err)
}
}

// first note should define all note fields, shoudl no have empty H2
func TestParseNoFieldInFirstNoteH2(t *testing.T) {

withoutFields := []byte(`# note1 tag1
##
First note has two fields, Field1 and Field2
##
text
# note2 tag2
##
text
##
Text`)

md := mankidown.NewParser()
_, err := md.Parse(withoutFields)

t.Logf("Error is %q", err)
if err == nil {
t.Errorf("\ngot %s\nwant Error", err)
}
}

func TestNoNtes(t *testing.T) {

withoutNotes := []byte(`# tag1 tag2
`)

md := mankidown.NewParser()
_, err := md.Parse(withoutNotes)

//t.Logf("Error is %q", err)
if err != nil {
t.Errorf("\ngot %s\nwant nil", err)
}
}

0 comments on commit 15178a0

Please sign in to comment.