/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 brzlib/tests/test_lazy_regex.py

  • Committer: Jelmer Vernooij
  • Date: 2017-05-21 12:41:27 UTC
  • mto: This revision was merged to the branch mainline in revision 6623.
  • Revision ID: jelmer@jelmer.uk-20170521124127-iv8etg0vwymyai6y
s/bzr/brz/ in apport config.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
import pickle
20
20
import re
21
21
 
22
 
from .. import errors
23
 
from .. import (
 
22
from brzlib import errors
 
23
from brzlib 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
 
 
37
29
class InstrumentedLazyRegex(lazy_regex.LazyRegex):
38
30
    """Keep track of actions on the lazy regex"""
39
31
 
48
40
        return super(InstrumentedLazyRegex, self).__getattr__(attr)
49
41
 
50
42
    def _real_re_compile(self, *args, **kwargs):
51
 
        self._actions.append(('_real_re_compile', args, kwargs))
 
43
        self._actions.append(('_real_re_compile',
 
44
                                               args, kwargs))
52
45
        return super(InstrumentedLazyRegex, self)._real_re_compile(
53
46
            *args, **kwargs)
54
47
 
60
53
        actions = []
61
54
        InstrumentedLazyRegex.use_actions(actions)
62
55
 
63
 
        pattern = InstrumentedLazyRegex(args=('foo',), kwargs={})
 
56
        pattern = InstrumentedLazyRegex(args=('foo',))
64
57
        actions.append(('created regex', 'foo'))
65
58
        # This match call should compile the regex and go through __getattr__
66
59
        pattern.match('foo')
71
64
        self.assertEqual([('created regex', 'foo'),
72
65
                          ('__getattr__', 'match'),
73
66
                          ('_real_re_compile', ('foo',), {}),
74
 
                          ], actions)
 
67
                         ], actions)
75
68
 
76
69
    def test_bad_pattern(self):
77
70
        """Ensure lazy regex handles bad patterns cleanly."""
78
71
        p = lazy_regex.lazy_compile('RE:[')
79
72
        # As p.match is lazy, we make it into a lambda so its handled
80
73
        # by assertRaises correctly.
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)$')
 
74
        e = self.assertRaises(errors.InvalidPattern, lambda: p.match('foo'))
 
75
        self.assertEqual(e.msg, '"RE:[" unexpected end of regular expression')
87
76
 
88
77
 
89
78
class TestLazyCompile(tests.TestCase):
124
113
        self.assertEqual('fooo', pattern.search('fooo').group())
125
114
 
126
115
    def test_split(self):
127
 
        pattern = lazy_regex.lazy_compile('[,;]+')
 
116
        pattern = lazy_regex.lazy_compile('[,;]*')
128
117
        self.assertEqual(['x', 'y', 'z'], pattern.split('x,y;z'))
129
118
 
130
119
    def test_pickle(self):
131
120
        # When pickling, just compile the regex.
132
121
        # Sphinx, which we use for documentation, pickles
133
122
        # some compiled regexes.
134
 
        lazy_pattern = lazy_regex.lazy_compile('[,;]+')
 
123
        lazy_pattern = lazy_regex.lazy_compile('[,;]*')
135
124
        pickled = pickle.dumps(lazy_pattern)
136
125
        unpickled_lazy_pattern = pickle.loads(pickled)
137
 
        self.assertEqual(
138
 
            ['x', 'y', 'z'], unpickled_lazy_pattern.split('x,y;z'))
 
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 brzlib in general, count on the lazy regexp compiler
 
134
    being installed, and this is done by loading brzlib.  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())