-
Notifications
You must be signed in to change notification settings - Fork 24
/
test.js
171 lines (126 loc) · 5.71 KB
/
test.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
var obj, compiled;
module('Original Tests');
// QUnit
test('invalid patches', function() {
throws(function() {
jsonpatch.apply({}, [{foo: '/bar'}]);
}, jsonpatch.InvalidPatchError, 'Bad operation');
throws(function() {
jsonpatch.apply({}, [{op: 'add', path: ''}]);
}, jsonpatch.InvalidPatchError, 'Path must start with a /');
});
test('add', function() {
obj = {foo: 1, baz: [{qux: 'hello'}]};
jsonpatch.apply(obj, [{op: 'add', path: '/bar', value: [1, 2, 3, 4]}]);
deepEqual(obj, {foo: 1, baz: [{qux: 'hello'}], bar: [1, 2, 3, 4]});
jsonpatch.apply(obj, [{op: 'add', path: '/baz/0/foo', value: 'world'}]);
deepEqual(obj, {foo: 1, baz: [{qux: 'hello', foo: 'world'}], bar: [1, 2, 3, 4]});
raises(function() {
jsonpatch.apply(obj, [{op: 'add', path: '/bar/8', value: '5'}]);
}, jsonpatch.PatchConflictError, 'Out of bounds (upper)');
raises(function() {
jsonpatch.apply(obj, [{op: 'add', path: '/bar/-1', value: '5'}]);
}, jsonpatch.PatchConflictError, 'Out of bounds (lower)');
raises(function() {
jsonpatch.apply(obj, [{op: 'add', path: '/bar/8'}]);
}, jsonpatch.InvalidPatchError, 'Patch member value not defined');
obj = {foo: 1, baz: [{qux: 'hello'}]};
jsonpatch.apply(obj, [{op: 'add', path: '/bar', value: true}]);
deepEqual(obj, {foo: 1, baz: [{qux: 'hello'}], bar: true});
obj = {foo: 1, baz: [{qux: 'hello'}]};
jsonpatch.apply(obj, [{op: 'add', path: '/bar', value: false}]);
deepEqual(obj, {foo: 1, baz: [{qux: 'hello'}], bar: false});
obj = {foo: 1, baz: [{qux: 'hello'}]};
jsonpatch.apply(obj, [{op: 'add', path: '/bar', value: null}]);
deepEqual(obj, {foo: 1, baz: [{qux: 'hello'}], bar: null});
});
test('remove', function() {
obj = {foo: 1, baz: [{qux: 'hello'}], bar: [1, 2, 3, 4]};
jsonpatch.apply(obj, [{op: 'remove', path: '/bar'}]);
deepEqual(obj, {foo: 1, baz: [{qux: 'hello'}]});
jsonpatch.apply(obj, [{op: 'remove', path: '/baz/0/qux'}]);
deepEqual(obj, {foo: 1, baz: [{}]});
});
test('replace', function() {
obj = {foo: 1, baz: [{qux: 'hello'}]};
jsonpatch.apply(obj, [{op: 'replace', path: '/foo', value: [1, 2, 3, 4]}]);
deepEqual(obj, {foo: [1, 2, 3, 4], baz: [{qux: 'hello'}]});
jsonpatch.apply(obj, [{op: 'replace', path: '/baz/0/qux', value: 'world'}]);
deepEqual(obj, {foo: [1, 2, 3, 4], baz: [{qux: 'world'}]});
});
test('test', function() {
obj = {foo: {bar: [1, 2, 5, 4]}};
deepEqual(obj, jsonpatch.apply(obj, [{op: 'test', path: '/foo', value: {bar: [1, 2, 5, 4]}}]));
raises(function() {
jsonpatch.apply(obj, [{op: 'test', path: '/foo', value: [1, 2]}]);
}, jsonpatch.PatchTestFailed);
});
test('move', function() {
obj = {foo: 1, baz: [{qux: 'hello'}]};
jsonpatch.apply(obj, [{op: 'move', from: '/foo', path: '/bar'}]);
deepEqual(obj, {baz: [{qux: 'hello'}], bar: 1});
jsonpatch.apply(obj, [{op: 'move', from: '/baz/0/qux', path: '/baz/1'}]);
deepEqual(obj, {baz: [{}, 'hello'], bar: 1});
});
test('copy', function() {
obj = {foo: 1, baz: [{qux: 'hello'}]};
jsonpatch.apply(obj, [{op: 'copy', from: '/foo', path: '/bar'}]);
deepEqual(obj, {foo: 1, baz: [{qux: 'hello'}], bar: 1});
jsonpatch.apply(obj, [{op: 'copy', from: '/baz/0/qux', path: '/baz/1'}]);
deepEqual(obj, {foo: 1, baz: [{qux: 'hello'}, 'hello'], bar: 1});
});
// JSLitmus
JSLitmus.test('Add Operation', function() {
obj = {foo: 1, baz: [{qux: 'hello'}]};
jsonpatch.apply(obj, [{op: 'add', path: '/bar', value: [1, 2, 3, 4]}]);
});
JSLitmus.test('Remove Operation', function() {
obj = {foo: 1, baz: [{qux: 'hello'}], bar: [1, 2, 3, 4]};
jsonpatch.apply(obj, [{op: 'remove', path: '/bar'}]);
});
JSLitmus.test('Replace Operation', function() {
obj = {foo: 1, baz: [{qux: 'hello'}]};
jsonpatch.apply(obj, [{op: 'replace', path: '/foo', value: [1, 2, 3, 4]}]);
});
JSLitmus.test('Move Operation', function() {
obj = {foo: 1, baz: [{qux: 'hello'}], bar: [1, 2, 3, 4]};
jsonpatch.apply(obj, [{op: 'move', from: '/baz/0', path: '/bar/0'}]);
});
JSLitmus.test('Copy Operation', function() {
obj = {foo: 1, baz: [{qux: 'hello'}], bar: [1, 2, 3, 4]};
jsonpatch.apply(obj, [{op: 'copy', from: '/baz/0', path: '/bar/0'}]);
});
JSLitmus.test('Test Operation', function() {
obj = {foo: 1, baz: [{qux: 'hello'}]};
jsonpatch.apply(obj, [{op: 'test', path: '/baz', value: [{qux: 'hello'}]}]);
});
var addCompiled = jsonpatch.compile([{op: 'add', path: '/bar', value: [1, 2, 3, 4]}]);
JSLitmus.test('Compiled Add Operation', function() {
obj = {foo: 1, baz: [{qux: 'hello'}]};
addCompiled(obj);
});
var removeCompiled = jsonpatch.compile([{op: 'remove', path: '/bar'}]);
JSLitmus.test('Compiled Remove Operation', function() {
obj = {foo: 1, baz: [{qux: 'hello'}], bar: [1, 2, 3, 4]};
removeCompiled(obj);
});
var replaceCompiled = jsonpatch.compile([{op: 'replace', path: '/foo', value: [1, 2, 3, 4]}]);
JSLitmus.test('Compiled Replace Operation', function() {
obj = {foo: 1, baz: [{qux: 'hello'}]};
replaceCompiled(obj);
});
var moveCompiled = jsonpatch.compile([{op: 'move', from: '/baz/0', path: '/bar/0'}]);
JSLitmus.test('Compiled Move Operation', function() {
obj = {foo: 1, baz: [{qux: 'hello'}], bar: [1, 2, 3, 4]};
moveCompiled(obj);
});
var copyCompiled = jsonpatch.compile([{op: 'copy', from: '/baz/0', path: '/bar/0'}]);
JSLitmus.test('Compiled Copy Operation', function() {
obj = {foo: 1, baz: [{qux: 'hello'}], bar: [1, 2, 3, 4]};
copyCompiled(obj);
});
var testCompiled = jsonpatch.compile([{op: 'test', path: '/baz', value: [{qux: 'hello'}]}]);
JSLitmus.test('Compiled Test Operation', function() {
obj = {foo: 1, baz: [{qux: 'hello'}]};
testCompiled(obj);
});