bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5967.9.2
by Martin Pool
lazy_regex tests ought to leave it installed when they're done |
1 |
# Copyright (C) 2006, 2011 Canonical Ltd
|
2063.4.1
by John Arbash Meinel
bzrlib.lazy_regex.lazy_compile creates a proxy object around re.compile() |
2 |
#
|
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.
|
|
7 |
#
|
|
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.
|
|
12 |
#
|
|
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
|
|
4183.7.1
by Sabin Iacob
update FSF mailing address |
15 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2063.4.1
by John Arbash Meinel
bzrlib.lazy_regex.lazy_compile creates a proxy object around re.compile() |
16 |
|
17 |
"""Test that lazy regexes are not compiled right away"""
|
|
18 |
||
6282.1.1
by Jelmer Vernooij
Allow lazy_regex.lazy_compile patterns to be pickled. |
19 |
import pickle |
2063.4.1
by John Arbash Meinel
bzrlib.lazy_regex.lazy_compile creates a proxy object around re.compile() |
20 |
import re |
21 |
||
6624
by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes') |
22 |
from .. import errors |
23 |
from .. import ( |
|
2063.4.1
by John Arbash Meinel
bzrlib.lazy_regex.lazy_compile creates a proxy object around re.compile() |
24 |
lazy_regex, |
25 |
tests, |
|
26 |
)
|
|
27 |
||
28 |
||
6734.1.22
by Jelmer Vernooij
review comments. |
29 |
class TestErrors(tests.TestCase): |
6729.3.1
by Jelmer Vernooij
Move lazy regex error to breezy.lazy_regex. |
30 |
|
31 |
def test_invalid_pattern(self): |
|
32 |
error = lazy_regex.InvalidPattern('Bad pattern msg.') |
|
33 |
self.assertEqualDiff("Invalid pattern(s) found. Bad pattern msg.", |
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
34 |
str(error)) |
6729.3.1
by Jelmer Vernooij
Move lazy regex error to breezy.lazy_regex. |
35 |
|
36 |
||
2063.4.1
by John Arbash Meinel
bzrlib.lazy_regex.lazy_compile creates a proxy object around re.compile() |
37 |
class InstrumentedLazyRegex(lazy_regex.LazyRegex): |
38 |
"""Keep track of actions on the lazy regex""" |
|
39 |
||
40 |
_actions = [] |
|
41 |
||
42 |
@classmethod
|
|
43 |
def use_actions(cls, actions): |
|
44 |
cls._actions = actions |
|
45 |
||
46 |
def __getattr__(self, attr): |
|
47 |
self._actions.append(('__getattr__', attr)) |
|
48 |
return super(InstrumentedLazyRegex, self).__getattr__(attr) |
|
49 |
||
50 |
def _real_re_compile(self, *args, **kwargs): |
|
7169.2.1
by Martin
Remove replacement of re.compile for lazy_regexp |
51 |
self._actions.append(('_real_re_compile', args, kwargs)) |
5967.9.2
by Martin Pool
lazy_regex tests ought to leave it installed when they're done |
52 |
return super(InstrumentedLazyRegex, self)._real_re_compile( |
53 |
*args, **kwargs) |
|
2063.4.1
by John Arbash Meinel
bzrlib.lazy_regex.lazy_compile creates a proxy object around re.compile() |
54 |
|
55 |
||
56 |
class TestLazyRegex(tests.TestCase): |
|
57 |
||
58 |
def test_lazy_compile(self): |
|
59 |
"""Make sure that LazyRegex objects compile at the right time""" |
|
60 |
actions = [] |
|
61 |
InstrumentedLazyRegex.use_actions(actions) |
|
62 |
||
7169.2.1
by Martin
Remove replacement of re.compile for lazy_regexp |
63 |
pattern = InstrumentedLazyRegex(args=('foo',), kwargs={}) |
2063.4.1
by John Arbash Meinel
bzrlib.lazy_regex.lazy_compile creates a proxy object around re.compile() |
64 |
actions.append(('created regex', 'foo')) |
65 |
# This match call should compile the regex and go through __getattr__
|
|
66 |
pattern.match('foo') |
|
67 |
# But a further call should not go through __getattr__ because it has
|
|
68 |
# been bound locally.
|
|
69 |
pattern.match('foo') |
|
70 |
||
71 |
self.assertEqual([('created regex', 'foo'), |
|
72 |
('__getattr__', 'match'), |
|
73 |
('_real_re_compile', ('foo',), {}), |
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
74 |
], actions) |
2063.4.1
by John Arbash Meinel
bzrlib.lazy_regex.lazy_compile creates a proxy object around re.compile() |
75 |
|
5326.2.4
by Parth Malwankar
updated InvalidPattern test cases to use assertRaises |
76 |
def test_bad_pattern(self): |
77 |
"""Ensure lazy regex handles bad patterns cleanly.""" |
|
78 |
p = lazy_regex.lazy_compile('RE:[') |
|
79 |
# As p.match is lazy, we make it into a lambda so its handled
|
|
80 |
# by assertRaises correctly.
|
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
81 |
e = self.assertRaises(lazy_regex.InvalidPattern, |
82 |
lambda: p.match('foo')) |
|
6754.6.1
by Martin
Fix test_lazy_re on Python 3 |
83 |
# Expect either old or new form of error message
|
6798.1.1
by Jelmer Vernooij
Properly escape backslashes. |
84 |
self.assertContainsRe(e.msg, '^"RE:\\[" ' |
7143.15.2
by Jelmer Vernooij
Run autopep8. |
85 |
'(unexpected end of regular expression'
|
86 |
'|unterminated character set at position 3)$') |
|
5326.2.4
by Parth Malwankar
updated InvalidPattern test cases to use assertRaises |
87 |
|
2063.4.1
by John Arbash Meinel
bzrlib.lazy_regex.lazy_compile creates a proxy object around re.compile() |
88 |
|
89 |
class TestLazyCompile(tests.TestCase): |
|
90 |
||
91 |
def test_simple_acts_like_regex(self): |
|
92 |
"""Test that the returned object has basic regex like functionality""" |
|
93 |
pattern = lazy_regex.lazy_compile('foo') |
|
94 |
self.assertIsInstance(pattern, lazy_regex.LazyRegex) |
|
95 |
self.assertTrue(pattern.match('foo')) |
|
96 |
self.assertIs(None, pattern.match('bar')) |
|
97 |
||
98 |
def test_extra_args(self): |
|
99 |
"""Test that extra arguments are also properly passed""" |
|
100 |
pattern = lazy_regex.lazy_compile('foo', re.I) |
|
101 |
self.assertIsInstance(pattern, lazy_regex.LazyRegex) |
|
102 |
self.assertTrue(pattern.match('foo')) |
|
103 |
self.assertTrue(pattern.match('Foo')) |
|
104 |
||
2063.4.2
by John Arbash Meinel
Add install and unistall functions, and tests |
105 |
def test_findall(self): |
106 |
pattern = lazy_regex.lazy_compile('fo*') |
|
107 |
self.assertEqual(['f', 'fo', 'foo', 'fooo'], |
|
108 |
pattern.findall('f fo foo fooo')) |
|
109 |
||
110 |
def test_finditer(self): |
|
111 |
pattern = lazy_regex.lazy_compile('fo*') |
|
112 |
matches = [(m.start(), m.end(), m.group()) |
|
113 |
for m in pattern.finditer('foo bar fop')] |
|
114 |
self.assertEqual([(0, 3, 'foo'), (8, 10, 'fo')], matches) |
|
115 |
||
116 |
def test_match(self): |
|
117 |
pattern = lazy_regex.lazy_compile('fo*') |
|
118 |
self.assertIs(None, pattern.match('baz foo')) |
|
119 |
self.assertEqual('fooo', pattern.match('fooo').group()) |
|
120 |
||
121 |
def test_search(self): |
|
122 |
pattern = lazy_regex.lazy_compile('fo*') |
|
123 |
self.assertEqual('foo', pattern.search('baz foo').group()) |
|
124 |
self.assertEqual('fooo', pattern.search('fooo').group()) |
|
125 |
||
126 |
def test_split(self): |
|
6754.6.1
by Martin
Fix test_lazy_re on Python 3 |
127 |
pattern = lazy_regex.lazy_compile('[,;]+') |
2063.4.2
by John Arbash Meinel
Add install and unistall functions, and tests |
128 |
self.assertEqual(['x', 'y', 'z'], pattern.split('x,y;z')) |
129 |
||
6282.1.1
by Jelmer Vernooij
Allow lazy_regex.lazy_compile patterns to be pickled. |
130 |
def test_pickle(self): |
131 |
# When pickling, just compile the regex.
|
|
132 |
# Sphinx, which we use for documentation, pickles
|
|
133 |
# some compiled regexes.
|
|
6754.6.1
by Martin
Fix test_lazy_re on Python 3 |
134 |
lazy_pattern = lazy_regex.lazy_compile('[,;]+') |
6282.1.1
by Jelmer Vernooij
Allow lazy_regex.lazy_compile patterns to be pickled. |
135 |
pickled = pickle.dumps(lazy_pattern) |
136 |
unpickled_lazy_pattern = pickle.loads(pickled) |
|
7169.2.4
by Martin
Merge trunk to resolve conflicts |
137 |
self.assertEqual( |
138 |
['x', 'y', 'z'], unpickled_lazy_pattern.split('x,y;z')) |