Russ Cox | 78961ed | 2010-02-24 16:11:14 -0800 | [diff] [blame] | 1 | // Copyright 2009 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package path |
| 6 | |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 7 | import "testing" |
Russ Cox | 78961ed | 2010-02-24 16:11:14 -0800 | [diff] [blame] | 8 | |
| 9 | type MatchTest struct { |
| 10 | pattern, s string |
| 11 | match bool |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 12 | err error |
Russ Cox | 78961ed | 2010-02-24 16:11:14 -0800 | [diff] [blame] | 13 | } |
| 14 | |
| 15 | var matchTests = []MatchTest{ |
Robert Griesemer | 3478891 | 2010-10-22 10:06:33 -0700 | [diff] [blame] | 16 | {"abc", "abc", true, nil}, |
| 17 | {"*", "abc", true, nil}, |
| 18 | {"*c", "abc", true, nil}, |
| 19 | {"a*", "a", true, nil}, |
| 20 | {"a*", "abc", true, nil}, |
| 21 | {"a*", "ab/c", false, nil}, |
| 22 | {"a*/b", "abc/b", true, nil}, |
| 23 | {"a*/b", "a/c/b", false, nil}, |
| 24 | {"a*b*c*d*e*/f", "axbxcxdxe/f", true, nil}, |
| 25 | {"a*b*c*d*e*/f", "axbxcxdxexxx/f", true, nil}, |
| 26 | {"a*b*c*d*e*/f", "axbxcxdxe/xxx/f", false, nil}, |
| 27 | {"a*b*c*d*e*/f", "axbxcxdxexxx/fff", false, nil}, |
| 28 | {"a*b?c*x", "abxbbxdbxebxczzx", true, nil}, |
| 29 | {"a*b?c*x", "abxbbxdbxebxczzy", false, nil}, |
| 30 | {"ab[c]", "abc", true, nil}, |
| 31 | {"ab[b-d]", "abc", true, nil}, |
| 32 | {"ab[e-g]", "abc", false, nil}, |
| 33 | {"ab[^c]", "abc", false, nil}, |
| 34 | {"ab[^b-d]", "abc", false, nil}, |
| 35 | {"ab[^e-g]", "abc", true, nil}, |
| 36 | {"a\\*b", "a*b", true, nil}, |
| 37 | {"a\\*b", "ab", false, nil}, |
| 38 | {"a?b", "a☺b", true, nil}, |
| 39 | {"a[^a]b", "a☺b", true, nil}, |
| 40 | {"a???b", "a☺b", false, nil}, |
| 41 | {"a[^a][^a][^a]b", "a☺b", false, nil}, |
| 42 | {"[a-ζ]*", "α", true, nil}, |
| 43 | {"*[a-ζ]", "A", false, nil}, |
| 44 | {"a?b", "a/b", false, nil}, |
| 45 | {"a*b", "a/b", false, nil}, |
| 46 | {"[\\]a]", "]", true, nil}, |
| 47 | {"[\\-]", "-", true, nil}, |
| 48 | {"[x\\-]", "x", true, nil}, |
| 49 | {"[x\\-]", "-", true, nil}, |
| 50 | {"[x\\-]", "z", false, nil}, |
| 51 | {"[\\-x]", "x", true, nil}, |
| 52 | {"[\\-x]", "-", true, nil}, |
| 53 | {"[\\-x]", "a", false, nil}, |
| 54 | {"[]a]", "]", false, ErrBadPattern}, |
| 55 | {"[-]", "-", false, ErrBadPattern}, |
| 56 | {"[x-]", "x", false, ErrBadPattern}, |
| 57 | {"[x-]", "-", false, ErrBadPattern}, |
| 58 | {"[x-]", "z", false, ErrBadPattern}, |
| 59 | {"[-x]", "x", false, ErrBadPattern}, |
| 60 | {"[-x]", "-", false, ErrBadPattern}, |
| 61 | {"[-x]", "a", false, ErrBadPattern}, |
| 62 | {"\\", "a", false, ErrBadPattern}, |
| 63 | {"[a-b-c]", "a", false, ErrBadPattern}, |
Pieter Droogendijk | 27032fd | 2013-07-31 16:58:28 +1000 | [diff] [blame] | 64 | {"[", "a", false, ErrBadPattern}, |
| 65 | {"[^", "a", false, ErrBadPattern}, |
| 66 | {"[^bc", "a", false, ErrBadPattern}, |
| 67 | {"a[", "a", false, nil}, |
| 68 | {"a[", "ab", false, ErrBadPattern}, |
Robert Griesemer | 3478891 | 2010-10-22 10:06:33 -0700 | [diff] [blame] | 69 | {"*x", "xxx", true, nil}, |
Russ Cox | 78961ed | 2010-02-24 16:11:14 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | func TestMatch(t *testing.T) { |
| 73 | for _, tt := range matchTests { |
| 74 | ok, err := Match(tt.pattern, tt.s) |
| 75 | if ok != tt.match || err != tt.err { |
Rob Pike | 1959c3a | 2010-09-23 13:48:56 +1000 | [diff] [blame] | 76 | t.Errorf("Match(%#q, %#q) = %v, %v want %v, nil", tt.pattern, tt.s, ok, err, tt.match) |
Russ Cox | 78961ed | 2010-02-24 16:11:14 -0800 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | } |