1
# Copyright (C) 2006 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"""
21
from bzrlib import errors
28
class InstrumentedLazyRegex(lazy_regex.LazyRegex):
29
"""Keep track of actions on the lazy regex"""
34
def use_actions(cls, actions):
35
cls._actions = actions
37
def __getattr__(self, attr):
38
self._actions.append(('__getattr__', attr))
39
return super(InstrumentedLazyRegex, self).__getattr__(attr)
41
def _real_re_compile(self, *args, **kwargs):
42
self._actions.append(('_real_re_compile',
44
return super(InstrumentedLazyRegex, self)._real_re_compile(*args, **kwargs)
47
class TestLazyRegex(tests.TestCase):
49
def test_lazy_compile(self):
50
"""Make sure that LazyRegex objects compile at the right time"""
52
InstrumentedLazyRegex.use_actions(actions)
54
pattern = InstrumentedLazyRegex(args=('foo',))
55
actions.append(('created regex', 'foo'))
56
# This match call should compile the regex and go through __getattr__
58
# But a further call should not go through __getattr__ because it has
62
self.assertEqual([('created regex', 'foo'),
63
('__getattr__', 'match'),
64
('_real_re_compile', ('foo',), {}),
67
def test_bad_pattern(self):
68
"""Ensure lazy regex handles bad patterns cleanly."""
69
p = lazy_regex.lazy_compile('RE:[')
70
# As p.match is lazy, we make it into a lambda so its handled
71
# by assertRaises correctly.
72
e = self.assertRaises(errors.InvalidPattern, lambda: p.match('foo'))
73
self.assertEqual(e.msg, '"RE:[" unexpected end of regular expression')
76
class TestLazyCompile(tests.TestCase):
78
def test_simple_acts_like_regex(self):
79
"""Test that the returned object has basic regex like functionality"""
80
pattern = lazy_regex.lazy_compile('foo')
81
self.assertIsInstance(pattern, lazy_regex.LazyRegex)
82
self.assertTrue(pattern.match('foo'))
83
self.assertIs(None, pattern.match('bar'))
85
def test_extra_args(self):
86
"""Test that extra arguments are also properly passed"""
87
pattern = lazy_regex.lazy_compile('foo', re.I)
88
self.assertIsInstance(pattern, lazy_regex.LazyRegex)
89
self.assertTrue(pattern.match('foo'))
90
self.assertTrue(pattern.match('Foo'))
92
def test_findall(self):
93
pattern = lazy_regex.lazy_compile('fo*')
94
self.assertEqual(['f', 'fo', 'foo', 'fooo'],
95
pattern.findall('f fo foo fooo'))
97
def test_finditer(self):
98
pattern = lazy_regex.lazy_compile('fo*')
99
matches = [(m.start(), m.end(), m.group())
100
for m in pattern.finditer('foo bar fop')]
101
self.assertEqual([(0, 3, 'foo'), (8, 10, 'fo')], matches)
103
def test_match(self):
104
pattern = lazy_regex.lazy_compile('fo*')
105
self.assertIs(None, pattern.match('baz foo'))
106
self.assertEqual('fooo', pattern.match('fooo').group())
108
def test_search(self):
109
pattern = lazy_regex.lazy_compile('fo*')
110
self.assertEqual('foo', pattern.search('baz foo').group())
111
self.assertEqual('fooo', pattern.search('fooo').group())
113
def test_split(self):
114
pattern = lazy_regex.lazy_compile('[,;]*')
115
self.assertEqual(['x', 'y', 'z'], pattern.split('x,y;z'))
118
class TestInstallLazyCompile(tests.TestCase):
121
super(TestInstallLazyCompile, self).setUp()
122
self.addCleanup(lazy_regex.reset_compile)
124
def test_install(self):
125
lazy_regex.install_lazy_compile()
126
pattern = re.compile('foo')
127
self.assertIsInstance(pattern, lazy_regex.LazyRegex)
129
def test_reset(self):
130
lazy_regex.install_lazy_compile()
131
lazy_regex.reset_compile()
132
pattern = re.compile('foo')
133
self.failIf(isinstance(pattern, lazy_regex.LazyRegex),
134
'lazy_regex.reset_compile() did not restore the original'
135
' compile() function %s' % (type(pattern),))
136
# but the returned object should still support regex operations
137
m = pattern.match('foo')
138
self.assertEqual('foo', m.group())