-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathe2e.test.ts
101 lines (87 loc) · 3.14 KB
/
e2e.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import * as vscode from "vscode";
import * as assert from "assert";
import { getDocumentUri, activateExtension, sleep } from "./helper";
suite("E2E tests", () => {
const TIMEOUT = 5000;
teardown(async () => {
await vscode.commands.executeCommand("workbench.action.closeAllEditors");
});
test("Should provide diagnostics", async () => {
await activateExtension();
const documentUri = getDocumentUri("diagnostics.py");
const document = await vscode.workspace.openTextDocument(documentUri);
await vscode.window.showTextDocument(document);
const editor = vscode.window.activeTextEditor;
assert.ok(editor, "No active text editor");
assert.ok(
editor.document.uri.fsPath.endsWith("diagnostics.py"),
"Active text editor is not diagnostics.py",
);
let actualDiagnostics = vscode.languages.getDiagnostics(documentUri);
if (actualDiagnostics.length === 0) {
// Wait for diagnostics to be computed
let timeout = TIMEOUT;
while (actualDiagnostics.length === 0 && timeout > 0) {
await sleep(100);
actualDiagnostics = vscode.languages.getDiagnostics(documentUri);
timeout -= 100;
}
assert.ok(actualDiagnostics.length > 0, `No diagnostics provided in ${TIMEOUT}ms`);
}
actualDiagnostics = actualDiagnostics.sort((a, b) => {
return a.range.start.compareTo(b.range.start);
});
const expectedDiagnostics = [
{
message: "Import block is un-sorted or un-formatted",
range: toRange(0, 0, 4, 0),
severity: vscode.DiagnosticSeverity.Warning,
},
{
message: "`pathlib.Path` imported but unused",
range: toRange(0, 20, 0, 24),
severity: vscode.DiagnosticSeverity.Warning,
},
{
message: "Undefined name `name`",
range: toRange(5, 35, 5, 39),
severity: vscode.DiagnosticSeverity.Error,
},
];
assert.equal(actualDiagnostics.length, expectedDiagnostics.length);
actualDiagnostics.forEach((actualDiagnostic, i) => {
const expectedDiagnostic = expectedDiagnostics[i];
assert.deepEqual(
new vscode.Diagnostic(
actualDiagnostic.range,
actualDiagnostic.message,
actualDiagnostic.severity,
),
expectedDiagnostic,
);
});
});
test("Should format document", async () => {
await activateExtension();
const docUri = getDocumentUri("formatting.py");
const document = await vscode.workspace.openTextDocument(docUri);
await vscode.window.showTextDocument(document);
const originalContent = document.getText();
const expectedContent = `\
def function(
foo,
bar,
):
print("hello world")
`;
assert.notEqual(originalContent, expectedContent);
await vscode.commands.executeCommand("editor.action.formatDocument");
const formattedContent = document.getText();
assert.equal(formattedContent, expectedContent);
});
});
function toRange(startLine: number, startChar: number, endLine: number, endChar: number) {
const start = new vscode.Position(startLine, startChar);
const end = new vscode.Position(endLine, endChar);
return new vscode.Range(start, end);
}