forked from hyperjump-io/json-pointer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointerSegments.test.js
More file actions
60 lines (54 loc) · 1.84 KB
/
pointerSegments.test.js
File metadata and controls
60 lines (54 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { describe, expect, test } from "vitest";
import { pointerSegments } from "./index.js";
describe("JsonPointer", () => {
describe("pointerSegments", () => {
/** @type [string, string[]][] */
const tests = [
["", []],
["/", [""]],
["/foo", ["foo"]],
["/foo/bar", ["foo", "bar"]],
["/foo/0", ["foo", "0"]],
["/a~1b", ["a/b"]],
["/m~0n", ["m~n"]],
["/~00", ["~0"]],
["/~01", ["~1"]],
["/~10", ["/0"]],
["/~11", ["/1"]],
["/~01~10", ["~1/0"]],
["/~00/~11", ["~0", "/1"]],
["/ ", [" "]],
["/c%d", ["c%d"]],
["/e^f", ["e^f"]],
["/g|h", ["g|h"]],
["/i\\j", ["i\\j"]],
["/k\"l", ["k\"l"]],
];
tests.forEach(([pointer, expected]) => {
test(`${JSON.stringify(pointer)} => ${JSON.stringify(expected)}`, () => {
expect([...pointerSegments(pointer)]).to.eql(expected);
});
});
});
describe("a pointer that doesn't start with '/'", () => {
test("should throw an error", () => {
expect(() => [...pointerSegments("foo")]).to.throw(Error, "Invalid JSON Pointer");
});
});
describe("a pointer with an invalid escape sequence", () => {
/** @type string[] */
const tests = [
"/~",
"/~2",
"/~a",
"/a~",
"/~~",
"/~0~",
];
tests.forEach((pointer) => {
test(`${JSON.stringify(pointer)} should throw an error`, () => {
expect(() => [...pointerSegments(pointer)]).to.throw(Error, "Invalid JSON Pointer");
});
});
});
});