Improve configtypes.Regex tests and docs.

This commit is contained in:
Florian Bruhin 2015-10-01 23:24:27 +02:00
parent 16ac3baf2e
commit d6301beb2a
2 changed files with 23 additions and 0 deletions

View File

@ -46,6 +46,12 @@ BOOLEAN_STATES = {'1': True, 'yes': True, 'true': True, 'on': True,
def _validate_regex(pattern, flags): def _validate_regex(pattern, flags):
"""Check if the given regex is valid.
This is more complicated than it could be since there's a warning on
invalid escapes with newer Python versions, and we want to catch that case
and treat it as invalid.
"""
with warnings.catch_warnings(record=True) as recorded_warnings: with warnings.catch_warnings(record=True) as recorded_warnings:
warnings.simplefilter('always') warnings.simplefilter('always')
try: try:

View File

@ -1092,12 +1092,29 @@ class TestRegex:
Warning('foo'), DeprecationWarning('foo'), Warning('foo'), DeprecationWarning('foo'),
]) ])
def test_passed_warnings(self, mocker, klass, warning): def test_passed_warnings(self, mocker, klass, warning):
"""Simulate re.compile showing a warning we don't know about yet.
The warning should be passed.
"""
m = mocker.patch('qutebrowser.config.configtypes.re') m = mocker.patch('qutebrowser.config.configtypes.re')
m.compile.side_effect = lambda *args: warnings.warn(warning) m.compile.side_effect = lambda *args: warnings.warn(warning)
m.error = re.error m.error = re.error
with pytest.raises(type(warning)): with pytest.raises(type(warning)):
klass().validate('foo') klass().validate('foo')
def test_bad_pattern_warning(self, mocker, klass):
"""Test a simulated bad pattern warning.
This only seems to happen with Python 3.5, so we simulate this for
better coverage.
"""
m = mocker.patch('qutebrowser.config.configtypes.re')
m.compile.side_effect = lambda *args: warnings.warn(r'bad escape \C',
DeprecationWarning)
m.error = re.error
with pytest.raises(configexc.ValidationError):
klass().validate('foo')
class TestRegexList: class TestRegexList: