/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: 2017-07-20 00:00:04 UTC
  • mfrom: (6690.5.2 bundle-guess)
  • Revision ID: jelmer@jelmer.uk-20170720000004-wlknc5gthdk3tokn
Merge lp:~jelmer/brz/bundle-guess.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Canonical Ltd
 
1
# Copyright (C) 2006, 2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
16
16
 
17
17
"""Test that lazy regexes are not compiled right away"""
18
18
 
 
19
import pickle
19
20
import re
20
21
 
21
 
from bzrlib import (
 
22
from .. import errors
 
23
from .. import (
22
24
    lazy_regex,
23
25
    tests,
24
26
    )
25
27
 
26
28
 
 
29
class ErrorTests(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
 
27
37
class InstrumentedLazyRegex(lazy_regex.LazyRegex):
28
38
    """Keep track of actions on the lazy regex"""
29
39
 
40
50
    def _real_re_compile(self, *args, **kwargs):
41
51
        self._actions.append(('_real_re_compile',
42
52
                                               args, kwargs))
43
 
        return super(InstrumentedLazyRegex, self)._real_re_compile(*args, **kwargs)
 
53
        return super(InstrumentedLazyRegex, self)._real_re_compile(
 
54
            *args, **kwargs)
44
55
 
45
56
 
46
57
class TestLazyRegex(tests.TestCase):
63
74
                          ('_real_re_compile', ('foo',), {}),
64
75
                         ], actions)
65
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, lambda: p.match('foo'))
 
83
        self.assertEqual(e.msg, '"RE:[" unexpected end of regular expression')
 
84
 
66
85
 
67
86
class TestLazyCompile(tests.TestCase):
68
87
 
105
124
        pattern = lazy_regex.lazy_compile('[,;]*')
106
125
        self.assertEqual(['x', 'y', 'z'], pattern.split('x,y;z'))
107
126
 
 
127
    def test_pickle(self):
 
128
        # When pickling, just compile the regex.
 
129
        # Sphinx, which we use for documentation, pickles
 
130
        # some compiled regexes.
 
131
        lazy_pattern = lazy_regex.lazy_compile('[,;]*')
 
132
        pickled = pickle.dumps(lazy_pattern)
 
133
        unpickled_lazy_pattern = pickle.loads(pickled)
 
134
        self.assertEqual(['x', 'y', 'z'],
 
135
            unpickled_lazy_pattern.split('x,y;z'))
 
136
 
108
137
 
109
138
class TestInstallLazyCompile(tests.TestCase):
 
139
    """Tests for lazy compiled regexps.
110
140
 
111
 
    def setUp(self):
112
 
        super(TestInstallLazyCompile, self).setUp()
113
 
        self.addCleanup(lazy_regex.reset_compile)
 
141
    Other tests, and breezy in general, count on the lazy regexp compiler
 
142
    being installed, and this is done by loading breezy.  So these tests
 
143
    assume it is installed, and leave it installed when they're done.
 
144
    """
114
145
 
115
146
    def test_install(self):
 
147
        # Don't count on it being present
116
148
        lazy_regex.install_lazy_compile()
117
149
        pattern = re.compile('foo')
118
150
        self.assertIsInstance(pattern, lazy_regex.LazyRegex)
119
151
 
120
152
    def test_reset(self):
121
 
        lazy_regex.install_lazy_compile()
122
153
        lazy_regex.reset_compile()
 
154
        self.addCleanup(lazy_regex.install_lazy_compile)
123
155
        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),))
 
156
        self.assertFalse(isinstance(pattern, lazy_regex.LazyRegex),
 
157
            'lazy_regex.reset_compile() did not restore the original'
 
158
            ' compile() function %s' % (type(pattern),))
127
159
        # but the returned object should still support regex operations
128
160
        m = pattern.match('foo')
129
161
        self.assertEqual('foo', m.group())