-
Notifications
You must be signed in to change notification settings - Fork 4
/
big_test.go
220 lines (208 loc) · 4.39 KB
/
big_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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package markdown
import (
"fmt"
"strings"
"testing"
)
var rep = strings.Repeat
func repf(f func(int) string, n int) string {
out := make([]string, n)
for i := 0; i < n; i++ {
out[i] = f(i)
}
return strings.Join(out, "")
}
// Many cases here derived from cmark-gfm/test/pathological_tests.py
var bigTests = []struct {
name string
in string
out string
}{
{
"nested strong emph",
rep("*a **a ", 65000) + "b" + rep(" a** a*", 65000),
"<p>" + rep("<em>a <strong>a ", 65000) + "b" + rep(" a</strong> a</em>", 65000) + "</p>\n",
},
{
"many emph closers with no openers",
rep("a_ ", 65000),
"",
},
{
"many emph openers with no closers",
rep("_a ", 65000),
"",
},
{
"many link closers with no openers",
rep("a]", 65000),
"",
},
{
"many link openers with no closers",
rep("[a", 65000),
"",
},
{
"mismatched openers and closers",
rep("*a_ ", 50000),
"",
},
{
"openers and closers multiple of 3",
"a**b" + rep("c* ", 50000),
"",
},
{
"link openers and emph closers",
rep("[ a_", 50000),
"",
},
{
"pattern [ (]( repeated",
rep("[ (](", 80000),
"",
},
{
"pattern ![[]() repeated",
rep("![[]()", 160000),
"<p>" + rep(`![<a href=""></a>`, 160000) + "</p>\n",
},
{
"hard link/emph case",
"**x [a*b**c*](d)",
`<p>**x <a href="d">a<em>b**c</em></a></p>` + "\n",
},
{
"nested brackets",
rep("[", 50000) + "a" + rep("]", 50000),
"",
},
{
"nested block quotes",
rep("> ", 50000) + "a",
rep("<blockquote>\n", 50000) + "<p>a</p>\n" + rep("</blockquote>\n", 50000),
},
{
"deeply nested lists",
repf(func(x int) string { return rep(" ", x) + "* a\n" }, 4000),
"<ul>\n" + rep("<li>a\n<ul>\n", 4000-1) + "<li>a</li>\n" + rep("</ul>\n</li>\n", 4000-1) + "</ul>\n",
},
{
"backticks",
repf(func(x int) string { return "e" + rep("`", x) }, 5000),
"",
},
{
"backticks2",
repf(func(x int) string { return "e" + rep("`", 5000-x) }, 5000),
"",
},
{
"unclosed links A",
rep("[a](<b", 30000),
"<p>" + rep("[a](<b", 30000) + "</p>\n",
},
{
"unclosed links B",
rep("[a](b", 30000),
"",
},
{
"unclosed links C",
rep("[a](b\\#", 30000),
"<p>" + rep("[a](b#", 30000) + "</p>\n",
},
{
"unclosed <!--",
"</" + rep(" <!--", 30000),
"<p></" + rep(" <!--", 30000) + "</p>\n",
},
{
"unclosed <?",
"</" + rep(" <?", 30000),
"<p></" + rep(" <?", 30000) + "</p>\n",
},
{
"unclosed <!X",
"</" + rep(" <!X", 30000),
"<p></" + rep(" <!X", 30000) + "</p>\n",
},
{
"unclosed <![CDATA[",
"</" + rep(" <![CDATA[", 30000),
"<p></" + rep(" <![CDATA[", 30000) + "</p>\n",
},
{
"tables",
rep("abc\ndef\n|-\n", 30000),
"<p>abc</p>\n<table>\n<thead>\n<tr>\n<th>def</th>\n</tr>\n</thead>\n<tbody>\n" +
rep("<tr>\n<td>abc</td>\n</tr>\n<tr>\n<td>def</td>\n</tr>\n<tr>\n<td>-</td>\n</tr>\n", 30000-1) +
"</tbody>\n</table>\n",
},
}
func compress(s string) string {
var out []byte
start := 0
S:
for i := 0; i+4 < len(s); i++ {
c := s[i]
for j := i + 1; j < i+100 && j < len(s); j++ {
if s[j] == c {
n := 1
w := j - i
for j+w <= len(s) && s[i:i+w] == s[j:j+w] {
j += w
n++
}
if n > 2 {
out = append(out, s[start:i]...)
out = fmt.Appendf(out, "«%d:%s»", n, s[i:i+w])
start = j
i = start - 1
continue S
}
}
}
}
out = append(out, s[start:]...)
return string(out)
}
func TestBig(t *testing.T) {
if testing.Short() {
t.Skip("skipping in -short mode")
}
for _, tt := range bigTests {
t.Run(tt.name, func(t *testing.T) {
var p Parser
p.Table = true
doc := p.Parse(tt.in)
out := ToHTML(doc)
if tt.out == "" {
tt.out = "<p>" + strings.TrimSpace(tt.in) + "</p>\n"
}
if out != tt.out {
t.Fatalf("%s: ToHTML(%q):\nhave %q\nwant %q", tt.name, compress(tt.in), compress(out), compress(tt.out))
}
})
}
}
func bench(b *testing.B, text string) {
for i := 0; i < b.N; i++ {
var p Parser
_ = ToHTML(p.Parse(text))
}
b.SetBytes(int64(len(text)))
}
func BenchmarkBrackets(b *testing.B) {
bench(b, rep("[", 10000)+"a"+rep("]", 10000))
}
func BenchmarkDeepList(b *testing.B) {
bench(b, repf(func(x int) string { return rep(" ", x) + "* a\n" }, 1000))
}
func BenchmarkList(b *testing.B) {
bench(b, repf(func(x int) string { return "* a\n" }, 1000))
}