Skip to content

Commit

Permalink
Add tests from #270
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed May 28, 2024
1 parent 46d8ff9 commit cb27d37
Showing 1 changed file with 113 additions and 18 deletions.
131 changes: 113 additions & 18 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { describe, it, expect } from "vitest";
import { describe, it, expect, TestOptions } from "vitest";
import * as pathToRegexp from "./index";

interface ParserTestSet {
path: string;
options?: pathToRegexp.ParseOptions;
expected: pathToRegexp.Token[];
testOptions?: TestOptions;
}

interface CompileTestSet {
path: string;
options?: pathToRegexp.CompileOptions;
testOptions?: TestOptions;
tests: Array<{
input: pathToRegexp.ParamData | undefined;
expected: string | null;
Expand All @@ -19,6 +21,7 @@ interface CompileTestSet {
interface MatchTestSet {
path: pathToRegexp.Path;
options?: pathToRegexp.MatchOptions;
testOptions?: TestOptions;
tests: Array<{
input: string;
matches: (string | undefined)[] | null;
Expand Down Expand Up @@ -2919,6 +2922,92 @@ const MATCH_TESTS: MatchTestSet[] = [
},
],
},

/**
* https://github.com/pillarjs/path-to-regexp/pull/270
*/
{
path: "/files/:path*.:ext*",
tests: [
{
input: "/files/hello/world.txt",
matches: ["/files/hello/world.txt", "hello/world", "txt"],
expected: {
path: "/files/hello/world.txt",
index: 0,
params: { path: ["hello", "world"], ext: ["txt"] },
},
},
{
input: "/files/my/photo.jpg/gif",
matches: ["/files/my/photo.jpg/gif", "my/photo.jpg/gif", undefined],
expected: {
path: "/files/my/photo.jpg/gif",
index: 0,
params: { path: ["my", "photo.jpg", "gif"], ext: undefined },
},
},
],
},
{
path: "#/*",
tests: [
{
input: "#/",
matches: ["#/", undefined],
expected: { path: "#/", index: 0, params: {} },
},
],
},
{
path: "/foo/:bar*",
tests: [
{
input: "/foo/test1//test2",
matches: ["/foo/test1//test2", "test1//test2"],
expected: {
path: "/foo/test1//test2",
index: 0,
params: { bar: ["test1", "test2"] },
},
},
],
},
{
path: "/entity/:id/*",
tests: [
{
input: "/entity/foo",
matches: ["/entity/foo", "foo", undefined],
expected: { path: "/entity/foo", index: 0, params: { id: "foo" } },
},
{
input: "/entity/foo/",
matches: ["/entity/foo/", "foo", undefined],
expected: { path: "/entity/foo/", index: 0, params: { id: "foo" } },
},
],
},
{
path: "/test/*",
tests: [
{
input: "/test",
matches: ["/test", undefined],
expected: { path: "/test", index: 0, params: {} },
},
{
input: "/test/",
matches: ["/test/", undefined],
expected: { path: "/test/", index: 0, params: {} },
},
{
input: "/test/route",
matches: ["/test/route", "route"],
expected: { path: "/test/route", index: 0, params: { "0": ["route"] } },
},
],
},
];

/**
Expand Down Expand Up @@ -3003,8 +3092,8 @@ describe("path-to-regexp", () => {

describe.each(PARSER_TESTS)(
"parse $path with $options",
({ path, options, expected }) => {
it("should parse the path", () => {
({ path, options, expected, testOptions }) => {
it("should parse the path", testOptions, () => {
const data = pathToRegexp.parse(path, options);
expect(data.tokens).toEqual(expected);
});
Expand All @@ -3013,32 +3102,38 @@ describe("path-to-regexp", () => {

describe.each(COMPILE_TESTS)(
"compile $path with $options",
({ path, options, tests }) => {
({ path, options, tests, testOptions = {} }) => {
const toPath = pathToRegexp.compile(path, options);

it.each(tests)("should compile $input", ({ input, expected }) => {
if (expected === null) {
expect(() => {
toPath(input);
}).toThrow();
} else {
expect(toPath(input)).toEqual(expected);
}
});
it.each(tests)(
"should compile $input",
testOptions,
({ input, expected }) => {
if (expected === null) {
expect(() => toPath(input)).toThrow();
} else {
expect(toPath(input)).toEqual(expected);
}
},
);
},
);

describe.each(MATCH_TESTS)(
"match $path with $options",
({ path, options, tests }) => {
({ path, options, tests, testOptions = {} }) => {
const keys: pathToRegexp.Key[] = [];
const re = pathToRegexp.pathToRegexp(path, keys, options);
const match = pathToRegexp.match(path, options);

it.each(tests)("should match $input", ({ input, matches, expected }) => {
expect(exec(re, input)).toEqual(matches);
expect(match(input)).toEqual(expected);
});
it.each(tests)(
"should match $input",
testOptions,
({ input, matches, expected }) => {
expect(exec(re, input)).toEqual(matches);
expect(match(input)).toEqual(expected);
},
);
},
);

Expand Down

0 comments on commit cb27d37

Please sign in to comment.