1
# Copyright (C) 2006 by 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Test that lazy regexes are not compiled right away"""
27
class InstrumentedLazyRegex(lazy_regex.LazyRegex):
28
"""Keep track of actions on the lazy regex"""
33
def use_actions(cls, actions):
34
cls._actions = actions
36
def __getattr__(self, attr):
37
self._actions.append(('__getattr__', attr))
38
return super(InstrumentedLazyRegex, self).__getattr__(attr)
40
def _real_re_compile(self, *args, **kwargs):
41
self._actions.append(('_real_re_compile',
43
return super(InstrumentedLazyRegex, self)._real_re_compile(*args, **kwargs)
46
class TestLazyRegex(tests.TestCase):
48
def test_lazy_compile(self):
49
"""Make sure that LazyRegex objects compile at the right time"""
51
InstrumentedLazyRegex.use_actions(actions)
53
pattern = InstrumentedLazyRegex(args=('foo',))
54
actions.append(('created regex', 'foo'))
55
# This match call should compile the regex and go through __getattr__
57
# But a further call should not go through __getattr__ because it has
61
self.assertEqual([('created regex', 'foo'),
62
('__getattr__', 'match'),
63
('_real_re_compile', ('foo',), {}),
67
class TestLazyCompile(tests.TestCase):
69
def test_simple_acts_like_regex(self):
70
"""Test that the returned object has basic regex like functionality"""
71
pattern = lazy_regex.lazy_compile('foo')
72
self.assertIsInstance(pattern, lazy_regex.LazyRegex)
73
self.assertTrue(pattern.match('foo'))
74
self.assertIs(None, pattern.match('bar'))
76
def test_extra_args(self):
77
"""Test that extra arguments are also properly passed"""
78
pattern = lazy_regex.lazy_compile('foo', re.I)
79
self.assertIsInstance(pattern, lazy_regex.LazyRegex)
80
self.assertTrue(pattern.match('foo'))
81
self.assertTrue(pattern.match('Foo'))