Skip to content

Commit 9c06381

Browse files
committed
Integrate doctests with pytest
1 parent 662e06d commit 9c06381

4 files changed

Lines changed: 59 additions & 31 deletions

File tree

src/flint/test/__main__.py

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,10 @@
88
import doctest
99
import traceback
1010
import argparse
11-
import pkgutil
12-
import importlib
13-
import re
1411

1512
import flint
1613
from flint.test.test_all import all_tests
17-
18-
19-
dunder_test_regex = re.compile(r'^(.*?)__test__\..*?\.(.*) \(line (\d+)\)$')
14+
from flint.test.test_docstrings import find_doctests
2015

2116

2217
def run_tests(verbose=None):
@@ -54,29 +49,6 @@ def run_tests(verbose=None):
5449
return failed, total
5550

5651

57-
def find_doctests(module):
58-
finder = doctest.DocTestFinder()
59-
tests = []
60-
for module_info in pkgutil.walk_packages(module.__path__, flint.__name__ + "."):
61-
try:
62-
module = importlib.import_module(module_info.name)
63-
64-
res = []
65-
for test in filter(lambda x: bool(x.examples), finder.find(module)):
66-
m = dunder_test_regex.match(test.name)
67-
if m is not None:
68-
groups = m.groups()
69-
test.name = groups[0] + groups[1]
70-
test.lineno = int(groups[2])
71-
res.append(test)
72-
73-
tests.append((module_info.name, res))
74-
75-
except Exception as e:
76-
print(f"Error importing {module_info.name}: {e}")
77-
return tests
78-
79-
8052
def run_doctests(tests, verbose=False):
8153
runner = doctest.DocTestRunner()
8254
for module, test_set in tests:

src/flint/test/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pyfiles = [
44
'__init__.py',
55
'__main__.py',
66
'test_all.py',
7+
'test_docstrings.py',
78
]
89

910
py.install_sources(

src/flint/test/test_docstrings.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import doctest
2+
import importlib
3+
import pkgutil
4+
import pytest
5+
import re
6+
7+
import flint
8+
9+
dunder_test_regex = re.compile(r'^(.*?)__test__\..*?\.(.*) \(line (\d+)\)$')
10+
11+
12+
def find_doctests(module):
13+
finder = doctest.DocTestFinder()
14+
tests = []
15+
for module_info in pkgutil.walk_packages(module.__path__, flint.__name__ + "."):
16+
try:
17+
module = importlib.import_module(module_info.name)
18+
19+
res = []
20+
for test in filter(lambda x: bool(x.examples), finder.find(module)):
21+
m = dunder_test_regex.match(test.name)
22+
if m is not None:
23+
groups = m.groups()
24+
test.name = groups[0] + groups[1]
25+
test.lineno = int(groups[2])
26+
res.append(test)
27+
28+
tests.append((module_info.name, res))
29+
30+
except Exception as e:
31+
print(f"Error importing {module_info.name}: {e}")
32+
return tests
33+
34+
35+
class PyTestDocTestRunner(doctest.DocTestRunner):
36+
def report_failure(self, out, test, example, got):
37+
pytest.fail(
38+
"\n".join([
39+
f"Failed: {test.name}, line: {test.lineno}",
40+
"Failed example:",
41+
f"\t{example.source.strip()}",
42+
"Expected:",
43+
f"\t{example.want.strip()}",
44+
"Got:",
45+
f"\t{got.strip()}"
46+
]),
47+
pytrace=False,
48+
)
49+
50+
51+
runner = PyTestDocTestRunner()
52+
53+
@pytest.mark.parametrize("module,test", [(module, test) for module, test_set in find_doctests(flint) for test in test_set])
54+
def test_docstrings(module, test):
55+
runner.run(test)

src/flint/types/fmpz_poly.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ cdef class fmpz_poly(flint_poly):
644644
here. The returned monomial allows the undo-ing of the deflation.
645645

646646
>>> f = fmpz_poly([1, 0, 1])
647-
>>> f.deflation()
647+
>>> f.deflation_monom()
648648
(1, x)
649649
"""
650650
n, m = self.deflation_index()
@@ -663,7 +663,7 @@ cdef class fmpz_poly(flint_poly):
663663
``deflation_monom``.
664664

665665
>>> f = fmpz_poly([1, 0, 1])
666-
>>> f.deflation()
666+
>>> f.deflation_index()
667667
(1, 1)
668668
"""
669669
cdef fmpz_poly res = fmpz_poly.__new__(fmpz_poly)

0 commit comments

Comments
 (0)