chore: 工具迁入正式工程 + 工程 README + 根目录草稿报告入 ignore
- eip4-poc/tools/{s3_rewriter,loadtest} → tools/(P4 改写器种子与压测 harness 随工程演进,PoC 冻结)
- 新增工程 README(模块图/纪律/常用命令/自举说明)
- 根目录 parity-reports/ 为 CLI 默认输出草稿,入 .gitignore(正式报告在 platform-parity/parity-reports/)
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
823
tools/s3_rewriter/test_rewriter.py
Normal file
823
tools/s3_rewriter/test_rewriter.py
Normal file
@@ -0,0 +1,823 @@
|
||||
"""
|
||||
test_rewriter.py — S3 改写器单元测试
|
||||
覆盖每条改写规则 + 内联逻辑 + 分类标签 + 边界情况。
|
||||
|
||||
运行:
|
||||
python -m pytest test_rewriter.py -v
|
||||
# 或
|
||||
python -m unittest test_rewriter -v
|
||||
"""
|
||||
|
||||
import unittest
|
||||
import sys
|
||||
import os
|
||||
|
||||
sys.path.insert(0, os.path.dirname(__file__))
|
||||
from rewrite import rewrite_script
|
||||
|
||||
|
||||
class TestHardS3Detection(unittest.TestCase):
|
||||
"""硬 S3 检测 → 整条转人工,标签 HARD"""
|
||||
|
||||
def _assert_hard(self, src: str, msg: str = ""):
|
||||
_, details, label = rewrite_script(src)
|
||||
self.assertEqual(label, "HARD", msg or f"应为 HARD: {src[:60]}")
|
||||
|
||||
def test_new_window(self):
|
||||
self._assert_hard('def w = new Window("弹窗")')
|
||||
|
||||
def test_new_button(self):
|
||||
self._assert_hard('def b = new Button("确认")')
|
||||
|
||||
def test_new_combobox(self):
|
||||
self._assert_hard('def c = new ComboBox("类型")')
|
||||
|
||||
def test_new_textfield(self):
|
||||
self._assert_hard('def tf = new TextField("备注")')
|
||||
|
||||
def test_new_label(self):
|
||||
self._assert_hard('def lbl = new Label("提示")')
|
||||
|
||||
def test_new_table(self):
|
||||
self._assert_hard('def tbl = new Table("列表")')
|
||||
|
||||
def test_new_panel(self):
|
||||
self._assert_hard('def p = new Panel()')
|
||||
|
||||
def test_showConfirmDialog(self):
|
||||
self._assert_hard('showConfirmDialog("确认删除?", { it -> })')
|
||||
|
||||
def test_ui_getCurrent(self):
|
||||
self._assert_hard('UI.getCurrent().addWindow(w)')
|
||||
|
||||
def test_hard_takes_priority_over_s3_patterns(self):
|
||||
"""即使同时含 setVisible,只要有 new Window 就整条 HARD"""
|
||||
src = '''
|
||||
group.getField("X").setVisible(true)
|
||||
def win = new Window("弹窗")
|
||||
'''
|
||||
_, _, label = rewrite_script(src)
|
||||
self.assertEqual(label, "HARD")
|
||||
|
||||
|
||||
class TestNoS3(unittest.TestCase):
|
||||
"""无 S3 模式 → 原样返回,标签 NO_S3"""
|
||||
|
||||
def _assert_no_s3(self, src: str):
|
||||
result, details, label = rewrite_script(src)
|
||||
self.assertEqual(label, "NO_S3")
|
||||
self.assertEqual(result, src)
|
||||
|
||||
def test_pure_expression(self):
|
||||
self._assert_no_s3("return item.AMOUNT > 0\n")
|
||||
|
||||
def test_s2_enable_button(self):
|
||||
self._assert_no_s3('enableButton("BTN", item.STATUS == "DRAFT")\n')
|
||||
|
||||
def test_s2_setFieldVisible(self):
|
||||
self._assert_no_s3('setFieldVisible("X", true)\n')
|
||||
|
||||
def test_empty_string(self):
|
||||
self._assert_no_s3("")
|
||||
|
||||
def test_no_vaadin(self):
|
||||
self._assert_no_s3("def x = item.A + item.B\nreturn x\n")
|
||||
|
||||
|
||||
class TestAddButton(unittest.TestCase):
|
||||
"""addButton 规则"""
|
||||
|
||||
def test_basic(self):
|
||||
src = 'group.getField("PROJ_STATUS").addButton("查看", handler)'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('addFieldButton("PROJ_STATUS", "查看", handler)', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_single_quote(self):
|
||||
src = "group.getField('REMARK').addButton('备注', remarkAction)"
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn("addFieldButton('REMARK', '备注', remarkAction)", result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_multiple_buttons(self):
|
||||
src = (
|
||||
'group.getField("X").addButton("A", h1)\n'
|
||||
'group.getField("Y").addButton("B", h2)\n'
|
||||
)
|
||||
result, _, _ = rewrite_script(src)
|
||||
self.assertIn('addFieldButton("X", "A", h1)', result)
|
||||
self.assertIn('addFieldButton("Y", "B", h2)', result)
|
||||
|
||||
def test_no_original_call_remains(self):
|
||||
src = 'group.getField("X").addButton("test", cb)'
|
||||
result, _, _ = rewrite_script(src)
|
||||
self.assertNotIn('group.getField("X").addButton', result)
|
||||
|
||||
|
||||
class TestGetValue(unittest.TestCase):
|
||||
"""getValue 规则"""
|
||||
|
||||
def test_basic(self):
|
||||
src = 'def v = group.getField("AMOUNT").getValue()'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('getFieldValue("AMOUNT")', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_no_original_remains(self):
|
||||
src = 'group.getField("X").getValue()'
|
||||
result, _, _ = rewrite_script(src)
|
||||
self.assertNotIn('.getValue()', result)
|
||||
|
||||
def test_multiple_fields(self):
|
||||
src = (
|
||||
'def a = group.getField("A").getValue()\n'
|
||||
'def b = group.getField("B").getValue()\n'
|
||||
)
|
||||
result, _, _ = rewrite_script(src)
|
||||
self.assertIn('getFieldValue("A")', result)
|
||||
self.assertIn('getFieldValue("B")', result)
|
||||
|
||||
|
||||
class TestSetValue(unittest.TestCase):
|
||||
"""setValue 规则"""
|
||||
|
||||
def test_basic(self):
|
||||
src = 'group.getField("STATUS").setValue("ACTIVE")'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('setFieldValue("STATUS", "ACTIVE")', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_variable_arg(self):
|
||||
src = 'group.getField("QTY").setValue(qty)'
|
||||
result, _, _ = rewrite_script(src)
|
||||
self.assertIn('setFieldValue("QTY", qty)', result)
|
||||
|
||||
|
||||
class TestSetVisible(unittest.TestCase):
|
||||
"""setVisible 规则"""
|
||||
|
||||
def test_basic_true(self):
|
||||
src = 'group.getField("NOTE").setVisible(true)'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('setFieldVisible("NOTE", true)', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_expression_arg(self):
|
||||
src = 'group.getField("CERT").setVisible(item.TYPE == "A")'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('setFieldVisible("CERT", item.TYPE == "A")', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_no_original_remains(self):
|
||||
src = 'group.getField("X").setVisible(false)'
|
||||
result, _, _ = rewrite_script(src)
|
||||
self.assertNotIn('.setVisible(', result)
|
||||
|
||||
|
||||
class TestSetCaption(unittest.TestCase):
|
||||
"""setCaption 规则"""
|
||||
|
||||
def test_basic(self):
|
||||
src = 'group.getField("AMOUNT").setCaption("合同金额(元)")'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('setFieldCaption("AMOUNT", "合同金额(元)")', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_variable_caption(self):
|
||||
src = 'group.getField("NO").setCaption(labelVar)'
|
||||
result, _, _ = rewrite_script(src)
|
||||
self.assertIn('setFieldCaption("NO", labelVar)', result)
|
||||
|
||||
|
||||
class TestSetEnabled(unittest.TestCase):
|
||||
"""setEnabled 规则"""
|
||||
|
||||
def test_basic(self):
|
||||
src = 'group.getField("BTN").setEnabled(false)'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('setFieldEnabled("BTN", false)', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
|
||||
class TestSetReadOnly(unittest.TestCase):
|
||||
"""setReadOnly 规则(字段级 + 组级)"""
|
||||
|
||||
def test_field_level(self):
|
||||
src = 'group.getField("CREATE_TIME").setReadOnly(true)'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('setFieldReadOnly("CREATE_TIME", true)', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_group_level(self):
|
||||
src = 'group.setReadOnly(true)'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('setGroupReadOnly(true)', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_group_expression(self):
|
||||
src = 'group.setReadOnly(item.STATUS != "DRAFT")'
|
||||
result, _, _ = rewrite_script(src)
|
||||
self.assertIn('setGroupReadOnly(item.STATUS != "DRAFT")', result)
|
||||
|
||||
def test_no_original_remains_group(self):
|
||||
src = 'group.setReadOnly(false)'
|
||||
result, _, _ = rewrite_script(src)
|
||||
self.assertNotIn('group.setReadOnly(', result)
|
||||
|
||||
|
||||
class TestSetRequired(unittest.TestCase):
|
||||
"""setRequired 规则"""
|
||||
|
||||
def test_basic(self):
|
||||
src = 'group.getField("TAX_NO").setRequired(true)'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('setFieldRequired("TAX_NO", true)', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
|
||||
class TestGetContainerDataSource(unittest.TestCase):
|
||||
"""getContainerDataSource / setFilter / removeAllItems 规则"""
|
||||
|
||||
def test_setFilter_basic(self):
|
||||
src = 'group.getField("DEPT").getContainerDataSource().setFilter("STATUS=1")'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('setFieldDataFilter("DEPT", "STATUS=1")', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_removeAllItems(self):
|
||||
src = 'group.getField("LIST").getContainerDataSource().removeAllItems()'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('clearFieldData("LIST")', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_bare_getContainerDataSource(self):
|
||||
src = 'def ds = group.getField("X").getContainerDataSource()'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('getFieldDataSource("X")', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_no_original_setFilter_remains(self):
|
||||
src = 'group.getField("X").getContainerDataSource().setFilter("A=1")'
|
||||
result, _, _ = rewrite_script(src)
|
||||
self.assertNotIn('getContainerDataSource()', result)
|
||||
self.assertNotIn('.setFilter(', result)
|
||||
|
||||
|
||||
class TestGetDisplay(unittest.TestCase):
|
||||
"""getDisplay 规则"""
|
||||
|
||||
def test_basic(self):
|
||||
src = 'def d = group.getField("STATUS").getDisplay()'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('getFieldDisplay("STATUS")', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_no_original_remains(self):
|
||||
src = 'group.getField("X").getDisplay()'
|
||||
result, _, _ = rewrite_script(src)
|
||||
self.assertNotIn('.getDisplay()', result)
|
||||
|
||||
|
||||
class TestNotificationShow(unittest.TestCase):
|
||||
"""Notification.show → showToast 规则"""
|
||||
|
||||
def test_basic(self):
|
||||
src = 'Notification.show("操作成功")'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('showToast("操作成功")', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_variable_msg(self):
|
||||
src = 'Notification.show(msg)'
|
||||
result, _, _ = rewrite_script(src)
|
||||
self.assertIn('showToast(msg)', result)
|
||||
|
||||
def test_no_original_remains(self):
|
||||
src = 'Notification.show("x")'
|
||||
result, _, _ = rewrite_script(src)
|
||||
self.assertNotIn('Notification.show(', result)
|
||||
|
||||
|
||||
class TestGroupSetEnabled(unittest.TestCase):
|
||||
"""group.setEnabled 组级规则"""
|
||||
|
||||
def test_basic(self):
|
||||
src = 'group.setEnabled(true)'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('setGroupEnabled(true)', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
|
||||
class TestInlineFieldHandle(unittest.TestCase):
|
||||
"""单赋值字段句柄变量内联"""
|
||||
|
||||
def test_basic_inline(self):
|
||||
src = (
|
||||
'def f = group.getField("STATUS")\n'
|
||||
'f.setVisible(true)\n'
|
||||
)
|
||||
result, details, label = rewrite_script(src)
|
||||
self.assertIn('setFieldVisible("STATUS", true)', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
# 确认赋值行被删除(变为空行)
|
||||
self.assertNotIn('def f = group.getField', result)
|
||||
|
||||
def test_inline_multiple_uses(self):
|
||||
src = (
|
||||
'def fld = group.getField("PROJ_NAME")\n'
|
||||
'fld.setCaption("项目名称")\n'
|
||||
'fld.setEnabled(false)\n'
|
||||
)
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('setFieldCaption("PROJ_NAME", "项目名称")', result)
|
||||
self.assertIn('setFieldEnabled("PROJ_NAME", false)', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_inline_then_setFilter(self):
|
||||
src = (
|
||||
'def combo = group.getField("DEPT")\n'
|
||||
'combo.getContainerDataSource().setFilter("TYPE=1")\n'
|
||||
)
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('setFieldDataFilter("DEPT", "TYPE=1")', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_inline_with_getValue(self):
|
||||
src = (
|
||||
'def f = group.getField("AMOUNT")\n'
|
||||
'def v = f.getValue()\n'
|
||||
)
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('getFieldValue("AMOUNT")', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
|
||||
class TestMixedPatterns(unittest.TestCase):
|
||||
"""多模式混合脚本"""
|
||||
|
||||
def test_addButton_plus_setVisible(self):
|
||||
src = (
|
||||
'group.getField("ACTION").addButton("审批", approveAction)\n'
|
||||
'group.getField("ACTION").setVisible(item.STATUS == "PENDING")\n'
|
||||
)
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('addFieldButton("ACTION", "审批", approveAction)', result)
|
||||
self.assertIn('setFieldVisible("ACTION", item.STATUS == "PENDING")', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_getValue_plus_setFilter(self):
|
||||
src = (
|
||||
'def projId = group.getField("PROJ_ID").getValue()\n'
|
||||
'group.getField("CONTRACT").getContainerDataSource().setFilter("PROJ_ID=" + projId)\n'
|
||||
)
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('getFieldValue("PROJ_ID")', result)
|
||||
self.assertIn('setFieldDataFilter("CONTRACT", "PROJ_ID=" + projId)', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_group_setReadOnly_plus_field(self):
|
||||
src = (
|
||||
'group.setReadOnly(true)\n'
|
||||
'group.getField("REMARK").setReadOnly(false)\n'
|
||||
)
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('setGroupReadOnly(true)', result)
|
||||
self.assertIn('setFieldReadOnly("REMARK", false)', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_notification_plus_getValue(self):
|
||||
src = (
|
||||
'def v = group.getField("QTY").getValue()\n'
|
||||
'if (v == null) { Notification.show("不能为空") }\n'
|
||||
)
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('getFieldValue("QTY")', result)
|
||||
self.assertIn('showToast("不能为空")', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_complex_multi_rule(self):
|
||||
src = '''
|
||||
def projType = group.getField("PROJ_TYPE").getValue()
|
||||
if (projType == "BIG") {
|
||||
group.getField("SAFETY_MGR").setVisible(true)
|
||||
group.getField("SAFETY_MGR").setRequired(true)
|
||||
group.getField("SAFETY_MGR").getContainerDataSource().setFilter("ROLE=SAFETY")
|
||||
} else {
|
||||
group.getField("SAFETY_MGR").setVisible(false)
|
||||
group.getField("SAFETY_MGR").setRequired(false)
|
||||
}
|
||||
group.getField("PROJ_NO").setCaption("项目编号")
|
||||
group.getField("SUBMIT_BTN").addButton("提交", submitAction)
|
||||
Notification.show("类型已切换")
|
||||
'''
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('getFieldValue("PROJ_TYPE")', result)
|
||||
self.assertIn('setFieldVisible("SAFETY_MGR", true)', result)
|
||||
self.assertIn('setFieldRequired("SAFETY_MGR", true)', result)
|
||||
self.assertIn('setFieldDataFilter("SAFETY_MGR", "ROLE=SAFETY")', result)
|
||||
self.assertIn('setFieldCaption("PROJ_NO", "项目编号")', result)
|
||||
self.assertIn('addFieldButton("SUBMIT_BTN", "提交", submitAction)', result)
|
||||
self.assertIn('showToast("类型已切换")', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
|
||||
class TestPartialLabel(unittest.TestCase):
|
||||
"""PARTIAL:部分改写,仍有 S3 残留"""
|
||||
|
||||
def test_unknown_method_causes_partial(self):
|
||||
src = (
|
||||
'group.getField("X").setVisible(true)\n'
|
||||
'group.getField("Y").setStyleName("highlight")\n' # 不在规则表
|
||||
)
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertEqual(label, "PARTIAL")
|
||||
# 已知规则部分已改写
|
||||
self.assertIn('setFieldVisible("X", true)', result)
|
||||
|
||||
|
||||
class TestRewriteDetails(unittest.TestCase):
|
||||
"""改写明细验证"""
|
||||
|
||||
def test_details_contain_rule_name(self):
|
||||
src = 'group.getField("X").setVisible(true)'
|
||||
_, details, _ = rewrite_script(src)
|
||||
rule_names = [d.rule_name for d in details]
|
||||
self.assertIn("setVisible", rule_names)
|
||||
|
||||
def test_hard_details_contain_reason(self):
|
||||
src = 'def w = new Window("test")'
|
||||
_, details, label = rewrite_script(src)
|
||||
self.assertEqual(label, "HARD")
|
||||
hard_details = [d for d in details if d.rule_name == "HARD_DETECTION"]
|
||||
self.assertTrue(len(hard_details) > 0)
|
||||
self.assertIn("动态创建 Vaadin 组件", hard_details[0].original)
|
||||
|
||||
def test_inline_detail_recorded(self):
|
||||
src = (
|
||||
'def f = group.getField("X")\n'
|
||||
'f.setValue("v")\n'
|
||||
)
|
||||
_, details, _ = rewrite_script(src)
|
||||
inline_rules = [d for d in details if "inline" in d.rule_name]
|
||||
self.assertTrue(len(inline_rules) > 0)
|
||||
|
||||
|
||||
class TestEdgeCases(unittest.TestCase):
|
||||
"""边界和特殊情况"""
|
||||
|
||||
def test_idempotent_s2_input(self):
|
||||
"""已是 S2 形态的脚本不会被误改"""
|
||||
src = 'setFieldVisible("X", true)\n'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertEqual(label, "NO_S3")
|
||||
self.assertEqual(result, src)
|
||||
|
||||
def test_single_quote_field_name(self):
|
||||
src = "group.getField('AMOUNT').getValue()"
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn("getFieldValue('AMOUNT')", result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_boolean_arg(self):
|
||||
src = 'group.getField("F").setVisible(false)'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('setFieldVisible("F", false)', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_removeAllItems_alone(self):
|
||||
src = 'group.getField("LIST").getContainerDataSource().removeAllItems()'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('clearFieldData("LIST")', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_only_whitespace(self):
|
||||
src = " \n\t\n "
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertEqual(label, "NO_S3")
|
||||
|
||||
def test_comment_only(self):
|
||||
src = "// 这是一段注释\n// 没有实质代码\n"
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertEqual(label, "NO_S3")
|
||||
|
||||
def test_hard_new_vertical_layout(self):
|
||||
src = 'def vl = new VerticalLayout()'
|
||||
_, _, label = rewrite_script(src)
|
||||
self.assertEqual(label, "HARD")
|
||||
|
||||
def test_hard_new_horizontal_layout(self):
|
||||
src = 'def hl = new HorizontalLayout()'
|
||||
_, _, label = rewrite_script(src)
|
||||
self.assertEqual(label, "HARD")
|
||||
|
||||
def test_setReadOnly_field_no_group_remains(self):
|
||||
src = 'group.getField("F").setReadOnly(true)'
|
||||
result, _, _ = rewrite_script(src)
|
||||
self.assertNotIn('group.getField', result)
|
||||
self.assertNotIn('.setReadOnly(', result)
|
||||
|
||||
def test_mixed_inline_multi_var(self):
|
||||
"""两个不同字段句柄变量同时内联"""
|
||||
src = (
|
||||
'def fA = group.getField("A")\n'
|
||||
'def fB = group.getField("B")\n'
|
||||
'fA.setVisible(true)\n'
|
||||
'fB.setEnabled(false)\n'
|
||||
)
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('setFieldVisible("A", true)', result)
|
||||
self.assertIn('setFieldEnabled("B", false)', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
|
||||
class TestSafeNavigation(unittest.TestCase):
|
||||
"""真实库迭代: Groovy 安全导航 ?. 在所有字段/组规则上等价于 ."""
|
||||
|
||||
def test_safenav_getValue(self):
|
||||
src = 'def v = group.getField("AMOUNT")?.getValue()'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('getFieldValue("AMOUNT")', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_safenav_setVisible(self):
|
||||
src = 'group.getField("NOTE")?.setVisible(true)'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('setFieldVisible("NOTE", true)', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_safenav_removeAllItems(self):
|
||||
src = 'group.getField("LIST")?.removeAllItems()'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('clearFieldData("LIST")', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_safenav_getContainerDataSource(self):
|
||||
src = 'def ds = group.getField("DEPT")?.getContainerDataSource()'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('getFieldDataSource("DEPT")', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_safenav_group_setReadOnly(self):
|
||||
src = 'group?.setReadOnly(true)'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('setGroupReadOnly(true)', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
|
||||
class TestInlineEdgeCases(unittest.TestCase):
|
||||
"""真实库迭代: 内联容忍尾随分号 / 行尾注释 / ?. 引用"""
|
||||
|
||||
def test_inline_with_semicolon(self):
|
||||
src = (
|
||||
'def f = group.getField("ISSHARE");\n'
|
||||
'f?.setReadOnly(true);\n'
|
||||
)
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('setFieldReadOnly("ISSHARE", true)', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_inline_with_trailing_comment(self):
|
||||
src = (
|
||||
'def eligibility = group.getField("ELIGIBILITY");//状态\n'
|
||||
'eligibility?.setVisible(false)\n'
|
||||
)
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('setFieldVisible("ELIGIBILITY", false)', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_inline_safenav_reference(self):
|
||||
src = (
|
||||
'def fld = group.getField("CITY")\n'
|
||||
'fld?.setEnabled(false)\n'
|
||||
)
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('setFieldEnabled("CITY", false)', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_inline_value_use_not_clobbered(self):
|
||||
"""变量当普通值用(var == null)不应被句柄内联误伤"""
|
||||
src = (
|
||||
'def f = group.getField("X")\n'
|
||||
'f?.setVisible(true)\n'
|
||||
'if (somethingElse == null) { doStuff() }\n'
|
||||
)
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('setFieldVisible("X", true)', result)
|
||||
self.assertIn('somethingElse == null', result)
|
||||
|
||||
|
||||
class TestSubTableButtons(unittest.TestCase):
|
||||
"""真实库迭代: 子表动作按钮 / 子表按钮显隐"""
|
||||
|
||||
def test_addSubTableButton(self):
|
||||
src = 'group.getField("GOODSDETAIL")?.addButton(group.getField("SELECT"),0);'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('addSubTableButton("GOODSDETAIL", "SELECT", 0)', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
self.assertNotIn('group.getField', result)
|
||||
|
||||
def test_addFieldButton_still_works(self):
|
||||
"""字段内联按钮(首参字符串)仍走 addFieldButton"""
|
||||
src = 'group.getField("ACTION")?.addButton("审批", approveAction)'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('addFieldButton("ACTION", "审批", approveAction)', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_showAddButton(self):
|
||||
src = 'group.getField("PAIDS")?.showAddButton(false)'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('setSubTableAddButtonVisible("PAIDS", false)', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_showDeleteButton(self):
|
||||
src = 'group.getField("SUB_PROJECTS")?.showDeleteButton(false)'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('setSubTableDeleteButtonVisible("SUB_PROJECTS", false)', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_getModifyButton_setVisible(self):
|
||||
src = 'group.getField("PAIDS")?.getModifyButton().setVisible(false);'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('setSubTableModifyButtonVisible("PAIDS", false)', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_getDetailButton_setVisible(self):
|
||||
src = 'group.getField("PAIDS")?.getDetailButton().setVisible(false);'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('setSubTableDetailButtonVisible("PAIDS", false)', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
|
||||
class TestNewFieldVerbs(unittest.TestCase):
|
||||
"""真实库迭代: 新增字段动词映射"""
|
||||
|
||||
def test_setRequiredError(self):
|
||||
src = 'group.getField("PROJECTCODE")?.setRequiredError("工程编码为必填项!")'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('setFieldRequiredError("PROJECTCODE", "工程编码为必填项!")', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_setViewWithoutEditor(self):
|
||||
src = 'group.getField("CONTENT")?.setViewWithoutEditor(true)'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('setFieldViewOnly("CONTENT", true)', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_focus(self):
|
||||
src = 'group.getField("NAME")?.focus()'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('focusField("NAME")', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_setWidth(self):
|
||||
src = 'group.getField("NAME").setWidth("200px")'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('setFieldWidth("NAME", "200px")', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_setHeight(self):
|
||||
src = 'group.getField("NOTE").setHeight("100px")'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('setFieldHeight("NOTE", "100px")', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_reload(self):
|
||||
src = 'group.getField("UNITID")?.reload();'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('reloadField("UNITID")', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_setConvertedValue(self):
|
||||
src = 'group.getField("YEAR")?.setConvertedValue(currentYear)'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('setFieldConvertedValue("YEAR", currentYear)', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_setNullSelectionItemId(self):
|
||||
src = 'group.getField("YEAR")?.setNullSelectionItemId(nullitem)'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('setFieldNullSelection("YEAR", nullitem)', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_removeItem(self):
|
||||
src = 'group.getField("STATE")?.removeItem(3)'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('removeFieldItem("STATE", 3)', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_propertyDataSource_setReadOnly(self):
|
||||
src = 'group.getField("VOUCHER_ID")?.getPropertyDataSource().setReadOnly(false);'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('setFieldReadOnly("VOUCHER_ID", false)', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
|
||||
class TestBalancedSetFilter(unittest.TestCase):
|
||||
"""真实库迭代: setFilter / setFilterMap 直接挂句柄,含嵌套参数"""
|
||||
|
||||
def test_setFilter_simple_var(self):
|
||||
src = 'group.getField("JOBSTATUSS")?.setFilter(searchMap)'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('setFieldFilter("JOBSTATUSS", searchMap)', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_setFilter_nested_map_literal(self):
|
||||
src = 'group.getField("UNITTYPE").setFilter(["EXTENDATTRIBUTEONE" : 0, "SYS_ENUMERATETYPE_ID" : "UNITTYPE"])'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn(
|
||||
'setFieldFilter("UNITTYPE", ["EXTENDATTRIBUTEONE" : 0, "SYS_ENUMERATETYPE_ID" : "UNITTYPE"])',
|
||||
result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_setFilter_nested_func_call(self):
|
||||
src = 'group.getField("SCHEMEITEM").setFilter(["USERID":GetDefaultValueFunction.getCurrentUserId()])'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn(
|
||||
'setFieldFilter("SCHEMEITEM", ["USERID":GetDefaultValueFunction.getCurrentUserId()])',
|
||||
result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_setFilterMap(self):
|
||||
src = 'group.getField("UNITID").setFilterMap(map)'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('setFieldFilterMap("UNITID", map)', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
|
||||
class TestOptionPopulation(unittest.TestCase):
|
||||
"""真实库迭代: 下拉选项填充 idiom → addFieldOption"""
|
||||
|
||||
def test_inline_datasource_then_option(self):
|
||||
src = (
|
||||
'group.getField("OVERTIMETYPE")?.removeAllItems();\n'
|
||||
'def icType = group.getField("OVERTIMETYPE")?.getContainerDataSource();\n'
|
||||
'icType?.addItem("请选择").getItemProperty("des").setValue("请选择");\n'
|
||||
)
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('clearFieldData("OVERTIMETYPE")', result)
|
||||
self.assertIn('addFieldOption("OVERTIMETYPE", "请选择", "请选择")', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_option_with_func_value(self):
|
||||
src = (
|
||||
'def ic = group.getField("X")?.getContainerDataSource();\n'
|
||||
'ic?.addItem(list.get_Value("CODEVALUE")).getItemProperty("des").setValue(list.get_Value("CODEDES"));\n'
|
||||
)
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn(
|
||||
'addFieldOption("X", list.get_Value("CODEVALUE"), list.get_Value("CODEDES"))',
|
||||
result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_option_full_safenav_chain(self):
|
||||
src = (
|
||||
'def ic = group.getField("PROOFRESULT")?.getContainerDataSource();\n'
|
||||
'ic?.addItem("通过")?.getItemProperty("des")?.setValue("通过");\n'
|
||||
)
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('addFieldOption("PROOFRESULT", "通过", "通过")', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
def test_option_via_handle_var_with_comment(self):
|
||||
src = (
|
||||
'def eligibility = group.getField("ELIGIBILITY");//状态\n'
|
||||
'eligibility?.removeAllItems();\n'
|
||||
'def ic = eligibility?.getContainerDataSource();\n'
|
||||
'ic?.addItem(2).getItemProperty("des").setValue("未评审");\n'
|
||||
)
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn('clearFieldData("ELIGIBILITY")', result)
|
||||
self.assertIn('addFieldOption("ELIGIBILITY", 2, "未评审")', result)
|
||||
self.assertEqual(label, "FULL_AUTO")
|
||||
|
||||
|
||||
class TestHonestBoundary(unittest.TestCase):
|
||||
"""真实库迭代: 深层 Vaadin 组件访问保持 PARTIAL,不错改"""
|
||||
|
||||
def test_comboBox_widget_access_stays_partial(self):
|
||||
src = 'group.getField("CHECKMONORQUA")?.comboBox.setValue(value);'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertEqual(label, "PARTIAL")
|
||||
|
||||
def test_table_widget_access_stays_partial(self):
|
||||
src = 'def cols = group.getField("MATERIALDETAILS").table.getVisibleColumns();'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertEqual(label, "PARTIAL")
|
||||
|
||||
def test_addButton_nonliteral_first_arg_stays_partial(self):
|
||||
"""addButton(field, 0) 首参为非 getField 变量,无法安全解析字段名"""
|
||||
src = 'group.getField("LISTS").addButton(field, 0);'
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertEqual(label, "PARTIAL")
|
||||
|
||||
def test_dynamic_component_in_chain_stays_partial(self):
|
||||
src = (
|
||||
'group.getField("X")?.parent?.parent?.addComponent('
|
||||
'new CaptionLabel("关联收集工程"),"COL")\n'
|
||||
)
|
||||
result, _, label = rewrite_script(src)
|
||||
self.assertIn(label, ("PARTIAL", "HARD"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main(verbosity=2)
|
||||
Reference in New Issue
Block a user