rewrite the invalid token_type test to check the warnings

This commit is contained in:
Keewis 2020-07-12 23:13:28 +02:00
parent 804df88e8d
commit f30c0cb9f6

View File

@ -9,7 +9,9 @@
:license: BSD, see LICENSE for details. :license: BSD, see LICENSE for details.
""" """
import re
from collections import namedtuple from collections import namedtuple
from contextlib import contextmanager
from inspect import cleandoc from inspect import cleandoc
from textwrap import dedent from textwrap import dedent
from unittest import TestCase, mock from unittest import TestCase, mock
@ -1068,7 +1070,7 @@ Methods:
.. method:: func(i, j) .. method:: func(i, j)
:noindex: :noindex:
description description
""" """
config = Config() config = Config()
@ -2005,21 +2007,6 @@ definition_after_normal_text : int
actual = _token_type(token) actual = _token_type(token)
self.assertEqual(expected, actual) self.assertEqual(expected, actual)
def test_token_type_invalid(self):
tokens = (
"{1, 2",
"}",
"'abc",
"def'",
'"ghi',
'jkl"',
)
for token in tokens:
# TODO: check for the warning
_token_type(token)
assert False
def test_tokenize_type_spec(self): def test_tokenize_type_spec(self):
types = ( types = (
"str", "str",
@ -2219,3 +2206,38 @@ Do as you please
:kwtype gotham_is_yours: None :kwtype gotham_is_yours: None
""" """
self.assertEqual(expected, actual) self.assertEqual(expected, actual)
@contextmanager
def warns(warning, match):
match_re = re.compile(match)
try:
yield warning
finally:
raw_warnings = warning.getvalue()
warnings = [w for w in raw_warnings.split("\n") if w.strip()]
assert len(warnings) == 1 and all(match_re.match(w) for w in warnings)
warning.truncate(0)
class TestNumpyDocstring:
def test_token_type_invalid(self, warning):
tokens = (
"{1, 2",
"}",
"'abc",
"def'",
'"ghi',
'jkl"',
)
errors = (
r".+: invalid value set \(missing closing brace\):",
r".+: invalid value set \(missing opening brace\):",
r".+: malformed string literal \(missing closing quote\):",
r".+: malformed string literal \(missing opening quote\):",
r".+: malformed string literal \(missing closing quote\):",
r".+: malformed string literal \(missing opening quote\):",
)
for token, error in zip(tokens, errors):
with warns(warning, match=error):
_token_type(token)