~bzr-pqm/bzr/bzr.dev

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
5326.2.2 by Parth Malwankar
added tests for InvalidPattern errors
22
from bzrlib import errors
2063.4.1 by John Arbash Meinel
bzrlib.lazy_regex.lazy_compile creates a proxy object around re.compile()
23
from bzrlib import (
24
    lazy_regex,
25
    tests,
26
    )
27
28
29
class InstrumentedLazyRegex(lazy_regex.LazyRegex):
30
    """Keep track of actions on the lazy regex"""
31
32
    _actions = []
33
34
    @classmethod
35
    def use_actions(cls, actions):
36
        cls._actions = actions
37
38
    def __getattr__(self, attr):
39
        self._actions.append(('__getattr__', attr))
40
        return super(InstrumentedLazyRegex, self).__getattr__(attr)
41
42
    def _real_re_compile(self, *args, **kwargs):
43
        self._actions.append(('_real_re_compile',
44
                                               args, kwargs))
5967.9.2 by Martin Pool
lazy_regex tests ought to leave it installed when they're done
45
        return super(InstrumentedLazyRegex, self)._real_re_compile(
46
            *args, **kwargs)
2063.4.1 by John Arbash Meinel
bzrlib.lazy_regex.lazy_compile creates a proxy object around re.compile()
47
48
49
class TestLazyRegex(tests.TestCase):
50
51
    def test_lazy_compile(self):
52
        """Make sure that LazyRegex objects compile at the right time"""
53
        actions = []
54
        InstrumentedLazyRegex.use_actions(actions)
55
56
        pattern = InstrumentedLazyRegex(args=('foo',))
57
        actions.append(('created regex', 'foo'))
58
        # This match call should compile the regex and go through __getattr__
59
        pattern.match('foo')
60
        # But a further call should not go through __getattr__ because it has
61
        # been bound locally.
62
        pattern.match('foo')
63
64
        self.assertEqual([('created regex', 'foo'),
65
                          ('__getattr__', 'match'),
66
                          ('_real_re_compile', ('foo',), {}),
67
                         ], actions)
68
5326.2.4 by Parth Malwankar
updated InvalidPattern test cases to use assertRaises
69
    def test_bad_pattern(self):
70
        """Ensure lazy regex handles bad patterns cleanly."""
71
        p = lazy_regex.lazy_compile('RE:[')
72
        # As p.match is lazy, we make it into a lambda so its handled
73
        # by assertRaises correctly.
74
        e = self.assertRaises(errors.InvalidPattern, lambda: p.match('foo'))
5339.1.1 by Parth Malwankar
fixes errors.InvalidPattern to work on Python2.5
75
        self.assertEqual(e.msg, '"RE:[" unexpected end of regular expression')
5326.2.4 by Parth Malwankar
updated InvalidPattern test cases to use assertRaises
76
2063.4.1 by John Arbash Meinel
bzrlib.lazy_regex.lazy_compile creates a proxy object around re.compile()
77
78
class TestLazyCompile(tests.TestCase):
79
80
    def test_simple_acts_like_regex(self):
81
        """Test that the returned object has basic regex like functionality"""
82
        pattern = lazy_regex.lazy_compile('foo')
83
        self.assertIsInstance(pattern, lazy_regex.LazyRegex)
84
        self.assertTrue(pattern.match('foo'))
85
        self.assertIs(None, pattern.match('bar'))
86
87
    def test_extra_args(self):
88
        """Test that extra arguments are also properly passed"""
89
        pattern = lazy_regex.lazy_compile('foo', re.I)
90
        self.assertIsInstance(pattern, lazy_regex.LazyRegex)
91
        self.assertTrue(pattern.match('foo'))
92
        self.assertTrue(pattern.match('Foo'))
93
2063.4.2 by John Arbash Meinel
Add install and unistall functions, and tests
94
    def test_findall(self):
95
        pattern = lazy_regex.lazy_compile('fo*')
96
        self.assertEqual(['f', 'fo', 'foo', 'fooo'],
97
                         pattern.findall('f fo foo fooo'))
98
99
    def test_finditer(self):
100
        pattern = lazy_regex.lazy_compile('fo*')
101
        matches = [(m.start(), m.end(), m.group())
102
                   for m in pattern.finditer('foo bar fop')]
103
        self.assertEqual([(0, 3, 'foo'), (8, 10, 'fo')], matches)
104
105
    def test_match(self):
106
        pattern = lazy_regex.lazy_compile('fo*')
107
        self.assertIs(None, pattern.match('baz foo'))
108
        self.assertEqual('fooo', pattern.match('fooo').group())
109
110
    def test_search(self):
111
        pattern = lazy_regex.lazy_compile('fo*')
112
        self.assertEqual('foo', pattern.search('baz foo').group())
113
        self.assertEqual('fooo', pattern.search('fooo').group())
114
115
    def test_split(self):
116
        pattern = lazy_regex.lazy_compile('[,;]*')
117
        self.assertEqual(['x', 'y', 'z'], pattern.split('x,y;z'))
118
6282.1.1 by Jelmer Vernooij
Allow lazy_regex.lazy_compile patterns to be pickled.
119
    def test_pickle(self):
120
        # When pickling, just compile the regex.
121
        # Sphinx, which we use for documentation, pickles
122
        # some compiled regexes.
123
        lazy_pattern = lazy_regex.lazy_compile('[,;]*')
124
        pickled = pickle.dumps(lazy_pattern)
125
        unpickled_lazy_pattern = pickle.loads(pickled)
126
        self.assertEqual(['x', 'y', 'z'],
127
            unpickled_lazy_pattern.split('x,y;z'))
128
2063.4.2 by John Arbash Meinel
Add install and unistall functions, and tests
129
130
class TestInstallLazyCompile(tests.TestCase):
5967.9.2 by Martin Pool
lazy_regex tests ought to leave it installed when they're done
131
    """Tests for lazy compiled regexps.
132
133
    Other tests, and bzrlib in general, count on the lazy regexp compiler
134
    being installed, and this is done by loading bzrlib.  So these tests
135
    assume it is installed, and leave it installed when they're done.
136
    """
2063.4.2 by John Arbash Meinel
Add install and unistall functions, and tests
137
138
    def test_install(self):
5967.9.6 by Martin Pool
lazy regexp tests should install it explicitly
139
        # Don't count on it being present
140
        lazy_regex.install_lazy_compile()
2063.4.2 by John Arbash Meinel
Add install and unistall functions, and tests
141
        pattern = re.compile('foo')
142
        self.assertIsInstance(pattern, lazy_regex.LazyRegex)
143
144
    def test_reset(self):
145
        lazy_regex.reset_compile()
5967.9.2 by Martin Pool
lazy_regex tests ought to leave it installed when they're done
146
        self.addCleanup(lazy_regex.install_lazy_compile)
2063.4.2 by John Arbash Meinel
Add install and unistall functions, and tests
147
        pattern = re.compile('foo')
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
148
        self.assertFalse(isinstance(pattern, lazy_regex.LazyRegex),
5967.9.2 by Martin Pool
lazy_regex tests ought to leave it installed when they're done
149
            'lazy_regex.reset_compile() did not restore the original'
150
            ' compile() function %s' % (type(pattern),))
2063.4.2 by John Arbash Meinel
Add install and unistall functions, and tests
151
        # but the returned object should still support regex operations
152
        m = pattern.match('foo')
153
        self.assertEqual('foo', m.group())