Path: blob/main/src/vs/editor/test/common/services/semanticTokensDto.test.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import assert from 'assert';6import { IFullSemanticTokensDto, IDeltaSemanticTokensDto, encodeSemanticTokensDto, ISemanticTokensDto, decodeSemanticTokensDto } from '../../../common/services/semanticTokensDto.js';7import { VSBuffer } from '../../../../base/common/buffer.js';8import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';910suite('SemanticTokensDto', () => {1112ensureNoDisposablesAreLeakedInTestSuite();1314function toArr(arr: Uint32Array): number[] {15const result: number[] = [];16for (let i = 0, len = arr.length; i < len; i++) {17result[i] = arr[i];18}19return result;20}2122function assertEqualFull(actual: IFullSemanticTokensDto, expected: IFullSemanticTokensDto): void {23const convert = (dto: IFullSemanticTokensDto) => {24return {25id: dto.id,26type: dto.type,27data: toArr(dto.data)28};29};30assert.deepStrictEqual(convert(actual), convert(expected));31}3233function assertEqualDelta(actual: IDeltaSemanticTokensDto, expected: IDeltaSemanticTokensDto): void {34const convertOne = (delta: { start: number; deleteCount: number; data?: Uint32Array }) => {35if (!delta.data) {36return delta;37}38return {39start: delta.start,40deleteCount: delta.deleteCount,41data: toArr(delta.data)42};43};44const convert = (dto: IDeltaSemanticTokensDto) => {45return {46id: dto.id,47type: dto.type,48deltas: dto.deltas.map(convertOne)49};50};51assert.deepStrictEqual(convert(actual), convert(expected));52}5354function testRoundTrip(value: ISemanticTokensDto): void {55const decoded = decodeSemanticTokensDto(encodeSemanticTokensDto(value));56if (value.type === 'full' && decoded.type === 'full') {57assertEqualFull(decoded, value);58} else if (value.type === 'delta' && decoded.type === 'delta') {59assertEqualDelta(decoded, value);60} else {61assert.fail('wrong type');62}63}6465test('full encoding', () => {66testRoundTrip({67id: 12,68type: 'full',69data: new Uint32Array([(1 << 24) + (2 << 16) + (3 << 8) + 4])70});71});7273test('delta encoding', () => {74testRoundTrip({75id: 12,76type: 'delta',77deltas: [{78start: 0,79deleteCount: 4,80data: undefined81}, {82start: 15,83deleteCount: 0,84data: new Uint32Array([(1 << 24) + (2 << 16) + (3 << 8) + 4])85}, {86start: 27,87deleteCount: 5,88data: new Uint32Array([(1 << 24) + (2 << 16) + (3 << 8) + 4, 1, 2, 3, 4, 5, 6, 7, 8, 9])89}]90});91});9293test('partial array buffer', () => {94const sharedArr = new Uint32Array([95(1 << 24) + (2 << 16) + (3 << 8) + 4,961, 2, 3, 4, 5, (1 << 24) + (2 << 16) + (3 << 8) + 497]);98testRoundTrip({99id: 12,100type: 'delta',101deltas: [{102start: 0,103deleteCount: 4,104data: sharedArr.subarray(0, 1)105}, {106start: 15,107deleteCount: 0,108data: sharedArr.subarray(1, sharedArr.length)109}]110});111});112113test('issue #94521: unusual backing array buffer', () => {114function wrapAndSliceUint8Arry(buff: Uint8Array, prefixLength: number, suffixLength: number): Uint8Array {115const wrapped = new Uint8Array(prefixLength + buff.byteLength + suffixLength);116wrapped.set(buff, prefixLength);117return wrapped.subarray(prefixLength, prefixLength + buff.byteLength);118}119function wrapAndSlice(buff: VSBuffer, prefixLength: number, suffixLength: number): VSBuffer {120return VSBuffer.wrap(wrapAndSliceUint8Arry(buff.buffer, prefixLength, suffixLength));121}122const dto: ISemanticTokensDto = {123id: 5,124type: 'full',125data: new Uint32Array([1, 2, 3, 4, 5])126};127const encoded = encodeSemanticTokensDto(dto);128129// with misaligned prefix and misaligned suffix130assertEqualFull(<IFullSemanticTokensDto>decodeSemanticTokensDto(wrapAndSlice(encoded, 1, 1)), dto);131// with misaligned prefix and aligned suffix132assertEqualFull(<IFullSemanticTokensDto>decodeSemanticTokensDto(wrapAndSlice(encoded, 1, 4)), dto);133// with aligned prefix and misaligned suffix134assertEqualFull(<IFullSemanticTokensDto>decodeSemanticTokensDto(wrapAndSlice(encoded, 4, 1)), dto);135// with aligned prefix and aligned suffix136assertEqualFull(<IFullSemanticTokensDto>decodeSemanticTokensDto(wrapAndSlice(encoded, 4, 4)), dto);137});138});139140141