-
Notifications
You must be signed in to change notification settings - Fork 7
/
comparator.spec.ts
338 lines (307 loc) · 12.4 KB
/
comparator.spec.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* Portions of this software was developed by employees of the National Institute
* of Standards and Technology (NIST), an agency of the Federal Government and is
* being made available as a public service. Pursuant to title 17 United States
* Code Section 105, works of NIST employees are not subject to copyright
* protection in the United States. This software may be subject to foreign
* copyright. Permission in the United States and in foreign countries, to the
* extent that NIST may hold copyright, to use, copy, modify, create derivative
* works, and distribute this software and its documentation without fee is hereby
* granted on a non-exclusive basis, provided that this notice and disclaimer
* of warranty appears in all copies.
*
* THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
* EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
* THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
* INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
* SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT
* SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
* INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
* OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
* CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
* PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
* OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
*/
import { expect } from 'chai';
import Comparator from './comparator';
import { BASE_SETTINGS, mergePartialComparatorStepConfigs } from './configuration';
import { ArrayChanged, PropertyChanged } from './results';
import { trackRawObject, TrackedArray } from './utils/tracked';
/**
* Comparator with default options to test "default" behavior
*/
const defaultComparator = new Comparator();
type CompareParams = Parameters<typeof defaultComparator.compare>;
type CompareElementsParams = Parameters<typeof defaultComparator['compareElements']>;
/**
* Convert Comparator.compare() parameters into Comparator.compareElements() parameters
*/
function trackCompareParams([l, , r]: CompareParams): CompareElementsParams {
return [trackRawObject('', l), trackRawObject('', r)];
}
describe('Comparator Comparison', () => {
const identical_obj = {
id: 1,
title: 'The quick brown fox',
data: {
nested_id: 1,
nested_title: 'The quick brown fox',
},
subItems: [
{
id: 2,
title: 'Subitem 1',
},
{
id: 3,
title: 'Subitem 2',
},
],
};
const identical: CompareParams = [identical_obj, 'identical_left', identical_obj, 'identical_right'];
it('Comparator.compare() on Identical Objects', () => {
const results = defaultComparator.compare(...identical);
expect(results.changes).to.have.length(1, 'comparison result should have one element');
const subchanges = results.changes[0];
expect(subchanges).instanceOf(ArrayChanged);
if (subchanges instanceof ArrayChanged) {
expect(subchanges.subChanges).to.have.length(2), 'both array items should be matched';
expect(subchanges.leftOnly).to.have.length(0, 'no unmatched left elements');
expect(subchanges.rightOnly).to.have.length(0, 'no unmatched right elements');
subchanges.subChanges.map((subChange) => {
expect(subChange.changes).to.have.length(
0,
`element pair ${subChange.leftPointer}:${subChange.rightPointer} should be identical`,
);
});
}
});
it('Comparator.compareElements() on Identical Objects', () => {
const [changes, cost] = defaultComparator['compareElements'](...trackCompareParams(identical));
expect(changes).to.have.length(1); // just a sanity check
expect(cost).to.equal(0, 'cost for identical objects should be 0');
});
const simple_array_difference: CompareParams = [
[
{
id: 0,
title: 'Element 1',
},
{
id: 1,
title: 'Element 2',
},
],
'array_changed_left',
[
{
id: 0,
title: 'Element 1',
},
{
id: 1,
title: 'Element 2 - Changed',
},
{
id: 2,
title: 'Element 3',
},
],
'array_changed_right',
];
it('Comparator.compare() on Array Difference', () => {
const results = defaultComparator.compare(...simple_array_difference);
expect(results.changes).to.have.length(1);
const arrayChanges = results.changes[0];
expect(arrayChanges).instanceOf(ArrayChanged);
if (arrayChanges instanceof ArrayChanged) {
expect(arrayChanges.leftOnly).to.have.length(0, 'there should be no unmatched left elements');
expect(arrayChanges.rightOnly).to.have.length(1, 'there should be one unmatched right element');
expect(arrayChanges.rightOnly[0].rightPointer).to.equal('/2');
expect(arrayChanges.subChanges).to.have.length(2, 'there should be two matched elements');
expect(arrayChanges.subChanges[0].leftPointer).to.equal('/0');
expect(arrayChanges.subChanges[0].rightPointer).to.equal('/0');
expect(arrayChanges.subChanges[0].changes).to.have.length(0);
expect(arrayChanges.subChanges[1].leftPointer).to.equal('/1');
expect(arrayChanges.subChanges[1].rightPointer).to.equal('/1');
expect(arrayChanges.subChanges[1].changes).to.have.length(1);
expect(arrayChanges.subChanges[1].changes[0]).instanceOf(PropertyChanged);
if (arrayChanges.subChanges[1].changes[0] instanceof PropertyChanged) {
expect(arrayChanges.subChanges[1].changes[0].leftElement).to.equal('Element 2');
expect(arrayChanges.subChanges[1].changes[0].rightElement).to.equal('Element 2 - Changed');
}
}
});
it('Comparator.compareElement() on Array Difference', () => {
const [changes, cost] = defaultComparator['compareElements'](...trackCompareParams(simple_array_difference));
expect(changes).to.have.length(1); // just a sanity check
expect(cost).to.be.greaterThan(2, "cost should be at least 2 accounting for Element 3's properties");
expect(cost).to.be.lessThanOrEqual(
4,
"cost should be no more than 3 accounting for Element 2's changed property",
);
});
it('Comparator.compare() on property exclusion', () => {
const result = defaultComparator.compare(
{ onlyOnLeft: true },
'prop_excluded_l',
{ onlyOnRight: true },
'prop_excluded_r',
);
expect(result.changes).to.have.lengthOf(2);
});
it('Comparator.compare() on invalid comparison types', () => {
expect(() => defaultComparator.compare([], 'invalid_l', {}, 'invalid_r')).to.throw();
});
});
describe('Comparator ignore option', () => {
const ignoreIdComparator = new Comparator({
'*': {
priority: 1,
ignore: ['id', 'ignoreme'],
},
});
// Ignore id on top level
it('should work on simple object properties', () => {
const simpleObject: CompareParams = [{ id: 1 }, 'simple_object_left', { id: 2 }, 'simple_object_right'];
const results = ignoreIdComparator.compare(...simpleObject);
expect(results.changes).to.have.length(0);
});
// Ignore id propagates downwards
it('should work on nested object properties', () => {
const nestedObject: CompareParams = [
{ test: 1, sub: { id: 1 } },
'nested_object_left',
{ test: 1, sub: { id: 2 } },
'nested_object_right',
];
const results = ignoreIdComparator.compare(...nestedObject);
expect(results.changes).to.have.length(0);
});
// Ignore id propagates through arrays
it('should work on array element properties', () => {
const arrayObject: CompareParams = [
[
{ test: 1, id: 2 },
{ test: 2, id: 1 },
],
'array_left',
[{ test: 1, id: 1 }, { test: 2 }],
'array_right',
];
const results = ignoreIdComparator.compare(...arrayObject);
expect(results.changes).to.have.length(1);
const arrayChanges = results.changes[0];
expect(arrayChanges).to.be.instanceOf(ArrayChanged);
if (arrayChanges instanceof ArrayChanged) {
expect(arrayChanges.leftOnly).to.have.length(0);
expect(arrayChanges.rightOnly).to.have.length(0);
// they should be matched to each other
expect(arrayChanges.subChanges).to.deep.equal([
{
leftPointer: '/0',
rightPointer: '/0',
changes: [],
score: 0,
},
{
leftPointer: '/1',
rightPointer: '/1',
changes: [],
score: 0,
},
]);
}
});
it('should ignore subobjects', () => {
const ignoredSubObjects: CompareParams = [
{
ignoreme: {
test: 'one',
},
},
'ignoredSubObject_left',
{
ignoreme: {
test: 'two',
},
},
'ignoredSubObject_right',
];
const results = ignoreIdComparator.compare(...ignoredSubObjects);
expect(results.changes).to.have.length(0);
});
});
describe('Comparator out of tree', () => {
const left = new TrackedArray('', [
{
id: 1,
subObjs: [
{
obj: 1,
},
],
},
{
id: 2,
subObjs: [
{
obj: 2,
},
],
},
]);
const right = new TrackedArray('', [
{
id: 1,
subObjs: [
{
obj: 2,
},
],
},
{
id: 2,
subObjs: [
{
obj: 1,
},
],
},
]);
it('Should not match out of tree when disabled', () => {
const [changes, count] = defaultComparator['compareArrays'](left, right, BASE_SETTINGS);
// greater arrays matched, but minor arrays are not
expect(count).to.equal(4);
expect(changes).to.have.length(1);
expect(changes[0]).instanceOf(ArrayChanged);
if (changes[0] instanceof ArrayChanged) {
expect(changes[0].leftOnly).to.have.length(0);
expect(changes[0].rightOnly).to.have.length(0);
expect(changes[0].subChanges).to.have.length(2);
expect(changes[0].subChanges[0].score).to.equal(2);
expect(changes[0].subChanges[1].score).to.equal(2);
expect(changes[0].outOfTreeChanges).to.have.length(0);
}
});
it('Should match out of tree when enabled', () => {
const [changes, count] = defaultComparator['compareArrays'](
left,
right,
mergePartialComparatorStepConfigs(BASE_SETTINGS, { priority: 1, outOfTreeEnabled: true }),
);
// greater arrays matched, minor arrays are matched out of tree
expect(count).to.equal(0);
expect(changes).to.have.length(1);
expect(changes[0]).instanceOf(ArrayChanged);
if (changes[0] instanceof ArrayChanged) {
expect(changes[0].leftOnly).to.have.length(0);
expect(changes[0].rightOnly).to.have.length(0);
expect(changes[0].subChanges).to.have.length(2);
expect(changes[0].subChanges[0].score).to.equal(0);
expect(changes[0].subChanges[1].score).to.equal(0);
expect(changes[0].outOfTreeChanges).to.have.length(2);
}
});
});