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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
# stdlib
import re
# 3rd party
import pytest
from coincidence.regressions import AdvancedFileRegressionFixture
from domdf_python_tools.paths import PathPlus
from sphinx.events import EventListener
from sphinx.ext.autodoc.directive import AutodocDirective
# this package
from sphinx_toolbox import __version__
from sphinx_toolbox.more_autodoc import regex
from sphinx_toolbox.testing import check_asset_copy, run_setup
from tests.regex_demo import no_flags, one_flag, two_flags
parser = regex.RegexParser()
@pytest.mark.parametrize(
"regex, expected",
[
(
re.compile(r"(?s)(\.\. start installation)(.*?)(\.\. end installation)"),
"(.. start installation)(.*?)(.. end installation)",
),
(
re.compile(r"[A-Za-z_]\w*"),
r"[A-Za-z_]\w*",
),
(
re.compile(r"^:(param|parameter|arg|argument)\s*"),
r"^:(param|parameter|arg|argument)\s*",
),
(
re.compile("^:(default|Default)[ ]"),
"^:(default|Default)[ ]",
),
(
re.compile("^:(default|Default) "),
"^:(default|Default)[ ]",
),
(
re.compile(r"\A:(default|Default) "),
r"\A:(default|Default)[ ]",
),
(
re.compile("^:(default|Default) "),
"^:(default|Default) {3}",
),
(
re.compile(":(default|Default) $"),
":(default|Default) $",
),
(
re.compile(r":(default|Default) \Z"),
r":(default|Default) \Z",
),
(
re.compile(" :(default|Default)"),
"[ ]:(default|Default)",
),
(
re.compile(" :(default|Default)"),
"[ ]{3}:(default|Default)",
),
(
re.compile("[ ]:(default|Default)"),
"[ ]:(default|Default)",
),
(
re.compile("hello (world)?"),
"hello (world)?",
),
(
re.compile("(hello){1,3} (world)?"),
"(hello){1,3} (world)?",
),
(
re.compile("(hello){3,3} (world)?"),
"(hello){3} (world)?",
),
(
re.compile(r"Issue #\d"),
"Issue #\\d",
),
(
re.compile(r"Hello \w"),
"Hello \\w",
),
(
re.compile(r"Not a word: \W"),
"Not a word: \\W",
),
(
re.compile(r"Not whitespace: \S"),
"Not whitespace: \\S",
),
(
re.compile(r"Not a number: \D"),
"Not a number: \\D",
),
(no_flags, no_flags.pattern.replace("\\?", '?')),
(one_flag, one_flag.pattern.replace("\\?", '?')),
(two_flags, "Hello\\s+[Ww]orld[.,](Lovely|Horrible) weather, isn't it(.*)?"),
]
)
def test_regex_parser(regex: re.Pattern, expected: str):
assert parser.parse_pattern(regex) == expected
terminal_parser = regex.TerminalRegexParser()
@pytest.mark.parametrize(
"regex",
[
re.compile(r"(?s)(\.\. start installation)(.*?)(\.\. end installation)"),
re.compile(r"[A-Za-z_]\w*"),
re.compile(r"^:(param|parameter|arg|argument)\s*"),
pytest.param(re.compile("^:(default|Default)[ ]"), id="default_square_brackets"),
re.compile("^:(default|Default) "),
re.compile(r"\A:(default|Default) "),
pytest.param(re.compile("^:(default|Default) "), id="default_spaces"),
re.compile(":(default|Default) $"),
re.compile(r":(default|Default) \Z"),
re.compile(" :(default|Default)"),
pytest.param(re.compile(" :(default|Default)"), id="default_leading_spaces"),
re.compile("[ ]:(default|Default)"),
re.compile("hello (world)?"),
re.compile("(hello){1,3} (world)?"),
re.compile("(hello){3,3} (world)?"),
re.compile(r"Issue #\d"),
re.compile(r"Hello \w"),
re.compile(r"Not a word: \W"),
re.compile(r"Not whitespace: \S"),
re.compile(r"Not a number: \D"),
no_flags,
one_flag,
two_flags,
]
)
def test_terminal_regex_parser(regex: re.Pattern, advanced_file_regression: AdvancedFileRegressionFixture):
advanced_file_regression.check(terminal_parser.parse_pattern(regex))
def test_copy_asset_files(tmp_pathplus: PathPlus, advanced_file_regression: AdvancedFileRegressionFixture):
check_asset_copy(
regex.copy_asset_files,
"_static/regex.css",
file_regression=advanced_file_regression,
)
@pytest.mark.parametrize(
"flags, expected",
[
pytest.param(re.ASCII, ":py:data:`re.ASCII`", id="re.ASCII"),
pytest.param(re.DEBUG, ":py:data:`re.DEBUG`", id="re.DEBUG"),
pytest.param(re.IGNORECASE, ":py:data:`re.IGNORECASE`", id="re.IGNORECASE"),
pytest.param(re.LOCALE, ":py:data:`re.LOCALE`", id="re.LOCALE"),
pytest.param(re.MULTILINE, ":py:data:`re.MULTILINE`", id="re.MULTILINE"),
pytest.param(re.DOTALL, ":py:data:`re.DOTALL`", id="re.DOTALL"),
pytest.param(re.VERBOSE, ":py:data:`re.VERBOSE`", id="re.VERBOSE"),
pytest.param(
re.VERBOSE | re.IGNORECASE,
":py:data:`re.IGNORECASE` ``|`` :py:data:`re.VERBOSE`",
id="re.VERBOSE|re.IGNORECASE"
),
]
)
def test_parse_regex_flags(flags: int, expected: str):
assert regex.parse_regex_flags(flags) == expected
def test_setup():
setup_ret, directives, roles, additional_nodes, app = run_setup(regex.setup)
assert setup_ret == {"parallel_read_safe": True, "version": __version__}
assert directives == {"autoregex": AutodocDirective}
assert "regex" in roles
assert isinstance(roles["regex"], regex.Regex)
assert additional_nodes == {regex.RegexNode}
assert app.registry.documenters["regex"] == regex.RegexDocumenter
assert app.events.listeners == {"config-inited": [EventListener(0, regex.configure, 500)]}
assert app.registry.css_files == []
|