From 56d1cf5e78a057a147dd0eee2e353a7640582041 Mon Sep 17 00:00:00 2001 From: NB-076 Date: Thu, 25 Jun 2026 08:28:43 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20code=20review=20=E2=80=94=20defensiv?= =?UTF-8?q?=E3=81=AA=E4=B8=8B*/=E5=8E=BB=E6=8E=89+B=E6=9E=9D=E4=B8=80?= =?UTF-8?q?=E8=87=B4=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 评审发现修正 ### 1. __import__('re') → re (cond.py, 3处) __import__绕过module级re引用,mock下前后不一致。 ### 2. NOT路径下*/去掉 (cond.py:124-126) NOT WS-PLAN-CODE(WS-IDX) > 50 → 返回保留了下标 其他路径(算术/标准)都去了,只有NOT路径没去。 ### 3. _match_constraint防禦*/去掉 (coverage.py) 两边字段名同时去掉下标再比较,防止约束侧/解析侧 下标处理不一致导致匹配失败。 ### 4. _match_leaf 防禦*/去掉 (coverage.py) CondLeaf路径同样的防禦。 ### 5. 裸字段分支去死码 (cond.py) 在外層guard保證下永遠為真, 是死码。合併為一行。 Co-Authored-By: Claude --- cobol_testgen/cond.py | 19 ++++++++++--------- cobol_testgen/coverage.py | 8 ++++++-- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/cobol_testgen/cond.py b/cobol_testgen/cond.py index 2352669..a12b128 100644 --- a/cobol_testgen/cond.py +++ b/cobol_testgen/cond.py @@ -100,10 +100,9 @@ def parse_single_condition(text, fields=None): # Bare field reference (no operator, no NOT): WS-EOF → WS-EOF = 'Y' if re.match(r'^[A-Z][A-Z0-9_-]*(?:\([^)]*\))?\s*$', text, re.IGNORECASE): - bare = __import__('re').match(r'^[A-Z][A-Z0-9_-]*', text, re.IGNORECASE) - if bare: - return (bare.group(0), '=', 'Y') - return (text, '=', 'Y') + bare = re.match(r'^[A-Z][A-Z0-9_-]*', text, re.IGNORECASE) + field = bare.group(0) if bare else text + return (field, '=', 'Y') # Bare NOT field reference (no operator): NOT WS-EOF → WS-EOF <> 'Y' if text.upper().startswith('NOT ') and not re.search(r'(>=|<=|<>|>|<|=)', text): @@ -122,6 +121,9 @@ def parse_single_condition(text, fields=None): if m_inner: inv_op_map = {'=': '<>', '<>': '=', '>': '<=', '<': '>=', '>=': '<', '<=': '>'} f = re.sub(r'\s*([(),])\s*', r'\1', m_inner.group(1)) + bare = re.match(r"^(\w[\w-]*)", f) + if bare: + f = bare.group(1) op = m_inner.group(2) val = m_inner.group(3).strip().strip("'").strip('"') inv = inv_op_map.get(op, op) @@ -163,7 +165,7 @@ def parse_single_condition(text, fields=None): if field.upper().endswith(' NOT'): field = field[:-4].strip() # Strip subscript: WS-KEY-DUP-CNT(WS-J) -> WS-KEY-DUP-CNT - bare_m = __import__('re').match(r'^(\w[\w-]*)', field) + bare_m = re.match(r'^(\w[\w-]*)', field) if bare_m: field = bare_m.group(1) return (field, m.group(2), m.group(3).strip().strip("'").strip('"')) @@ -183,10 +185,9 @@ def parse_single_condition(text, fields=None): # Bare field: WS-EOF (no operator) -> WS-EOF = 'Y' if re.match(r'^[A-Z][A-Z0-9_-]*(?:\([^)]*\))?\s*$', text, re.IGNORECASE): - bare = __import__('re').match(r'^[A-Z][A-Z0-9_-]*', text, re.IGNORECASE) - if bare: - return (bare.group(0), '=', 'Y') - return (text, '=', 'Y') + bare = re.match(r'^[A-Z][A-Z0-9_-]*', text, re.IGNORECASE) + field = bare.group(0) if bare else text + return (field, '=', 'Y') return None diff --git a/cobol_testgen/coverage.py b/cobol_testgen/coverage.py index 011d2a8..2540551 100644 --- a/cobol_testgen/coverage.py +++ b/cobol_testgen/coverage.py @@ -182,14 +182,18 @@ def mark_coverage(decision_points, leaf_stats, branch_paths, fields): def _match_constraint(c, parsed): if len(c) != 4: return False - return (c[0] == parsed[0] and c[1] == parsed[1] + c0 = re.sub(r'\s*\(.*?\)\s*$', '', str(c[0])).strip() + p0 = re.sub(r'\s*\(.*?\)\s*$', '', str(parsed[0])).strip() + return (c0 == p0 and c[1] == parsed[1] and str(c[2]) == str(parsed[2])) def _match_leaf(c, leaf): if len(c) != 4: return False - return (c[0] == leaf.field and c[1] == leaf.op + c0 = re.sub(r'\s*\(.*?\)\s*$', '', str(c[0])).strip() + l0 = re.sub(r'\s*\(.*?\)\s*$', '', str(leaf.field)).strip() + return (c0 == l0 and c[1] == leaf.op and str(c[2]) == str(leaf.value))