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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
# 3rd party
from sphinx.events import EventListener
# this package
from sphinx_toolbox import rest_example
from sphinx_toolbox.rest_example import make_rest_example, rest_example_purger, reSTExampleDirective
from sphinx_toolbox.testing import run_setup
from tests.common import AttrDict
class FakeBuildEnvironment(AttrDict):
def __init__(self, tab_width: int):
config = AttrDict({"docutils_tab_width": tab_width})
super().__init__({"config": config})
def test_make_rest_example():
assert make_rest_example(
{},
FakeBuildEnvironment(4), # type: ignore[arg-type]
[],
) == [".. container:: rest-example", '', " .. code-block:: rest", '']
assert make_rest_example(
{"hello": "world"},
FakeBuildEnvironment(4), # type: ignore[arg-type]
[],
) == [".. container:: rest-example", '', " .. code-block:: rest", " :hello: world", '']
assert make_rest_example(
{"hello": "world", "flag": None},
FakeBuildEnvironment(4), # type: ignore[arg-type]
[],
) == [
".. container:: rest-example",
'',
" .. code-block:: rest",
" :hello: world",
" :flag:",
''
]
assert make_rest_example(
{"hello": "world", "flag": None},
FakeBuildEnvironment(4), # type: ignore[arg-type]
["this is some content"],
) == [
".. container:: rest-example",
'',
" .. code-block:: rest",
" :hello: world",
" :flag:",
'',
" this is some content",
'',
" this is some content",
'',
]
assert make_rest_example(
{},
FakeBuildEnvironment(8), # type: ignore[arg-type]
[],
) == [".. container:: rest-example", '', " .. code-block:: rest", '']
assert make_rest_example(
{"hello": "world"},
FakeBuildEnvironment(8), # type: ignore[arg-type]
[],
) == [
".. container:: rest-example",
'',
" .. code-block:: rest",
" :hello: world",
''
]
assert make_rest_example(
{"hello": "world", "flag": None},
FakeBuildEnvironment(8), # type: ignore[arg-type]
[],
) == [
".. container:: rest-example",
'',
" .. code-block:: rest",
" :hello: world",
" :flag:",
'',
]
assert make_rest_example(
{"hello": "world", "flag": None},
FakeBuildEnvironment(8), # type: ignore[arg-type]
["this is some content"],
) == [
".. container:: rest-example",
'',
" .. code-block:: rest",
" :hello: world",
" :flag:",
'',
" this is some content",
'',
" this is some content",
'',
]
def test_setup():
setup_ret, directives, roles, additional_nodes, app = run_setup(rest_example.setup)
assert app.events.listeners == {
"env-purge-doc": [EventListener(id=0, handler=rest_example_purger.purge_nodes, priority=500), ],
}
assert directives == {
"rest-example": reSTExampleDirective,
}
assert app.registry.source_parsers == {}
|