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