Path: blob/main/extensions/copilot/test/simulation/fixtures/unknown/issue-7660/positionOffsetTransformer.spec.ts
13405 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation and GitHub. All rights reserved.2*--------------------------------------------------------------------------------------------*/34import { expect } from 'chai';5import { Position, Range, TextEdit } from '../../../../vscodeTypes';6import { PositionOffsetTransformer } from '../positionOffsetTransformer';78describe('PositionOffsetTransformer', () => {9const sampleText = `line110line211line3`;1213let transformer: PositionOffsetTransformer;1415beforeEach(() => {16transformer = new PositionOffsetTransformer(sampleText);17});1819it('should initialize correctly', () => {20expect(transformer.getLineCount()).to.equal(3);21});2223it('should get correct offset for a position', () => {24const position = new Position(1, 2);25const offset = transformer.getOffset(position);26expect(offset).to.equal(8); // 6 (line1\n) + 2 (line2)27});2829it('should get correct position for an offset', () => {30const offset = 8;31const position = transformer.getPosition(offset);32expect(position.line).to.equal(1);33expect(position.character).to.equal(2);34});3536it('should convert range to offset range and back', () => {37const range = new Range(new Position(0, 1), new Position(1, 2));38const offsetRange = transformer.toOffsetRange(range);39expect(offsetRange.start).to.equal(1);40expect(offsetRange.endExclusive).to.equal(8);4142const newRange = transformer.toRange(offsetRange);43expect(newRange.start.line).to.equal(0);44expect(newRange.start.character).to.equal(1);45expect(newRange.end.line).to.equal(1);46expect(newRange.end.character).to.equal(2);47});4849it('should apply offset edits correctly', () => {50const edits = [51new TextEdit(new Range(new Position(0, 0), new Position(0, 5)), 'Hello'),52new TextEdit(new Range(new Position(1, 0), new Position(1, 5)), 'World')53];54const offsetEdit = transformer.toOffsetEdit(edits);55transformer.applyOffsetEdits(offsetEdit);5657const newText = transformer['_lines'].join('\n');58expect(newText).to.equal('Hello\nWorld\nline3');59});6061it('should validate position correctly', () => {62const invalidPosition = new Position(10, 10);63const validPosition = transformer.validatePosition(invalidPosition);64expect(validPosition.line).to.equal(2);65expect(validPosition.character).to.equal(5);66});6768it('should validate range correctly', () => {69const invalidRange = new Range(new Position(10, 10), new Position(20, 20));70const validRange = transformer.validateRange(invalidRange);71expect(validRange.start.line).to.equal(2);72expect(validRange.start.character).to.equal(5);73expect(validRange.end.line).to.equal(2);74expect(validRange.end.character).to.equal(5);75});76});777879