Skip to content

test: add unit tests for undo/redo commands - #565

Merged
pengfeixx merged 1 commit into
linuxdeepin:masterfrom
pengfeixx:test/ut-undo-commands
Sep 4, 2026
Merged

test: add unit tests for undo/redo commands#565
pengfeixx merged 1 commit into
linuxdeepin:masterfrom
pengfeixx:test/ut-undo-commands

Conversation

@pengfeixx

@pengfeixx pengfeixx commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

内容

新增文本编辑撤销/重做命令的 GTest 单元测试:插入、删除、替换、缩进、拖拽插入、行尾格式化及撤销列表,共 18 个文件。

说明

  • 基于 master 独立拉出,可独立评审与合并
  • 仅新增单元测试代码,不改动编辑器本体功能

Summary by Sourcery

Add isolated unit-test coverage for the editor’s undo/redo command classes without changing editor functionality.

Enhancements:

  • Add comprehensive GTest coverage for editor undo/redo commands, including text insertion, deletion, replacement, indentation, drag-and-drop insertion, line-ending formatting, mark updates, and undo-list ordering.
  • Introduce isolated test fixtures and link seams that exercise real Qt document behavior while decoupling tests from the full editor implementation.

Build:

  • Add a dedicated CMake test module with independently executable undo/redo test targets and automatic GTest discovery.

Tests:

  • Add unit tests covering normal paths, boundary cases, reversible undo/redo behavior, command ordering, side effects, text variants, and known edge-case behavior across 14 undo/redo command test suites.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @pengfeixx, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 6 days and 6 hours by commenting @sourcery-ai review. Upgrade to get a review now.

@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown
  • 检测到敏感词unset变动
详情
    {
    "unset": {
        "tests/editor_undo/editor_undo_helpers.h": {
            "b": [
                "//    \u8fd0\u884c\u73af\u5883\u6ce8\u5165\uff0c\u672c\u6587\u4ef6\u4e0d\u8bfb\u53d6/\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff0c\u65e0 qputenv/qunsetenv \u914d\u5bf9\u95ee\u9898\uff09"
            ]
        }
    }
}

@sourcery-ai

sourcery-ai Bot commented Sep 4, 2026

Copy link
Copy Markdown

Reviewer's Guide

Adds an independently buildable GTest suite for editor undo/redo commands, using Qt-backed document fixtures plus link seams for application-specific collaborators, with broad branch, boundary, round-trip, side-effect, and ownership coverage across 14 command/list test targets.

File-Level Changes

Change Details Files
Added a dedicated CMake test module that builds the undo/redo implementation sources into a shared static core and exposes each command through an independently discoverable GTest executable.
  • Configured Qt/Dtk/KF dependencies, offscreen execution, optional debug coverage, and per-command test targets.
  • Included the insert, delete, indent, drag-insert, mark, line-ending, replace-all, and undo-list implementations under test.
tests/editor_undo/CMakeLists.txt
Created shared fixtures and link seams to isolate editor undo commands from the full editor application while preserving real Qt document and cursor behavior.
  • Added QApplication/offscreen setup, cursor and selection helpers, paragraph-separator normalization, recording commands, and fixture lifecycle management.
  • Stubbed TextEdit, EditWrapper, Window, and BottomBar dependencies and recorded observable side effects such as selection restoration, mark updates, UI state, and line-ending format changes.
tests/editor_undo/editor_undo_helpers.h
tests/editor_undo/editor_undo_seam.h
tests/editor_undo/editor_undo_seam.cpp
Added comprehensive unit coverage for text insertion and deletion command variants, including single-cursor, column-edit, middle-button, drag-and-drop, and line-ending deletion behavior.
  • Verified redo/undo document mutations, cursor and selection restoration, command IDs, null-editor paths, empty and boundary inputs, CRLF handling, UTF-16/multibyte text, and repeated cycles.
  • Covered column-selection orientation, offset accumulation, deletion-to-line-end/whole-line branches, and documented tests that preserve currently observed non-reversible defect behavior.
tests/editor_undo/test_inserttextundocommand.cpp
tests/editor_undo/test_midbuttoninserttextundocommand.cpp
tests/editor_undo/test_draginserttextundocommand.cpp
tests/editor_undo/test_deletebackcommand.cpp
tests/editor_undo/test_deletebackaltcommand.cpp
tests/editor_undo/test_deletetextundocommand.cpp
tests/editor_undo/test_deletetextundocommand2.cpp
Added unit coverage for indentation, mark updates, full-document replacement, and end-of-line format commands.
  • Tested single- and multi-line indentation/unindentation, tab and space widths, selection clamping, and multibyte content.
  • Verified mark conversion/application ordering, replacement field propagation, full-document replacement round trips, and bottom-bar format propagation across enum values.
tests/editor_undo/test_indenttextcommand.cpp
tests/editor_undo/test_unindenttextcommand.cpp
tests/editor_undo/test_changemarkcommand.cpp
tests/editor_undo/test_replaceallcommand.cpp
tests/editor_undo/test_endlineformartcommand.cpp
Added coverage for block-based insertion and composite undo-list orchestration, including side effects and ownership behavior.
  • Exercised small/large text thresholds, exact block multiples, quit handling, selection replacement, wrapper/editor null cases, and UI enablement hooks.
  • Verified UndoList append filtering, reverse undo and forward redo ordering, progress-log boundaries, empty lists, repeated round trips, and child-command destruction.
tests/editor_undo/test_insertblockbytextcommand.cpp
tests/editor_undo/test_undolist.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

AI 代码审查报告

项目: linuxdeepin/deepin-editor
PR: #565
分支: test/ut-undo-commands → master
作者: pengfeixx
提交信息: test: add unit tests for undo/redo commands
平台: GitHub


总体评价

总分: 100/100 — 优秀

未发现严重安全问题,代码质量优秀,测试覆盖全面。本次提交为 deepin-editor 项目的 src/editor 目录下 14 个撤销/重做命令类(QUndoCommand 子类)新增了完整的单元测试,采用链接接缝(link seam)技术隔离被测类的依赖,测试设计专业,分支覆盖完整,并正确记录了源码中的 defect 候选。


漏洞统计

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个

指标 数量
当前漏洞总数 0
新增漏洞 0
修复漏洞 0
持平漏洞 0

四维度评分

维度1: 语法逻辑 ✓ (25/25)

语法正确,逻辑清晰

评价: 无编译错误,逻辑合理,边界处理完善

分析详情:

  1. CMakeLists.txt — 语法正确。set(CMAKE_AUTOMOC OFF) 防御性设置合理,防止上游目录属性渗透。静态库 editor_undo_core 正确链接 9 个被测源文件和接缝文件。eu_add_test 函数封装 14 个测试目标,gtest_discover_tests 正确设置 QT_QPA_PLATFORM=offscreen 无头测试环境。

  2. editor_undo_helpers.hensureApp() 使用 static 局部变量保证 QApplication 单例。toLf() 正确归一化 U+2029 段落分隔符。RecordingCommand 的 null 检查到位(if (m_orderLog))。EditorUndoTestBase 的 SetUp/TearDown 生命周期管理正确(重置seam → 安装seam → 构造edit / 卸载seam → 销毁edit)。

  3. editor_undo_seam.cpp — 链接接缝实现完整。g_seam 指针 null 检查在所有 15 个记录桩函数中一致。TextEdit 空壳的 14 个事件 override 全部正确委托 DPlainTextEdit 基类。staticMetaObject 初始化为基类元对象,满足 Qt6 PMF 连接的 assertObjectType<TextEdit> ODR 使用。convertReplaceToMark 的确定性映射逻辑正确。

  4. 14 个测试文件 — 每个测试用例采用 Arrange-Act-Assert 模式,断言精确。多个测试正确记录了源码中的 defect 候选并按实际行为断言(如 DeleteTextUndoCommand 无选区路径 undo 重插位置偏移、DeleteTextUndoCommand2 多选区 undo 使用最后 beginPostion、DragInsertTextUndoCommand 缺少 CRLF 归一化),体现了极高的测试专业水准。

问题列表: 无


维度2: 代码质量 ✓ (25/25)

代码结构清晰,注释完整

评价: 无重复代码,可读性好,符合编码规范

分析详情:

  1. 注释完整性 (5/5) — 每个文件头部均有 SPDX 许可证声明和详细的模块说明。每个测试用例都有 Arrange/Act/Assert 注释。分支清单文档详尽(B1/B2/R1/U1 等映射)。每个测试文件包含"最小清单自检" 10 项核对。注释质量极高。

  2. 代码重复 (5/5)editor_undo_helpers.h 有效集中了公共测试设施(ensureApptoLfcursorAtmakeSelectionfakeBottomBarRecordingCommandEditorUndoTestBase)。CMakeLists.txt 中 eu_add_test 函数消除了 14 个测试目标的重复配置。重复率极低。

  3. 结构合理性 (5/5) — 测试类层次清晰:EditorUndoTestBase → 各具体 Fixture。辅助函数正确放置在 ut:: 命名空间。匿名命名空间用于 TEST_P 参数结构体。每个被测类一个测试文件,组织合理。

  4. 调试信息清理 (5/5) — 无残留调试代码,无敏感注释。CMakeLists.txt 中 message(STATUS "UT: test_${name} configured") 是构建配置日志,属正常构建输出。

  5. 命名规范 (5/5) — 测试命名 ClassName_MethodName_Condition 一致且语义清晰。辅助函数命名明确(ensureApptoLfcursorAtmakeSelectionfakeBottomBar)。

问题列表: 无


维度3: 代码性能 ✓ (20/20)

性能良好,资源使用合理

评价: 算法复杂度合理,无性能瓶颈

分析详情:

  1. 算法复杂度 — 所有测试用例时间复杂度均为 O(1) 或 O(n)(n 为小常数,如 3-10 条命令、1-2MB 文本)。大文本测试 QString(2 * kBlockSize, 'b') 构造 2MB 字符串,在测试环境中可接受。

  2. 资源管理EditorUndoTestBase::TearDown 正确 delete editut_install_seam(nullptr)。堆上构造的命令对象在测试末尾正确 deleteensureApp() 使用 static 单例避免重复创建 QApplication。无资源泄漏。

  3. 内存使用 — 大文本测试在结束后由 QString 析构自动释放。RecordingCommandCountingCommand 使用指针引用 fixture 成员,无额外堆分配。无不必要的拷贝。

  4. 系统调用gtest_discover_tests 在 CMake 配置期执行,非运行期。测试代码本身无频繁系统调用。

问题列表: 无


维度4: 代码安全 ✓ (30/30)

存在0个安全漏洞

评价: 无安全风险,安全合规

分析详情:

本次提交为纯测试代码,无外部用户输入、无网络/文件操作、无命令执行、无硬编码密钥。所有容器操作使用 Qt 安全 API。

安全漏洞检查清单:

  • 无用户输入未校验风险(全部硬编码测试数据)
  • 无命令注入/SQL注入风险(无系统调用/数据库操作)
  • 无硬编码密钥/敏感信息泄露(无密码/Token/密钥)
  • 无路径遍历风险(无文件路径操作)
  • 无缓冲区溢出风险(使用 Qt 安全容器 API)
  • 无不安全密码算法(无加密操作)
  • 无权限绕过风险(测试代码无权限操作)

reinterpret_cast 使用说明: fakeBottomBar()reinterpret_cast<EditWrapper *>(&wrapperHost) 使用 reinterpret_cast 伪造指针,这是链接接缝测试技术的标准做法,注释明确说明了安全性(非虚成员调用,不触碰 vtable),不构成安全漏洞。

安全漏洞列表: 无


改进建议

本次提交代码质量优秀,无安全问题。以下为可选的改进建议(不影响评分):

  1. CMakeLists.txt 中 gcov 链接条件if(CMAKE_BUILD_TYPE STREQUAL "Debug") 检查可能在多配置生成器(如 Ninja Multi-Config)下不生效,建议使用生成器表达式 target_link_libraries(test_${name} PRIVATE $<$<CONFIG:Debug>:gcov>) 替代。这是最佳实践建议,不影响当前功能。

  2. 大文本测试性能test_insertblockbytextcommand.cpp 中的 2MB 文本测试可考虑标注 DISABLED_ 前缀或放入单独的性能测试套件,避免在常规 CI 中消耗过多时间。这是可选优化,不影响正确性。


审查结论

本次提交为 deepin-editor 项目新增了 14 个撤销/重做命令类的单元测试,代码实现与 commit message 目的("add unit tests for undo/redo commands")完全一致。测试采用链接接缝技术隔离依赖,分支覆盖完整,正确记录了源码 defect 候选,代码质量优秀,无安全问题。建议合并。

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: lzwind, pengfeixx

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Add GTest suites for text edit undo commands: insert/delete/replace,
indent/unindent, drag insert, end-line format and the undo list.

新增文本编辑撤销命令的 GTest 单元测试,覆盖插入、删除、替换、缩进、
拖拽插入、行尾格式化及撤销列表。

Log: 新增撤销重做命令单元测试
Influence: 仅新增单元测试代码,不影响编辑器本体功能。
@pengfeixx
pengfeixx force-pushed the test/ut-undo-commands branch from d3e07b1 to 7e6e823 Compare September 4, 2026 09:07
@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown
  • 检测到敏感词unset变动
详情
    {
    "unset": {
        "tests/editor_undo/editor_undo_helpers.h": {
            "b": [
                "//    \u8fd0\u884c\u73af\u5883\u6ce8\u5165\uff0c\u672c\u6587\u4ef6\u4e0d\u8bfb\u53d6/\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff0c\u65e0 qputenv/qunsetenv \u914d\u5bf9\u95ee\u9898\uff09"
            ]
        }
    }
}

@pengfeixx
pengfeixx merged commit d6934cf into linuxdeepin:master Sep 4, 2026
15 of 17 checks passed
@pengfeixx
pengfeixx deleted the test/ut-undo-commands branch September 4, 2026 09:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants