Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/test/simulation/fixtures/unknown/issue-7660/positionOffsetTransformer.spec.ts
13405 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation and GitHub. All rights reserved.
3
*--------------------------------------------------------------------------------------------*/
4
5
import { expect } from 'chai';
6
import { Position, Range, TextEdit } from '../../../../vscodeTypes';
7
import { PositionOffsetTransformer } from '../positionOffsetTransformer';
8
9
describe('PositionOffsetTransformer', () => {
10
const sampleText = `line1
11
line2
12
line3`;
13
14
let transformer: PositionOffsetTransformer;
15
16
beforeEach(() => {
17
transformer = new PositionOffsetTransformer(sampleText);
18
});
19
20
it('should initialize correctly', () => {
21
expect(transformer.getLineCount()).to.equal(3);
22
});
23
24
it('should get correct offset for a position', () => {
25
const position = new Position(1, 2);
26
const offset = transformer.getOffset(position);
27
expect(offset).to.equal(8); // 6 (line1\n) + 2 (line2)
28
});
29
30
it('should get correct position for an offset', () => {
31
const offset = 8;
32
const position = transformer.getPosition(offset);
33
expect(position.line).to.equal(1);
34
expect(position.character).to.equal(2);
35
});
36
37
it('should convert range to offset range and back', () => {
38
const range = new Range(new Position(0, 1), new Position(1, 2));
39
const offsetRange = transformer.toOffsetRange(range);
40
expect(offsetRange.start).to.equal(1);
41
expect(offsetRange.endExclusive).to.equal(8);
42
43
const newRange = transformer.toRange(offsetRange);
44
expect(newRange.start.line).to.equal(0);
45
expect(newRange.start.character).to.equal(1);
46
expect(newRange.end.line).to.equal(1);
47
expect(newRange.end.character).to.equal(2);
48
});
49
50
it('should apply offset edits correctly', () => {
51
const edits = [
52
new TextEdit(new Range(new Position(0, 0), new Position(0, 5)), 'Hello'),
53
new TextEdit(new Range(new Position(1, 0), new Position(1, 5)), 'World')
54
];
55
const offsetEdit = transformer.toOffsetEdit(edits);
56
transformer.applyOffsetEdits(offsetEdit);
57
58
const newText = transformer['_lines'].join('\n');
59
expect(newText).to.equal('Hello\nWorld\nline3');
60
});
61
62
it('should validate position correctly', () => {
63
const invalidPosition = new Position(10, 10);
64
const validPosition = transformer.validatePosition(invalidPosition);
65
expect(validPosition.line).to.equal(2);
66
expect(validPosition.character).to.equal(5);
67
});
68
69
it('should validate range correctly', () => {
70
const invalidRange = new Range(new Position(10, 10), new Position(20, 20));
71
const validRange = transformer.validateRange(invalidRange);
72
expect(validRange.start.line).to.equal(2);
73
expect(validRange.start.character).to.equal(5);
74
expect(validRange.end.line).to.equal(2);
75
expect(validRange.end.character).to.equal(5);
76
});
77
});
78
79