-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvariable-manager.test.ts
382 lines (307 loc) · 14.4 KB
/
variable-manager.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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// tslint:disable:no-invalid-template-strings
import { EMPTY, Subject } from 'rxjs';
import { BeforeModelDestroyedEvent } from '../../model/events/before-model-destroyed-event';
import { ModelChangedEvent } from '../../model/events/model-changed-event';
import { ModelManager } from '../../model/manager/model-manager';
import { PropertyLocation } from '../../model/property/property-location';
import { PartialObjectMock } from '../../test/partial-object-mock';
import { Logger } from '../../util/logging/logger';
import { VariableManager } from './variable-manager';
describe('Variable manager', () => {
let manager: VariableManager;
let mockLogger: Partial<Logger>;
let mockModelManager: Partial<ModelManager>;
let mockModelChangedEvent: Partial<ModelChangedEvent>;
let mockBeforeModelDestroyedEvent: Partial<BeforeModelDestroyedEvent>;
const model = {};
const parent = {};
const root = {};
beforeEach(() => {
mockLogger = {
warn: jest.fn(),
error: jest.fn()
};
mockModelChangedEvent = {
publishChange: jest.fn()
};
mockModelManager = {
getParent: jest.fn().mockImplementation(inputModel => {
if (inputModel === model) {
return parent;
}
if (inputModel === parent) {
return root;
}
return undefined;
})
};
mockBeforeModelDestroyedEvent = {
getBeforeDestructionObservable: jest.fn()
};
manager = new VariableManager(
mockLogger as Logger,
mockModelManager as ModelManager,
mockModelChangedEvent as ModelChangedEvent,
mockBeforeModelDestroyedEvent as BeforeModelDestroyedEvent
);
});
test('should support basic read/write', () => {
manager.set('a', 'a value', model);
manager.set('b', 'b value', model);
manager.set('c', 'c value', model);
expect(manager.get('a', model)).toBe('a value');
expect(manager.get('b', model)).toBe('b value');
expect(manager.get('c', model)).toBe('c value');
});
test('should return undefined and warn for an unknown key', () => {
expect(manager.get('a', model)).toBe(undefined);
expect(mockLogger.warn).toHaveBeenCalledWith('Attempting to lookup unassigned variable: a');
});
test('should allow overwrite of existing variable', () => {
manager.set('a', 'a value', model);
manager.set('a', 'new a value', model);
expect(manager.get('a', model)).toBe('new a value');
});
test('should resolve to higher scopes', () => {
manager.set('a', 'a value', model);
manager.set('b', 'b value', parent);
manager.set('c', 'c value', root);
expect(manager.get('a', model)).toBe('a value');
expect(manager.get('b', model)).toBe('b value');
expect(manager.get('c', model)).toBe('c value');
});
test('should support variable shadowing', () => {
manager.set('a', 'a value', model);
manager.set('a', "parent's a value", parent);
expect(manager.get('a', model)).toBe('a value');
expect(manager.get('a', parent)).toBe("parent's a value");
expect(manager.get('a', root)).toBeUndefined();
});
test('should determine if a key is contained in the provided model scope', () => {
manager.set('a', 'a value', root);
manager.set('b', "parent's b value", parent);
expect(manager.has('a', root)).toBe(true);
expect(manager.has('a', model)).toBe(true);
expect(manager.has('b', root)).toBe(false);
expect(manager.has('b', parent)).toBe(true);
expect(manager.has('b', model)).toBe(true);
expect(manager.has('c', model)).toBe(false);
});
test('should treat an undefined variable the same as none at all', () => {
manager.set('a', undefined, root);
expect(manager.has('a', root)).toBe(false);
});
});
describe('Variable manager reference tracking', () => {
// Not mocking variable reference, we want to test a little more integration here
let manager: VariableManager;
let mockLogger: PartialObjectMock<Logger>;
let mockModelManager: PartialObjectMock<ModelManager>;
let mockModelChangedEvent: PartialObjectMock<ModelChangedEvent>;
let mockBeforeModelDestroyedEvent: Partial<BeforeModelDestroyedEvent>;
let mockParentLocation: PartialObjectMock<PropertyLocation>;
const parent = {};
let mockModelLocation: PartialObjectMock<PropertyLocation>;
const model = {};
beforeEach(() => {
mockModelChangedEvent = { publishChange: jest.fn() };
mockLogger = {
error: jest.fn()
};
mockModelManager = {
getParent: jest.fn().mockImplementation(inputModel => {
if (inputModel === model) {
return parent;
}
return undefined;
}),
getRoot: jest.fn().mockReturnValue(parent),
isAncestor: jest.fn(
(amodel: object, potentialAncestor: object) => amodel === model && potentialAncestor === parent
)
};
mockBeforeModelDestroyedEvent = {
getBeforeDestructionObservable: jest.fn().mockReturnValue(EMPTY)
};
mockModelLocation = {
parentModel: model,
setProperty: jest.fn(),
toString: () => 'modelProp'
};
mockParentLocation = {
parentModel: parent,
setProperty: jest.fn(),
toString: () => 'parentProp'
};
manager = new VariableManager(
mockLogger as Logger,
mockModelManager as ModelManager,
mockModelChangedEvent as ModelChangedEvent,
mockBeforeModelDestroyedEvent as BeforeModelDestroyedEvent
);
});
test('should be able to register references', () => {
manager.registerReference(mockModelLocation as PropertyLocation, '${test}');
expect(manager.isVariableReference(mockModelLocation as PropertyLocation)).toBe(true);
expect(manager.isVariableReference(mockParentLocation as PropertyLocation)).toBe(false);
manager.registerReference(mockParentLocation as PropertyLocation, '${test}');
expect(manager.isVariableReference(mockParentLocation as PropertyLocation)).toBe(true);
});
test('should log error if registering twice to the same location', () => {
manager.registerReference(mockModelLocation as PropertyLocation, '${test}');
manager.registerReference(mockModelLocation as PropertyLocation, '${test}');
expect(mockLogger.error).toHaveBeenCalledWith(
'Attempting to register reference which has already been declared at modelProp'
);
});
test('should update existing references when setting a variable', () => {
manager.registerReference(mockModelLocation as PropertyLocation, '${test}');
expect(mockModelChangedEvent.publishChange).nthCalledWith(1, model);
manager.set('test', 'foo', model);
expect(mockModelLocation.setProperty).toHaveBeenLastCalledWith('foo');
expect(mockModelChangedEvent.publishChange).nthCalledWith(2, model);
manager.set('test', 'baz', model);
expect(mockModelLocation.setProperty).toHaveBeenLastCalledWith('baz');
expect(mockModelChangedEvent.publishChange).nthCalledWith(3, model);
expect(mockModelChangedEvent.publishChange).toHaveBeenCalledTimes(3);
});
test('should shadow existing variables when setting at a more specific scope', () => {
manager.registerReference(mockModelLocation as PropertyLocation, '${test}');
expect(mockModelLocation.setProperty).toHaveBeenNthCalledWith(1, undefined);
manager.set('test', 'foo', parent);
expect(mockModelLocation.setProperty).toHaveBeenNthCalledWith(2, 'foo');
manager.set('test', 'baz', model);
expect(mockModelLocation.setProperty).toHaveBeenNthCalledWith(3, 'baz');
manager.set('test', 'more baz', model);
expect(mockModelLocation.setProperty).toHaveBeenNthCalledWith(4, 'more baz');
manager.set('test', 'bar', parent);
// Not called again
expect(mockModelLocation.setProperty).toHaveBeenCalledTimes(4);
});
test('should not shadow references for parent models', () => {
manager.registerReference(mockModelLocation as PropertyLocation, '${test}');
manager.registerReference(mockParentLocation as PropertyLocation, '${test}');
manager.set('test', 'baz', model);
expect(mockModelLocation.setProperty).toHaveBeenLastCalledWith('baz');
expect(mockParentLocation.setProperty).toHaveBeenLastCalledWith(undefined);
manager.set('test', 'foo', parent);
expect(mockModelLocation.setProperty).toHaveBeenLastCalledWith('baz');
expect(mockParentLocation.setProperty).toHaveBeenLastCalledWith('foo');
});
test('should track references in complex variables correctly', () => {
manager.registerReference(mockModelLocation as PropertyLocation, '${${a}${b}}');
expect(mockModelLocation.setProperty).toHaveBeenNthCalledWith(1, undefined);
manager.set('foobar', 'baz', model);
manager.set('b', 'bar', model);
expect(mockModelLocation.setProperty).toHaveBeenNthCalledWith(2, undefined);
manager.set('a', 'foo', model);
expect(mockModelLocation.setProperty).toHaveBeenNthCalledWith(3, 'baz');
manager.set('b', 'c', model);
expect(mockModelLocation.setProperty).toHaveBeenNthCalledWith(4, undefined);
manager.set('b', 'd', model);
expect(mockModelLocation.setProperty).toHaveBeenNthCalledWith(5, undefined);
manager.set('food', 'ta da', model);
expect(mockModelLocation.setProperty).toHaveBeenNthCalledWith(6, 'ta da');
});
test('should track references in interpolated variable strings correctly', () => {
manager.registerReference(mockModelLocation as PropertyLocation, '${a} and ${b}');
expect(mockModelLocation.setProperty).toHaveBeenNthCalledWith(1, undefined);
manager.set('b', 'bar', model);
expect(mockModelLocation.setProperty).toHaveBeenNthCalledWith(2, undefined);
manager.set('a', 'foo', model);
expect(mockModelLocation.setProperty).toHaveBeenNthCalledWith(3, 'foo and bar');
});
test('should handle deregistration of references', () => {
manager.registerReference(mockModelLocation as PropertyLocation, '${test}');
expect(mockModelLocation.setProperty).toHaveBeenNthCalledWith(1, undefined);
expect(manager.isVariableReference(mockModelLocation as PropertyLocation)).toBe(true);
expect(manager.deregisterReference(mockModelLocation as PropertyLocation)).toBe('${test}');
expect(manager.isVariableReference(mockModelLocation as PropertyLocation)).toBe(false);
manager.set('test', 'foo', model);
expect(mockModelLocation.setProperty).toHaveBeenCalledTimes(1);
});
test('deregister throws error if location does contain reference', () => {
mockLogger.error = jest.fn(() => ({
throw: () => {
throw Error();
}
}));
expect(() => manager.deregisterReference(mockModelLocation as PropertyLocation)).toThrow();
expect(mockLogger.error)
// tslint:disable-next-line:max-line-length
.toHaveBeenCalledWith(
'Attempted to deregister reference at modelProp which does not contain a registered reference'
);
});
test('can determine strings containing a variable expression', () => {
expect(manager.isVariableExpression('test')).toBe(false);
expect(manager.isVariableExpression('${test}')).toBe(true);
expect(manager.isVariableExpression('test ${test}')).toBe(true);
expect(manager.isVariableExpression('{}')).toBe(false);
expect(manager.isVariableExpression('')).toBe(false);
expect(manager.isVariableExpression('${${nes}${ted}}')).toBe(true);
expect(manager.isVariableExpression('${}')).toBe(true);
expect(manager.isVariableExpression('some ${bad')).toBe(true);
expect(manager.isVariableExpression('${${nested}${bad}')).toBe(true);
});
test('registering a reference returns the resolved value', () => {
expect(manager.registerReference(mockModelLocation as PropertyLocation, '${test}')).toBeUndefined();
manager.set('test', 'foo', parent);
expect(manager.registerReference(mockParentLocation as PropertyLocation, '${test}')).toBe('foo');
manager.deregisterReference(mockParentLocation as PropertyLocation);
manager.set('bar', 'baz', parent);
expect(manager.registerReference(mockParentLocation as PropertyLocation, '${test} + ${bar}')).toBe('foo + baz');
});
test('allows retrieving a variable expression without deregistering', () => {
manager.registerReference(mockModelLocation as PropertyLocation, 'Hi: ${test}');
manager.set('test', 'foo', model);
expect(mockModelLocation.setProperty).lastCalledWith('Hi: foo');
expect(manager.getVariableExpressionFromLocation(mockModelLocation as PropertyLocation)).toBe('Hi: ${test}');
manager.set('test', 'bar', model);
expect(mockModelLocation.setProperty).lastCalledWith('Hi: bar');
});
test('getVariableExpressionFromLocation throws error if location does contain reference', () => {
mockLogger.error = jest.fn(() => ({
throw: () => {
throw Error();
}
}));
expect(() => manager.getVariableExpressionFromLocation(mockModelLocation as PropertyLocation)).toThrow();
expect(mockLogger.error)
// tslint:disable-next-line:max-line-length
.toHaveBeenCalledWith(
'Attempted to resolve reference at modelProp which does not contain a registered reference'
);
});
test('removes dangling references after a model is destroyed', () => {
const destroySubject = new Subject();
mockBeforeModelDestroyedEvent.getBeforeDestructionObservable = jest
.fn()
.mockReturnValue(destroySubject.asObservable());
manager.registerReference(mockModelLocation as PropertyLocation, '${test}');
manager.set('test', 'foo', parent);
destroySubject.next(model);
destroySubject.complete();
// Reference should no longer be tracked
expect(manager.isVariableReference(mockModelLocation as PropertyLocation)).toBe(false);
// Setting a value should not call model manager or set property
(mockModelManager.getParent as jest.Mock).mockClear();
manager.set('test', 'bar', parent);
expect(mockModelManager.getParent).not.toHaveBeenCalled();
expect(mockModelLocation.setProperty).not.toHaveBeenCalledWith('bar');
});
test('dangling reference removal handles case where reference previously removed', () => {
const destroySubject = new Subject();
mockBeforeModelDestroyedEvent.getBeforeDestructionObservable = jest
.fn()
.mockReturnValue(destroySubject.asObservable());
manager.registerReference(mockModelLocation as PropertyLocation, '${test}');
manager.set('test', 'foo', parent);
manager.deregisterReference(mockModelLocation as PropertyLocation);
// Reference should no longer be tracked
expect(manager.isVariableReference(mockModelLocation as PropertyLocation)).toBe(false);
destroySubject.next(model);
destroySubject.complete();
expect(mockLogger.error).not.toHaveBeenCalled();
});
});