diff --git a/extensions/markdown-language-features/src/test/documentLinkProvider.test.ts b/extensions/markdown-language-features/src/test/documentLinkProvider.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..dcaf30ec94d62314e2c976cb899d0162a34f648a --- /dev/null +++ b/extensions/markdown-language-features/src/test/documentLinkProvider.test.ts @@ -0,0 +1,46 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import * as assert from 'assert'; +import 'mocha'; +import * as vscode from 'vscode'; +import LinkProvider from '../features/documentLinkProvider'; +import { InMemoryDocument } from './inMemoryDocument'; + + +const testFileName = vscode.Uri.parse('test.md'); + +const noopToken = new class implements vscode.CancellationToken { + private _onCancellationRequestedEmitter = new vscode.EventEmitter(); + public onCancellationRequested = this._onCancellationRequestedEmitter.event; + + get isCancellationRequested() { return false; } +}; + +function getLinksForFile(fileContents: string) { + const doc = new InMemoryDocument(testFileName, fileContents); + const provider = new LinkProvider(); + return provider.provideDocumentLinks(doc, noopToken); +} + + +suite('markdown.DocumentLinkProvider', () => { + test('Should not return anything for empty document', async () => { + const links = getLinksForFile(''); + assert.strictEqual(links.length, 0); + }); + + test('Should not return anything for simple document without links', async () => { + const links = getLinksForFile('# a\nfdasfdfsafsa'); + assert.strictEqual(links.length, 0); + }); + + test('Should should detect basic http link', async () => { + const links = getLinksForFile('a [b](https://example.com) c'); + assert.strictEqual(links.length, 1); + }); +}); + + diff --git a/extensions/markdown-language-features/src/test/inMemoryDocument.ts b/extensions/markdown-language-features/src/test/inMemoryDocument.ts index 60ff91f15f1a744ed0a253c14629108767f40f36..bfcf3721b656bad0804b8d2d5e04767459fa94bd 100644 --- a/extensions/markdown-language-features/src/test/inMemoryDocument.ts +++ b/extensions/markdown-language-features/src/test/inMemoryDocument.ts @@ -44,8 +44,12 @@ export class InMemoryDocument implements vscode.TextDocument { offsetAt(_position: vscode.Position): never { throw new Error('Method not implemented.'); } - positionAt(_offset: number): never { - throw new Error('Method not implemented.'); + positionAt(offset: number): vscode.Position { + const before = this._contents.slice(0, offset); + const newLines = before.match(/\n/g); + const line = newLines ? newLines.length : 0; + const preCharacters = before.match(/(\n|^).*$/g); + return new vscode.Position(line, preCharacters ? preCharacters[0].length : 0); } getText(_range?: vscode.Range | undefined): string { return this._contents;