Use RegexEq in configtypes tests
This prevents the tests from failing if the regex cache overflows, since re.compile objects compare by identitiy instead of patterns.
This commit is contained in:
parent
43a42def2b
commit
05bcddb6f9
@ -69,6 +69,34 @@ class NetworkProxy(QNetworkProxy):
|
|||||||
password=self.password())
|
password=self.password())
|
||||||
|
|
||||||
|
|
||||||
|
class RegexEq:
|
||||||
|
|
||||||
|
"""A class to compare regex objects."""
|
||||||
|
|
||||||
|
def __init__(self, pattern, flags=0):
|
||||||
|
# We compile the regex because re.compile also adds flags defined in
|
||||||
|
# the pattern and implicit flags to it's .flags.
|
||||||
|
# See https://docs.python.org/3/library/re.html#re.regex.flags
|
||||||
|
compiled = re.compile(pattern, flags)
|
||||||
|
self.pattern = compiled.pattern
|
||||||
|
self.flags = compiled.flags
|
||||||
|
self._user_flags = flags
|
||||||
|
|
||||||
|
def __eq__(self, other):
|
||||||
|
try:
|
||||||
|
# Works for RegexEq objects and re.compile objects
|
||||||
|
return (self.pattern, self.flags) == (other.pattern, other.flags)
|
||||||
|
except AttributeError:
|
||||||
|
return NotImplemented
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
if self._user_flags:
|
||||||
|
return "RegexEq({!r}, flags={})".format(self.pattern,
|
||||||
|
self._user_flags)
|
||||||
|
else:
|
||||||
|
return "RegexEq({!r})".format(self.pattern)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def os_mock(mocker):
|
def os_mock(mocker):
|
||||||
"""Fixture that mocks and returns os from the configtypes module."""
|
"""Fixture that mocks and returns os from the configtypes module."""
|
||||||
@ -1037,7 +1065,7 @@ class TestRegex:
|
|||||||
klass().validate(val)
|
klass().validate(val)
|
||||||
|
|
||||||
@pytest.mark.parametrize('val, expected', [
|
@pytest.mark.parametrize('val, expected', [
|
||||||
(r'foobar', re.compile(r'foobar')),
|
(r'foobar', RegexEq(r'foobar')),
|
||||||
('', None),
|
('', None),
|
||||||
])
|
])
|
||||||
def test_transform_empty(self, klass, val, expected):
|
def test_transform_empty(self, klass, val, expected):
|
||||||
@ -1070,10 +1098,10 @@ class TestRegexList:
|
|||||||
klass().validate(val)
|
klass().validate(val)
|
||||||
|
|
||||||
@pytest.mark.parametrize('val, expected', [
|
@pytest.mark.parametrize('val, expected', [
|
||||||
('foo', [re.compile('foo')]),
|
('foo', [RegexEq('foo')]),
|
||||||
('foo,bar,baz', [re.compile('foo'), re.compile('bar'),
|
('foo,bar,baz', [RegexEq('foo'), RegexEq('bar'),
|
||||||
re.compile('baz')]),
|
RegexEq('baz')]),
|
||||||
('foo,,bar', [re.compile('foo'), None, re.compile('bar')]),
|
('foo,,bar', [RegexEq('foo'), None, RegexEq('bar')]),
|
||||||
('', None),
|
('', None),
|
||||||
])
|
])
|
||||||
def test_transform(self, klass, val, expected):
|
def test_transform(self, klass, val, expected):
|
||||||
|
Loading…
Reference in New Issue
Block a user