init commit
This commit is contained in:
commit
1497f114e6
47 files changed
+8358
No files matched your search
@@ -0,0 +1,849 @@
|
||||
#include "ActionFile.h"
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
#include <limits>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace fs = std::filesystem;
|
||||
using Bilicraft::ActionKind;
|
||||
using Bilicraft::EntityFace;
|
||||
using Bilicraft::LoadActionFile;
|
||||
using Bilicraft::ParseInteractiveCommand;
|
||||
|
||||
void RejectInteractive(std::string_view text, std::string_view reason = {})
|
||||
{
|
||||
try
|
||||
{
|
||||
(void)ParseInteractiveCommand(text);
|
||||
FAIL() << "Expected console rejection for: " << text;
|
||||
}
|
||||
catch (const std::runtime_error &error)
|
||||
{
|
||||
const std::string message = error.what();
|
||||
EXPECT_TRUE(message.starts_with("<console>:1:")) << message;
|
||||
EXPECT_NE(message.find(reason), std::string::npos) << message;
|
||||
}
|
||||
}
|
||||
|
||||
class ActionFileTest : public ::testing::Test
|
||||
{
|
||||
protected:
|
||||
fs::path base;
|
||||
fs::path directory;
|
||||
|
||||
void SetUp() override
|
||||
{
|
||||
// CMake compiles this source with an absolute path. Standalone builds
|
||||
// should likewise pass its absolute path, or run from the source root.
|
||||
const auto source = fs::absolute(fs::path(__FILE__)).lexically_normal();
|
||||
ASSERT_TRUE(fs::is_regular_file(source));
|
||||
const auto build = source.parent_path().parent_path() / "build";
|
||||
fs::create_directories(build);
|
||||
base = fs::canonical(build);
|
||||
ASSERT_EQ(base.parent_path(), fs::canonical(source.parent_path().parent_path()));
|
||||
static std::atomic<unsigned long> sequence{0};
|
||||
const auto stamp = std::chrono::steady_clock::now().time_since_epoch().count();
|
||||
for (unsigned attempt = 0; attempt < 100; ++attempt)
|
||||
{
|
||||
const auto candidate =
|
||||
base / ("action-file-tests-" + std::to_string(stamp) + "-" + std::to_string(sequence++));
|
||||
if (fs::create_directory(candidate))
|
||||
{
|
||||
directory = fs::canonical(candidate);
|
||||
return;
|
||||
}
|
||||
}
|
||||
FAIL() << "Unable to allocate unique test directory under " << base;
|
||||
}
|
||||
|
||||
void TearDown() override
|
||||
{
|
||||
if (directory.empty())
|
||||
return;
|
||||
std::error_code ec;
|
||||
const auto resolved = fs::canonical(directory, ec);
|
||||
// Verify the exact absolute deletion target remains our own directory.
|
||||
ASSERT_FALSE(ec);
|
||||
ASSERT_EQ(resolved, directory);
|
||||
ASSERT_EQ(resolved.parent_path(), base);
|
||||
ASSERT_TRUE(resolved.filename().string().starts_with("action-file-tests-"));
|
||||
fs::remove_all(resolved, ec);
|
||||
EXPECT_FALSE(ec);
|
||||
}
|
||||
|
||||
fs::path Write(std::string_view filename, std::string_view contents)
|
||||
{
|
||||
const auto path = directory / std::string(filename);
|
||||
std::ofstream out(path, std::ios::binary);
|
||||
if (!out || !out.write(contents.data(), static_cast<std::streamsize>(contents.size())))
|
||||
throw std::runtime_error("Unable to write test file: " + path.string());
|
||||
return path;
|
||||
}
|
||||
|
||||
Bilicraft::ActionProgram Parse(std::string_view contents)
|
||||
{
|
||||
return LoadActionFile(Write("test.act", contents));
|
||||
}
|
||||
|
||||
void Reject(std::string_view contents, std::string_view reason = {}, std::size_t line = 2)
|
||||
{
|
||||
const auto path = Write("test.act", contents);
|
||||
try
|
||||
{
|
||||
(void)LoadActionFile(path);
|
||||
FAIL() << "Expected rejection for: " << contents;
|
||||
}
|
||||
catch (const std::runtime_error &error)
|
||||
{
|
||||
const std::string message = error.what();
|
||||
EXPECT_NE(message.find(path.string() + ":" + std::to_string(line) + ":"), std::string::npos) << message;
|
||||
EXPECT_NE(message.find(reason), std::string::npos) << message;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(ActionFileTest, ParsesEveryInstructionAndStoresSourceLocations)
|
||||
{
|
||||
const auto program = Parse("# action: farming\n"
|
||||
"goto -1 +2 3\nrclick 4 5 6 shift\nlclick 7 8 9 shift destory\n"
|
||||
"grclick 2 9\nglclick 4 1\nsw 9\nswb 3 8 2\n"
|
||||
"sendcmd /say hello world\nsendmsg hello everyone\nsendmsg @Farmer hello friend\n");
|
||||
EXPECT_EQ(program.name, "farming");
|
||||
ASSERT_EQ(program.commands.size(), 10U);
|
||||
const auto &c = program.commands;
|
||||
EXPECT_EQ(c[0].kind, ActionKind::Goto);
|
||||
EXPECT_EQ(c[0].x, -1);
|
||||
EXPECT_EQ(c[0].y, 2);
|
||||
EXPECT_EQ(c[0].z, 3);
|
||||
EXPECT_EQ(c[1].kind, ActionKind::RightClick);
|
||||
EXPECT_TRUE(c[1].shift);
|
||||
EXPECT_FALSE(c[1].destroy);
|
||||
EXPECT_EQ(c[2].kind, ActionKind::LeftClick);
|
||||
EXPECT_TRUE(c[2].shift);
|
||||
EXPECT_TRUE(c[2].destroy);
|
||||
EXPECT_EQ(c[3].kind, ActionKind::GuiRightClick);
|
||||
EXPECT_EQ(c[3].row, 2);
|
||||
EXPECT_EQ(c[3].col, 9);
|
||||
EXPECT_EQ(c[4].kind, ActionKind::GuiLeftClick);
|
||||
EXPECT_EQ(c[4].row, 4);
|
||||
EXPECT_EQ(c[4].col, 1);
|
||||
EXPECT_EQ(c[5].kind, ActionKind::SelectHotbar);
|
||||
EXPECT_EQ(c[5].slot, 9);
|
||||
EXPECT_EQ(c[6].kind, ActionKind::SwapBackpack);
|
||||
EXPECT_EQ(c[6].row, 3);
|
||||
EXPECT_EQ(c[6].col, 8);
|
||||
EXPECT_EQ(c[6].slot, 2);
|
||||
EXPECT_EQ(c[7].kind, ActionKind::SendCommand);
|
||||
EXPECT_EQ(c[7].text, "/say hello world");
|
||||
EXPECT_EQ(c[8].kind, ActionKind::SendMessage);
|
||||
EXPECT_EQ(c[8].text, "hello everyone");
|
||||
EXPECT_TRUE(c[8].recipient.empty());
|
||||
EXPECT_EQ(c[9].text, "hello friend");
|
||||
EXPECT_EQ(c[9].recipient, "Farmer");
|
||||
for (std::size_t i = 0; i < c.size(); ++i)
|
||||
{
|
||||
EXPECT_FALSE(c[i].entity);
|
||||
EXPECT_EQ(c[i].entityFace, EntityFace::Any);
|
||||
EXPECT_EQ(c[i].source, directory / "test.act");
|
||||
EXPECT_EQ(c[i].line, i + 2);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, ParsesDropCurrentOrExplicitSlotAndOptionalAll)
|
||||
{
|
||||
const auto program = Parse("# action: dropping\ndrop\ndrop 1\ndrop 9\ndrop all\n"
|
||||
"drop 3 all # entire stack\ndrop all 5\n");
|
||||
ASSERT_EQ(program.commands.size(), 6U);
|
||||
const int slots[] = {0, 1, 9, 0, 3, 5};
|
||||
for (std::size_t i = 0; i < program.commands.size(); ++i)
|
||||
{
|
||||
EXPECT_EQ(program.commands[i].kind, ActionKind::Drop);
|
||||
EXPECT_EQ(program.commands[i].slot, slots[i]);
|
||||
EXPECT_EQ(program.commands[i].all, i >= 3);
|
||||
EXPECT_EQ(program.commands[i].line, i + 2);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, RejectsInvalidDropSlotsFlagsAndExtraArguments)
|
||||
{
|
||||
for (const auto command :
|
||||
{"drop 0", "drop -1", "drop 10", "drop 2147483648", "drop 1.5", "drop ALL", "drop shift", "drop ?all",
|
||||
"drop all all", "drop 1 2", "drop 1 all all", "drop all 0", "drop all 1 2", "drop 1 unknown"})
|
||||
{
|
||||
SCOPED_TRACE(command);
|
||||
Reject("# action: test\n" + std::string(command));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, DropKeepsCurrentSlotUnresolvedAcrossImports)
|
||||
{
|
||||
Write("child.act", "# action: child\ndrop all\n");
|
||||
const auto program = Parse("# action: parent\nsw 9\nimport child\ndrop 2\n");
|
||||
ASSERT_EQ(program.commands.size(), 3U);
|
||||
EXPECT_EQ(program.commands[1].kind, ActionKind::Drop);
|
||||
EXPECT_EQ(program.commands[1].slot, 0); // Resolve at execution, after sw 9.
|
||||
EXPECT_TRUE(program.commands[1].all);
|
||||
EXPECT_EQ(program.commands[1].source, directory / "child.act");
|
||||
EXPECT_EQ(program.commands[2].slot, 2);
|
||||
EXPECT_FALSE(program.commands[2].all);
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, HandlesBomCrLfWhitespaceAndInlineComments)
|
||||
{
|
||||
const auto program = Parse("\xEF\xBB\xBF # ordinary comment\r\n\r\n # action: example # note\r\n"
|
||||
"\t goto\t1 2 3 # comment\r\n sendmsg hello # removed\r\n# trailing\r\n");
|
||||
EXPECT_EQ(program.name, "example");
|
||||
ASSERT_EQ(program.commands.size(), 2U);
|
||||
EXPECT_EQ(program.commands[0].line, 4U);
|
||||
EXPECT_EQ(program.commands[1].text, "hello");
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, AllowsHeaderOnlyAndNoFinalNewline)
|
||||
{
|
||||
EXPECT_TRUE(Parse("# action: empty").commands.empty());
|
||||
EXPECT_EQ(Parse("# action: test\nsw 1").commands.size(), 1U);
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, SupportsDestroyAliasAndEitherFlagOrder)
|
||||
{
|
||||
const auto program = Parse("# action: test\nlclick 1 2 3 destroy shift\nlclick 1 2 3 destory\n"
|
||||
"lclick 1 2 3 shift\nlclick 1 2 3\nrclick 1 2 3\n");
|
||||
ASSERT_EQ(program.commands.size(), 5U);
|
||||
EXPECT_TRUE(program.commands[0].shift);
|
||||
EXPECT_TRUE(program.commands[0].destroy);
|
||||
EXPECT_TRUE(program.commands[1].destroy);
|
||||
EXPECT_FALSE(program.commands[1].shift);
|
||||
EXPECT_TRUE(program.commands[2].shift);
|
||||
EXPECT_FALSE(program.commands[2].destroy);
|
||||
EXPECT_FALSE(program.commands[3].shift);
|
||||
EXPECT_FALSE(program.commands[3].destroy);
|
||||
EXPECT_FALSE(program.commands[4].shift);
|
||||
for (const auto &command : program.commands)
|
||||
{
|
||||
EXPECT_FALSE(command.entity);
|
||||
EXPECT_EQ(command.entityFace, EntityFace::Any);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, ParsesAllEntryFacesWithShortAndFullNamesInFilesAndConsole)
|
||||
{
|
||||
const struct
|
||||
{
|
||||
const char *shortName;
|
||||
const char *fullName;
|
||||
EntityFace face;
|
||||
} faces[] = {{"t", "top", EntityFace::Top}, {"b", "bottom", EntityFace::Bottom},
|
||||
{"s", "south", EntityFace::South}, {"n", "north", EntityFace::North},
|
||||
{"w", "west", EntityFace::West}, {"e", "east", EntityFace::East}};
|
||||
for (const auto verb : {"rclick", "lclick"})
|
||||
{
|
||||
for (const auto &face : faces)
|
||||
{
|
||||
for (const auto token : {face.shortName, face.fullName})
|
||||
{
|
||||
const auto text = std::string(verb) + " -1 +2 3 entry " + token + " # frame";
|
||||
SCOPED_TRACE(text);
|
||||
const auto program = Parse("# action: test\n" + text);
|
||||
ASSERT_EQ(program.commands.size(), 1U);
|
||||
const auto interactive = ParseInteractiveCommand(text);
|
||||
ASSERT_TRUE(interactive.has_value());
|
||||
const auto &file = program.commands[0];
|
||||
for (const auto *command : {&file, &*interactive})
|
||||
{
|
||||
EXPECT_EQ(command->kind,
|
||||
std::string_view(verb) == "rclick" ? ActionKind::RightClick : ActionKind::LeftClick);
|
||||
EXPECT_EQ(command->x, -1);
|
||||
EXPECT_EQ(command->y, 2);
|
||||
EXPECT_EQ(command->z, 3);
|
||||
EXPECT_TRUE(command->entity);
|
||||
EXPECT_EQ(command->entityFace, face.face);
|
||||
EXPECT_FALSE(command->shift);
|
||||
EXPECT_FALSE(command->destroy);
|
||||
}
|
||||
EXPECT_EQ(file.source, directory / "test.act");
|
||||
EXPECT_EQ(file.line, 2U);
|
||||
EXPECT_EQ(interactive->source, fs::path("<console>"));
|
||||
EXPECT_EQ(interactive->line, 1U);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, EntryAllowsOmittedFaceAndEveryLegacyFlagOrder)
|
||||
{
|
||||
// Treat entry [face] as one flag group: it may precede, follow or sit
|
||||
// between the legacy flags, but the face itself cannot move independently.
|
||||
for (const auto entry : {"entry", "entry n", "entry north"})
|
||||
{
|
||||
for (const auto verb : {"rclick", "lclick"})
|
||||
{
|
||||
for (const auto suffix : {std::string(entry), std::string(entry) + " shift", "shift " + std::string(entry)})
|
||||
{
|
||||
const auto text = std::string(verb) + " 1 2 3 " + suffix;
|
||||
SCOPED_TRACE(text);
|
||||
const auto program = Parse("# action: test\n" + text);
|
||||
ASSERT_EQ(program.commands.size(), 1U);
|
||||
const auto interactive = ParseInteractiveCommand(text);
|
||||
ASSERT_TRUE(interactive.has_value());
|
||||
for (const auto *command : {&program.commands[0], &*interactive})
|
||||
{
|
||||
EXPECT_TRUE(command->entity);
|
||||
EXPECT_EQ(command->entityFace,
|
||||
std::string_view(entry) == "entry" ? EntityFace::Any : EntityFace::North);
|
||||
EXPECT_EQ(command->shift, suffix.find("shift") != std::string::npos);
|
||||
EXPECT_FALSE(command->destroy);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const auto destroy : {"destory", "destroy"})
|
||||
{
|
||||
const std::string e = entry, d = destroy;
|
||||
for (const auto &suffix :
|
||||
{e + " " + d, d + " " + e, e + " shift " + d, e + " " + d + " shift", "shift " + e + " " + d,
|
||||
"shift " + d + " " + e, d + " " + e + " shift", d + " shift " + e})
|
||||
{
|
||||
const auto text = "lclick 1 2 3 " + suffix;
|
||||
SCOPED_TRACE(text);
|
||||
const auto program = Parse("# action: test\n" + text);
|
||||
ASSERT_EQ(program.commands.size(), 1U);
|
||||
const auto interactive = ParseInteractiveCommand(text);
|
||||
ASSERT_TRUE(interactive.has_value());
|
||||
for (const auto *command : {&program.commands[0], &*interactive})
|
||||
{
|
||||
EXPECT_TRUE(command->entity);
|
||||
EXPECT_EQ(command->entityFace, e == "entry" ? EntityFace::Any : EntityFace::North);
|
||||
EXPECT_EQ(command->shift, suffix.find("shift") != std::string::npos);
|
||||
EXPECT_TRUE(command->destroy);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, RejectsInvalidEntryFacesFlagsAndOrderingInFilesAndConsole)
|
||||
{
|
||||
for (const auto text : {"rclick 1 2 3 entry entry",
|
||||
"lclick 1 2 3 entry n entry",
|
||||
"lclick 1 2 3 entry entry n",
|
||||
"rclick 1 2 3 entry n n",
|
||||
"lclick 1 2 3 entry n north",
|
||||
"lclick 1 2 3 entry n s",
|
||||
"rclick 1 2 3 n entry",
|
||||
"lclick 1 2 3 north entry",
|
||||
"rclick 1 2 3 entry shift n",
|
||||
"lclick 1 2 3 entry destroy n",
|
||||
"lclick 1 2 3 entry destory north",
|
||||
"lclick 1 2 3 entry n shift e",
|
||||
"rclick 1 2 3 n",
|
||||
"lclick 1 2 3 top",
|
||||
"rclick 1 2 3 shift n",
|
||||
"rclick 1 2 3 entry any",
|
||||
"lclick 1 2 3 entry unknown",
|
||||
"rclick 1 2 3 ENTRY",
|
||||
"rclick 1 2 3 entry N",
|
||||
"lclick 1 2 3 entry North",
|
||||
"rclick 1 2 3 entry shift shift",
|
||||
"lclick 1 2 3 entry n shift shift",
|
||||
"lclick 1 2 3 entry destroy destroy",
|
||||
"lclick 1 2 3 entry destory destroy",
|
||||
"lclick 1 2 3 entry destroy destory",
|
||||
"lclick 1 2 3 entry destory destory",
|
||||
"rclick 1 2 3 entry destroy",
|
||||
"rclick 1 2 3 entry n destory",
|
||||
"rclick 1 2 3 destroy entry",
|
||||
"rclick 1 2 3 destory entry n",
|
||||
"goto 1 2 3 entry",
|
||||
"goto 1 2 3 entry n",
|
||||
"goto 1 2 3 n",
|
||||
"grclick 1 2 entry",
|
||||
"glclick 1 2 entry",
|
||||
"sw 1 entry",
|
||||
"swb 1 2 3 entry",
|
||||
"drop entry",
|
||||
"rclick entry 1 2 3",
|
||||
"lclick 1 2 entry",
|
||||
"lclick 1 2 3 entry n shift destroy extra"})
|
||||
{
|
||||
SCOPED_TRACE(text);
|
||||
Reject("# action: test\n" + std::string(text));
|
||||
RejectInteractive(text);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, AcceptsIntegerAndSlotBoundaries)
|
||||
{
|
||||
const auto program = Parse("# action: bounds\ngoto -2147483648 2147483647 -0\n"
|
||||
"grclick 2147483647 9\nglclick 1 1\nsw 1\nsw 9\nswb 1 1 1\nswb 3 9 9\n");
|
||||
ASSERT_EQ(program.commands.size(), 7U);
|
||||
EXPECT_EQ(program.commands[0].x, std::numeric_limits<int>::min());
|
||||
EXPECT_EQ(program.commands[0].y, std::numeric_limits<int>::max());
|
||||
EXPECT_EQ(program.commands[1].row, std::numeric_limits<int>::max());
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, PreservesUtf8NamesAndMessageText)
|
||||
{
|
||||
const auto program = Parse("# action: 种田\nsendmsg @玩家 你好 世界\n");
|
||||
EXPECT_EQ(program.name, "种田");
|
||||
ASSERT_EQ(program.commands.size(), 1U);
|
||||
EXPECT_EQ(program.commands[0].recipient, "玩家");
|
||||
EXPECT_EQ(program.commands[0].text, "你好 世界");
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, RejectsMissingHeader)
|
||||
{
|
||||
Reject("", "missing action header", 1);
|
||||
Reject("# ordinary comment", "missing action header", 1);
|
||||
Reject("goto 1 2 3\n# action: late\n", "before first instruction", 1);
|
||||
Reject("import child\n# action: late\n", "before first instruction", 1);
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, RejectsDuplicateHeaders)
|
||||
{
|
||||
Reject("# action: test\n# action: test\n", "duplicate");
|
||||
Reject("# action: test\nsw 1\n# action: other\n", "duplicate", 3);
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, RejectsEmptyAndUnsafeHeaderNames)
|
||||
{
|
||||
for (const auto name :
|
||||
{"", " ", ".", "..", "../evil", "dir/name", "dir\\name", "C:evil", "a b", "a.", "a*", "name.act"})
|
||||
{
|
||||
SCOPED_TRACE(name);
|
||||
Reject("# action: " + std::string(name), "simple filename", 1);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, RejectsInvalidIntegers)
|
||||
{
|
||||
for (const auto value : {"1.0", "1x", "0x10", "2147483648", "-2147483649", "+", "-", "+-1", "--1", "++1", "1e2"})
|
||||
{
|
||||
SCOPED_TRACE(value);
|
||||
Reject("# action: test\ngoto " + std::string(value) + " 2 3\n", "integer");
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, RejectsIncorrectArgumentCounts)
|
||||
{
|
||||
for (const auto command : {"goto", "goto 1 2", "goto 1 2 3 4", "rclick 1 2", "rclick 1 2 3 shift shift",
|
||||
"lclick 1 2", "lclick 1 2 3 shift destory extra", "grclick 1", "grclick 1 2 3",
|
||||
"glclick 1", "glclick 1 2 3", "sw", "sw 1 2", "swb 1 2", "swb 1 2 3 4"})
|
||||
{
|
||||
SCOPED_TRACE(command);
|
||||
Reject("# action: test\n" + std::string(command));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, RejectsUnknownAndDuplicateFlags)
|
||||
{
|
||||
for (const auto command : {"rclick 1 2 3 destory", "rclick 1 2 3 SHIFT", "rclick 1 2 3 unknown",
|
||||
"lclick 1 2 3 shift shift", "lclick 1 2 3 destroy destory",
|
||||
"lclick 1 2 3 destory destory", "lclick 1 2 3 nope", "lclick 1 2 3 destory nope"})
|
||||
{
|
||||
SCOPED_TRACE(command);
|
||||
Reject("# action: test\n" + std::string(command), "unknown or duplicate flag");
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, RejectsInvalidGuiBackpackAndHotbarRanges)
|
||||
{
|
||||
for (const auto command :
|
||||
{"grclick 0 1", "grclick -1 1", "grclick 1 0", "grclick 1 10", "glclick 0 1", "glclick 1 10", "sw 0", "sw 10",
|
||||
"sw -1", "swb 0 1 1", "swb 4 1 1", "swb 1 0 1", "swb 1 10 1", "swb 1 1 0", "swb 1 1 10"})
|
||||
{
|
||||
SCOPED_TRACE(command);
|
||||
Reject("# action: test\n" + std::string(command));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, RejectsEmptyTextAndMissingRecipientMessages)
|
||||
{
|
||||
for (const auto command : {"sendcmd", "sendcmd # comment", "sendcmd /", "sendcmd / ", "sendcmd / # comment",
|
||||
"sendmsg", "sendmsg @Player", "sendmsg @Player # comment", "sendmsg @ hello"})
|
||||
{
|
||||
SCOPED_TRACE(command);
|
||||
Reject("# action: test\n" + std::string(command));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, RejectsUnknownInstructionsAndEmbeddedNul)
|
||||
{
|
||||
Reject("# action: test\nunknown 1 2 3", "unknown instruction");
|
||||
const std::string input = std::string("# action: test\nsendmsg hi") + '\0' + "hidden";
|
||||
Reject(input, "NUL");
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, ExpandsNestedAndRepeatedImportsInExactOrder)
|
||||
{
|
||||
Write("leaf.act", "# action: leaf\nsendmsg leaf\n");
|
||||
Write("child.act", "# action: child\nsendmsg child-before\nimport leaf\nsendmsg child-after\n");
|
||||
const auto program =
|
||||
Parse("# action: root\nsendmsg before\nimport child\nsendmsg middle\nimport child\nsendmsg after\n");
|
||||
EXPECT_EQ(program.name, "root");
|
||||
ASSERT_EQ(program.commands.size(), 9U);
|
||||
const std::string expected[] = {"before", "child-before", "leaf", "child-after", "middle",
|
||||
"child-before", "leaf", "child-after", "after"};
|
||||
for (std::size_t i = 0; i < 9; ++i)
|
||||
EXPECT_EQ(program.commands[i].text, expected[i]);
|
||||
EXPECT_EQ(program.commands[2].source, directory / "leaf.act");
|
||||
EXPECT_EQ(program.commands[2].line, 2U);
|
||||
EXPECT_EQ(program.commands[3].source, directory / "child.act");
|
||||
EXPECT_EQ(program.commands[3].line, 4U);
|
||||
EXPECT_EQ(program.commands[8].source, directory / "test.act");
|
||||
EXPECT_EQ(program.commands[8].line, 6U);
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, AllowsEmptyImports)
|
||||
{
|
||||
Write("empty.act", "# action: empty\n");
|
||||
EXPECT_TRUE(Parse("# action: root\nimport empty\nimport empty\n").commands.empty());
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, RejectsMissingImportWithImportSite)
|
||||
{
|
||||
Reject("# action: root\nimport missing\n", "cannot import 'missing'");
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, RejectsImportHeaderMismatchWithBothLocations)
|
||||
{
|
||||
const auto child = Write("child.act", "# action: different\n");
|
||||
Reject("# action: root\nimport child\n", child.string() + ":1:");
|
||||
Reject("# action: root\nimport child\n", "does not match imported name");
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, ReportsImportedSyntaxErrorWithBothLocations)
|
||||
{
|
||||
const auto child = Write("child.act", "# action: child\nsw 99\n");
|
||||
Reject("# action: root\nimport child\n", child.string() + ":2:");
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, RejectsDirectAndIndirectCycles)
|
||||
{
|
||||
Reject("# action: test\nimport test\n", "cyclic import");
|
||||
Write("child.act", "# action: child\nimport test\n");
|
||||
Reject("# action: test\nimport child\n", "cyclic import");
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, RejectsUnsafeImportPathsAndArity)
|
||||
{
|
||||
for (const auto name : {"", "../child", "./child", "/child", "folder/child", "folder\\child", "C:child", ".", "..",
|
||||
"child extra", "child.act"})
|
||||
{
|
||||
SCOPED_TRACE(name);
|
||||
Reject("# action: test\nimport " + std::string(name), "one simple action name");
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, RejectsSymlinkImportEscapingDirectory)
|
||||
{
|
||||
fs::create_directory(directory / "outside");
|
||||
const auto outside = Write("outside/child.act", "# action: child\nsw 1\n");
|
||||
std::error_code ec;
|
||||
fs::create_symlink(outside, directory / "child.act", ec);
|
||||
if (ec)
|
||||
GTEST_SKIP() << "Symlinks unavailable: " << ec.message();
|
||||
Reject("# action: test\nimport child\n", "escapes");
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, RejectsMissingAndNonregularRootFiles)
|
||||
{
|
||||
EXPECT_THROW(LoadActionFile(directory / "missing.act"), std::runtime_error);
|
||||
fs::create_directory(directory / "folder.act");
|
||||
EXPECT_THROW(LoadActionFile(directory / "folder.act"), std::runtime_error);
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, RejectsEntryFilesWithoutActExtension)
|
||||
{
|
||||
for (const auto name : {"plain", "test.txt", "test.ACT", "test.act.bak"})
|
||||
{
|
||||
const auto path = Write(name, "# action: test\n");
|
||||
try
|
||||
{
|
||||
(void)LoadActionFile(path);
|
||||
FAIL() << "Accepted unsupported extension: " << name;
|
||||
}
|
||||
catch (const std::runtime_error &error)
|
||||
{
|
||||
const std::string message = error.what();
|
||||
EXPECT_NE(message.find(path.string() + ":1:"), std::string::npos);
|
||||
EXPECT_NE(message.find(".act extension"), std::string::npos);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, PreservesCommandsWithOrWithoutLeadingSlash)
|
||||
{
|
||||
const auto program = Parse("# action: test\nsendcmd /say hi there\nsendcmd say hi there\n");
|
||||
ASSERT_EQ(program.commands.size(), 2U);
|
||||
EXPECT_EQ(program.commands[0].text, "/say hi there");
|
||||
EXPECT_EQ(program.commands[1].text, "say hi there");
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, ParsesWaitDefaultUnitEveryUnitAndZero)
|
||||
{
|
||||
const auto program = Parse("# action: waits\nwait 2\nwait 2 s\nwait 2 ms\nwait 2 min\nwait 2 h\nwait 2 d\n"
|
||||
"wait 0\nwait 0 s\nwait 0 ms\nwait 0 min\nwait 0 h\nwait 0 d\n");
|
||||
const std::chrono::milliseconds::rep expected[] = {2000, 2000, 2, 120000, 7200000, 172800000, 0, 0, 0, 0, 0, 0};
|
||||
ASSERT_EQ(program.commands.size(), std::size(expected));
|
||||
for (std::size_t i = 0; i < program.commands.size(); ++i)
|
||||
{
|
||||
EXPECT_EQ(program.commands[i].kind, ActionKind::Wait);
|
||||
EXPECT_EQ(program.commands[i].waitDuration.count(), expected[i]);
|
||||
EXPECT_EQ(program.commands[i].source, directory / "test.act");
|
||||
EXPECT_EQ(program.commands[i].line, i + 2);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, ParsesExactWaitFractionsWithoutRounding)
|
||||
{
|
||||
const auto program = Parse("# action: waits\nwait 0.001\nwait 1.125 s\nwait 1.000 ms\nwait 0.00005 min\n"
|
||||
"wait 0.000005 h\nwait 0.0000003125 d\nwait 0002.500000 s\n"
|
||||
"wait 0.0000000000000000000000000 ms\nwait 0.1230000000000000000000000 s\n");
|
||||
const std::chrono::milliseconds::rep expected[] = {1, 1125, 1, 3, 18, 27, 2500, 0, 123};
|
||||
ASSERT_EQ(program.commands.size(), std::size(expected));
|
||||
for (std::size_t i = 0; i < program.commands.size(); ++i)
|
||||
EXPECT_EQ(program.commands[i].waitDuration.count(), expected[i]);
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, AcceptsWaitLimitInEveryUnitAndRejectsOverflow)
|
||||
{
|
||||
for (const auto duration : {"31536000", "31536000 s", "31536000000 ms", "525600 min", "8760 h", "365 d",
|
||||
"365.000000000000000000000000000000 d"})
|
||||
{
|
||||
SCOPED_TRACE(duration);
|
||||
const auto program = Parse("# action: waits\nwait " + std::string(duration));
|
||||
ASSERT_EQ(program.commands.size(), 1U);
|
||||
EXPECT_EQ(program.commands[0].waitDuration.count(), 31536000000LL);
|
||||
}
|
||||
for (const auto duration :
|
||||
{"31536000.001", "31536000.001 s", "31536000001 ms", "525600.00005 min", "8760.000005 h", "365.0000003125 d",
|
||||
"366 d", "999999999999999999999999999999999999 s", "18446744073709551616 ms", "9223372036854775808 d"})
|
||||
{
|
||||
SCOPED_TRACE(duration);
|
||||
Reject("# action: waits\nwait " + std::string(duration), "365 days");
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, RejectsSubMillisecondWaitFractions)
|
||||
{
|
||||
for (const auto duration : {"0.0001", "0.0001 s", "0.5 ms", "0.00001 min", "0.0000001 h", "0.000000001 d",
|
||||
"0.000000000000000000000000000000000001 s", "1.0000000000000000000000001 ms"})
|
||||
{
|
||||
SCOPED_TRACE(duration);
|
||||
Reject("# action: waits\nwait " + std::string(duration), "whole number of milliseconds");
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, RejectsInvalidWaitNumbersUnitsAndArgumentCounts)
|
||||
{
|
||||
for (const auto command :
|
||||
{"wait", "wait s", "wait -1", "wait -0", "wait +1", "wait NaN", "wait nan",
|
||||
"wait Inf", "wait inf", "wait infinity", "wait 1e3", "wait 1E3", "wait 0x10", "wait 1,5",
|
||||
"wait .", "wait .5", "wait 1.", "wait 1.2.3", "wait 1s", "wait 1 S", "wait 1 m",
|
||||
"wait 1 sec", "wait 1 seconds", "wait 1 unknown", "wait 1 ms extra", "wait 1 2 3", "wait 1 s ms"})
|
||||
{
|
||||
SCOPED_TRACE(command);
|
||||
Reject("# action: waits\n" + std::string(command));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, KeepsWaitImportOrderingLocationsAndErrorContext)
|
||||
{
|
||||
const auto child = Write("child.act", "# action: child\nwait 0.125 s\n");
|
||||
const auto program = Parse("# action: parent\nwait 0\nimport child\nsendmsg still here\nwait 2 ms # pause\n");
|
||||
ASSERT_EQ(program.commands.size(), 4U);
|
||||
EXPECT_EQ(program.commands[0].waitDuration.count(), 0);
|
||||
EXPECT_EQ(program.commands[1].kind, ActionKind::Wait);
|
||||
EXPECT_EQ(program.commands[1].waitDuration.count(), 125);
|
||||
EXPECT_EQ(program.commands[1].source, child);
|
||||
EXPECT_EQ(program.commands[1].line, 2U);
|
||||
EXPECT_EQ(program.commands[2].text, "still here");
|
||||
EXPECT_EQ(program.commands[3].waitDuration.count(), 2);
|
||||
Write("child.act", "# action: child\nwait 366 d\n");
|
||||
Reject("# action: parent\nimport child\n", child.string() + ":2:");
|
||||
}
|
||||
|
||||
TEST(InteractiveCommandTest, BlankAndCommentLinesHaveNoCommand)
|
||||
{
|
||||
for (const auto text : {"", " ", "\t", "# comment", " \t# action: not-a-file", "# wait 1", "# import child"})
|
||||
EXPECT_FALSE(ParseInteractiveCommand(text).has_value()) << text;
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, InteractiveCommandsReuseEveryGameInstructionAndFlag)
|
||||
{
|
||||
for (const auto text : {"goto -1 +2 3",
|
||||
"rclick 1 2 3",
|
||||
"rclick 1 2 3 shift",
|
||||
"lclick 1 2 3",
|
||||
"lclick 1 2 3 destroy shift",
|
||||
"lclick 1 2 3 shift destory",
|
||||
"lclick 1 2 3 destory shift",
|
||||
"lclick 1 2 3 shift destroy",
|
||||
"rclick 1 2 3 entry",
|
||||
"rclick 1 2 3 entry t shift",
|
||||
"rclick 1 2 3 shift entry bottom",
|
||||
"lclick 1 2 3 entry",
|
||||
"lclick 1 2 3 entry south",
|
||||
"lclick 1 2 3 entry n shift destroy",
|
||||
"lclick 1 2 3 shift entry w destory",
|
||||
"lclick 1 2 3 destory shift entry east",
|
||||
"lclick 1 2 3 destroy entry shift",
|
||||
"grclick 2 9",
|
||||
"glclick 4 1",
|
||||
"sw 9",
|
||||
"swb 3 8 2",
|
||||
"drop",
|
||||
"drop all",
|
||||
"drop 3",
|
||||
"drop 3 all",
|
||||
"drop all 3",
|
||||
"sendcmd /say hello world",
|
||||
"sendcmd say hi",
|
||||
"sendmsg hello everyone",
|
||||
"sendmsg @Farmer hello friend",
|
||||
"sendmsg @玩家 你好 世界",
|
||||
"\t sw\t2 # inline comment"})
|
||||
{
|
||||
SCOPED_TRACE(text);
|
||||
const auto interactive = ParseInteractiveCommand(text);
|
||||
ASSERT_TRUE(interactive.has_value());
|
||||
const auto program = Parse("# action: test\n" + std::string(text));
|
||||
ASSERT_EQ(program.commands.size(), 1U);
|
||||
const auto &file = program.commands[0];
|
||||
EXPECT_EQ(interactive->kind, file.kind);
|
||||
EXPECT_EQ(interactive->x, file.x);
|
||||
EXPECT_EQ(interactive->y, file.y);
|
||||
EXPECT_EQ(interactive->z, file.z);
|
||||
EXPECT_EQ(interactive->row, file.row);
|
||||
EXPECT_EQ(interactive->col, file.col);
|
||||
EXPECT_EQ(interactive->slot, file.slot);
|
||||
EXPECT_EQ(interactive->shift, file.shift);
|
||||
EXPECT_EQ(interactive->destroy, file.destroy);
|
||||
EXPECT_EQ(interactive->entity, file.entity);
|
||||
EXPECT_EQ(interactive->entityFace, file.entityFace);
|
||||
EXPECT_EQ(interactive->all, file.all);
|
||||
EXPECT_EQ(interactive->text, file.text);
|
||||
EXPECT_EQ(interactive->recipient, file.recipient);
|
||||
EXPECT_EQ(interactive->waitDuration, std::chrono::milliseconds{0});
|
||||
EXPECT_EQ(interactive->waitDuration, file.waitDuration);
|
||||
EXPECT_EQ(interactive->source, fs::path("<console>"));
|
||||
EXPECT_EQ(interactive->line, 1U);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(InteractiveCommandTest, PreservesMessageTextAndInlineCommentRules)
|
||||
{
|
||||
auto command = ParseInteractiveCommand(" sendmsg @玩家 你好 世界 # hidden");
|
||||
ASSERT_TRUE(command.has_value());
|
||||
EXPECT_EQ(command->recipient, "玩家");
|
||||
EXPECT_EQ(command->text, "你好 世界");
|
||||
command = ParseInteractiveCommand("sendmsg run child.act; wait 1");
|
||||
ASSERT_TRUE(command.has_value());
|
||||
EXPECT_EQ(command->kind, ActionKind::SendMessage);
|
||||
EXPECT_EQ(command->text, "run child.act; wait 1");
|
||||
}
|
||||
|
||||
TEST(InteractiveCommandTest, RejectsWaitAndImportWithRunGuidance)
|
||||
{
|
||||
for (const auto text : {"wait 0", "wait 1 s", "wait", "wait nonsense", "import child", "import ../child", "import"})
|
||||
{
|
||||
SCOPED_TRACE(text);
|
||||
RejectInteractive(text, "run file.act");
|
||||
}
|
||||
}
|
||||
|
||||
TEST(InteractiveCommandTest, RejectsInvalidGameCommandsWithConsoleLocation)
|
||||
{
|
||||
for (const auto text :
|
||||
{"unknown", "goto", "goto 1 2 3 4", "goto 2147483648 1 2", "rclick 1 2 3 destroy", "lclick 1 2 3 shift shift",
|
||||
"grclick 0 1", "glclick 1 10", "sw 0", "swb 4 1 2", "drop all all", "drop 0", "sendcmd", "sendcmd /",
|
||||
"sendmsg", "sendmsg @Player", "sendmsg @ hello", "run file.act", "sw 1; sw 2"})
|
||||
{
|
||||
SCOPED_TRACE(text);
|
||||
RejectInteractive(text);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(InteractiveCommandTest, RejectsNewlineAndNulEvenInsideComments)
|
||||
{
|
||||
for (const auto text : {"\n", "\r", "sw 1\n", "sw 1\r\n", "sw 1\nsw 2", "sendmsg hi\rsw 2", "# comment\nsw 2",
|
||||
"# comment\r", "sendcmd /say hello\n/stop"})
|
||||
{
|
||||
SCOPED_TRACE(text);
|
||||
RejectInteractive(text, "one line");
|
||||
}
|
||||
RejectInteractive(std::string("sendmsg hi") + '\0' + "hidden", "NUL");
|
||||
RejectInteractive(std::string("# comment") + '\0', "NUL");
|
||||
RejectInteractive(std::string(1, '\0'), "NUL");
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, EnforcesDepthLimitAndAcceptsBoundary)
|
||||
{
|
||||
for (int i = 0; i < 32; ++i)
|
||||
{
|
||||
std::string body = "# action: depth" + std::to_string(i) + "\n";
|
||||
if (i != 31)
|
||||
body += "import depth" + std::to_string(i + 1) + "\n";
|
||||
Write("depth" + std::to_string(i) + ".act", body);
|
||||
}
|
||||
EXPECT_TRUE(LoadActionFile(directory / "depth0.act").commands.empty());
|
||||
Reject("# action: root\nimport depth0\n", "depth limit");
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, EnforcesExpandedCommandLimitAndAcceptsBoundary)
|
||||
{
|
||||
std::string contents = "# action: test\n";
|
||||
for (int i = 0; i < 100000; ++i)
|
||||
contents += "sw 1\n";
|
||||
EXPECT_EQ(Parse(contents).commands.size(), 100000U);
|
||||
contents += "sw 1\n";
|
||||
Reject(contents, "expanded command limit", 100002);
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, EnforcesExpandedCommandLimitAcrossImports)
|
||||
{
|
||||
std::string child = "# action: child\n";
|
||||
for (int i = 0; i < 50001; ++i)
|
||||
child += "sw 1\n";
|
||||
Write("child.act", child);
|
||||
Reject("# action: root\nimport child\nimport child\n", "expanded command limit", 3);
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, EnforcesIndividualFileSizeLimit)
|
||||
{
|
||||
std::string contents = "# action: test\n#";
|
||||
contents.resize(1024 * 1024, 'x');
|
||||
EXPECT_TRUE(Parse(contents).commands.empty());
|
||||
contents += 'x';
|
||||
Reject(contents, "file size limit", 1);
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, EnforcesTotalInputSizeLimit)
|
||||
{
|
||||
std::string child = "# action: child\n#";
|
||||
child.resize(1024 * 1024, 'x');
|
||||
Write("child.act", child);
|
||||
std::string contents = "# action: root\n";
|
||||
for (int i = 0; i < 16; ++i)
|
||||
contents += "import child\n";
|
||||
Reject(contents, "total action input limit", 17);
|
||||
}
|
||||
|
||||
TEST_F(ActionFileTest, EnforcesLoadLimitEvenForEmptyImports)
|
||||
{
|
||||
Write("empty.act", "# action: empty\n");
|
||||
std::string contents = "# action: root\n";
|
||||
for (int i = 0; i < 4096; ++i)
|
||||
contents += "import empty\n";
|
||||
Reject(contents, "file load limit", 4097);
|
||||
}
|
||||
} // namespace
|
||||
Reference in new issue
Block a user