From 2e1356b4c67206e1eda82d842fe4280452a048ff Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Tue, 24 Mar 2020 21:53:09 +0900 Subject: Add command-line parser on console scripts --- test/console/commandline/CommandLineParser.test.ts | 29 ++++++++++++++++++++++ test/console/commandline/CommandParser.test.ts | 15 +++++++++++ 2 files changed, 44 insertions(+) create mode 100644 test/console/commandline/CommandLineParser.test.ts create mode 100644 test/console/commandline/CommandParser.test.ts (limited to 'test/console/commandline') diff --git a/test/console/commandline/CommandLineParser.test.ts b/test/console/commandline/CommandLineParser.test.ts new file mode 100644 index 0000000..6aec682 --- /dev/null +++ b/test/console/commandline/CommandLineParser.test.ts @@ -0,0 +1,29 @@ +import CommandLineParser, {InputPhase} from "../../../src/console/commandline/CommandLineParser"; +import { Command } from "../../../src/shared/Command"; +import { expect } from "chai"; + +describe("CommandLineParser", () => { + describe("#inputPhase", () => { + it("returns parsed command-line", () => { + const sut = new CommandLineParser(); + expect(sut.inputPhase("")).to.equal(InputPhase.OnCommand); + expect(sut.inputPhase("op")).to.equal(InputPhase.OnCommand); + expect(sut.inputPhase("open ")).to.equal(InputPhase.OnArgs); + expect(sut.inputPhase("open apple")).to.equal(InputPhase.OnArgs) + }); + }); + describe("#parse", () => { + it("returns parsed command-line", () => { + const sut = new CommandLineParser(); + expect(sut.parse("open google apple")).to.deep.equal({ + command: Command.Open, + args: "google apple", + }); + + expect(sut.parse("qa")).to.deep.equal({ + command: Command.QuitAll, + args: "", + }); + }) + }) +}); diff --git a/test/console/commandline/CommandParser.test.ts b/test/console/commandline/CommandParser.test.ts new file mode 100644 index 0000000..4ad78fd --- /dev/null +++ b/test/console/commandline/CommandParser.test.ts @@ -0,0 +1,15 @@ +import CommandParser, { UnknownCommandError } from "../../../src/console/commandline/CommandParser"; +import { Command } from "../../../src/shared/Command"; +import { expect } from "chai" + +describe("CommandParser", () => { + describe("#parse", () => { + it("returns matched command with the string", () => { + const sut = new CommandParser(); + expect(sut.parse("open")).to.equal(Command.Open); + expect(sut.parse("w")).to.equal(Command.WindowOpen); + expect(sut.parse("bdelete!")).to.equal(Command.BufferDeleteForce); + expect(() => sut.parse("harakiri")).to.throw(UnknownCommandError); + }) + }) +}); -- cgit v1.2.3 From f7479c36ad0883455e511a86adf86e147ff4460a Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Wed, 25 Mar 2020 07:01:59 +0900 Subject: Remove ! from commands --- src/console/commandline/CommandParser.ts | 8 ++------ src/shared/Command.ts | 2 -- test/console/commandline/CommandParser.test.ts | 2 +- 3 files changed, 3 insertions(+), 9 deletions(-) (limited to 'test/console/commandline') diff --git a/src/console/commandline/CommandParser.ts b/src/console/commandline/CommandParser.ts index 5228c77..79ae8c8 100644 --- a/src/console/commandline/CommandParser.ts +++ b/src/console/commandline/CommandParser.ts @@ -8,6 +8,8 @@ export class UnknownCommandError extends Error { export default class CommandParser { parse(value: string): Command { + value = value.replace(/!$/, ""); + switch (value) { case 'o': case 'open': @@ -25,14 +27,8 @@ export default class CommandParser { case 'bdel': case 'bdelete': return Command.BufferDelete; - case 'bd!': - case 'bdel!': - case 'bdelete!': - return Command.BufferDeleteForce; case 'bdeletes': return Command.BuffersDelete; - case 'bdeletes!': - return Command.BuffersDeleteForce; case 'addbookmark': return Command.AddBookmark; case 'q': diff --git a/src/shared/Command.ts b/src/shared/Command.ts index e492f4a..cd2ed0a 100644 --- a/src/shared/Command.ts +++ b/src/shared/Command.ts @@ -4,9 +4,7 @@ export enum Command { WindowOpen = "winopen", Buffer = "buffer", BufferDelete = "bdelete", - BufferDeleteForce = "bdelete!", BuffersDelete = "bdeletes", - BuffersDeleteForce = "bdeletes!", AddBookmark = "addbookmark", Quit = "quit", QuitAll = "quitall", diff --git a/test/console/commandline/CommandParser.test.ts b/test/console/commandline/CommandParser.test.ts index 4ad78fd..129821a 100644 --- a/test/console/commandline/CommandParser.test.ts +++ b/test/console/commandline/CommandParser.test.ts @@ -8,7 +8,7 @@ describe("CommandParser", () => { const sut = new CommandParser(); expect(sut.parse("open")).to.equal(Command.Open); expect(sut.parse("w")).to.equal(Command.WindowOpen); - expect(sut.parse("bdelete!")).to.equal(Command.BufferDeleteForce); + expect(sut.parse("bdelete!")).to.equal(Command.BufferDelete); expect(() => sut.parse("harakiri")).to.throw(UnknownCommandError); }) }) -- cgit v1.2.3 From 7d51364584e9081f71f4691a713bb737f7573a74 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Wed, 25 Mar 2020 07:19:44 +0900 Subject: Revert "Remove ! from commands" This reverts commit f7479c36ad0883455e511a86adf86e147ff4460a. --- src/console/commandline/CommandParser.ts | 8 ++++++-- src/shared/Command.ts | 2 ++ test/console/commandline/CommandParser.test.ts | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) (limited to 'test/console/commandline') diff --git a/src/console/commandline/CommandParser.ts b/src/console/commandline/CommandParser.ts index 79ae8c8..5228c77 100644 --- a/src/console/commandline/CommandParser.ts +++ b/src/console/commandline/CommandParser.ts @@ -8,8 +8,6 @@ export class UnknownCommandError extends Error { export default class CommandParser { parse(value: string): Command { - value = value.replace(/!$/, ""); - switch (value) { case 'o': case 'open': @@ -27,8 +25,14 @@ export default class CommandParser { case 'bdel': case 'bdelete': return Command.BufferDelete; + case 'bd!': + case 'bdel!': + case 'bdelete!': + return Command.BufferDeleteForce; case 'bdeletes': return Command.BuffersDelete; + case 'bdeletes!': + return Command.BuffersDeleteForce; case 'addbookmark': return Command.AddBookmark; case 'q': diff --git a/src/shared/Command.ts b/src/shared/Command.ts index cd2ed0a..e492f4a 100644 --- a/src/shared/Command.ts +++ b/src/shared/Command.ts @@ -4,7 +4,9 @@ export enum Command { WindowOpen = "winopen", Buffer = "buffer", BufferDelete = "bdelete", + BufferDeleteForce = "bdelete!", BuffersDelete = "bdeletes", + BuffersDeleteForce = "bdeletes!", AddBookmark = "addbookmark", Quit = "quit", QuitAll = "quitall", diff --git a/test/console/commandline/CommandParser.test.ts b/test/console/commandline/CommandParser.test.ts index 129821a..4ad78fd 100644 --- a/test/console/commandline/CommandParser.test.ts +++ b/test/console/commandline/CommandParser.test.ts @@ -8,7 +8,7 @@ describe("CommandParser", () => { const sut = new CommandParser(); expect(sut.parse("open")).to.equal(Command.Open); expect(sut.parse("w")).to.equal(Command.WindowOpen); - expect(sut.parse("bdelete!")).to.equal(Command.BufferDelete); + expect(sut.parse("bdelete!")).to.equal(Command.BufferDeleteForce); expect(() => sut.parse("harakiri")).to.throw(UnknownCommandError); }) }) -- cgit v1.2.3