memos/plugin/gomark/parser/parser_test.go

96 lines
1.6 KiB
Go
Raw Normal View History

2023-12-12 23:24:02 +08:00
package parser
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/usememos/memos/plugin/gomark/ast"
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
)
func TestParser(t *testing.T) {
tests := []struct {
text string
nodes []ast.Node
}{
{
text: "Hello world!",
nodes: []ast.Node{
&ast.Paragraph{
Children: []ast.Node{
&ast.Text{
Content: "Hello world!",
},
},
},
},
},
{
text: "**Hello** world!",
nodes: []ast.Node{
&ast.Paragraph{
Children: []ast.Node{
&ast.Bold{
Symbol: "*",
Content: "Hello",
},
&ast.Text{
Content: " world!",
},
},
},
},
},
{
text: "Hello **world**!",
nodes: []ast.Node{
&ast.Paragraph{
Children: []ast.Node{
&ast.Text{
Content: "Hello ",
},
&ast.Bold{
Symbol: "*",
Content: "world",
},
&ast.Text{
Content: "!",
},
},
},
},
},
2023-12-12 23:38:43 +08:00
{
text: "Hello **world**!\n```javascript\nconsole.log(\"Hello world!\");\n```",
nodes: []ast.Node{
&ast.Paragraph{
Children: []ast.Node{
&ast.Text{
Content: "Hello ",
},
&ast.Bold{
Symbol: "*",
Content: "world",
},
&ast.Text{
Content: "!",
},
},
},
&ast.LineBreak{},
&ast.CodeBlock{
Language: "javascript",
Content: "console.log(\"Hello world!\");",
},
},
},
2023-12-12 23:24:02 +08:00
}
for _, test := range tests {
tokens := tokenizer.Tokenize(test.text)
nodes := Parse(tokens)
require.Equal(t, test.nodes, nodes)
}
}