| 1 |
"""Tests for ``_docutils`` module.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import docutils.parsers.rst.directives as docutils_directives |
| 6 |
import docutils.parsers.rst.roles as docutils_roles |
| 7 |
import pytest |
| 8 |
|
| 9 |
from rstcheck_core import _docutils, _extras |
| 10 |
|
| 11 |
|
| 12 |
class TestIgnoreDirectivesAndRoles: |
| 13 |
"""Test ``ignore_directives_and_roles`` function.""" |
| 14 |
|
| 15 |
@staticmethod |
| 16 |
@pytest.mark.usefixtures("patch_docutils_directives_and_roles_dict") |
| 17 |
def test_with_empty_lists() -> None: |
| 18 |
"""Test with empty lists.""" |
| 19 |
directives: list[str] = [] |
| 20 |
roles: list[str] = [] |
| 21 |
|
| 22 |
_docutils.ignore_directives_and_roles(directives, roles) |
| 23 |
|
| 24 |
assert not docutils_directives._directives |
| 25 |
assert not docutils_roles._roles |
| 26 |
|
| 27 |
@staticmethod |
| 28 |
@pytest.mark.usefixtures("patch_docutils_directives_and_roles_dict") |
| 29 |
def test_with_only_roles() -> None: |
| 30 |
"""Test with only roles to add.""" |
| 31 |
directives: list[str] = [] |
| 32 |
roles = ["test_role"] |
| 33 |
|
| 34 |
_docutils.ignore_directives_and_roles(directives, roles) |
| 35 |
|
| 36 |
assert not docutils_directives._directives |
| 37 |
assert "test_role" in docutils_roles._roles |
| 38 |
|
| 39 |
@staticmethod |
| 40 |
@pytest.mark.usefixtures("patch_docutils_directives_and_roles_dict") |
| 41 |
def test_with_only_directives() -> None: |
| 42 |
"""Test with only directives to add.""" |
| 43 |
directives = ["test_directive"] |
| 44 |
roles: list[str] = [] |
| 45 |
|
| 46 |
_docutils.ignore_directives_and_roles(directives, roles) |
| 47 |
|
| 48 |
assert "test_directive" in docutils_directives._directives |
| 49 |
assert not docutils_roles._roles |
| 50 |
|
| 51 |
@staticmethod |
| 52 |
@pytest.mark.usefixtures("patch_docutils_directives_and_roles_dict") |
| 53 |
def test_with_both() -> None: |
| 54 |
"""Test with both.""" |
| 55 |
directives = ["test_directive"] |
| 56 |
roles = ["test_role"] |
| 57 |
|
| 58 |
_docutils.ignore_directives_and_roles(directives, roles) |
| 59 |
|
| 60 |
assert "test_directive" in docutils_directives._directives |
| 61 |
assert "test_role" in docutils_roles._roles |
| 62 |
|
| 63 |
|
| 64 |
class TestRegisterCodeRirective: |
| 65 |
"""Test ``register_code_directive`` function.""" |
| 66 |
|
| 67 |
@staticmethod |
| 68 |
@pytest.mark.skipif(not _extras.SPHINX_INSTALLED, reason="Depends on sphinx extra.") |
| 69 |
@pytest.mark.usefixtures("patch_docutils_directives_and_roles_dict") |
| 70 |
def test_does_nothing_when_sphinx_installed() -> None: |
| 71 |
"""Test function does nothing when sphinx is installed.""" |
| 72 |
_docutils.register_code_directive() |
| 73 |
|
| 74 |
assert "code" not in docutils_directives._directives |
| 75 |
assert "code-block" not in docutils_directives._directives |
| 76 |
assert "sourcecode" not in docutils_directives._directives |
| 77 |
|
| 78 |
@staticmethod |
| 79 |
@pytest.mark.skipif(_extras.SPHINX_INSTALLED, reason="Test without sphinx extra.") |
| 80 |
@pytest.mark.usefixtures("patch_docutils_directives_and_roles_dict") |
| 81 |
def test_registers_all_when_sphinx_is_missing() -> None: |
| 82 |
"""Test function registers all directives when sphinx is missing.""" |
| 83 |
_docutils.register_code_directive() |
| 84 |
|
| 85 |
assert "code" in docutils_directives._directives |
| 86 |
assert "code-block" in docutils_directives._directives |
| 87 |
assert "sourcecode" in docutils_directives._directives |
| 88 |
|
| 89 |
@staticmethod |
| 90 |
@pytest.mark.skipif(_extras.SPHINX_INSTALLED, reason="Test without sphinx extra.") |
| 91 |
@pytest.mark.usefixtures("patch_docutils_directives_and_roles_dict") |
| 92 |
def test_does_nothing_when_sphinx_is_missing_and_all_ignored() -> None: |
| 93 |
"""Test function does nothing when sphinx is missing, but all ignores are ``True``.""" |
| 94 |
_docutils.register_code_directive( |
| 95 |
ignore_code_directive=True, |
| 96 |
ignore_codeblock_directive=True, |
| 97 |
ignore_sourcecode_directive=True, |
| 98 |
) |
| 99 |
|
| 100 |
assert "code" not in docutils_directives._directives |
| 101 |
assert "code-block" not in docutils_directives._directives |
| 102 |
assert "sourcecode" not in docutils_directives._directives |
| 103 |
|
| 104 |
@staticmethod |
| 105 |
@pytest.mark.skipif(_extras.SPHINX_INSTALLED, reason="Test without sphinx extra.") |
| 106 |
@pytest.mark.usefixtures("patch_docutils_directives_and_roles_dict") |
| 107 |
def test_install_only_code_when_others_are_ignored() -> None: |
| 108 |
"""Test function installes only code directive when others are ignored.""" |
| 109 |
_docutils.register_code_directive( |
| 110 |
ignore_codeblock_directive=True, ignore_sourcecode_directive=True |
| 111 |
) |
| 112 |
|
| 113 |
assert "code" in docutils_directives._directives |
| 114 |
assert "code-block" not in docutils_directives._directives |
| 115 |
assert "sourcecode" not in docutils_directives._directives |
| 116 |
|
| 117 |
@staticmethod |
| 118 |
@pytest.mark.skipif(_extras.SPHINX_INSTALLED, reason="Test without sphinx extra.") |
| 119 |
@pytest.mark.usefixtures("patch_docutils_directives_and_roles_dict") |
| 120 |
def test_install_only_code_block_when_others_are_ignored() -> None: |
| 121 |
"""Test function installes only code-block directive when others are ignored.""" |
| 122 |
_docutils.register_code_directive( |
| 123 |
ignore_code_directive=True, ignore_sourcecode_directive=True |
| 124 |
) |
| 125 |
|
| 126 |
assert "code" not in docutils_directives._directives |
| 127 |
assert "code-block" in docutils_directives._directives |
| 128 |
assert "sourcecode" not in docutils_directives._directives |
| 129 |
|
| 130 |
@staticmethod |
| 131 |
@pytest.mark.skipif(_extras.SPHINX_INSTALLED, reason="Test without sphinx extra.") |
| 132 |
@pytest.mark.usefixtures("patch_docutils_directives_and_roles_dict") |
| 133 |
def test_install_only_sourcecode_when_others_are_ignored() -> None: |
| 134 |
"""Test function installes only sourcecode directive when others are ignored.""" |
| 135 |
_docutils.register_code_directive( |
| 136 |
ignore_code_directive=True, ignore_codeblock_directive=True |
| 137 |
) |
| 138 |
|
| 139 |
assert "code" not in docutils_directives._directives |
| 140 |
assert "code-block" not in docutils_directives._directives |
| 141 |
assert "sourcecode" in docutils_directives._directives |