E4c (VDE-583 §3): - Parameter/ExpressionEngine/ParameterManager:参数表 CRUD + 表达式依赖收敛重算 - update_all 返回变化数;min/max 钳制 + Integer 取整;依赖失败保持原值并记录错误 - bind_to_feature(param, feature_id, field) → resolve_parameters 将参数值替换进特征定义 - 表达式求值器补充 %(取模)运算符(与 * / 同优先级) E4d (VDE-583 §4): - vde::core::UndoStack:push(执行+合并)/undo/redo/undo_text/redo_text - set_clean/is_clean 干净状态;begin_macro/end_macro 宏折叠为单撤销单元 - set_max_undo_levels 深度限制(裁剪失效干净标记) 测试:expression 153 / param 34 / undo_stack 19 全部通过;E4 全量构建 210/210。
335 lines
11 KiB
C++
335 lines
11 KiB
C++
/// @file test_undo_stack.cpp
|
||
/// @brief 撤销/重做栈 (E4d, vde::core::UndoStack) 测试
|
||
///
|
||
/// 覆盖:
|
||
/// push/undo/redo、重做分支截断、命令合并、undo_text/redo_text、
|
||
/// 干净状态(set_clean/is_clean)、宏(begin_macro/end_macro)、
|
||
/// 深度限制(set_max_undo_levels)、clear
|
||
|
||
#include <gtest/gtest.h>
|
||
#include "vde/core/undo_stack.h"
|
||
#include <memory>
|
||
|
||
using namespace vde::core;
|
||
|
||
namespace {
|
||
|
||
/// 用于验证合并语义的计数器命令
|
||
class CounterCommand : public Command {
|
||
public:
|
||
CounterCommand(int* counter, int delta, bool mergeable)
|
||
: counter_(counter), delta_(delta), mergeable_(mergeable) {}
|
||
|
||
void execute() override { *counter_ += delta_; }
|
||
void undo() override { *counter_ -= delta_; }
|
||
[[nodiscard]] std::string description() const override { return "counter"; }
|
||
|
||
bool merge(Command* other) override {
|
||
auto* c = dynamic_cast<CounterCommand*>(other);
|
||
if (!c || !mergeable_) return false;
|
||
delta_ += c->delta_;
|
||
return true;
|
||
}
|
||
|
||
private:
|
||
int* counter_;
|
||
int delta_;
|
||
bool mergeable_;
|
||
};
|
||
|
||
} // namespace
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// push / undo / redo
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(UndoStack, PushExecutesCommand) {
|
||
int x = 0;
|
||
UndoStack stack;
|
||
EXPECT_TRUE(stack.push(make_command([&] { x = 5; }, [&] { x = 0; }, "set x=5")));
|
||
EXPECT_EQ(x, 5);
|
||
EXPECT_TRUE(stack.can_undo());
|
||
EXPECT_FALSE(stack.can_redo());
|
||
EXPECT_EQ(stack.count(), 1u);
|
||
EXPECT_EQ(stack.index(), 0);
|
||
}
|
||
|
||
TEST(UndoStack, PushNullFails) {
|
||
UndoStack stack;
|
||
EXPECT_FALSE(stack.push(nullptr));
|
||
EXPECT_EQ(stack.count(), 0u);
|
||
}
|
||
|
||
TEST(UndoStack, UndoRedo) {
|
||
int x = 0;
|
||
UndoStack stack;
|
||
stack.push(make_command([&] { x = 1; }, [&] { x = 0; }, "op1"));
|
||
stack.push(make_command([&] { x = 2; }, [&] { x = 1; }, "op2"));
|
||
stack.push(make_command([&] { x = 3; }, [&] { x = 2; }, "op3"));
|
||
|
||
EXPECT_TRUE(stack.undo());
|
||
EXPECT_EQ(x, 2);
|
||
EXPECT_TRUE(stack.undo());
|
||
EXPECT_EQ(x, 1);
|
||
EXPECT_TRUE(stack.can_redo());
|
||
|
||
EXPECT_TRUE(stack.redo());
|
||
EXPECT_EQ(x, 2);
|
||
EXPECT_TRUE(stack.redo());
|
||
EXPECT_EQ(x, 3);
|
||
EXPECT_FALSE(stack.redo());
|
||
EXPECT_TRUE(stack.can_undo());
|
||
}
|
||
|
||
TEST(UndoStack, UndoPastStartFails) {
|
||
UndoStack stack;
|
||
stack.push(make_command([] {}, [] {}, "op"));
|
||
EXPECT_TRUE(stack.undo());
|
||
EXPECT_FALSE(stack.undo()); // 回到初始状态后不可再撤销
|
||
EXPECT_FALSE(stack.can_undo());
|
||
}
|
||
|
||
TEST(UndoStack, PushTruncatesRedoBranch) {
|
||
int x = 0;
|
||
UndoStack stack;
|
||
stack.push(make_command([&] { x = 1; }, [&] { x = 0; }, "op1"));
|
||
stack.push(make_command([&] { x = 2; }, [&] { x = 1; }, "op2"));
|
||
stack.push(make_command([&] { x = 3; }, [&] { x = 2; }, "op3"));
|
||
|
||
EXPECT_TRUE(stack.undo()); // 回到 op2
|
||
EXPECT_TRUE(stack.can_redo());
|
||
|
||
// 压入新命令 → 重做分支被截断
|
||
stack.push(make_command([&] { x = 99; }, [&] { x = 2; }, "new"));
|
||
EXPECT_EQ(x, 99);
|
||
EXPECT_FALSE(stack.can_redo());
|
||
EXPECT_EQ(stack.count(), 3u); // op1, op2, new(op3 被截断)
|
||
|
||
// 旧 op3 已不可重做
|
||
EXPECT_TRUE(stack.undo());
|
||
EXPECT_EQ(x, 2);
|
||
EXPECT_FALSE(stack.redo() && x == 3);
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 命令合并
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(UndoStack, MergeableCommandsCollapse) {
|
||
int counter = 0;
|
||
UndoStack stack;
|
||
stack.push(std::make_unique<CounterCommand>(&counter, 1, /*mergeable=*/true));
|
||
stack.push(std::make_unique<CounterCommand>(&counter, 2, /*mergeable=*/true));
|
||
stack.push(std::make_unique<CounterCommand>(&counter, 4, /*mergeable=*/true));
|
||
EXPECT_EQ(counter, 7);
|
||
EXPECT_EQ(stack.count(), 1u); // 合并为一个条目
|
||
|
||
// 撤销一次 = 撤销合并的全部
|
||
EXPECT_TRUE(stack.undo());
|
||
EXPECT_EQ(counter, 0);
|
||
EXPECT_TRUE(stack.redo());
|
||
EXPECT_EQ(counter, 7);
|
||
}
|
||
|
||
TEST(UndoStack, NonMergeableCommandsStaySeparate) {
|
||
int counter = 0;
|
||
UndoStack stack;
|
||
stack.push(std::make_unique<CounterCommand>(&counter, 1, /*mergeable=*/false));
|
||
stack.push(std::make_unique<CounterCommand>(&counter, 2, /*mergeable=*/false));
|
||
EXPECT_EQ(stack.count(), 2u);
|
||
EXPECT_TRUE(stack.undo());
|
||
EXPECT_EQ(counter, 1);
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// undo_text / redo_text / 干净状态
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(UndoStack, UndoRedoText) {
|
||
UndoStack stack;
|
||
EXPECT_EQ(stack.undo_text(), "");
|
||
EXPECT_EQ(stack.redo_text(), "");
|
||
|
||
stack.push(make_command([] {}, [] {}, "draw box"));
|
||
EXPECT_EQ(stack.undo_text(), "draw box");
|
||
stack.push(make_command([] {}, [] {}, "fillet edge"));
|
||
EXPECT_EQ(stack.undo_text(), "fillet edge");
|
||
|
||
EXPECT_TRUE(stack.undo());
|
||
EXPECT_EQ(stack.undo_text(), "draw box");
|
||
EXPECT_EQ(stack.redo_text(), "fillet edge");
|
||
}
|
||
|
||
TEST(UndoStack, CleanState) {
|
||
UndoStack stack;
|
||
EXPECT_TRUE(stack.is_clean()); // 初始空状态 = 干净
|
||
|
||
stack.push(make_command([] {}, [] {}, "op"));
|
||
EXPECT_FALSE(stack.is_clean());
|
||
|
||
stack.set_clean();
|
||
EXPECT_TRUE(stack.is_clean());
|
||
|
||
stack.push(make_command([] {}, [] {}, "op2"));
|
||
EXPECT_FALSE(stack.is_clean());
|
||
|
||
// 撤销回干净标记位置
|
||
EXPECT_TRUE(stack.undo());
|
||
EXPECT_TRUE(stack.is_clean());
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 宏
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(UndoStack, MacroCollapsesToSingleUndoUnit) {
|
||
int x = 0;
|
||
UndoStack stack;
|
||
stack.begin_macro("make part");
|
||
EXPECT_TRUE(stack.is_macro_active());
|
||
stack.push(make_command([&] { x += 1; }, [&] { x -= 1; }, "inc1"));
|
||
stack.push(make_command([&] { x += 1; }, [&] { x -= 1; }, "inc2"));
|
||
stack.push(make_command([&] { x += 1; }, [&] { x -= 1; }, "inc3"));
|
||
EXPECT_FALSE(stack.is_macro_active() == false || x != 3);
|
||
stack.end_macro();
|
||
EXPECT_FALSE(stack.is_macro_active());
|
||
|
||
// 折叠为单个撤销单元;子命令已在录制期执行(不重复执行)
|
||
EXPECT_EQ(x, 3);
|
||
EXPECT_EQ(stack.count(), 1u);
|
||
|
||
// 一次撤销 = 全部子命令逆序撤销
|
||
EXPECT_TRUE(stack.undo());
|
||
EXPECT_EQ(x, 0);
|
||
EXPECT_FALSE(stack.can_undo());
|
||
|
||
// 重做 = 全部子命令重新执行
|
||
EXPECT_TRUE(stack.redo());
|
||
EXPECT_EQ(x, 3);
|
||
}
|
||
|
||
TEST(UndoStack, MacroUndoTextUsesMacroName) {
|
||
UndoStack stack;
|
||
stack.begin_macro("drill holes");
|
||
stack.push(make_command([] {}, [] {}, "sub1"));
|
||
stack.push(make_command([] {}, [] {}, "sub2"));
|
||
stack.end_macro();
|
||
EXPECT_EQ(stack.count(), 1u);
|
||
EXPECT_NE(stack.undo_text().find("drill holes"), std::string::npos);
|
||
}
|
||
|
||
TEST(UndoStack, EmptyMacroIsNoOp) {
|
||
UndoStack stack;
|
||
stack.begin_macro("nothing");
|
||
stack.end_macro();
|
||
EXPECT_EQ(stack.count(), 0u);
|
||
EXPECT_FALSE(stack.can_undo());
|
||
}
|
||
|
||
TEST(UndoStack, MacroWithoutEndStillRecords) {
|
||
int x = 0;
|
||
UndoStack stack;
|
||
stack.begin_macro("pending");
|
||
stack.push(make_command([&] { x += 1; }, [&] { x -= 1; }, "inc"));
|
||
// 未调用 end_macro → 命令已执行但未进入栈
|
||
EXPECT_EQ(x, 1);
|
||
EXPECT_EQ(stack.count(), 0u);
|
||
// clear 清理未结束的宏
|
||
stack.clear();
|
||
EXPECT_FALSE(stack.is_macro_active());
|
||
}
|
||
|
||
TEST(UndoStack, MacroThenNormalCommand) {
|
||
int x = 0;
|
||
UndoStack stack;
|
||
stack.begin_macro("group");
|
||
stack.push(make_command([&] { x += 10; }, [&] { x -= 10; }, "a"));
|
||
stack.push(make_command([&] { x += 10; }, [&] { x -= 10; }, "b"));
|
||
stack.end_macro();
|
||
stack.push(make_command([&] { x += 5; }, [&] { x -= 5; }, "single"));
|
||
EXPECT_EQ(x, 25);
|
||
EXPECT_EQ(stack.count(), 2u);
|
||
|
||
EXPECT_TRUE(stack.undo());
|
||
EXPECT_EQ(x, 20); // 撤销 single
|
||
EXPECT_TRUE(stack.undo());
|
||
EXPECT_EQ(x, 0); // 撤销整个宏
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 深度限制 / clear
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(UndoStack, MaxUndoLevelsTrimsOldest) {
|
||
UndoStack stack;
|
||
stack.set_max_undo_levels(2);
|
||
stack.push(make_command([] {}, [] {}, "op1"));
|
||
stack.push(make_command([] {}, [] {}, "op2"));
|
||
stack.push(make_command([] {}, [] {}, "op3"));
|
||
stack.push(make_command([] {}, [] {}, "op4"));
|
||
|
||
EXPECT_EQ(stack.count(), 2u); // op3, op4 保留
|
||
EXPECT_EQ(stack.max_undo_levels(), 2);
|
||
EXPECT_EQ(stack.undo_text(), "op4");
|
||
EXPECT_TRUE(stack.undo());
|
||
EXPECT_EQ(stack.undo_text(), "op3");
|
||
EXPECT_TRUE(stack.undo());
|
||
EXPECT_FALSE(stack.undo()); // op1/op2 已被裁剪
|
||
}
|
||
|
||
TEST(UndoStack, MaxUndoLevelsZeroIsUnlimited) {
|
||
UndoStack stack;
|
||
stack.set_max_undo_levels(0);
|
||
for (int i = 0; i < 100; ++i) {
|
||
stack.push(make_command([] {}, [] {}, "op"));
|
||
}
|
||
EXPECT_EQ(stack.count(), 100u);
|
||
}
|
||
|
||
TEST(UndoStack, MaxUndoLevelsTrimInvalidatesCleanMarker) {
|
||
UndoStack stack;
|
||
stack.set_max_undo_levels(2);
|
||
stack.push(make_command([] {}, [] {}, "op1"));
|
||
stack.set_clean(); // 干净标记在 op1(最旧)
|
||
stack.push(make_command([] {}, [] {}, "op2"));
|
||
stack.push(make_command([] {}, [] {}, "op3")); // 裁剪掉 op1 → 干净标记失效
|
||
|
||
EXPECT_FALSE(stack.is_clean());
|
||
// 撤销回 op2 也不应视为干净(原保存点已丢失)
|
||
EXPECT_TRUE(stack.undo());
|
||
EXPECT_FALSE(stack.is_clean());
|
||
}
|
||
|
||
TEST(UndoStack, ClearResetsAll) {
|
||
int x = 0;
|
||
UndoStack stack;
|
||
stack.push(make_command([&] { x = 1; }, [&] { x = 0; }, "op"));
|
||
stack.set_clean();
|
||
stack.begin_macro("m");
|
||
stack.clear();
|
||
|
||
EXPECT_EQ(stack.count(), 0u);
|
||
EXPECT_EQ(stack.index(), -1);
|
||
EXPECT_TRUE(stack.is_clean());
|
||
EXPECT_FALSE(stack.is_macro_active());
|
||
EXPECT_FALSE(stack.can_undo());
|
||
EXPECT_FALSE(stack.can_redo());
|
||
EXPECT_EQ(stack.undo_text(), "");
|
||
}
|
||
|
||
TEST(UndoStack, SetCleanOnNonCleanIndex) {
|
||
UndoStack stack;
|
||
stack.push(make_command([] {}, [] {}, "a"));
|
||
stack.push(make_command([] {}, [] {}, "b"));
|
||
stack.push(make_command([] {}, [] {}, "c"));
|
||
stack.set_clean(); // 保存点在 c
|
||
EXPECT_TRUE(stack.is_clean());
|
||
|
||
EXPECT_TRUE(stack.undo());
|
||
EXPECT_FALSE(stack.is_clean());
|
||
EXPECT_TRUE(stack.undo());
|
||
EXPECT_TRUE(stack.redo());
|
||
EXPECT_TRUE(stack.redo());
|
||
EXPECT_TRUE(stack.is_clean()); // 回到 c
|
||
}
|