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
|
from pathlib import Path
from hypothesis import given
from hypothesis.strategies import text
import pytest
from pdoc import docstrings
# The important tests are in test_snapshot.py (and, by extension, testdata/)
# mostly some fuzzing here.
here = Path(__file__).parent.absolute()
@given(text())
def test_google(s):
ret = docstrings.google(s)
assert not s or ret
@given(text())
def test_numpy(s):
ret = docstrings.numpy(s)
assert not s or ret
@given(text())
def test_rst(s):
ret = docstrings.rst(s, None)
assert not s or ret
@given(text())
def test_rst_extract_options_fuzz(s):
content, options = docstrings._rst_extract_options(s)
assert not s or content or options
def test_rst_extract_options():
content = (
":alpha: beta\n"
":charlie:delta:foxtrot\n"
"rest of content\n"
":option ignored: as follows content\n"
)
content, options = docstrings._rst_extract_options(content)
assert options == {
"alpha": "beta",
"charlie": "delta:foxtrot",
}
assert content == ("\nrest of content\n" ":option ignored: as follows content\n")
def test_rst_include_trim_lines():
content = "alpha\nbeta\ncharlie\ndelta\necho"
trimmed = docstrings._rst_include_trim(
content, {"start-line": "2", "end-line": "4"}
)
assert trimmed == "charlie\ndelta"
def test_rst_include_trim_pattern():
content = "alpha\nbeta\ncharlie\ndelta\necho"
trimmed = docstrings._rst_include_trim(
content, {"start-after": "beta", "end-before": "echo"}
)
assert trimmed == "\ncharlie\ndelta\n"
def test_rst_include_trim_mixture():
content = "alpha\nbeta\ncharlie\ndelta\necho"
trimmed = docstrings._rst_include_trim(
content, {"start-after": "beta", "end-line": "4"}
)
assert trimmed == "\ncharlie\ndelta"
def test_rst_include_nonexistent():
with pytest.warns(UserWarning, match="Cannot include 'nonexistent.txt'"):
docstrings.rst(".. include:: nonexistent.txt", None)
def test_rst_include_invalid_options():
with pytest.warns(UserWarning, match="Failed to process include options"):
docstrings.rst(
".. include:: ../README.md\n :start-line: invalid",
here / "test_docstrings.py",
)
|