Add alias command to the interactive console
This commit is contained in:
1 parent
1497f114e6
commit
79ad7114b8
6 files changed
+131
-30
No files matched your search
@@ -16,37 +16,72 @@ namespace
|
||||
using namespace Bilicraft;
|
||||
TEST(ConsoleCommand, ParsesControlCommandsAndPathsWithSpaces)
|
||||
{
|
||||
EXPECT_EQ(ParseConsoleCommand(" ").kind, ConsoleKind::Empty);
|
||||
EXPECT_EQ(ParseConsoleCommand("# comment").kind, ConsoleKind::Empty);
|
||||
EXPECT_EQ(ParseConsoleCommand("help # help").kind, ConsoleKind::Help);
|
||||
EXPECT_EQ(ParseConsoleCommand("tasks").kind, ConsoleKind::Tasks);
|
||||
EXPECT_EQ(ParseConsoleCommand("exit").kind, ConsoleKind::Quit);
|
||||
EXPECT_EQ(ParseConsoleCommand("quit").kind, ConsoleKind::Quit);
|
||||
EXPECT_EQ(ParseConsoleCommand("run ../../actions/demo.act").path, "../../actions/demo.act");
|
||||
EXPECT_EQ(ParseConsoleCommand("run \"path with spaces.act\"").path, "path with spaces.act");
|
||||
EXPECT_EQ(ParseConsoleCommand("run 'path with spaces.act'").path, "path with spaces.act");
|
||||
ConsoleCommandParser parser;
|
||||
EXPECT_EQ(parser.Parse(" ").kind, ConsoleKind::Empty);
|
||||
EXPECT_EQ(parser.Parse("# comment").kind, ConsoleKind::Empty);
|
||||
EXPECT_EQ(parser.Parse("help # help").kind, ConsoleKind::Help);
|
||||
EXPECT_EQ(parser.Parse("tasks").kind, ConsoleKind::Tasks);
|
||||
EXPECT_EQ(parser.Parse("exit").kind, ConsoleKind::Quit);
|
||||
EXPECT_EQ(parser.Parse("quit").kind, ConsoleKind::Quit);
|
||||
EXPECT_EQ(parser.Parse("run ../../actions/demo.act").argument, "../../actions/demo.act");
|
||||
EXPECT_EQ(parser.Parse("run \"path with spaces.act\"").argument, "path with spaces.act");
|
||||
EXPECT_EQ(parser.Parse("run 'path with spaces.act'").argument, "path with spaces.act");
|
||||
}
|
||||
TEST(ConsoleCommand, RejectsInvalidControlsAndInteractiveWaitOrImport)
|
||||
{
|
||||
ConsoleCommandParser parser;
|
||||
for (auto text : {"run", "run \"\"", "help now", "tasks 1", "quit now", "wait 3", "import demo",
|
||||
"sw 1\nsw 2", "sw 1\rsw 2"})
|
||||
EXPECT_THROW(ParseConsoleCommand(text), std::exception) << text;
|
||||
EXPECT_THROW(ParseConsoleCommand(std::string("sw 1\0x", 6)), std::exception);
|
||||
EXPECT_THROW(parser.Parse(text), std::exception) << text;
|
||||
EXPECT_THROW(parser.Parse(std::string("sw 1\0x", 6)), std::exception);
|
||||
}
|
||||
TEST(ConsoleCommand, UsesTheSameGameCommandParser)
|
||||
{
|
||||
const auto message = ParseConsoleCommand("sendmsg @Steve hello world");
|
||||
ConsoleCommandParser parser;
|
||||
const auto message = parser.Parse("sendmsg @Steve hello world");
|
||||
ASSERT_EQ(message.kind, ConsoleKind::Action);
|
||||
ASSERT_TRUE(message.action);
|
||||
EXPECT_EQ(message.action->recipient, "Steve");
|
||||
EXPECT_EQ(message.action->text, "hello world");
|
||||
EXPECT_EQ(ParseConsoleCommand("drop all").action->kind, ActionKind::Drop);
|
||||
EXPECT_TRUE(ParseConsoleCommand("drop all").action->all);
|
||||
EXPECT_EQ(parser.Parse("drop all").action->kind, ActionKind::Drop);
|
||||
EXPECT_TRUE(parser.Parse("drop all").action->all);
|
||||
}
|
||||
|
||||
TEST(ConsoleCommand, ExpandsSessionAliasesWithArguments)
|
||||
{
|
||||
ConsoleCommandParser parser;
|
||||
EXPECT_EQ(parser.Parse("alias tell sendmsg @Steve").kind, ConsoleKind::Empty);
|
||||
parser.Parse("alias report tell 收获");
|
||||
const auto result = parser.Parse("report 完成 # comment");
|
||||
ASSERT_TRUE(result.action);
|
||||
EXPECT_EQ(result.action->recipient, "Steve");
|
||||
EXPECT_EQ(result.action->text, "收获 完成");
|
||||
parser.Parse("alias tell run");
|
||||
EXPECT_EQ(parser.Parse("tell 'path with spaces.act'").argument, "path with spaces.act");
|
||||
EXPECT_EQ(parser.Parse("alias tell").argument, "alias tell run\n");
|
||||
EXPECT_EQ(parser.Parse("alias").argument, "alias report tell 收获\nalias tell run\n");
|
||||
EXPECT_THROW(ConsoleCommandParser{}.Parse("tell hello"), std::exception);
|
||||
}
|
||||
|
||||
TEST(ConsoleCommand, SupportsSameNameDefaultsAndRejectsAliasCyclesAndOversizedExpansion)
|
||||
{
|
||||
ConsoleCommandParser parser;
|
||||
parser.Parse("alias sw sw 2");
|
||||
const auto selected = parser.Parse("sw");
|
||||
ASSERT_TRUE(selected.action);
|
||||
EXPECT_EQ(selected.action->slot, 2);
|
||||
parser.Parse("alias a b");
|
||||
parser.Parse("alias b a");
|
||||
EXPECT_THROW(parser.Parse("a"), std::exception);
|
||||
EXPECT_THROW(parser.Parse("alias alias quit"), std::exception);
|
||||
parser.Parse("alias m sendmsg hello");
|
||||
EXPECT_THROW(parser.Parse("m " + std::string(65534, 'x')), std::exception);
|
||||
}
|
||||
|
||||
#ifndef _WIN32
|
||||
TEST(ConsoleCommand, NativePseudoTerminalWaitsForEnterAndParsesTheCommand)
|
||||
{
|
||||
ConsoleCommandParser parser;
|
||||
const int master = posix_openpt(O_RDWR | O_NOCTTY);
|
||||
ASSERT_GE(master, 0);
|
||||
struct Fd
|
||||
@@ -75,7 +110,7 @@ TEST(ConsoleCommand, NativePseudoTerminalWaitsForEnterAndParsesTheCommand)
|
||||
if (!line) std::this_thread::yield();
|
||||
}
|
||||
ASSERT_TRUE(line);
|
||||
const auto command = ParseConsoleCommand(*line);
|
||||
const auto command = parser.Parse(*line);
|
||||
ASSERT_TRUE(command.action);
|
||||
EXPECT_EQ(command.action->kind, ActionKind::SelectHotbar);
|
||||
EXPECT_EQ(command.action->slot, 3);
|
||||
@@ -83,6 +118,7 @@ TEST(ConsoleCommand, NativePseudoTerminalWaitsForEnterAndParsesTheCommand)
|
||||
|
||||
TEST(ConsoleCommand, PipeInputDoesNotConsumeNextCommandUntilPreviousExecutionFinishes)
|
||||
{
|
||||
ConsoleCommandParser parser;
|
||||
int descriptors[2];
|
||||
ASSERT_EQ(pipe(descriptors), 0);
|
||||
struct Close
|
||||
@@ -104,7 +140,7 @@ TEST(ConsoleCommand, PipeInputDoesNotConsumeNextCommandUntilPreviousExecutionFin
|
||||
if (auto line = terminal.PollLine())
|
||||
{
|
||||
++linesRead;
|
||||
scheduler.SubmitInteractive(*ParseConsoleCommand(*line).action);
|
||||
scheduler.SubmitInteractive(*parser.Parse(*line).action);
|
||||
}
|
||||
};
|
||||
pump();
|
||||
|
||||
Reference in new issue
Block a user