1
2
3
4
5 package syntax
6
7 import (
8 "testing"
9 )
10
11 func TestRunesInFoldableRange(t *testing.T) {
12 for _, tt := range []struct {
13 regexp string
14 want int
15 }{
16 {`[a-z]`, 26},
17 {`[a-bd-eg-h]`, 6},
18 {`[\x00-A]`, 1},
19 {`[\x{1e943}-\x{1e949}]`, 1},
20 } {
21 re, err := Parse(tt.regexp, MatchNL|Perl&^OneLine)
22 if err != nil {
23 t.Errorf("Parse(%#q) = error %v", tt.regexp, err)
24 continue
25 }
26 if got := runesInFoldableRange(re); got != tt.want {
27 t.Errorf("runesInFoldableRange(%#q) = %d, want %d", tt.regexp, got, tt.want)
28 }
29 }
30 }
31
32 func TestIncludesMostFoldableRunes(t *testing.T) {
33 for _, tt := range []struct {
34 regexp string
35 want bool
36 }{
37 {``, false},
38 {`[A-z]`, false},
39 {`[A-\x{10000}]`, true},
40 {`\W`, true},
41 {`[\x{1e900}-\x{3e949}]`, false},
42 } {
43 re, err := Parse(tt.regexp, MatchNL|Perl&^OneLine)
44 if err != nil {
45 t.Errorf("Parse(%#q) = error %v", tt.regexp, err)
46 continue
47 }
48 if got := includesMostFoldableRunes(re); got != tt.want {
49 t.Errorf("includesMostFoldableRunes(%#q) = %t, want %t", tt.regexp, got, tt.want)
50 }
51 }
52 }
53
54 func TestCharClassIsFoldSensitive(t *testing.T) {
55 for _, tt := range []struct {
56 regexp string
57 want bool
58 }{
59 {`\d`, false},
60 {`[A-B]`, true},
61 {`[kK]`, true},
62 {`[A-Ba-b]`, false},
63 {`[^Aa]`, false},
64 {`\W`, true},
65 {`[A-\x{1e943}]`, false},
66 {`[B-\x{1e943}]`, true},
67 {`[A-\x{1e942}]`, true},
68 {`\D`, false},
69 } {
70 re, err := Parse(tt.regexp, MatchNL|Perl&^OneLine)
71 if err != nil {
72 t.Errorf("Parse(%#q) = error %v", tt.regexp, err)
73 continue
74 }
75 if got := charClassIsFoldSensitive(re); got != tt.want {
76 t.Errorf("foldSensitiveCharClass(%#q) = %t, want %t", tt.regexp, got, tt.want)
77 }
78 }
79 }
80
81 func BenchmarkIssue69303(b *testing.B) {
82 for _, tt := range []struct {
83 name string
84 re string
85 }{
86 {name: "negated", re: `.[^.]\w\W`},
87 {name: "first-rune-sensitive", re: `[\x{13a0}\x{b000}-\x{10ffff}]`},
88 } {
89 b.Run(tt.name, func(b *testing.B) {
90 re, err := Parse(tt.re, MatchNL|Perl&^OneLine)
91 if err != nil {
92 b.Fatal(err)
93 }
94 for b.Loop() {
95 _ = re.String()
96 }
97 })
98 }
99 }
100
View as plain text