mapping_table.create_mapping_tab 是单个 632 行的 tkinter 闭包巨型函数, 把其中相对独立的 5 个块抽到 mapping_table_helpers: - open_entry_detail_window:弹出条目详情(原代码出现 2 次) - compute_card_layout:根据画布宽度计算列数和卡片宽度 - set_card_bg:批量切换 widget + 子 widget 背景色 - make_card_event_handlers:单卡片的 click/dbl/enter/leave 处理器工厂 - render_card_labels:渲染单卡片内的 title + meta + body label 逻辑保持等价,只是提取闭包消除重复嵌套。新增 8 个单元测试覆盖 compute_card_layout / API 表面,1026 → 1034 用例全过。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
66 lines
1.9 KiB
Python
66 lines
1.9 KiB
Python
"""单元测试:gui/components/mapping_table_helpers.py
|
||
|
||
只测 compute_card_layout(纯函数,无 tk 依赖),其他 tk-依赖函数靠
|
||
mapping_table.py 整体 import 冒烟覆盖。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import pytest
|
||
|
||
from gui.components import mapping_table_helpers as h
|
||
|
||
|
||
def test_compute_card_layout_narrow_canvas_one_column():
|
||
cols, w = h.compute_card_layout(canvas_width=400)
|
||
assert cols == 1
|
||
assert w >= 220
|
||
|
||
|
||
def test_compute_card_layout_wide_canvas_three_columns_capped():
|
||
cols, w = h.compute_card_layout(canvas_width=2000)
|
||
assert cols == 3
|
||
assert w >= 220
|
||
|
||
|
||
def test_compute_card_layout_medium_canvas():
|
||
cols, w = h.compute_card_layout(canvas_width=900)
|
||
assert 1 <= cols <= 3
|
||
assert w >= 220
|
||
|
||
|
||
def test_compute_card_layout_zero_width_falls_back_to_one_column():
|
||
cols, w = h.compute_card_layout(canvas_width=0)
|
||
assert cols == 1
|
||
|
||
|
||
def test_compute_card_layout_respects_max_columns_param():
|
||
cols, _ = h.compute_card_layout(canvas_width=10000, max_columns=5)
|
||
assert cols == 5
|
||
|
||
|
||
def test_compute_card_layout_min_card_width_param():
|
||
"""min_card_w 越大,能放下的列数越少"""
|
||
cols_a, _ = h.compute_card_layout(canvas_width=900, min_card_w=200)
|
||
cols_b, _ = h.compute_card_layout(canvas_width=900, min_card_w=600)
|
||
assert cols_a >= cols_b
|
||
|
||
|
||
def test_open_entry_detail_window_empty_content_no_crash():
|
||
"""空 content 应直接 return 不创建窗口(不需要 tk root)"""
|
||
h.open_entry_detail_window(root=None, content="", scrolled_text_cls=None)
|
||
|
||
|
||
def test_module_public_api():
|
||
"""API 表面:保护重命名/删除时主调用方能跟上"""
|
||
public = {x for x in dir(h) if not x.startswith("_") and not x[0].isupper()}
|
||
expected = {
|
||
"compute_card_layout",
|
||
"make_card_event_handlers",
|
||
"open_entry_detail_window",
|
||
"render_card_labels",
|
||
"set_card_bg",
|
||
}
|
||
# 至少暴露这些
|
||
assert expected.issubset(public)
|