|
1 |
| -import Ptr, { InvalidPtrError } from "./index"; |
| 1 | +import Ptr, { InvalidPtrError, EvalError } from "./index"; |
2 | 2 |
|
3 | 3 | describe("Ptr", () => {
|
4 | 4 | describe("parse", () => {
|
@@ -35,4 +35,24 @@ describe("Ptr", () => {
|
35 | 35 | }).toThrowError(new InvalidPtrError(" "));
|
36 | 36 | });
|
37 | 37 | });
|
| 38 | + |
| 39 | + describe("eval", () => { |
| 40 | + it("handles evaluating JSON pointers against any input", () => { |
| 41 | + expect(Ptr.parse("").eval(null)).toEqual(null); |
| 42 | + expect(Ptr.parse("").eval(true)).toEqual(true); |
| 43 | + expect(Ptr.parse("").eval(3.14)).toEqual(3.14); |
| 44 | + expect(Ptr.parse("").eval("foo")).toEqual("foo"); |
| 45 | + expect(Ptr.parse("").eval([])).toEqual([]); |
| 46 | + expect(Ptr.parse("").eval({})).toEqual({}); |
| 47 | + expect(Ptr.parse("/foo").eval({ foo: "bar" })).toEqual("bar"); |
| 48 | + expect(Ptr.parse("/0").eval(["bar"])).toEqual("bar"); |
| 49 | + expect(Ptr.parse("/foo/1/bar").eval({foo: [null, { bar: "x" }]})).toEqual("x"); |
| 50 | + }); |
| 51 | + |
| 52 | + it("returns an error when an instance lacks a property", () => { |
| 53 | + expect(() => { Ptr.parse("/foo").eval(3.14) }).toThrow(new EvalError(3.14, "foo")); |
| 54 | + expect(() => { Ptr.parse("/0").eval([]) }).toThrow(new EvalError([], "0")); |
| 55 | + expect(() => { Ptr.parse("/foo").eval({}) }).toThrow(new EvalError({}, "foo")); |
| 56 | + }); |
| 57 | + }); |
38 | 58 | });
|
0 commit comments