Path: blob/main/extensions/copilot/src/platform/inlineEdits/test/common/textEditLength.spec.ts
13405 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*--------------------------------------------------------------------------------------------*/456import { describe, expect, it } from 'vitest';7import { Range } from '../../../../util/vs/editor/common/core/range';8import { TextLength } from '../../../../util/vs/editor/common/core/text/textLength';9import { SingleTextEditLength, TextLengthEdit } from '../../common/dataTypes/textEditLength';1011describe('getRange', () => {1213it('should return undefined for empty edits', () => {14const textLengthEdit = TextLengthEdit.empty;15expect(textLengthEdit.getRange()).toMatchInlineSnapshot(`undefined`);16});1718it('should return the correct range for single edit', () => {19const range = new Range(1, 1, 1, 5);20const textLength = new TextLength(0, 4);21const singleEdit = new SingleTextEditLength(range, textLength);22const textLengthEdit = new TextLengthEdit([singleEdit]);23expect(textLengthEdit.getRange()?.toString()).toMatchInlineSnapshot(`"[1,1 -> 1,5]"`);24});2526it('should return the correct range for multiple edits', () => {27const range1 = new Range(1, 1, 1, 5);28const textLength1 = new TextLength(0, 4);29const singleEdit1 = new SingleTextEditLength(range1, textLength1);3031const range2 = new Range(2, 1, 2, 5);32const textLength2 = new TextLength(0, 4);33const singleEdit2 = new SingleTextEditLength(range2, textLength2);3435const textLengthEdit = new TextLengthEdit([singleEdit1, singleEdit2]);36expect(textLengthEdit.getRange()?.toString()).toMatchInlineSnapshot(`"[1,1 -> 2,5]"`);37});38});3940describe('compose', () => {4142it('should return empty for composing two empty edits', () => {43const edit1 = TextLengthEdit.empty;44const edit2 = TextLengthEdit.empty;45const composedEdit = edit1.compose(edit2);46expect(composedEdit.edits).toMatchInlineSnapshot(`[]`);47});4849it('should compose two non-overlapping edits correctly', () => {50const range1 = new Range(1, 1, 1, 5);51const textLength1 = new TextLength(0, 4);52const singleEdit1 = new SingleTextEditLength(range1, textLength1);53const edit1 = new TextLengthEdit([singleEdit1]);5455const range2 = new Range(2, 1, 2, 5);56const textLength2 = new TextLength(0, 4);57const singleEdit2 = new SingleTextEditLength(range2, textLength2);58const edit2 = new TextLengthEdit([singleEdit2]);5960const composedEdit = edit1.compose(edit2);61expect(composedEdit.edits.toString()).toMatchInlineSnapshot(`"{ range: [1,1 -> 1,5], newLength: 0,4 },{ range: [2,1 -> 2,5], newLength: 0,4 }"`);62});6364it('should compose two non-overlapping edits correctly - 2', () => {65const range1 = new Range(1, 1, 1, 5);66const textLength1 = new TextLength(2, 4);67const singleEdit1 = new SingleTextEditLength(range1, textLength1);68const edit1 = new TextLengthEdit([singleEdit1]);6970const range2 = new Range(2, 1, 2, 5);71const textLength2 = new TextLength(0, 4);72const singleEdit2 = new SingleTextEditLength(range2, textLength2);73const edit2 = new TextLengthEdit([singleEdit2]);7475const composedEdit = edit1.compose(edit2);76expect(composedEdit.edits.toString()).toMatchInlineSnapshot(`"{ range: [1,1 -> 1,5], newLength: 2,4 }"`);77});7879it('should compose two non-overlapping edits correctly - 3', () => {80const range1 = new Range(1, 1, 1, 5);81const textLength1 = new TextLength(2, 4);82const singleEdit1 = new SingleTextEditLength(range1, textLength1);83const edit1 = new TextLengthEdit([singleEdit1]);8485const range2 = new Range(12, 1, 12, 5);86const textLength2 = new TextLength(4, 4);87const singleEdit2 = new SingleTextEditLength(range2, textLength2);88const edit2 = new TextLengthEdit([singleEdit2]);8990const composedEdit = edit1.compose(edit2);91expect(composedEdit.edits.toString()).toMatchInlineSnapshot(`"{ range: [1,1 -> 1,5], newLength: 2,4 },{ range: [10,1 -> 10,5], newLength: 4,4 }"`);9293const composedEdit2 = edit2.compose(edit1);94expect(composedEdit2.edits.toString()).toMatchInlineSnapshot(`"{ range: [1,1 -> 1,5], newLength: 2,4 },{ range: [12,1 -> 12,5], newLength: 4,4 }"`);95});9697it('should compose overlapping edits correctly', () => {98const range1 = new Range(1, 1, 1, 5);99const textLength1 = new TextLength(0, 4);100const singleEdit1 = new SingleTextEditLength(range1, textLength1);101const edit1 = new TextLengthEdit([singleEdit1]);102103const range2 = new Range(1, 3, 1, 7);104const textLength2 = new TextLength(0, 4);105const singleEdit2 = new SingleTextEditLength(range2, textLength2);106const edit2 = new TextLengthEdit([singleEdit2]);107108const composedEdit = edit1.compose(edit2);109expect(composedEdit.edits.toString()).toMatchInlineSnapshot(`"{ range: [1,1 -> 1,7], newLength: 0,6 }"`);110});111});112113114