41
42
def _real_re_compile(self, *args, **kwargs):
42
43
self._actions.append(('_real_re_compile',
44
return super(InstrumentedLazyRegex, self)._real_re_compile(*args, **kwargs)
45
return super(InstrumentedLazyRegex, self)._real_re_compile(
47
49
class TestLazyRegex(tests.TestCase):
114
116
pattern = lazy_regex.lazy_compile('[,;]*')
115
117
self.assertEqual(['x', 'y', 'z'], pattern.split('x,y;z'))
119
def test_pickle(self):
120
# When pickling, just compile the regex.
121
# Sphinx, which we use for documentation, pickles
122
# some compiled regexes.
123
lazy_pattern = lazy_regex.lazy_compile('[,;]*')
124
pickled = pickle.dumps(lazy_pattern)
125
unpickled_lazy_pattern = pickle.loads(pickled)
126
self.assertEqual(['x', 'y', 'z'],
127
unpickled_lazy_pattern.split('x,y;z'))
118
130
class TestInstallLazyCompile(tests.TestCase):
131
"""Tests for lazy compiled regexps.
121
super(TestInstallLazyCompile, self).setUp()
122
self.addCleanup(lazy_regex.reset_compile)
133
Other tests, and bzrlib in general, count on the lazy regexp compiler
134
being installed, and this is done by loading bzrlib. So these tests
135
assume it is installed, and leave it installed when they're done.
124
138
def test_install(self):
139
# Don't count on it being present
125
140
lazy_regex.install_lazy_compile()
126
141
pattern = re.compile('foo')
127
142
self.assertIsInstance(pattern, lazy_regex.LazyRegex)
129
144
def test_reset(self):
130
lazy_regex.install_lazy_compile()
131
145
lazy_regex.reset_compile()
146
self.addCleanup(lazy_regex.install_lazy_compile)
132
147
pattern = re.compile('foo')
133
self.failIf(isinstance(pattern, lazy_regex.LazyRegex),
134
'lazy_regex.reset_compile() did not restore the original'
135
' compile() function %s' % (type(pattern),))
148
self.assertFalse(isinstance(pattern, lazy_regex.LazyRegex),
149
'lazy_regex.reset_compile() did not restore the original'
150
' compile() function %s' % (type(pattern),))
136
151
# but the returned object should still support regex operations
137
152
m = pattern.match('foo')
138
153
self.assertEqual('foo', m.group())