Extremely Popular
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322// Copyright 2018-2020 the oak authors. All rights reserved. MIT license.
import { test, assertEquals, assertStrictEq } from "./test_deps.ts";import { Key, ParseOptions, Path, pathToRegExp, RegExpOptions,} from "./pathToRegExp.ts";
interface TestFixture { name: string; source: Path; options?: RegExpOptions & ParseOptions; keys?: Key[]; fixtures: { [value: string]: string[] | null; };}
const testFixtures: TestFixture[] = [ { name: "/", source: "/", fixtures: { "/": ["/"], "/route": null, }, }, { name: "/test", source: "/test", fixtures: { "/test": ["/test"], "/route": null, "/test/route": null, "/test/": ["/test/"], }, }, { name: "/test/", source: "/test/", fixtures: { "/test": null, "/test/": ["/test/"], "/test//": ["/test//"], }, }, { name: "/test sensitive", source: "/test", options: { sensitive: true }, fixtures: { "/test": ["/test"], "/TEST": null, }, }, { name: "/TEST sensitive", source: "/TEST", options: { sensitive: true }, fixtures: { "/test": null, "/TEST": ["/TEST"], }, }, { name: "/test strict", source: "/test", options: { strict: true, }, fixtures: { "/test": ["/test"], "/test/": null, "/TEST": ["/TEST"], }, }, { name: "/test/ strict", source: "/test/", options: { strict: true, }, fixtures: { "/test": null, "/test/": ["/test/"], "/test//": null, }, }, { name: "/test non-ending", source: "/test", options: { end: false, }, fixtures: { "/test": ["/test"], "/test/": ["/test/"], "/test/route": ["/test"], "/route": null, }, }, { name: "/test/ non-ending", source: "/test/", options: { end: false, }, fixtures: { "/test": null, "/test/route": ["/test/"], "/test//": ["/test//"], "/test//route": ["/test/"], }, }, { name: "/:test non-ending", source: "/:test", options: { end: false, }, keys: [ { name: "test", prefix: "/", delimiter: "/", optional: false, repeat: false, pattern: "[^\\/]+?", partial: false, }, ], fixtures: { "/route": ["/route", "route"], }, }, { name: "/:test/ non-ending", source: "/:test/", options: { end: false, }, keys: [ { name: "test", prefix: "/", delimiter: "/", optional: false, repeat: false, pattern: "[^\\/]+?", partial: false, }, ], fixtures: { "/route": null, "/route/": ["/route/", "route"], }, }, { name: "empty non-ending", source: "", options: { end: false, }, fixtures: { "": [""], "/": ["/"], route: [""], "/route": [""], "/route/": [""], }, }, { name: "/test non-starting", source: "/test", options: { start: false, }, fixtures: { "/test": ["/test"], "/test/": ["/test/"], "/route/test": ["/test"], "/test/route": null, "/route/test/deep": null, "/route": null, }, }, { name: "/test/ non-starting", source: "/test/", options: { start: false, }, fixtures: { "/test": null, "/test/route": null, "/test//route": null, "/test//": ["/test//"], "/route/test/": ["/test/"], }, }, { name: "/:test non-starting", source: "/:test", options: { start: false, }, keys: [ { name: "test", prefix: "/", delimiter: "/", optional: false, repeat: false, pattern: "[^\\/]+?", partial: false, }, ], fixtures: { "/route": ["/route", "route"], }, }, { name: "/:test/ non-starting", source: "/:test/", options: { start: false, }, keys: [ { name: "test", prefix: "/", delimiter: "/", optional: false, repeat: false, pattern: "[^\\/]+?", partial: false, }, ], fixtures: { "/route": null, "/route/": ["/route/", "route"], }, }, { name: "empty non-starting", source: "", options: { start: false, }, fixtures: { "": [""], "/": ["/"], route: [""], "/route": [""], "/route/": ["/"], }, }, { name: "/one /two array", source: ["/one", "/two"], fixtures: { "/one": ["/one"], "/two": ["/two"], "/three": null, "/one/two": null, }, }, { name: "/:test", source: "/:test", keys: [ { name: "test", prefix: "/", delimiter: "/", optional: false, repeat: false, pattern: "[^\\/]+?", partial: false, }, ], fixtures: { "/route": ["/route", "route"], "/another": ["/another", "another"], "/something/else": null, "/route.json": ["/route.json", "route.json"], "/something%2Felse": ["/something%2Felse", "something%2Felse"], "/something%2Felse%2Fmore": [ "/something%2Felse%2Fmore", "something%2Felse%2Fmore", ], "/;,:@&=+$-_.!~*()": ["/;,:@&=+$-_.!~*()", ";,:@&=+$-_.!~*()"], }, },];
for (const { name, source, fixtures, keys, options } of testFixtures) { test({ name: `pathToRegExp ${name}`, fn() { const actualKeys: Key[] = []; const re = pathToRegExp(source, actualKeys, options); if (keys) { assertEquals(actualKeys, keys); } else { assertEquals(actualKeys.length, 0); } for (const [fixture, expected] of Object.entries(fixtures)) { const actual = re.exec(fixture); if (!actual) { assertStrictEq(expected, null, "Expected a match."); } else { assertEquals([...actual], expected); } } }, });}