1
# Copyright (C) 2006, 2011 Canonical Ltd
1
# Copyright (C) 2006 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
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"""
21
from bzrlib import errors
22
21
from bzrlib import (
41
40
def _real_re_compile(self, *args, **kwargs):
42
41
self._actions.append(('_real_re_compile',
44
return super(InstrumentedLazyRegex, self)._real_re_compile(
43
return super(InstrumentedLazyRegex, self)._real_re_compile(*args, **kwargs)
48
46
class TestLazyRegex(tests.TestCase):
65
63
('_real_re_compile', ('foo',), {}),
68
def test_bad_pattern(self):
69
"""Ensure lazy regex handles bad patterns cleanly."""
70
p = lazy_regex.lazy_compile('RE:[')
71
# As p.match is lazy, we make it into a lambda so its handled
72
# by assertRaises correctly.
73
e = self.assertRaises(errors.InvalidPattern, lambda: p.match('foo'))
74
self.assertEqual(e.msg, '"RE:[" unexpected end of regular expression')
77
67
class TestLazyCompile(tests.TestCase):
119
109
class TestInstallLazyCompile(tests.TestCase):
120
"""Tests for lazy compiled regexps.
122
Other tests, and bzrlib in general, count on the lazy regexp compiler
123
being installed, and this is done by loading bzrlib. So these tests
124
assume it is installed, and leave it installed when they're done.
112
super(TestInstallLazyCompile, self).setUp()
113
self.addCleanup(lazy_regex.reset_compile)
127
115
def test_install(self):
128
# Don't count on it being present
129
116
lazy_regex.install_lazy_compile()
130
117
pattern = re.compile('foo')
131
118
self.assertIsInstance(pattern, lazy_regex.LazyRegex)
133
120
def test_reset(self):
121
lazy_regex.install_lazy_compile()
134
122
lazy_regex.reset_compile()
135
self.addCleanup(lazy_regex.install_lazy_compile)
136
123
pattern = re.compile('foo')
137
self.assertFalse(isinstance(pattern, lazy_regex.LazyRegex),
138
'lazy_regex.reset_compile() did not restore the original'
139
' 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),))
140
127
# but the returned object should still support regex operations
141
128
m = pattern.match('foo')
142
129
self.assertEqual('foo', m.group())