31
31
def test_invalid_pattern(self):
32
32
error = lazy_regex.InvalidPattern('Bad pattern msg.')
33
33
self.assertEqualDiff("Invalid pattern(s) found. Bad pattern msg.",
37
37
class InstrumentedLazyRegex(lazy_regex.LazyRegex):
48
48
return super(InstrumentedLazyRegex, self).__getattr__(attr)
50
50
def _real_re_compile(self, *args, **kwargs):
51
self._actions.append(('_real_re_compile',
51
self._actions.append(('_real_re_compile', args, kwargs))
53
52
return super(InstrumentedLazyRegex, self)._real_re_compile(
62
61
InstrumentedLazyRegex.use_actions(actions)
64
pattern = InstrumentedLazyRegex(args=('foo',))
63
pattern = InstrumentedLazyRegex(args=('foo',), kwargs={})
65
64
actions.append(('created regex', 'foo'))
66
65
# This match call should compile the regex and go through __getattr__
67
66
pattern.match('foo')
72
71
self.assertEqual([('created regex', 'foo'),
73
72
('__getattr__', 'match'),
74
73
('_real_re_compile', ('foo',), {}),
77
76
def test_bad_pattern(self):
78
77
"""Ensure lazy regex handles bad patterns cleanly."""
79
78
p = lazy_regex.lazy_compile('RE:[')
80
79
# As p.match is lazy, we make it into a lambda so its handled
81
80
# by assertRaises correctly.
82
e = self.assertRaises(lazy_regex.InvalidPattern, lambda: p.match('foo'))
81
e = self.assertRaises(lazy_regex.InvalidPattern,
82
lambda: p.match('foo'))
83
83
# Expect either old or new form of error message
84
84
self.assertContainsRe(e.msg, '^"RE:\\[" '
85
'(unexpected end of regular expression'
86
'|unterminated character set at position 3)$')
85
'(unexpected end of regular expression'
86
'|unterminated character set at position 3)$')
89
89
class TestLazyCompile(tests.TestCase):
134
134
lazy_pattern = lazy_regex.lazy_compile('[,;]+')
135
135
pickled = pickle.dumps(lazy_pattern)
136
136
unpickled_lazy_pattern = pickle.loads(pickled)
137
self.assertEqual(['x', 'y', 'z'],
138
unpickled_lazy_pattern.split('x,y;z'))
141
class TestInstallLazyCompile(tests.TestCase):
142
"""Tests for lazy compiled regexps.
144
Other tests, and breezy in general, count on the lazy regexp compiler
145
being installed, and this is done by loading breezy. So these tests
146
assume it is installed, and leave it installed when they're done.
149
def test_install(self):
150
# Don't count on it being present
151
lazy_regex.install_lazy_compile()
152
pattern = re.compile('foo')
153
self.assertIsInstance(pattern, lazy_regex.LazyRegex)
155
def test_reset(self):
156
lazy_regex.reset_compile()
157
self.addCleanup(lazy_regex.install_lazy_compile)
158
pattern = re.compile('foo')
159
self.assertFalse(isinstance(pattern, lazy_regex.LazyRegex),
160
'lazy_regex.reset_compile() did not restore the original'
161
' compile() function %s' % (type(pattern),))
162
# but the returned object should still support regex operations
163
m = pattern.match('foo')
164
self.assertEqual('foo', m.group())
138
['x', 'y', 'z'], unpickled_lazy_pattern.split('x,y;z'))