Add action file loops and display the GPLv3 notice at console startup

This commit is contained in:
cantyonion committed 2026-11-24 16:44:34 +08:00
1 parent 12c7332337
commit b8799a62e7
9 files changed
+305 -47

No files matched your search

+76
View File
@@ -1,4 +1,5 @@
#include "ActionFile.h"
#include "ActionScheduler.h"
#include "ConsoleCommand.h"
#include <atomic>
@@ -113,6 +114,81 @@ class ActionFileTest : public ::testing::Test
}
};
TEST_F(ActionFileTest, ExecutesNestedLoopsAndSingleStatementImportsInOrder)
{
Write("collect.act", "# action: collect\nsw 2\nsw 3\n");
auto program = Parse("# action: test\ndefine n 2\nalias pick sw\ndo $n begin\n"
" do 0 sw 9\n pick 1\n do 2\n # count the import as one statement\n"
" import collect\nend\ndo 2 do 2 pick 4\nsw 5\n");
Bilicraft::ActionScheduler queue;
queue.Enqueue(std::move(program));
for (int expected : {1,2,3,2,3,1,2,3,2,3,4,4,4,4,5})
{
const auto work = queue.Next({});
ASSERT_TRUE(work);
EXPECT_EQ(work->command.slot, expected);
queue.Finish(true);
}
EXPECT_FALSE(queue.Next({}));
EXPECT_FALSE(queue.HasWork());
}
TEST_F(ActionFileTest, AlwaysLoopResumesAfterWaitAndStopsOnCommandFailure)
{
using namespace std::chrono_literals;
auto program = Parse("# action: test\ndo always begin\nsw 3\nwait 2 s\nsw 4\nend\nsw 5");
Bilicraft::ActionScheduler queue;
Bilicraft::ActionScheduler::Clock::time_point now{};
queue.Enqueue(std::move(program), now);
for (int i = 0; i < 3; ++i)
{
const auto first = queue.Next(now);
ASSERT_TRUE(first);
EXPECT_EQ(first->command.slot, 3);
queue.Finish(true);
EXPECT_FALSE(queue.Next(now));
now += 2s;
const auto second = queue.Next(now);
ASSERT_TRUE(second);
EXPECT_EQ(second->command.slot, 4);
queue.Finish(i < 2);
}
EXPECT_FALSE(queue.HasWork());
}
TEST_F(ActionFileTest, EmptyInfiniteLoopReturnsControlAndVeryLargeCountsStayCompact)
{
Bilicraft::ActionScheduler queue;
auto program = Parse("# action: test\ndo 18446744073709551615 begin\nend\n"
"do always do 0 sw 9\nsw 5");
ASSERT_LT(program.commands.size(), 10u);
queue.Enqueue(std::move(program));
EXPECT_FALSE(queue.Next({}));
ASSERT_TRUE(queue.HasWork());
queue.SubmitInteractive(*ParseInteractiveCommand("sw 1"));
const auto work = queue.Next({});
ASSERT_TRUE(work);
EXPECT_TRUE(work->interactive);
queue.Finish(true);
EXPECT_FALSE(queue.Next({}));
}
TEST_F(ActionFileTest, ValidatesLoopStructureAndSkippedBodiesAtLoadTime)
{
for (auto count : {"-1", "1.2", "infine", "2x", "18446744073709551616"})
Reject(std::string("# action: test\ndo ") + count + " sw 1", "nonnegative integer or always");
Reject("# action: test\ndo", "nonnegative integer or always");
Reject("# action: test\ndo 1\n# no body", "missing statement");
Reject("# action: test\ndo 1 begin\nsw 1", "missing end");
Reject("# action: test\ndo 0 invalid", "unknown instruction");
Reject("# action: test\nend", "matching do");
Reject("# action: test\nbegin", "after do");
Reject("# action: test\ndo 1 begin\ndo 2\nend", "complete body", 4);
Write("child.act", "# action: child\nend");
Reject("# action: test\ndo 1 begin\nimport child\nend", "matching do", 3);
RejectInteractive("do always sw 1", "only available in action files");
}
TEST_F(ActionFileTest, ExpandsBindingsInSourceOrderAcrossImports)
{
Write("child.act", "# action: child\nhome\ndefine pos1 20 64 -8\nalias pause wait 1 s\n");