Skip to content

Commit 0d74c85

Browse files
authored
Extract inner check functions in test_domain_cpp (#12336)
1 parent 172a2aa commit 0d74c85

File tree

1 file changed

+53
-55
lines changed

1 file changed

+53
-55
lines changed

tests/test_domains/test_domain_cpp.py

Lines changed: 53 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,19 +1046,21 @@ def test_domain_cpp_ast_attributes():
10461046
check('enumerator', '{key}Foo [[attr1]] [[attr2]] = 42', {2: '3Foo'})
10471047

10481048

1049+
def check_ast_xref_parsing(target):
1050+
class Config:
1051+
cpp_id_attributes = ["id_attr"]
1052+
cpp_paren_attributes = ["paren_attr"]
1053+
1054+
parser = DefinitionParser(target, location='', config=Config())
1055+
parser.parse_xref_object()
1056+
parser.assert_end()
1057+
1058+
10491059
def test_domain_cpp_ast_xref_parsing():
1050-
def check(target):
1051-
class Config:
1052-
cpp_id_attributes = ["id_attr"]
1053-
cpp_paren_attributes = ["paren_attr"]
1054-
parser = DefinitionParser(target, location=None,
1055-
config=Config())
1056-
ast, isShorthand = parser.parse_xref_object()
1057-
parser.assert_end()
1058-
check('f')
1059-
check('f()')
1060-
check('void f()')
1061-
check('T f()')
1060+
check_ast_xref_parsing('f')
1061+
check_ast_xref_parsing('f()')
1062+
check_ast_xref_parsing('void f()')
1063+
check_ast_xref_parsing('T f()')
10621064

10631065

10641066
@pytest.mark.parametrize(
@@ -1213,18 +1215,12 @@ def test_domain_cpp_build_misuse_of_roles(app, status, warning):
12131215
def test_domain_cpp_build_with_add_function_parentheses_is_True(app, status, warning):
12141216
app.build(force_all=True)
12151217

1216-
def check(spec, text, file):
1217-
pattern = '<li><p>%s<a .*?><code .*?><span .*?>%s</span></code></a></p></li>' % spec
1218-
res = re.search(pattern, text)
1219-
if not res:
1220-
print(f"Pattern\n\t{pattern}\nnot found in {file}")
1221-
raise AssertionError
12221218
rolePatterns = [
1223-
('', 'Sphinx'),
1224-
('', 'Sphinx::version'),
1225-
('', 'version'),
1226-
('', 'List'),
1227-
('', 'MyEnum'),
1219+
'Sphinx',
1220+
'Sphinx::version',
1221+
'version',
1222+
'List',
1223+
'MyEnum',
12281224
]
12291225
parenPatterns = [
12301226
('ref function without parens ', r'paren_1\(\)'),
@@ -1237,35 +1233,33 @@ def check(spec, text, file):
12371233
('ref op call with parens, explicit title ', 'paren_8_title'),
12381234
]
12391235

1240-
f = 'roles.html'
1241-
t = (app.outdir / f).read_text(encoding='utf8')
1242-
for s in rolePatterns:
1243-
check(s, t, f)
1244-
for s in parenPatterns:
1245-
check(s, t, f)
1236+
text = (app.outdir / 'roles.html').read_text(encoding='utf8')
1237+
for ref_text in rolePatterns:
1238+
pattern = f'<li><p><a .*?><code .*?><span .*?>{ref_text}</span></code></a></p></li>'
1239+
match = re.search(pattern, text)
1240+
assert match is not None, f"Pattern not found in roles.html:\n\t{pattern}"
1241+
for (desc_text, ref_text) in parenPatterns:
1242+
pattern = f'<li><p>{desc_text}<a .*?><code .*?><span .*?>{ref_text}</span></code></a></p></li>'
1243+
match = re.search(pattern, text)
1244+
assert match is not None, f"Pattern not found in roles.html:\n\t{pattern}"
12461245

1247-
f = 'any-role.html'
1248-
t = (app.outdir / f).read_text(encoding='utf8')
1249-
for s in parenPatterns:
1250-
check(s, t, f)
1246+
text = (app.outdir / 'any-role.html').read_text(encoding='utf8')
1247+
for (desc_text, ref_text) in parenPatterns:
1248+
pattern = f'<li><p>{desc_text}<a .*?><code .*?><span .*?>{ref_text}</span></code></a></p></li>'
1249+
match = re.search(pattern, text)
1250+
assert match is not None, f"Pattern not found in any-role.html:\n\t{pattern}"
12511251

12521252

12531253
@pytest.mark.sphinx(testroot='domain-cpp', confoverrides={'add_function_parentheses': False})
12541254
def test_domain_cpp_build_with_add_function_parentheses_is_False(app, status, warning):
12551255
app.build(force_all=True)
12561256

1257-
def check(spec, text, file):
1258-
pattern = '<li><p>%s<a .*?><code .*?><span .*?>%s</span></code></a></p></li>' % spec
1259-
res = re.search(pattern, text)
1260-
if not res:
1261-
print(f"Pattern\n\t{pattern}\nnot found in {file}")
1262-
raise AssertionError
12631257
rolePatterns = [
1264-
('', 'Sphinx'),
1265-
('', 'Sphinx::version'),
1266-
('', 'version'),
1267-
('', 'List'),
1268-
('', 'MyEnum'),
1258+
'Sphinx',
1259+
'Sphinx::version',
1260+
'version',
1261+
'List',
1262+
'MyEnum',
12691263
]
12701264
parenPatterns = [
12711265
('ref function without parens ', 'paren_1'),
@@ -1278,17 +1272,21 @@ def check(spec, text, file):
12781272
('ref op call with parens, explicit title ', 'paren_8_title'),
12791273
]
12801274

1281-
f = 'roles.html'
1282-
t = (app.outdir / f).read_text(encoding='utf8')
1283-
for s in rolePatterns:
1284-
check(s, t, f)
1285-
for s in parenPatterns:
1286-
check(s, t, f)
1287-
1288-
f = 'any-role.html'
1289-
t = (app.outdir / f).read_text(encoding='utf8')
1290-
for s in parenPatterns:
1291-
check(s, t, f)
1275+
text = (app.outdir / 'roles.html').read_text(encoding='utf8')
1276+
for ref_text in rolePatterns:
1277+
pattern = f'<li><p><a .*?><code .*?><span .*?>{ref_text}</span></code></a></p></li>'
1278+
match = re.search(pattern, text)
1279+
assert match is not None, f"Pattern not found in roles.html:\n\t{pattern}"
1280+
for (desc_text, ref_text) in parenPatterns:
1281+
pattern = f'<li><p>{desc_text}<a .*?><code .*?><span .*?>{ref_text}</span></code></a></p></li>'
1282+
match = re.search(pattern, text)
1283+
assert match is not None, f"Pattern not found in roles.html:\n\t{pattern}"
1284+
1285+
text = (app.outdir / 'any-role.html').read_text(encoding='utf8')
1286+
for (desc_text, ref_text) in parenPatterns:
1287+
pattern = f'<li><p>{desc_text}<a .*?><code .*?><span .*?>{ref_text}</span></code></a></p></li>'
1288+
match = re.search(pattern, text)
1289+
assert match is not None, f"Pattern not found in any-role.html:\n\t{pattern}"
12921290

12931291

12941292
@pytest.mark.sphinx(testroot='domain-cpp')

0 commit comments

Comments
 (0)