Debian Codegraph @

parser/regexp_test.go

raw symbols SCIP
1 package parser
2
3 import (
4 "regexp"
5 "testing"
6 )
7
8 func TestRegExp(t *testing.T) {
9 tt(t, func() {
10 {
11 // err
12 test := func(input string, expect interface{}) {
13 _, err := TransformRegExp(input)
14 is(err, expect)
15 }
16
17 test("[", "Unterminated character class")
18
19 test("(", "Unterminated group")
20
21 test("(?=)", "re2: Invalid (?=) <lookahead>")
22
23 test("(?=)", "re2: Invalid (?=) <lookahead>")
24
25 test("(?!)", "re2: Invalid (?!) <lookahead>")
26
27 // An error anyway
28 test("(?=", "re2: Invalid (?=) <lookahead>")
29
30 test("\\1", "re2: Invalid \\1 <backreference>")
31
32 test("\\90", "re2: Invalid \\90 <backreference>")
33
34 test("\\9123456789", "re2: Invalid \\9123456789 <backreference>")
35
36 test("\\(?=)", "Unmatched ')'")
37
38 test(")", "Unmatched ')'")
39 }
40
41 {
42 // err
43 test := func(input, expect string, expectErr interface{}) {
44 output, err := TransformRegExp(input)
45 is(output, expect)
46 is(err, expectErr)
47 }
48
49 test("(?!)", "(?!)", "re2: Invalid (?!) <lookahead>")
50
51 test(")", "", "Unmatched ')'")
52
53 test("(?!))", "", "re2: Invalid (?!) <lookahead>")
54
55 test("\\0", "\\0", nil)
56
57 test("\\1", "\\1", "re2: Invalid \\1 <backreference>")
58
59 test("\\9123456789", "\\9123456789", "re2: Invalid \\9123456789 <backreference>")
60 }
61
62 {
63 // err
64 test := func(input string, expect string) {
65 result, err := TransformRegExp(input)
66 is(err, nil)
67 if is(result, expect) {
68 _, err = regexp.Compile(result)
69 if !is(err, nil) {
70 t.Log(result)
71 }
72 }
73 }
74
75 test("", "")
76
77 test("abc", "abc")
78
79 test(`\abc`, `abc`)
80
81 test(`\a\b\c`, `a\bc`)
82
83 test(`\x`, `x`)
84
85 test(`\c`, `c`)
86
87 test(`\cA`, `\x01`)
88
89 test(`\cz`, `\x1a`)
90
91 test(`\ca`, `\x01`)
92
93 test(`\cj`, `\x0a`)
94
95 test(`\ck`, `\x0b`)
96
97 test(`\+`, `\+`)
98
99 test(`[\b]`, `[\x08]`)
100
101 test(`\u0z01\x\undefined`, `u0z01xundefined`)
102
103 test(`\\|'|\r|\n|\t|\u2028|\u2029`, `\\|'|\r|\n|\t|\x{2028}|\x{2029}`)
104
105 test("]", "]")
106
107 test("}", "}")
108
109 test("%", "%")
110
111 test("(%)", "(%)")
112
113 test("(?:[%\\s])", "(?:[%\\s])")
114
115 test("[[]", "[[]")
116
117 test("\\101", "\\x41")
118
119 test("\\51", "\\x29")
120
121 test("\\051", "\\x29")
122
123 test("\\175", "\\x7d")
124
125 test("\\04", "\\x04")
126
127 test(`<%([\s\S]+?)%>`, `<%([\s\S]+?)%>`)
128
129 test(`(.)^`, "(.)^")
130
131 test(`<%-([\s\S]+?)%>|<%=([\s\S]+?)%>|<%([\s\S]+?)%>|$`, `<%-([\s\S]+?)%>|<%=([\s\S]+?)%>|<%([\s\S]+?)%>|$`)
132
133 test(`\$`, `\$`)
134
135 test(`[G-b]`, `[G-b]`)
136
137 test(`[G-b\0]`, `[G-b\0]`)
138 }
139 })
140 }
141
142 func TestTransformRegExp(t *testing.T) {
143 tt(t, func() {
144 pattern, err := TransformRegExp(`\s+abc\s+`)
145 is(err, nil)
146 is(pattern, `\s+abc\s+`)
147 is(regexp.MustCompile(pattern).MatchString("\t abc def"), true)
148 })
149 }