-
Notifications
You must be signed in to change notification settings - Fork 62
/
roff_test.go
343 lines (265 loc) · 9.2 KB
/
roff_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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package md2man
import (
"testing"
"github.com/russross/blackfriday/v2"
)
type TestParams struct {
extensions blackfriday.Extensions
}
func TestEmphasis(t *testing.T) {
var tests = []string{
"nothing inline\n",
".nh\n\n.PP\nnothing inline\n",
"simple *inline* test\n",
".nh\n\n.PP\nsimple \\fIinline\\fP test\n",
"*at the* beginning\n",
".nh\n\n.PP\n\\fIat the\\fP beginning\n",
"at the *end*\n",
".nh\n\n.PP\nat the \\fIend\\fP\n",
"*try two* in *one line*\n",
".nh\n\n.PP\n\\fItry two\\fP in \\fIone line\\fP\n",
"over *two\nlines* test\n",
".nh\n\n.PP\nover \\fItwo\nlines\\fP test\n",
"odd *number of* markers* here\n",
".nh\n\n.PP\nodd \\fInumber of\\fP markers* here\n",
"odd *number\nof* markers* here\n",
".nh\n\n.PP\nodd \\fInumber\nof\\fP markers* here\n",
"simple _inline_ test\n",
".nh\n\n.PP\nsimple \\fIinline\\fP test\n",
"_at the_ beginning\n",
".nh\n\n.PP\n\\fIat the\\fP beginning\n",
"at the _end_\n",
".nh\n\n.PP\nat the \\fIend\\fP\n",
"_try two_ in _one line_\n",
".nh\n\n.PP\n\\fItry two\\fP in \\fIone line\\fP\n",
"over _two\nlines_ test\n",
".nh\n\n.PP\nover \\fItwo\nlines\\fP test\n",
"odd _number of_ markers_ here\n",
".nh\n\n.PP\nodd \\fInumber of\\fP markers\\_ here\n",
"odd _number\nof_ markers_ here\n",
".nh\n\n.PP\nodd \\fInumber\nof\\fP markers\\_ here\n",
"mix of *markers_\n",
".nh\n\n.PP\nmix of *markers\\_\n",
"*What is A\\* algorithm?*\n",
".nh\n\n.PP\n\\fIWhat is A* algorithm?\\fP\n",
}
doTestsInline(t, tests)
}
func TestStrong(t *testing.T) {
var tests = []string{
"nothing inline\n",
".nh\n\n.PP\nnothing inline\n",
"simple **inline** test\n",
".nh\n\n.PP\nsimple \\fBinline\\fP test\n",
"**at the** beginning\n",
".nh\n\n.PP\n\\fBat the\\fP beginning\n",
"at the **end**\n",
".nh\n\n.PP\nat the \\fBend\\fP\n",
"**try two** in **one line**\n",
".nh\n\n.PP\n\\fBtry two\\fP in \\fBone line\\fP\n",
"over **two\nlines** test\n",
".nh\n\n.PP\nover \\fBtwo\nlines\\fP test\n",
"odd **number of** markers** here\n",
".nh\n\n.PP\nodd \\fBnumber of\\fP markers** here\n",
"odd **number\nof** markers** here\n",
".nh\n\n.PP\nodd \\fBnumber\nof\\fP markers** here\n",
"simple __inline__ test\n",
".nh\n\n.PP\nsimple \\fBinline\\fP test\n",
"__at the__ beginning\n",
".nh\n\n.PP\n\\fBat the\\fP beginning\n",
"at the __end__\n",
".nh\n\n.PP\nat the \\fBend\\fP\n",
"__try two__ in __one line__\n",
".nh\n\n.PP\n\\fBtry two\\fP in \\fBone line\\fP\n",
"over __two\nlines__ test\n",
".nh\n\n.PP\nover \\fBtwo\nlines\\fP test\n",
"odd __number of__ markers__ here\n",
".nh\n\n.PP\nodd \\fBnumber of\\fP markers\\_\\_ here\n",
"odd __number\nof__ markers__ here\n",
".nh\n\n.PP\nodd \\fBnumber\nof\\fP markers\\_\\_ here\n",
"mix of **markers__\n",
".nh\n\n.PP\nmix of **markers\\_\\_\n",
"**`/usr`** : this folder is named `usr`\n",
".nh\n\n.PP\n\\fB\\fB\\fC/usr\\fR\\fP : this folder is named \\fB\\fCusr\\fR\n",
"**`/usr`** :\n\n this folder is named `usr`\n",
".nh\n\n.PP\n\\fB\\fB\\fC/usr\\fR\\fP :\n\n.PP\nthis folder is named \\fB\\fCusr\\fR\n",
}
doTestsInline(t, tests)
}
func TestEmphasisMix(t *testing.T) {
var tests = []string{
"***triple emphasis***\n",
".nh\n\n.PP\n\\fB\\fItriple emphasis\\fP\\fP\n",
"***triple\nemphasis***\n",
".nh\n\n.PP\n\\fB\\fItriple\nemphasis\\fP\\fP\n",
"___triple emphasis___\n",
".nh\n\n.PP\n\\fB\\fItriple emphasis\\fP\\fP\n",
"***triple emphasis___\n",
".nh\n\n.PP\n***triple emphasis\\_\\_\\_\n",
"*__triple emphasis__*\n",
".nh\n\n.PP\n\\fI\\fBtriple emphasis\\fP\\fP\n",
"__*triple emphasis*__\n",
".nh\n\n.PP\n\\fB\\fItriple emphasis\\fP\\fP\n",
"**improper *nesting** is* bad\n",
".nh\n\n.PP\n\\fBimproper *nesting\\fP is* bad\n",
"*improper **nesting* is** bad\n",
".nh\n\n.PP\n*improper \\fBnesting* is\\fP bad\n",
}
doTestsInline(t, tests)
}
func TestCodeSpan(t *testing.T) {
var tests = []string{
"`source code`\n",
".nh\n\n.PP\n\\fB\\fCsource code\\fR\n",
"` source code with spaces `\n",
".nh\n\n.PP\n\\fB\\fCsource code with spaces\\fR\n",
"` source code with spaces `not here\n",
".nh\n\n.PP\n\\fB\\fCsource code with spaces\\fRnot here\n",
"a `single marker\n",
".nh\n\n.PP\na `single marker\n",
"a single multi-tick marker with ``` no text\n",
".nh\n\n.PP\na single multi\\-tick marker with ``` no text\n",
"markers with ` ` a space\n",
".nh\n\n.PP\nmarkers with a space\n",
"`source code` and a `stray\n",
".nh\n\n.PP\n\\fB\\fCsource code\\fR and a `stray\n",
"`source *with* _awkward characters_ in it`\n",
".nh\n\n.PP\n\\fB\\fCsource *with* \\_awkward characters\\_ in it\\fR\n",
"`split over\ntwo lines`\n",
".nh\n\n.PP\n\\fB\\fCsplit over\ntwo lines\\fR\n",
"```multiple ticks``` for the marker\n",
".nh\n\n.PP\n\\fB\\fCmultiple ticks\\fR for the marker\n",
"```multiple ticks `with` ticks inside```\n",
".nh\n\n.PP\n\\fB\\fCmultiple ticks `with` ticks inside\\fR\n",
}
doTestsInline(t, tests)
}
func TestListLists(t *testing.T) {
var tests = []string{
"\n\n**[grpc]**\n: Section for gRPC socket listener settings. Contains three properties:\n - **address** (Default: \"/run/containerd/containerd.sock\")\n - **uid** (Default: 0)\n - **gid** (Default: 0)",
".nh\n\n.TP\n\\fB[grpc]\\fP\nSection for gRPC socket listener settings. Contains three properties:\n.RS\n.IP \\(bu 2\n\\fBaddress\\fP (Default: \"/run/containerd/containerd.sock\")\n.IP \\(bu 2\n\\fBuid\\fP (Default: 0)\n.IP \\(bu 2\n\\fBgid\\fP (Default: 0)\n\n.RE\n\n",
}
doTestsParam(t, tests, TestParams{blackfriday.DefinitionLists})
}
func TestLineBreak(t *testing.T) {
var tests = []string{
"this line \nhas a break\n",
".nh\n\n.PP\nthis line\n.br\nhas a break\n",
"this line \ndoes not\n",
".nh\n\n.PP\nthis line\ndoes not\n",
"this line\\\ndoes not\n",
".nh\n\n.PP\nthis line\\\\\ndoes not\n",
"this line\\ \ndoes not\n",
".nh\n\n.PP\nthis line\\\\\ndoes not\n",
"this has an \nextra space\n",
".nh\n\n.PP\nthis has an\n.br\nextra space\n",
}
doTestsInline(t, tests)
tests = []string{
"this line \nhas a break\n",
".nh\n\n.PP\nthis line\n.br\nhas a break\n",
"this line \ndoes not\n",
".nh\n\n.PP\nthis line\ndoes not\n",
"this line\\\nhas a break\n",
".nh\n\n.PP\nthis line\n.br\nhas a break\n",
"this line\\ \ndoes not\n",
".nh\n\n.PP\nthis line\\\\\ndoes not\n",
"this has an \nextra space\n",
".nh\n\n.PP\nthis has an\n.br\nextra space\n",
}
doTestsInlineParam(t, tests, TestParams{
extensions: blackfriday.BackslashLineBreak})
}
func TestTable(t *testing.T) {
var tests = []string{
`
| Animal | Color |
| --------------| --- |
| elephant | Gray. The elephant is very gray. |
| wombat | No idea. |
| zebra | Sometimes black and sometimes white, depending on the stripe. |
| robin | red. |
`,
`.nh
.TS
allbox;
l l
l l .
\fB\fCAnimal\fR \fB\fCColor\fR
elephant T{
Gray. The elephant is very gray.
T}
wombat No idea.
zebra T{
Sometimes black and sometimes white, depending on the stripe.
T}
robin red.
.TE
`,
}
doTestsInlineParam(t, tests, TestParams{blackfriday.Tables})
}
func TestLinks(t *testing.T) {
var tests = []string{
"See [docs](https://docs.docker.com/) for\nmore",
".nh\n\n.PP\nSee docs\n\\[la]https://docs.docker.com/\\[ra] for\nmore\n",
}
doTestsInline(t, tests)
}
func TestEscapeCharacters(t *testing.T) {
var tests = []string{
"Test-one_two&three\\four~five",
".nh\n\n.PP\nTest\\-one\\_two\\&three\\\\four~five\n",
}
doTestsInline(t, tests)
}
func execRecoverableTestSuite(t *testing.T, tests []string, params TestParams, suite func(candidate *string)) {
// Catch and report panics. This is useful when running 'go test -v' on
// the integration server. When developing, though, crash dump is often
// preferable, so recovery can be easily turned off with doRecover = false.
var candidate string
const doRecover = true
if doRecover {
defer func() {
if err := recover(); err != nil {
t.Errorf("\npanic while processing [%#v]: %s\n", candidate, err)
}
}()
}
suite(&candidate)
}
func runMarkdown(input string, params TestParams) string {
renderer := NewRoffRenderer()
return string(blackfriday.Run([]byte(input), blackfriday.WithRenderer(renderer),
blackfriday.WithExtensions(params.extensions)))
}
func doTestsParam(t *testing.T, tests []string, params TestParams) {
execRecoverableTestSuite(t, tests, params, func(candidate *string) {
for i := 0; i+1 < len(tests); i += 2 {
input := tests[i]
*candidate = input
expected := tests[i+1]
actual := runMarkdown(*candidate, params)
if actual != expected {
t.Errorf("\nInput [%#v]\nExpected[%#v]\nActual [%#v]",
*candidate, expected, actual)
}
// now test every substring to stress test bounds checking
if !testing.Short() {
for start := 0; start < len(input); start++ {
for end := start + 1; end <= len(input); end++ {
*candidate = input[start:end]
runMarkdown(*candidate, params)
}
}
}
}
})
}
func doTestsInline(t *testing.T, tests []string) {
doTestsInlineParam(t, tests, TestParams{})
}
func doTestsInlineParam(t *testing.T, tests []string, params TestParams) {
params.extensions |= blackfriday.Strikethrough
doTestsParam(t, tests, params)
}