fnmatch_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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", NoEscape, false},
  40. {"*foo.txt", "bar/foo.txt", 0, true},
  41. {"*foo.txt", "bar/foo.txt", PathName, false},
  42. {"*/foo.txt", "bar/foo.txt", 0, true},
  43. {"*/foo.txt", "bar/foo.txt", PathName, true},
  44. {"*/foo.txt", "bar/baz/foo.txt", 0, true},
  45. {"*/foo.txt", "bar/baz/foo.txt", PathName, false},
  46. {"**/foo.txt", "bar/baz/foo.txt", 0, true},
  47. {"**/foo.txt", "bar/baz/foo.txt", PathName, true},
  48. {"foo.txt", "foo.TXT", CaseFold, true},
  49. {"(?i)foo.txt", "foo.TXT", 0, true},
  50. {"(?i)**foo.txt", "/dev/tmp/foo.TXT", 0, true},
  51. {"(?i)!**foo.txt", "/dev/tmp/foo.TXT", 0, false},
  52. // These characters are literals in glob, but not in regexp.
  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(hello", 0, true},
  58. {"hey)hello", "hey)hello", 0, true},
  59. {"hey|hello", "hey|hello", 0, true},
  60. {"hey|hello", "hey|other", 0, false},
  61. }
  62. func TestMatch(t *testing.T) {
  63. switch runtime.GOOS {
  64. case "windows":
  65. testcases = append(testcases, testcase{"foo.txt", "foo.TXT", 0, true})
  66. case "darwin":
  67. testcases = append(testcases, testcase{"foo.txt", "foo.TXT", 0, true})
  68. fallthrough
  69. default:
  70. testcases = append(testcases, testcase{"f\\[ab\\]o.txt", "f[ab]o.txt", 0, true})
  71. testcases = append(testcases, testcase{"foo\\.txt", "foo.txt", 0, true})
  72. testcases = append(testcases, testcase{"foo\\*.txt", "foo*.txt", 0, true})
  73. testcases = append(testcases, testcase{"foo\\.txt", "foo.txt", NoEscape, false})
  74. testcases = append(testcases, testcase{"f\\\\\\[ab\\\\\\]o.txt", "f\\[ab\\]o.txt", 0, true})
  75. }
  76. for _, tc := range testcases {
  77. if m, err := Match(tc.pat, filepath.FromSlash(tc.name), tc.flags); m != tc.match {
  78. if err != nil {
  79. t.Error(err)
  80. } else {
  81. t.Errorf("Match(%q, %q, %d) != %v", tc.pat, tc.name, tc.flags, tc.match)
  82. }
  83. }
  84. }
  85. }
  86. func TestInvalid(t *testing.T) {
  87. if _, err := Match("foo[bar", "...", 0); err == nil {
  88. t.Error("Unexpected nil error")
  89. }
  90. }