forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProtectableMap.test.ts
More file actions
83 lines (66 loc) · 2.71 KB
/
ProtectableMap.test.ts
File metadata and controls
83 lines (66 loc) · 2.71 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { ProtectableMap } from '../ProtectableMap';
class ExampleApi {
public clearedCount: number = 0;
public deletedCount: number = 0;
public setCount: number = 0;
private _studentAgesByName: ProtectableMap<string, number>;
public constructor() {
this._studentAgesByName = new ProtectableMap({
onClear: (source: ProtectableMap<string, number>) => {
++this.clearedCount;
},
onDelete: (source: ProtectableMap<string, number>, key: string) => {
++this.deletedCount;
},
onSet: (source: ProtectableMap<string, number>, key: string, value: number) => {
++this.setCount;
if (key.toUpperCase() !== key) {
throw new Error('The key must be all upper case: ' + key);
}
// If the provided value is negative, clamp it to zero:
return Math.max(value, 0);
}
});
}
public get studentAgesByName(): Map<string, number> {
return this._studentAgesByName.protectedView;
}
public doUnprotectedOperations(): void {
// These are unprotected because they interact with this._studentAgesByName
// instead of this._studentAgesByName.protectedView.
this._studentAgesByName.clear();
this._studentAgesByName.set('Dave', -123);
}
}
describe('ProtectableMap', () => {
test('Protected operations', () => {
const exampleApi: ExampleApi = new ExampleApi();
exampleApi.studentAgesByName.clear();
exampleApi.studentAgesByName.set('ALICE', 23);
exampleApi.studentAgesByName.set('BOB', 21);
exampleApi.studentAgesByName.set('BOB', -1);
exampleApi.studentAgesByName.set('CHARLIE', 22);
exampleApi.studentAgesByName.delete('CHARLIE');
expect(exampleApi.clearedCount).toEqual(1);
expect(exampleApi.setCount).toEqual(4);
expect(exampleApi.deletedCount).toEqual(1);
expect(exampleApi.studentAgesByName.get('ALICE')).toEqual(23);
expect(exampleApi.studentAgesByName.get('BOB')).toEqual(0); // clamped by onSet()
expect(exampleApi.studentAgesByName.has('CHARLIE')).toEqual(false);
});
test('Unprotected operations', () => {
const exampleApi: ExampleApi = new ExampleApi();
exampleApi.doUnprotectedOperations();
// Interacting directly with the ProtectableMap bypasses the hooks
expect(exampleApi.clearedCount).toEqual(0);
expect(exampleApi.studentAgesByName.get('Dave')).toEqual(-123);
});
test('Error case', () => {
const exampleApi: ExampleApi = new ExampleApi();
expect(() => {
exampleApi.studentAgesByName.set('Jane', 23);
}).toThrowError('The key must be all upper case: Jane');
});
});