-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform_test.go
108 lines (93 loc) · 1.77 KB
/
transform_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package pikchr
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
)
func TestTransformer(t *testing.T) {
t.Parallel()
tests := []struct {
desc string
give string
wantBodies []string
}{
{
desc: "empty",
give: "",
},
{
desc: "pikchr",
give: unlines(
"```pikchr",
"foo",
"```",
),
wantBodies: []string{"foo\n"},
},
{
desc: "pikchr and not",
give: unlines(
"Foo",
"",
"```pikchr",
"foo",
"```",
"",
"Bar",
"",
"```go",
"bar",
"",
"Baz",
"",
),
wantBodies: []string{"foo\n"},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.desc, func(t *testing.T) {
t.Parallel()
p := goldmark.New().Parser()
p.AddOptions(
parser.WithASTTransformers(
util.Prioritized(&Transformer{}, 100),
),
)
src := []byte(tt.give)
got := p.Parse(text.NewReader(src))
var gotBodies []string
_ = ast.Walk(got, func(node ast.Node, enter bool) (ast.WalkStatus, error) {
if !enter {
return ast.WalkContinue, nil
}
switch n := node.(type) {
case *Block:
gotBodies = append(gotBodies, string(getLines(src, n)))
}
return ast.WalkContinue, nil
})
assert.Equal(t, tt.wantBodies, gotBodies)
})
}
}
func TestTransformer_RepeatedTransformations(t *testing.T) {
t.Parallel()
src := []byte(unlines(
"```pikchr",
"foo",
"```",
))
r := text.NewReader(src)
pctx := parser.NewContext()
doc := goldmark.New().Parser().
Parse(r, parser.WithContext(pctx)).(*ast.Document)
var trans Transformer
for i := 0; i < 10; i++ {
trans.Transform(doc, r, pctx)
}
}