/
testUtils.ts
91 lines (76 loc) · 2.76 KB
/
testUtils.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
"use strict";
import {TextEditor} from '../src/textEditor';
import * as vscode from "vscode";
import * as assert from 'assert';
import {join} from 'path';
import * as os from 'os';
import * as fs from 'fs';
import { Configuration } from '../src/configuration/configuration';
function rndName() {
return Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 10);
}
async function createRandomFile(contents: string): Promise<vscode.Uri> {
const tmpFile = join(os.tmpdir(), rndName());
try {
fs.writeFileSync(tmpFile, contents);
return vscode.Uri.file(tmpFile);
} catch (error) {
throw error;
}
}
export function assertEqualLines(expectedLines: string[]) {
assert.equal(TextEditor.getLineCount(), expectedLines.length, "Line count does not match.");
for (let i = 0; i < expectedLines.length; i++) {
assert.equal(TextEditor.readLineAt(i), expectedLines[i], `Line ${i} is different.`);
}
}
/**
* Assert that the first two arguments are equal, and fail a test otherwise.
*
* The only difference between this and assert.equal is that here we
* check to ensure the types of the variables are correct.
*/
export function assertEqual<T>(one: T, two: T, message: string = ""): void {
assert.equal(one, two, message);
}
export async function setupWorkspace(): Promise<any> {
const file = await createRandomFile("");
const doc = await vscode.workspace.openTextDocument(file);
await vscode.window.showTextDocument(doc);
setTextEditorOptions(2, true);
assert.ok(vscode.window.activeTextEditor);
}
export async function cleanUpWorkspace(): Promise<any> {
// https://github.com/Microsoft/vscode/blob/master/extensions/vscode-api-tests/src/utils.ts
return new Promise((c, e) => {
if (vscode.window.visibleTextEditors.length === 0) {
return c();
}
// TODO: the visibleTextEditors variable doesn't seem to be
// up to date after a onDidChangeActiveTextEditor event, not
// even using a setTimeout 0... so we MUST poll :(
let interval = setInterval(() => {
if (vscode.window.visibleTextEditors.length > 0) {
return;
}
clearInterval(interval);
c();
}, 10);
vscode.commands.executeCommand('workbench.action.closeAllEditors')
.then(() => null, (err: any) => {
clearInterval(interval);
e(err);
});
}).then(() => {
assert.equal(vscode.window.visibleTextEditors.length, 0);
assert(!vscode.window.activeTextEditor);
});
}
export function setTextEditorOptions(tabSize: number, insertSpaces: boolean): void {
Configuration.tabstop = tabSize;
Configuration.expandtab = insertSpaces;
let options = vscode.window.activeTextEditor!.options;
options.tabSize = tabSize;
options.insertSpaces = insertSpaces;
vscode.window.activeTextEditor!.options = options;
}