match_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. package path
  5. import "testing"
  6. type MatchTest struct {
  7. pattern, s string
  8. match bool
  9. err error
  10. }
  11. var matchTests = []MatchTest{
  12. {"abc", "abc", true, nil},
  13. {"*", "abc", true, nil},
  14. {"*c", "abc", true, nil},
  15. {"a*", "a", true, nil},
  16. {"a*", "abc", true, nil},
  17. {"a*", "ab/c", false, nil},
  18. {"a*/b", "abc/b", true, nil},
  19. {"a*/b", "a/c/b", false, nil},
  20. {"a*b*c*d*e*/f", "axbxcxdxe/f", true, nil},
  21. {"a*b*c*d*e*/f", "axbxcxdxexxx/f", true, nil},
  22. {"a*b*c*d*e*/f", "axbxcxdxe/xxx/f", false, nil},
  23. {"a*b*c*d*e*/f", "axbxcxdxexxx/fff", false, nil},
  24. {"a*b?c*x", "abxbbxdbxebxczzx", true, nil},
  25. {"a*b?c*x", "abxbbxdbxebxczzy", false, nil},
  26. {"ab[c]", "abc", true, nil},
  27. {"ab[b-d]", "abc", true, nil},
  28. {"ab[e-g]", "abc", false, nil},
  29. {"ab[^c]", "abc", false, nil},
  30. {"ab[^b-d]", "abc", false, nil},
  31. {"ab[^e-g]", "abc", true, nil},
  32. {"a\\*b", "a*b", true, nil},
  33. {"a\\*b", "ab", false, nil},
  34. {"a?b", "a☺b", true, nil},
  35. {"a[^a]b", "a☺b", true, nil},
  36. {"a???b", "a☺b", false, nil},
  37. {"a[^a][^a][^a]b", "a☺b", false, nil},
  38. {"[a-ζ]*", "α", true, nil},
  39. {"*[a-ζ]", "A", false, nil},
  40. {"a?b", "a/b", false, nil},
  41. {"a*b", "a/b", false, nil},
  42. {"[\\]a]", "]", true, nil},
  43. {"[\\-]", "-", true, nil},
  44. {"[x\\-]", "x", true, nil},
  45. {"[x\\-]", "-", true, nil},
  46. {"[x\\-]", "z", false, nil},
  47. {"[\\-x]", "x", true, nil},
  48. {"[\\-x]", "-", true, nil},
  49. {"[\\-x]", "a", false, nil},
  50. {"[]a]", "]", false, ErrBadPattern},
  51. {"[-]", "-", false, ErrBadPattern},
  52. {"[x-]", "x", false, ErrBadPattern},
  53. {"[x-]", "-", false, ErrBadPattern},
  54. {"[x-]", "z", false, ErrBadPattern},
  55. {"[-x]", "x", false, ErrBadPattern},
  56. {"[-x]", "-", false, ErrBadPattern},
  57. {"[-x]", "a", false, ErrBadPattern},
  58. {"\\", "a", false, ErrBadPattern},
  59. {"[a-b-c]", "a", false, ErrBadPattern},
  60. {"[", "a", false, ErrBadPattern},
  61. {"[^", "a", false, ErrBadPattern},
  62. {"[^bc", "a", false, ErrBadPattern},
  63. {"a[", "a", false, nil},
  64. {"a[", "ab", false, ErrBadPattern},
  65. {"*x", "xxx", true, nil},
  66. }
  67. func TestMatch(t *testing.T) {
  68. for _, tt := range matchTests {
  69. ok, err := Match(tt.pattern, tt.s)
  70. if ok != tt.match || err != tt.err {
  71. t.Errorf("Match(%#q, %#q) = %v, %v want %v, nil", tt.pattern, tt.s, ok, err, tt.match)
  72. }
  73. }
  74. }