Skip to content

Commit

Permalink
Fixes #11145: Write integration test for new option bag in `TextEdito…
Browse files Browse the repository at this point in the history
…r.edit`
  • Loading branch information
alexdima committed Aug 30, 2016
1 parent 943217e commit e1b98c6
Showing 1 changed file with 72 additions and 32 deletions.
104 changes: 72 additions & 32 deletions extensions/vscode-api-tests/src/editor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,96 @@
'use strict';

import * as assert from 'assert';
import {workspace, window, Position, Range} from 'vscode';
import {workspace, window, Position, Range, commands} from 'vscode';
import {createRandomFile, deleteFile, cleanUp} from './utils';

suite('editor tests', () => {

teardown(cleanUp);

test('make edit', () => {
return createRandomFile().then(file => {
function withRandomFileEditor(initialContents:string, run:(editor:vscode.TextEditor, doc:vscode.TextDocument)=>Thenable<void>): Thenable<boolean> {
return createRandomFile(initialContents).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 run(editor, doc).then(_ => {
if (doc.isDirty) {
return doc.save().then(saved => {
assert.ok(saved);
assert.ok(!doc.isDirty);
return deleteFile(file);
});
} else {
return deleteFile(file);
});
}
});
});
});
});
}

test('make edit', () => {
return withRandomFileEditor('', (editor, doc) => {
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);
});
});
});

test('issue #6281: Edits fail to validate ranges correctly before applying', () => {
return createRandomFile('Hello world!').then(file => {
return workspace.openTextDocument(file).then(doc => {
return window.showTextDocument(doc).then((editor) => {
return editor.edit((builder) => {
builder.replace(new Range(0, 0, Number.MAX_VALUE, Number.MAX_VALUE), 'new');
}).then(applied => {
assert.ok(applied);
assert.equal(doc.getText(), 'new');
assert.ok(doc.isDirty);
return withRandomFileEditor('Hello world!', (editor, doc) => {
return editor.edit((builder) => {
builder.replace(new Range(0, 0, Number.MAX_VALUE, Number.MAX_VALUE), 'new');
}).then(applied => {
assert.ok(applied);
assert.equal(doc.getText(), 'new');
assert.ok(doc.isDirty);
});
});
});

return doc.save().then(saved => {
assert.ok(saved);
assert.ok(!doc.isDirty);
function executeReplace(editor:vscode.TextEditor, range:Range, text:string, undoStopBefore:boolean, undoStopAfter: boolean): Thenable<boolean> {
return editor.edit((builder) => {
builder.replace(range, text);
}, { undoStopBefore: undoStopBefore, undoStopAfter: undoStopAfter });
}

return deleteFile(file);
});
});
});
test('TextEditor.edit can control undo/redo stack 1', () => {
return withRandomFileEditor('Hello world!', (editor, doc) => {
return executeReplace(editor, new Range(0, 0, 0, 1), 'h', false, false).then(applied => {
assert.ok(applied);
assert.equal(doc.getText(), 'hello world!');
assert.ok(doc.isDirty);
return executeReplace(editor, new Range(0, 1, 0, 5), 'ELLO', false, false);
}).then(applied => {
assert.ok(applied);
assert.equal(doc.getText(), 'hELLO world!');
assert.ok(doc.isDirty);
return commands.executeCommand('undo');
}).then(_ => {
assert.equal(doc.getText(), 'Hello world!');
});
});
});

test('TextEditor.edit can control undo/redo stack 2', () => {
return withRandomFileEditor('Hello world!', (editor, doc) => {
return executeReplace(editor, new Range(0, 0, 0, 1), 'h', false, false).then(applied => {
assert.ok(applied);
assert.equal(doc.getText(), 'hello world!');
assert.ok(doc.isDirty);
return executeReplace(editor, new Range(0, 1, 0, 5), 'ELLO', true, false);
}).then(applied => {
assert.ok(applied);
assert.equal(doc.getText(), 'hELLO world!');
assert.ok(doc.isDirty);
return commands.executeCommand('undo');
}).then(_ => {
assert.equal(doc.getText(), 'hello world!');
});
});
});
});
});

0 comments on commit e1b98c6

Please sign in to comment.