-
Notifications
You must be signed in to change notification settings - Fork 18
/
relative-json-pointer.spec.js
119 lines (102 loc) · 3.35 KB
/
relative-json-pointer.spec.js
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 { expect } from 'chai';
import { resolveRelativeJsonPointer } from '../../src/relative-json-pointer.js';
describe('relative json pointer util', function () {
let data;
beforeEach(function () {
data = {
foo: ['bar', 'baz'],
highly: {
nested: {
objects: true,
arrayinarray: [1, [11, 12], [3, 4, 5]],
},
},
};
});
describe('resolve to value', function () {
it('resolves 0 from root', function () {
expect(resolveRelativeJsonPointer(data, '/', '0')).to.eql(data);
});
it('resolves 0', function () {
expect(resolveRelativeJsonPointer(data, '/foo/1', '0')).to.eql('baz');
});
it('resolves 1', function () {
expect(resolveRelativeJsonPointer(data, '/foo/1', '1')).to.eql([
'bar',
'baz',
]);
});
it('resolves 1/0', function () {
expect(resolveRelativeJsonPointer(data, '/foo/1', '1/0')).to.eql('bar');
});
it('resolves 1/highly/nested/objects', function () {
expect(
resolveRelativeJsonPointer(data, '/foo/1', '2/highly/nested/objects')
).to.eql(true);
});
it('resolves from within array to sibling array in parent', () => {
expect(
resolveRelativeJsonPointer(data, '/highly/nested/arrayinarray/2/2', '0')
).to.eql(5, 'from 5 to itself');
expect(
resolveRelativeJsonPointer(
data,
'/highly/nested/arrayinarray/2/2',
'1/1'
)
).to.eql(4, 'from 5 to 4');
expect(
resolveRelativeJsonPointer(
data,
'/highly/nested/arrayinarray/2/2',
'2/1/1'
)
).to.eql(12, 'from 5 to 12');
});
it('does not throw if 0 value and root reference', function () {
expect(resolveRelativeJsonPointer(data, '/', '0')).to.eql(data);
});
it('throws if with leading zeros', () => {
expect(function () {
resolveRelativeJsonPointer(data, '/foo', '01');
}).to.throw(/invalid relative pointer/i);
});
it('throws if non number', () => {
expect(function () {
resolveRelativeJsonPointer(data, '/foo', 'd');
}).to.throw(/invalid relative pointer/i);
});
it('throws when trying to go above the root', function () {
expect(function () {
resolveRelativeJsonPointer(data, '/', '1');
}).to.throw(/trying to reference/i);
expect(function () {
resolveRelativeJsonPointer(data, '/foo/1', '3');
}).to.throw(/trying to reference value above root/i);
});
it('throws if trying to get key of root', function () {
expect(function () {
resolveRelativeJsonPointer(data, '/', '0#');
}).to.throw();
});
it('resolves 0/objects', function () {
expect(
resolveRelativeJsonPointer(data, '/highly/nested', '0/objects')
).to.eql(true);
});
});
describe('resolve to property name', function () {
it('resolves to index in array', function () {
expect(resolveRelativeJsonPointer(data, '/foo/1', '0#')).to.eql(1);
});
it('resolves to property name', function () {
expect(resolveRelativeJsonPointer(data, '/foo/1', '1#')).to.eql('foo');
expect(resolveRelativeJsonPointer(data, '/highly/nested', '0#')).to.eql(
'nested'
);
expect(resolveRelativeJsonPointer(data, '/highly/nested', '1#')).to.eql(
'highly'
);
});
});
});