Add define parameters and support aliases and definitions in action files

This commit is contained in:
cantyonion committed 2026-11-24 13:45:18 +08:00
1 parent fb8ef636bd
commit 0a6193f68e
12 files changed
+291 -63

No files matched your search

+39
View File
@@ -1,4 +1,5 @@
#include "ActionFile.h"
#include "ConsoleCommand.h"
#include <atomic>
#include <chrono>
@@ -112,6 +113,44 @@ class ActionFileTest : public ::testing::Test
}
};
TEST_F(ActionFileTest, ExpandsBindingsInSourceOrderAcrossImports)
{
Write("child.act", "# action: child\nhome\ndefine pos1 20 64 -8\nalias pause wait 1 s\n");
const auto program = Parse("# action: root\ndefine pos1 1 1 1\nalias home goto $pos1\n"
"define file child\nalias include import\ninclude $file\nhome\npause\n");
ASSERT_EQ(program.commands.size(), 3u);
const auto &first = program.commands[0];
EXPECT_EQ(first.kind, ActionKind::Goto);
EXPECT_EQ(first.x, 1);
EXPECT_EQ(first.source.filename(), "child.act");
EXPECT_EQ(first.line, 2u);
EXPECT_EQ(program.commands[1].x, 20);
EXPECT_EQ(program.commands[1].y, 64);
EXPECT_EQ(program.commands[1].z, -8);
EXPECT_EQ(program.commands[2].waitDuration, std::chrono::seconds(1));
}
TEST_F(ActionFileTest, RunInheritsConsoleBindingsAndKeepsOverridesLocal)
{
Bilicraft::ConsoleCommandParser console;
console.Parse("define slot 1");
console.Parse("alias pick sw $slot");
const auto path = Write("test.act", "# action: test\npick\ndefine slot 3\npick\n");
const auto program = LoadActionFile(path, console.GetBindings());
ASSERT_EQ(program.commands.size(), 2u);
EXPECT_EQ(program.commands[0].slot, 1);
EXPECT_EQ(program.commands[1].slot, 3);
EXPECT_EQ(console.Parse("pick").action->slot, 1);
EXPECT_THROW(LoadActionFile(path), std::runtime_error);
}
TEST_F(ActionFileTest, ReportsBindingErrorsAtTheSourceLine)
{
Reject("# action: test\ngoto $missing\n", "undefined parameter: $missing");
Reject("# action: test\nalias a b\nalias b a\na\n", "alias cycle", 4);
Reject("# action: test\ndefine name\n", "requires a name and value");
}
TEST_F(ActionFileTest, ParsesEveryInstructionAndStoresSourceLocations)
{
const auto program = Parse("# action: farming\n"
+37
View File
@@ -78,6 +78,43 @@ TEST(ConsoleCommand, SupportsSameNameDefaultsAndRejectsAliasCyclesAndOversizedEx
EXPECT_THROW(parser.Parse("m " + std::string(65534, 'x')), std::exception);
}
TEST(ConsoleCommand, ExpandsCoordinatesDirectlyAndReadsDefinitionsWhenCallingAliases)
{
ConsoleCommandParser parser;
parser.Parse("alias home goto $pos1");
parser.Parse("define pos1 1 1 1");
const auto direct = parser.Parse("goto $pos1");
ASSERT_TRUE(direct.action);
EXPECT_EQ(direct.action->kind, ActionKind::Goto);
EXPECT_EQ(direct.action->x, 1);
EXPECT_EQ(direct.action->y, 1);
EXPECT_EQ(direct.action->z, 1);
parser.Parse("define pos1 20 64 -8");
const auto home = parser.Parse("home");
ASSERT_TRUE(home.action);
EXPECT_EQ(home.action->x, 20);
EXPECT_EQ(home.action->y, 64);
EXPECT_EQ(home.action->z, -8);
EXPECT_EQ(parser.Parse("define pos1").argument, "define pos1 20 64 -8\n");
EXPECT_EQ(parser.Parse("define").argument, "define pos1 20 64 -8\n");
}
TEST(ConsoleCommand, SubstitutesOnlyExplicitWholeArgumentsOnceAndPreservesTextSpacing)
{
ConsoleCommandParser parser;
parser.Parse("define point 1 2 3");
parser.Parse("define literal $point");
const auto message = parser.Parse("sendmsg point prefix$point $point $literal $point");
ASSERT_TRUE(message.action);
EXPECT_EQ(message.action->text, "point prefix$point 1 2 3 $point 1 2 3");
EXPECT_THROW(parser.Parse("goto $missing"), std::exception);
EXPECT_THROW(parser.Parse("define 1point 5"), std::exception);
EXPECT_THROW(parser.Parse("alias define quit"), std::exception);
EXPECT_THROW(ConsoleCommandParser{}.Parse("goto $point"), std::exception);
parser.Parse("define big " + std::string(33000, 'x'));
EXPECT_THROW(parser.Parse("sendmsg $big $big"), std::exception);
}
#ifndef _WIN32
TEST(ConsoleCommand, NativePseudoTerminalWaitsForEnterAndParsesTheCommand)
{