29
class TestErrors(tests.TestCase):
29
class ErrorTests(tests.TestCase):
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', args, kwargs))
51
self._actions.append(('_real_re_compile',
52
53
return super(InstrumentedLazyRegex, self)._real_re_compile(
61
62
InstrumentedLazyRegex.use_actions(actions)
63
pattern = InstrumentedLazyRegex(args=('foo',), kwargs={})
64
pattern = InstrumentedLazyRegex(args=('foo',))
64
65
actions.append(('created regex', 'foo'))
65
66
# This match call should compile the regex and go through __getattr__
66
67
pattern.match('foo')
71
72
self.assertEqual([('created regex', 'foo'),
72
73
('__getattr__', 'match'),
73
74
('_real_re_compile', ('foo',), {}),
76
77
def test_bad_pattern(self):
77
78
"""Ensure lazy regex handles bad patterns cleanly."""
78
79
p = lazy_regex.lazy_compile('RE:[')
79
80
# As p.match is lazy, we make it into a lambda so its handled
80
81
# by assertRaises correctly.
81
e = self.assertRaises(lazy_regex.InvalidPattern,
82
lambda: p.match('foo'))
83
# Expect either old or new form of error message
84
self.assertContainsRe(e.msg, '^"RE:\\[" '
85
'(unexpected end of regular expression'
86
'|unterminated character set at position 3)$')
82
e = self.assertRaises(lazy_regex.InvalidPattern, lambda: p.match('foo'))
83
self.assertEqual(e.msg, '"RE:[" unexpected end of regular expression')
89
86
class TestLazyCompile(tests.TestCase):
124
121
self.assertEqual('fooo', pattern.search('fooo').group())
126
123
def test_split(self):
127
pattern = lazy_regex.lazy_compile('[,;]+')
124
pattern = lazy_regex.lazy_compile('[,;]*')
128
125
self.assertEqual(['x', 'y', 'z'], pattern.split('x,y;z'))
130
127
def test_pickle(self):
131
128
# When pickling, just compile the regex.
132
129
# Sphinx, which we use for documentation, pickles
133
130
# some compiled regexes.
134
lazy_pattern = lazy_regex.lazy_compile('[,;]+')
131
lazy_pattern = lazy_regex.lazy_compile('[,;]*')
135
132
pickled = pickle.dumps(lazy_pattern)
136
133
unpickled_lazy_pattern = pickle.loads(pickled)
138
['x', 'y', 'z'], unpickled_lazy_pattern.split('x,y;z'))
134
self.assertEqual(['x', 'y', 'z'],
135
unpickled_lazy_pattern.split('x,y;z'))
138
class TestInstallLazyCompile(tests.TestCase):
139
"""Tests for lazy compiled regexps.
141
Other tests, and breezy in general, count on the lazy regexp compiler
142
being installed, and this is done by loading breezy. So these tests
143
assume it is installed, and leave it installed when they're done.
146
def test_install(self):
147
# Don't count on it being present
148
lazy_regex.install_lazy_compile()
149
pattern = re.compile('foo')
150
self.assertIsInstance(pattern, lazy_regex.LazyRegex)
152
def test_reset(self):
153
lazy_regex.reset_compile()
154
self.addCleanup(lazy_regex.install_lazy_compile)
155
pattern = re.compile('foo')
156
self.assertFalse(isinstance(pattern, lazy_regex.LazyRegex),
157
'lazy_regex.reset_compile() did not restore the original'
158
' compile() function %s' % (type(pattern),))
159
# but the returned object should still support regex operations
160
m = pattern.match('foo')
161
self.assertEqual('foo', m.group())