162 lines
5.9 KiB
C++
162 lines
5.9 KiB
C++
#include "ConsoleCommand.h"
|
|
#include "ActionScheduler.h"
|
|
#include "TerminalInput.h"
|
|
|
|
#include <gtest/gtest.h>
|
|
#include <thread>
|
|
|
|
#ifndef _WIN32
|
|
#include <cstdlib>
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
#endif
|
|
|
|
namespace
|
|
{
|
|
using namespace Bilicraft;
|
|
TEST(ConsoleCommand, ParsesControlCommandsAndPathsWithSpaces)
|
|
{
|
|
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(parser.Parse(text), std::exception) << text;
|
|
EXPECT_THROW(parser.Parse(std::string("sw 1\0x", 6)), std::exception);
|
|
}
|
|
TEST(ConsoleCommand, UsesTheSameGameCommandParser)
|
|
{
|
|
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(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
|
|
{
|
|
int value;
|
|
~Fd() { if (value >= 0) close(value); }
|
|
} masterGuard{master};
|
|
ASSERT_EQ(grantpt(master), 0);
|
|
ASSERT_EQ(unlockpt(master), 0);
|
|
const char *name = ptsname(master);
|
|
ASSERT_NE(name, nullptr);
|
|
const int slave = open(name, O_RDWR | O_NOCTTY);
|
|
ASSERT_GE(slave, 0);
|
|
Fd slaveGuard{slave};
|
|
TerminalInput terminal(slave);
|
|
ASSERT_TRUE(terminal.IsInteractive());
|
|
ASSERT_EQ(write(master, "sw 3", 4), 4);
|
|
EXPECT_FALSE(terminal.PollLine());
|
|
ASSERT_EQ(write(master, "\n", 1), 1);
|
|
// Delivery through a PTY can be asynchronous; a bounded poll, not a blocking read.
|
|
std::optional<std::string> line;
|
|
const auto deadline = ActionScheduler::Clock::now() + std::chrono::seconds(1);
|
|
while (!line && ActionScheduler::Clock::now() < deadline)
|
|
{
|
|
line = terminal.PollLine();
|
|
if (!line) std::this_thread::yield();
|
|
}
|
|
ASSERT_TRUE(line);
|
|
const auto command = parser.Parse(*line);
|
|
ASSERT_TRUE(command.action);
|
|
EXPECT_EQ(command.action->kind, ActionKind::SelectHotbar);
|
|
EXPECT_EQ(command.action->slot, 3);
|
|
}
|
|
|
|
TEST(ConsoleCommand, PipeInputDoesNotConsumeNextCommandUntilPreviousExecutionFinishes)
|
|
{
|
|
ConsoleCommandParser parser;
|
|
int descriptors[2];
|
|
ASSERT_EQ(pipe(descriptors), 0);
|
|
struct Close
|
|
{
|
|
int a, b;
|
|
~Close() { close(a); close(b); }
|
|
} closePipe{descriptors[0], descriptors[1]};
|
|
const std::string input = "sendmsg first\nsendmsg second\n";
|
|
ASSERT_EQ(write(descriptors[1], input.data(), input.size()), static_cast<ssize_t>(input.size()));
|
|
TerminalInput terminal(descriptors[0]);
|
|
ActionScheduler scheduler;
|
|
const auto now = ActionScheduler::Clock::time_point{};
|
|
scheduler.Enqueue({"active", {*ParseInteractiveCommand("sendmsg active")}});
|
|
auto work = scheduler.Next(now);
|
|
ASSERT_EQ(work->command.text, "active");
|
|
int linesRead = 0;
|
|
const auto pump = [&] {
|
|
if (scheduler.InteractivePending()) return;
|
|
if (auto line = terminal.PollLine())
|
|
{
|
|
++linesRead;
|
|
scheduler.SubmitInteractive(*parser.Parse(*line).action);
|
|
}
|
|
};
|
|
pump();
|
|
pump();
|
|
EXPECT_EQ(linesRead, 1);
|
|
scheduler.Finish(true);
|
|
work = scheduler.Next(now);
|
|
ASSERT_TRUE(work->interactive);
|
|
EXPECT_EQ(work->command.text, "first");
|
|
pump();
|
|
EXPECT_EQ(linesRead, 1);
|
|
scheduler.Finish(true);
|
|
pump();
|
|
EXPECT_EQ(linesRead, 2);
|
|
EXPECT_EQ(scheduler.Next(now)->command.text, "second");
|
|
}
|
|
#endif
|
|
} // namespace
|