提交 4ea2714d 编写于 作者: B Benjamin Pasero

add more meaningful api tests

上级 8a0f53ba
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as assert from 'assert';
import * as fs from 'fs';
import * as os from 'os';
import {workspace, window, Position} from 'vscode';
import {createRandomFile, deleteFile} from './utils';
import {join} from 'path';
suite("editor tests", () => {
test('make edit', (done) => {
createRandomFile().then(file => {
return workspace.openTextDocument(file).then(doc => {
return window.showTextDocument(doc).then((editor) => {
return editor.edit((builder) => {
builder.insert(new Position(0, 0), 'Hello World');
}).then(applied => {
assert.ok(applied);
assert.equal(doc.getText(), 'Hello World');
assert.ok(doc.isDirty);
return doc.save().then(saved => {
assert.ok(saved);
assert.ok(!doc.isDirty);
return deleteFile(file);
});
});
});
});
}).then(() => done(), (error) => done(error));
});
});
\ No newline at end of file
......@@ -21,7 +21,8 @@ const testRunner = require('vscode/lib/testrunner');
// See https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically#set-options for more info
testRunner.configure({
ui: 'tdd', // the TDD UI is being used in extension.test.ts (suite, test, etc.)
useColors: true // colored output from test results
useColors: true, // colored output from test results
timeout: 10000
});
export= testRunner;
\ No newline at end of file
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as vscode from 'vscode';
import * as fs from 'fs';
import * as os from 'os';
import {join} from 'path';
function rndName() {
return Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 10);
}
export function createRandomFile(contents = ''): Thenable<vscode.Uri> {
return new Promise((resolve, reject) => {
const tmpFile = join(os.tmpdir(), rndName());
fs.writeFile(tmpFile, contents, (error) => {
if (error) {
return reject(error);
}
resolve(vscode.Uri.file(tmpFile));
});
});
}
export function deleteFile(file: vscode.Uri): Thenable<boolean> {
return new Promise((resolve, reject) => {
fs.unlink(file.fsPath, (err) => {
if (err) {
reject(err);
} else {
resolve(true);
}
});
});
}
\ No newline at end of file
......@@ -6,12 +6,18 @@
'use strict';
import * as assert from 'assert';
import {window, workspace} from 'vscode';
import {workspace, window} from 'vscode';
import {join} from 'path';
suite("window namespace texts", () => {
// test('open document fires event', (done) => {
// });
suite("window namespace tests", () => {
test('active text editor', (done) => {
workspace.openTextDocument(join(workspace.rootPath, './far.js')).then(doc => {
return window.showTextDocument(doc).then((editor) => {
const active = window.activeTextEditor;
assert.ok(active);
assert.equal(active.document.uri.fsPath, doc.uri.fsPath);
});
}).then(() => done(), (error) => done(error));
});
});
\ No newline at end of file
......@@ -6,8 +6,15 @@
'use strict';
import * as assert from 'assert';
import {workspace, TextDocument} from 'vscode';
import {workspace, TextDocument, window, Position} from 'vscode';
import {createRandomFile, deleteFile} from './utils';
import {join} from 'path';
import * as fs from 'fs';
import * as os from 'os';
function rndName() {
return Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 10);
}
suite('workspace-namespace', () => {
......@@ -38,17 +45,52 @@ suite('workspace-namespace', () => {
});
});
// test('createTextDocument', done => {
test('events: onDidOpenTextDocument, onDidChangeTextDocument, onDidSaveTextDocument', (done) => {
createRandomFile().then(file => {
let onDidOpenTextDocument = false;
workspace.onDidOpenTextDocument(e => {
assert.equal(e.uri.fsPath, file.fsPath);
onDidOpenTextDocument = true;
});
let onDidChangeTextDocument = false;
workspace.onDidChangeTextDocument(e => {
assert.equal(e.document.uri.fsPath, file.fsPath);
onDidChangeTextDocument = true;
});
let onDidSaveTextDocument = false;
workspace.onDidSaveTextDocument(e => {
assert.equal(e.uri.fsPath, file.fsPath);
onDidSaveTextDocument = true;
});
return workspace.openTextDocument(file).then(doc => {
return window.showTextDocument(doc).then((editor) => {
return editor.edit((builder) => {
builder.insert(new Position(0, 0), 'Hello World');
}).then(applied => {
return doc.save().then(saved => {
assert.ok(onDidOpenTextDocument);
assert.ok(onDidChangeTextDocument);
assert.ok(onDidSaveTextDocument);
return deleteFile(file);
});
});
});
});
}).then(() => done(), (error) => done(error));
});
// let text = 'Das Pferd isst keinen Reis.'
test('findFiles', done => {
workspace.findFiles('*.js', null).then((res) => {
assert.equal(res.length, 1);
assert.equal(workspace.asRelativePath(res[0]), '/far.js');
// workspace.createTextDocument(text).then(doc => {
// assert.equal(doc.getText(), text);
// assert.equal(doc.uri.scheme, 'untitled');
// assert.equal(doc.languageId, 'plaintext');
// done();
// }, err => {
// done(err);
// });
// });
done();
}, err => {
done(err);
});
});
});
\ No newline at end of file
......@@ -190,6 +190,15 @@ function setupIPC(): TPromise<Server> {
// there's a running instance, let's connect to it
return connect(env.mainIPCHandle).then(
client => {
// Tests from CLI require to be the only instance currently (TODO@Ben support multiple instances and output)
if (env.isTestingFromCli) {
const errorMsg = 'Running tests from the command line is currently only supported if no other instance of Code is running.';
console.error(errorMsg);
return Promise.wrapError(errorMsg);
}
env.log('Sending env to running instance...');
const service = client.getService<LaunchService>('LaunchService', LaunchService);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册