Skip to content

test(editor): add unit tests for editor command classes - #501

Merged
deepin-bot[bot] merged 2 commits into
linuxdeepin:masterfrom
pengfeixx:test/ut-editor-cmd-new
Aug 3, 2026
Merged

test(editor): add unit tests for editor command classes#501
deepin-bot[bot] merged 2 commits into
linuxdeepin:masterfrom
pengfeixx:test/ut-editor-cmd-new

Conversation

@pengfeixx

@pengfeixx pengfeixx commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Add tests for ChangeMarkCommand, EndlineFormartCommand, IndentTextCommand, LineNumberArea and UndoList.

Log: 新增editor模块命令类的单元测试
Influence: 覆盖编辑器撤销/重做命令类的未覆盖函数

Summary by Sourcery

Add unit tests for editor command and UI helper classes to improve undo/redo and interaction coverage.

Tests:

  • Introduce tests for IndentTextCommand and UnindentTextCommand covering construction, destruction, undo, and redo behavior.
  • Add tests for ChangeMarkCommand covering redo, undo, and destructor paths.
  • Add tests for EndlineFormartCommand covering construction, destruction, undo, and redo behavior.
  • Add tests for UndoList covering lifecycle, child command cleanup, and undo/redo operations.
  • Add tests for LineNumberArea covering press point retrieval and mouse press event handling.

@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 have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@sourcery-ai

sourcery-ai Bot commented Aug 3, 2026

Copy link
Copy Markdown

Reviewer's Guide

Add focused unit tests for editor command and UI helper classes to cover previously untested undo/redo logic, destructor behavior, and mouse interaction paths in the editor module.

File-Level Changes

Change Details Files
Add unit tests for indentation and unindentation commands, including undo/redo and destructor behavior.
  • Create UT_IndentTextCommand tests that exercise construction, redo, undo, and both destructor paths using a TextEdit with multi-line content.
  • Create UT_UnindentTextCommand tests that validate constructor, redo behavior for mixed tab/space indentation, and undo round-trip restoring original text.
tests/src/editor/ut_indenttextcommond.cpp
Add unit tests for ChangeMarkCommand covering redo, undo, and destructor paths.
  • Instantiate ChangeMarkCommand with different old/new mark lists to exercise redo and undo implementations.
  • Add a destructor-focused test to ensure ChangeMarkCommand instances can be safely destroyed in normal usage.
tests/src/editor/ut_changemarkcommand.cpp
Add unit tests for EndlineFormartCommand to validate construction and undo/redo behavior with BottomBar integration.
  • Construct EndlineFormartCommand with Unix→Windows format conversion and assert successful allocation.
  • Exercise redo and undo methods to cover end-of-line format switching paths without crashing.
tests/src/editor/ut_endlineformatcommond.cpp
Add unit tests for UndoList to cover lifecycle management and child command handling, including undo/redo.
  • Test basic construction/destruction of UndoList to cover default destructor.
  • Append IndentTextCommand children (including a nullptr) and delete the UndoList to exercise ownership cleanup loop.
  • Exercise undo and redo on a list containing an IndentTextCommand to cover command-list sequencing.
tests/src/editor/ut_undolist.cpp
Add unit tests for LineNumberArea to cover press-point querying and safe mouse press handling.
  • Verify default getPressPoint() returns a null QPoint on a freshly constructed LineNumberArea.
  • Simulate a MouseButtonPress event with an out-of-range x coordinate to cover the early-return path in mousePressEvent and ensure no crash.
tests/src/editor/ut_linenumberarea.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

Add tests for ChangeMarkCommand, EndlineFormartCommand, IndentTextCommand,
LineNumberArea and UndoList.

Log: 新增editor模块命令类的单元测试
Influence: 覆盖编辑器撤销/重做命令类的未覆盖函数
@pengfeixx
pengfeixx force-pushed the test/ut-editor-cmd-new branch from a38605a to 6447806 Compare August 3, 2026 01:59
Log: 修正新增测试文件的SPDX版权头年份
Influence: 无
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:95分

■ 【总体评价】

代码为文本编辑器组件新增了全面的单元测试覆盖,有效提升了核心撤销/重做逻辑的测试完备性。
逻辑正确且无安全漏洞,但因测试用例存在较多重复代码导致质量一般扣5分。

■ 【详细分析】

  • 1.语法逻辑(基本正确)✓

测试用例正确使用了GTest框架的TEST宏,对象的创建与销毁配对正确,如ut_changemarkcommand.cpp中的redoundo测试逻辑清晰。UT_UndoList::destructor_with_children中传入了nullptr以验证边界条件,符合测试规范。
潜在问题:若被测函数appendCom未对nullptr进行防御性检查,可能导致测试崩溃,但这属于被测代码的问题而非测试代码本身的语法错误。
建议:确保被测代码具备良好的空指针防御能力。

  • 2.代码质量(一般)✕

测试代码中存在大量重复的对象初始化和清理逻辑,例如在ut_indenttextcommond.cpput_undolist.cpp的多个测试用例中,重复创建TextEdit、设置文本和光标位置。
潜在问题:代码冗余度高,维护成本增加;若TextEdit的初始化方式发生变更,需要修改多处测试代码。
建议:使用GTest的SetUp()TearDown()方法提取公共的初始化和清理逻辑,或者提供辅助函数来减少重复代码。

  • 3.代码性能(无性能问题)✓

作为单元测试代码,主要关注功能验证,不涉及复杂算法或高频调用,测试用例执行效率高,无性能瓶颈。
建议:保持当前的轻量化测试数据规模。

  • 4.代码安全(存在0个安全漏洞)✓

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
本次代码为纯本地单元测试,不涉及网络通信、文件解析或外部输入处理,不存在注入、溢出等安全风险。
建议:维持现状,在后续涉及复杂输入处理的测试中注意模拟恶意输入。

■ 【改进建议代码示例】

// 以 ut_undolist.cpp 为例,展示如何使用 SetUp/TearDown 减少重复代码
#include "gtest/gtest.h"
#include "../../src/editor/undolist.h"
#include "../../src/editor/indenttextcommond.h"
#include "../../src/editor/dtextedit.h"
#include "../stub.h"

class UT_UndoList : public ::testing::Test {
protected:
    void SetUp() override {
        list = new UndoList;
        edit = new TextEdit;
        edit->setPlainText("aaa\nbbb");
        QTextCursor c = edit->textCursor();
        c.setPosition(0);
        edit->setTextCursor(c);
    }

    void TearDown() override {
        delete list;
        delete edit;
    }

    UndoList *list;
    TextEdit *edit;
};

TEST_F(UT_UndoList, constructor_and_destructor)
{
    ASSERT_TRUE(list != nullptr);
}

TEST_F(UT_UndoList, destructor_with_children)
{
    list->appendCom(new IndentTextCommand(edit, 0, 0, 0, 0));
    list->appendCom(new IndentTextCommand(edit, 0, 0, 0, 0));
    list->appendCom(nullptr);
}

TEST_F(UT_UndoList, undo_redo)
{
    list->appendCom(new IndentTextCommand(edit, 0, 0, 0, 0));
    list->redo();
    list->undo();
}

@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

@pengfeixx

Copy link
Copy Markdown
Contributor Author

/merge

@deepin-bot
deepin-bot Bot merged commit fa9de02 into linuxdeepin:master Aug 3, 2026
20 checks passed
@pengfeixx
pengfeixx deleted the test/ut-editor-cmd-new branch August 3, 2026 02:14
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