mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-22 01:29:04 +01:00
@@ -2,92 +2,101 @@
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
import * as vscode from 'vscode';
|
||||
import * as assert from 'assert';
|
||||
import 'mocha';
|
||||
import { SkinnyTextDocument, checkSmartPaste, createEditAddingLinksForUriList, appendToLinkSnippet, validateLink } from '../languageFeatures/copyFiles/shared';
|
||||
import * as vscode from 'vscode';
|
||||
import { InMemoryDocument } from '../client/inMemoryDocument';
|
||||
import { appendToLinkSnippet, createEditAddingLinksForUriList, findValidUriInText, shouldSmartPaste } from '../languageFeatures/copyFiles/shared';
|
||||
|
||||
suite('createEditAddingLinksForUriList', () => {
|
||||
|
||||
test('Markdown Link Pasting should occur for a valid link (end to end)', async () => {
|
||||
// createEditAddingLinksForUriList -> checkSmartPaste -> tryGetUriListSnippet -> createUriListSnippet -> createLinkSnippet
|
||||
|
||||
const skinnyDocument: SkinnyTextDocument = {
|
||||
uri: vscode.Uri.parse('file:///path/to/your/file'),
|
||||
offsetAt: function () { return 0; },
|
||||
getText: function () { return 'hello world!'; },
|
||||
};
|
||||
|
||||
const result = createEditAddingLinksForUriList(skinnyDocument, [new vscode.Range(0, 0, 0, 12)], 'https://www.microsoft.com/', true, true);
|
||||
const result = createEditAddingLinksForUriList(
|
||||
new InMemoryDocument(vscode.Uri.file('test.md'), 'hello world!'), [new vscode.Range(0, 0, 0, 12)], 'https://www.microsoft.com/', true, true);
|
||||
// need to check the actual result -> snippet value
|
||||
assert.strictEqual(result?.label, 'Insert Markdown Link');
|
||||
});
|
||||
|
||||
suite('validateLink', () => {
|
||||
|
||||
test('Markdown pasting should occur for a valid link.', () => {
|
||||
const isLink = validateLink('https://www.microsoft.com/').isValid;
|
||||
assert.strictEqual(isLink, true);
|
||||
test('Markdown pasting should occur for a valid link', () => {
|
||||
assert.strictEqual(
|
||||
findValidUriInText('https://www.microsoft.com/'),
|
||||
'https://www.microsoft.com/');
|
||||
});
|
||||
|
||||
test('Markdown pasting should occur for a valid link preceded by a new line.', () => {
|
||||
const isLink = validateLink('\r\nhttps://www.microsoft.com/').isValid;
|
||||
assert.strictEqual(isLink, true);
|
||||
test('Markdown pasting should occur for a valid link preceded by a new line', () => {
|
||||
assert.strictEqual(
|
||||
findValidUriInText('\r\nhttps://www.microsoft.com/'),
|
||||
'https://www.microsoft.com/');
|
||||
});
|
||||
|
||||
test('Markdown pasting should occur for a valid link followed by a new line.', () => {
|
||||
const isLink = validateLink('https://www.microsoft.com/\r\n').isValid;
|
||||
assert.strictEqual(isLink, true);
|
||||
test('Markdown pasting should occur for a valid link followed by a new line', () => {
|
||||
assert.strictEqual(
|
||||
findValidUriInText('https://www.microsoft.com/\r\n'),
|
||||
'https://www.microsoft.com/');
|
||||
});
|
||||
|
||||
test('Markdown pasting should not occur for a valid hostname and invalid protool.', () => {
|
||||
const isLink = validateLink('invalid:www.microsoft.com').isValid;
|
||||
assert.strictEqual(isLink, false);
|
||||
test('Markdown pasting should not occur for a valid hostname and invalid protool', () => {
|
||||
assert.strictEqual(
|
||||
findValidUriInText('invalid:www.microsoft.com'),
|
||||
undefined);
|
||||
});
|
||||
|
||||
test('Markdown pasting should not occur for plain text.', () => {
|
||||
const isLink = validateLink('hello world!').isValid;
|
||||
assert.strictEqual(isLink, false);
|
||||
test('Markdown pasting should not occur for plain text', () => {
|
||||
assert.strictEqual(
|
||||
findValidUriInText('hello world!'),
|
||||
undefined);
|
||||
});
|
||||
|
||||
test('Markdown pasting should not occur for plain text including a colon.', () => {
|
||||
const isLink = validateLink('hello: world!').isValid;
|
||||
assert.strictEqual(isLink, false);
|
||||
test('Markdown pasting should not occur for plain text including a colon', () => {
|
||||
assert.strictEqual(
|
||||
findValidUriInText('hello: world!'),
|
||||
undefined);
|
||||
});
|
||||
|
||||
test('Markdown pasting should not occur for plain text including a slashes.', () => {
|
||||
const isLink = validateLink('helloworld!').isValid;
|
||||
assert.strictEqual(isLink, false);
|
||||
test('Markdown pasting should not occur for plain text including a slashes', () => {
|
||||
assert.strictEqual(
|
||||
findValidUriInText('helloworld!'),
|
||||
undefined);
|
||||
});
|
||||
|
||||
test('Markdown pasting should not occur for a link followed by text.', () => {
|
||||
const isLink = validateLink('https://www.microsoft.com/ hello world!').isValid;
|
||||
assert.strictEqual(isLink, false);
|
||||
test('Markdown pasting should not occur for a link followed by text', () => {
|
||||
assert.strictEqual(
|
||||
findValidUriInText('https://www.microsoft.com/ hello world!'),
|
||||
undefined);
|
||||
});
|
||||
|
||||
test('Markdown pasting should occur for a link preceded or followed by spaces.', () => {
|
||||
const isLink = validateLink(' https://www.microsoft.com/ ').isValid;
|
||||
assert.strictEqual(isLink, true);
|
||||
test('Markdown pasting should occur for a link preceded or followed by spaces', () => {
|
||||
assert.strictEqual(
|
||||
findValidUriInText(' https://www.microsoft.com/ '),
|
||||
'https://www.microsoft.com/');
|
||||
});
|
||||
|
||||
test('Markdown pasting should not occur for a link with an invalid scheme.', () => {
|
||||
const isLink = validateLink('hello:www.microsoft.com').isValid;
|
||||
assert.strictEqual(isLink, false);
|
||||
test('Markdown pasting should not occur for a link with an invalid scheme', () => {
|
||||
assert.strictEqual(
|
||||
findValidUriInText('hello:www.microsoft.com'),
|
||||
undefined);
|
||||
});
|
||||
|
||||
test('Markdown pasting should not occur for multiple links being pasted.', () => {
|
||||
const isLink = validateLink('https://www.microsoft.com/\r\nhttps://www.microsoft.com/\r\nhttps://www.microsoft.com/\r\nhttps://www.microsoft.com/').isValid;
|
||||
assert.strictEqual(isLink, false);
|
||||
test('Markdown pasting should not occur for multiple links being pasted', () => {
|
||||
assert.strictEqual(
|
||||
findValidUriInText('https://www.microsoft.com/\r\nhttps://www.microsoft.com/\r\nhttps://www.microsoft.com/\r\nhttps://www.microsoft.com/'),
|
||||
undefined);
|
||||
});
|
||||
|
||||
test('Markdown pasting should not occur for multiple links with spaces being pasted.', () => {
|
||||
const isLink = validateLink('https://www.microsoft.com/ \r\nhttps://www.microsoft.com/\r\nhttps://www.microsoft.com/\r\n hello \r\nhttps://www.microsoft.com/').isValid;
|
||||
assert.strictEqual(isLink, false);
|
||||
test('Markdown pasting should not occur for multiple links with spaces being pasted', () => {
|
||||
assert.strictEqual(
|
||||
findValidUriInText('https://www.microsoft.com/ \r\nhttps://www.microsoft.com/\r\nhttps://www.microsoft.com/\r\n hello \r\nhttps://www.microsoft.com/'),
|
||||
undefined);
|
||||
});
|
||||
|
||||
test('Markdown pasting should not occur for just a valid uri scheme', () => {
|
||||
const isLink = validateLink('https://').isValid;
|
||||
assert.strictEqual(isLink, false);
|
||||
assert.strictEqual(
|
||||
findValidUriInText('https://'),
|
||||
undefined);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -132,107 +141,86 @@ suite('createEditAddingLinksForUriList', () => {
|
||||
|
||||
suite('checkSmartPaste', () => {
|
||||
|
||||
const skinnyDocument: SkinnyTextDocument = {
|
||||
uri: vscode.Uri.file('/path/to/your/file'),
|
||||
offsetAt: function () { return 0; },
|
||||
getText: function () { return 'hello world!'; },
|
||||
};
|
||||
|
||||
test('Should evaluate pasteAsMarkdownLink as true for selected plain text', () => {
|
||||
const pasteAsMarkdownLink = checkSmartPaste(skinnyDocument, new vscode.Range(0, 0, 0, 12), new vscode.Range(0, 0, 0, 12));
|
||||
assert.strictEqual(pasteAsMarkdownLink, true);
|
||||
assert.strictEqual(
|
||||
shouldSmartPaste(makeTestDoc('hello world'), new vscode.Range(0, 0, 0, 12)),
|
||||
true);
|
||||
});
|
||||
|
||||
test('Should evaluate pasteAsMarkdownLink as false for a valid selected link', () => {
|
||||
skinnyDocument.getText = function () { return 'https://www.microsoft.com'; };
|
||||
const pasteAsMarkdownLink = checkSmartPaste(skinnyDocument, new vscode.Range(0, 0, 0, 25), new vscode.Range(0, 0, 0, 25));
|
||||
assert.strictEqual(pasteAsMarkdownLink, false);
|
||||
assert.strictEqual(
|
||||
shouldSmartPaste(makeTestDoc('https://www.microsoft.com'), new vscode.Range(0, 0, 0, 25)),
|
||||
false);
|
||||
});
|
||||
|
||||
test('Should evaluate pasteAsMarkdownLink as false for a valid selected link with trailing whitespace', () => {
|
||||
skinnyDocument.getText = function () { return ' https://www.microsoft.com '; };
|
||||
const pasteAsMarkdownLink = checkSmartPaste(skinnyDocument, new vscode.Range(0, 0, 0, 30), new vscode.Range(0, 0, 0, 30));
|
||||
assert.strictEqual(pasteAsMarkdownLink, false);
|
||||
assert.strictEqual(
|
||||
shouldSmartPaste(makeTestDoc(' https://www.microsoft.com '), new vscode.Range(0, 0, 0, 30)),
|
||||
false);
|
||||
});
|
||||
|
||||
test('Should evaluate pasteAsMarkdownLink as true for a link pasted in square brackets', () => {
|
||||
skinnyDocument.getText = function () { return '[abc]'; };
|
||||
const pasteAsMarkdownLink = checkSmartPaste(skinnyDocument, new vscode.Range(0, 1, 0, 4), new vscode.Range(0, 1, 0, 4));
|
||||
assert.strictEqual(pasteAsMarkdownLink, true);
|
||||
assert.strictEqual(
|
||||
shouldSmartPaste(makeTestDoc('[abc]'), new vscode.Range(0, 1, 0, 4)),
|
||||
true);
|
||||
});
|
||||
|
||||
test('Should evaluate pasteAsMarkdownLink as false for no selection', () => {
|
||||
const pasteAsMarkdownLink = checkSmartPaste(skinnyDocument, new vscode.Range(0, 0, 0, 0), new vscode.Range(0, 0, 0, 0));
|
||||
assert.strictEqual(pasteAsMarkdownLink, false);
|
||||
assert.strictEqual(
|
||||
shouldSmartPaste(makeTestDoc('xyz'), new vscode.Range(0, 0, 0, 0)),
|
||||
false);
|
||||
});
|
||||
|
||||
test('Should evaluate pasteAsMarkdownLink as false for selected whitespace and new lines', () => {
|
||||
skinnyDocument.getText = function () { return ' \r\n\r\n'; };
|
||||
const pasteAsMarkdownLink = checkSmartPaste(skinnyDocument, new vscode.Range(0, 0, 0, 7), new vscode.Range(0, 0, 0, 7));
|
||||
assert.strictEqual(pasteAsMarkdownLink, false);
|
||||
assert.strictEqual(
|
||||
shouldSmartPaste(makeTestDoc(' \r\n\r\n'), new vscode.Range(0, 0, 0, 7)),
|
||||
false);
|
||||
});
|
||||
|
||||
test('Should evaluate pasteAsMarkdownLink as false for pasting within a backtick code block', () => {
|
||||
skinnyDocument.getText = function () { return '```\r\n\r\n```'; };
|
||||
const pasteAsMarkdownLink = checkSmartPaste(skinnyDocument, new vscode.Range(0, 5, 0, 5), new vscode.Range(0, 5, 0, 5));
|
||||
assert.strictEqual(pasteAsMarkdownLink, false);
|
||||
assert.strictEqual(
|
||||
shouldSmartPaste(makeTestDoc('```\r\n\r\n```'), new vscode.Range(0, 5, 0, 5)),
|
||||
false);
|
||||
});
|
||||
|
||||
test('Should evaluate pasteAsMarkdownLink as false for pasting within a tilde code block', () => {
|
||||
skinnyDocument.getText = function () { return '~~~\r\n\r\n~~~'; };
|
||||
const pasteAsMarkdownLink = checkSmartPaste(skinnyDocument, new vscode.Range(0, 5, 0, 5), new vscode.Range(0, 5, 0, 5));
|
||||
assert.strictEqual(pasteAsMarkdownLink, false);
|
||||
assert.strictEqual(
|
||||
shouldSmartPaste(makeTestDoc('~~~\r\n\r\n~~~'), new vscode.Range(0, 5, 0, 5)),
|
||||
false);
|
||||
});
|
||||
|
||||
test('Should evaluate pasteAsMarkdownLink as false for pasting within a math block', () => {
|
||||
skinnyDocument.getText = function () { return '$$$\r\n\r\n$$$'; };
|
||||
const pasteAsMarkdownLink = checkSmartPaste(skinnyDocument, new vscode.Range(0, 5, 0, 5), new vscode.Range(0, 5, 0, 5));
|
||||
assert.strictEqual(pasteAsMarkdownLink, false);
|
||||
assert.strictEqual(
|
||||
shouldSmartPaste(makeTestDoc('$$$\r\n\r\n$$$'), new vscode.Range(0, 5, 0, 5)),
|
||||
false);
|
||||
});
|
||||
|
||||
const linkSkinnyDoc: SkinnyTextDocument = {
|
||||
uri: vscode.Uri.file('/path/to/your/file'),
|
||||
offsetAt: function () { return 0; },
|
||||
getText: function () { return '[a](bcdef)'; },
|
||||
};
|
||||
|
||||
test('Should evaluate pasteAsMarkdownLink as false for pasting within a Markdown link', () => {
|
||||
const pasteAsMarkdownLink = checkSmartPaste(linkSkinnyDoc, new vscode.Range(0, 4, 0, 6), new vscode.Range(0, 4, 0, 6));
|
||||
assert.strictEqual(pasteAsMarkdownLink, false);
|
||||
assert.strictEqual(
|
||||
shouldSmartPaste(makeTestDoc('[a](bcdef)'), new vscode.Range(0, 4, 0, 6)),
|
||||
false);
|
||||
});
|
||||
|
||||
|
||||
const imageLinkSkinnyDoc: SkinnyTextDocument = {
|
||||
uri: vscode.Uri.file('/path/to/your/file'),
|
||||
offsetAt: function () { return 0; },
|
||||
getText: function () { return ''; },
|
||||
};
|
||||
|
||||
test('Should evaluate pasteAsMarkdownLink as false for pasting within a Markdown image link', () => {
|
||||
const pasteAsMarkdownLink = checkSmartPaste(imageLinkSkinnyDoc, new vscode.Range(0, 5, 0, 10), new vscode.Range(0, 5, 0, 10));
|
||||
assert.strictEqual(pasteAsMarkdownLink, false);
|
||||
assert.strictEqual(
|
||||
shouldSmartPaste(makeTestDoc(''), new vscode.Range(0, 5, 0, 10)),
|
||||
false);
|
||||
});
|
||||
|
||||
const inlineCodeSkinnyCode: SkinnyTextDocument = {
|
||||
uri: vscode.Uri.file('/path/to/your/file'),
|
||||
offsetAt: function () { return 0; },
|
||||
getText: function () { return '``'; },
|
||||
};
|
||||
|
||||
test('Should evaluate pasteAsMarkdownLink as false for pasting within inline code', () => {
|
||||
const pasteAsMarkdownLink = checkSmartPaste(inlineCodeSkinnyCode, new vscode.Range(0, 1, 0, 1), new vscode.Range(0, 1, 0, 1));
|
||||
assert.strictEqual(pasteAsMarkdownLink, false);
|
||||
assert.strictEqual(
|
||||
shouldSmartPaste(makeTestDoc('``'), new vscode.Range(0, 1, 0, 1)),
|
||||
false);
|
||||
});
|
||||
|
||||
const inlineMathSkinnyDoc: SkinnyTextDocument = {
|
||||
uri: vscode.Uri.file('/path/to/your/file'),
|
||||
offsetAt: function () { return 0; },
|
||||
getText: function () { return '$$'; },
|
||||
};
|
||||
|
||||
test('Should evaluate pasteAsMarkdownLink as false for pasting within inline math', () => {
|
||||
const pasteAsMarkdownLink = checkSmartPaste(inlineMathSkinnyDoc, new vscode.Range(0, 1, 0, 1), new vscode.Range(0, 1, 0, 1));
|
||||
assert.strictEqual(pasteAsMarkdownLink, false);
|
||||
assert.strictEqual(
|
||||
shouldSmartPaste(makeTestDoc('$$'), new vscode.Range(0, 1, 0, 1)),
|
||||
false);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function makeTestDoc(contents: string) {
|
||||
return new InMemoryDocument(vscode.Uri.file('test.md'), contents);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user