Split up MD link tests (#153048)

This splits the markdown link tests in two:

- Tests for detecting links in md files (`MdLinkComputer`)
- Tests for the actual vs code editor link provider

Also fixes a few cases splitting these tests up caught
This commit is contained in:
Matt Bierner
2022-06-23 17:53:56 -07:00
committed by GitHub
parent 4a7a6a597a
commit ab7bc9fb0b
2 changed files with 92 additions and 42 deletions

View File

@@ -6,7 +6,7 @@
import * as assert from 'assert';
import 'mocha';
import * as vscode from 'vscode';
import { MdLinkProvider, MdVsCodeLinkProvider } from '../languageFeatures/documentLinks';
import { MdLink, MdLinkComputer, MdLinkProvider, MdVsCodeLinkProvider } from '../languageFeatures/documentLinks';
import { noopToken } from '../util/cancellation';
import { InMemoryDocument } from '../util/inMemoryDocument';
import { createNewMarkdownEngine } from './engine';
@@ -15,25 +15,23 @@ import { nulLogger } from './nulLogging';
import { assertRangeEqual, joinLines, workspacePath } from './util';
function getLinksForFile(fileContents: string) {
const doc = new InMemoryDocument(workspacePath('x.md'), fileContents);
const workspace = new InMemoryMdWorkspace([doc]);
suite('Markdown: MdLinkComputer', () => {
const engine = createNewMarkdownEngine();
const linkProvider = new MdLinkProvider(engine, workspace, nulLogger);
const provider = new MdVsCodeLinkProvider(linkProvider);
return provider.provideDocumentLinks(doc, noopToken);
}
function assertLinksEqual(actualLinks: readonly vscode.DocumentLink[], expectedRanges: readonly vscode.Range[]) {
assert.strictEqual(actualLinks.length, expectedRanges.length);
for (let i = 0; i < actualLinks.length; ++i) {
assertRangeEqual(actualLinks[i].range, expectedRanges[i], `Range ${i} to be equal`);
function getLinksForFile(fileContents: string): Promise<MdLink[]> {
const doc = new InMemoryDocument(workspacePath('x.md'), fileContents);
const engine = createNewMarkdownEngine();
const linkProvider = new MdLinkComputer(engine);
return linkProvider.getAllLinks(doc, noopToken);
}
function assertLinksEqual(actualLinks: readonly MdLink[], expectedRanges: readonly vscode.Range[]) {
assert.strictEqual(actualLinks.length, expectedRanges.length);
for (let i = 0; i < actualLinks.length; ++i) {
assertRangeEqual(actualLinks[i].source.hrefRange, expectedRanges[i], `Range ${i} to be equal`);
}
}
}
suite('Markdown: DocumentLinkProvider', () => {
test('Should not return anything for empty document', async () => {
const links = await getLinksForFile('');
assertLinksEqual(links, []);
@@ -182,30 +180,32 @@ suite('Markdown: DocumentLinkProvider', () => {
});
test('Should find reference link shorthand (#141285)', async () => {
{
const links = await getLinksForFile(joinLines(
'[ref]',
'[ref]: https://example.com',
));
assertLinksEqual(links, [
new vscode.Range(0, 1, 0, 4),
new vscode.Range(1, 7, 1, 26),
]);
}
{
const links = await getLinksForFile(joinLines(
'[Does Not Work]',
'[def]: https://example.com',
));
assertLinksEqual(links, [
new vscode.Range(1, 7, 1, 26),
]);
}
const links = await getLinksForFile(joinLines(
'[ref]',
'[ref]: https://example.com',
));
assertLinksEqual(links, [
new vscode.Range(0, 1, 0, 4),
new vscode.Range(1, 7, 1, 26),
]);
});
test('Should not include reference link shorthand when source does not exist (#141285)', async () => {
const links = await getLinksForFile('[Works]');
assertLinksEqual(links, []);
test('Should find reference link shorthand using empty closing brackets (#141285)', async () => {
const links = await getLinksForFile(joinLines(
'[ref][]',
));
assertLinksEqual(links, [
new vscode.Range(0, 1, 0, 4),
]);
});
test.skip('Should find reference link shorthand for link with space in label (#141285)', async () => {
const links = await getLinksForFile(joinLines(
'[ref with space]',
));
assertLinksEqual(links, [
new vscode.Range(0, 7, 0, 26),
]);
});
test('Should not include reference links with escaped leading brackets', async () => {
@@ -462,3 +462,46 @@ suite('Markdown: DocumentLinkProvider', () => {
]);
});
});
suite('Markdown: VS Code DocumentLinkProvider', () => {
function getLinksForFile(fileContents: string) {
const doc = new InMemoryDocument(workspacePath('x.md'), fileContents);
const workspace = new InMemoryMdWorkspace([doc]);
const engine = createNewMarkdownEngine();
const linkProvider = new MdLinkProvider(engine, workspace, nulLogger);
const provider = new MdVsCodeLinkProvider(linkProvider);
return provider.provideDocumentLinks(doc, noopToken);
}
function assertLinksEqual(actualLinks: readonly vscode.DocumentLink[], expectedRanges: readonly vscode.Range[]) {
assert.strictEqual(actualLinks.length, expectedRanges.length);
for (let i = 0; i < actualLinks.length; ++i) {
assertRangeEqual(actualLinks[i].range, expectedRanges[i], `Range ${i} to be equal`);
}
}
test('Should include defined reference links (#141285)', async () => {
const links = await getLinksForFile(joinLines(
'[ref]',
'[ref][]',
'[ref][ref]',
'',
'[ref]: http://example.com'
));
assertLinksEqual(links, [
new vscode.Range(0, 1, 0, 4),
new vscode.Range(1, 1, 1, 4),
new vscode.Range(2, 6, 2, 9),
new vscode.Range(4, 7, 4, 25),
]);
});
test('Should not include reference link shorthand when definition does not exist (#141285)', async () => {
const links = await getLinksForFile('[ref]');
assertLinksEqual(links, []);
});
});