22
from bzrlib 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.",
29
37
class InstrumentedLazyRegex(lazy_regex.LazyRegex):
30
38
"""Keep track of actions on the lazy regex"""
40
48
return super(InstrumentedLazyRegex, self).__getattr__(attr)
42
50
def _real_re_compile(self, *args, **kwargs):
43
self._actions.append(('_real_re_compile',
51
self._actions.append(('_real_re_compile', args, kwargs))
45
52
return super(InstrumentedLazyRegex, self)._real_re_compile(
54
61
InstrumentedLazyRegex.use_actions(actions)
56
pattern = InstrumentedLazyRegex(args=('foo',))
63
pattern = InstrumentedLazyRegex(args=('foo',), kwargs={})
57
64
actions.append(('created regex', 'foo'))
58
65
# This match call should compile the regex and go through __getattr__
59
66
pattern.match('foo')
64
71
self.assertEqual([('created regex', 'foo'),
65
72
('__getattr__', 'match'),
66
73
('_real_re_compile', ('foo',), {}),
69
76
def test_bad_pattern(self):
70
77
"""Ensure lazy regex handles bad patterns cleanly."""
71
78
p = lazy_regex.lazy_compile('RE:[')
72
79
# As p.match is lazy, we make it into a lambda so its handled
73
80
# by assertRaises correctly.
74
e = self.assertRaises(errors.InvalidPattern, lambda: p.match('foo'))
75
self.assertEqual(e.msg, '"RE:[" unexpected end of regular expression')
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)$')
78
89
class TestLazyCompile(tests.TestCase):
113
124
self.assertEqual('fooo', pattern.search('fooo').group())
115
126
def test_split(self):
116
pattern = lazy_regex.lazy_compile('[,;]*')
127
pattern = lazy_regex.lazy_compile('[,;]+')
117
128
self.assertEqual(['x', 'y', 'z'], pattern.split('x,y;z'))
119
130
def test_pickle(self):
120
131
# When pickling, just compile the regex.
121
132
# Sphinx, which we use for documentation, pickles
122
133
# some compiled regexes.
123
lazy_pattern = lazy_regex.lazy_compile('[,;]*')
134
lazy_pattern = lazy_regex.lazy_compile('[,;]+')
124
135
pickled = pickle.dumps(lazy_pattern)
125
136
unpickled_lazy_pattern = pickle.loads(pickled)
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 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.
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())
138
['x', 'y', 'z'], unpickled_lazy_pattern.split('x,y;z'))