|
9 | 9 | from typing import Any |
10 | 10 | from unittest import mock |
11 | 11 |
|
| 12 | +import pytest |
12 | 13 | from docstring_to_markdown import UnknownFormatError |
13 | 14 | from flaky import flaky |
14 | 15 |
|
@@ -234,6 +235,54 @@ def test_clip_column() -> None: |
234 | 235 | assert _utils.clip_column(4, ["123\n", "123"], 1) == 3 |
235 | 236 |
|
236 | 237 |
|
| 238 | +@pytest.mark.parametrize( |
| 239 | + ("contents", "expected"), |
| 240 | + [ |
| 241 | + ( |
| 242 | + "Use ``object_hook`` and object_hook.", |
| 243 | + r"Use ``object_hook`` and object\_hook.", |
| 244 | + ), |
| 245 | + ("`*a_b*[c]#d` *a_b*[c]#d", r"`*a_b*[c]#d` \*a\_b\*\[c\]\#d"), |
| 246 | + ("``a `b_c` d_e`` f_g", r"``a `b_c` d_e`` f\_g"), |
| 247 | + ("`a_b` ``c_d``", "`a_b` ``c_d``"), |
| 248 | + ("` `", "` `"), |
| 249 | + (r"\`a_b`", r"\\`a_b`"), |
| 250 | + (r"\\`a_b`", r"\\\\`a_b`"), |
| 251 | + (r"`path\name`", r"`path\name`"), |
| 252 | + (r"`a\` tail_word", r"`a\` tail\_word"), |
| 253 | + ("`a_b`` c_d", r"`a\_b`` c\_d"), |
| 254 | + ("``a_b` c_d`", r"``a\_b` c_d`"), |
| 255 | + ("`a_b\nc_d`", "`a\\_b\nc\\_d`"), |
| 256 | + ("`a\nb` *outside* `c_d`", "`a\nb` \\*outside\\* `c\\_d`"), |
| 257 | + ("`a_b`\n`c_d`", "`a_b`\n`c_d`"), |
| 258 | + ("`*a_b*\n# c_d\nend`", "`\\*a\\_b\\*\n\\# c\\_d\nend`"), |
| 259 | + ("`a_b\n\nc_d`", "`a\\_b\n\nc\\_d`"), |
| 260 | + ("`a_b\n \nc_d`", "`a\\_b\n\u00a0\u00a0\nc\\_d`"), |
| 261 | + ("`a_b\r\n\r\nc_d`", "`a\\_b\r\n\r\nc\\_d`"), |
| 262 | + ("`a_b\rc_d`", "`a\\_b\rc\\_d`"), |
| 263 | + ("`a b\t_c` tail _word", "`a b\t_c` tail\u00a0\u00a0\\_word"), |
| 264 | + (r"unmatched `path\name_value", r"unmatched `path\\name\_value"), |
| 265 | + ], |
| 266 | +) |
| 267 | +def test_escape_markdown_code_spans(contents, expected) -> None: |
| 268 | + assert _utils.escape_markdown(contents) == expected |
| 269 | + |
| 270 | + |
| 271 | +@pytest.mark.parametrize( |
| 272 | + ("markup_kind", "contents", "expected"), |
| 273 | + [ |
| 274 | + ("markdown", "Use ``object_hook``.", "Use ``object_hook``."), |
| 275 | + ("markdown", "Use object_hook.", r"Use object\_hook."), |
| 276 | + ("plaintext", "Use ``object_hook``.", "Use ``object_hook``."), |
| 277 | + ("markdown", r"Use ``path\name``.", r"Use ``path\name``."), |
| 278 | + ], |
| 279 | +) |
| 280 | +def test_format_docstring_code_spans(markup_kind, contents, expected) -> None: |
| 281 | + assert _utils.format_docstring( |
| 282 | + contents, markup_kind, signature_config={"formatter": None} |
| 283 | + ) == {"kind": markup_kind, "value": expected} |
| 284 | + |
| 285 | + |
237 | 286 | @mock.patch("docstring_to_markdown.convert") |
238 | 287 | def test_format_docstring_valid_rst_signature(mock_convert) -> None: |
239 | 288 | """Test that a valid RST docstring includes the function signature.""" |
|
0 commit comments