Path: blob/main/extensions/emmet/src/test/completion.test.ts
4774 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import * as assert from 'assert';6import 'mocha';7import { CancellationTokenSource, CompletionTriggerKind, Selection } from 'vscode';8import { DefaultCompletionItemProvider } from '../defaultCompletionProvider';9import { closeAllEditors, withRandomFileEditor } from './testUtils';1011const completionProvider = new DefaultCompletionItemProvider();1213suite('Tests for completion in CSS embedded in HTML', () => {14teardown(closeAllEditors);1516test('style attribute & attribute value in html', async () => {17await testCompletionProvider('html', '<div style="|"', [{ label: 'padding: ;' }]);18await testCompletionProvider('html', `<div style='|'`, [{ label: 'padding: ;' }]);19await testCompletionProvider('html', `<div style='p|'`, [{ label: 'padding: ;' }]);20await testCompletionProvider('html', `<div style='color: #0|'`, [{ label: '#000000' }]);21});2223// https://github.com/microsoft/vscode/issues/7976624test('microsoft/vscode#79766, correct region determination', async () => {25await testCompletionProvider('html', `<div style="color: #000">di|</div>`, [26{ label: 'div', documentation: `<div>|</div>` }27]);28});2930// https://github.com/microsoft/vscode/issues/8694131test('microsoft/vscode#86941, widows should be completed after width', async () => {32await testCompletionProvider('css', `.foo { wi| }`, [33{ label: 'width: ;', documentation: `width: |;` }34]);35await testCompletionProvider('css', `.foo { wid| }`, [36{ label: 'width: ;', documentation: `width: |;` }37]);38try {39await testCompletionProvider('css', `.foo { wi| }`, [40{ label: 'widows: ;', documentation: `widows: |;` }41]);42} catch (e) {43assert.strictEqual(e.message, `Didn't find completion item with label widows: ;`);44}45try {46await testCompletionProvider('css', `.foo { wid| }`, [47{ label: 'widows: ;', documentation: `widows: |;` }48]);49} catch (e) {50assert.strictEqual(e.message, `Didn't find completion item with label widows: ;`);51}52await testCompletionProvider('css', `.foo { wido| }`, [53{ label: 'widows: ;', documentation: `widows: |;` }54]);55});5657// https://github.com/microsoft/vscode/issues/11702058test('microsoft/vscode#117020, ! at end of abbreviation should have completion', async () => {59await testCompletionProvider('css', `.foo { bdbn!| }`, [60{ label: 'border-bottom: none !important;', documentation: `border-bottom: none !important;` }61]);62});6364// https://github.com/microsoft/vscode/issues/13846165test('microsoft/vscode#138461, JSX array noise', async () => {66await testCompletionProvider('jsx', 'a[i]', undefined);67await testCompletionProvider('jsx', 'Component[a b]', undefined);68await testCompletionProvider('jsx', '[a, b]', undefined);69await testCompletionProvider('jsx', '[a=b]', [70{ label: '<div a="b"></div>', documentation: '<div a="b">|</div>' }71]);72});7374// https://github.com/microsoft/vscode-emmet-helper/pull/9075test('microsoft/vscode-emmet-helper#90', async () => {76await testCompletionProvider('html', 'dialog', [77{ label: '<dialog></dialog>', documentation: '<dialog>|</dialog>' }78]);79});80});8182interface TestCompletionItem {83label: string;8485documentation?: string;86}8788function testCompletionProvider(fileExtension: string, contents: string, expectedItems: TestCompletionItem[] | undefined): Thenable<boolean> {89const cursorPos = contents.indexOf('|');90const slicedContents = contents.slice(0, cursorPos) + contents.slice(cursorPos + 1);9192return withRandomFileEditor(slicedContents, fileExtension, async (editor, _doc) => {93const selection = new Selection(editor.document.positionAt(cursorPos), editor.document.positionAt(cursorPos));94editor.selection = selection;95const cancelSrc = new CancellationTokenSource();96const completionPromise = completionProvider.provideCompletionItems(97editor.document,98editor.selection.active,99cancelSrc.token,100{ triggerKind: CompletionTriggerKind.Invoke, triggerCharacter: undefined }101);102if (!completionPromise) {103return Promise.resolve();104}105106const completionList = await completionPromise;107if (!completionList || !completionList.items || !completionList.items.length) {108if (completionList === undefined) {109assert.strictEqual(expectedItems, completionList);110}111return Promise.resolve();112}113114assert.strictEqual(expectedItems === undefined, false);115expectedItems!.forEach(eItem => {116const matches = completionList.items.filter(i => i.label === eItem.label);117const match = matches && matches.length > 0 ? matches[0] : undefined;118assert.ok(match, `Didn't find completion item with label ${eItem.label}`);119120if (match) {121assert.strictEqual(match.detail, 'Emmet Abbreviation', `Match needs to come from Emmet`);122123if (eItem.documentation) {124assert.strictEqual(match.documentation, eItem.documentation, `Emmet completion Documentation doesn't match`);125}126}127});128129return Promise.resolve();130});131}132133134