1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# Copyright (C) 2006, 2011 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
"""Test that lazy regexes are not compiled right away"""
import re
from bzrlib import errors
from bzrlib import (
lazy_regex,
tests,
)
class InstrumentedLazyRegex(lazy_regex.LazyRegex):
"""Keep track of actions on the lazy regex"""
_actions = []
@classmethod
def use_actions(cls, actions):
cls._actions = actions
def __getattr__(self, attr):
self._actions.append(('__getattr__', attr))
return super(InstrumentedLazyRegex, self).__getattr__(attr)
def _real_re_compile(self, *args, **kwargs):
self._actions.append(('_real_re_compile',
args, kwargs))
return super(InstrumentedLazyRegex, self)._real_re_compile(
*args, **kwargs)
class TestLazyRegex(tests.TestCase):
def test_lazy_compile(self):
"""Make sure that LazyRegex objects compile at the right time"""
actions = []
InstrumentedLazyRegex.use_actions(actions)
pattern = InstrumentedLazyRegex(args=('foo',))
actions.append(('created regex', 'foo'))
# This match call should compile the regex and go through __getattr__
pattern.match('foo')
# But a further call should not go through __getattr__ because it has
# been bound locally.
pattern.match('foo')
self.assertEqual([('created regex', 'foo'),
('__getattr__', 'match'),
('_real_re_compile', ('foo',), {}),
], actions)
def test_bad_pattern(self):
"""Ensure lazy regex handles bad patterns cleanly."""
p = lazy_regex.lazy_compile('RE:[')
# As p.match is lazy, we make it into a lambda so its handled
# by assertRaises correctly.
e = self.assertRaises(errors.InvalidPattern, lambda: p.match('foo'))
self.assertEqual(e.msg, '"RE:[" unexpected end of regular expression')
class TestLazyCompile(tests.TestCase):
def test_simple_acts_like_regex(self):
"""Test that the returned object has basic regex like functionality"""
pattern = lazy_regex.lazy_compile('foo')
self.assertIsInstance(pattern, lazy_regex.LazyRegex)
self.assertTrue(pattern.match('foo'))
self.assertIs(None, pattern.match('bar'))
def test_extra_args(self):
"""Test that extra arguments are also properly passed"""
pattern = lazy_regex.lazy_compile('foo', re.I)
self.assertIsInstance(pattern, lazy_regex.LazyRegex)
self.assertTrue(pattern.match('foo'))
self.assertTrue(pattern.match('Foo'))
def test_findall(self):
pattern = lazy_regex.lazy_compile('fo*')
self.assertEqual(['f', 'fo', 'foo', 'fooo'],
pattern.findall('f fo foo fooo'))
def test_finditer(self):
pattern = lazy_regex.lazy_compile('fo*')
matches = [(m.start(), m.end(), m.group())
for m in pattern.finditer('foo bar fop')]
self.assertEqual([(0, 3, 'foo'), (8, 10, 'fo')], matches)
def test_match(self):
pattern = lazy_regex.lazy_compile('fo*')
self.assertIs(None, pattern.match('baz foo'))
self.assertEqual('fooo', pattern.match('fooo').group())
def test_search(self):
pattern = lazy_regex.lazy_compile('fo*')
self.assertEqual('foo', pattern.search('baz foo').group())
self.assertEqual('fooo', pattern.search('fooo').group())
def test_split(self):
pattern = lazy_regex.lazy_compile('[,;]*')
self.assertEqual(['x', 'y', 'z'], pattern.split('x,y;z'))
class TestInstallLazyCompile(tests.TestCase):
"""Tests for lazy compiled regexps.
Other tests, and bzrlib in general, count on the lazy regexp compiler
being installed, and this is done by loading bzrlib. So these tests
assume it is installed, and leave it installed when they're done.
"""
def test_install(self):
# Don't count on it being present
lazy_regex.install_lazy_compile()
pattern = re.compile('foo')
self.assertIsInstance(pattern, lazy_regex.LazyRegex)
def test_reset(self):
lazy_regex.reset_compile()
self.addCleanup(lazy_regex.install_lazy_compile)
pattern = re.compile('foo')
self.assertFalse(isinstance(pattern, lazy_regex.LazyRegex),
'lazy_regex.reset_compile() did not restore the original'
' compile() function %s' % (type(pattern),))
# but the returned object should still support regex operations
m = pattern.match('foo')
self.assertEqual('foo', m.group())
|