~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_lazy_regex.py

Initial stab at repository format support.

Show diffs side-by-side

added added

removed removed

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