/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: Breezy landing bot
  • Author(s): Colin Watson
  • Date: 2020-11-16 21:47:08 UTC
  • mfrom: (7521.1.1 remove-lp-workaround)
  • Revision ID: breezy.the.bot@gmail.com-20201116214708-jos209mgxi41oy15
Remove breezy.git workaround for bazaar.launchpad.net.

Merged from https://code.launchpad.net/~cjwatson/brz/remove-lp-workaround/+merge/393710

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
import pickle
20
20
import re
21
21
 
22
 
from bzrlib import errors
23
 
from bzrlib import (
 
22
from .. import errors
 
23
from .. import (
24
24
    lazy_regex,
25
25
    tests,
26
26
    )
27
27
 
28
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
 
29
37
class InstrumentedLazyRegex(lazy_regex.LazyRegex):
30
38
    """Keep track of actions on the lazy regex"""
31
39
 
40
48
        return super(InstrumentedLazyRegex, self).__getattr__(attr)
41
49
 
42
50
    def _real_re_compile(self, *args, **kwargs):
43
 
        self._actions.append(('_real_re_compile',
44
 
                                               args, kwargs))
 
51
        self._actions.append(('_real_re_compile', args, kwargs))
45
52
        return super(InstrumentedLazyRegex, self)._real_re_compile(
46
53
            *args, **kwargs)
47
54
 
53
60
        actions = []
54
61
        InstrumentedLazyRegex.use_actions(actions)
55
62
 
56
 
        pattern = InstrumentedLazyRegex(args=('foo',))
 
63
        pattern = InstrumentedLazyRegex(args=('foo',), kwargs={})
57
64
        actions.append(('created regex', 'foo'))
58
65
        # This match call should compile the regex and go through __getattr__
59
66
        pattern.match('foo')
64
71
        self.assertEqual([('created regex', 'foo'),
65
72
                          ('__getattr__', 'match'),
66
73
                          ('_real_re_compile', ('foo',), {}),
67
 
                         ], actions)
 
74
                          ], actions)
68
75
 
69
76
    def test_bad_pattern(self):
70
77
        """Ensure lazy regex handles bad patterns cleanly."""
71
78
        p = lazy_regex.lazy_compile('RE:[')
72
79
        # As p.match is lazy, we make it into a lambda so its handled
73
80
        # by assertRaises correctly.
74
 
        e = self.assertRaises(errors.InvalidPattern, lambda: p.match('foo'))
75
 
        self.assertEqual(e.msg, '"RE:[" unexpected end of regular expression')
 
81
        e = self.assertRaises(lazy_regex.InvalidPattern,
 
82
                              lambda: p.match('foo'))
 
83
        # Expect either old or new form of error message
 
84
        self.assertContainsRe(e.msg, '^"RE:\\[" '
 
85
                              '(unexpected end of regular expression'
 
86
                              '|unterminated character set at position 3)$')
76
87
 
77
88
 
78
89
class TestLazyCompile(tests.TestCase):
113
124
        self.assertEqual('fooo', pattern.search('fooo').group())
114
125
 
115
126
    def test_split(self):
116
 
        pattern = lazy_regex.lazy_compile('[,;]*')
 
127
        pattern = lazy_regex.lazy_compile('[,;]+')
117
128
        self.assertEqual(['x', 'y', 'z'], pattern.split('x,y;z'))
118
129
 
119
130
    def test_pickle(self):
120
131
        # When pickling, just compile the regex.
121
132
        # Sphinx, which we use for documentation, pickles
122
133
        # some compiled regexes.
123
 
        lazy_pattern = lazy_regex.lazy_compile('[,;]*')
 
134
        lazy_pattern = lazy_regex.lazy_compile('[,;]+')
124
135
        pickled = pickle.dumps(lazy_pattern)
125
136
        unpickled_lazy_pattern = pickle.loads(pickled)
126
 
        self.assertEqual(['x', 'y', 'z'],
127
 
            unpickled_lazy_pattern.split('x,y;z'))
128
 
 
129
 
 
130
 
class TestInstallLazyCompile(tests.TestCase):
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
 
    """
137
 
 
138
 
    def test_install(self):
139
 
        # Don't count on it being present
140
 
        lazy_regex.install_lazy_compile()
141
 
        pattern = re.compile('foo')
142
 
        self.assertIsInstance(pattern, lazy_regex.LazyRegex)
143
 
 
144
 
    def test_reset(self):
145
 
        lazy_regex.reset_compile()
146
 
        self.addCleanup(lazy_regex.install_lazy_compile)
147
 
        pattern = re.compile('foo')
148
 
        self.assertFalse(isinstance(pattern, lazy_regex.LazyRegex),
149
 
            'lazy_regex.reset_compile() did not restore the original'
150
 
            ' compile() function %s' % (type(pattern),))
151
 
        # but the returned object should still support regex operations
152
 
        m = pattern.match('foo')
153
 
        self.assertEqual('foo', m.group())
 
137
        self.assertEqual(
 
138
            ['x', 'y', 'z'], unpickled_lazy_pattern.split('x,y;z'))