Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/test/common/services/semanticTokensDto.test.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import assert from 'assert';
7
import { IFullSemanticTokensDto, IDeltaSemanticTokensDto, encodeSemanticTokensDto, ISemanticTokensDto, decodeSemanticTokensDto } from '../../../common/services/semanticTokensDto.js';
8
import { VSBuffer } from '../../../../base/common/buffer.js';
9
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';
10
11
suite('SemanticTokensDto', () => {
12
13
ensureNoDisposablesAreLeakedInTestSuite();
14
15
function toArr(arr: Uint32Array): number[] {
16
const result: number[] = [];
17
for (let i = 0, len = arr.length; i < len; i++) {
18
result[i] = arr[i];
19
}
20
return result;
21
}
22
23
function assertEqualFull(actual: IFullSemanticTokensDto, expected: IFullSemanticTokensDto): void {
24
const convert = (dto: IFullSemanticTokensDto) => {
25
return {
26
id: dto.id,
27
type: dto.type,
28
data: toArr(dto.data)
29
};
30
};
31
assert.deepStrictEqual(convert(actual), convert(expected));
32
}
33
34
function assertEqualDelta(actual: IDeltaSemanticTokensDto, expected: IDeltaSemanticTokensDto): void {
35
const convertOne = (delta: { start: number; deleteCount: number; data?: Uint32Array }) => {
36
if (!delta.data) {
37
return delta;
38
}
39
return {
40
start: delta.start,
41
deleteCount: delta.deleteCount,
42
data: toArr(delta.data)
43
};
44
};
45
const convert = (dto: IDeltaSemanticTokensDto) => {
46
return {
47
id: dto.id,
48
type: dto.type,
49
deltas: dto.deltas.map(convertOne)
50
};
51
};
52
assert.deepStrictEqual(convert(actual), convert(expected));
53
}
54
55
function testRoundTrip(value: ISemanticTokensDto): void {
56
const decoded = decodeSemanticTokensDto(encodeSemanticTokensDto(value));
57
if (value.type === 'full' && decoded.type === 'full') {
58
assertEqualFull(decoded, value);
59
} else if (value.type === 'delta' && decoded.type === 'delta') {
60
assertEqualDelta(decoded, value);
61
} else {
62
assert.fail('wrong type');
63
}
64
}
65
66
test('full encoding', () => {
67
testRoundTrip({
68
id: 12,
69
type: 'full',
70
data: new Uint32Array([(1 << 24) + (2 << 16) + (3 << 8) + 4])
71
});
72
});
73
74
test('delta encoding', () => {
75
testRoundTrip({
76
id: 12,
77
type: 'delta',
78
deltas: [{
79
start: 0,
80
deleteCount: 4,
81
data: undefined
82
}, {
83
start: 15,
84
deleteCount: 0,
85
data: new Uint32Array([(1 << 24) + (2 << 16) + (3 << 8) + 4])
86
}, {
87
start: 27,
88
deleteCount: 5,
89
data: new Uint32Array([(1 << 24) + (2 << 16) + (3 << 8) + 4, 1, 2, 3, 4, 5, 6, 7, 8, 9])
90
}]
91
});
92
});
93
94
test('partial array buffer', () => {
95
const sharedArr = new Uint32Array([
96
(1 << 24) + (2 << 16) + (3 << 8) + 4,
97
1, 2, 3, 4, 5, (1 << 24) + (2 << 16) + (3 << 8) + 4
98
]);
99
testRoundTrip({
100
id: 12,
101
type: 'delta',
102
deltas: [{
103
start: 0,
104
deleteCount: 4,
105
data: sharedArr.subarray(0, 1)
106
}, {
107
start: 15,
108
deleteCount: 0,
109
data: sharedArr.subarray(1, sharedArr.length)
110
}]
111
});
112
});
113
114
test('issue #94521: unusual backing array buffer', () => {
115
function wrapAndSliceUint8Arry(buff: Uint8Array, prefixLength: number, suffixLength: number): Uint8Array {
116
const wrapped = new Uint8Array(prefixLength + buff.byteLength + suffixLength);
117
wrapped.set(buff, prefixLength);
118
return wrapped.subarray(prefixLength, prefixLength + buff.byteLength);
119
}
120
function wrapAndSlice(buff: VSBuffer, prefixLength: number, suffixLength: number): VSBuffer {
121
return VSBuffer.wrap(wrapAndSliceUint8Arry(buff.buffer, prefixLength, suffixLength));
122
}
123
const dto: ISemanticTokensDto = {
124
id: 5,
125
type: 'full',
126
data: new Uint32Array([1, 2, 3, 4, 5])
127
};
128
const encoded = encodeSemanticTokensDto(dto);
129
130
// with misaligned prefix and misaligned suffix
131
assertEqualFull(<IFullSemanticTokensDto>decodeSemanticTokensDto(wrapAndSlice(encoded, 1, 1)), dto);
132
// with misaligned prefix and aligned suffix
133
assertEqualFull(<IFullSemanticTokensDto>decodeSemanticTokensDto(wrapAndSlice(encoded, 1, 4)), dto);
134
// with aligned prefix and misaligned suffix
135
assertEqualFull(<IFullSemanticTokensDto>decodeSemanticTokensDto(wrapAndSlice(encoded, 4, 1)), dto);
136
// with aligned prefix and aligned suffix
137
assertEqualFull(<IFullSemanticTokensDto>decodeSemanticTokensDto(wrapAndSlice(encoded, 4, 4)), dto);
138
});
139
});
140
141