Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Legend-Master committed Nov 27, 2023
1 parent 5775823 commit ea747c2
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/languageservice/services/yamlSelectionRanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export function getSelectionRanges(document: TextDocument, positions: Position[]
}
startOffset = newOffset;
}

// Allow equal for children to override
if (!currentNode || startOffset >= currentNode.offset) {
currentNode = node;
Expand Down Expand Up @@ -123,9 +124,10 @@ export function getSelectionRanges(document: TextDocument, positions: Position[]
}

function getTextFromOffsets(startOffset: number, endOffset: number): string {
const start = document.positionAt(startOffset);
const end = document.positionAt(endOffset);
return document.getText({ start, end });
return document.getText({
start: document.positionAt(startOffset),
end: document.positionAt(endOffset),
});
}
}

Expand Down
72 changes: 72 additions & 0 deletions test/yamlSelectionRanges.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ key:
{ start: { line: 1, character: 0 }, end: { line: 3, character: 8 } },
]);

positions = [
{
line: 3,
character: 3,
},
];
ranges = getSelectionRanges(document, positions);
expect(ranges.length).equal(positions.length);
expectSelections(ranges[0], [
{ start: { line: 3, character: 2 }, end: { line: 3, character: 8 } },
{ start: { line: 2, character: 2 }, end: { line: 3, character: 8 } },
{ start: { line: 1, character: 0 }, end: { line: 3, character: 8 } },
]);

positions = [
{
line: 2,
Expand All @@ -78,6 +92,64 @@ key:
]);
});

it('selection ranges for array of objects', () => {
const yaml = `
times:
- second: 1
millisecond: 10
- second: 2
millisecond: 0
`;
let positions: Position[] = [
{
line: 4,
character: 0,
},
];
const document = setupTextDocument(yaml);
let ranges = getSelectionRanges(document, positions);
expect(ranges.length).equal(positions.length);
expectSelections(ranges[0], [
{ start: { line: 2, character: 2 }, end: { line: 5, character: 18 } },
{ start: { line: 1, character: 0 }, end: { line: 5, character: 18 } },
]);

positions = [
{
line: 5,
character: 2,
},
];
ranges = getSelectionRanges(document, positions);
expect(ranges.length).equal(positions.length);
expectSelections(ranges[0], [
{ start: { line: 4, character: 4 }, end: { line: 5, character: 18 } },
{ start: { line: 2, character: 2 }, end: { line: 5, character: 18 } },
{ start: { line: 1, character: 0 }, end: { line: 5, character: 18 } },
]);
});

it('selection ranges for trailing spaces', () => {
const yaml = `
key:
- 1
- 2 \t
`;
const positions: Position[] = [
{
line: 2,
character: 9,
},
];
const document = setupTextDocument(yaml);
const ranges = getSelectionRanges(document, positions);
expect(ranges.length).equal(positions.length);
expectSelections(ranges[0], [
{ start: { line: 2, character: 2 }, end: { line: 3, character: 9 } },
{ start: { line: 1, character: 0 }, end: { line: 3, character: 9 } },
]);
});

it('selection ranges jump for "" \'\'', () => {
const yaml = `
- "word"
Expand Down

0 comments on commit ea747c2

Please sign in to comment.