Skip to content

Commit cb27d37

Browse files
committed
Add tests from #270
1 parent 46d8ff9 commit cb27d37

File tree

1 file changed

+113
-18
lines changed

1 file changed

+113
-18
lines changed

Diff for: src/index.spec.ts

+113-18
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
import { describe, it, expect } from "vitest";
1+
import { describe, it, expect, TestOptions } from "vitest";
22
import * as pathToRegexp from "./index";
33

44
interface ParserTestSet {
55
path: string;
66
options?: pathToRegexp.ParseOptions;
77
expected: pathToRegexp.Token[];
8+
testOptions?: TestOptions;
89
}
910

1011
interface CompileTestSet {
1112
path: string;
1213
options?: pathToRegexp.CompileOptions;
14+
testOptions?: TestOptions;
1315
tests: Array<{
1416
input: pathToRegexp.ParamData | undefined;
1517
expected: string | null;
@@ -19,6 +21,7 @@ interface CompileTestSet {
1921
interface MatchTestSet {
2022
path: pathToRegexp.Path;
2123
options?: pathToRegexp.MatchOptions;
24+
testOptions?: TestOptions;
2225
tests: Array<{
2326
input: string;
2427
matches: (string | undefined)[] | null;
@@ -2919,6 +2922,92 @@ const MATCH_TESTS: MatchTestSet[] = [
29192922
},
29202923
],
29212924
},
2925+
2926+
/**
2927+
* https://github.com/pillarjs/path-to-regexp/pull/270
2928+
*/
2929+
{
2930+
path: "/files/:path*.:ext*",
2931+
tests: [
2932+
{
2933+
input: "/files/hello/world.txt",
2934+
matches: ["/files/hello/world.txt", "hello/world", "txt"],
2935+
expected: {
2936+
path: "/files/hello/world.txt",
2937+
index: 0,
2938+
params: { path: ["hello", "world"], ext: ["txt"] },
2939+
},
2940+
},
2941+
{
2942+
input: "/files/my/photo.jpg/gif",
2943+
matches: ["/files/my/photo.jpg/gif", "my/photo.jpg/gif", undefined],
2944+
expected: {
2945+
path: "/files/my/photo.jpg/gif",
2946+
index: 0,
2947+
params: { path: ["my", "photo.jpg", "gif"], ext: undefined },
2948+
},
2949+
},
2950+
],
2951+
},
2952+
{
2953+
path: "#/*",
2954+
tests: [
2955+
{
2956+
input: "#/",
2957+
matches: ["#/", undefined],
2958+
expected: { path: "#/", index: 0, params: {} },
2959+
},
2960+
],
2961+
},
2962+
{
2963+
path: "/foo/:bar*",
2964+
tests: [
2965+
{
2966+
input: "/foo/test1//test2",
2967+
matches: ["/foo/test1//test2", "test1//test2"],
2968+
expected: {
2969+
path: "/foo/test1//test2",
2970+
index: 0,
2971+
params: { bar: ["test1", "test2"] },
2972+
},
2973+
},
2974+
],
2975+
},
2976+
{
2977+
path: "/entity/:id/*",
2978+
tests: [
2979+
{
2980+
input: "/entity/foo",
2981+
matches: ["/entity/foo", "foo", undefined],
2982+
expected: { path: "/entity/foo", index: 0, params: { id: "foo" } },
2983+
},
2984+
{
2985+
input: "/entity/foo/",
2986+
matches: ["/entity/foo/", "foo", undefined],
2987+
expected: { path: "/entity/foo/", index: 0, params: { id: "foo" } },
2988+
},
2989+
],
2990+
},
2991+
{
2992+
path: "/test/*",
2993+
tests: [
2994+
{
2995+
input: "/test",
2996+
matches: ["/test", undefined],
2997+
expected: { path: "/test", index: 0, params: {} },
2998+
},
2999+
{
3000+
input: "/test/",
3001+
matches: ["/test/", undefined],
3002+
expected: { path: "/test/", index: 0, params: {} },
3003+
},
3004+
{
3005+
input: "/test/route",
3006+
matches: ["/test/route", "route"],
3007+
expected: { path: "/test/route", index: 0, params: { "0": ["route"] } },
3008+
},
3009+
],
3010+
},
29223011
];
29233012

29243013
/**
@@ -3003,8 +3092,8 @@ describe("path-to-regexp", () => {
30033092

30043093
describe.each(PARSER_TESTS)(
30053094
"parse $path with $options",
3006-
({ path, options, expected }) => {
3007-
it("should parse the path", () => {
3095+
({ path, options, expected, testOptions }) => {
3096+
it("should parse the path", testOptions, () => {
30083097
const data = pathToRegexp.parse(path, options);
30093098
expect(data.tokens).toEqual(expected);
30103099
});
@@ -3013,32 +3102,38 @@ describe("path-to-regexp", () => {
30133102

30143103
describe.each(COMPILE_TESTS)(
30153104
"compile $path with $options",
3016-
({ path, options, tests }) => {
3105+
({ path, options, tests, testOptions = {} }) => {
30173106
const toPath = pathToRegexp.compile(path, options);
30183107

3019-
it.each(tests)("should compile $input", ({ input, expected }) => {
3020-
if (expected === null) {
3021-
expect(() => {
3022-
toPath(input);
3023-
}).toThrow();
3024-
} else {
3025-
expect(toPath(input)).toEqual(expected);
3026-
}
3027-
});
3108+
it.each(tests)(
3109+
"should compile $input",
3110+
testOptions,
3111+
({ input, expected }) => {
3112+
if (expected === null) {
3113+
expect(() => toPath(input)).toThrow();
3114+
} else {
3115+
expect(toPath(input)).toEqual(expected);
3116+
}
3117+
},
3118+
);
30283119
},
30293120
);
30303121

30313122
describe.each(MATCH_TESTS)(
30323123
"match $path with $options",
3033-
({ path, options, tests }) => {
3124+
({ path, options, tests, testOptions = {} }) => {
30343125
const keys: pathToRegexp.Key[] = [];
30353126
const re = pathToRegexp.pathToRegexp(path, keys, options);
30363127
const match = pathToRegexp.match(path, options);
30373128

3038-
it.each(tests)("should match $input", ({ input, matches, expected }) => {
3039-
expect(exec(re, input)).toEqual(matches);
3040-
expect(match(input)).toEqual(expected);
3041-
});
3129+
it.each(tests)(
3130+
"should match $input",
3131+
testOptions,
3132+
({ input, matches, expected }) => {
3133+
expect(exec(re, input)).toEqual(matches);
3134+
expect(match(input)).toEqual(expected);
3135+
},
3136+
);
30423137
},
30433138
);
30443139

0 commit comments

Comments
 (0)