| 1 |
"""This file tests the source code provided by the documentation. |
| 2 |
|
| 3 |
See |
| 4 |
- doctest documentation: https://docs.python.org/3/library/doctest.html |
| 5 |
- Issue 443: https://github.com/collective/icalendar/issues/443 |
| 6 |
|
| 7 |
This file should be tests, too: |
| 8 |
|
| 9 |
>>> print("Hello World!") |
| 10 |
Hello World! |
| 11 |
|
| 12 |
""" |
| 13 |
|
| 14 |
import doctest |
| 15 |
import importlib |
| 16 |
import pathlib |
| 17 |
import sys |
| 18 |
|
| 19 |
import pytest |
| 20 |
|
| 21 |
HERE = pathlib.Path(__file__).parent |
| 22 |
PROJECT_PATH = HERE.parent.parent |
| 23 |
|
| 24 |
PYTHON_FILES = list(PROJECT_PATH.rglob("*.py")) |
| 25 |
|
| 26 |
MODULE_NAMES = [ |
| 27 |
"recurring_ical_events", |
| 28 |
] |
| 29 |
|
| 30 |
|
| 31 |
@pytest.mark.parametrize("module_name", MODULE_NAMES) |
| 32 |
def test_docstring_of_python_file(module_name): |
| 33 |
"""This test runs doctest on the Python module.""" |
| 34 |
module = importlib.import_module(module_name) |
| 35 |
test_result = doctest.testmod(module, name=module_name) |
| 36 |
assert test_result.failed == 0, f"{test_result.failed} errors in {module_name}" |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
DOCS = PROJECT_PATH / "docs" |
| 41 |
DOCUMENT_PATHS = [PROJECT_PATH / "README.rst", *list(DOCS.glob("*/*.rst")), *list(DOCS.glob("*.rst")), *list(DOCS.glob("*/*.md")), *list(DOCS.glob("*/*.md"))] |
| 42 |
|
| 43 |
|
| 44 |
@pytest.mark.parametrize("document", DOCUMENT_PATHS) |
| 45 |
def test_documentation_file(document, env_for_doctest): |
| 46 |
"""This test runs doctest on a documentation file. |
| 47 |
|
| 48 |
functions are also replaced to work. |
| 49 |
""" |
| 50 |
test_result = doctest.testfile( |
| 51 |
str(document), |
| 52 |
module_relative=False, |
| 53 |
globs=env_for_doctest, |
| 54 |
raise_on_error=False, |
| 55 |
) |
| 56 |
assert test_result.failed == 0, f"{test_result.failed} errors in {document.name}" |
| 57 |
|
| 58 |
|
| 59 |
def test_can_import_zoneinfo(env_for_doctest): |
| 60 |
"""Allow importing zoneinfo for tests.""" |
| 61 |
assert "zoneinfo" in sys.modules |