fnmatch_test.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package fnmatch
  16. import (
  17. "path/filepath"
  18. "runtime"
  19. "testing"
  20. )
  21. type testcase struct {
  22. pat string
  23. name string
  24. flags int
  25. match bool
  26. }
  27. var testcases = []testcase{
  28. {"", "", 0, true},
  29. {"*", "", 0, true},
  30. {"*", "foo", 0, true},
  31. {"*", "bar", 0, true},
  32. {"*", "*", 0, true},
  33. {"**", "f", 0, true},
  34. {"**", "foo.txt", 0, true},
  35. {"*.*", "foo.txt", 0, true},
  36. {"foo*.txt", "foobar.txt", 0, true},
  37. {"foo.txt", "foo.txt", 0, true},
  38. {"foo.txt", "bar/foo.txt", 0, false},
  39. {"*/foo.txt", "bar/foo.txt", 0, true},
  40. {"f?o.txt", "foo.txt", 0, true},
  41. {"f?o.txt", "fooo.txt", 0, false},
  42. {"f[ab]o.txt", "foo.txt", 0, false},
  43. {"f[ab]o.txt", "fao.txt", 0, true},
  44. {"f[ab]o.txt", "fbo.txt", 0, true},
  45. {"f[ab]o.txt", "fco.txt", 0, false},
  46. {"f[ab]o.txt", "fabo.txt", 0, false},
  47. {"f[ab]o.txt", "f[ab]o.txt", 0, false},
  48. {"f\\[ab\\]o.txt", "f[ab]o.txt", FNM_NOESCAPE, false},
  49. {"*foo.txt", "bar/foo.txt", 0, true},
  50. {"*foo.txt", "bar/foo.txt", FNM_PATHNAME, false},
  51. {"*/foo.txt", "bar/foo.txt", 0, true},
  52. {"*/foo.txt", "bar/foo.txt", FNM_PATHNAME, true},
  53. {"*/foo.txt", "bar/baz/foo.txt", 0, true},
  54. {"*/foo.txt", "bar/baz/foo.txt", FNM_PATHNAME, false},
  55. {"**/foo.txt", "bar/baz/foo.txt", 0, true},
  56. {"**/foo.txt", "bar/baz/foo.txt", FNM_PATHNAME, true},
  57. {"foo.txt", "foo.TXT", FNM_CASEFOLD, true},
  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. }