-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtransform_test.go
154 lines (137 loc) · 2.68 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package mermaid
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"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
noScript bool
wantBodies []string
wantScript bool
}{
{
desc: "empty",
give: "",
},
{
desc: "mermaid",
give: unlines(
"```mermaid",
"foo",
"```",
),
wantBodies: []string{"foo\n"},
wantScript: true,
},
{
desc: "mermaid and not",
give: unlines(
"Foo",
"",
"```mermaid",
"foo",
"```",
"",
"Bar",
"",
"```go",
"bar",
"",
"Baz",
"",
),
wantBodies: []string{"foo\n"},
wantScript: true,
},
{
desc: "noscript",
noScript: true,
give: unlines(
"```mermaid",
"foo",
"```",
),
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{
NoScript: tt.noScript,
}, 100),
),
)
src := []byte(tt.give)
got := p.Parse(text.NewReader(src))
var (
gotBodies []string
gotScript int
)
err := ast.Walk(got, func(node ast.Node, enter bool) (ast.WalkStatus, error) {
if !enter {
return ast.WalkContinue, nil
}
switch n := node.(type) {
case *Block:
var buff bytes.Buffer
lines := n.Lines()
for i := 0; i < lines.Len(); i++ {
line := lines.At(i)
buff.Write(line.Value(src))
}
gotBodies = append(gotBodies, buff.String())
case *ScriptBlock:
gotScript++
}
return ast.WalkContinue, nil
})
require.NoError(t, err)
assert.Equal(t, tt.wantBodies, gotBodies)
if tt.wantScript {
assert.Equal(t, 1, gotScript)
} else {
assert.Zero(t, gotScript)
}
})
}
}
func TestTransformer_RepeatedTransformations(t *testing.T) {
t.Parallel()
src := []byte(unlines(
"```mermaid",
"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)
}
var scriptCount int
err := ast.Walk(doc, func(node ast.Node, enter bool) (ast.WalkStatus, error) {
if _, ok := node.(*ScriptBlock); ok && enter {
scriptCount++
}
return ast.WalkContinue, nil
})
require.NoError(t, err)
assert.Equal(t, 1, scriptCount)
}