Improve configtypes.Regex tests and docs.
This commit is contained in:
parent
16ac3baf2e
commit
d6301beb2a
@ -46,6 +46,12 @@ BOOLEAN_STATES = {'1': True, 'yes': True, 'true': True, 'on': True,
|
||||
|
||||
|
||||
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:
|
||||
warnings.simplefilter('always')
|
||||
try:
|
||||
|
@ -1092,12 +1092,29 @@ class TestRegex:
|
||||
Warning('foo'), DeprecationWarning('foo'),
|
||||
])
|
||||
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.compile.side_effect = lambda *args: warnings.warn(warning)
|
||||
m.error = re.error
|
||||
with pytest.raises(type(warning)):
|
||||
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:
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user