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'))
83
def test_findall(self):
84
pattern = lazy_regex.lazy_compile('fo*')
85
self.assertEqual(['f', 'fo', 'foo', 'fooo'],
86
pattern.findall('f fo foo fooo'))
88
def test_finditer(self):
89
pattern = lazy_regex.lazy_compile('fo*')
90
matches = [(m.start(), m.end(), m.group())
91
for m in pattern.finditer('foo bar fop')]
92
self.assertEqual([(0, 3, 'foo'), (8, 10, 'fo')], matches)
95
pattern = lazy_regex.lazy_compile('fo*')
96
self.assertIs(None, pattern.match('baz foo'))
97
self.assertEqual('fooo', pattern.match('fooo').group())
99
def test_search(self):
100
pattern = lazy_regex.lazy_compile('fo*')
101
self.assertEqual('foo', pattern.search('baz foo').group())
102
self.assertEqual('fooo', pattern.search('fooo').group())
104
def test_split(self):
105
pattern = lazy_regex.lazy_compile('[,;]*')
106
self.assertEqual(['x', 'y', 'z'], pattern.split('x,y;z'))
109
class TestInstallLazyCompile(tests.TestCase):
112
super(TestInstallLazyCompile, self).setUp()
113
self.addCleanup(lazy_regex.reset_compile)
115
def test_install(self):
116
lazy_regex.install_lazy_compile()
117
pattern = re.compile('foo')
118
self.assertIsInstance(pattern, lazy_regex.LazyRegex)
120
def test_reset(self):
121
lazy_regex.install_lazy_compile()
122
lazy_regex.reset_compile()
123
pattern = re.compile('foo')
124
self.failIf(isinstance(pattern, lazy_regex.LazyRegex),
125
'lazy_regex.reset_compile() did not restore the original'
126
' compile() function %s' % (type(pattern),))
127
# but the returned object should still support regex operations
128
m = pattern.match('foo')
129
self.assertEqual('foo', m.group())