Skip to content

Commit

Permalink
Merge pull request #79 from benjaminRomano/master
Browse files Browse the repository at this point in the history
Created tests for modeInsert
  • Loading branch information
jpoon committed Dec 2, 2015
2 parents b1fe475 + 0c7a002 commit 4883816
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/mode/mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export abstract class Mode {

abstract ShouldBeActivated(key : string, currentMode : ModeName) : boolean;

abstract HandleActivation(key : string) : void;
abstract HandleActivation(key : string) : Thenable<void> | void;

abstract HandleKeyEvent(key : string) : void;
}
17 changes: 10 additions & 7 deletions src/mode/modeInsert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import TextEditor from './../textEditor';
import Cursor from './../cursor';

export default class InsertMode extends Mode {
private activationKeyHandler : { [ key : string] : () => void; } = {};

private activationKeyHandler : { [ key : string] : () => Thenable<void> | void; };

constructor() {
super(ModeName.Insert);
Expand All @@ -24,12 +25,12 @@ export default class InsertMode extends Mode {

// open blank line below current line
"o" : () => {
vscode.commands.executeCommand("editor.action.insertLineAfter");
return vscode.commands.executeCommand("editor.action.insertLineAfter");
},

// open blank line above current line
"O" : () => {
vscode.commands.executeCommand("editor.action.insertLineBefore");
return vscode.commands.executeCommand("editor.action.insertLineBefore");
}
};
}
Expand All @@ -38,15 +39,17 @@ export default class InsertMode extends Mode {
return key in this.activationKeyHandler;
}

HandleActivation(key : string) : void {
this.activationKeyHandler[key]();
HandleActivation(key : string) : Thenable<void> | void {
return this.activationKeyHandler[key]();
}

HandleKeyEvent(key : string) : void {
HandleKeyEvent(key : string) : Thenable<boolean> {
this.keyHistory.push(key);
TextEditor.insert(this.ResolveKeyValue(key));
var thenable = TextEditor.insert(this.ResolveKeyValue(key));

vscode.commands.executeCommand("editor.action.triggerSuggest");

return thenable;
}

// Some keys have names that are different to their value.
Expand Down
4 changes: 4 additions & 0 deletions src/textEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export default class TextEditor {
});
}

static readFile(): string {
return vscode.window.activeTextEditor.document.getText();
}

static readLine(lineNo: number = null): string {
if (lineNo === null) {
lineNo = vscode.window.activeTextEditor.selection.active.line;
Expand Down
1 change: 0 additions & 1 deletion test/cursor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ suite("cursor", () => {
assert.equal(current.character, 0);

let down = Cursor.down();
console.log(down.character);
assert.equal(down.line, 2);
assert.equal(down.character, 0);
});
Expand Down
128 changes: 128 additions & 0 deletions test/mode/modeInsert.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import * as assert from 'assert';

import ModeInsert from '../../src/mode/modeInsert';
import {ModeName} from '../../src/mode/mode';
import Cursor from '../../src/cursor';
import TextEditor from '../../src/textEditor';

import * as testUtils from '../testUtils';

import * as vscode from 'vscode';

let modeHandler: ModeInsert = null;

suite("Mode Insert", () => {
setup((done) => {
modeHandler = new ModeInsert();

testUtils.clearTextEditor()
.then(done);
});

teardown((done) => {
modeHandler = null;

testUtils.clearTextEditor()
.then(done);
});

test("can be activated", () => {
let activationKeys = ['i', 'I', 'o', 'O', 'a', 'A'];

for (let i = 0; i < activationKeys.length; i++) {
let key = activationKeys[i];
assert.equal(modeHandler.ShouldBeActivated(key, ModeName.Insert), true, key);
}
});

test("can handle key events", (done) => {
let expected = "!";

modeHandler.HandleKeyEvent("!")
.then(() => {
return testUtils.assertTextEditorText(expected);
}).then(done);
});

test("Can handle 'o'", (done) => {
let expected = "text\n";

TextEditor.insert("text")
.then(() => {
return modeHandler.HandleActivation("o");
}).then(() => {
return testUtils.assertTextEditorText(expected);
}).then(done);
});

test("Can handle 'O'", (done) => {
let expected = "\ntext";

TextEditor.insert("text")
.then(() => {
return modeHandler.HandleActivation("O");
}).then(() => {
return testUtils.assertTextEditorText(expected);
}).then(done);
});

test("Can handle 'i'", (done) => {
let expected = "text!text";

TextEditor.insert("texttext")
.then(() => {
Cursor.move(new vscode.Position(0, 4));
}).then(() => {
return modeHandler.HandleActivation("i");
}).then(() => {
return modeHandler.HandleKeyEvent("!");
}).then(() => {
return testUtils.assertTextEditorText(expected);
}).then(done);
});

test("Can handle 'I'", (done) => {
let expected = "!text";

TextEditor.insert("text")
.then(() => {
Cursor.move(new vscode.Position(0, 4));
}).then(() => {
return modeHandler.HandleActivation("I");
}).then(() => {
return modeHandler.HandleKeyEvent("!");
}).then(() => {
return testUtils.assertTextEditorText(expected);
}).then(done);
});

test("Can handle 'a'", (done) => {
let expected = "textt!ext";

TextEditor.insert("texttext")
.then(() => {
Cursor.move(new vscode.Position(0, 4));
}).then(() => {
return modeHandler.HandleActivation("a");
}).then(() => {
return modeHandler.HandleKeyEvent("!");
}).then(() => {
return testUtils.assertTextEditorText(expected);
}).then(done);
});

test("Can handle 'A'", (done) => {
let expected = "text!";

TextEditor.insert("text")
.then(() => {
Cursor.move(new vscode.Position(0, 0));
}).then(() => {
return modeHandler.HandleActivation("A");
}).then(() => {
return modeHandler.HandleKeyEvent("!");
}).then(() => {
return testUtils.assertTextEditorText(expected);
}).then(done);
});
});
22 changes: 22 additions & 0 deletions test/testUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as vscode from 'vscode';
import TextEditor from '../src/textEditor';
import Cursor from '../src/cursor';
import * as assert from 'assert';

export function clearTextEditor(): Thenable<void> {
let range = new vscode.Range(Cursor.documentBegin(), Cursor.documentEnd());
return TextEditor.delete(range).then(() => {
return;
});
}

export function assertTextEditorText(expected: string, lineNo?: number) {
let actual: string;
if (isNaN(lineNo) || typeof lineNo === 'undefined') {
actual = TextEditor.readFile();
} else {
actual = TextEditor.readLine(lineNo);
}

assert.equal(actual, expected);
}
10 changes: 6 additions & 4 deletions test/textEditor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ import * as vscode from 'vscode';
import TextEditor from './../src/textEditor';
import Cursor from './../src/cursor';

import * as testUtils from './testUtils';

suite("text editor", () => {
suiteSetup(done => {
let range = new vscode.Range(Cursor.documentBegin(), Cursor.documentEnd());
TextEditor.delete(range).then(done());
testUtils.clearTextEditor()
.then(done);
});

suiteTeardown(done => {
var range = new vscode.Range(Cursor.documentBegin(), Cursor.documentEnd());
TextEditor.delete(range).then(done());
testUtils.clearTextEditor()
.then(done);
});

test("insert 'Hello World'", done => {
Expand Down

0 comments on commit 4883816

Please sign in to comment.