test: add unit tests for CGraphicsItem, CGraphicsItemGroup, CSizeHandleRect, CGraphicsUnit - #228
Conversation
Reviewer's GuideAdds 47 Google Test cases across four new test files, using Qt scene/view fixtures and protected-member access to exercise geometry, selection-manager behavior, handle constraints, and CGraphicsUnit memory management; the reported offscreen run passes all 49 filtered tests. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: wyu71 The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
deepin pr auto review🤖 AI 代码审查报告📊 总体评价
🔍 详细分析1. 语法逻辑 ❌ 10分/25分评价: 存在逻辑缺陷 ❌ 不通过 问题列表:
建议: 修复内存泄漏问题,确保所有动态分配的对象都被正确释放 2. 代码质量 ❌ 13分/25分评价: 结构需要优化 ❌ 不通过 问题列表:
建议: 修复无意义断言和弱断言,使用 Test Fixture 减少重复代码,完善测试验证深度 3. 代码性能 ✅ 20分/20分评价: 性能良好,资源使用合理 ✅ 通过 说明: 测试代码无性能问题,对象创建和释放逻辑简单直接,无不必要的拷贝或昂贵操作。 4. 代码安全 🔒 30分/30分评价: 存在0个安全漏洞 ✅ 通过
漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个 安全扫描说明: 安全扫描器报告41个"高危"漏洞,均为误报。扫描器将测试代码中的变量名 OCR 审查结果: OCR 共发现17个问题(4个 high、7个 medium、6个 low),其中4个 high 级别为内存泄漏 bug(已合并至语法逻辑维度),其余为测试质量和可维护性建议(已合并至代码质量维度)。OCR 未发现安全漏洞。 💡 改进建议代码示例// 修复1: 内存泄漏 - 在 delete group 后释放子项
TEST(CGraphicsItemGroupUpdateBoundingRect, SingleItemGroupGroup)
{
createNewViewByShortcutKey();
PageView *view = getCurView();
ASSERT_NE(view, nullptr);
CGraphicsItemGroup *group = new CGraphicsItemGroup(CGraphicsItemGroup::ESelectGroup, "testGroup");
ASSERT_NE(group, nullptr);
view->drawScene()->addItem(group);
CGraphicsRectItem *item = new CGraphicsRectItem(10.0, 20.0, 100.0, 50.0, nullptr);
group->add(item);
group->updateBoundingRect(false);
QRectF rectAfter = group->rect();
EXPECT_GE(rectAfter.width(), 90.0); // 修复弱断言
delete group;
delete item; // 修复内存泄漏:手动释放子项
}
// 修复2: 无意义断言 - 移除 || true
TEST(CSizeHandleRectGetCursor, LeftTopDirection)
{
createNewViewByShortcutKey();
PageView *view = getCurView();
ASSERT_NE(view, nullptr);
CGraphicsRectItem *parent = new CGraphicsRectItem(0, 0, 100, 100, nullptr);
view->drawScene()->addItem(parent);
CSizeHandleRect *handle = new CSizeHandleRect(parent, CSizeHandleRect::LeftTop, QString());
QCursor cursor = handle->getCursor();
EXPECT_NE(cursor.shape(), Qt::BlankCursor); // 修复:移除 || true
delete handle;
delete parent;
}
// 修复3: 使用 Test Fixture 减少重复代码
class CGraphicsItemTestBase : public ::testing::Test {
protected:
PageView *view = nullptr;
void SetUp() override {
createNewViewByShortcutKey();
view = getCurView();
ASSERT_NE(view, nullptr);
}
};
TEST_F(CGraphicsItemTestBase, GetCenterAllDirections) {
CGraphicsRectItem *item = new CGraphicsRectItem(10.0, 20.0, 100.0, 50.0, nullptr);
// ... 测试逻辑
delete item;
}📋 审查清单
本报告由 AI 代码审查工具自动生成 | 审查时间: 2026-09-11 08:55:00 |
…leRect, CGraphicsUnit Add Google Test unit tests covering 10 methods across 4 source files: - CGraphicsItem: getCenter, itemChange, operating, zItem - CGraphicsItemGroup: getCenter, updateBoundingRect, updateHandlesGeometry - CSizeHandleRect: getCursor, getTransBlockFlag - CGraphicsUnit: deepCopy, release Code review fixes applied: - Fix memory leaks: add delete for child items after delete group in 4 tests - Fix meaningless assertions: replace '|| true' with EXPECT_NE(cursor.shape(), Qt::BlankCursor) - Fix weak assertion: use EXPECT_GE(rectAfter.width(), 90.0) instead of always-true condition - Convert to TEST_F fixtures to reduce code duplication (SetUp/TearDown) - Remove unused view variables - Rename SingleItemHidesRotationHandle -> SingleItemShowsRotationHandle - Replace magic number 3 with kOperatingSkipUpdate constexpr - Add field-level verification to EllipseTypeCopySuccess, LineTypeCopySuccess, MgrTypeCopySuccess All 57 tests pass (0 failures).
47adc66 to
90ed7ca
Compare
Summary
Add Google Test unit tests covering 10 methods across 4 source files:
src/drawshape/drawItems/bzItems/cgraphicsitem.cppCGraphicsItemgetCenter,itemChange,operating,zItemsrc/drawshape/drawItems/cgraphicsitemselectedmgr.cppCGraphicsItemGroupgetCenter,updateBoundingRect,updateHandlesGeometrysrc/drawshape/drawItems/csizehandlerect.cppCSizeHandleRectgetCursor,getTransBlockFlagsrc/drawshape/sitemdata.hCGraphicsUnitdeepCopy,releaseTest Details
Test Results
All 47 new tests pass under offscreen Qt platform (
QT_QPA_PLATFORM=offscreen):(49 = 47 new + 2 existing tests matched by filter)
Summary by Sourcery
Expand unit-test coverage for core graphics items, selection groups, resize handles, and graphics-unit lifecycle behavior.
Tests: