提交 01c6d17c 编写于 作者: R Ramya Achutha Rao

Tests for Expand Abbreviation

上级 0f70188d
...@@ -143,7 +143,7 @@ export function expandAbbreviation(args) { ...@@ -143,7 +143,7 @@ export function expandAbbreviation(args) {
abbreviationList.push({ syntax, abbreviation, rangeToReplace }); abbreviationList.push({ syntax, abbreviation, rangeToReplace });
}); });
expandAbbreviationInRange(editor, abbreviationList, allAbbreviationsSame); return expandAbbreviationInRange(editor, abbreviationList, allAbbreviationsSame);
} }
...@@ -191,7 +191,7 @@ export function isValidLocationForEmmetAbbreviation(currentNode: Node, syntax: s ...@@ -191,7 +191,7 @@ export function isValidLocationForEmmetAbbreviation(currentNode: Node, syntax: s
* @param expandAbbrList * @param expandAbbrList
* @param insertSameSnippet * @param insertSameSnippet
*/ */
function expandAbbreviationInRange(editor: vscode.TextEditor, expandAbbrList: ExpandAbbreviationInput[], insertSameSnippet: boolean) { function expandAbbreviationInRange(editor: vscode.TextEditor, expandAbbrList: ExpandAbbreviationInput[], insertSameSnippet: boolean): Thenable<boolean> {
if (!expandAbbrList || expandAbbrList.length === 0) { if (!expandAbbrList || expandAbbrList.length === 0) {
return; return;
} }
...@@ -200,14 +200,15 @@ function expandAbbreviationInRange(editor: vscode.TextEditor, expandAbbrList: Ex ...@@ -200,14 +200,15 @@ function expandAbbreviationInRange(editor: vscode.TextEditor, expandAbbrList: Ex
// Snippet to replace at multiple cursors are not the same // Snippet to replace at multiple cursors are not the same
// `editor.insertSnippet` will have to be called for each instance separately // `editor.insertSnippet` will have to be called for each instance separately
// We will not be able to maintain multiple cursors after snippet insertion // We will not be able to maintain multiple cursors after snippet insertion
let insertPromises = [];
if (!insertSameSnippet) { if (!insertSameSnippet) {
expandAbbrList.forEach((expandAbbrInput: ExpandAbbreviationInput) => { expandAbbrList.forEach((expandAbbrInput: ExpandAbbreviationInput) => {
let expandedText = expandAbbr(expandAbbrInput, newLine); let expandedText = expandAbbr(expandAbbrInput, newLine);
if (expandedText) { if (expandedText) {
editor.insertSnippet(new vscode.SnippetString(expandedText), expandAbbrInput.rangeToReplace); insertPromises.push(editor.insertSnippet(new vscode.SnippetString(expandedText), expandAbbrInput.rangeToReplace));
} }
}); });
return; return Promise.all(insertPromises).then(() => Promise.resolve(true));
} }
// Snippet to replace at all cursors are the same // Snippet to replace at all cursors are the same
...@@ -219,7 +220,7 @@ function expandAbbreviationInRange(editor: vscode.TextEditor, expandAbbrList: Ex ...@@ -219,7 +220,7 @@ function expandAbbreviationInRange(editor: vscode.TextEditor, expandAbbrList: Ex
return new vscode.Range(value.rangeToReplace.start.line, value.rangeToReplace.start.character, value.rangeToReplace.end.line, value.rangeToReplace.end.character); return new vscode.Range(value.rangeToReplace.start.line, value.rangeToReplace.start.character, value.rangeToReplace.end.line, value.rangeToReplace.end.character);
}); });
if (expandedText) { if (expandedText) {
editor.insertSnippet(new vscode.SnippetString(expandedText), allRanges); return editor.insertSnippet(new vscode.SnippetString(expandedText), allRanges);
} }
} }
...@@ -232,7 +233,7 @@ function expandAbbr(input: ExpandAbbreviationInput, newLine: string): string { ...@@ -232,7 +233,7 @@ function expandAbbr(input: ExpandAbbreviationInput, newLine: string): string {
const expandOptions = getExpandOptions(emmetConfig['syntaxProfiles'], emmetConfig['variables'], input.syntax, input.textToWrap); const expandOptions = getExpandOptions(emmetConfig['syntaxProfiles'], emmetConfig['variables'], input.syntax, input.textToWrap);
// Below fixes https://github.com/Microsoft/vscode/issues/29898 // Below fixes https://github.com/Microsoft/vscode/issues/29898
// With this, Emmet formats inline elements as block elements // With this, Emmet formats inline elements as block elements
// ensuring the wrapped multi line text does not get merged to a single line // ensuring the wrapped multi line text does not get merged to a single line
if (input.textToWrap && !input.rangeToReplace.isSingleLine) { if (input.textToWrap && !input.rangeToReplace.isSingleLine) {
expandOptions.profile['inlineBreak'] = 1; expandOptions.profile['inlineBreak'] = 1;
...@@ -286,8 +287,8 @@ function getSyntaxFromArgs(args: any): string { ...@@ -286,8 +287,8 @@ function getSyntaxFromArgs(args: any): string {
} }
const mappedModes = getMappingForIncludedLanguages(); const mappedModes = getMappingForIncludedLanguages();
let language: string = (typeof args !== 'object' || !args['language']) ? editor.document.languageId : args['language']; let language: string = (!args || typeof args !== 'object' || !args['language']) ? editor.document.languageId : args['language'];
let parentMode: string = typeof args === 'object' ? args['parentMode'] : undefined; let parentMode: string = (args && typeof args === 'object') ? args['parentMode'] : undefined;
let excludedLanguages = vscode.workspace.getConfiguration('emmet')['exlcudeLanguages'] ? vscode.workspace.getConfiguration('emmet')['exlcudeLanguages'] : []; let excludedLanguages = vscode.workspace.getConfiguration('emmet')['exlcudeLanguages'] ? vscode.workspace.getConfiguration('emmet')['exlcudeLanguages'] : [];
let syntax = getEmmetMode((mappedModes[language] ? mappedModes[language] : language), excludedLanguages); let syntax = getEmmetMode((mappedModes[language] ? mappedModes[language] : language), excludedLanguages);
if (syntax) { if (syntax) {
......
/*---------------------------------------------------------------------------------------------
* 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 { Selection } from 'vscode';
import { withRandomFileEditor, closeAllEditors } from './testUtils';
import { expandAbbreviation } from '../abbreviationActions';
const cssContents = `
.boo {
margin: 20px 10px;
background-image: url('tryme.png');
m10
}
.boo .hoo {
margin: 10px;
ind
}
`;
const scssContents = `
.boo {
margin: 20px 10px;
background-image: url('tryme.png');
.boo .hoo {
margin: 10px;
}
}
`;
const htmlContents = `
<body class="header">
<ul class="nav main">
<li class="item1">img</li>
<li class="item2">hithere</li>
ul>li
ul>li*2
ul>li.item$*2
ul>li.item$@44*2
<div
</ul>
<style>
.boo {
m10
}
</style>
</body>
`;
suite('Tests for Expand Abbreviations (HTML)', () => {
teardown(closeAllEditors);
test('Expand snippets (HTML)', () => {
return testHtmlExpandAbbreviation(new Selection(3, 23, 3, 23), 'img', '<img src=\"\" alt=\"\">');
});
test('Expand abbreviation (HTML)', () => {
return testHtmlExpandAbbreviation(new Selection(5, 25, 5, 25), 'ul>li', '<ul>\n\t\t\t<li></li>\n\t\t</ul>');
});
test('Expand text that is neither an abbreviation nor a snippet to tags (HTML)', () => {
return testHtmlExpandAbbreviation(new Selection(4, 20, 4, 27), 'hithere', '<hithere></hithere>');
});
test('Expand abbreviation with repeaters (HTML)', () => {
return testHtmlExpandAbbreviation(new Selection(6, 27, 6, 27), 'ul>li*2', '<ul>\n\t\t\t<li></li>\n\t\t\t<li></li>\n\t\t</ul>');
});
test('Expand abbreviation with numbered repeaters (HTML)', () => {
return testHtmlExpandAbbreviation(new Selection(7, 33, 7, 33), 'ul>li.item$*2', '<ul>\n\t\t\t<li class="item1"></li>\n\t\t\t<li class="item2"></li>\n\t\t</ul>');
});
test('Expand abbreviation with numbered repeaters with offset (HTML)', () => {
return testHtmlExpandAbbreviation(new Selection(8, 36, 8, 36), 'ul>li.item$@44*2', '<ul>\n\t\t\t<li class="item44"></li>\n\t\t\t<li class="item45"></li>\n\t\t</ul>');
});
test('Expand tag that is opened, but not closed (HTML)', () => {
return testHtmlExpandAbbreviation(new Selection(9, 6, 9, 6), '<div', '<div></div>');
});
test('No expanding text inside open tag (HTML)', () => {
return testHtmlExpandAbbreviation(new Selection(2, 4, 2, 4), '', '', true);
});
test('Expand css when inside style tag (HTML)', () => {
return withRandomFileEditor(htmlContents, 'html', (editor, doc) => {
editor.selection = new Selection(13, 3, 13, 6);
let expandPromise = expandAbbreviation({ language: 'css' });
if (!expandPromise) {
return Promise.resolve();
}
return expandPromise.then(() => {
assert.equal(editor.document.getText(), htmlContents.replace('m10', 'margin: 10px;'));
return Promise.resolve();
});
});
});
});
suite('Tests for Expand Abbreviations (CSS)', () => {
teardown(closeAllEditors);
test('Expand abbreviation (CSS)', () => {
return withRandomFileEditor(cssContents, 'css', (editor, doc) => {
editor.selection = new Selection(4, 1, 4, 4);
return expandAbbreviation(null).then(() => {
assert.equal(editor.document.getText(), cssContents.replace('m10', 'margin: 10px;'));
return Promise.resolve();
});
});
});
});
function testHtmlExpandAbbreviation(selection: Selection, abbreviation: string, expandedText: string, shouldFail?: boolean): Thenable<any> {
return withRandomFileEditor(htmlContents, 'html', (editor, doc) => {
editor.selection = selection;
let expandPromise = expandAbbreviation(null);
if (!expandPromise) {
if (!shouldFail) {
assert.equal(1, 2, `Problem with expanding ${abbreviation} to ${expandedText}`);
}
return Promise.resolve();
}
return expandPromise.then(() => {
assert.equal(editor.document.getText(), htmlContents.replace(abbreviation, expandedText));
return Promise.resolve();
});
});
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册