test.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. var testStr = "\\/$^+?.()=!|{},[].*"
  40. assertMatch(testStr, testStr);
  41. assertMatch(testStr, testStr, { flags: 'g' });
  42. // Equivalent matches without/with using RegExp 'g'
  43. assertNotMatch(".min.", "http://example.com/jquery.min.js");
  44. assertMatch("*.min.*", "http://example.com/jquery.min.js");
  45. assertMatch(".min.", "http://example.com/jquery.min.js", { flags: 'g' });
  46. assertNotMatch("http:", "http://example.com/jquery.min.js");
  47. assertMatch("http:*", "http://example.com/jquery.min.js");
  48. assertMatch("http:", "http://example.com/jquery.min.js", { flags: 'g' });
  49. assertNotMatch("min.js", "http://example.com/jquery.min.js");
  50. assertMatch("*.min.js", "http://example.com/jquery.min.js");
  51. assertMatch("min.js", "http://example.com/jquery.min.js", { flags: 'g' });
  52. // Match anywhere (globally) using RegExp 'g'
  53. assertMatch("min", "http://example.com/jquery.min.js", { flags: 'g' });
  54. assertMatch("/js/", "http://example.com/js/jquery.min.js", { flags: 'g' });
  55. assertNotMatch("/js*jq*.js", "http://example.com/js/jquery.min.js");
  56. assertMatch("/js*jq*.js", "http://example.com/js/jquery.min.js", { flags: 'g' });
  57. // Extended mode
  58. // ?: Match one character, no more and no less
  59. assertMatch("f?o", "foo", { extended: true });
  60. assertNotMatch("f?o", "fooo", { extended: true });
  61. assertNotMatch("f?oo", "foo", { extended: true });
  62. // ?: Match one character with RegExp 'g'
  63. assertMatch("f?o", "foo", { extended: true, globstar: globstar, flags: 'g' });
  64. assertMatch("f?o", "fooo", { extended: true, globstar: globstar, flags: 'g' });
  65. assertMatch("f?o?", "fooo", { extended: true, globstar: globstar, flags: 'g' });
  66. assertNotMatch("?fo", "fooo", { extended: true, globstar: globstar, flags: 'g' });
  67. assertNotMatch("f?oo", "foo", { extended: true, globstar: globstar, flags: 'g' });
  68. assertNotMatch("foo?", "foo", { extended: true, globstar: globstar, flags: 'g' });
  69. // []: Match a character range
  70. assertMatch("fo[oz]", "foo", { extended: true });
  71. assertMatch("fo[oz]", "foz", { extended: true });
  72. assertNotMatch("fo[oz]", "fog", { extended: true });
  73. // []: Match a character range and RegExp 'g' (regresion)
  74. assertMatch("fo[oz]", "foo", { extended: true, globstar: globstar, flags: 'g' });
  75. assertMatch("fo[oz]", "foz", { extended: true, globstar: globstar, flags: 'g' });
  76. assertNotMatch("fo[oz]", "fog", { extended: true, globstar: globstar, flags: 'g' });
  77. // {}: Match a choice of different substrings
  78. assertMatch("foo{bar,baaz}", "foobaaz", { extended: true });
  79. assertMatch("foo{bar,baaz}", "foobar", { extended: true });
  80. assertNotMatch("foo{bar,baaz}", "foobuzz", { extended: true });
  81. assertMatch("foo{bar,b*z}", "foobuzz", { extended: true });
  82. // {}: Match a choice of different substrings and RegExp 'g' (regression)
  83. assertMatch("foo{bar,baaz}", "foobaaz", { extended: true, globstar: globstar, flags: 'g' });
  84. assertMatch("foo{bar,baaz}", "foobar", { extended: true, globstar: globstar, flags: 'g' });
  85. assertNotMatch("foo{bar,baaz}", "foobuzz", { extended: true, globstar: globstar, flags: 'g' });
  86. assertMatch("foo{bar,b*z}", "foobuzz", { extended: true, globstar: globstar, flags: 'g' });
  87. // More complex extended matches
  88. assertMatch("http://?o[oz].b*z.com/{*.js,*.html}",
  89. "http://foo.baaz.com/jquery.min.js",
  90. { extended: true });
  91. assertMatch("http://?o[oz].b*z.com/{*.js,*.html}",
  92. "http://moz.buzz.com/index.html",
  93. { extended: true });
  94. assertNotMatch("http://?o[oz].b*z.com/{*.js,*.html}",
  95. "http://moz.buzz.com/index.htm",
  96. { extended: true });
  97. assertNotMatch("http://?o[oz].b*z.com/{*.js,*.html}",
  98. "http://moz.bar.com/index.html",
  99. { extended: true });
  100. assertNotMatch("http://?o[oz].b*z.com/{*.js,*.html}",
  101. "http://flozz.buzz.com/index.html",
  102. { extended: true });
  103. // More complex extended matches and RegExp 'g' (regresion)
  104. assertMatch("http://?o[oz].b*z.com/{*.js,*.html}",
  105. "http://foo.baaz.com/jquery.min.js",
  106. { extended: true, globstar: globstar, flags: 'g' });
  107. assertMatch("http://?o[oz].b*z.com/{*.js,*.html}",
  108. "http://moz.buzz.com/index.html",
  109. { extended: true, globstar: globstar, flags: 'g' });
  110. assertNotMatch("http://?o[oz].b*z.com/{*.js,*.html}",
  111. "http://moz.buzz.com/index.htm",
  112. { extended: true, globstar: globstar, flags: 'g' });
  113. assertNotMatch("http://?o[oz].b*z.com/{*.js,*.html}",
  114. "http://moz.bar.com/index.html",
  115. { extended: true, globstar: globstar, flags: 'g' });
  116. assertNotMatch("http://?o[oz].b*z.com/{*.js,*.html}",
  117. "http://flozz.buzz.com/index.html",
  118. { extended: true, globstar: globstar, flags: 'g' });
  119. // globstar
  120. assertMatch("http://foo.com/**/{*.js,*.html}",
  121. "http://foo.com/bar/jquery.min.js",
  122. { extended: true, globstar: globstar, flags: 'g' });
  123. assertMatch("http://foo.com/**/{*.js,*.html}",
  124. "http://foo.com/bar/baz/jquery.min.js",
  125. { extended: true, globstar: globstar, flags: 'g' });
  126. assertMatch("http://foo.com/**",
  127. "http://foo.com/bar/baz/jquery.min.js",
  128. { extended: true, globstar: globstar, flags: 'g' });
  129. // Remaining special chars should still match themselves
  130. var testExtStr = "\\/$^+.()=!|,.*"
  131. assertMatch(testExtStr, testExtStr, { extended: true });
  132. assertMatch(testExtStr, testExtStr, { extended: true, globstar: globstar, flags: 'g' });
  133. }
  134. // regression
  135. // globstar false
  136. test(false)
  137. // globstar true
  138. test(true);
  139. // globstar specific tests
  140. assertMatch("/foo/*", "/foo/bar.txt", {globstar: true });
  141. assertMatch("/foo/**", "/foo/baz.txt", {globstar: true });
  142. assertMatch("/foo/**", "/foo/bar/baz.txt", {globstar: true });
  143. assertMatch("/foo/*/*.txt", "/foo/bar/baz.txt", {globstar: true });
  144. assertMatch("/foo/**/*.txt", "/foo/bar/baz.txt", {globstar: true });
  145. assertMatch("/foo/**/*.txt", "/foo/bar/baz/qux.txt", {globstar: true });
  146. assertMatch("/foo/**/bar.txt", "/foo/bar.txt", {globstar: true });
  147. assertMatch("/foo/**/**/bar.txt", "/foo/bar.txt", {globstar: true });
  148. assertMatch("/foo/**/*/baz.txt", "/foo/bar/baz.txt", {globstar: true });
  149. assertMatch("/foo/**/*.txt", "/foo/bar.txt", {globstar: true });
  150. assertMatch("/foo/**/**/*.txt", "/foo/bar.txt", {globstar: true });
  151. assertMatch("/foo/**/*/*.txt", "/foo/bar/baz.txt", {globstar: true });
  152. assertMatch("**/*.txt", "/foo/bar/baz/qux.txt", {globstar: true });
  153. assertMatch("**/foo.txt", "foo.txt", {globstar: true });
  154. assertMatch("**/*.txt", "foo.txt", {globstar: true });
  155. assertNotMatch("/foo/*", "/foo/bar/baz.txt", {globstar: true });
  156. assertNotMatch("/foo/*.txt", "/foo/bar/baz.txt", {globstar: true });
  157. assertNotMatch("/foo/*/*.txt", "/foo/bar/baz/qux.txt", {globstar: true });
  158. assertNotMatch("/foo/*/bar.txt", "/foo/bar.txt", {globstar: true });
  159. assertNotMatch("/foo/*/*/baz.txt", "/foo/bar/baz.txt", {globstar: true });
  160. assertNotMatch("/foo/**.txt", "/foo/bar/baz/qux.txt", {globstar: true });
  161. assertNotMatch("/foo/bar**/*.txt", "/foo/bar/baz/qux.txt", {globstar: true });
  162. assertNotMatch("/foo/bar**", "/foo/bar/baz.txt", {globstar: true });
  163. assertNotMatch("**/.txt", "/foo/bar/baz/qux.txt", {globstar: true });
  164. assertNotMatch("*/*.txt", "/foo/bar/baz/qux.txt", {globstar: true });
  165. assertNotMatch("*/*.txt", "foo.txt", {globstar: true });
  166. assertNotMatch("http://foo.com/*",
  167. "http://foo.com/bar/baz/jquery.min.js",
  168. { extended: true, globstar: true });
  169. assertNotMatch("http://foo.com/*",
  170. "http://foo.com/bar/baz/jquery.min.js",
  171. { globstar: true });
  172. assertMatch("http://foo.com/*",
  173. "http://foo.com/bar/baz/jquery.min.js",
  174. { globstar: false });
  175. assertMatch("http://foo.com/**",
  176. "http://foo.com/bar/baz/jquery.min.js",
  177. { globstar: true });
  178. assertMatch("http://foo.com/*/*/jquery.min.js",
  179. "http://foo.com/bar/baz/jquery.min.js",
  180. { globstar: true });
  181. assertMatch("http://foo.com/**/jquery.min.js",
  182. "http://foo.com/bar/baz/jquery.min.js",
  183. { globstar: true });
  184. assertMatch("http://foo.com/*/*/jquery.min.js",
  185. "http://foo.com/bar/baz/jquery.min.js",
  186. { globstar: false });
  187. assertMatch("http://foo.com/*/jquery.min.js",
  188. "http://foo.com/bar/baz/jquery.min.js",
  189. { globstar: false });
  190. assertNotMatch("http://foo.com/*/jquery.min.js",
  191. "http://foo.com/bar/baz/jquery.min.js",
  192. { globstar: true });
  193. console.log("Ok!");