| 1 |
import doctest |
| 2 |
import pkgutil |
| 3 |
import re |
| 4 |
from unittest import TestCase |
| 5 |
|
| 6 |
import av |
| 7 |
import av.datasets |
| 8 |
|
| 9 |
|
| 10 |
def fix_doctests(suite): |
| 11 |
for case in suite._tests: |
| 12 |
# Add some more flags. |
| 13 |
case._dt_optionflags = ( |
| 14 |
(case._dt_optionflags or 0) |
| 15 |
| doctest.IGNORE_EXCEPTION_DETAIL |
| 16 |
| doctest.ELLIPSIS |
| 17 |
| doctest.NORMALIZE_WHITESPACE |
| 18 |
) |
| 19 |
|
| 20 |
case._dt_test.globs["av"] = av |
| 21 |
case._dt_test.globs["video_path"] = av.datasets.curated( |
| 22 |
"pexels/time-lapse-video-of-night-sky-857195.mp4" |
| 23 |
) |
| 24 |
|
| 25 |
for example in case._dt_test.examples: |
| 26 |
# Remove b prefix from strings. |
| 27 |
if example.want.startswith("b'"): |
| 28 |
example.want = example.want[1:] |
| 29 |
|
| 30 |
|
| 31 |
def register_doctests(mod): |
| 32 |
if isinstance(mod, str): |
| 33 |
mod = __import__(mod, fromlist=[""]) |
| 34 |
|
| 35 |
try: |
| 36 |
suite = doctest.DocTestSuite(mod) |
| 37 |
except ValueError: |
| 38 |
return |
| 39 |
|
| 40 |
fix_doctests(suite) |
| 41 |
|
| 42 |
cls_name = "Test" + "".join(x.title() for x in mod.__name__.split(".")) |
| 43 |
cls = type(cls_name, (TestCase,), {}) |
| 44 |
|
| 45 |
for test in suite._tests: |
| 46 |
|
| 47 |
def func(self): |
| 48 |
return test.runTest() |
| 49 |
|
| 50 |
name = str("test_" + re.sub("[^a-zA-Z0-9]+", "_", test.id()).strip("_")) |
| 51 |
func.__name__ = name |
| 52 |
setattr(cls, name, func) |
| 53 |
|
| 54 |
globals()[cls_name] = cls |
| 55 |
|
| 56 |
|
| 57 |
for importer, mod_name, ispkg in pkgutil.walk_packages( |
| 58 |
path=av.__path__, prefix=av.__name__ + ".", onerror=lambda x: None |
| 59 |
): |
| 60 |
register_doctests(mod_name) |