Skip to content

Commit b3b1f30

Browse files
committed
test: optim unit testings
1 parent 6ce2f09 commit b3b1f30

File tree

4 files changed

+76
-65
lines changed

4 files changed

+76
-65
lines changed

.pre-commit-config.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ repos:
88
"--recursive",
99
"--in-place",
1010
"--remove-all-unused-imports",
11-
"--remove-unused-variable",
1211
"--ignore-init-module-imports",
1312
]
1413
files: \.py$

rapid_table/table_matcher/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def deal_duplicate_bb(thead_part):
7676
r'<td colspan="(\d)+" rowspan="(\d)+">(.+?)</td>|'
7777
r'<td rowspan="(\d)+">(.+?)</td>|'
7878
r'<td colspan="(\d)+">(.+?)</td>|'
79-
r'<td>(.*?)</td>'
79+
r"<td>(.*?)</td>"
8080
)
8181
td_iter = re.finditer(td_pattern, thead_part)
8282
td_list = [t.group() for t in td_iter]

tests/table_matcher/utils.py

Lines changed: 0 additions & 63 deletions
This file was deleted.

tests/test_table_matcher.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# -*- encoding: utf-8 -*-
2+
import sys
3+
import warnings
4+
5+
import pytest
6+
7+
8+
@pytest.mark.skipif(
9+
sys.version_info.major == 3 and sys.version_info.minor < 12,
10+
reason="仅在python>=3.12时测试",
11+
)
12+
def test_regex_syntax_warning():
13+
"""测试捕获正则表达式中无效转义序列产生的 SyntaxWarning"""
14+
15+
with warnings.catch_warnings(record=True) as w:
16+
warnings.simplefilter("always")
17+
18+
# 使用 compile() 来编译包含无效转义序列的代码,这会触发 SyntaxWarning
19+
code_with_invalid_escape = """
20+
import re
21+
thead_part = '<td></td> rowspan="2"></b></td>'
22+
isolate_pattern = (
23+
'<td></td> rowspan="(\d)+" colspan="(\d)+"></b></td>|'
24+
'<td></td> colspan="(\d)+" rowspan="(\d)+"></b></td>|'
25+
'<td></td> rowspan="(\d)+"></b></td>|'
26+
'<td></td> colspan="(\d)+"></b></td>'
27+
)
28+
re.finditer(isolate_pattern, thead_part)
29+
"""
30+
31+
# 编译代码时会产生 SyntaxWarning
32+
compile(code_with_invalid_escape, "<string>", "exec")
33+
34+
# 检查是否捕获到 SyntaxWarning
35+
syntax_warnings = [
36+
warn for warn in w if issubclass(warn.category, SyntaxWarning)
37+
]
38+
assert (
39+
len(syntax_warnings) > 0
40+
), f"未捕获到 SyntaxWarning: {[str(warn.message) for warn in w]}"
41+
42+
# 应该捕获到无效转义序列的警告
43+
for warning in syntax_warnings:
44+
assert "invalid escape sequence" in str(warning.message)
45+
46+
47+
@pytest.mark.skipif(
48+
sys.version_info.major == 3 and sys.version_info.minor < 12,
49+
reason="仅在python>=3.12时测试",
50+
)
51+
def test_correct_regex_pattern():
52+
with warnings.catch_warnings(record=True) as w:
53+
warnings.simplefilter("always")
54+
55+
# 这不会触发 SyntaxWarning
56+
code_with_invalid_escape = """
57+
import re
58+
thead_part = '<td></td> rowspan="2"></b></td>'
59+
isolate_pattern_raw = (
60+
r'<td></td> rowspan="(\d)+" colspan="(\d)+"></b></td>|'
61+
r'<td></td> colspan="(\d)+" rowspan="(\d)+"></b></td>|'
62+
r'<td></td> rowspan="(\d)+"></b></td>|'
63+
r'<td></td> colspan="(\d)+"></b></td>'
64+
)
65+
re.finditer(isolate_pattern_raw, thead_part)
66+
"""
67+
compile(code_with_invalid_escape, "<string>", "exec")
68+
69+
# 检查是否捕获到 SyntaxWarning
70+
syntax_warnings = [
71+
warn for warn in w if issubclass(warn.category, SyntaxWarning)
72+
]
73+
assert (
74+
len(syntax_warnings) == 0
75+
), f"正常写法捕获到 SyntaxWarning: {[str(warn.message) for warn in w]}"

0 commit comments

Comments
 (0)