fnmatch_test.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package fnmatch
  7. import (
  8. "path/filepath"
  9. "runtime"
  10. "testing"
  11. )
  12. type testcase struct {
  13. pat string
  14. name string
  15. flags int
  16. match bool
  17. }
  18. var testcases = []testcase{
  19. {"", "", 0, true},
  20. {"*", "", 0, true},
  21. {"*", "foo", 0, true},
  22. {"*", "bar", 0, true},
  23. {"*", "*", 0, true},
  24. {"**", "f", 0, true},
  25. {"**", "foo.txt", 0, true},
  26. {"*.*", "foo.txt", 0, true},
  27. {"foo*.txt", "foobar.txt", 0, true},
  28. {"foo.txt", "foo.txt", 0, true},
  29. {"foo.txt", "bar/foo.txt", 0, false},
  30. {"*/foo.txt", "bar/foo.txt", 0, true},
  31. {"f?o.txt", "foo.txt", 0, true},
  32. {"f?o.txt", "fooo.txt", 0, false},
  33. {"f[ab]o.txt", "foo.txt", 0, false},
  34. {"f[ab]o.txt", "fao.txt", 0, true},
  35. {"f[ab]o.txt", "fbo.txt", 0, true},
  36. {"f[ab]o.txt", "fco.txt", 0, false},
  37. {"f[ab]o.txt", "fabo.txt", 0, false},
  38. {"f[ab]o.txt", "f[ab]o.txt", 0, false},
  39. {"f\\[ab\\]o.txt", "f[ab]o.txt", FNM_NOESCAPE, false},
  40. {"*foo.txt", "bar/foo.txt", 0, true},
  41. {"*foo.txt", "bar/foo.txt", FNM_PATHNAME, false},
  42. {"*/foo.txt", "bar/foo.txt", 0, true},
  43. {"*/foo.txt", "bar/foo.txt", FNM_PATHNAME, true},
  44. {"*/foo.txt", "bar/baz/foo.txt", 0, true},
  45. {"*/foo.txt", "bar/baz/foo.txt", FNM_PATHNAME, false},
  46. {"**/foo.txt", "bar/baz/foo.txt", 0, true},
  47. {"**/foo.txt", "bar/baz/foo.txt", FNM_PATHNAME, true},
  48. {"foo.txt", "foo.TXT", FNM_CASEFOLD, true},
  49. // These characters are literals in glob, but not in regexp.
  50. {"hey$hello", "hey$hello", 0, true},
  51. {"hey^hello", "hey^hello", 0, true},
  52. {"hey{hello", "hey{hello", 0, true},
  53. {"hey}hello", "hey}hello", 0, true},
  54. {"hey(hello", "hey(hello", 0, true},
  55. {"hey)hello", "hey)hello", 0, true},
  56. {"hey|hello", "hey|hello", 0, true},
  57. {"hey|hello", "hey|other", 0, false},
  58. }
  59. func TestMatch(t *testing.T) {
  60. switch runtime.GOOS {
  61. case "windows":
  62. testcases = append(testcases, testcase{"foo.txt", "foo.TXT", 0, true})
  63. case "darwin":
  64. testcases = append(testcases, testcase{"foo.txt", "foo.TXT", 0, true})
  65. fallthrough
  66. default:
  67. testcases = append(testcases, testcase{"f\\[ab\\]o.txt", "f[ab]o.txt", 0, true})
  68. testcases = append(testcases, testcase{"foo\\.txt", "foo.txt", 0, true})
  69. testcases = append(testcases, testcase{"foo\\*.txt", "foo*.txt", 0, true})
  70. testcases = append(testcases, testcase{"foo\\.txt", "foo.txt", FNM_NOESCAPE, false})
  71. testcases = append(testcases, testcase{"f\\\\\\[ab\\\\\\]o.txt", "f\\[ab\\]o.txt", 0, true})
  72. }
  73. for _, tc := range testcases {
  74. if m, err := Match(tc.pat, filepath.FromSlash(tc.name), tc.flags); m != tc.match {
  75. if err != nil {
  76. t.Error(err)
  77. } else {
  78. t.Errorf("Match(%q, %q, %d) != %v", tc.pat, tc.name, tc.flags, tc.match)
  79. }
  80. }
  81. }
  82. }
  83. func TestInvalid(t *testing.T) {
  84. if _, err := Match("foo[bar", "...", 0); err == nil {
  85. t.Error("Unexpected nil error")
  86. }
  87. }