Skip to main content
Module

std/path/globrex_test.ts

Deno standard library
Go to Latest
File
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
// This file is ported from globrex@0.1.2// MIT License// Copyright (c) 2018 Terkel Gjervig Nielsen
const { test } = Deno;import { assertEquals } from "../testing/asserts.ts";import { GlobrexOptions, globrex } from "./globrex.ts";
const isWin = Deno.build.os === "win";const t = { equal: assertEquals, is: assertEquals };
function match( glob: string, strUnix: string, strWin?: string | object, opts: GlobrexOptions = {}): boolean { if (typeof strWin === "object") { opts = strWin; strWin = ""; } const { regex } = globrex(glob, opts); const match = (isWin && strWin ? strWin : strUnix).match(regex); if (match && !regex.flags.includes("g")) { assertEquals(match.length, 1); } return !!match;}
test({ name: "globrex: standard", fn(): void { const res = globrex("*.js"); t.equal(typeof globrex, "function", "constructor is a typeof function"); t.equal(res instanceof Object, true, "returns object"); t.equal(res.regex.toString(), "/^.*\\.js$/", "returns regex object"); },});
test({ name: "globrex: Standard * matching", fn(): void { t.equal(match("*", "foo"), true, "match everything"); t.equal(match("*", "foo", { flags: "g" }), true, "match everything"); t.equal(match("f*", "foo"), true, "match the end"); t.equal(match("f*", "foo", { flags: "g" }), true, "match the end"); t.equal(match("*o", "foo"), true, "match the start"); t.equal(match("*o", "foo", { flags: "g" }), true, "match the start"); t.equal(match("u*orn", "unicorn"), true, "match the middle"); t.equal( match("u*orn", "unicorn", { flags: "g" }), true, "match the middle" ); t.equal(match("ico", "unicorn"), false, "do not match without g"); t.equal( match("ico", "unicorn", { flags: "g" }), true, 'match anywhere with RegExp "g"' ); t.equal(match("u*nicorn", "unicorn"), true, "match zero characters"); t.equal( match("u*nicorn", "unicorn", { flags: "g" }), true, "match zero characters" ); },});
test({ name: "globrex: advance * matching", fn(): void { t.equal( match("*.min.js", "http://example.com/jquery.min.js", { globstar: false, }), true, "complex match" ); t.equal( match("*.min.*", "http://example.com/jquery.min.js", { globstar: false }), true, "complex match" ); t.equal( match("*/js/*.js", "http://example.com/js/jquery.min.js", { globstar: false, }), true, "complex match" ); t.equal( match("*.min.*", "http://example.com/jquery.min.js", { flags: "g" }), true, "complex match global" ); t.equal( match("*.min.js", "http://example.com/jquery.min.js", { flags: "g" }), true, "complex match global" ); t.equal( match("*/js/*.js", "http://example.com/js/jquery.min.js", { flags: "g" }), true, "complex match global" );
const str = "\\/$^+?.()=!|{},[].*"; t.equal(match(str, str), true, "battle test complex string - strict"); t.equal( match(str, str, { flags: "g" }), true, "battle test complex string - strict" );
t.equal( match(".min.", "http://example.com/jquery.min.js"), false, 'matches without/with using RegExp "g"' ); t.equal( match("*.min.*", "http://example.com/jquery.min.js"), true, 'matches without/with using RegExp "g"' ); t.equal( match(".min.", "http://example.com/jquery.min.js", { flags: "g" }), true, 'matches without/with using RegExp "g"' ); t.equal( match("http:", "http://example.com/jquery.min.js"), false, 'matches without/with using RegExp "g"' ); t.equal( match("http:*", "http://example.com/jquery.min.js"), true, 'matches without/with using RegExp "g"' ); t.equal( match("http:", "http://example.com/jquery.min.js", { flags: "g" }), true, 'matches without/with using RegExp "g"' ); t.equal( match("min.js", "http://example.com/jquery.min.js"), false, 'matches without/with using RegExp "g"' ); t.equal( match("*.min.js", "http://example.com/jquery.min.js"), true, 'matches without/with using RegExp "g"' ); t.equal( match("min.js", "http://example.com/jquery.min.js", { flags: "g" }), true, 'matches without/with using RegExp "g"' ); t.equal( match("min", "http://example.com/jquery.min.js", { flags: "g" }), true, 'match anywhere (globally) using RegExp "g"' ); t.equal( match("/js/", "http://example.com/js/jquery.min.js", { flags: "g" }), true, 'match anywhere (globally) using RegExp "g"' ); t.equal(match("/js*jq*.js", "http://example.com/js/jquery.min.js"), false); t.equal( match("/js*jq*.js", "http://example.com/js/jquery.min.js", { flags: "g", }), true ); },});
test({ name: "globrex: ? match one character, no more and no less", fn(): void { t.equal(match("f?o", "foo", { extended: true }), true); t.equal(match("f?o", "fooo", { extended: true }), false); t.equal(match("f?oo", "foo", { extended: true }), false);
const tester = (globstar: boolean): void => { t.equal( match("f?o", "foo", { extended: true, globstar, flags: "g" }), true ); t.equal( match("f?o", "fooo", { extended: true, globstar, flags: "g" }), true ); t.equal( match("f?o?", "fooo", { extended: true, globstar, flags: "g" }), true );
t.equal( match("?fo", "fooo", { extended: true, globstar, flags: "g" }), false ); t.equal( match("f?oo", "foo", { extended: true, globstar, flags: "g" }), false ); t.equal( match("foo?", "foo", { extended: true, globstar, flags: "g" }), false ); };
tester(true); tester(false); },});
test({ name: "globrex: [] match a character range", fn(): void { t.equal(match("fo[oz]", "foo", { extended: true }), true); t.equal(match("fo[oz]", "foz", { extended: true }), true); t.equal(match("fo[oz]", "fog", { extended: true }), false); t.equal(match("fo[a-z]", "fob", { extended: true }), true); t.equal(match("fo[a-d]", "fot", { extended: true }), false); t.equal(match("fo[!tz]", "fot", { extended: true }), false); t.equal(match("fo[!tz]", "fob", { extended: true }), true);
const tester = (globstar: boolean): void => { t.equal( match("fo[oz]", "foo", { extended: true, globstar, flags: "g" }), true ); t.equal( match("fo[oz]", "foz", { extended: true, globstar, flags: "g" }), true ); t.equal( match("fo[oz]", "fog", { extended: true, globstar, flags: "g" }), false ); };
tester(true); tester(false); },});
test({ name: "globrex: [] extended character ranges", fn(): void { t.equal( match("[[:alnum:]]/bar.txt", "a/bar.txt", { extended: true }), true ); t.equal( match("@([[:alnum:]abc]|11)/bar.txt", "11/bar.txt", { extended: true }), true ); t.equal( match("@([[:alnum:]abc]|11)/bar.txt", "a/bar.txt", { extended: true }), true ); t.equal( match("@([[:alnum:]abc]|11)/bar.txt", "b/bar.txt", { extended: true }), true ); t.equal( match("@([[:alnum:]abc]|11)/bar.txt", "c/bar.txt", { extended: true }), true ); t.equal( match("@([[:alnum:]abc]|11)/bar.txt", "abc/bar.txt", { extended: true }), false ); t.equal( match("@([[:alnum:]abc]|11)/bar.txt", "3/bar.txt", { extended: true }), true ); t.equal( match("[[:digit:]]/bar.txt", "1/bar.txt", { extended: true }), true ); t.equal( match("[[:digit:]b]/bar.txt", "b/bar.txt", { extended: true }), true ); t.equal( match("[![:digit:]b]/bar.txt", "a/bar.txt", { extended: true }), true ); t.equal( match("[[:alnum:]]/bar.txt", "!/bar.txt", { extended: true }), false ); t.equal( match("[[:digit:]]/bar.txt", "a/bar.txt", { extended: true }), false ); t.equal( match("[[:digit:]b]/bar.txt", "a/bar.txt", { extended: true }), false ); },});
test({ name: "globrex: {} match a choice of different substrings", fn(): void { t.equal(match("foo{bar,baaz}", "foobaaz", { extended: true }), true); t.equal(match("foo{bar,baaz}", "foobar", { extended: true }), true); t.equal(match("foo{bar,baaz}", "foobuzz", { extended: true }), false); t.equal(match("foo{bar,b*z}", "foobuzz", { extended: true }), true);
const tester = (globstar: boolean): void => { t.equal( match("foo{bar,baaz}", "foobaaz", { extended: true, globstar, flag: "g", }), true ); t.equal( match("foo{bar,baaz}", "foobar", { extended: true, globstar, flag: "g", }), true ); t.equal( match("foo{bar,baaz}", "foobuzz", { extended: true, globstar, flag: "g", }), false ); t.equal( match("foo{bar,b*z}", "foobuzz", { extended: true, globstar, flag: "g", }), true ); };
tester(true); tester(false); },});
test({ name: "globrex: complex extended matches", fn(): void { t.equal( match( "http://?o[oz].b*z.com/{*.js,*.html}", "http://foo.baaz.com/jquery.min.js", { extended: true } ), true ); t.equal( match( "http://?o[oz].b*z.com/{*.js,*.html}", "http://moz.buzz.com/index.html", { extended: true } ), true ); t.equal( match( "http://?o[oz].b*z.com/{*.js,*.html}", "http://moz.buzz.com/index.htm", { extended: true } ), false ); t.equal( match( "http://?o[oz].b*z.com/{*.js,*.html}", "http://moz.bar.com/index.html", { extended: true } ), false ); t.equal( match( "http://?o[oz].b*z.com/{*.js,*.html}", "http://flozz.buzz.com/index.html", { extended: true } ), false );
const tester = (globstar: boolean): void => { t.equal( match( "http://?o[oz].b*z.com/{*.js,*.html}", "http://foo.baaz.com/jquery.min.js", { extended: true, globstar, flags: "g" } ), true ); t.equal( match( "http://?o[oz].b*z.com/{*.js,*.html}", "http://moz.buzz.com/index.html", { extended: true, globstar, flags: "g" } ), true ); t.equal( match( "http://?o[oz].b*z.com/{*.js,*.html}", "http://moz.buzz.com/index.htm", { extended: true, globstar, flags: "g" } ), false ); t.equal( match( "http://?o[oz].b*z.com/{*.js,*.html}", "http://moz.bar.com/index.html", { extended: true, globstar, flags: "g" } ), false ); t.equal( match( "http://?o[oz].b*z.com/{*.js,*.html}", "http://flozz.buzz.com/index.html", { extended: true, globstar, flags: "g" } ), false ); };
tester(true); tester(false); },});
test({ name: "globrex: standard globstar", fn(): void { const tester = (globstar: boolean): void => { t.equal( match( "http://foo.com/**/{*.js,*.html}", "http://foo.com/bar/jquery.min.js", { extended: true, globstar, flags: "g" } ), true ); t.equal( match( "http://foo.com/**/{*.js,*.html}", "http://foo.com/bar/baz/jquery.min.js", { extended: true, globstar, flags: "g" } ), true ); t.equal( match("http://foo.com/**", "http://foo.com/bar/baz/jquery.min.js", { extended: true, globstar, flags: "g", }), true ); };
tester(true); tester(false); },});
test({ name: "globrex: remaining chars should match themself", fn(): void { const tester = (globstar: boolean): void => { const testExtStr = "\\/$^+.()=!|,.*"; t.equal(match(testExtStr, testExtStr, { extended: true }), true); t.equal( match(testExtStr, testExtStr, { extended: true, globstar, flags: "g" }), true ); };
tester(true); tester(false); },});
test({ name: "globrex: globstar advance testing", fn(): void { t.equal(match("/foo/*", "/foo/bar.txt", { globstar: true }), true); t.equal(match("/foo/**", "/foo/bar.txt", { globstar: true }), true); t.equal(match("/foo/**", "/foo/bar/baz.txt", { globstar: true }), true); t.equal(match("/foo/**", "/foo/bar/baz.txt", { globstar: true }), true); t.equal( match("/foo/*/*.txt", "/foo/bar/baz.txt", { globstar: true }), true ); t.equal( match("/foo/**/*.txt", "/foo/bar/baz.txt", { globstar: true }), true ); t.equal( match("/foo/**/*.txt", "/foo/bar/baz/qux.txt", { globstar: true }), true ); t.equal(match("/foo/**/bar.txt", "/foo/bar.txt", { globstar: true }), true); t.equal( match("/foo/**/**/bar.txt", "/foo/bar.txt", { globstar: true }), true ); t.equal( match("/foo/**/*/baz.txt", "/foo/bar/baz.txt", { globstar: true }), true ); t.equal(match("/foo/**/*.txt", "/foo/bar.txt", { globstar: true }), true); t.equal( match("/foo/**/**/*.txt", "/foo/bar.txt", { globstar: true }), true ); t.equal( match("/foo/**/*/*.txt", "/foo/bar/baz.txt", { globstar: true }), true ); t.equal( match("**/*.txt", "/foo/bar/baz/qux.txt", { globstar: true }), true ); t.equal(match("**/foo.txt", "foo.txt", { globstar: true }), true); t.equal(match("**/*.txt", "foo.txt", { globstar: true }), true); t.equal(match("/foo/*", "/foo/bar/baz.txt", { globstar: true }), false); t.equal(match("/foo/*.txt", "/foo/bar/baz.txt", { globstar: true }), false); t.equal( match("/foo/*/*.txt", "/foo/bar/baz/qux.txt", { globstar: true }), false ); t.equal(match("/foo/*/bar.txt", "/foo/bar.txt", { globstar: true }), false); t.equal( match("/foo/*/*/baz.txt", "/foo/bar/baz.txt", { globstar: true }), false ); t.equal( match("/foo/**.txt", "/foo/bar/baz/qux.txt", { globstar: true }), false ); t.equal( match("/foo/bar**/*.txt", "/foo/bar/baz/qux.txt", { globstar: true }), false ); t.equal(match("/foo/bar**", "/foo/bar/baz.txt", { globstar: true }), false); t.equal( match("**/.txt", "/foo/bar/baz/qux.txt", { globstar: true }), false ); t.equal( match("*/*.txt", "/foo/bar/baz/qux.txt", { globstar: true }), false ); t.equal(match("*/*.txt", "foo.txt", { globstar: true }), false); t.equal( match("http://foo.com/*", "http://foo.com/bar/baz/jquery.min.js", { extended: true, globstar: true, }), false ); t.equal( match("http://foo.com/*", "http://foo.com/bar/baz/jquery.min.js", { globstar: true, }), false ); t.equal( match("http://foo.com/*", "http://foo.com/bar/baz/jquery.min.js", { globstar: false, }), true ); t.equal( match("http://foo.com/**", "http://foo.com/bar/baz/jquery.min.js", { globstar: true, }), true ); t.equal( match( "http://foo.com/*/*/jquery.min.js", "http://foo.com/bar/baz/jquery.min.js", { globstar: true } ), true ); t.equal( match( "http://foo.com/**/jquery.min.js", "http://foo.com/bar/baz/jquery.min.js", { globstar: true } ), true ); t.equal( match( "http://foo.com/*/*/jquery.min.js", "http://foo.com/bar/baz/jquery.min.js", { globstar: false } ), true ); t.equal( match( "http://foo.com/*/jquery.min.js", "http://foo.com/bar/baz/jquery.min.js", { globstar: false } ), true ); t.equal( match( "http://foo.com/*/jquery.min.js", "http://foo.com/bar/baz/jquery.min.js", { globstar: true } ), false ); },});
test({ name: "globrex: extended extglob ?", fn(): void { t.equal(match("(foo).txt", "(foo).txt", { extended: true }), true); t.equal(match("?(foo).txt", "foo.txt", { extended: true }), true); t.equal(match("?(foo).txt", ".txt", { extended: true }), true); t.equal(match("?(foo|bar)baz.txt", "foobaz.txt", { extended: true }), true); t.equal( match("?(ba[zr]|qux)baz.txt", "bazbaz.txt", { extended: true }), true ); t.equal( match("?(ba[zr]|qux)baz.txt", "barbaz.txt", { extended: true }), true ); t.equal( match("?(ba[zr]|qux)baz.txt", "quxbaz.txt", { extended: true }), true ); t.equal( match("?(ba[!zr]|qux)baz.txt", "batbaz.txt", { extended: true }), true ); t.equal(match("?(ba*|qux)baz.txt", "batbaz.txt", { extended: true }), true); t.equal( match("?(ba*|qux)baz.txt", "batttbaz.txt", { extended: true }), true ); t.equal(match("?(ba*|qux)baz.txt", "quxbaz.txt", { extended: true }), true); t.equal( match("?(ba?(z|r)|qux)baz.txt", "bazbaz.txt", { extended: true }), true ); t.equal( match("?(ba?(z|?(r))|qux)baz.txt", "bazbaz.txt", { extended: true }), true ); t.equal(match("?(foo).txt", "foo.txt", { extended: false }), false); t.equal( match("?(foo|bar)baz.txt", "foobarbaz.txt", { extended: true }), false ); t.equal( match("?(ba[zr]|qux)baz.txt", "bazquxbaz.txt", { extended: true }), false ); t.equal( match("?(ba[!zr]|qux)baz.txt", "bazbaz.txt", { extended: true }), false ); },});
test({ name: "globrex: extended extglob *", fn(): void { t.equal(match("*(foo).txt", "foo.txt", { extended: true }), true); t.equal(match("*foo.txt", "bofoo.txt", { extended: true }), true); t.equal(match("*(foo).txt", "foofoo.txt", { extended: true }), true); t.equal(match("*(foo).txt", ".txt", { extended: true }), true); t.equal(match("*(fooo).txt", ".txt", { extended: true }), true); t.equal(match("*(fooo).txt", "foo.txt", { extended: true }), false); t.equal(match("*(foo|bar).txt", "foobar.txt", { extended: true }), true); t.equal(match("*(foo|bar).txt", "barbar.txt", { extended: true }), true); t.equal(match("*(foo|bar).txt", "barfoobar.txt", { extended: true }), true); t.equal(match("*(foo|bar).txt", ".txt", { extended: true }), true); t.equal(match("*(foo|ba[rt]).txt", "bat.txt", { extended: true }), true); t.equal(match("*(foo|b*[rt]).txt", "blat.txt", { extended: true }), true); t.equal(match("*(foo|b*[rt]).txt", "tlat.txt", { extended: true }), false); t.equal( match("*(*).txt", "whatever.txt", { extended: true, globstar: true }), true ); t.equal( match("*(foo|bar)/**/*.txt", "foo/hello/world/bar.txt", { extended: true, globstar: true, }), true ); t.equal( match("*(foo|bar)/**/*.txt", "foo/world/bar.txt", { extended: true, globstar: true, }), true ); },});
test({ name: "globrex: extended extglob +", fn(): void { t.equal(match("+(foo).txt", "foo.txt", { extended: true }), true); t.equal(match("+foo.txt", "+foo.txt", { extended: true }), true); t.equal(match("+(foo).txt", ".txt", { extended: true }), false); t.equal(match("+(foo|bar).txt", "foobar.txt", { extended: true }), true); },});
test({ name: "globrex: extended extglob @", fn(): void { t.equal(match("@(foo).txt", "foo.txt", { extended: true }), true); t.equal(match("@foo.txt", "@foo.txt", { extended: true }), true); t.equal(match("@(foo|baz)bar.txt", "foobar.txt", { extended: true }), true); t.equal( match("@(foo|baz)bar.txt", "foobazbar.txt", { extended: true }), false ); t.equal( match("@(foo|baz)bar.txt", "foofoobar.txt", { extended: true }), false ); t.equal( match("@(foo|baz)bar.txt", "toofoobar.txt", { extended: true }), false ); },});
test({ name: "globrex: extended extglob !", fn(): void { t.equal(match("!(boo).txt", "foo.txt", { extended: true }), true); t.equal(match("!(foo|baz)bar.txt", "buzbar.txt", { extended: true }), true); t.equal(match("!bar.txt", "!bar.txt", { extended: true }), true); t.equal( match("!({foo,bar})baz.txt", "notbaz.txt", { extended: true }), true ); t.equal( match("!({foo,bar})baz.txt", "foobaz.txt", { extended: true }), false ); },});
test({ name: "globrex: strict", fn(): void { t.equal(match("foo//bar.txt", "foo/bar.txt"), true); t.equal(match("foo///bar.txt", "foo/bar.txt"), true); t.equal(match("foo///bar.txt", "foo/bar.txt", { strict: true }), false); },});
test({ name: "globrex: stress testing", fn(): void { t.equal( match("**/*/?yfile.{md,js,txt}", "foo/bar/baz/myfile.md", { extended: true, }), true ); t.equal( match("**/*/?yfile.{md,js,txt}", "foo/baz/myfile.md", { extended: true }), true ); t.equal( match("**/*/?yfile.{md,js,txt}", "foo/baz/tyfile.js", { extended: true }), true ); t.equal( match("[[:digit:]_.]/file.js", "1/file.js", { extended: true }), true ); t.equal( match("[[:digit:]_.]/file.js", "2/file.js", { extended: true }), true ); t.equal( match("[[:digit:]_.]/file.js", "_/file.js", { extended: true }), true ); t.equal( match("[[:digit:]_.]/file.js", "./file.js", { extended: true }), true ); t.equal( match("[[:digit:]_.]/file.js", "z/file.js", { extended: true }), false ); },});