glob_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 2019 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "testing"
  17. )
  18. func TestMatch(t *testing.T) {
  19. testCases := []struct {
  20. pattern, name string
  21. match bool
  22. }{
  23. {"a/*", "b/", false},
  24. {"a/*", "b/a", false},
  25. {"a/*", "b/b/", false},
  26. {"a/*", "b/b/c", false},
  27. {"a/**/*", "b/", false},
  28. {"a/**/*", "b/a", false},
  29. {"a/**/*", "b/b/", false},
  30. {"a/**/*", "b/b/c", false},
  31. {"a/*", "a/", false},
  32. {"a/*", "a/a", true},
  33. {"a/*", "a/b/", false},
  34. {"a/*", "a/b/c", false},
  35. {"a/*/", "a/", false},
  36. {"a/*/", "a/a", false},
  37. {"a/*/", "a/b/", true},
  38. {"a/*/", "a/b/c", false},
  39. {"a/**/*", "a/", false},
  40. {"a/**/*", "a/a", true},
  41. {"a/**/*", "a/b/", false},
  42. {"a/**/*", "a/b/c", true},
  43. {"a/**/*/", "a/", false},
  44. {"a/**/*/", "a/a", false},
  45. {"a/**/*/", "a/b/", true},
  46. {"a/**/*/", "a/b/c", false},
  47. {"**/*", "a/", false},
  48. {"**/*", "a/a", true},
  49. {"**/*", "a/b/", false},
  50. {"**/*", "a/b/c", true},
  51. {"**/*/", "a/", true},
  52. {"**/*/", "a/a", false},
  53. {"**/*/", "a/b/", true},
  54. {"**/*/", "a/b/c", false},
  55. {`a/\*\*/\*`, `a/**/*`, true},
  56. {`a/\*\*/\*`, `a/a/*`, false},
  57. {`a/\*\*/\*`, `a/**/a`, false},
  58. {`a/\*\*/\*`, `a/a/a`, false},
  59. {`a/**/\*`, `a/**/*`, true},
  60. {`a/**/\*`, `a/a/*`, true},
  61. {`a/**/\*`, `a/**/a`, false},
  62. {`a/**/\*`, `a/a/a`, false},
  63. {`a/\*\*/*`, `a/**/*`, true},
  64. {`a/\*\*/*`, `a/a/*`, false},
  65. {`a/\*\*/*`, `a/**/a`, true},
  66. {`a/\*\*/*`, `a/a/a`, false},
  67. {`*/**/a`, `a/a/a`, true},
  68. {`*/**/a`, `*/a/a`, true},
  69. {`*/**/a`, `a/**/a`, true},
  70. {`*/**/a`, `*/**/a`, true},
  71. {`\*/\*\*/a`, `a/a/a`, false},
  72. {`\*/\*\*/a`, `*/a/a`, false},
  73. {`\*/\*\*/a`, `a/**/a`, false},
  74. {`\*/\*\*/a`, `*/**/a`, true},
  75. {`a/?`, `a/?`, true},
  76. {`a/?`, `a/a`, true},
  77. {`a/\?`, `a/?`, true},
  78. {`a/\?`, `a/a`, false},
  79. {`a/?`, `a/?`, true},
  80. {`a/?`, `a/a`, true},
  81. {`a/\?`, `a/?`, true},
  82. {`a/\?`, `a/a`, false},
  83. {`a/[a-c]`, `a/b`, true},
  84. {`a/[abc]`, `a/b`, true},
  85. {`a/\[abc]`, `a/b`, false},
  86. {`a/\[abc]`, `a/[abc]`, true},
  87. {`a/\[abc\]`, `a/b`, false},
  88. {`a/\[abc\]`, `a/[abc]`, true},
  89. {`a/?`, `a/?`, true},
  90. {`a/?`, `a/a`, true},
  91. {`a/\?`, `a/?`, true},
  92. {`a/\?`, `a/a`, false},
  93. {"/a/*", "/a/", false},
  94. {"/a/*", "/a/a", true},
  95. {"/a/*", "/a/b/", false},
  96. {"/a/*", "/a/b/c", false},
  97. {"/a/*/", "/a/", false},
  98. {"/a/*/", "/a/a", false},
  99. {"/a/*/", "/a/b/", true},
  100. {"/a/*/", "/a/b/c", false},
  101. {"/a/**/*", "/a/", false},
  102. {"/a/**/*", "/a/a", true},
  103. {"/a/**/*", "/a/b/", false},
  104. {"/a/**/*", "/a/b/c", true},
  105. {"/**/*", "/a/", false},
  106. {"/**/*", "/a/a", true},
  107. {"/**/*", "/a/b/", false},
  108. {"/**/*", "/a/b/c", true},
  109. {"/**/*/", "/a/", true},
  110. {"/**/*/", "/a/a", false},
  111. {"/**/*/", "/a/b/", true},
  112. {"/**/*/", "/a/b/c", false},
  113. {`a`, `/a`, false},
  114. {`/a`, `a`, false},
  115. {`*`, `/a`, false},
  116. {`/*`, `a`, false},
  117. {`**/*`, `/a`, false},
  118. {`/**/*`, `a`, false},
  119. }
  120. for _, test := range testCases {
  121. t.Run(test.pattern+","+test.name, func(t *testing.T) {
  122. match, err := Match(test.pattern, test.name)
  123. if err != nil {
  124. t.Fatal(err)
  125. }
  126. if match != test.match {
  127. t.Errorf("want: %v, got %v", test.match, match)
  128. }
  129. })
  130. }
  131. }