| 1 |
"""Tests for ``inline_config`` module.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import typing as t |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from rstcheck_core import inline_config, types |
| 10 |
|
| 11 |
|
| 12 |
class TestInlineConfigGetter: |
| 13 |
"""Test ``get_inline_config_from_source`` function.""" |
| 14 |
|
| 15 |
@staticmethod |
| 16 |
def test_empty_string_source() -> None: |
| 17 |
"""Test giving an empty string as source results in no config found.""" |
| 18 |
source = "" |
| 19 |
|
| 20 |
result = list(inline_config.get_inline_config_from_source(source, "<string>")) |
| 21 |
|
| 22 |
assert not result |
| 23 |
|
| 24 |
@staticmethod |
| 25 |
def test_source_without_config() -> None: |
| 26 |
"""Test giving source without config comment results in no config found.""" |
| 27 |
source = """ |
| 28 |
Example |
| 29 |
======= |
| 30 |
""" |
| 31 |
|
| 32 |
result = list(inline_config.get_inline_config_from_source(source, "<string>")) |
| 33 |
|
| 34 |
assert not result |
| 35 |
|
| 36 |
@staticmethod |
| 37 |
def test_source_with_correct_config() -> None: |
| 38 |
"""Test giving source with correct config comment results in config found.""" |
| 39 |
source = """ |
| 40 |
Example |
| 41 |
======= |
| 42 |
.. rstcheck: ignore-languages=cpp |
| 43 |
""" |
| 44 |
|
| 45 |
result = list(inline_config.get_inline_config_from_source(source, "<string>")) |
| 46 |
|
| 47 |
assert result == [types.InlineConfig(key="ignore-languages", value="cpp")] |
| 48 |
|
| 49 |
@staticmethod |
| 50 |
def test_source_with_unknown_config() -> None: |
| 51 |
"""Test unknown config in comments is ignored.""" |
| 52 |
source = """ |
| 53 |
Example |
| 54 |
======= |
| 55 |
.. rstcheck: unknown-config=true |
| 56 |
.. rstcheck: ignore_languages=json |
| 57 |
""" |
| 58 |
|
| 59 |
result = list(inline_config.get_inline_config_from_source(source, "<string>")) |
| 60 |
|
| 61 |
assert not result |
| 62 |
|
| 63 |
@staticmethod |
| 64 |
def test_source_with_invalid_config_format_is_ignored() -> None: |
| 65 |
"""Test unknown config in comments is ignored.""" |
| 66 |
source = """ |
| 67 |
Example |
| 68 |
======= |
| 69 |
.. rstcheck: ignore-languages: cpp |
| 70 |
""" |
| 71 |
|
| 72 |
result = list(inline_config.get_inline_config_from_source(source, "<string>")) |
| 73 |
|
| 74 |
assert not result |
| 75 |
|
| 76 |
@staticmethod |
| 77 |
def test_source_with_known_and_unknown_config() -> None: |
| 78 |
"""Test unknown config comments are ignored when mixed with valid confg.""" |
| 79 |
source = """ |
| 80 |
Example |
| 81 |
======= |
| 82 |
.. rstcheck: unknown-config=true |
| 83 |
.. rstcheck: ignore-languages=cpp |
| 84 |
""" |
| 85 |
|
| 86 |
result = list(inline_config.get_inline_config_from_source(source, "<string>")) |
| 87 |
|
| 88 |
assert result == [types.InlineConfig(key="ignore-languages", value="cpp")] |
| 89 |
|
| 90 |
@staticmethod |
| 91 |
def test_source_with_correct_config_multiple_values() -> None: |
| 92 |
"""Test giving source with correct config comment results in values found. |
| 93 |
|
| 94 |
Test with multiple values on one comment. |
| 95 |
""" |
| 96 |
source = """ |
| 97 |
Example |
| 98 |
======= |
| 99 |
.. rstcheck: ignore-languages=cpp,json |
| 100 |
""" |
| 101 |
|
| 102 |
result = list(inline_config.get_inline_config_from_source(source, "<string>")) |
| 103 |
|
| 104 |
assert result == [types.InlineConfig(key="ignore-languages", value="cpp,json")] |
| 105 |
|
| 106 |
@staticmethod |
| 107 |
def test_source_with_correct_config_multiple_comments() -> None: |
| 108 |
"""Test giving source with correct config comment results in configs found. |
| 109 |
|
| 110 |
Test with multiple configs on different comments. |
| 111 |
""" |
| 112 |
source = """ |
| 113 |
Example |
| 114 |
======= |
| 115 |
.. rstcheck: ignore-languages=cpp |
| 116 |
.. rstcheck: ignore-languages=json |
| 117 |
""" |
| 118 |
|
| 119 |
result = list(inline_config.get_inline_config_from_source(source, "<string>")) |
| 120 |
|
| 121 |
assert result == [ |
| 122 |
types.InlineConfig(key="ignore-languages", value="cpp"), |
| 123 |
types.InlineConfig(key="ignore-languages", value="json"), |
| 124 |
] |
| 125 |
|
| 126 |
@staticmethod |
| 127 |
def test_source_with_correct_config_whitespace() -> None: |
| 128 |
"""Test giving source with correct config comment results in configs found. |
| 129 |
|
| 130 |
Test whitspace around equal sign. |
| 131 |
""" |
| 132 |
source = """ |
| 133 |
Example |
| 134 |
======= |
| 135 |
.. rstcheck: ignore-languages =cpp |
| 136 |
.. rstcheck: ignore-languages= json |
| 137 |
.. rstcheck: ignore-languages = python |
| 138 |
""" |
| 139 |
|
| 140 |
result = list(inline_config.get_inline_config_from_source(source, "<string>")) |
| 141 |
|
| 142 |
assert result == [ |
| 143 |
types.InlineConfig(key="ignore-languages", value="cpp"), |
| 144 |
types.InlineConfig(key="ignore-languages", value="json"), |
| 145 |
types.InlineConfig(key="ignore-languages", value="python"), |
| 146 |
] |
| 147 |
|
| 148 |
@staticmethod |
| 149 |
def test_source_with_multiple_correct_configs() -> None: |
| 150 |
"""Test source with multiple correct configs. |
| 151 |
|
| 152 |
Test whitspace around equal sign. |
| 153 |
""" |
| 154 |
source = """ |
| 155 |
Example |
| 156 |
======= |
| 157 |
.. rstcheck: ignore-languages=cpp |
| 158 |
.. rstcheck: ignore-roles=role |
| 159 |
.. rstcheck: ignore-languages=python |
| 160 |
.. rstcheck: ignore-directives=direct |
| 161 |
""" |
| 162 |
|
| 163 |
result = list(inline_config.get_inline_config_from_source(source, "<string>")) |
| 164 |
|
| 165 |
assert result == [ |
| 166 |
types.InlineConfig(key="ignore-languages", value="cpp"), |
| 167 |
types.InlineConfig(key="ignore-roles", value="role"), |
| 168 |
types.InlineConfig(key="ignore-languages", value="python"), |
| 169 |
types.InlineConfig(key="ignore-directives", value="direct"), |
| 170 |
] |
| 171 |
|
| 172 |
|
| 173 |
class TestConfigFilterAndSpliter: |
| 174 |
"""Test ``_filter_config_and_split_values`` function.""" |
| 175 |
|
| 176 |
@staticmethod |
| 177 |
def test_only_specified_config_is_used() -> None: |
| 178 |
"""Test only specified config is returned.""" |
| 179 |
source = """ |
| 180 |
Example |
| 181 |
======= |
| 182 |
.. rstcheck: unknown-config=true |
| 183 |
.. rstcheck: ignore-languages=cpp |
| 184 |
.. rstcheck: ignore-directives=directive1 |
| 185 |
""" |
| 186 |
|
| 187 |
result = list( |
| 188 |
inline_config._filter_config_and_split_values("ignore-languages", source, "<string>") |
| 189 |
) |
| 190 |
|
| 191 |
assert result == ["cpp"] |
| 192 |
|
| 193 |
@staticmethod |
| 194 |
def test_configs_are_comma_splitted() -> None: |
| 195 |
"""Test configs are split on comma.""" |
| 196 |
source = """ |
| 197 |
Example |
| 198 |
======= |
| 199 |
.. rstcheck: ignore-languages=cpp,json |
| 200 |
.. rstcheck: ignore-languages=python |
| 201 |
""" |
| 202 |
|
| 203 |
result = list( |
| 204 |
inline_config._filter_config_and_split_values("ignore-languages", source, "<string>") |
| 205 |
) |
| 206 |
|
| 207 |
assert result == ["cpp", "json", "python"] |
| 208 |
|
| 209 |
|
| 210 |
class FindIgnoreFn(t.Protocol): |
| 211 |
def __call__( |
| 212 |
self, |
| 213 |
source: str, |
| 214 |
source_origin: types.SourceFileOrString, |
| 215 |
warn_unknown_settings: bool = False, |
| 216 |
) -> t.Generator[str, None, None]: ... |
| 217 |
|
| 218 |
|
| 219 |
class TestFindIgnoredFunctions: |
| 220 |
"""Test ``find_ignored_*`` function. |
| 221 |
|
| 222 |
- find_ignored_directives |
| 223 |
- find_ignored_roles |
| 224 |
- find_ignored_substitutions |
| 225 |
- find_ignored_languages |
| 226 |
""" |
| 227 |
|
| 228 |
@staticmethod |
| 229 |
@pytest.mark.parametrize( |
| 230 |
("target_function", "expected_result"), |
| 231 |
[ |
| 232 |
(inline_config.find_ignored_directives, ["directive1"]), |
| 233 |
(inline_config.find_ignored_roles, ["role1"]), |
| 234 |
(inline_config.find_ignored_substitutions, ["substitution1"]), |
| 235 |
(inline_config.find_ignored_languages, ["cpp"]), |
| 236 |
], |
| 237 |
) |
| 238 |
def test_only_target_config_is_used( |
| 239 |
target_function: FindIgnoreFn, expected_result: str |
| 240 |
) -> None: |
| 241 |
"""Test only targeted configs are returned.""" |
| 242 |
source = """ |
| 243 |
Example |
| 244 |
======= |
| 245 |
.. rstcheck: unknown-config=true |
| 246 |
.. rstcheck: ignore-directives=directive1 |
| 247 |
.. rstcheck: ignore-roles=role1 |
| 248 |
.. rstcheck: ignore-substitutions=substitution1 |
| 249 |
.. rstcheck: ignore-languages=cpp |
| 250 |
""" |
| 251 |
|
| 252 |
result = list(target_function(source, "<string>")) |
| 253 |
|
| 254 |
assert result == expected_result |
| 255 |
|
| 256 |
@staticmethod |
| 257 |
@pytest.mark.parametrize( |
| 258 |
("target_function", "expected_result"), |
| 259 |
[ |
| 260 |
(inline_config.find_ignored_directives, ["directive1", "directive3", "directive2"]), |
| 261 |
(inline_config.find_ignored_roles, ["role1", "role3", "role2"]), |
| 262 |
( |
| 263 |
inline_config.find_ignored_substitutions, |
| 264 |
["substitution1", "substitution3", "substitution2"], |
| 265 |
), |
| 266 |
(inline_config.find_ignored_languages, ["cpp", "json", "python"]), |
| 267 |
], |
| 268 |
) |
| 269 |
def test_values_are_comma_splitted(target_function: FindIgnoreFn, expected_result: str) -> None: |
| 270 |
"""Test config values are split on comma.""" |
| 271 |
source = """ |
| 272 |
Example |
| 273 |
======= |
| 274 |
.. rstcheck: unknown-config=true |
| 275 |
.. rstcheck: ignore-directives=directive1,directive3 |
| 276 |
.. rstcheck: ignore-directives=directive2 |
| 277 |
.. rstcheck: ignore-roles=role1,role3 |
| 278 |
.. rstcheck: ignore-roles=role2 |
| 279 |
.. rstcheck: ignore-substitutions=substitution1,substitution3 |
| 280 |
.. rstcheck: ignore-substitutions=substitution2 |
| 281 |
.. rstcheck: ignore-languages=cpp,json |
| 282 |
.. rstcheck: ignore-languages=python |
| 283 |
""" |
| 284 |
|
| 285 |
result = list(target_function(source, "<string>")) |
| 286 |
|
| 287 |
assert result == expected_result |