1
# Copyright (C) 2006, 2011 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
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
17
"""Test that lazy regexes are not compiled right away"""
22
from bzrlib import errors
29
class InstrumentedLazyRegex(lazy_regex.LazyRegex):
30
"""Keep track of actions on the lazy regex"""
35
def use_actions(cls, actions):
36
cls._actions = actions
38
def __getattr__(self, attr):
39
self._actions.append(('__getattr__', attr))
40
return super(InstrumentedLazyRegex, self).__getattr__(attr)
42
def _real_re_compile(self, *args, **kwargs):
43
self._actions.append(('_real_re_compile',
45
return super(InstrumentedLazyRegex, self)._real_re_compile(
49
class TestLazyRegex(tests.TestCase):
51
def test_lazy_compile(self):
52
"""Make sure that LazyRegex objects compile at the right time"""
54
InstrumentedLazyRegex.use_actions(actions)
56
pattern = InstrumentedLazyRegex(args=('foo',))
57
actions.append(('created regex', 'foo'))
58
# This match call should compile the regex and go through __getattr__
60
# But a further call should not go through __getattr__ because it has
64
self.assertEqual([('created regex', 'foo'),
65
('__getattr__', 'match'),
66
('_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
class TestLazyCompile(tests.TestCase):
80
def test_simple_acts_like_regex(self):
81
"""Test that the returned object has basic regex like functionality"""
82
pattern = lazy_regex.lazy_compile('foo')
83
self.assertIsInstance(pattern, lazy_regex.LazyRegex)
84
self.assertTrue(pattern.match('foo'))
85
self.assertIs(None, pattern.match('bar'))
87
def test_extra_args(self):
88
"""Test that extra arguments are also properly passed"""
89
pattern = lazy_regex.lazy_compile('foo', re.I)
90
self.assertIsInstance(pattern, lazy_regex.LazyRegex)
91
self.assertTrue(pattern.match('foo'))
92
self.assertTrue(pattern.match('Foo'))
94
def test_findall(self):
95
pattern = lazy_regex.lazy_compile('fo*')
96
self.assertEqual(['f', 'fo', 'foo', 'fooo'],
97
pattern.findall('f fo foo fooo'))
99
def test_finditer(self):
100
pattern = lazy_regex.lazy_compile('fo*')
101
matches = [(m.start(), m.end(), m.group())
102
for m in pattern.finditer('foo bar fop')]
103
self.assertEqual([(0, 3, 'foo'), (8, 10, 'fo')], matches)
105
def test_match(self):
106
pattern = lazy_regex.lazy_compile('fo*')
107
self.assertIs(None, pattern.match('baz foo'))
108
self.assertEqual('fooo', pattern.match('fooo').group())
110
def test_search(self):
111
pattern = lazy_regex.lazy_compile('fo*')
112
self.assertEqual('foo', pattern.search('baz foo').group())
113
self.assertEqual('fooo', pattern.search('fooo').group())
115
def test_split(self):
116
pattern = lazy_regex.lazy_compile('[,;]*')
117
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
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())