-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
extended_test.go
172 lines (163 loc) · 3.42 KB
/
extended_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
package regext
import (
"regexp"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRemoveComments(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
description string
expr string
expected string
}{
{
description: "no comments",
expr: " just a normal expr ",
expected: " just a normal expr ",
},
{
description: "trailing comment",
expr: " something # with a trailing comment",
expected: " something ",
},
{
description: "escaped comment",
expr: " something \\# with a trailing comment",
expected: " something \\# with a trailing comment",
},
{
description: "escaped comment in a comment",
expr: " something #\\# with a trailing comment",
expected: " something ",
},
{
description: "comment in a comment",
expr: " something # with a trailing #comment",
expected: " something ",
},
{
description: "multiline expr",
expr: `some #comment
and another #comment
much # wow`,
expected: `some
and another
much `,
},
} {
tc := tc // enable parallel sub-tests
t.Run(tc.description, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tc.expected, removeComments(tc.expr))
})
}
}
func TestRemoveWhitespace(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
description string
expr string
expected string
}{
{
description: "only spaces",
expr: " \t \n ",
expected: "",
},
{
description: "leading spaces",
expr: " hi",
expected: "hi",
},
{
description: "trailing spaces",
expr: "hi ",
expected: "hi",
},
{
description: "surrounding spaces",
expr: " hi ",
expected: "hi",
},
{
description: "multiline expr",
expr: `
some lines \s here
and here
`,
expected: `somelines\shereandhere`,
},
} {
tc := tc // enable parallel sub-tests
t.Run(tc.description, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tc.expected, removeWhitespace(tc.expr))
})
}
}
func TestExtendedRegex(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
description string
expr string
expected string
}{
{
description: "blank expr",
expr: "",
expected: "",
},
{
description: "nothing to remove",
expr: `some(\sexpr)`,
expected: `some(\sexpr)`,
},
{
description: "remove comment",
expr: `some#comment`,
expected: `some`,
},
{
description: "remove whitespace",
expr: `some
(\s expr) `,
expected: `some(\sexpr)`,
},
{
description: "remove both comments and whitespace",
expr: `
some \s #complex
(\s expr)? # expression
`,
expected: `some\s(\sexpr)?`,
},
} {
tc := tc // enable parallel sub-tests
t.Run(tc.description, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tc.expected, extendedRegexp(tc.expr))
})
}
}
func TestCompile(t *testing.T) {
t.Parallel()
expr, err := Compile(`some
long #expr`)
require.NoError(t, err)
assert.Equal(t, regexp.MustCompile(`somelong`), expr)
_, err = Compile(`broken expr**`)
assert.Error(t, err)
}
func TestMustCompile(t *testing.T) {
t.Parallel()
assert.NotPanics(t, func() {
expr := MustCompile(`some
long #expr`)
assert.Equal(t, regexp.MustCompile(`somelong`), expr)
})
assert.Panics(t, func() {
_ = MustCompile(`broken expr**`)
})
}