diff options
| author | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2017-10-29 22:14:55 +0900 |
|---|---|---|
| committer | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2017-10-29 22:23:15 +0900 |
| commit | d7d2e823fc1a59a4c94c471b4f1dfa1501f226b4 (patch) | |
| tree | 4178fc9e1538a4c3267a236dbdabb701bb14ea86 | |
| parent | 6274ea7ec8a0fae41e3f54c8161d16b5909daa8e (diff) | |
| download | sphinx-git-d7d2e823fc1a59a4c94c471b4f1dfa1501f226b4.tar.gz | |
sphinx.config.ENUM allows to validate items of lists or tuples
| -rw-r--r-- | sphinx/config.py | 9 | ||||
| -rw-r--r-- | tests/test_config.py | 12 |
2 files changed, 18 insertions, 3 deletions
diff --git a/sphinx/config.py b/sphinx/config.py index 02ee529a3..6abdbc55c 100644 --- a/sphinx/config.py +++ b/sphinx/config.py @@ -24,7 +24,7 @@ from sphinx.util.pycompat import execfile_, NoneType if False: # For type annotation - from typing import Any, Callable, Dict, Iterable, Iterator, List, Tuple # NOQA + from typing import Any, Callable, Dict, Iterable, Iterator, List, Tuple, Union # NOQA from sphinx.util.tags import Tags # NOQA logger = logging.getLogger(__name__) @@ -63,8 +63,11 @@ class ENUM(object): self.candidates = candidates def match(self, value): - # type: (unicode) -> bool - return value in self.candidates + # type: (Union[unicode,List,Tuple]) -> bool + if isinstance(value, (list, tuple)): + return all(item in self.candidates for item in value) + else: + return value in self.candidates string_classes = [text_type] # type: List diff --git a/tests/test_config.py b/tests/test_config.py index f9300c30e..578f6e55c 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -240,3 +240,15 @@ def test_check_enum(app, status, warning): def test_check_enum_failed(app, status, warning): assert "The config value `value17` has to be a one of ('default', 'one', 'two'), " \ "but `invalid` is given." in warning.getvalue() + + +@pytest.mark.sphinx(testroot='config', confoverrides={'value17': ['one', 'two']}) +def test_check_enum_for_list(app, status, warning): + assert "The config value `value17` has to be a one of ('default', 'one', 'two'), " \ + not in warning.getvalue() + + +@pytest.mark.sphinx(testroot='config', confoverrides={'value17': ['one', 'two', 'invalid']}) +def test_check_enum_for_list_failed(app, status, warning): + assert "The config value `value17` has to be a one of ('default', 'one', 'two'), " \ + "but `['one', 'two', 'invalid']` is given." in warning.getvalue() |
