add more meaningful api tests

This commit is contained in:
Benjamin Pasero
2015-11-25 08:44:13 +01:00
parent 8a0f53baa4
commit 4ea2714df8
6 changed files with 156 additions and 19 deletions

View 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 text = 'Das Pferd isst keinen Reis.'
let onDidChangeTextDocument = false;
workspace.onDidChangeTextDocument(e => {
assert.equal(e.document.uri.fsPath, file.fsPath);
onDidChangeTextDocument = true;
});
// 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);
// });
// });
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));
});
test('findFiles', done => {
workspace.findFiles('*.js', null).then((res) => {
assert.equal(res.length, 1);
assert.equal(workspace.asRelativePath(res[0]), '/far.js');
done();
}, err => {
done(err);
});
});
});