forked from PyAV-Org/PyAV
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_doctests.py
More file actions
71 lines (51 loc) · 1.61 KB
/
Copy pathtest_doctests.py
File metadata and controls
71 lines (51 loc) · 1.61 KB
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
from __future__ import print_function
from unittest import TestCase
import doctest
import os
import re
import sys
import pkgutil
import av
from .common import is_py3
try:
basestring
except NameError:
basestring = str
def fix_doctests(suite):
for case in suite._tests:
# Add some more flags.
case._dt_optionflags = (
(case._dt_optionflags or 0) |
doctest.IGNORE_EXCEPTION_DETAIL |
doctest.ELLIPSIS |
doctest.NORMALIZE_WHITESPACE
)
case._dt_test.globs['av'] = av
for example in case._dt_test.examples:
# Remove b prefix from strings.
if is_py3 and example.want.startswith("b'"):
example.want = example.want[1:]
def register_doctests(mod):
if isinstance(mod, basestring):
mod = __import__(mod, fromlist=[''])
try:
suite = doctest.DocTestSuite(mod)
except ValueError as e:
if e.args[-1] != 'has no docstrings':
print('test_doctest load error:', e, file=sys.__stdout__)
return
fix_doctests(suite)
cls_name = 'Test' + ''.join(x.title() for x in mod.__name__.split('.'))
cls = type(cls_name, (TestCase, ), {})
for test in suite._tests:
func = lambda self: test.runTest()
name = str('test_' + re.sub('[^a-zA-Z0-9]+', '_', test.id()).strip('_'))
func.__name__ = name
setattr(cls, name, func)
globals()[cls_name] = cls
for importer, mod_name, ispkg in pkgutil.walk_packages(
path=av.__path__,
prefix=av.__name__+'.',
onerror=lambda x: None
):
register_doctests(mod_name)