/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_lazy_regex.py

  • Committer: Jelmer Vernooij
  • Date: 2018-11-16 11:42:27 UTC
  • mto: (7143.16.20 even-more-cleanups)
  • mto: This revision was merged to the branch mainline in revision 7175.
  • Revision ID: jelmer@jelmer.uk-20181116114227-lwabsodakoymo3ew
Remove flake8 issues now fixed.

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 pickle
 
20
import re
 
21
 
 
22
from .. import errors
 
23
from .. import (
 
24
    lazy_regex,
 
25
    tests,
 
26
    )
 
27
 
 
28
 
 
29
class TestErrors(tests.TestCase):
 
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.",
 
34
                             str(error))
 
35
 
 
36
 
 
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):
 
51
        self._actions.append(('_real_re_compile',
 
52
                              args, kwargs))
 
53
        return super(InstrumentedLazyRegex, self)._real_re_compile(
 
54
            *args, **kwargs)
 
55
 
 
56
 
 
57
class TestLazyRegex(tests.TestCase):
 
58
 
 
59
    def test_lazy_compile(self):
 
60
        """Make sure that LazyRegex objects compile at the right time"""
 
61
        actions = []
 
62
        InstrumentedLazyRegex.use_actions(actions)
 
63
 
 
64
        pattern = InstrumentedLazyRegex(args=('foo',))
 
65
        actions.append(('created regex', 'foo'))
 
66
        # This match call should compile the regex and go through __getattr__
 
67
        pattern.match('foo')
 
68
        # But a further call should not go through __getattr__ because it has
 
69
        # been bound locally.
 
70
        pattern.match('foo')
 
71
 
 
72
        self.assertEqual([('created regex', 'foo'),
 
73
                          ('__getattr__', 'match'),
 
74
                          ('_real_re_compile', ('foo',), {}),
 
75
                          ], actions)
 
76
 
 
77
    def test_bad_pattern(self):
 
78
        """Ensure lazy regex handles bad patterns cleanly."""
 
79
        p = lazy_regex.lazy_compile('RE:[')
 
80
        # As p.match is lazy, we make it into a lambda so its handled
 
81
        # by assertRaises correctly.
 
82
        e = self.assertRaises(lazy_regex.InvalidPattern,
 
83
                              lambda: p.match('foo'))
 
84
        # Expect either old or new form of error message
 
85
        self.assertContainsRe(e.msg, '^"RE:\\[" '
 
86
                              '(unexpected end of regular expression'
 
87
                              '|unterminated character set at position 3)$')
 
88
 
 
89
 
 
90
class TestLazyCompile(tests.TestCase):
 
91
 
 
92
    def test_simple_acts_like_regex(self):
 
93
        """Test that the returned object has basic regex like functionality"""
 
94
        pattern = lazy_regex.lazy_compile('foo')
 
95
        self.assertIsInstance(pattern, lazy_regex.LazyRegex)
 
96
        self.assertTrue(pattern.match('foo'))
 
97
        self.assertIs(None, pattern.match('bar'))
 
98
 
 
99
    def test_extra_args(self):
 
100
        """Test that extra arguments are also properly passed"""
 
101
        pattern = lazy_regex.lazy_compile('foo', re.I)
 
102
        self.assertIsInstance(pattern, lazy_regex.LazyRegex)
 
103
        self.assertTrue(pattern.match('foo'))
 
104
        self.assertTrue(pattern.match('Foo'))
 
105
 
 
106
    def test_findall(self):
 
107
        pattern = lazy_regex.lazy_compile('fo*')
 
108
        self.assertEqual(['f', 'fo', 'foo', 'fooo'],
 
109
                         pattern.findall('f fo foo fooo'))
 
110
 
 
111
    def test_finditer(self):
 
112
        pattern = lazy_regex.lazy_compile('fo*')
 
113
        matches = [(m.start(), m.end(), m.group())
 
114
                   for m in pattern.finditer('foo bar fop')]
 
115
        self.assertEqual([(0, 3, 'foo'), (8, 10, 'fo')], matches)
 
116
 
 
117
    def test_match(self):
 
118
        pattern = lazy_regex.lazy_compile('fo*')
 
119
        self.assertIs(None, pattern.match('baz foo'))
 
120
        self.assertEqual('fooo', pattern.match('fooo').group())
 
121
 
 
122
    def test_search(self):
 
123
        pattern = lazy_regex.lazy_compile('fo*')
 
124
        self.assertEqual('foo', pattern.search('baz foo').group())
 
125
        self.assertEqual('fooo', pattern.search('fooo').group())
 
126
 
 
127
    def test_split(self):
 
128
        pattern = lazy_regex.lazy_compile('[,;]+')
 
129
        self.assertEqual(['x', 'y', 'z'], pattern.split('x,y;z'))
 
130
 
 
131
    def test_pickle(self):
 
132
        # When pickling, just compile the regex.
 
133
        # Sphinx, which we use for documentation, pickles
 
134
        # some compiled regexes.
 
135
        lazy_pattern = lazy_regex.lazy_compile('[,;]+')
 
136
        pickled = pickle.dumps(lazy_pattern)
 
137
        unpickled_lazy_pattern = pickle.loads(pickled)
 
138
        self.assertEqual(['x', 'y', 'z'],
 
139
                         unpickled_lazy_pattern.split('x,y;z'))
 
140
 
 
141
 
 
142
class TestInstallLazyCompile(tests.TestCase):
 
143
    """Tests for lazy compiled regexps.
 
144
 
 
145
    Other tests, and breezy in general, count on the lazy regexp compiler
 
146
    being installed, and this is done by loading breezy.  So these tests
 
147
    assume it is installed, and leave it installed when they're done.
 
148
    """
 
149
 
 
150
    def test_install(self):
 
151
        # Don't count on it being present
 
152
        lazy_regex.install_lazy_compile()
 
153
        pattern = re.compile('foo')
 
154
        self.assertIsInstance(pattern, lazy_regex.LazyRegex)
 
155
 
 
156
    def test_reset(self):
 
157
        lazy_regex.reset_compile()
 
158
        self.addCleanup(lazy_regex.install_lazy_compile)
 
159
        pattern = re.compile('foo')
 
160
        self.assertFalse(isinstance(pattern, lazy_regex.LazyRegex),
 
161
                         'lazy_regex.reset_compile() did not restore the original'
 
162
                         ' compile() function %s' % (type(pattern),))
 
163
        # but the returned object should still support regex operations
 
164
        m = pattern.match('foo')
 
165
        self.assertEqual('foo', m.group())