2023-05-23 21:11:01 +08:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-09-17 22:55:13 +08:00
|
|
|
|
2023-12-12 23:24:02 +08:00
|
|
|
"github.com/usememos/memos/plugin/gomark/ast"
|
2023-05-23 21:11:01 +08:00
|
|
|
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestParagraphParser(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
text string
|
2023-12-12 23:24:02 +08:00
|
|
|
paragraph ast.Node
|
2023-05-23 21:11:01 +08:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
text: "",
|
|
|
|
paragraph: nil,
|
|
|
|
},
|
2023-12-14 00:04:20 +08:00
|
|
|
{
|
|
|
|
text: "\n",
|
|
|
|
paragraph: nil,
|
|
|
|
},
|
2023-05-23 21:11:01 +08:00
|
|
|
{
|
2023-12-12 23:24:02 +08:00
|
|
|
text: "Hello world!",
|
|
|
|
paragraph: &ast.Paragraph{
|
|
|
|
Children: []ast.Node{
|
|
|
|
&ast.Text{
|
|
|
|
Content: "Hello world!",
|
2023-05-23 21:11:01 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-12-14 00:04:20 +08:00
|
|
|
{
|
|
|
|
text: "Hello world!\n",
|
|
|
|
paragraph: &ast.Paragraph{
|
|
|
|
Children: []ast.Node{
|
|
|
|
&ast.Text{
|
|
|
|
Content: "Hello world!",
|
|
|
|
},
|
|
|
|
&ast.LineBreak{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: "Hello world!\n\nNew paragraph.",
|
|
|
|
paragraph: &ast.Paragraph{
|
|
|
|
Children: []ast.Node{
|
|
|
|
&ast.Text{
|
|
|
|
Content: "Hello world!",
|
|
|
|
},
|
|
|
|
&ast.LineBreak{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-05-23 21:11:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
tokens := tokenizer.Tokenize(test.text)
|
2023-12-13 23:50:05 +08:00
|
|
|
node, _ := NewParagraphParser().Parse(tokens)
|
|
|
|
require.Equal(t, StringifyNodes([]ast.Node{test.paragraph}), StringifyNodes([]ast.Node{node}))
|
2023-05-23 21:11:01 +08:00
|
|
|
}
|
|
|
|
}
|