13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
17
"""Test that lazy regexes are not compiled right away"""
22
from bzrlib import errors
23
21
from bzrlib import (
42
40
def _real_re_compile(self, *args, **kwargs):
43
41
self._actions.append(('_real_re_compile',
45
return super(InstrumentedLazyRegex, self)._real_re_compile(
43
return super(InstrumentedLazyRegex, self)._real_re_compile(*args, **kwargs)
49
46
class TestLazyRegex(tests.TestCase):
66
63
('_real_re_compile', ('foo',), {}),
69
def test_bad_pattern(self):
70
"""Ensure lazy regex handles bad patterns cleanly."""
71
p = lazy_regex.lazy_compile('RE:[')
72
# As p.match is lazy, we make it into a lambda so its handled
73
# by assertRaises correctly.
74
e = self.assertRaises(errors.InvalidPattern, lambda: p.match('foo'))
75
self.assertEqual(e.msg, '"RE:[" unexpected end of regular expression')
78
67
class TestLazyCompile(tests.TestCase):
116
105
pattern = lazy_regex.lazy_compile('[,;]*')
117
106
self.assertEqual(['x', 'y', 'z'], pattern.split('x,y;z'))
119
def test_pickle(self):
120
# When pickling, just compile the regex.
121
# Sphinx, which we use for documentation, pickles
122
# some compiled regexes.
123
lazy_pattern = lazy_regex.lazy_compile('[,;]*')
124
pickled = pickle.dumps(lazy_pattern)
125
unpickled_lazy_pattern = pickle.loads(pickled)
126
self.assertEqual(['x', 'y', 'z'],
127
unpickled_lazy_pattern.split('x,y;z'))
130
109
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.
112
super(TestInstallLazyCompile, self).setUp()
113
self.addCleanup(lazy_regex.reset_compile)
138
115
def test_install(self):
139
# Don't count on it being present
140
116
lazy_regex.install_lazy_compile()
141
117
pattern = re.compile('foo')
142
118
self.assertIsInstance(pattern, lazy_regex.LazyRegex)
144
120
def test_reset(self):
121
lazy_regex.install_lazy_compile()
145
122
lazy_regex.reset_compile()
146
self.addCleanup(lazy_regex.install_lazy_compile)
147
123
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),))
124
self.failIf(isinstance(pattern, lazy_regex.LazyRegex),
125
'lazy_regex.reset_compile() did not restore the original'
126
' compile() function %s' % (type(pattern),))
151
127
# but the returned object should still support regex operations
152
128
m = pattern.match('foo')
153
129
self.assertEqual('foo', m.group())