9
9
10
10
# 3rd party
11
11
import pytest
12
- from bs4 import BeautifulSoup # type: ignore
12
+ import sphinx
13
+ from bs4 import BeautifulSoup , NavigableString # type: ignore
13
14
from pytest_regressions .file_regression import FileRegressionFixture # type: ignore
14
15
from sphinx_toolbox .testing import HTMLRegressionFixture
15
16
@@ -53,6 +54,33 @@ def test(app):
53
54
54
55
# pytestmark = pytest.mark.sphinx('html', testroot='root')
55
56
57
+ return_arrow = " → "
58
+
59
+
60
+ def preprocess_soup (soup : BeautifulSoup ):
61
+
62
+ if sphinx .version_info >= (3 , 5 ): # pragma: no cover
63
+ for em in soup .select ("em.property" ):
64
+ child = '' .join (c .string for c in em .contents )
65
+ for c in em .children :
66
+ c .extract ()
67
+ em .contents = []
68
+ em .insert (0 , child )
69
+
70
+ for dl in soup .select ("dl.py.method dt" ): # .sig.sig-object.py
71
+ if return_arrow in dl .contents :
72
+ arrow_idx = dl .contents .index (return_arrow )
73
+ dl .contents [arrow_idx ] = NavigableString (
74
+ dl .contents [arrow_idx ] + dl .contents [arrow_idx + 1 ].contents [0 ]
75
+ )
76
+ dl .contents [arrow_idx + 1 ].extract ()
77
+
78
+ for dt in soup .select ("span.pre" ):
79
+ dt .replace_with_children ()
80
+
81
+ for dt in soup .select ("span.sig-return" ):
82
+ dt .replace_with (NavigableString (dt .get_text ()))
83
+
56
84
57
85
@pytest .mark .parametrize (
58
86
"page" , [
@@ -64,6 +92,8 @@ def test_index(page: BeautifulSoup, html_regression: HTMLRegressionFixture):
64
92
title = page .find ("h1" ).contents [0 ].strip ()
65
93
assert "autoenum Demo" == title
66
94
95
+ preprocess_soup (page )
96
+
67
97
html_regression .check (page , jinja2 = True )
68
98
69
99
# Now test the directive
@@ -81,10 +111,8 @@ def test_index(page: BeautifulSoup, html_regression: HTMLRegressionFixture):
81
111
assert class_ .find ("dt" )["id" ] == "enum_tools.demo.NoMethods"
82
112
assert class_ .find ("dd" ).findAll ('p' )[0 ].contents [0 ] == "An enumeration of people without any methods."
83
113
84
- assert str (class_ .find ("dd" ).findAll ('p' )[1 ].contents [0 ]) == (
85
- '<code class="xref py py-class docutils literal notranslate">'
86
- '<span class="pre">int</span></code>'
87
- )
114
+ tag = '<code class="xref py py-class docutils literal notranslate">int</code>'
115
+ assert str (class_ .find ("dd" ).findAll ('p' )[1 ].contents [0 ]) == tag
88
116
assert class_ .find ("dd" ).findAll ('p' )[2 ].contents [0 ] == "Valid values are as follows:"
89
117
90
118
attr_count = 0
@@ -147,6 +175,8 @@ def test_flag(page: BeautifulSoup, html_regression: HTMLRegressionFixture):
147
175
title = page .find ("h1" ).contents [0 ].strip ()
148
176
assert "autoenum Demo - Flag" == title
149
177
178
+ preprocess_soup (page )
179
+
150
180
html_regression .check (page , jinja2 = True )
151
181
152
182
# Now test the directive
@@ -164,10 +194,8 @@ def test_flag(page: BeautifulSoup, html_regression: HTMLRegressionFixture):
164
194
165
195
assert class_ .find ("dd" ).findAll ('p' )[0 ].contents [0 ] == "An enumeration of status codes."
166
196
167
- assert str (class_ .find ("dd" ).findAll ('p' )[1 ].contents [0 ]) == (
168
- '<code class="xref py py-class docutils literal notranslate">'
169
- '<span class="pre">int</span></code>'
170
- )
197
+ tag = '<code class="xref py py-class docutils literal notranslate">int</code>'
198
+ assert str (class_ .find ("dd" ).findAll ('p' )[1 ].contents [0 ]) == tag
171
199
assert class_ .find ("dd" ).findAll ('p' )[2 ].contents [0 ] == "Valid values are as follows:"
172
200
173
201
attr_count = 0
@@ -232,6 +260,8 @@ def test_no_member_doc(page: BeautifulSoup, html_regression: HTMLRegressionFixtu
232
260
title = page .find ("h1" ).contents [0 ].strip ()
233
261
assert "autoenum Demo - Members without docstrings" == title
234
262
263
+ preprocess_soup (page )
264
+
235
265
html_regression .check (page , jinja2 = True )
236
266
237
267
# Now test the directive
@@ -247,10 +277,8 @@ def test_no_member_doc(page: BeautifulSoup, html_regression: HTMLRegressionFixtu
247
277
0 ] == "An enumeration of people without any member docstrings."
248
278
249
279
if class_count == 0 :
250
- assert str (class_ .find ("dd" ).findAll ('p' )[1 ].contents [0 ]) == (
251
- '<code class="xref py py-class docutils literal notranslate">'
252
- '<span class="pre">int</span></code>'
253
- )
280
+ tag = '<code class="xref py py-class docutils literal notranslate">int</code>'
281
+ assert str (class_ .find ("dd" ).findAll ('p' )[1 ].contents [0 ]) == tag
254
282
assert class_ .find ("dd" ).findAll ('p' )[2 ].contents [0 ] == "Valid values are as follows:"
255
283
else :
256
284
assert class_ .find ("dd" ).findAll ('p' )[1 ].contents [0 ] == "Valid values are as follows:"
0 commit comments