~bzr-pqm/bzr/bzr.dev

2052.3.4 by John Arbash Meinel
[merge] bzr.dev
1
# Copyright (C) 2006 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
19
import re
20
21
from bzrlib import (
22
    lazy_regex,
23
    tests,
24
    )
25
26
27
class InstrumentedLazyRegex(lazy_regex.LazyRegex):
28
    """Keep track of actions on the lazy regex"""
29
30
    _actions = []
31
32
    @classmethod
33
    def use_actions(cls, actions):
34
        cls._actions = actions
35
36
    def __getattr__(self, attr):
37
        self._actions.append(('__getattr__', attr))
38
        return super(InstrumentedLazyRegex, self).__getattr__(attr)
39
40
    def _real_re_compile(self, *args, **kwargs):
41
        self._actions.append(('_real_re_compile',
42
                                               args, kwargs))
43
        return super(InstrumentedLazyRegex, self)._real_re_compile(*args, **kwargs)
44
45
46
class TestLazyRegex(tests.TestCase):
47
48
    def test_lazy_compile(self):
49
        """Make sure that LazyRegex objects compile at the right time"""
50
        actions = []
51
        InstrumentedLazyRegex.use_actions(actions)
52
53
        pattern = InstrumentedLazyRegex(args=('foo',))
54
        actions.append(('created regex', 'foo'))
55
        # This match call should compile the regex and go through __getattr__
56
        pattern.match('foo')
57
        # But a further call should not go through __getattr__ because it has
58
        # been bound locally.
59
        pattern.match('foo')
60
61
        self.assertEqual([('created regex', 'foo'),
62
                          ('__getattr__', 'match'),
63
                          ('_real_re_compile', ('foo',), {}),
64
                         ], actions)
65
66
67
class TestLazyCompile(tests.TestCase):
68
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'))
75
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'))
82
2063.4.2 by John Arbash Meinel
Add install and unistall functions, and tests
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'))
87
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)
93
94
    def test_match(self):
95
        pattern = lazy_regex.lazy_compile('fo*')
96
        self.assertIs(None, pattern.match('baz foo'))
97
        self.assertEqual('fooo', pattern.match('fooo').group())
98
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())
103
104
    def test_split(self):
105
        pattern = lazy_regex.lazy_compile('[,;]*')
106
        self.assertEqual(['x', 'y', 'z'], pattern.split('x,y;z'))
107
108
109
class TestInstallLazyCompile(tests.TestCase):
110
111
    def setUp(self):
112
        super(TestInstallLazyCompile, self).setUp()
113
        self.addCleanup(lazy_regex.reset_compile)
114
115
    def test_install(self):
116
        lazy_regex.install_lazy_compile()
117
        pattern = re.compile('foo')
118
        self.assertIsInstance(pattern, lazy_regex.LazyRegex)
119
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())