Basic test for substitute.

This commit is contained in:
rebornix 2016-07-01 15:38:13 +08:00
parent 9c941a1080
commit c3c720397f
3 changed files with 64 additions and 5 deletions

View File

@ -23,7 +23,7 @@ export async function showCmdLine(initialText: string, modeHandler : ModeHandler
}
}
function runCmdLine(command : string, modeHandler : ModeHandler) : Promise<{}> {
export async function runCmdLine(command : string, modeHandler : ModeHandler) : Promise<{}> {
if (!command || command.length === 0) {
return;
}
@ -34,7 +34,7 @@ function runCmdLine(command : string, modeHandler : ModeHandler) : Promise<{}> {
return;
}
cmd.execute(vscode.window.activeTextEditor, modeHandler);
await cmd.execute(vscode.window.activeTextEditor, modeHandler);
} catch (e) {
modeHandler.setupStatusBarItem(e.toString());
}

View File

@ -87,16 +87,16 @@ export class CommandLine {
return ":" + this.range.toString() + " " + this.command.toString();
}
execute(document : vscode.TextEditor, modeHandler : ModeHandler) : void {
async execute(document : vscode.TextEditor, modeHandler : ModeHandler) : Promise<void> {
if (!this.command) {
this.range.execute(document);
return;
}
if (this.range.isEmpty) {
this.command.execute(modeHandler);
await this.command.execute(modeHandler);
} else {
this.command.executeWithRange(modeHandler, this.range);
await this.command.executeWithRange(modeHandler, this.range);
}
}

View File

@ -0,0 +1,59 @@
"use strict";
import * as assert from 'assert';
import { ModeHandler } from '../../src/mode/modeHandler';
import { setupWorkspace, cleanUpWorkspace, assertEqualLines, assertEqual } from './../testUtils';
import { ModeName } from '../../src/mode/mode';
import { TextEditor } from '../../src/textEditor';
import { runCmdLine } from '../../src/cmd_line/main';
suite("Basic substitute", () => {
let modeHandler: ModeHandler;
setup(async () => {
await setupWorkspace();
modeHandler = new ModeHandler();
});
teardown(cleanUpWorkspace);
test("Replace single word once", async () => {
await modeHandler.handleMultipleKeyEvents(['i', 'a', 'b', 'a', '<esc>']);
await runCmdLine("%s/a/d", modeHandler);
assertEqualLines([
"dba"
]);
});
test("Replace with `g` flag", async () => {
await modeHandler.handleMultipleKeyEvents(['i', 'a', 'b', 'a', '<esc>']);
await runCmdLine("%s/a/d/g", modeHandler);
assertEqualLines([
"dbd"
]);
});
test("Replace multiple lines", async () => {
await modeHandler.handleMultipleKeyEvents(['i', 'a', 'b', 'a', '<esc>', 'o', 'a', 'b']);
await runCmdLine("%s/a/d/g", modeHandler);
assertEqualLines([
"dbd",
"db"
]);
});
test("Replace across specific lines", async () => {
await modeHandler.handleMultipleKeyEvents(['i', 'a', 'b', 'a', '<esc>', 'o', 'a', 'b']);
await runCmdLine("1,1s/a/d/g", modeHandler);
assertEqualLines([
"dbd",
"ab"
]);
});
});