-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathjson.test.ts
119 lines (109 loc) · 4.07 KB
/
json.test.ts
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { test, expect } from "@jest/globals";
import {
JsonListKeysTool,
JsonSpec,
JsonGetValueTool,
} from "../../tools/json.js";
test("JsonListKeysTool", async () => {
const jsonSpec = new JsonSpec({
foo: "bar",
baz: { test: { foo: [1, 2, 3], qux: [{ x: 1, y: 2, z: 3 }, { a: 1 }] } },
});
const jsonListKeysTool = new JsonListKeysTool(jsonSpec);
expect(await jsonListKeysTool.invoke("")).toBe("foo, baz");
expect(await jsonListKeysTool.invoke("/foo")).toContain("not a dictionary");
expect(await jsonListKeysTool.invoke("/baz")).toBe("test");
expect(await jsonListKeysTool.invoke("/baz/test")).toBe("foo, qux");
expect(await jsonListKeysTool.invoke("/baz/test/foo")).toContain(
"not a dictionary"
);
expect(await jsonListKeysTool.invoke("/baz/test/foo/0")).toContain(
"not a dictionary"
);
expect(await jsonListKeysTool.invoke("/baz/test/qux")).toContain(
"not a dictionary"
);
expect(await jsonListKeysTool.invoke("/baz/test/qux/0")).toBe("x, y, z");
expect(await jsonListKeysTool.invoke("/baz/test/qux/1")).toBe("a");
expect(await jsonListKeysTool.invoke("/bar")).toContain("not a dictionary");
});
test("JsonListKeysTool, paths containing escaped characters", async () => {
const jsonSpec = new JsonSpec({
paths: {
"a~b": 1,
"a/b": 2,
"a~/b": 3,
"a//~b": 4,
},
});
const jsonListKeyTool = new JsonListKeysTool(jsonSpec);
expect(await jsonListKeyTool.invoke("/paths")).toBe(
"a~0b, a~1b, a~0~1b, a~1~1~0b"
);
});
test("JsonGetValueTool", async () => {
const jsonSpec = new JsonSpec({
foo: "bar",
baz: { test: { foo: [1, 2, 3], qux: [{ x: 1, y: 2, z: 3 }, { a: 1 }] } },
});
const jsonGetValueTool = new JsonGetValueTool(jsonSpec);
expect(await jsonGetValueTool.invoke("")).toBe(
`{"foo":"bar","baz":{"test":{"foo":[1,2,3],"qux":[{"x":1,"y":2,"z":3},{"a":1}]}}}`
);
expect(await jsonGetValueTool.invoke("/foo")).toBe("bar");
expect(await jsonGetValueTool.invoke("/baz")).toBe(
`{"test":{"foo":[1,2,3],"qux":[{"x":1,"y":2,"z":3},{"a":1}]}}`
);
expect(await jsonGetValueTool.invoke("/baz/test")).toBe(
`{"foo":[1,2,3],"qux":[{"x":1,"y":2,"z":3},{"a":1}]}`
);
expect(await jsonGetValueTool.invoke("/baz/test/foo")).toBe("[1,2,3]");
expect(await jsonGetValueTool.invoke("/baz/test/foo/0")).toBe("1");
expect(await jsonGetValueTool.invoke("/baz/test/qux")).toBe(
`[{"x":1,"y":2,"z":3},{"a":1}]`
);
expect(await jsonGetValueTool.invoke("/baz/test/qux/0")).toBe(
`{"x":1,"y":2,"z":3}`
);
expect(await jsonGetValueTool.invoke("/baz/test/qux/0/x")).toBe("1");
expect(await jsonGetValueTool.invoke("/baz/test/qux/1")).toBe(`{"a":1}`);
expect(await jsonGetValueTool.invoke("/bar")).toContain(`null`);
});
test("JsonGetValueTool, large values", async () => {
const jsonSpec = new JsonSpec(
{ foo: "bar", baz: { test: { foo: [1, 2, 3, 4] } } },
5
);
const jsonGetValueTool = new JsonGetValueTool(jsonSpec);
expect(await jsonGetValueTool.invoke("")).toContain("large dictionary");
expect(await jsonGetValueTool.invoke("/foo")).toBe("bar");
expect(await jsonGetValueTool.invoke("/baz")).toContain("large dictionary");
expect(await jsonGetValueTool.invoke("/baz/test")).toContain(
"large dictionary"
);
expect(await jsonGetValueTool.invoke("/baz/test/foo")).toBe("[1,2,...");
expect(await jsonGetValueTool.invoke("/baz/test/foo/0")).toBe("1");
});
test("JsonGetValueTool, paths containing escaped characters", async () => {
const jsonSpec = new JsonSpec({
paths: {
"~IDSGenericFXCrossRate": 1,
"/IDSGenericFXCrossRate": 2,
"~/IDSGenericFXCrossRate": 3,
"/~IDSGenericFXCrossRate": 4,
},
});
const jsonGetValueTool = new JsonGetValueTool(jsonSpec);
expect(await jsonGetValueTool.invoke("/paths/~0IDSGenericFXCrossRate")).toBe(
"1"
);
expect(await jsonGetValueTool.invoke("/paths/~1IDSGenericFXCrossRate")).toBe(
"2"
);
expect(
await jsonGetValueTool.invoke("/paths/~0~1IDSGenericFXCrossRate")
).toBe("3");
expect(
await jsonGetValueTool.invoke("/paths/~1~0IDSGenericFXCrossRate")
).toBe("4");
});