-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtransform_test.go
137 lines (120 loc) · 2.78 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
package toc
import (
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"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()
src := []byte(strings.Join([]string{
"# Foo",
"## Bar",
"# Baz",
"### Qux",
"## Quux",
}, "\n") + "\n")
tests := []struct {
desc string
giveTitle string
wantTitle string
}{
{
desc: "default title",
wantTitle: _defaultTitle,
},
{
desc: "custom title",
giveTitle: "Contents",
wantTitle: "Contents",
},
}
for _, tt := range tests {
tt := tt // for t.Parallel
t.Run(tt.desc, func(t *testing.T) {
t.Parallel()
doc := parser.NewParser(
parser.WithInlineParsers(parser.DefaultInlineParsers()...),
parser.WithBlockParsers(parser.DefaultBlockParsers()...),
parser.WithAutoHeadingID(),
parser.WithASTTransformers(
util.Prioritized(&Transformer{
Title: tt.giveTitle,
}, 100),
),
).Parse(text.NewReader(src))
heading, ok := doc.FirstChild().(*ast.Heading)
require.True(t, ok, "first child must be a heading, got %T", doc.FirstChild())
assert.Equal(t, tt.wantTitle, string(heading.Text(src)), "title mismatch")
})
}
}
// From: https://github.com/abhinav/goldmark-toc/issues/61
func TestTransformerWithTitleDepth(t *testing.T) {
t.Parallel()
src := []byte(strings.Join([]string{
"# Hey",
"## Now",
"# Then",
"### There",
"## Now",
}, "\n") + "\n")
type testCase struct {
desc string
giveDepth int
wantDepth int
}
tests := []testCase{
{
desc: "default",
wantDepth: _defaultTitleDepth,
},
{
desc: "< 1",
giveDepth: -1,
wantDepth: 1,
},
{
desc: "> 6",
giveDepth: 7,
wantDepth: 6,
},
{
desc: "absurd",
giveDepth: 130931,
wantDepth: 6,
},
}
for i := _defaultTitleDepth; i <= _maxTitleDepth; i++ {
tests = append(tests, testCase{
desc: fmt.Sprintf("valid/%d", i),
giveDepth: i,
wantDepth: i,
})
}
for _, tt := range tests {
tt := tt // for t.Parallel
t.Run(tt.desc, func(t *testing.T) {
t.Parallel()
doc := parser.NewParser(
parser.WithInlineParsers(parser.DefaultInlineParsers()...),
parser.WithBlockParsers(parser.DefaultBlockParsers()...),
parser.WithAutoHeadingID(),
parser.WithASTTransformers(
util.Prioritized(&Transformer{
TitleDepth: tt.giveDepth,
}, 100),
),
).Parse(text.NewReader(src))
// Should definitely still be a heading
heading, ok := doc.FirstChild().(*ast.Heading)
require.True(t, ok, "first child must be a heading, got %T", doc.FirstChild())
assert.Equal(t, tt.wantDepth, heading.Level, "level mismatch")
})
}
}