22
from brzlib import errors
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.",
37
29
class InstrumentedLazyRegex(lazy_regex.LazyRegex):
38
30
"""Keep track of actions on the lazy regex"""
48
40
return super(InstrumentedLazyRegex, self).__getattr__(attr)
50
42
def _real_re_compile(self, *args, **kwargs):
51
self._actions.append(('_real_re_compile', args, kwargs))
43
self._actions.append(('_real_re_compile',
52
45
return super(InstrumentedLazyRegex, self)._real_re_compile(
61
54
InstrumentedLazyRegex.use_actions(actions)
63
pattern = InstrumentedLazyRegex(args=('foo',), kwargs={})
56
pattern = InstrumentedLazyRegex(args=('foo',))
64
57
actions.append(('created regex', 'foo'))
65
58
# This match call should compile the regex and go through __getattr__
66
59
pattern.match('foo')
71
64
self.assertEqual([('created regex', 'foo'),
72
65
('__getattr__', 'match'),
73
66
('_real_re_compile', ('foo',), {}),
76
69
def test_bad_pattern(self):
77
70
"""Ensure lazy regex handles bad patterns cleanly."""
78
71
p = lazy_regex.lazy_compile('RE:[')
79
72
# As p.match is lazy, we make it into a lambda so its handled
80
73
# 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)$')
74
e = self.assertRaises(errors.InvalidPattern, lambda: p.match('foo'))
75
self.assertEqual(e.msg, '"RE:[" unexpected end of regular expression')
89
78
class TestLazyCompile(tests.TestCase):
124
113
self.assertEqual('fooo', pattern.search('fooo').group())
126
115
def test_split(self):
127
pattern = lazy_regex.lazy_compile('[,;]+')
116
pattern = lazy_regex.lazy_compile('[,;]*')
128
117
self.assertEqual(['x', 'y', 'z'], pattern.split('x,y;z'))
130
119
def test_pickle(self):
131
120
# When pickling, just compile the regex.
132
121
# Sphinx, which we use for documentation, pickles
133
122
# some compiled regexes.
134
lazy_pattern = lazy_regex.lazy_compile('[,;]+')
123
lazy_pattern = lazy_regex.lazy_compile('[,;]*')
135
124
pickled = pickle.dumps(lazy_pattern)
136
125
unpickled_lazy_pattern = pickle.loads(pickled)
138
['x', 'y', 'z'], unpickled_lazy_pattern.split('x,y;z'))
126
self.assertEqual(['x', 'y', 'z'],
127
unpickled_lazy_pattern.split('x,y;z'))
130
class TestInstallLazyCompile(tests.TestCase):
131
"""Tests for lazy compiled regexps.
133
Other tests, and brzlib in general, count on the lazy regexp compiler
134
being installed, and this is done by loading brzlib. So these tests
135
assume it is installed, and leave it installed when they're done.
138
def test_install(self):
139
# Don't count on it being present
140
lazy_regex.install_lazy_compile()
141
pattern = re.compile('foo')
142
self.assertIsInstance(pattern, lazy_regex.LazyRegex)
144
def test_reset(self):
145
lazy_regex.reset_compile()
146
self.addCleanup(lazy_regex.install_lazy_compile)
147
pattern = re.compile('foo')
148
self.assertFalse(isinstance(pattern, lazy_regex.LazyRegex),
149
'lazy_regex.reset_compile() did not restore the original'
150
' compile() function %s' % (type(pattern),))
151
# but the returned object should still support regex operations
152
m = pattern.match('foo')
153
self.assertEqual('foo', m.group())