-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlineclass_scan_test.go
More file actions
478 lines (422 loc) · 18.2 KB
/
Copy pathlineclass_scan_test.go
File metadata and controls
478 lines (422 loc) · 18.2 KB
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
package lint
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
// These tests exhaustively pin the branches of the flat-classifier byte
// scanners. They are pure functions, so a table covers every arm —
// including the ones no corpus fixture happens to exercise (HTML
// declarations, over-indented fences, malformed list markers).
func TestLeadingSpacesAndIndent(t *testing.T) {
assert.Equal(t, 0, leadingSpaces([]byte("abc")))
assert.Equal(t, 2, leadingSpaces([]byte(" abc")))
assert.Equal(t, 3, leadingSpaces([]byte(" ")))
assert.Equal(t, 0, leadingSpaces([]byte("\tabc")))
assert.Equal(t, 0, indentColumns([]byte(""), 0))
assert.Equal(t, 0, indentColumns([]byte("x"), 0))
assert.Equal(t, 1, indentColumns([]byte(" x"), 0))
assert.Equal(t, 4, indentColumns([]byte("\tx"), 0))
assert.Equal(t, 4, indentColumns([]byte(" \tx"), 0)) // 2 spaces then tab → next stop
assert.Equal(t, 4, indentColumns([]byte(" x"), 0))
assert.Equal(t, 5, indentColumns([]byte(" "), 0)) // all spaces, no content
// startCol shifts the tab stop: a tab at absolute column 2 advances to
// column 4 (2 columns), not a full 4.
assert.Equal(t, 2, indentColumns([]byte("\tx"), 2))
assert.Equal(t, 3, indentColumns([]byte("\t x"), 2)) // tab→col4 (2) + space (1)
}
func TestBlankScanners(t *testing.T) {
assert.True(t, isBlankBytes([]byte("")))
assert.True(t, isBlankBytes([]byte(" \t ")))
assert.False(t, isBlankBytes([]byte("x")))
assert.False(t, isBlankBytes([]byte(" x")))
assert.True(t, isBlankFrom([]byte("abc"), 3))
assert.True(t, isBlankFrom([]byte("ab "), 2))
assert.False(t, isBlankFrom([]byte("ab c"), 2))
}
func TestHeadingScanners(t *testing.T) {
assert.True(t, isATXHeading([]byte("# h")))
assert.True(t, isATXHeading([]byte("###### h")))
assert.True(t, isATXHeading([]byte("#"))) // EOL after marker
assert.True(t, isATXHeading([]byte("#\tt"))) // tab after marker
assert.False(t, isATXHeading([]byte("####### h"))) // 7 hashes → not ATX
assert.False(t, isATXHeading([]byte("#h"))) // no space
assert.False(t, isATXHeading([]byte("text")))
assert.True(t, lcIsSetextUnderline([]byte("===")))
assert.True(t, lcIsSetextUnderline([]byte("---")))
assert.True(t, lcIsSetextUnderline([]byte("=== "))) // trailing spaces
assert.False(t, lcIsSetextUnderline([]byte("")))
assert.False(t, lcIsSetextUnderline([]byte("= ="))) // gap then more
assert.False(t, lcIsSetextUnderline([]byte("abc")))
}
func TestFenceScanners(t *testing.T) {
ch, n, info, ok := detectFenceOpen([]byte("```"))
assert.True(t, ok)
assert.Equal(t, byte('`'), ch)
assert.Equal(t, 3, n)
assert.False(t, info)
_, _, info, ok = detectFenceOpen([]byte("```go"))
assert.True(t, ok)
assert.True(t, info)
_, _, _, ok = detectFenceOpen([]byte("``"))
assert.False(t, ok, "fewer than three backticks")
_, _, _, ok = detectFenceOpen([]byte("```a`b"))
assert.False(t, ok, "backtick in a backtick-fence info string")
_, _, info, ok = detectFenceOpen([]byte("~~~a`b"))
assert.True(t, ok, "tilde fence allows a backtick in its info")
assert.True(t, info)
_, _, _, ok = detectFenceOpen([]byte(" ```"))
assert.False(t, ok, "four-space indent is code, not a fence")
_, _, _, ok = detectFenceOpen([]byte("text"))
assert.False(t, ok)
assert.True(t, isFenceClose([]byte("```"), '`', 3))
assert.True(t, isFenceClose([]byte("````"), '`', 3), "longer than the open fence")
assert.True(t, isFenceClose([]byte("``` "), '`', 3), "trailing whitespace allowed")
assert.False(t, isFenceClose([]byte("``"), '`', 3), "shorter than the open fence")
assert.False(t, isFenceClose([]byte("```x"), '`', 3), "non-space after the fence")
assert.False(t, isFenceClose([]byte(" ```"), '`', 3), "over-indented")
}
func TestHTMLScanners(t *testing.T) {
end, kind := htmlBlockEnd([]byte("<!-- x"))
assert.Equal(t, htmlMarker, kind)
assert.Equal(t, "-->", string(end))
end, kind = htmlBlockEnd([]byte("<![CDATA[x"))
assert.Equal(t, htmlMarker, kind)
assert.Equal(t, "]]>", string(end))
end, kind = htmlBlockEnd([]byte("<?php"))
assert.Equal(t, htmlMarker, kind)
assert.Equal(t, "?>", string(end))
end, kind = htmlBlockEnd([]byte("<!DOCTYPE html>"))
assert.Equal(t, htmlMarker, kind)
assert.Equal(t, ">", string(end))
_, kind = htmlBlockEnd([]byte("<pre>")) // type-1 raw block
assert.Equal(t, htmlRaw, kind)
_, kind = htmlBlockEnd([]byte("<SCRIPT type=x")) // case-insensitive
assert.Equal(t, htmlRaw, kind)
_, kind = htmlBlockEnd([]byte("<div>")) // type-6 block tag
assert.Equal(t, htmlTag6, kind)
_, kind = htmlBlockEnd([]byte("</Details>")) // closing tag, case-insensitive
assert.Equal(t, htmlTag6, kind)
_, kind = htmlBlockEnd([]byte(`<img src="x.png">`)) // type-7 complete tag
assert.Equal(t, htmlTag7, kind)
_, kind = htmlBlockEnd([]byte(" <!-- x")) // ≤3 indent still opens
assert.Equal(t, htmlMarker, kind)
_, kind = htmlBlockEnd([]byte(" <!-- x")) // 4-space indent does not
assert.Equal(t, htmlBlockNone, kind)
_, kind = htmlBlockEnd([]byte("<!5 not a decl"))
assert.Equal(t, htmlBlockNone, kind)
_, kind = htmlBlockEnd([]byte("<img> trailing text")) // tag not alone on line → not type-7
assert.Equal(t, htmlBlockNone, kind)
_, kind = htmlBlockEnd([]byte("text"))
assert.Equal(t, htmlBlockNone, kind)
}
// TestHTMLType7Start exhaustively pins the complete-tag scanner for the
// type-7 block start: a single open or closing tag filling the line, with
// the full attribute grammar (unquoted / single- / double-quoted values,
// valueless attributes, self-closing), and the rejects.
func TestHTMLType7Start(t *testing.T) {
// Valid type-7 starts.
for _, s := range []string{
"<img>", "<br>", "<br/>", "<br />", "<span>", "</div>", "</div >",
"<a-b>", "<img src=x>", "<img src='x'>", `<img src="x">`,
`<img src="x" alt='y'>`, "<input disabled>", "<x a='1' b=\"2\" c=d>",
"<img\tsrc=x>", "<col :ns=v>", "<el _x=v>", "<el a.b-c:d=v>",
"<img> ", // trailing whitespace allowed
} {
assert.Truef(t, htmlType7Start([]byte(s)), "expected type-7 start: %q", s)
}
// Not type-7 starts.
for _, s := range []string{
"<img> text after", // not alone on the line
"<img src=>", // '=' with no value
"<img src=", // '=' at end of input, no value
`<img src="x`, // unclosed double quote
"<img src='x", // unclosed single quote
"<1tag>", // name may not start with a digit
"< img>", // space after '<'
"<>", // no name
"</>", // closing with no name
"</div", // closing with no '>'
"<img src=\"x\"y>", // missing whitespace before next attribute
"<img /x>", // '/' not immediately before '>'
"<img", // no '>'
"<=bad>", // not a name
} {
assert.Falsef(t, htmlType7Start([]byte(s)), "expected NOT type-7: %q", s)
}
}
// TestByteClassHelpers pins the small ASCII-class predicates.
func TestByteClassHelpers(t *testing.T) {
assert.True(t, isASCIILetterByte('a'))
assert.True(t, isASCIILetterByte('Z'))
assert.False(t, isASCIILetterByte('5'))
assert.False(t, isASCIILetterByte('!'))
assert.True(t, isASCIIAlnum('a'))
assert.True(t, isASCIIAlnum('7'))
assert.False(t, isASCIIAlnum('-'))
}
// TestHTMLType6AndClose pins the type-6 tag-block opener arms and the
// case-insensitive type-1 close scan (containsType1Close / containsFold).
func TestHTMLType6AndClose(t *testing.T) {
assert.True(t, htmlType6Start([]byte("<div>")))
assert.True(t, htmlType6Start([]byte("<section foo")), "whitespace boundary")
assert.True(t, htmlType6Start([]byte("<hr/>")), "self-closing boundary")
assert.True(t, htmlType6Start([]byte("</details>")), "closing tag")
assert.True(t, htmlType6Start([]byte("<TABLE")), "case-insensitive, EOL boundary")
assert.False(t, htmlType6Start([]byte("<div=x")), "non-boundary char after a tag name")
assert.False(t, htmlType6Start([]byte("<hr/x")), "slash not followed by >")
assert.False(t, htmlType6Start([]byte("<notatag>")), "name not in the type-6 set")
assert.False(t, htmlType6Start([]byte("<>")), "no tag name")
assert.False(t, htmlType6Start([]byte("<verylongtagnamexceeds>")), "name longer than the buffer")
assert.True(t, containsType1Close([]byte("x</PRE>y")), "case-insensitive close")
assert.True(t, containsType1Close([]byte("lead </TextArea> trail")))
assert.False(t, containsType1Close([]byte("x</em>y")))
assert.False(t, containsType1Close([]byte("short")), "shorter than any closer")
}
func TestContainsFold(t *testing.T) {
needle := []byte("</pre>")
assert.True(t, containsFold([]byte("</PRE>"), needle), "match at position 0")
assert.True(t, containsFold([]byte("x</Pre>y"), needle), "match in middle")
assert.True(t, containsFold([]byte("x</PRE>"), needle), "match at last window")
assert.False(t, containsFold([]byte("x</em>y"), needle), "no match")
assert.False(t, containsFold([]byte("</pr"), needle), "needle longer than line")
assert.False(t, containsFold([]byte(""), needle), "empty line")
}
func TestContainerMarkerScanners(t *testing.T) {
assert.Equal(t, 2, blockquoteMarker([]byte("> x")))
assert.Equal(t, 1, blockquoteMarker([]byte(">x")), "marker with no following space")
assert.Equal(t, 1, blockquoteMarker([]byte(">")))
assert.Equal(t, 4, blockquoteMarker([]byte(" > x")), "≤3 indent allowed")
assert.Equal(t, 0, blockquoteMarker([]byte(" > x")), "4-space indent is not a marker")
assert.Equal(t, 0, blockquoteMarker([]byte("text")))
assert.Equal(t, 2, listMarkerWidth([]byte("- x")))
assert.Equal(t, 2, listMarkerWidth([]byte("* x")))
assert.Equal(t, 2, listMarkerWidth([]byte("+ x")))
assert.Equal(t, 3, listMarkerWidth([]byte("1. x")))
assert.Equal(t, 4, listMarkerWidth([]byte("12) x")))
assert.Equal(t, 0, listMarkerWidth([]byte("-x")), "no space after the marker")
assert.Equal(t, 0, listMarkerWidth([]byte("- ")), "no content after the marker")
assert.Equal(t, 0, listMarkerWidth([]byte("1.x")), "ordered marker needs a space")
assert.Equal(t, 0, listMarkerWidth([]byte("1")), "digits with no delimiter")
assert.Equal(t, 0, listMarkerWidth([]byte(" - x")), "4-space indent is not a marker")
assert.Equal(t, 0, listMarkerWidth([]byte("text")))
}
// TestContainerConsume pins both arms of each container's continuation
// matcher: the blockquote marker, its miss, the list width match, the
// list miss (a dedent), and the blank-line continuation of a list.
func TestContainerConsume(t *testing.T) {
bq := lc0Container{blockquote: true}
next, ok := bq.consume([]byte("> x"), 0)
assert.True(t, ok)
assert.Equal(t, 2, next)
next, ok = bq.consume([]byte(">x"), 0) // marker with no following space
assert.True(t, ok)
assert.Equal(t, 1, next)
next, ok = bq.consume([]byte(" > x"), 0) // ≤3 leading spaces then marker
assert.True(t, ok)
assert.Equal(t, 5, next)
_, ok = bq.consume([]byte("plain"), 0)
assert.False(t, ok, "a line with no marker closes the blockquote")
_, ok = bq.consume([]byte(" > over-indented"), 0)
assert.False(t, ok)
li := lc0Container{width: 2}
next, ok = li.consume([]byte(" x"), 0)
assert.True(t, ok)
assert.Equal(t, 2, next)
next, ok = li.consume([]byte("\tx"), 0)
assert.True(t, ok, "a tab satisfies the list-item width in columns")
assert.Equal(t, 1, next)
next, ok = li.consume([]byte(""), 0)
assert.True(t, ok, "a blank line continues a list item")
assert.Equal(t, 0, next)
_, ok = li.consume([]byte(" x"), 0)
assert.False(t, ok, "one space is under the width-2 list item")
_, ok = li.consume([]byte("x"), 0)
assert.False(t, ok, "a dedented line closes the list item")
}
// TestHTMLType1Start pins the type-1 raw-block opener recognition: the four
// names case-insensitively, the boundary after the name, and the rejects.
func TestHTMLType1Start(t *testing.T) {
assert.True(t, htmlType1Start([]byte("<pre>")))
assert.True(t, htmlType1Start([]byte("<TEXTAREA")), "EOL right after the name")
assert.True(t, htmlType1Start([]byte("<style\tx")), "tab boundary")
assert.True(t, htmlType1Start([]byte("<script foo")))
assert.False(t, htmlType1Start([]byte("<prefix>")), "name is a prefix of a longer tag")
assert.False(t, htmlType1Start([]byte("<div>")))
assert.False(t, htmlType1Start([]byte("<pr")), "shorter than any name")
}
// TestHTMLType6Tags_ZeroByteValue guards the map's per-entry memory layout:
// map[string]bool wastes 1 byte per value (× ~63 entries); struct{} wastes 0.
// An alloc test cannot distinguish the two because the Go compiler's
// m[string(b)] optimization applies to both value types equally.
func TestHTMLType6Tags_ZeroByteValue(t *testing.T) {
vt := reflect.TypeOf(htmlType6Tags).Elem()
if vt.Size() != 0 {
t.Fatalf("htmlType6Tags value type %s has size %d bytes, want 0 (use map[string]struct{})", vt, vt.Size())
}
}
// TestHTMLType6Tags_CommonMarkComplete verifies that htmlType6Tags contains the
// complete CommonMark spec type-6 block tag list. The full enumeration here
// prevents silent omissions: the map has three independent copies in the
// codebase and any copy can drift without a full-list gate.
func TestHTMLType6Tags_CommonMarkComplete(t *testing.T) {
// Full CommonMark 0.31.2 type-6 block tag set.
want := []string{
"address", "article", "aside", "base", "basefont", "blockquote",
"body", "caption", "center", "col", "colgroup", "dd", "details",
"dialog", "dir", "div", "dl", "dt", "fieldset", "figcaption",
"figure", "footer", "form", "frame", "frameset", "h1", "h2", "h3",
"h4", "h5", "h6", "head", "header", "hr", "html", "iframe",
"legend", "li", "link", "main", "menu", "menuitem", "meta", "nav",
"noframes", "ol", "optgroup", "option", "p", "param", "search",
"section", "summary", "table", "tbody", "td", "tfoot", "th",
"thead", "title", "tr", "track", "ul",
}
for _, tag := range want {
if _, ok := htmlType6Tags[tag]; !ok {
t.Errorf("htmlType6Tags missing CommonMark type-6 tag %q", tag)
}
}
}
// TestScanHTMLTag pins the dispatch between open and closing tags.
func TestScanHTMLTag(t *testing.T) {
n, ok := scanHTMLTag([]byte("<img>"))
assert.True(t, ok)
assert.Equal(t, 5, n)
n, ok = scanHTMLTag([]byte("</div>"))
assert.True(t, ok)
assert.Equal(t, 6, n)
// Does not start with '<'.
_, ok = scanHTMLTag([]byte("img>"))
assert.False(t, ok)
// Fewer than 3 bytes.
_, ok = scanHTMLTag([]byte("<a"))
assert.False(t, ok)
}
// TestScanClosingTag pins the closing-tag scanner.
func TestScanClosingTag(t *testing.T) {
s := []byte("</div>")
n, ok := scanClosingTag(s, 2)
assert.True(t, ok)
assert.Equal(t, 6, n)
// Optional whitespace before '>'.
n, ok = scanClosingTag([]byte("</div >"), 2)
assert.True(t, ok)
assert.Equal(t, 7, n)
// Missing '>'.
_, ok = scanClosingTag([]byte("</div"), 2)
assert.False(t, ok)
// Name starts with a digit.
_, ok = scanClosingTag([]byte("</1tag>"), 2)
assert.False(t, ok)
}
// TestScanOpenTag pins the open-tag scanner: tag name, attributes, self-close.
func TestScanOpenTag(t *testing.T) {
n, ok := scanOpenTag([]byte("<img>"), 1)
assert.True(t, ok)
assert.Equal(t, 5, n)
n, ok = scanOpenTag([]byte("<br/>"), 1)
assert.True(t, ok)
assert.Equal(t, 5, n)
n, ok = scanOpenTag([]byte("<br />"), 1)
assert.True(t, ok)
assert.Equal(t, 6, n)
n, ok = scanOpenTag([]byte(`<img src="x">`), 1)
assert.True(t, ok)
assert.Equal(t, 13, n)
// '=' with no value.
_, ok = scanOpenTag([]byte("<img src=>"), 1)
assert.False(t, ok)
// No closing '>'.
_, ok = scanOpenTag([]byte("<img"), 1)
assert.False(t, ok)
}
// TestScanTagName pins the tag-name scanner.
func TestScanTagName(t *testing.T) {
i, ok := scanTagName([]byte("img>"), 0)
assert.True(t, ok)
assert.Equal(t, 3, i)
// Hyphens allowed inside a name.
i, ok = scanTagName([]byte("a-b>"), 0)
assert.True(t, ok)
assert.Equal(t, 3, i)
// Digit at start → reject.
_, ok = scanTagName([]byte("1tag"), 0)
assert.False(t, ok)
// Empty input.
_, ok = scanTagName([]byte(""), 0)
assert.False(t, ok)
// Name runs to end of input (no terminator byte).
i, ok = scanTagName([]byte("img"), 0)
assert.True(t, ok)
assert.Equal(t, 3, i)
}
// TestScanAttribute pins the attribute scanner.
func TestScanAttribute(t *testing.T) {
// Valueless attribute.
i, ok := scanAttribute([]byte(" disabled>"), 0)
assert.True(t, ok)
assert.Equal(t, 9, i)
// Unquoted value.
i, ok = scanAttribute([]byte(" src=x>"), 0)
assert.True(t, ok)
assert.Equal(t, 6, i)
// Double-quoted value.
i, ok = scanAttribute([]byte(` src="x">`), 0)
assert.True(t, ok)
assert.Equal(t, 8, i)
// '=' with no value.
_, ok = scanAttribute([]byte(" src=>"), 0)
assert.False(t, ok)
// No leading whitespace → reject.
_, ok = scanAttribute([]byte("src=x>"), 0)
assert.False(t, ok)
}
// TestScanAttrValue pins the attribute-value scanner.
func TestScanAttrValue(t *testing.T) {
// Unquoted value (stops at '>').
i, ok := scanAttrValue([]byte("value>"), 0)
assert.True(t, ok)
assert.Equal(t, 5, i)
// Single-quoted.
i, ok = scanAttrValue([]byte("'value'"), 0)
assert.True(t, ok)
assert.Equal(t, 7, i)
// Double-quoted.
i, ok = scanAttrValue([]byte(`"value"`), 0)
assert.True(t, ok)
assert.Equal(t, 7, i)
// Unclosed single quote.
_, ok = scanAttrValue([]byte("'value"), 0)
assert.False(t, ok)
// Empty unquoted value (stop byte at position 0).
_, ok = scanAttrValue([]byte(">"), 0)
assert.False(t, ok)
}
// TestSkipHTMLWS pins the whitespace-skipper.
func TestSkipHTMLWS(t *testing.T) {
assert.Equal(t, 2, skipHTMLWS([]byte(" x"), 0))
assert.Equal(t, 1, skipHTMLWS([]byte("\tx"), 0))
assert.Equal(t, 2, skipHTMLWS([]byte(" \tx"), 0))
assert.Equal(t, 0, skipHTMLWS([]byte("x"), 0))
assert.Equal(t, 2, skipHTMLWS([]byte(" "), 2))
}
// TestIsUnquotedStop pins which bytes end an unquoted attribute value.
func TestIsUnquotedStop(t *testing.T) {
for _, b := range []byte{' ', '\t', '"', '\'', '=', '<', '>', '`'} {
assert.Truef(t, isUnquotedStop(b), "expected stop for 0x%02x", b)
}
for _, b := range []byte{'a', '0', '-', '_', '/'} {
assert.Falsef(t, isUnquotedStop(b), "expected non-stop for 0x%02x", b)
}
}
// TestEqualFoldASCII pins the case-insensitive ASCII byte comparison.
// b is always lowercase; a may be any case. Callers always pass same-length
// slices.
func TestEqualFoldASCII(t *testing.T) {
assert.True(t, equalFoldASCII([]byte("pre"), []byte("pre")))
assert.True(t, equalFoldASCII([]byte("PRE"), []byte("pre")))
assert.True(t, equalFoldASCII([]byte("ScRiPt"), []byte("script")))
assert.False(t, equalFoldASCII([]byte("pre"), []byte("div")))
assert.False(t, equalFoldASCII([]byte("foo"), []byte("bar")))
}