17
17
"""Test that lazy regexes are not compiled right away"""
29
class TestErrors(tests.TestCase):
31
def test_invalid_pattern(self):
32
error = lazy_regex.InvalidPattern('Bad pattern msg.')
33
self.assertEqualDiff("Invalid pattern(s) found. Bad pattern msg.",
27
37
class InstrumentedLazyRegex(lazy_regex.LazyRegex):
28
38
"""Keep track of actions on the lazy regex"""
40
50
def _real_re_compile(self, *args, **kwargs):
41
51
self._actions.append(('_real_re_compile',
43
return super(InstrumentedLazyRegex, self)._real_re_compile(*args, **kwargs)
53
return super(InstrumentedLazyRegex, self)._real_re_compile(
46
57
class TestLazyRegex(tests.TestCase):
61
72
self.assertEqual([('created regex', 'foo'),
62
73
('__getattr__', 'match'),
63
74
('_real_re_compile', ('foo',), {}),
77
def test_bad_pattern(self):
78
"""Ensure lazy regex handles bad patterns cleanly."""
79
p = lazy_regex.lazy_compile('RE:[')
80
# As p.match is lazy, we make it into a lambda so its handled
81
# by assertRaises correctly.
82
e = self.assertRaises(lazy_regex.InvalidPattern,
83
lambda: p.match('foo'))
84
# Expect either old or new form of error message
85
self.assertContainsRe(e.msg, '^"RE:\\[" '
86
'(unexpected end of regular expression'
87
'|unterminated character set at position 3)$')
67
90
class TestLazyCompile(tests.TestCase):
102
125
self.assertEqual('fooo', pattern.search('fooo').group())
104
127
def test_split(self):
105
pattern = lazy_regex.lazy_compile('[,;]*')
128
pattern = lazy_regex.lazy_compile('[,;]+')
106
129
self.assertEqual(['x', 'y', 'z'], pattern.split('x,y;z'))
131
def test_pickle(self):
132
# When pickling, just compile the regex.
133
# Sphinx, which we use for documentation, pickles
134
# some compiled regexes.
135
lazy_pattern = lazy_regex.lazy_compile('[,;]+')
136
pickled = pickle.dumps(lazy_pattern)
137
unpickled_lazy_pattern = pickle.loads(pickled)
138
self.assertEqual(['x', 'y', 'z'],
139
unpickled_lazy_pattern.split('x,y;z'))
109
142
class TestInstallLazyCompile(tests.TestCase):
143
"""Tests for lazy compiled regexps.
112
super(TestInstallLazyCompile, self).setUp()
113
self.addCleanup(lazy_regex.reset_compile)
145
Other tests, and breezy in general, count on the lazy regexp compiler
146
being installed, and this is done by loading breezy. So these tests
147
assume it is installed, and leave it installed when they're done.
115
150
def test_install(self):
151
# Don't count on it being present
116
152
lazy_regex.install_lazy_compile()
117
153
pattern = re.compile('foo')
118
154
self.assertIsInstance(pattern, lazy_regex.LazyRegex)
120
156
def test_reset(self):
121
lazy_regex.install_lazy_compile()
122
157
lazy_regex.reset_compile()
158
self.addCleanup(lazy_regex.install_lazy_compile)
123
159
pattern = re.compile('foo')
124
self.failIf(isinstance(pattern, lazy_regex.LazyRegex),
125
'lazy_regex.reset_compile() did not restore the original'
126
' compile() function %s' % (type(pattern),))
160
self.assertFalse(isinstance(pattern, lazy_regex.LazyRegex),
161
'lazy_regex.reset_compile() did not restore the original'
162
' compile() function %s' % (type(pattern),))
127
163
# but the returned object should still support regex operations
128
164
m = pattern.match('foo')
129
165
self.assertEqual('foo', m.group())