From 05bcddb6f99aa62f9f0c72a93dd8484c8433cb98 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 28 Sep 2015 14:24:36 +0200 Subject: [PATCH] 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. --- tests/unit/config/test_configtypes.py | 38 +++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/tests/unit/config/test_configtypes.py b/tests/unit/config/test_configtypes.py index 164e04585..e74529886 100644 --- a/tests/unit/config/test_configtypes.py +++ b/tests/unit/config/test_configtypes.py @@ -69,6 +69,34 @@ class NetworkProxy(QNetworkProxy): 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 def os_mock(mocker): """Fixture that mocks and returns os from the configtypes module.""" @@ -1037,7 +1065,7 @@ class TestRegex: klass().validate(val) @pytest.mark.parametrize('val, expected', [ - (r'foobar', re.compile(r'foobar')), + (r'foobar', RegexEq(r'foobar')), ('', None), ]) def test_transform_empty(self, klass, val, expected): @@ -1070,10 +1098,10 @@ class TestRegexList: klass().validate(val) @pytest.mark.parametrize('val, expected', [ - ('foo', [re.compile('foo')]), - ('foo,bar,baz', [re.compile('foo'), re.compile('bar'), - re.compile('baz')]), - ('foo,,bar', [re.compile('foo'), None, re.compile('bar')]), + ('foo', [RegexEq('foo')]), + ('foo,bar,baz', [RegexEq('foo'), RegexEq('bar'), + RegexEq('baz')]), + ('foo,,bar', [RegexEq('foo'), None, RegexEq('bar')]), ('', None), ]) def test_transform(self, klass, val, expected):