Skip to content

Commit 14c9d77

Browse files
author
Andrew Hall
authored
Add razor completion integration test (#7852)
Completes devdiv.visualstudio.com/DevDiv/_workitems/edit/1861421 and devdiv.visualstudio.com/DevDiv/_workitems/edit/1861422 Adds a test for <text> and <div> completion and sets up scaffolding for other completion tests.
1 parent 9f5693e commit 14c9d77

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import * as path from 'path';
7+
import * as vscode from 'vscode';
8+
import { beforeAll, afterAll, test, expect, beforeEach } from '@jest/globals';
9+
import testAssetWorkspace from './testAssets/testAssetWorkspace';
10+
import * as integrationHelpers from '../../lsptoolshost/integrationTests/integrationHelpers';
11+
12+
integrationHelpers.describeIfWindows(`Razor Completion ${testAssetWorkspace.description}`, function () {
13+
beforeAll(async function () {
14+
if (!integrationHelpers.isRazorWorkspace(vscode.workspace)) {
15+
return;
16+
}
17+
18+
await integrationHelpers.activateCSharpExtension();
19+
});
20+
21+
beforeEach(async function () {
22+
await integrationHelpers.openFileInWorkspaceAsync(path.join('Pages', 'Completion.razor'));
23+
});
24+
25+
afterAll(async () => {
26+
await testAssetWorkspace.cleanupWorkspace();
27+
});
28+
29+
test('Text Tag', async () => {
30+
if (!integrationHelpers.isRazorWorkspace(vscode.workspace)) {
31+
return;
32+
}
33+
34+
const insertPosition = new vscode.Position(4, 4);
35+
await insertText(insertPosition, '<te');
36+
const completionList = await getCompletionsAsync(new vscode.Position(4, 7), 'e', undefined);
37+
expect(completionList.items.length).toBeGreaterThan(0);
38+
const textTagCompletionItem = completionList.items.find((item) => item.label === 'text');
39+
40+
if (!textTagCompletionItem) {
41+
throw new Error(completionList.items.reduce((acc, item) => acc + item.label + '\n', ''));
42+
}
43+
44+
expect(textTagCompletionItem).toBeDefined();
45+
expect(textTagCompletionItem!.kind).toEqual(vscode.CompletionItemKind.Text);
46+
expect(textTagCompletionItem!.insertText).toBe('text');
47+
});
48+
49+
test('Div Tag', async () => {
50+
if (!integrationHelpers.isRazorWorkspace(vscode.workspace)) {
51+
return;
52+
}
53+
54+
const insertPosition = new vscode.Position(6, 0);
55+
await insertText(insertPosition, '<di');
56+
const completionList = await getCompletionsAsync(new vscode.Position(6, 3), undefined, 10);
57+
expect(completionList.items.length).toBeGreaterThan(0);
58+
const divTagCompletionItem = completionList.items.find((item) => item.label === 'div');
59+
60+
if (!divTagCompletionItem) {
61+
throw new Error(completionList.items.reduce((acc, item) => acc + item.label + '\n', ''));
62+
}
63+
64+
expect(divTagCompletionItem).toBeDefined();
65+
66+
// Reader, you may be wondering why the kind is a Property. To that I say: I don't know.
67+
// If you find out please add a detailed explanation. Thank you in advance.
68+
expect(divTagCompletionItem!.kind).toEqual(vscode.CompletionItemKind.Property);
69+
expect(divTagCompletionItem!.insertText).toBe('div');
70+
71+
const documentation = divTagCompletionItem!.documentation as vscode.MarkdownString;
72+
expect(documentation.value).toBe(
73+
'The div element has no special meaning at all. It represents its children. It can be used with the class, lang, and title attributes to mark up semantics common to a group of consecutive elements.\n\n[MDN Reference](https://developer.mozilla.org/docs/Web/HTML/Element/div)'
74+
);
75+
});
76+
77+
async function getCompletionsAsync(
78+
position: vscode.Position,
79+
triggerCharacter: string | undefined,
80+
resolveCount: number | undefined
81+
): Promise<vscode.CompletionList> {
82+
const activeEditor = vscode.window.activeTextEditor;
83+
if (!activeEditor) {
84+
throw new Error('No active editor');
85+
}
86+
87+
return await vscode.commands.executeCommand(
88+
'vscode.executeCompletionItemProvider',
89+
activeEditor.document.uri,
90+
position,
91+
triggerCharacter,
92+
resolveCount
93+
);
94+
}
95+
96+
async function insertText(position: vscode.Position, text: string): Promise<void> {
97+
const activeEditor = vscode.window.activeTextEditor;
98+
if (!activeEditor) {
99+
throw new Error('No active editor');
100+
}
101+
102+
await activeEditor.edit((builder) => {
103+
builder.insert(position, text);
104+
});
105+
}
106+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@page "/completion"
2+
3+
@* Insert text tag in below using completion *@
4+
@{
5+
6+
}
7+
8+
@code {
9+
10+
}

0 commit comments

Comments
 (0)