File: test_autoenum.py

package info (click to toggle)
python-enum-tools 0.12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 656 kB
  • sloc: python: 1,447; makefile: 4
file content (363 lines) | stat: -rw-r--r-- 10,450 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# Based on https://github.com/pauleveritt/customizing_sphinx/tree/master/tests/integration
# and https://github.com/sphinx-doc/sphinx/issues/7008

# stdlib
import sys
from decimal import Decimal
from enum import Enum
from pathlib import Path

# 3rd party
import pytest
import sphinx
from bs4 import BeautifulSoup, NavigableString  # type: ignore[import]
from sphinx.application import Sphinx
from sphinx_toolbox.testing import HTMLRegressionFixture

# this package
from enum_tools.autoenum import EnumDocumenter

NEW_ENUM_REPR = sys.version_info >= (3, 13)

xfail_312 = pytest.mark.xfail(
		reason="Python 3.12 behaviour has not been finalised yet.",
		condition=sys.version_info[:2] == (3, 13) and sys.version_info.releaselevel == "alpha"
		)


@pytest.mark.parametrize("obj", [
		"abcdefg",
		b"abcdefg",
		b"\x00\x01",
		12345,
		123.45,
		Decimal(123.45),
		Path('.'),
		])
def test_cannot_document_member(obj: object):
	assert not EnumDocumenter.can_document_member(obj, '', True, '')


class MyEnum(Enum):
	foo = 1
	bar = 2


def test_can_document_member():
	assert EnumDocumenter.can_document_member(MyEnum, '', True, '')


def test(app: Sphinx) -> None:
	# app is a Sphinx application object for default sphinx project (`tests/roots/test-root`).
	app.build()


# @pytest.mark.sphinx(buildername='latex')
# def test_latex(app):
# 	# latex builder is chosen here.
# 	app.build()

# pytestmark = pytest.mark.sphinx('html', testroot='root')

return_arrow = " → "


def preprocess_soup(soup: BeautifulSoup) -> None:

	if sphinx.version_info >= (3, 5):  # pragma: no cover
		for em in soup.select("em.property"):
			child = ''.join(c.string for c in em.contents)
			for c in em.children:
				c.extract()
			em.contents = []
			em.insert(0, child)

		for dl in soup.select("dl.py.method dt"):  # .sig.sig-object.py
			if return_arrow in dl.contents:
				arrow_idx = dl.contents.index(return_arrow)
				dl.contents[arrow_idx] = NavigableString(
						dl.contents[arrow_idx] + dl.contents[arrow_idx + 1].contents[0]
						)
				dl.contents[arrow_idx + 1].extract()

	for dt in soup.select("span.pre"):
		dt.replace_with_children()

	for dt in soup.select("span.sig-return"):
		dt.replace_with(NavigableString(dt.get_text()))

	for div in soup.findAll("script"):
		if div.get("src"):
			div["src"] = div["src"].split("?v=")[0]
			print(div["src"])


@xfail_312
@pytest.mark.parametrize(
		"page", [
				"index.html",
				], indirect=True
		)
def test_index(page: BeautifulSoup, html_regression: HTMLRegressionFixture):
	# Make sure the page title is what you expect
	title = page.find("h1").contents[0].strip()
	assert "autoenum Demo" == title

	preprocess_soup(page)

	html_regression.check(page, jinja2=True)

	# Now test the directive

	class_count = 0

	for class_ in page.findAll("dl"):
		if "enum" not in class_["class"]:
			continue

		if class_count == 0:
			assert class_.find("dt")["id"] == "enum_tools.demo.People"
			assert class_.find("dd").findAll('p')[0].contents[0] == "An enumeration of people."
		elif class_count == 1:
			assert class_.find("dt")["id"] == "enum_tools.demo.NoMethods"
			assert class_.find("dd").findAll('p')[0].contents[0] == "An enumeration of people without any methods."

		tag = '<code class="xref py py-class docutils literal notranslate">int</code>'
		assert str(class_.find("dd").findAll('p')[1].contents[0]) == tag
		assert class_.find("dd").findAll('p')[2].contents[0] == "Valid values are as follows:"

		attr_count = 0

		for attr in class_.findAll("dl"):
			if "attribute" not in attr["class"]:
				continue

			if class_count == 0:
				class_name = "People"
			else:
				class_name = "NoMethods"

			if attr_count == 0:
				assert attr.find("dt")["id"] == f"enum_tools.demo.{class_name}.Bob"

				if NEW_ENUM_REPR:
					assert attr.find("dt").em.contents[0] == f" = {class_name}.Bob"
				else:
					assert attr.find("dt").em.contents[0] == f" = <{class_name}.Bob: 1>"

				assert str(attr.find("dd").contents[0]) == "<p>A person called Bob</p>"

			elif attr_count == 1:
				assert attr.find("dt")["id"] == f"enum_tools.demo.{class_name}.Alice"

				if NEW_ENUM_REPR:
					assert attr.find("dt").em.contents[0] == f" = {class_name}.Alice"
				else:
					assert attr.find("dt").em.contents[0] == f" = <{class_name}.Alice: 2>"

				assert str(attr.find("dd").contents[0]) == "<p>A person called Alice</p>"

			elif attr_count == 2:
				assert attr.find("dt")["id"] == f"enum_tools.demo.{class_name}.Carol"

				if NEW_ENUM_REPR:
					assert attr.find("dt").em.contents[0] == f" = {class_name}.Carol"
				else:
					assert attr.find("dt").em.contents[0] == f" = <{class_name}.Carol: 3>"

				if class_count == 0:
					assert str(attr.find("dd").contents[0]) == "<p>A person called Carol.</p>"
					assert str(attr.find("dd").contents[1]) == '\n'
					assert str(attr.find("dd").contents[2]) == "<p>This is a multiline docstring.</p>"
				else:
					assert str(attr.find("dd").contents[0]) == "<p>A person called Carol</p>"

			elif attr_count == 3:
				assert attr.find("dt")["id"] == f"enum_tools.demo.{class_name}.Dennis"

				if NEW_ENUM_REPR:
					assert attr.find("dt").em.contents[0] == f" = {class_name}.Dennis"
				else:
					assert attr.find("dt").em.contents[0] == f" = <{class_name}.Dennis: 4>"

				assert str(attr.find("dd").contents[0]) == "<p>A person called Dennis</p>"

			attr_count += 1

		class_count += 1

	# print(page)

	assert class_count == 2


@xfail_312
@pytest.mark.parametrize(
		"page", [
				"flag.html",
				], indirect=True
		)
def test_flag(page: BeautifulSoup, html_regression: HTMLRegressionFixture):
	# Make sure the page title is what you expect
	title = page.find("h1").contents[0].strip()
	assert "autoenum Demo - Flag" == title

	preprocess_soup(page)

	html_regression.check(page, jinja2=True)

	# Now test the directive

	class_count = 0

	for class_ in page.findAll("dl"):
		if "flag" not in class_["class"]:
			continue

		if class_count == 0:
			assert class_.find("dt")["id"] == "enum_tools.demo.StatusFlags"
		elif class_count == 1:
			assert class_.find("dt")["id"] == "id0"

		assert class_.find("dd").findAll('p')[0].contents[0] == "An enumeration of status codes."

		tag = '<code class="xref py py-class docutils literal notranslate">int</code>'
		assert str(class_.find("dd").findAll('p')[1].contents[0]) == tag
		assert class_.find("dd").findAll('p')[2].contents[0] == "Valid values are as follows:"

		attr_count = 0

		for attr in class_.findAll("dl"):
			if "attribute" not in attr["class"]:
				continue

			if attr_count == 0:
				if class_count == 0:
					assert attr.find("dt")["id"] == "enum_tools.demo.StatusFlags.Running"
				elif class_count == 1:
					assert attr.find("dt")["id"] == "id1"

				if NEW_ENUM_REPR:
					assert attr.find("dt").em.contents[0] == " = StatusFlags.Running"
				else:
					assert attr.find("dt").em.contents[0] == " = <StatusFlags.Running: 1>"

				assert str(attr.find("dd").contents[0]) == "<p>The system is running.</p>"
			elif attr_count == 1:
				if class_count == 0:
					assert attr.find("dt")["id"] == "enum_tools.demo.StatusFlags.Stopped"
				elif class_count == 1:
					assert attr.find("dt")["id"] == "id2"

				if NEW_ENUM_REPR:
					assert attr.find("dt").em.contents[0] == " = StatusFlags.Stopped"
				else:
					assert attr.find("dt").em.contents[0] == " = <StatusFlags.Stopped: 2>"

				assert str(attr.find("dd").contents[0]) == "<p>The system has stopped.</p>"
			elif attr_count == 2:
				if class_count == 0:
					assert attr.find("dt")["id"] == "enum_tools.demo.StatusFlags.Error"
				elif class_count == 1:
					assert attr.find("dt")["id"] == "id3"

				if NEW_ENUM_REPR:
					assert attr.find("dt").em.contents[0] == " = StatusFlags.Error"
				else:
					assert attr.find("dt").em.contents[0] == " = <StatusFlags.Error: 4>"

				assert str(attr.find("dd").contents[0]) == "<p>An error has occurred.</p>"

			attr_count += 1

		class_count += 1

	# print(page)

	assert class_count == 2


@xfail_312
@pytest.mark.parametrize(
		"page", [
				"no-member-doc.html",
				], indirect=True
		)
def test_no_member_doc(page: BeautifulSoup, html_regression: HTMLRegressionFixture):
	# Make sure the page title is what you expect
	title = page.find("h1").contents[0].strip()
	assert "autoenum Demo - Members without docstrings" == title

	preprocess_soup(page)

	html_regression.check(page, jinja2=True)

	# Now test the directive

	class_count = 0

	for class_ in page.findAll("dl"):
		if "enum" not in class_["class"]:
			continue

		assert class_.find("dt")["id"] == "enum_tools.demo.NoMemberDoc"
		assert class_.find("dd").findAll('p')[0].contents[
				0] == "An enumeration of people without any member docstrings."

		if class_count == 0:
			tag = '<code class="xref py py-class docutils literal notranslate">int</code>'
			assert str(class_.find("dd").findAll('p')[1].contents[0]) == tag
			assert class_.find("dd").findAll('p')[2].contents[0] == "Valid values are as follows:"
		else:
			assert class_.find("dd").findAll('p')[1].contents[0] == "Valid values are as follows:"

		attr_count = 0

		for attr in class_.findAll("dl"):
			if "attribute" not in attr["class"]:
				continue

			if attr_count == 0:
				if class_count == 0:
					assert attr.find("dt")["id"] == "enum_tools.demo.NoMemberDoc.Bob"
				elif class_count == 1:
					assert attr.find("dt")["id"] == "id1"

				if NEW_ENUM_REPR:
					assert attr.find("dt").em.contents[0] == " = NoMemberDoc.Bob"
				else:
					assert attr.find("dt").em.contents[0] == " = <NoMemberDoc.Bob: 1>"

				assert not attr.find("dd").contents
			elif attr_count == 1:
				if class_count == 0:
					assert attr.find("dt")["id"] == "enum_tools.demo.NoMemberDoc.Alice"
				elif class_count == 1:
					assert attr.find("dt")["id"] == "id2"

				if NEW_ENUM_REPR:
					assert attr.find("dt").em.contents[0] == " = NoMemberDoc.Alice"
				else:
					assert attr.find("dt").em.contents[0] == " = <NoMemberDoc.Alice: 2>"

				assert not attr.find("dd").contents
			elif attr_count == 2:
				if class_count == 0:
					assert attr.find("dt")["id"] == "enum_tools.demo.NoMemberDoc.Carol"
				elif class_count == 1:
					assert attr.find("dt")["id"] == "id3"

				if NEW_ENUM_REPR:
					assert attr.find("dt").em.contents[0] == " = NoMemberDoc.Carol"
				else:
					assert attr.find("dt").em.contents[0] == " = <NoMemberDoc.Carol: 3>"

				assert not attr.find("dd").contents

			attr_count += 1

		class_count += 1

	# print(page)

	assert class_count == 1