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"""
29
class ErrorTests(tests.TestCase):
31
def test_invalid_pattern(self):
32
error = lazy_regex.InvalidPattern('Bad pattern msg.')
33
self.assertEqualDiff("Invalid pattern(s) found. Bad pattern msg.",
37
class InstrumentedLazyRegex(lazy_regex.LazyRegex):
38
"""Keep track of actions on the lazy regex"""
43
def use_actions(cls, actions):
44
cls._actions = actions
46
def __getattr__(self, attr):
47
self._actions.append(('__getattr__', attr))
48
return super(InstrumentedLazyRegex, self).__getattr__(attr)
50
def _real_re_compile(self, *args, **kwargs):
51
self._actions.append(('_real_re_compile',
53
return super(InstrumentedLazyRegex, self)._real_re_compile(
57
class TestLazyRegex(tests.TestCase):
59
def test_lazy_compile(self):
60
"""Make sure that LazyRegex objects compile at the right time"""
62
InstrumentedLazyRegex.use_actions(actions)
64
pattern = InstrumentedLazyRegex(args=('foo',))
65
actions.append(('created regex', 'foo'))
66
# This match call should compile the regex and go through __getattr__
68
# But a further call should not go through __getattr__ because it has
72
self.assertEqual([('created regex', 'foo'),
73
('__getattr__', 'match'),
74
('_real_re_compile', ('foo',), {}),
77
def test_bad_pattern(self):
78
"""Ensure lazy regex handles bad patterns cleanly."""
79
p = lazy_regex.lazy_compile('RE:[')
80
# As p.match is lazy, we make it into a lambda so its handled
81
# by assertRaises correctly.
82
e = self.assertRaises(lazy_regex.InvalidPattern, lambda: p.match('foo'))
83
self.assertEqual(e.msg, '"RE:[" unexpected end of regular expression')
86
class TestLazyCompile(tests.TestCase):
88
def test_simple_acts_like_regex(self):
89
"""Test that the returned object has basic regex like functionality"""
90
pattern = lazy_regex.lazy_compile('foo')
91
self.assertIsInstance(pattern, lazy_regex.LazyRegex)
92
self.assertTrue(pattern.match('foo'))
93
self.assertIs(None, pattern.match('bar'))
95
def test_extra_args(self):
96
"""Test that extra arguments are also properly passed"""
97
pattern = lazy_regex.lazy_compile('foo', re.I)
98
self.assertIsInstance(pattern, lazy_regex.LazyRegex)
99
self.assertTrue(pattern.match('foo'))
100
self.assertTrue(pattern.match('Foo'))
102
def test_findall(self):
103
pattern = lazy_regex.lazy_compile('fo*')
104
self.assertEqual(['f', 'fo', 'foo', 'fooo'],
105
pattern.findall('f fo foo fooo'))
107
def test_finditer(self):
108
pattern = lazy_regex.lazy_compile('fo*')
109
matches = [(m.start(), m.end(), m.group())
110
for m in pattern.finditer('foo bar fop')]
111
self.assertEqual([(0, 3, 'foo'), (8, 10, 'fo')], matches)
113
def test_match(self):
114
pattern = lazy_regex.lazy_compile('fo*')
115
self.assertIs(None, pattern.match('baz foo'))
116
self.assertEqual('fooo', pattern.match('fooo').group())
118
def test_search(self):
119
pattern = lazy_regex.lazy_compile('fo*')
120
self.assertEqual('foo', pattern.search('baz foo').group())
121
self.assertEqual('fooo', pattern.search('fooo').group())
123
def test_split(self):
124
pattern = lazy_regex.lazy_compile('[,;]*')
125
self.assertEqual(['x', 'y', 'z'], pattern.split('x,y;z'))
127
def test_pickle(self):
128
# When pickling, just compile the regex.
129
# Sphinx, which we use for documentation, pickles
130
# some compiled regexes.
131
lazy_pattern = lazy_regex.lazy_compile('[,;]*')
132
pickled = pickle.dumps(lazy_pattern)
133
unpickled_lazy_pattern = pickle.loads(pickled)
134
self.assertEqual(['x', 'y', 'z'],
135
unpickled_lazy_pattern.split('x,y;z'))
138
class TestInstallLazyCompile(tests.TestCase):
139
"""Tests for lazy compiled regexps.
141
Other tests, and breezy in general, count on the lazy regexp compiler
142
being installed, and this is done by loading breezy. So these tests
143
assume it is installed, and leave it installed when they're done.
146
def test_install(self):
147
# Don't count on it being present
148
lazy_regex.install_lazy_compile()
149
pattern = re.compile('foo')
150
self.assertIsInstance(pattern, lazy_regex.LazyRegex)
152
def test_reset(self):
153
lazy_regex.reset_compile()
154
self.addCleanup(lazy_regex.install_lazy_compile)
155
pattern = re.compile('foo')
156
self.assertFalse(isinstance(pattern, lazy_regex.LazyRegex),
157
'lazy_regex.reset_compile() did not restore the original'
158
' compile() function %s' % (type(pattern),))
159
# but the returned object should still support regex operations
160
m = pattern.match('foo')
161
self.assertEqual('foo', m.group())