-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
59 lines (55 loc) · 1.68 KB
/
parser_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package media
import (
"github.com/stretchr/testify/assert"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/text"
"testing"
)
func TestParse(t *testing.T) {
cases := []struct {
str string
_type ast.NodeKind
}{
{"!v[video](https://example.org/test.mp4)", KindMedia},
{"!a[audio](https://example.org/test.mp3)", KindMedia},
{"!p[picture](https://example.org/test.png)", KindMedia},
{"!v[](https://example.org/test.mp4)", KindMedia}, //no alt text, still valid
{"!v[test]()", ast.KindText}, //no url
{"!x[test](1)", ast.KindText}, //wrong type
}
md := goldmark.New(goldmark.WithExtensions(Extension{}))
for _, c := range cases {
src := []byte(c.str)
doc := md.Parser().Parse(text.NewReader(src))
n := doc.FirstChild().FirstChild()
assert.Equal(t, c._type, n.Kind())
doc.Dump(src, 0)
}
}
func TestParsePreserve(t *testing.T) {
cases := []struct {
str string
pre string
after string
}{
{"pre!v[Video](h)after", "pre", "after"},
{"pre!v[](h)after", "pre", "after"}, // no alt text, valid
{"pre!v[]()after", "pre!v", "after"}, // no url, should be handled as Link
{"pre!x[1](1)after", "pre!x", "after"}, // invalid, should be handled as Link
}
md := goldmark.New(goldmark.WithExtensions(Extension{}))
for _, c := range cases {
src := []byte(c.str)
doc := md.Parser().Parse(text.NewReader(src))
p := doc.FirstChild()
c1 := p.FirstChild()
c2 := p.LastChild()
t1 := string(c1.Text(src))
t2 := string(c2.Text(src))
assert.Equal(t, ast.KindText, c1.Kind())
assert.Equal(t, ast.KindText, c2.Kind())
assert.Equal(t, c.pre, t1)
assert.Equal(t, c.after, t2)
}
}