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