| 1 |
// Copyright (c) 2017, Daniel Martí <[email protected]> |
| 2 |
// See LICENSE for licensing information |
| 3 |
|
| 4 |
package pattern |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"regexp" |
| 9 |
"regexp/syntax" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/go-quicktest/qt" |
| 13 |
) |
| 14 |
|
| 15 |
var regexpTests = []struct { |
| 16 |
pat string |
| 17 |
mode Mode |
| 18 |
want string |
| 19 |
wantErr string |
| 20 |
|
| 21 |
mustMatch []string |
| 22 |
mustNotMatch []string |
| 23 |
}{ |
| 24 |
{pat: ``, want: ``}, |
| 25 |
{pat: `foo`, want: `foo`}, |
| 26 |
{ |
| 27 |
pat: `foo`, mode: NoGlobCase, want: `(?si)foo`, |
| 28 |
mustMatch: []string{"foo", "FOO", "Foo"}, |
| 29 |
mustNotMatch: []string{"bar"}, |
| 30 |
}, |
| 31 |
{pat: `foóà中`, mode: Filenames, want: `foóà中`}, |
| 32 |
{pat: `.`, want: `(?s)\.`}, |
| 33 |
{pat: `foo*`, want: `(?s)foo.*`}, |
| 34 |
{pat: `foo*`, mode: Shortest, want: `(?sU)foo.*`}, |
| 35 |
{pat: `foo*`, mode: Shortest | Filenames, want: `(?sU)foo[^/]*`}, |
| 36 |
{ |
| 37 |
pat: `*foo*`, mode: EntireString, want: `(?s)^.*foo.*$`, |
| 38 |
mustMatch: []string{"foo", "prefix-foo", "foo-suffix", "foo.suffix", ".foo.", "a\nbfooc\nd"}, |
| 39 |
mustNotMatch: []string{"bar"}, |
| 40 |
}, |
| 41 |
{ |
| 42 |
pat: `foo*`, mode: Filenames | EntireString, want: `(?s)^foo[^/]*$`, |
| 43 |
mustMatch: []string{"foo", "foo-suffix", "foo.suffix", "foo\nsuffix"}, |
| 44 |
mustNotMatch: []string{"prefix-foo", "foo/suffix"}, |
| 45 |
}, |
| 46 |
{ |
| 47 |
pat: `foo/*`, mode: Filenames | EntireString, want: `(?s)^foo/([^/.][^/]*)?$`, |
| 48 |
mustMatch: []string{"foo/", "foo/suffix"}, |
| 49 |
mustNotMatch: []string{"foo/.suffix", "foo/bar/baz"}, |
| 50 |
}, |
| 51 |
{ |
| 52 |
pat: `foo/*`, mode: Filenames | EntireString | GlobLeadingDot, want: `(?s)^foo/[^/]*$`, |
| 53 |
mustMatch: []string{"foo/", "foo/suffix", "foo/.suffix"}, |
| 54 |
mustNotMatch: []string{"foo/bar/baz"}, |
| 55 |
}, |
| 56 |
{pat: `*foo`, mode: Filenames, want: `(?s)([^/.][^/]*)?foo`}, |
| 57 |
{ |
| 58 |
pat: `*foo`, mode: Filenames | EntireString, want: `(?s)^([^/.][^/]*)?foo$`, |
| 59 |
mustMatch: []string{"foo", "prefix-foo", "prefix.foo"}, |
| 60 |
mustNotMatch: []string{"foo-suffix", "/prefix/foo", ".foo", ".prefix-foo"}, |
| 61 |
}, |
| 62 |
{pat: `**`, want: `(?s).*.*`}, |
| 63 |
{ |
| 64 |
pat: `**`, mode: Filenames | EntireString, want: `(?s)^(/|[^/.][^/]*)*$`, |
| 65 |
mustMatch: []string{"/foo", "/prefix/foo", "/a.b.c/foo", "/a/b/c/foo", "/foo/suffix.ext", "/a\n/\nb"}, |
| 66 |
mustNotMatch: []string{"/.prefix/foo", "/prefix/.foo"}, |
| 67 |
}, |
| 68 |
{ |
| 69 |
pat: `**`, mode: Filenames | NoGlobStar | EntireString, want: `(?s)^([^/.][^/]*)?$`, |
| 70 |
mustMatch: []string{"foo.bar"}, |
| 71 |
mustNotMatch: []string{"foo/bar", ".foo"}, |
| 72 |
}, |
| 73 |
{ |
| 74 |
pat: `**`, mode: Filenames | EntireString | GlobLeadingDot, want: `(?s)^.*$`, |
| 75 |
mustMatch: []string{"/foo", "/prefix/foo", "/a.b.c/foo", "/a/b/c/foo", "/foo/suffix.ext", "/a\n/\nb", "/.prefix/foo", "/prefix/.foo"}, |
| 76 |
}, |
| 77 |
{pat: `/**/foo`, want: `(?s)/.*.*/foo`}, |
| 78 |
{ |
| 79 |
pat: `/**/foo`, mode: Filenames | EntireString, want: `(?s)^/((/|[^/.][^/]*)*/)?foo$`, |
| 80 |
mustMatch: []string{"/foo", "/prefix/foo", "/a.b.c/foo", "/a/b/c/foo"}, |
| 81 |
mustNotMatch: []string{"/foo/suffix", "prefix/foo", "/.prefix/foo", "/prefix/.foo"}, |
| 82 |
}, |
| 83 |
{ |
| 84 |
pat: `/**/foo`, mode: Filenames | EntireString | GlobLeadingDot, want: `(?s)^/(.*/)?foo$`, |
| 85 |
mustMatch: []string{"/foo", "/prefix/foo", "/a.b.c/foo", "/a/b/c/foo", "/.prefix/foo"}, |
| 86 |
mustNotMatch: []string{"/foo/suffix", "prefix/foo", "/prefix/.foo"}, |
| 87 |
}, |
| 88 |
{pat: `/**/foo`, mode: Filenames | NoGlobStar, want: `(?s)/([^/.][^/]*)?/foo`}, |
| 89 |
{pat: `/**/à`, mode: Filenames, want: `(?s)/((/|[^/.][^/]*)*/)?à`}, |
| 90 |
{ |
| 91 |
pat: `/**foo`, mode: Filenames, want: `(?s)/([^/.][^/]*)?foo`, |
| 92 |
// These all match because without EntireString, we match substrings. |
| 93 |
mustMatch: []string{"/foo", "/prefix-foo", "/foo-suffix", "/sub/foo"}, |
| 94 |
}, |
| 95 |
{ |
| 96 |
pat: `/**foo`, mode: Filenames | EntireString, want: `(?s)^/([^/.][^/]*)?foo$`, |
| 97 |
mustMatch: []string{"/foo", "/prefix-foo"}, |
| 98 |
mustNotMatch: []string{"/foo-suffix", "/sub/foo", "/.foo", "/.prefix-foo"}, |
| 99 |
}, |
| 100 |
{ |
| 101 |
pat: `/foo**`, mode: Filenames | EntireString, want: `(?s)^/foo[^/]*$`, |
| 102 |
mustMatch: []string{"/foo", "/foo-suffix", "/foo.suffix"}, |
| 103 |
mustNotMatch: []string{"/prefix-foo", "/foo/sub"}, |
| 104 |
}, |
| 105 |
{pat: `\*`, want: `(?s)\*`}, |
| 106 |
{pat: `\`, wantErr: `^\\ at end of pattern$`}, |
| 107 |
{pat: `?`, want: `(?s).`}, |
| 108 |
{ |
| 109 |
pat: `?`, mode: EntireString, want: `(?s)^.$`, |
| 110 |
mustMatch: []string{"a", "\n", " "}, |
| 111 |
mustNotMatch: []string{"abc", ""}, |
| 112 |
}, |
| 113 |
{pat: `?`, mode: Filenames, want: `(?s)[^/]`}, |
| 114 |
{pat: `?à`, want: `(?s).à`}, |
| 115 |
{pat: `ŀ*`, want: `(?s)ŀ.*`}, |
| 116 |
{pat: `\a`, want: `(?s)a`}, |
| 117 |
{pat: `(`, want: `(?s)\(`}, |
| 118 |
{pat: `a|b`, want: `(?s)a\|b`}, |
| 119 |
{pat: `x{3}`, want: `(?s)x\{3\}`}, |
| 120 |
{pat: `{3,4}`, want: `(?s)\{3,4\}`}, |
| 121 |
{pat: `[a]`, want: `(?s)[a]`}, |
| 122 |
{pat: `[abc]`, want: `(?s)[abc]`}, |
| 123 |
{pat: `[^bc]`, want: `(?s)[^bc]`}, |
| 124 |
{pat: `[!bc]`, want: `(?s)[^bc]`}, |
| 125 |
{pat: `[[]`, want: `(?s)[[]`}, |
| 126 |
{pat: `[\]]`, want: `(?s)[\]]`}, |
| 127 |
{pat: `[\]]`, mode: Filenames, want: `(?s)[\]]`}, |
| 128 |
{pat: `[]]`, want: `(?s)[]]`}, |
| 129 |
{pat: `[!]]`, want: `(?s)[^]]`}, |
| 130 |
{pat: `[^]]`, want: `(?s)[^]]`}, |
| 131 |
{pat: `[a/b]`, want: `(?s)[a/b]`}, |
| 132 |
{ |
| 133 |
pat: `[a/b]`, mode: EntireString | Filenames, want: `(?s)^\[a/b\]$`, |
| 134 |
mustMatch: []string{"[a/b]"}, |
| 135 |
mustNotMatch: []string{"a", "/", "b"}, |
| 136 |
}, |
| 137 |
{ |
| 138 |
pat: `[]/a]`, mode: EntireString | Filenames, want: `(?s)^\[\]/a\]$`, |
| 139 |
mustMatch: []string{"[]/a]"}, |
| 140 |
mustNotMatch: []string{"]", "/", "a", "/a]", "/a"}, |
| 141 |
}, |
| 142 |
{ |
| 143 |
pat: `[[:alpha:]/]`, mode: EntireString | Filenames, want: `(?s)^\[\[:alpha:\]/\]$`, |
| 144 |
mustMatch: []string{"[[:alpha:]/]"}, |
| 145 |
mustNotMatch: []string{"/", "a", "Z"}, |
| 146 |
}, |
| 147 |
{ |
| 148 |
pat: `[[:wrong:]/]`, mode: EntireString | Filenames, want: `(?s)^\[\[:wrong:\]/\]$`, |
| 149 |
mustMatch: []string{"[[:wrong:]/]"}, |
| 150 |
mustNotMatch: []string{"/", "a", "Z"}, |
| 151 |
}, |
| 152 |
{pat: `[[.x.]]`, mode: Filenames, wantErr: `^charClass invalid$`}, |
| 153 |
{ |
| 154 |
pat: `[[./.]]`, mode: EntireString | Filenames, want: `(?s)^\[\[\./\.\]\]$`, |
| 155 |
mustMatch: []string{"[[./.]]"}, |
| 156 |
mustNotMatch: []string{"/", "x"}, |
| 157 |
}, |
| 158 |
{ |
| 159 |
pat: `[a/*`, mode: EntireString | Filenames, want: `(?s)^\[a/([^/.][^/]*)?$`, |
| 160 |
mustMatch: []string{"[a/", "[a/file"}, |
| 161 |
mustNotMatch: []string{"[a/.file", "a/file"}, |
| 162 |
}, |
| 163 |
{pat: `[a/\`, mode: Filenames, wantErr: `^\\ at end of pattern$`}, |
| 164 |
{ |
| 165 |
pat: `[\]/]`, mode: EntireString | Filenames, want: `(?s)^\[\\\]/\]$`, |
| 166 |
mustMatch: []string{`[\]/]`}, |
| 167 |
mustNotMatch: []string{"]/]", "/"}, |
| 168 |
}, |
| 169 |
{ |
| 170 |
pat: `[!]/a]`, mode: EntireString | Filenames, want: `(?s)^\[!\]/a\]$`, |
| 171 |
mustMatch: []string{"[!]/a]"}, |
| 172 |
mustNotMatch: []string{"]/a]", "/"}, |
| 173 |
}, |
| 174 |
{ |
| 175 |
pat: `[\[:digit:]/]`, mode: EntireString | Filenames, want: `(?s)^[\[:digit:]/\]$`, |
| 176 |
mustMatch: []string{"[/]", ":/]", "d/]"}, |
| 177 |
mustNotMatch: []string{`[\[:digit:]/]`, "/"}, |
| 178 |
}, |
| 179 |
// An unmatched "[" is a literal, like in Bash. |
| 180 |
{ |
| 181 |
pat: `[`, mode: EntireString, want: `(?s)^\[$`, |
| 182 |
mustMatch: []string{"["}, |
| 183 |
mustNotMatch: []string{"", "[]"}, |
| 184 |
}, |
| 185 |
// TODO: bash treats the trailing backslash as a literal instead. |
| 186 |
{pat: `[\`, wantErr: `^\\ at end of pattern$`}, |
| 187 |
{pat: `[^`, want: `(?s)\[\^`}, |
| 188 |
{pat: `[!`, want: `(?s)\[!`}, |
| 189 |
{pat: `[!bc]`, want: `(?s)[^bc]`}, |
| 190 |
{pat: `[]`, want: `(?s)\[\]`}, |
| 191 |
{pat: `[^]`, want: `(?s)\[\^\]`}, |
| 192 |
{pat: `[!]`, want: `(?s)\[!\]`}, |
| 193 |
{ |
| 194 |
pat: `[ab`, mode: EntireString, want: `(?s)^\[ab$`, |
| 195 |
mustMatch: []string{"[ab"}, |
| 196 |
mustNotMatch: []string{"ab", "a"}, |
| 197 |
}, |
| 198 |
{ |
| 199 |
pat: `[a*`, mode: EntireString, want: `(?s)^\[a.*$`, |
| 200 |
mustMatch: []string{"[a", "[abc"}, |
| 201 |
mustNotMatch: []string{"a", "[b"}, |
| 202 |
}, |
| 203 |
{pat: `[z-a`, want: `(?s)\[z-a`}, |
| 204 |
{pat: `[[ab`, want: `(?s)\[\[ab`}, |
| 205 |
{pat: `[a[b`, want: `(?s)\[a\[b`}, |
| 206 |
{ |
| 207 |
pat: `[[abc]`, mode: EntireString, want: `(?s)^[[abc]$`, |
| 208 |
mustMatch: []string{"[", "a"}, |
| 209 |
mustNotMatch: []string{"[a", "d"}, |
| 210 |
}, |
| 211 |
{ |
| 212 |
pat: `[[:alpha:]`, mode: EntireString, want: `(?s)^\[[:alpha:]$`, |
| 213 |
mustMatch: []string{"[a", "[:"}, |
| 214 |
mustNotMatch: []string{"[1", "["}, |
| 215 |
}, |
| 216 |
// Invalid character classes are errors even in an unmatched bracket. |
| 217 |
{pat: `[[:wrong:]`, wantErr: `^charClass invalid$`}, |
| 218 |
{pat: `[[.x.]`, wantErr: `^charClass invalid$`}, |
| 219 |
{pat: `[z-a[:wrong:]`, wantErr: `^charClass invalid$`}, |
| 220 |
{pat: `[a-]`, want: `(?s)[a-]`}, |
| 221 |
{ |
| 222 |
pat: `[\0]`, want: `(?s)[0]`, |
| 223 |
mustMatch: []string{"0"}, |
| 224 |
mustNotMatch: []string{"\x00", "a"}, |
| 225 |
}, |
| 226 |
{ |
| 227 |
pat: `[\d]`, want: `(?s)[d]`, |
| 228 |
mustMatch: []string{"d"}, |
| 229 |
mustNotMatch: []string{"5"}, |
| 230 |
}, |
| 231 |
{pat: `[\5]`, want: `(?s)[5]`}, |
| 232 |
{ |
| 233 |
pat: `[\é]`, want: `(?s)[é]`, |
| 234 |
mustMatch: []string{"é"}, |
| 235 |
}, |
| 236 |
{ |
| 237 |
pat: `[\-]`, want: `(?s)[\-]`, |
| 238 |
mustMatch: []string{"-"}, |
| 239 |
mustNotMatch: []string{"a"}, |
| 240 |
}, |
| 241 |
{pat: `[z-a]`, wantErr: `^invalid range: z-a$`}, |
| 242 |
{pat: `[a-a]`, want: `(?s)[a-a]`}, |
| 243 |
{pat: `[aa]`, want: `(?s)[aa]`}, |
| 244 |
{pat: `[0-4A-Z]`, want: `(?s)[0-4A-Z]`}, |
| 245 |
{pat: `[-a]`, want: `(?s)[-a]`}, |
| 246 |
{pat: `[^-a]`, want: `(?s)[^-a]`}, |
| 247 |
{pat: `[a-]`, want: `(?s)[a-]`}, |
| 248 |
{pat: `[[:digit:]]`, want: `(?s)[[:digit:]]`}, |
| 249 |
{ |
| 250 |
pat: `*[![:space:]]*`, mode: EntireString, want: `(?s)^.*[^[:space:]].*$`, |
| 251 |
mustMatch: []string{"x", " x ", "\t\n!"}, |
| 252 |
mustNotMatch: []string{"", " ", " \t\n "}, |
| 253 |
}, |
| 254 |
{pat: `[!a[:space:]0-9]`, want: `(?s)[^a[:space:]0-9]`}, |
| 255 |
{pat: `[a[:digit]]`, wantErr: `^charClass invalid$`}, |
| 256 |
{pat: `[[:`, wantErr: `^charClass invalid$`}, |
| 257 |
// Like Bash, an unclosed extended operator group is literal text, |
| 258 |
// and the operator is parsed as a regular character. |
| 259 |
{ |
| 260 |
pat: `@(a`, mode: ExtendedOperators | EntireString, want: `(?s)^@\(a$`, |
| 261 |
mustMatch: []string{"@(a"}, |
| 262 |
mustNotMatch: []string{"a"}, |
| 263 |
}, |
| 264 |
{ |
| 265 |
pat: `@(a|b`, mode: ExtendedOperators | EntireString, want: `(?s)^@\(a\|b$`, |
| 266 |
mustMatch: []string{"@(a|b"}, |
| 267 |
mustNotMatch: []string{"a", "b"}, |
| 268 |
}, |
| 269 |
{ |
| 270 |
pat: `*(a`, mode: ExtendedOperators | EntireString, want: `(?s)^.*\(a$`, |
| 271 |
mustMatch: []string{"(a", "foo(a"}, |
| 272 |
mustNotMatch: []string{"a"}, |
| 273 |
}, |
| 274 |
{pat: `+(`, mode: ExtendedOperators, want: `(?s)\+\(`}, |
| 275 |
{pat: `!(a`, mode: ExtendedOperators | EntireString, want: `(?s)^!\(a$`}, |
| 276 |
{ |
| 277 |
pat: `@(a|@(b)`, mode: ExtendedOperators | EntireString, want: `(?s)^@\(a\|(b)$`, |
| 278 |
mustMatch: []string{"@(a|b"}, |
| 279 |
mustNotMatch: []string{"a", "b"}, |
| 280 |
}, |
| 281 |
{pat: `[[:digit`, wantErr: `^charClass invalid$`}, |
| 282 |
{pat: `[[:wrong:]]`, wantErr: `^charClass invalid$`}, |
| 283 |
{pat: `[[=x=]]`, wantErr: `^charClass invalid$`}, |
| 284 |
{pat: `[[.x.]]`, wantErr: `^charClass invalid$`}, |
| 285 |
} |
| 286 |
|
| 287 |
func TestRegexp(t *testing.T) { |
| 288 |
t.Parallel() |
| 289 |
for i, tc := range regexpTests { |
| 290 |
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) { |
| 291 |
t.Logf("input: pattern=%q mode=%#b\n", tc.pat, tc.mode) |
| 292 |
got, gotErr := Regexp(tc.pat, tc.mode) |
| 293 |
if tc.wantErr != "" { |
| 294 |
qt.Assert(t, qt.ErrorMatches(gotErr, tc.wantErr)) |
| 295 |
} else { |
| 296 |
qt.Assert(t, qt.IsNil(gotErr)) |
| 297 |
} |
| 298 |
if got != tc.want { |
| 299 |
t.Errorf("(%q, %#b) got %q, wanted %q", tc.pat, tc.mode, got, tc.want) |
| 300 |
} |
| 301 |
_, rxErr := syntax.Parse(got, syntax.Perl) |
| 302 |
if gotErr == nil && rxErr != nil { |
| 303 |
t.Fatalf("regexp/syntax.Parse(%q) failed with %q", got, rxErr) |
| 304 |
} |
| 305 |
rx := regexp.MustCompile(got) |
| 306 |
for _, s := range tc.mustMatch { |
| 307 |
qt.Check(t, qt.IsTrue(rx.MatchString(s)), qt.Commentf("must match: %q", s)) |
| 308 |
} |
| 309 |
for _, s := range tc.mustNotMatch { |
| 310 |
qt.Check(t, qt.IsFalse(rx.MatchString(s)), qt.Commentf("must not match: %q", s)) |
| 311 |
} |
| 312 |
}) |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
var metaTests = []struct { |
| 317 |
pat string |
| 318 |
wantHas bool |
| 319 |
wantQuote string |
| 320 |
}{ |
| 321 |
{``, false, ``}, |
| 322 |
{`foo`, false, `foo`}, |
| 323 |
{`.`, false, `.`}, |
| 324 |
{`*`, true, `\*`}, |
| 325 |
{`foo?`, true, `foo\?`}, |
| 326 |
{`\[`, false, `\\\[`}, |
| 327 |
{`{`, false, `{`}, |
| 328 |
{`[ab]`, true, `\[ab]`}, |
| 329 |
{`[ab`, false, `\[ab`}, |
| 330 |
{`ab]`, false, `ab]`}, |
| 331 |
{`[a\]`, false, `\[a\\]`}, |
| 332 |
{`[[:wrong:]]`, true, `\[\[:wrong:]]`}, |
| 333 |
{`[[:`, false, `\[\[:`}, |
| 334 |
} |
| 335 |
|
| 336 |
func TestMeta(t *testing.T) { |
| 337 |
t.Parallel() |
| 338 |
for _, tc := range metaTests { |
| 339 |
if got := HasMeta(tc.pat, 0); got != tc.wantHas { |
| 340 |
t.Errorf("HasMeta(%q, 0) got %t, wanted %t", |
| 341 |
tc.pat, got, tc.wantHas) |
| 342 |
} |
| 343 |
if got := QuoteMeta(tc.pat, 0); got != tc.wantQuote { |
| 344 |
t.Errorf("QuoteMeta(%q, 0) got %q, wanted %q", |
| 345 |
tc.pat, got, tc.wantQuote) |
| 346 |
} |
| 347 |
} |
| 348 |
} |