Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 69 additions & 34 deletions src/cases.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,64 +41,99 @@ export interface MatchTestSet {
export const PARSER_TESTS: ParserTestSet[] = [
{
path: "/",
expected: new TokenData([{ type: "text", value: "/" }]),
expected: new TokenData([{ type: "text", value: "/" }], "/"),
},
{
path: "/:test",
expected: new TokenData([
{ type: "text", value: "/" },
{ type: "param", name: "test" },
]),
expected: new TokenData(
[
{ type: "text", value: "/" },
{ type: "param", name: "test" },
],
"/:test",
),
},
{
path: "/:a:b",
expected: new TokenData(
[
{ type: "text", value: "/" },
{ type: "param", name: "a" },
{ type: "param", name: "b" },
],
"/:a:b",
),
},
{
path: '/:"0"',
expected: new TokenData([
{ type: "text", value: "/" },
{ type: "param", name: "0" },
]),
expected: new TokenData(
[
{ type: "text", value: "/" },
{ type: "param", name: "0" },
],
'/:"0"',
),
},
{
path: "/:_",
expected: new TokenData([
{ type: "text", value: "/" },
{ type: "param", name: "_" },
]),
expected: new TokenData(
[
{ type: "text", value: "/" },
{ type: "param", name: "_" },
],
"/:_",
),
},
{
path: "/:café",
expected: new TokenData([
{ type: "text", value: "/" },
{ type: "param", name: "café" },
]),
expected: new TokenData(
[
{ type: "text", value: "/" },
{ type: "param", name: "café" },
],
"/:café",
),
},
{
path: '/:"123"',
expected: new TokenData([
{ type: "text", value: "/" },
{ type: "param", name: "123" },
]),
expected: new TokenData(
[
{ type: "text", value: "/" },
{ type: "param", name: "123" },
],
'/:"123"',
),
},
{
path: '/:"1\\"\\2\\"3"',
expected: new TokenData([
{ type: "text", value: "/" },
{ type: "param", name: '1"2"3' },
]),
expected: new TokenData(
[
{ type: "text", value: "/" },
{ type: "param", name: '1"2"3' },
],
'/:"1\\"\\2\\"3"',
),
},
{
path: "/*path",
expected: new TokenData([
{ type: "text", value: "/" },
{ type: "wildcard", name: "path" },
]),
expected: new TokenData(
[
{ type: "text", value: "/" },
{ type: "wildcard", name: "path" },
],
"/*path",
),
},
{
path: '/:"test"stuff',
expected: new TokenData([
{ type: "text", value: "/" },
{ type: "param", name: "test" },
{ type: "text", value: "stuff" },
]),
expected: new TokenData(
[
{ type: "text", value: "/" },
{ type: "param", name: "test" },
{ type: "text", value: "stuff" },
],
'/:"test"stuff',
),
},
];

Expand Down
63 changes: 57 additions & 6 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { describe, it, expect } from "vitest";
import { parse, compile, match, stringify } from "./index.js";
import {
parse,
compile,
match,
stringify,
pathToRegexp,
TokenData,
} from "./index.js";
import {
PARSER_TESTS,
COMPILE_TESTS,
Expand All @@ -15,38 +22,39 @@ describe("path-to-regexp", () => {
it("should throw on unbalanced group", () => {
expect(() => parse("/{:foo,")).toThrow(
new TypeError(
"Unexpected END at 7, expected }: https://git.new/pathToRegexpError",
"Unexpected END at index 7, expected }: /{:foo,; visit https://git.new/pathToRegexpError for more info",
),
);
});

it("should throw on nested unbalanced group", () => {
expect(() => parse("/{:foo/{x,y}")).toThrow(
new TypeError(
"Unexpected END at 12, expected }: https://git.new/pathToRegexpError",
"Unexpected END at index 12, expected }: /{:foo/{x,y}; visit https://git.new/pathToRegexpError for more info",
),
);
});

it("should throw on missing param name", () => {
expect(() => parse("/:/")).toThrow(
new TypeError(
"Missing parameter name at 2: https://git.new/pathToRegexpError",
"Missing parameter name at index 2: /:/; visit https://git.new/pathToRegexpError for more info",
),
);
});

it("should throw on missing wildcard name", () => {
expect(() => parse("/*/")).toThrow(
new TypeError(
"Missing parameter name at 2: https://git.new/pathToRegexpError",
"Missing parameter name at index 2: /*/; visit https://git.new/pathToRegexpError for more info",
),
);
});

it("should throw on unterminated quote", () => {
expect(() => parse('/:"foo')).toThrow(
new TypeError(
"Unterminated quote at 2: https://git.new/pathToRegexpError",
'Unterminated quote at index 2: /:"foo; visit https://git.new/pathToRegexpError for more info',
),
);
});
Expand Down Expand Up @@ -94,6 +102,49 @@ describe("path-to-regexp", () => {
});
});

describe("pathToRegexp errors", () => {
it("should throw when missing text between params", () => {
expect(() => pathToRegexp("/:foo:bar")).toThrow(
new TypeError(
'Missing text before "bar": /:foo:bar; visit https://git.new/pathToRegexpError for more info',
),
);
});

it("should throw when missing text between params using TokenData", () => {
expect(() =>
pathToRegexp(
new TokenData([
{ type: "param", name: "a" },
{ type: "param", name: "b" },
]),
),
).toThrow(
new TypeError(
'Missing text before "b"; visit https://git.new/pathToRegexpError for more info',
),
);
});

it("should throw with `originalPath` when missing text between params using TokenData", () => {
expect(() =>
pathToRegexp(
new TokenData(
[
{ type: "param", name: "a" },
{ type: "param", name: "b" },
],
"/[a][b]",
),
),
).toThrow(
new TypeError(
'Missing text before "b": /[a][b]; visit https://git.new/pathToRegexpError for more info',
),
);
});
});

describe.each(PARSER_TESTS)(
"parse $path with $options",
({ path, options, expected }) => {
Expand Down
Loading