test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. var globToRegexp = require("./index.js");
  2. var assert = require("assert");
  3. function assertMatch(glob, str, opts) {
  4. //console.log(glob, globToRegexp(glob, opts));
  5. assert.ok(globToRegexp(glob, opts).test(str));
  6. }
  7. function assertNotMatch(glob, str, opts) {
  8. //console.log(glob, globToRegexp(glob, opts));
  9. assert.equal(false, globToRegexp(glob, opts).test(str));
  10. }
  11. function test(globstar) {
  12. // Match everything
  13. assertMatch("*", "foo");
  14. assertMatch("*", "foo", { flags: 'g' });
  15. // Match the end
  16. assertMatch("f*", "foo");
  17. assertMatch("f*", "foo", { flags: 'g' });
  18. // Match the start
  19. assertMatch("*o", "foo");
  20. assertMatch("*o", "foo", { flags: 'g' });
  21. // Match the middle
  22. assertMatch("f*uck", "firetruck");
  23. assertMatch("f*uck", "firetruck", { flags: 'g' });
  24. // Don't match without Regexp 'g'
  25. assertNotMatch("uc", "firetruck");
  26. // Match anywhere with RegExp 'g'
  27. assertMatch("uc", "firetruck", { flags: 'g' });
  28. // Match zero characters
  29. assertMatch("f*uck", "fuck");
  30. assertMatch("f*uck", "fuck", { flags: 'g' });
  31. // More complex matches
  32. assertMatch("*.min.js", "http://example.com/jquery.min.js", {globstar: false});
  33. assertMatch("*.min.*", "http://example.com/jquery.min.js", {globstar: false});
  34. assertMatch("*/js/*.js", "http://example.com/js/jquery.min.js", {globstar: false});
  35. // More complex matches with RegExp 'g' flag (complex regression)
  36. assertMatch("*.min.*", "http://example.com/jquery.min.js", { flags: 'g' });
  37. assertMatch("*.min.js", "http://example.com/jquery.min.js", { flags: 'g' });
  38. assertMatch("*/js/*.js", "http://example.com/js/jquery.min.js", { flags: 'g' });
  39. // Test string "\\\\/$^+?.()=!|{},[].*" represents <glob>\\/$^+?.()=!|{},[].*</glob>
  40. // The equivalent regex is: /^\\\/\$\^\+\?\.\(\)\=\!\|\{\}\,\[\]\..*$/
  41. // Both glob and regex match: \/$^+?.()=!|{},[].*
  42. var testStr = "\\\\/$^+?.()=!|{},[].*";
  43. var targetStr = "\\/$^+?.()=!|{},[].*";
  44. assertMatch(testStr, targetStr);
  45. assertMatch(testStr, targetStr, { flags: 'g' });
  46. // Equivalent matches without/with using RegExp 'g'
  47. assertNotMatch(".min.", "http://example.com/jquery.min.js");
  48. assertMatch("*.min.*", "http://example.com/jquery.min.js");
  49. assertMatch(".min.", "http://example.com/jquery.min.js", { flags: 'g' });
  50. assertNotMatch("http:", "http://example.com/jquery.min.js");
  51. assertMatch("http:*", "http://example.com/jquery.min.js");
  52. assertMatch("http:", "http://example.com/jquery.min.js", { flags: 'g' });
  53. assertNotMatch("min.js", "http://example.com/jquery.min.js");
  54. assertMatch("*.min.js", "http://example.com/jquery.min.js");
  55. assertMatch("min.js", "http://example.com/jquery.min.js", { flags: 'g' });
  56. // Match anywhere (globally) using RegExp 'g'
  57. assertMatch("min", "http://example.com/jquery.min.js", { flags: 'g' });
  58. assertMatch("/js/", "http://example.com/js/jquery.min.js", { flags: 'g' });
  59. assertNotMatch("/js*jq*.js", "http://example.com/js/jquery.min.js");
  60. assertMatch("/js*jq*.js", "http://example.com/js/jquery.min.js", { flags: 'g' });
  61. // Extended mode
  62. // ?: Match one character, no more and no less
  63. assertMatch("f?o", "foo", { extended: true });
  64. assertNotMatch("f?o", "fooo", { extended: true });
  65. assertNotMatch("f?oo", "foo", { extended: true });
  66. // ?: Match one character with RegExp 'g'
  67. assertMatch("f?o", "foo", { extended: true, globstar: globstar, flags: 'g' });
  68. assertMatch("f?o", "fooo", { extended: true, globstar: globstar, flags: 'g' });
  69. assertMatch("f?o?", "fooo", { extended: true, globstar: globstar, flags: 'g' });
  70. assertNotMatch("?fo", "fooo", { extended: true, globstar: globstar, flags: 'g' });
  71. assertNotMatch("f?oo", "foo", { extended: true, globstar: globstar, flags: 'g' });
  72. assertNotMatch("foo?", "foo", { extended: true, globstar: globstar, flags: 'g' });
  73. // []: Match a character range
  74. assertMatch("fo[oz]", "foo", { extended: true });
  75. assertMatch("fo[oz]", "foz", { extended: true });
  76. assertNotMatch("fo[oz]", "fog", { extended: true });
  77. // []: Match a character range and RegExp 'g' (regresion)
  78. assertMatch("fo[oz]", "foo", { extended: true, globstar: globstar, flags: 'g' });
  79. assertMatch("fo[oz]", "foz", { extended: true, globstar: globstar, flags: 'g' });
  80. assertNotMatch("fo[oz]", "fog", { extended: true, globstar: globstar, flags: 'g' });
  81. // {}: Match a choice of different substrings
  82. assertMatch("foo{bar,baaz}", "foobaaz", { extended: true });
  83. assertMatch("foo{bar,baaz}", "foobar", { extended: true });
  84. assertNotMatch("foo{bar,baaz}", "foobuzz", { extended: true });
  85. assertMatch("foo{bar,b*z}", "foobuzz", { extended: true });
  86. // {}: Match a choice of different substrings and RegExp 'g' (regression)
  87. assertMatch("foo{bar,baaz}", "foobaaz", { extended: true, globstar: globstar, flags: 'g' });
  88. assertMatch("foo{bar,baaz}", "foobar", { extended: true, globstar: globstar, flags: 'g' });
  89. assertNotMatch("foo{bar,baaz}", "foobuzz", { extended: true, globstar: globstar, flags: 'g' });
  90. assertMatch("foo{bar,b*z}", "foobuzz", { extended: true, globstar: globstar, flags: 'g' });
  91. // More complex extended matches
  92. assertMatch("http://?o[oz].b*z.com/{*.js,*.html}",
  93. "http://foo.baaz.com/jquery.min.js",
  94. { extended: true });
  95. assertMatch("http://?o[oz].b*z.com/{*.js,*.html}",
  96. "http://moz.buzz.com/index.html",
  97. { extended: true });
  98. assertNotMatch("http://?o[oz].b*z.com/{*.js,*.html}",
  99. "http://moz.buzz.com/index.htm",
  100. { extended: true });
  101. assertNotMatch("http://?o[oz].b*z.com/{*.js,*.html}",
  102. "http://moz.bar.com/index.html",
  103. { extended: true });
  104. assertNotMatch("http://?o[oz].b*z.com/{*.js,*.html}",
  105. "http://flozz.buzz.com/index.html",
  106. { extended: true });
  107. // More complex extended matches and RegExp 'g' (regresion)
  108. assertMatch("http://?o[oz].b*z.com/{*.js,*.html}",
  109. "http://foo.baaz.com/jquery.min.js",
  110. { extended: true, globstar: globstar, flags: 'g' });
  111. assertMatch("http://?o[oz].b*z.com/{*.js,*.html}",
  112. "http://moz.buzz.com/index.html",
  113. { extended: true, globstar: globstar, flags: 'g' });
  114. assertNotMatch("http://?o[oz].b*z.com/{*.js,*.html}",
  115. "http://moz.buzz.com/index.htm",
  116. { extended: true, globstar: globstar, flags: 'g' });
  117. assertNotMatch("http://?o[oz].b*z.com/{*.js,*.html}",
  118. "http://moz.bar.com/index.html",
  119. { extended: true, globstar: globstar, flags: 'g' });
  120. assertNotMatch("http://?o[oz].b*z.com/{*.js,*.html}",
  121. "http://flozz.buzz.com/index.html",
  122. { extended: true, globstar: globstar, flags: 'g' });
  123. // globstar
  124. assertMatch("http://foo.com/**/{*.js,*.html}",
  125. "http://foo.com/bar/jquery.min.js",
  126. { extended: true, globstar: globstar, flags: 'g' });
  127. assertMatch("http://foo.com/**/{*.js,*.html}",
  128. "http://foo.com/bar/baz/jquery.min.js",
  129. { extended: true, globstar: globstar, flags: 'g' });
  130. assertMatch("http://foo.com/**",
  131. "http://foo.com/bar/baz/jquery.min.js",
  132. { extended: true, globstar: globstar, flags: 'g' });
  133. // Remaining special chars should still match themselves
  134. // Test string "\\\\/$^+.()=!|,.*" represents <glob>\\/$^+.()=!|,.*</glob>
  135. // The equivalent regex is: /^\\\/\$\^\+\.\(\)\=\!\|\,\..*$/
  136. // Both glob and regex match: \/$^+.()=!|,.*
  137. var testExtStr = "\\\\/$^+.()=!|,.*";
  138. var targetExtStr = "\\/$^+.()=!|,.*";
  139. assertMatch(testExtStr, targetExtStr, { extended: true });
  140. assertMatch(testExtStr, targetExtStr, { extended: true, globstar: globstar, flags: 'g' });
  141. }
  142. // regression
  143. // globstar false
  144. test(false)
  145. // globstar true
  146. test(true);
  147. // globstar specific tests
  148. assertMatch("/foo/*", "/foo/bar.txt", {globstar: true });
  149. assertMatch("/foo/**", "/foo/baz.txt", {globstar: true });
  150. assertMatch("/foo/**", "/foo/bar/baz.txt", {globstar: true });
  151. assertMatch("/foo/*/*.txt", "/foo/bar/baz.txt", {globstar: true });
  152. assertMatch("/foo/**/*.txt", "/foo/bar/baz.txt", {globstar: true });
  153. assertMatch("/foo/**/*.txt", "/foo/bar/baz/qux.txt", {globstar: true });
  154. assertMatch("/foo/**/bar.txt", "/foo/bar.txt", {globstar: true });
  155. assertMatch("/foo/**/**/bar.txt", "/foo/bar.txt", {globstar: true });
  156. assertMatch("/foo/**/*/baz.txt", "/foo/bar/baz.txt", {globstar: true });
  157. assertMatch("/foo/**/*.txt", "/foo/bar.txt", {globstar: true });
  158. assertMatch("/foo/**/**/*.txt", "/foo/bar.txt", {globstar: true });
  159. assertMatch("/foo/**/*/*.txt", "/foo/bar/baz.txt", {globstar: true });
  160. assertMatch("**/*.txt", "/foo/bar/baz/qux.txt", {globstar: true });
  161. assertMatch("**/foo.txt", "foo.txt", {globstar: true });
  162. assertMatch("**/*.txt", "foo.txt", {globstar: true });
  163. assertNotMatch("/foo/*", "/foo/bar/baz.txt", {globstar: true });
  164. assertNotMatch("/foo/*.txt", "/foo/bar/baz.txt", {globstar: true });
  165. assertNotMatch("/foo/*/*.txt", "/foo/bar/baz/qux.txt", {globstar: true });
  166. assertNotMatch("/foo/*/bar.txt", "/foo/bar.txt", {globstar: true });
  167. assertNotMatch("/foo/*/*/baz.txt", "/foo/bar/baz.txt", {globstar: true });
  168. assertNotMatch("/foo/**.txt", "/foo/bar/baz/qux.txt", {globstar: true });
  169. assertNotMatch("/foo/bar**/*.txt", "/foo/bar/baz/qux.txt", {globstar: true });
  170. assertNotMatch("/foo/bar**", "/foo/bar/baz.txt", {globstar: true });
  171. assertNotMatch("**/.txt", "/foo/bar/baz/qux.txt", {globstar: true });
  172. assertNotMatch("*/*.txt", "/foo/bar/baz/qux.txt", {globstar: true });
  173. assertNotMatch("*/*.txt", "foo.txt", {globstar: true });
  174. assertNotMatch("http://foo.com/*",
  175. "http://foo.com/bar/baz/jquery.min.js",
  176. { extended: true, globstar: true });
  177. assertNotMatch("http://foo.com/*",
  178. "http://foo.com/bar/baz/jquery.min.js",
  179. { globstar: true });
  180. assertMatch("http://foo.com/*",
  181. "http://foo.com/bar/baz/jquery.min.js",
  182. { globstar: false });
  183. assertMatch("http://foo.com/**",
  184. "http://foo.com/bar/baz/jquery.min.js",
  185. { globstar: true });
  186. assertMatch("http://foo.com/*/*/jquery.min.js",
  187. "http://foo.com/bar/baz/jquery.min.js",
  188. { globstar: true });
  189. assertMatch("http://foo.com/**/jquery.min.js",
  190. "http://foo.com/bar/baz/jquery.min.js",
  191. { globstar: true });
  192. assertMatch("http://foo.com/*/*/jquery.min.js",
  193. "http://foo.com/bar/baz/jquery.min.js",
  194. { globstar: false });
  195. assertMatch("http://foo.com/*/jquery.min.js",
  196. "http://foo.com/bar/baz/jquery.min.js",
  197. { globstar: false });
  198. assertNotMatch("http://foo.com/*/jquery.min.js",
  199. "http://foo.com/bar/baz/jquery.min.js",
  200. { globstar: true });
  201. console.log("Ok!");