| 1 |
const fs = require('fs'); |
| 2 |
const test = require('tape'); |
| 3 |
const globrex = require('../'); |
| 4 |
const {match, matchRegex, matchSegments} = require('./helpers'); |
| 5 |
|
| 6 |
test('globrex: standard', t => { |
| 7 |
t.plan(3); |
| 8 |
let res = globrex('*.js'); |
| 9 |
t.equal(typeof globrex, 'function', 'consturctor is a typeof function'); |
| 10 |
t.equal(res instanceof Object, true, 'returns object'); |
| 11 |
t.equal(res.regex.toString(), '/^.*\\.js$/', 'returns regex object'); |
| 12 |
}); |
| 13 |
|
| 14 |
test('globrex: Standard * matching', t => { |
| 15 |
t.plan(12); |
| 16 |
t.equal(match('*', 'foo'), true, 'match everything'); |
| 17 |
t.equal(match('*', 'foo', { flags:'g' }), true, 'match everything'); |
| 18 |
t.equal(match('f*', 'foo'), true, 'match the end'); |
| 19 |
t.equal(match('f*', 'foo', { flags:'g' }), true, 'match the end'); |
| 20 |
t.equal(match('*o', 'foo'), true, 'match the start'); |
| 21 |
t.equal(match('*o', 'foo', { flags:'g' }), true, 'match the start'); |
| 22 |
t.equal(match('f*uck', 'firetruck'), true, 'match the middle'); |
| 23 |
t.equal(match('f*uck', 'firetruck', { flags:'g' }), true, 'match the middle'); |
| 24 |
t.equal(match('uc', 'firetruck'), false, 'do not match without g'); |
| 25 |
t.equal(match('uc', 'firetruck', { flags:'g' }), true, 'match anywhere with RegExp "g"'); |
| 26 |
t.equal(match('f*uck', 'fuck'), true, 'match zero characters'); |
| 27 |
t.equal(match('f*uck', 'fuck', { flags:'g' }), true, 'match zero characters'); |
| 28 |
}); |
| 29 |
|
| 30 |
test('globrex: advance * matching', t => { |
| 31 |
t.plan(21); |
| 32 |
t.equal(match('*.min.js', 'http://example.com/jquery.min.js', { globstar:false }), true, 'complex match'); |
| 33 |
t.equal(match('*.min.*', 'http://example.com/jquery.min.js', { globstar:false }), true, 'complex match'); |
| 34 |
t.equal(match('*/js/*.js', 'http://example.com/js/jquery.min.js', { globstar:false }), true, 'complex match'); |
| 35 |
t.equal(match('*.min.*', 'http://example.com/jquery.min.js', { flags:'g' }), true, 'complex match global'); |
| 36 |
t.equal(match('*.min.js', 'http://example.com/jquery.min.js', { flags:'g' }), true, 'complex match global'); |
| 37 |
t.equal(match('*/js/*.js', 'http://example.com/js/jquery.min.js', { flags:'g' }), true, 'complex match global'); |
| 38 |
|
| 39 |
const str = '\\/$^+?.()=!|{},[].*'; |
| 40 |
t.equal(match(str, str), true, 'battle test complex string - strict'); |
| 41 |
t.equal(match(str, str, { flags:'g' }), true, 'battle test complex string - strict'); |
| 42 |
|
| 43 |
t.equal(match('.min.', 'http://example.com/jquery.min.js'), false, 'matches without/with using RegExp "g"'); |
| 44 |
t.equal(match('*.min.*', 'http://example.com/jquery.min.js'), true, 'matches without/with using RegExp "g"'); |
| 45 |
t.equal(match('.min.', 'http://example.com/jquery.min.js', { flags:'g' }), true, 'matches without/with using RegExp "g"'); |
| 46 |
t.equal(match('http:', 'http://example.com/jquery.min.js'), false, 'matches without/with using RegExp "g"'); |
| 47 |
t.equal(match('http:*', 'http://example.com/jquery.min.js'), true, 'matches without/with using RegExp "g"'); |
| 48 |
t.equal(match('http:', 'http://example.com/jquery.min.js', { flags:'g' }), true, 'matches without/with using RegExp "g"'); |
| 49 |
t.equal(match('min.js', 'http://example.com/jquery.min.js'), false, 'matches without/with using RegExp "g"'); |
| 50 |
t.equal(match('*.min.js', 'http://example.com/jquery.min.js'), true, 'matches without/with using RegExp "g"'); |
| 51 |
t.equal(match('min.js', 'http://example.com/jquery.min.js', { flags:'g' }), true, 'matches without/with using RegExp "g"'); |
| 52 |
t.equal(match('min', 'http://example.com/jquery.min.js', { flags:'g' }), true, 'match anywhere (globally) using RegExp "g"'); |
| 53 |
t.equal(match('/js/', 'http://example.com/js/jquery.min.js', { flags:'g' }), true, 'match anywhere (globally) using RegExp "g"'); |
| 54 |
t.equal(match('/js*jq*.js', 'http://example.com/js/jquery.min.js'), false); |
| 55 |
t.equal(match('/js*jq*.js', 'http://example.com/js/jquery.min.js', { flags:'g' }), true); |
| 56 |
}); |
| 57 |
|
| 58 |
test('globrex: ? match one character, no more and no less', t => { |
| 59 |
t.plan(15); |
| 60 |
t.equal(match('f?o', 'foo', { extended:true }), true) |
| 61 |
t.equal(match('f?o', 'fooo', { extended:true }), false) |
| 62 |
t.equal(match('f?oo', 'foo', { extended:true }), false) |
| 63 |
|
| 64 |
const tester = globstar => { |
| 65 |
t.equal(match('f?o', 'foo', { extended:true, globstar, flags:'g' }), true) |
| 66 |
t.equal(match('f?o', 'fooo', { extended:true, globstar, flags:'g' }), true) |
| 67 |
t.equal(match('f?o?', 'fooo', { extended:true, globstar, flags:'g' }), true) |
| 68 |
|
| 69 |
t.equal(match('?fo', 'fooo', { extended:true, globstar, flags:'g' }), false) |
| 70 |
t.equal(match('f?oo', 'foo', { extended:true, globstar, flags:'g' }), false) |
| 71 |
t.equal(match('foo?', 'foo', { extended:true, globstar, flags:'g' }), false) |
| 72 |
} |
| 73 |
|
| 74 |
tester(true); |
| 75 |
tester(false); |
| 76 |
}); |
| 77 |
|
| 78 |
test('globrex: [] match a character range', t => { |
| 79 |
t.plan(13); |
| 80 |
t.equal(match('fo[oz]', 'foo', { extended:true }), true); |
| 81 |
t.equal(match('fo[oz]', 'foz', { extended:true }), true); |
| 82 |
t.equal(match('fo[oz]', 'fog', { extended:true }), false); |
| 83 |
t.equal(match('fo[a-z]', 'fob', { extended:true }), true); |
| 84 |
t.equal(match('fo[a-d]', 'fot', { extended:true }), false); |
| 85 |
t.equal(match('fo[!tz]', 'fot', { extended:true }), false); |
| 86 |
t.equal(match('fo[!tz]', 'fob', { extended:true }), true); |
| 87 |
|
| 88 |
const tester = globstar => { |
| 89 |
t.equal(match('fo[oz]', 'foo', { extended:true, globstar, flags:'g' }), true); |
| 90 |
t.equal(match('fo[oz]', 'foz', { extended:true, globstar, flags:'g' }), true); |
| 91 |
t.equal(match('fo[oz]', 'fog', { extended:true, globstar, flags:'g' }), false); |
| 92 |
} |
| 93 |
|
| 94 |
tester(true); |
| 95 |
tester(false); |
| 96 |
}) |
| 97 |
|
| 98 |
test('globrex: [] extended character ranges', t => { |
| 99 |
t.plan(13); |
| 100 |
t.equal(match('[[:alnum:]]/bar.txt', 'a/bar.txt', { extended:true }), true); |
| 101 |
t.equal(match('@([[:alnum:]abc]|11)/bar.txt', '11/bar.txt', { extended:true }), true); |
| 102 |
t.equal(match('@([[:alnum:]abc]|11)/bar.txt', 'a/bar.txt', { extended:true }), true); |
| 103 |
t.equal(match('@([[:alnum:]abc]|11)/bar.txt', 'b/bar.txt', { extended:true }), true); |
| 104 |
t.equal(match('@([[:alnum:]abc]|11)/bar.txt', 'c/bar.txt', { extended:true }), true); |
| 105 |
t.equal(match('@([[:alnum:]abc]|11)/bar.txt', 'abc/bar.txt', { extended:true }), false); |
| 106 |
t.equal(match('@([[:alnum:]abc]|11)/bar.txt', '3/bar.txt', { extended:true }), true); |
| 107 |
t.equal(match('[[:digit:]]/bar.txt', '1/bar.txt', { extended:true }), true); |
| 108 |
t.equal(match('[[:digit:]b]/bar.txt', 'b/bar.txt', { extended:true }), true); |
| 109 |
t.equal(match('[![:digit:]b]/bar.txt', 'a/bar.txt', { extended:true }), true); |
| 110 |
t.equal(match('[[:alnum:]]/bar.txt', '!/bar.txt', { extended:true }), false); |
| 111 |
t.equal(match('[[:digit:]]/bar.txt', 'a/bar.txt', { extended:true }), false); |
| 112 |
t.equal(match('[[:digit:]b]/bar.txt', 'a/bar.txt', { extended:true }), false); |
| 113 |
}); |
| 114 |
|
| 115 |
test('globrex: {} match a choice of different substrings', t => { |
| 116 |
t.plan(12); |
| 117 |
t.equal(match('foo{bar,baaz}', 'foobaaz', { extended:true }), true); |
| 118 |
t.equal(match('foo{bar,baaz}', 'foobar', { extended:true }), true); |
| 119 |
t.equal(match('foo{bar,baaz}', 'foobuzz', { extended:true }), false); |
| 120 |
t.equal(match('foo{bar,b*z}', 'foobuzz', { extended:true }), true); |
| 121 |
|
| 122 |
const tester = globstar => { |
| 123 |
t.equal(match('foo{bar,baaz}', 'foobaaz', { extended:true, globstar, flag:'g' }), true); |
| 124 |
t.equal(match('foo{bar,baaz}', 'foobar', { extended:true, globstar, flag:'g' }), true); |
| 125 |
t.equal(match('foo{bar,baaz}', 'foobuzz', { extended:true, globstar, flag:'g' }), false); |
| 126 |
t.equal(match('foo{bar,b*z}', 'foobuzz', { extended:true, globstar, flag:'g'}), true); |
| 127 |
} |
| 128 |
|
| 129 |
tester(true); |
| 130 |
tester(false); |
| 131 |
}); |
| 132 |
|
| 133 |
test('globrex: complex extended matches', t => { |
| 134 |
t.plan(15) |
| 135 |
t.equal(match('http://?o[oz].b*z.com/{*.js,*.html}', 'http://foo.baaz.com/jquery.min.js', { extended:true }), true); |
| 136 |
t.equal(match('http://?o[oz].b*z.com/{*.js,*.html}', 'http://moz.buzz.com/index.html', { extended:true }), true); |
| 137 |
t.equal(match('http://?o[oz].b*z.com/{*.js,*.html}', 'http://moz.buzz.com/index.htm', { extended:true }), false); |
| 138 |
t.equal(match('http://?o[oz].b*z.com/{*.js,*.html}', 'http://moz.bar.com/index.html', { extended:true }), false); |
| 139 |
t.equal(match('http://?o[oz].b*z.com/{*.js,*.html}', 'http://flozz.buzz.com/index.html', { extended:true }), false); |
| 140 |
|
| 141 |
const tester = globstar => { |
| 142 |
t.equal(match('http://?o[oz].b*z.com/{*.js,*.html}', 'http://foo.baaz.com/jquery.min.js', { extended:true, globstar, flags:'g' }), true); |
| 143 |
t.equal(match('http://?o[oz].b*z.com/{*.js,*.html}', 'http://moz.buzz.com/index.html', { extended:true, globstar, flags:'g' }), true); |
| 144 |
t.equal(match('http://?o[oz].b*z.com/{*.js,*.html}', 'http://moz.buzz.com/index.htm', { extended:true, globstar, flags:'g' }), false); |
| 145 |
t.equal(match('http://?o[oz].b*z.com/{*.js,*.html}', 'http://moz.bar.com/index.html', { extended:true, globstar, flags:'g' }), false); |
| 146 |
t.equal(match('http://?o[oz].b*z.com/{*.js,*.html}', 'http://flozz.buzz.com/index.html', { extended:true, globstar, flags:'g' }), false); |
| 147 |
} |
| 148 |
|
| 149 |
tester(true); |
| 150 |
tester(false); |
| 151 |
}); |
| 152 |
|
| 153 |
test('globrex: standard globstar', t => { |
| 154 |
t.plan(6); |
| 155 |
|
| 156 |
const tester = globstar => { |
| 157 |
t.equal(match('http://foo.com/**/{*.js,*.html}', 'http://foo.com/bar/jquery.min.js', { extended: true, globstar, flags: 'g' }), true); |
| 158 |
t.equal(match('http://foo.com/**/{*.js,*.html}', 'http://foo.com/bar/baz/jquery.min.js', { extended: true, globstar, flags: 'g' }), true); |
| 159 |
t.equal(match('http://foo.com/**', 'http://foo.com/bar/baz/jquery.min.js', { extended: true, globstar, flags: 'g' }), true); |
| 160 |
} |
| 161 |
|
| 162 |
tester(true); |
| 163 |
tester(false); |
| 164 |
}); |
| 165 |
|
| 166 |
test('globrex: remaining chars should match themself', t => { |
| 167 |
t.plan(4); |
| 168 |
|
| 169 |
const tester = globstar => { |
| 170 |
const testExtStr = '\\/$^+.()=!|,.*'; |
| 171 |
t.equal(match(testExtStr, testExtStr, { extended:true }), true); |
| 172 |
t.equal(match(testExtStr, testExtStr, { extended:true, globstar, flags: 'g' }), true); |
| 173 |
} |
| 174 |
|
| 175 |
tester(true); |
| 176 |
tester(false); |
| 177 |
}); |
| 178 |
|
| 179 |
test('globrex: globstar advance testing', t => { |
| 180 |
t.plan(36); |
| 181 |
t.equal(match('/foo/*', '/foo/bar.txt', { globstar:true }), true); |
| 182 |
t.equal(match('/foo/**', '/foo/bar.txt', { globstar:true }), true); |
| 183 |
t.equal(match('/foo/**', '/foo/bar/baz.txt', { globstar:true }), true); |
| 184 |
t.equal(match('/foo/**', '/foo/bar/baz.txt', { globstar:true }), true); |
| 185 |
t.equal(match('/foo/*/*.txt', '/foo/bar/baz.txt', { globstar:true }), true); |
| 186 |
t.equal(match('/foo/**/*.txt', '/foo/bar/baz.txt', { globstar:true }), true); |
| 187 |
t.equal(match('/foo/**/*.txt', '/foo/bar/baz/qux.txt', { globstar:true }), true); |
| 188 |
t.equal(match('/foo/**/bar.txt', '/foo/bar.txt', { globstar:true }), true); |
| 189 |
t.equal(match('/foo/**/**/bar.txt', '/foo/bar.txt', { globstar:true }), true); |
| 190 |
t.equal(match('/foo/**/*/baz.txt', '/foo/bar/baz.txt', { globstar:true }), true); |
| 191 |
t.equal(match('/foo/**/*.txt', '/foo/bar.txt', { globstar:true }), true); |
| 192 |
t.equal(match('/foo/**/**/*.txt', '/foo/bar.txt', { globstar:true }), true); |
| 193 |
t.equal(match('/foo/**/*/*.txt', '/foo/bar/baz.txt', { globstar:true }), true); |
| 194 |
t.equal(match('**/*.txt', '/foo/bar/baz/qux.txt', { globstar:true }), true); |
| 195 |
t.equal(match('**/foo.txt', 'foo.txt', { globstar:true }), true); |
| 196 |
t.equal(match('**/*.txt', 'foo.txt', { globstar:true }), true); |
| 197 |
t.equal(match('/foo/*', '/foo/bar/baz.txt', { globstar:true }), false); |
| 198 |
t.equal(match('/foo/*.txt', '/foo/bar/baz.txt', { globstar:true }), false); |
| 199 |
t.equal(match('/foo/*/*.txt', '/foo/bar/baz/qux.txt', { globstar:true }), false); |
| 200 |
t.equal(match('/foo/*/bar.txt', '/foo/bar.txt', { globstar:true }), false); |
| 201 |
t.equal(match('/foo/*/*/baz.txt', '/foo/bar/baz.txt', { globstar:true }), false); |
| 202 |
t.equal(match('/foo/**.txt', '/foo/bar/baz/qux.txt', { globstar:true }), false); |
| 203 |
t.equal(match('/foo/bar**/*.txt', '/foo/bar/baz/qux.txt', { globstar:true }), false); |
| 204 |
t.equal(match('/foo/bar**', '/foo/bar/baz.txt', { globstar:true }), false); |
| 205 |
t.equal(match('**/.txt', '/foo/bar/baz/qux.txt', { globstar:true }), false); |
| 206 |
t.equal(match('*/*.txt', '/foo/bar/baz/qux.txt', { globstar:true }), false); |
| 207 |
t.equal(match('*/*.txt', 'foo.txt', { globstar:true }), false); |
| 208 |
t.equal(match('http://foo.com/*', 'http://foo.com/bar/baz/jquery.min.js', { extended: true, globstar: true }), false); |
| 209 |
t.equal(match('http://foo.com/*', 'http://foo.com/bar/baz/jquery.min.js', { globstar: true }), false); |
| 210 |
t.equal(match('http://foo.com/*', 'http://foo.com/bar/baz/jquery.min.js', { globstar: false }), true); |
| 211 |
t.equal(match('http://foo.com/**', 'http://foo.com/bar/baz/jquery.min.js', { globstar: true }), true); |
| 212 |
t.equal(match("http://foo.com/*/*/jquery.min.js", "http://foo.com/bar/baz/jquery.min.js", { globstar: true }), true); |
| 213 |
t.equal(match("http://foo.com/**/jquery.min.js", "http://foo.com/bar/baz/jquery.min.js", { globstar: true }), true); |
| 214 |
t.equal(match("http://foo.com/*/*/jquery.min.js", "http://foo.com/bar/baz/jquery.min.js", { globstar: false }), true); |
| 215 |
t.equal(match("http://foo.com/*/jquery.min.js", "http://foo.com/bar/baz/jquery.min.js", { globstar: false }), true); |
| 216 |
t.equal(match("http://foo.com/*/jquery.min.js", "http://foo.com/bar/baz/jquery.min.js", { globstar: true }), false); |
| 217 |
}); |
| 218 |
|
| 219 |
test('globrex: extended extglob ?', t => { |
| 220 |
t.plan(17); |
| 221 |
t.equal(match('(foo).txt', '(foo).txt', { extended:true }), true); |
| 222 |
t.equal(match('?(foo).txt', 'foo.txt', { extended:true }), true); |
| 223 |
t.equal(match('?(foo).txt', '.txt', { extended:true }), true); |
| 224 |
t.equal(match('?(foo|bar)baz.txt', 'foobaz.txt', { extended:true }), true); |
| 225 |
t.equal(match('?(ba[zr]|qux)baz.txt', 'bazbaz.txt', { extended:true }), true); |
| 226 |
t.equal(match('?(ba[zr]|qux)baz.txt', 'barbaz.txt', { extended:true }), true); |
| 227 |
t.equal(match('?(ba[zr]|qux)baz.txt', 'quxbaz.txt', { extended:true }), true); |
| 228 |
t.equal(match('?(ba[!zr]|qux)baz.txt', 'batbaz.txt', { extended:true }), true); |
| 229 |
t.equal(match('?(ba*|qux)baz.txt', 'batbaz.txt', { extended:true }), true); |
| 230 |
t.equal(match('?(ba*|qux)baz.txt', 'batttbaz.txt', { extended:true }), true); |
| 231 |
t.equal(match('?(ba*|qux)baz.txt', 'quxbaz.txt', { extended:true }), true); |
| 232 |
t.equal(match('?(ba?(z|r)|qux)baz.txt', 'bazbaz.txt', { extended:true }), true); |
| 233 |
t.equal(match('?(ba?(z|?(r))|qux)baz.txt', 'bazbaz.txt', { extended:true }), true); |
| 234 |
t.equal(match('?(foo).txt', 'foo.txt', { extended:false }), false); |
| 235 |
t.equal(match('?(foo|bar)baz.txt', 'foobarbaz.txt', { extended:true }), false); |
| 236 |
t.equal(match('?(ba[zr]|qux)baz.txt', 'bazquxbaz.txt', { extended:true }), false); |
| 237 |
t.equal(match('?(ba[!zr]|qux)baz.txt', 'bazbaz.txt', { extended:true }), false); |
| 238 |
}); |
| 239 |
|
| 240 |
test('globrex: extended extglob *', t => { |
| 241 |
t.plan(16); |
| 242 |
t.equal(match('*(foo).txt', 'foo.txt', { extended:true }), true); |
| 243 |
t.equal(match('*foo.txt', 'bofoo.txt', { extended:true }), true); |
| 244 |
t.equal(match('*(foo).txt', 'foofoo.txt', { extended:true }), true); |
| 245 |
t.equal(match('*(foo).txt', '.txt', { extended:true }), true); |
| 246 |
t.equal(match('*(fooo).txt', '.txt', { extended:true }), true); |
| 247 |
t.equal(match('*(fooo).txt', 'foo.txt', { extended:true }), false); |
| 248 |
t.equal(match('*(foo|bar).txt', 'foobar.txt', { extended:true }), true); |
| 249 |
t.equal(match('*(foo|bar).txt', 'barbar.txt', { extended:true }), true); |
| 250 |
t.equal(match('*(foo|bar).txt', 'barfoobar.txt', { extended:true }), true); |
| 251 |
t.equal(match('*(foo|bar).txt', '.txt', { extended:true }), true); |
| 252 |
t.equal(match('*(foo|ba[rt]).txt', 'bat.txt', { extended:true }), true); |
| 253 |
t.equal(match('*(foo|b*[rt]).txt', 'blat.txt', { extended:true }), true); |
| 254 |
t.equal(match('*(foo|b*[rt]).txt', 'tlat.txt', { extended:true }), false); |
| 255 |
t.equal(match('*(*).txt', 'whatever.txt', { extended:true, globstar:true }), true); |
| 256 |
t.equal(match('*(foo|bar)/**/*.txt', 'foo/hello/world/bar.txt', { extended:true, globstar:true }), true); |
| 257 |
t.equal(match('*(foo|bar)/**/*.txt', 'foo/world/bar.txt', { extended:true, globstar: true }), true); |
| 258 |
}); |
| 259 |
|
| 260 |
test('globrex: extended extglob +', t => { |
| 261 |
t.plan(4); |
| 262 |
t.equal(match('+(foo).txt', 'foo.txt', { extended:true }), true); |
| 263 |
t.equal(match('+foo.txt', '+foo.txt', { extended:true }), true); |
| 264 |
t.equal(match('+(foo).txt', '.txt', { extended:true }), false); |
| 265 |
t.equal(match('+(foo|bar).txt', 'foobar.txt', { extended:true }), true); |
| 266 |
}); |
| 267 |
|
| 268 |
test('globrex: extended extglob @', t => { |
| 269 |
t.plan(6); |
| 270 |
t.equal(match('@(foo).txt', 'foo.txt', { extended:true }), true); |
| 271 |
t.equal(match('@foo.txt', '@foo.txt', { extended:true }), true); |
| 272 |
t.equal(match('@(foo|baz)bar.txt', 'foobar.txt', { extended:true }), true); |
| 273 |
t.equal(match('@(foo|baz)bar.txt', 'foobazbar.txt', { extended:true }), false); |
| 274 |
t.equal(match('@(foo|baz)bar.txt', 'foofoobar.txt', { extended:true }), false); |
| 275 |
t.equal(match('@(foo|baz)bar.txt', 'toofoobar.txt', { extended:true }), false); |
| 276 |
}); |
| 277 |
|
| 278 |
test('globrex: extended extglob !', t => { |
| 279 |
t.plan(5); |
| 280 |
t.equal(match('!(boo).txt', 'foo.txt', { extended: true }), true); |
| 281 |
t.equal(match('!(foo|baz)bar.txt', 'buzbar.txt', { extended: true }), true); |
| 282 |
t.equal(match('!bar.txt', '!bar.txt', { extended: true }), true); |
| 283 |
t.equal(match('!({foo,bar})baz.txt', 'notbaz.txt', { extended: true }), true); |
| 284 |
t.equal(match('!({foo,bar})baz.txt', 'foobaz.txt', { extended: true }), false); |
| 285 |
}); |
| 286 |
|
| 287 |
|
| 288 |
test('globrex: strict', t => { |
| 289 |
t.plan(3); |
| 290 |
t.equal(match('foo//bar.txt', 'foo/bar.txt'), true); |
| 291 |
t.equal(match('foo///bar.txt', 'foo/bar.txt'), true); |
| 292 |
t.equal(match('foo///bar.txt', 'foo/bar.txt', { strict:true }), false); |
| 293 |
}); |
| 294 |
|
| 295 |
|
| 296 |
test('globrex: filepath path-regex', t => { |
| 297 |
let opts = { extended:true, filepath:true }, res, pattern; |
| 298 |
|
| 299 |
res = globrex('', opts); |
| 300 |
t.is(res.hasOwnProperty('path'), true); |
| 301 |
t.is(res.path.hasOwnProperty('regex'), true); |
| 302 |
t.is(res.path.hasOwnProperty('segments'), true); |
| 303 |
t.is(Array.isArray(res.path.segments), true); |
| 304 |
|
| 305 |
pattern = 'foo/bar/baz.js'; |
| 306 |
res = matchRegex(t, pattern, '/^foo\\/bar\\/baz\\.js$/', '/^foo\\\\+bar\\\\+baz\\.js$/', opts); |
| 307 |
t.is(res.path.segments.length, 3); |
| 308 |
|
| 309 |
res = matchRegex(t, '../foo/bar.js', '/^\\.\\.\\/foo\\/bar\\.js$/', '/^\\.\\.\\\\+foo\\\\+bar\\.js$/', opts); |
| 310 |
t.is(res.path.segments.length, 3); |
| 311 |
|
| 312 |
res = matchRegex(t, '*/bar.js', '/^.*\\/bar\\.js$/', '/^.*\\\\+bar\\.js$/', opts); |
| 313 |
t.is(res.path.segments.length, 2); |
| 314 |
|
| 315 |
opts.globstar = true; |
| 316 |
res = matchRegex(t, '**/bar.js', '/^((?:[^\\/]*(?:\\/|$))*)bar\\.js$/', '/^((?:[^\\\\]*(?:\\\\|$))*)bar\\.js$/', opts); |
| 317 |
t.is(res.path.segments.length, 2); |
| 318 |
|
| 319 |
t.end(); |
| 320 |
}) |
| 321 |
|
| 322 |
test('globrex: filepath path segments', t => { |
| 323 |
let opts = { extended:true }, res, win, unix; |
| 324 |
|
| 325 |
unix = [/^foo$/, /^bar$/, /^([^\/]*)$/, /^baz\.(md|js|txt)$/]; |
| 326 |
win = [/^foo$/, /^bar$/, /^([^\\]*)$/, /^baz\.(md|js|txt)$/]; |
| 327 |
matchSegments(t, 'foo/bar/*/baz.{md,js,txt}', unix, win, {...opts, globstar:true}); |
| 328 |
|
| 329 |
unix = [/^foo$/, /^.*$/, /^baz\.md$/]; |
| 330 |
win = [/^foo$/, /^.*$/, /^baz\.md$/]; |
| 331 |
matchSegments(t, 'foo/*/baz.md', unix, win, opts); |
| 332 |
|
| 333 |
unix = [/^foo$/, /^.*$/, /^baz\.md$/]; |
| 334 |
win = [/^foo$/, /^.*$/, /^baz\.md$/]; |
| 335 |
matchSegments(t, 'foo/**/baz.md', unix, win, opts); |
| 336 |
|
| 337 |
unix = [/^foo$/, /^((?:[^\/]*(?:\/|$))*)$/, /^baz\.md$/]; |
| 338 |
win = [/^foo$/, /^((?:[^\\]*(?:\\|$))*)$/, /^baz\.md$/]; |
| 339 |
matchSegments(t, 'foo/**/baz.md', unix, win, {...opts, globstar:true}); |
| 340 |
|
| 341 |
unix = [/^foo$/, /^.*$/, /^.*\.md$/]; |
| 342 |
win = [/^foo$/, /^.*$/, /^.*\.md$/]; |
| 343 |
matchSegments(t, 'foo/**/*.md', unix, win, opts); |
| 344 |
|
| 345 |
unix = [/^foo$/, /^((?:[^\/]*(?:\/|$))*)$/, /^([^\/]*)\.md$/]; |
| 346 |
win = [/^foo$/, /^((?:[^\\]*(?:\\|$))*)$/, /^([^\\]*)\.md$/]; |
| 347 |
matchSegments(t, 'foo/**/*.md', unix, win, {...opts, globstar:true}); |
| 348 |
|
| 349 |
unix = [/^foo$/, /^:$/, /^b:az$/]; |
| 350 |
win = [/^foo$/, /^:$/, /^b:az$/]; |
| 351 |
matchSegments(t, 'foo/:/b:az', unix, win, opts); |
| 352 |
|
| 353 |
unix = [/^foo$/, /^baz\.md$/]; |
| 354 |
win = [/^foo$/, /^baz\.md$/]; |
| 355 |
matchSegments(t, 'foo///baz.md', unix, win, {...opts, strict:true}); |
| 356 |
|
| 357 |
unix = [/^foo$/, /^baz\.md$/]; |
| 358 |
win = [/^foo$/, /^baz\.md$/]; |
| 359 |
matchSegments(t, 'foo///baz.md', unix, win, {...opts, strict:false}); |
| 360 |
|
| 361 |
t.end(); |
| 362 |
}); |
| 363 |
|
| 364 |
test('globrex: stress testing', t => { |
| 365 |
t.plan(8); |
| 366 |
t.equal(match('**/*/?yfile.{md,js,txt}', 'foo/bar/baz/myfile.md', { extended:true }), true); |
| 367 |
t.equal(match('**/*/?yfile.{md,js,txt}', 'foo/baz/myfile.md', { extended:true }), true); |
| 368 |
t.equal(match('**/*/?yfile.{md,js,txt}', 'foo/baz/tyfile.js', { extended:true }), true); |
| 369 |
t.equal(match('[[:digit:]_.]/file.js', '1/file.js', { extended:true }), true); |
| 370 |
t.equal(match('[[:digit:]_.]/file.js', '2/file.js', { extended:true }), true); |
| 371 |
t.equal(match('[[:digit:]_.]/file.js', '_/file.js', { extended:true }), true); |
| 372 |
t.equal(match('[[:digit:]_.]/file.js', './file.js', { extended:true }), true); |
| 373 |
t.equal(match('[[:digit:]_.]/file.js', 'z/file.js', { extended:true }), false); |
| 374 |
}); |