Skip to content

Commit 841f2bd

Browse files
committed
Remove unneeded uses of flat_dict()
1 parent 59cf4d4 commit 841f2bd

File tree

1 file changed

+143
-159
lines changed

1 file changed

+143
-159
lines changed

tests/test_build_html.py

Lines changed: 143 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import os
55
import posixpath
66
import re
7-
from itertools import chain, cycle
87
from pathlib import Path
98
from unittest.mock import ANY, call, patch
109

@@ -59,13 +58,8 @@ def parse(fname):
5958
etree_cache.clear()
6059

6160

62-
def flat_dict(d):
63-
return chain.from_iterable(
64-
[
65-
zip(cycle([fname]), values)
66-
for fname, values in d.items()
67-
],
68-
)
61+
def flat_dict(d: dict[str, list[str]]):
62+
return ((fname, value) for fname, values in d.items() for value in values)
6963

7064

7165
def tail_check(check):
@@ -409,7 +403,6 @@ def test_html4_error(make_app, tmp_path):
409403
@pytest.mark.test_params(shared_result='test_build_html_output')
410404
def test_html5_output(app, cached_etree_parse, fname, expect):
411405
app.build()
412-
print(app.outdir / fname)
413406
check_xpath(cached_etree_parse(app.outdir / fname), fname, *expect)
414407

415408

@@ -440,7 +433,6 @@ def test_html5_output(app, cached_etree_parse, fname, expect):
440433
@pytest.mark.test_params(shared_result='test_build_html_output_docutils18')
441434
def test_docutils_output(app, cached_etree_parse, fname, expect):
442435
app.build()
443-
print(app.outdir / fname)
444436
check_xpath(cached_etree_parse(app.outdir / fname), fname, *expect)
445437

446438

@@ -565,48 +557,46 @@ def test_tocdepth(app, cached_etree_parse, fname, expect):
565557
check_xpath(cached_etree_parse(app.outdir / fname), fname, *expect)
566558

567559

568-
@pytest.mark.parametrize(("fname", "expect"), flat_dict({
569-
'index.html': [
570-
(".//li[@class='toctree-l3']/a", '1.1.1. Foo A1', True),
571-
(".//li[@class='toctree-l3']/a", '1.2.1. Foo B1', True),
572-
(".//li[@class='toctree-l3']/a", '2.1.1. Bar A1', False),
573-
(".//li[@class='toctree-l3']/a", '2.2.1. Bar B1', False),
574-
575-
# index.rst
576-
(".//h1", 'test-tocdepth', True),
577-
578-
# foo.rst
579-
(".//h2", 'Foo', True),
580-
(".//h3", 'Foo A', True),
581-
(".//h4", 'Foo A1', True),
582-
(".//h3", 'Foo B', True),
583-
(".//h4", 'Foo B1', True),
584-
(".//h2//span[@class='section-number']", '1. ', True),
585-
(".//h3//span[@class='section-number']", '1.1. ', True),
586-
(".//h4//span[@class='section-number']", '1.1.1. ', True),
587-
(".//h3//span[@class='section-number']", '1.2. ', True),
588-
(".//h4//span[@class='section-number']", '1.2.1. ', True),
589-
590-
# bar.rst
591-
(".//h2", 'Bar', True),
592-
(".//h3", 'Bar A', True),
593-
(".//h3", 'Bar B', True),
594-
(".//h4", 'Bar B1', True),
595-
(".//h2//span[@class='section-number']", '2. ', True),
596-
(".//h3//span[@class='section-number']", '2.1. ', True),
597-
(".//h3//span[@class='section-number']", '2.2. ', True),
598-
(".//h4//span[@class='section-number']", '2.2.1. ', True),
599-
600-
# baz.rst
601-
(".//h4", 'Baz A', True),
602-
(".//h4//span[@class='section-number']", '2.1.1. ', True),
603-
],
604-
}))
560+
@pytest.mark.parametrize("expect", [
561+
(".//li[@class='toctree-l3']/a", '1.1.1. Foo A1', True),
562+
(".//li[@class='toctree-l3']/a", '1.2.1. Foo B1', True),
563+
(".//li[@class='toctree-l3']/a", '2.1.1. Bar A1', False),
564+
(".//li[@class='toctree-l3']/a", '2.2.1. Bar B1', False),
565+
566+
# index.rst
567+
(".//h1", 'test-tocdepth', True),
568+
569+
# foo.rst
570+
(".//h2", 'Foo', True),
571+
(".//h3", 'Foo A', True),
572+
(".//h4", 'Foo A1', True),
573+
(".//h3", 'Foo B', True),
574+
(".//h4", 'Foo B1', True),
575+
(".//h2//span[@class='section-number']", '1. ', True),
576+
(".//h3//span[@class='section-number']", '1.1. ', True),
577+
(".//h4//span[@class='section-number']", '1.1.1. ', True),
578+
(".//h3//span[@class='section-number']", '1.2. ', True),
579+
(".//h4//span[@class='section-number']", '1.2.1. ', True),
580+
581+
# bar.rst
582+
(".//h2", 'Bar', True),
583+
(".//h3", 'Bar A', True),
584+
(".//h3", 'Bar B', True),
585+
(".//h4", 'Bar B1', True),
586+
(".//h2//span[@class='section-number']", '2. ', True),
587+
(".//h3//span[@class='section-number']", '2.1. ', True),
588+
(".//h3//span[@class='section-number']", '2.2. ', True),
589+
(".//h4//span[@class='section-number']", '2.2.1. ', True),
590+
591+
# baz.rst
592+
(".//h4", 'Baz A', True),
593+
(".//h4//span[@class='section-number']", '2.1.1. ', True),
594+
])
605595
@pytest.mark.sphinx('singlehtml', testroot='tocdepth')
606596
@pytest.mark.test_params(shared_result='test_build_html_tocdepth')
607-
def test_tocdepth_singlehtml(app, cached_etree_parse, fname, expect):
597+
def test_tocdepth_singlehtml(app, cached_etree_parse, expect):
608598
app.build()
609-
check_xpath(cached_etree_parse(app.outdir / fname), fname, *expect)
599+
check_xpath(cached_etree_parse(app.outdir / 'index.html'), 'index.html', *expect)
610600

611601

612602
@pytest.mark.sphinx('html', testroot='numfig')
@@ -1043,96 +1033,92 @@ def test_numfig_with_secnum_depth(app, cached_etree_parse, fname, expect):
10431033
check_xpath(cached_etree_parse(app.outdir / fname), fname, *expect)
10441034

10451035

1046-
@pytest.mark.parametrize(("fname", "expect"), flat_dict({
1047-
'index.html': [
1048-
(FIGURE_CAPTION + "/span[@class='caption-number']", '^Fig. 1 $', True),
1049-
(FIGURE_CAPTION + "/span[@class='caption-number']", '^Fig. 2 $', True),
1050-
(".//table/caption/span[@class='caption-number']",
1051-
'^Table 1 $', True),
1052-
(".//table/caption/span[@class='caption-number']",
1053-
'^Table 2 $', True),
1054-
(".//div[@class='code-block-caption']/"
1055-
"span[@class='caption-number']", '^Listing 1 $', True),
1056-
(".//div[@class='code-block-caption']/"
1057-
"span[@class='caption-number']", '^Listing 2 $', True),
1058-
(".//li/p/a/span", '^Fig. 1$', True),
1059-
(".//li/p/a/span", '^Figure2.2$', True),
1060-
(".//li/p/a/span", '^Table 1$', True),
1061-
(".//li/p/a/span", '^Table:2.2$', True),
1062-
(".//li/p/a/span", '^Listing 1$', True),
1063-
(".//li/p/a/span", '^Code-2.2$', True),
1064-
(".//li/p/a/span", '^Section.1$', True),
1065-
(".//li/p/a/span", '^Section.2.1$', True),
1066-
(".//li/p/a/span", '^Fig.1 should be Fig.1$', True),
1067-
(".//li/p/a/span", '^Sect.1 Foo$', True),
1068-
(FIGURE_CAPTION + "/span[@class='caption-number']", '^Fig. 1.1 $', True),
1069-
(FIGURE_CAPTION + "/span[@class='caption-number']", '^Fig. 1.2 $', True),
1070-
(FIGURE_CAPTION + "/span[@class='caption-number']", '^Fig. 1.3 $', True),
1071-
(FIGURE_CAPTION + "/span[@class='caption-number']", '^Fig. 1.4 $', True),
1072-
(".//table/caption/span[@class='caption-number']",
1073-
'^Table 1.1 $', True),
1074-
(".//table/caption/span[@class='caption-number']",
1075-
'^Table 1.2 $', True),
1076-
(".//table/caption/span[@class='caption-number']",
1077-
'^Table 1.3 $', True),
1078-
(".//table/caption/span[@class='caption-number']",
1079-
'^Table 1.4 $', True),
1080-
(".//div[@class='code-block-caption']/"
1081-
"span[@class='caption-number']", '^Listing 1.1 $', True),
1082-
(".//div[@class='code-block-caption']/"
1083-
"span[@class='caption-number']", '^Listing 1.2 $', True),
1084-
(".//div[@class='code-block-caption']/"
1085-
"span[@class='caption-number']", '^Listing 1.3 $', True),
1086-
(".//div[@class='code-block-caption']/"
1087-
"span[@class='caption-number']", '^Listing 1.4 $', True),
1088-
(FIGURE_CAPTION + "/span[@class='caption-number']", '^Fig. 2.1 $', True),
1089-
(FIGURE_CAPTION + "/span[@class='caption-number']", '^Fig. 2.3 $', True),
1090-
(FIGURE_CAPTION + "/span[@class='caption-number']", '^Fig. 2.4 $', True),
1091-
(".//table/caption/span[@class='caption-number']",
1092-
'^Table 2.1 $', True),
1093-
(".//table/caption/span[@class='caption-number']",
1094-
'^Table 2.3 $', True),
1095-
(".//table/caption/span[@class='caption-number']",
1096-
'^Table 2.4 $', True),
1097-
(".//div[@class='code-block-caption']/"
1098-
"span[@class='caption-number']", '^Listing 2.1 $', True),
1099-
(".//div[@class='code-block-caption']/"
1100-
"span[@class='caption-number']", '^Listing 2.3 $', True),
1101-
(".//div[@class='code-block-caption']/"
1102-
"span[@class='caption-number']", '^Listing 2.4 $', True),
1103-
(FIGURE_CAPTION + "/span[@class='caption-number']", '^Fig. 2.2 $', True),
1104-
(".//table/caption/span[@class='caption-number']",
1105-
'^Table 2.2 $', True),
1106-
(".//div[@class='code-block-caption']/"
1107-
"span[@class='caption-number']", '^Listing 2.2 $', True),
1108-
],
1109-
}))
1036+
@pytest.mark.parametrize("expect", [
1037+
(FIGURE_CAPTION + "/span[@class='caption-number']", '^Fig. 1 $', True),
1038+
(FIGURE_CAPTION + "/span[@class='caption-number']", '^Fig. 2 $', True),
1039+
(".//table/caption/span[@class='caption-number']",
1040+
'^Table 1 $', True),
1041+
(".//table/caption/span[@class='caption-number']",
1042+
'^Table 2 $', True),
1043+
(".//div[@class='code-block-caption']/"
1044+
"span[@class='caption-number']", '^Listing 1 $', True),
1045+
(".//div[@class='code-block-caption']/"
1046+
"span[@class='caption-number']", '^Listing 2 $', True),
1047+
(".//li/p/a/span", '^Fig. 1$', True),
1048+
(".//li/p/a/span", '^Figure2.2$', True),
1049+
(".//li/p/a/span", '^Table 1$', True),
1050+
(".//li/p/a/span", '^Table:2.2$', True),
1051+
(".//li/p/a/span", '^Listing 1$', True),
1052+
(".//li/p/a/span", '^Code-2.2$', True),
1053+
(".//li/p/a/span", '^Section.1$', True),
1054+
(".//li/p/a/span", '^Section.2.1$', True),
1055+
(".//li/p/a/span", '^Fig.1 should be Fig.1$', True),
1056+
(".//li/p/a/span", '^Sect.1 Foo$', True),
1057+
(FIGURE_CAPTION + "/span[@class='caption-number']", '^Fig. 1.1 $', True),
1058+
(FIGURE_CAPTION + "/span[@class='caption-number']", '^Fig. 1.2 $', True),
1059+
(FIGURE_CAPTION + "/span[@class='caption-number']", '^Fig. 1.3 $', True),
1060+
(FIGURE_CAPTION + "/span[@class='caption-number']", '^Fig. 1.4 $', True),
1061+
(".//table/caption/span[@class='caption-number']",
1062+
'^Table 1.1 $', True),
1063+
(".//table/caption/span[@class='caption-number']",
1064+
'^Table 1.2 $', True),
1065+
(".//table/caption/span[@class='caption-number']",
1066+
'^Table 1.3 $', True),
1067+
(".//table/caption/span[@class='caption-number']",
1068+
'^Table 1.4 $', True),
1069+
(".//div[@class='code-block-caption']/"
1070+
"span[@class='caption-number']", '^Listing 1.1 $', True),
1071+
(".//div[@class='code-block-caption']/"
1072+
"span[@class='caption-number']", '^Listing 1.2 $', True),
1073+
(".//div[@class='code-block-caption']/"
1074+
"span[@class='caption-number']", '^Listing 1.3 $', True),
1075+
(".//div[@class='code-block-caption']/"
1076+
"span[@class='caption-number']", '^Listing 1.4 $', True),
1077+
(FIGURE_CAPTION + "/span[@class='caption-number']", '^Fig. 2.1 $', True),
1078+
(FIGURE_CAPTION + "/span[@class='caption-number']", '^Fig. 2.3 $', True),
1079+
(FIGURE_CAPTION + "/span[@class='caption-number']", '^Fig. 2.4 $', True),
1080+
(".//table/caption/span[@class='caption-number']",
1081+
'^Table 2.1 $', True),
1082+
(".//table/caption/span[@class='caption-number']",
1083+
'^Table 2.3 $', True),
1084+
(".//table/caption/span[@class='caption-number']",
1085+
'^Table 2.4 $', True),
1086+
(".//div[@class='code-block-caption']/"
1087+
"span[@class='caption-number']", '^Listing 2.1 $', True),
1088+
(".//div[@class='code-block-caption']/"
1089+
"span[@class='caption-number']", '^Listing 2.3 $', True),
1090+
(".//div[@class='code-block-caption']/"
1091+
"span[@class='caption-number']", '^Listing 2.4 $', True),
1092+
(FIGURE_CAPTION + "/span[@class='caption-number']", '^Fig. 2.2 $', True),
1093+
(".//table/caption/span[@class='caption-number']",
1094+
'^Table 2.2 $', True),
1095+
(".//div[@class='code-block-caption']/"
1096+
"span[@class='caption-number']", '^Listing 2.2 $', True),
1097+
])
11101098
@pytest.mark.sphinx('singlehtml', testroot='numfig', confoverrides={'numfig': True})
11111099
@pytest.mark.test_params(shared_result='test_build_html_numfig_on')
1112-
def test_numfig_with_singlehtml(app, cached_etree_parse, fname, expect):
1100+
def test_numfig_with_singlehtml(app, cached_etree_parse, expect):
11131101
app.build()
1114-
check_xpath(cached_etree_parse(app.outdir / fname), fname, *expect)
1115-
1116-
1117-
@pytest.mark.parametrize(("fname", "expect"), flat_dict({
1118-
'index.html': [
1119-
(FIGURE_CAPTION + "//span[@class='caption-number']", "Fig. 1", True),
1120-
(FIGURE_CAPTION + "//span[@class='caption-number']", "Fig. 2", True),
1121-
(FIGURE_CAPTION + "//span[@class='caption-number']", "Fig. 3", True),
1122-
(".//div//span[@class='caption-number']", "No.1 ", True),
1123-
(".//div//span[@class='caption-number']", "No.2 ", True),
1124-
(".//li/p/a/span", 'Fig. 1', True),
1125-
(".//li/p/a/span", 'Fig. 2', True),
1126-
(".//li/p/a/span", 'Fig. 3', True),
1127-
(".//li/p/a/span", 'No.1', True),
1128-
(".//li/p/a/span", 'No.2', True),
1129-
],
1130-
}))
1102+
check_xpath(cached_etree_parse(app.outdir / 'index.html'), 'index.html', *expect)
1103+
1104+
1105+
@pytest.mark.parametrize("expect", [
1106+
(FIGURE_CAPTION + "//span[@class='caption-number']", "Fig. 1", True),
1107+
(FIGURE_CAPTION + "//span[@class='caption-number']", "Fig. 2", True),
1108+
(FIGURE_CAPTION + "//span[@class='caption-number']", "Fig. 3", True),
1109+
(".//div//span[@class='caption-number']", "No.1 ", True),
1110+
(".//div//span[@class='caption-number']", "No.2 ", True),
1111+
(".//li/p/a/span", 'Fig. 1', True),
1112+
(".//li/p/a/span", 'Fig. 2', True),
1113+
(".//li/p/a/span", 'Fig. 3', True),
1114+
(".//li/p/a/span", 'No.1', True),
1115+
(".//li/p/a/span", 'No.2', True),
1116+
])
11311117
@pytest.mark.sphinx('html', testroot='add_enumerable_node',
11321118
srcdir='test_enumerable_node')
1133-
def test_enumerable_node(app, cached_etree_parse, fname, expect):
1119+
def test_enumerable_node(app, cached_etree_parse, expect):
11341120
app.build()
1135-
check_xpath(cached_etree_parse(app.outdir / fname), fname, *expect)
1121+
check_xpath(cached_etree_parse(app.outdir / 'index.html'), 'index.html', *expect)
11361122

11371123

11381124
@pytest.mark.sphinx('html', testroot='html_assets')
@@ -1368,34 +1354,32 @@ def test_html_raw_directive(app, status, warning):
13681354
assert '<p>LaTeX: abc ghi</p>' in result
13691355

13701356

1371-
@pytest.mark.parametrize(("fname", "expect"), flat_dict({
1372-
'index.html': [
1373-
(".//link[@href='_static/persistent.css']"
1374-
"[@rel='stylesheet']", '', True),
1375-
(".//link[@href='_static/default.css']"
1376-
"[@rel='stylesheet']"
1377-
"[@title='Default']", '', True),
1378-
(".//link[@href='_static/alternate1.css']"
1379-
"[@rel='alternate stylesheet']"
1380-
"[@title='Alternate']", '', True),
1381-
(".//link[@href='_static/alternate2.css']"
1382-
"[@rel='alternate stylesheet']", '', True),
1383-
(".//link[@href='_static/more_persistent.css']"
1384-
"[@rel='stylesheet']", '', True),
1385-
(".//link[@href='_static/more_default.css']"
1386-
"[@rel='stylesheet']"
1387-
"[@title='Default']", '', True),
1388-
(".//link[@href='_static/more_alternate1.css']"
1389-
"[@rel='alternate stylesheet']"
1390-
"[@title='Alternate']", '', True),
1391-
(".//link[@href='_static/more_alternate2.css']"
1392-
"[@rel='alternate stylesheet']", '', True),
1393-
],
1394-
}))
1357+
@pytest.mark.parametrize("expect", [
1358+
(".//link[@href='_static/persistent.css']"
1359+
"[@rel='stylesheet']", '', True),
1360+
(".//link[@href='_static/default.css']"
1361+
"[@rel='stylesheet']"
1362+
"[@title='Default']", '', True),
1363+
(".//link[@href='_static/alternate1.css']"
1364+
"[@rel='alternate stylesheet']"
1365+
"[@title='Alternate']", '', True),
1366+
(".//link[@href='_static/alternate2.css']"
1367+
"[@rel='alternate stylesheet']", '', True),
1368+
(".//link[@href='_static/more_persistent.css']"
1369+
"[@rel='stylesheet']", '', True),
1370+
(".//link[@href='_static/more_default.css']"
1371+
"[@rel='stylesheet']"
1372+
"[@title='Default']", '', True),
1373+
(".//link[@href='_static/more_alternate1.css']"
1374+
"[@rel='alternate stylesheet']"
1375+
"[@title='Alternate']", '', True),
1376+
(".//link[@href='_static/more_alternate2.css']"
1377+
"[@rel='alternate stylesheet']", '', True),
1378+
])
13951379
@pytest.mark.sphinx('html', testroot='stylesheets')
1396-
def test_alternate_stylesheets(app, cached_etree_parse, fname, expect):
1380+
def test_alternate_stylesheets(app, cached_etree_parse, expect):
13971381
app.build()
1398-
check_xpath(cached_etree_parse(app.outdir / fname), fname, *expect)
1382+
check_xpath(cached_etree_parse(app.outdir / 'index.html'), 'index.html', *expect)
13991383

14001384

14011385
@pytest.mark.sphinx('html', testroot='html_style')

0 commit comments

Comments
 (0)