/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: 2020-05-06 02:13:25 UTC
  • mfrom: (7490.7.21 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200506021325-awbmmqu1zyorz7sj
Merge 3.1 branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
    def test_invalid_pattern(self):
32
32
        error = lazy_regex.InvalidPattern('Bad pattern msg.')
33
33
        self.assertEqualDiff("Invalid pattern(s) found. Bad pattern msg.",
34
 
            str(error))
 
34
                             str(error))
35
35
 
36
36
 
37
37
class InstrumentedLazyRegex(lazy_regex.LazyRegex):
48
48
        return super(InstrumentedLazyRegex, self).__getattr__(attr)
49
49
 
50
50
    def _real_re_compile(self, *args, **kwargs):
51
 
        self._actions.append(('_real_re_compile',
52
 
                                               args, kwargs))
 
51
        self._actions.append(('_real_re_compile', args, kwargs))
53
52
        return super(InstrumentedLazyRegex, self)._real_re_compile(
54
53
            *args, **kwargs)
55
54
 
61
60
        actions = []
62
61
        InstrumentedLazyRegex.use_actions(actions)
63
62
 
64
 
        pattern = InstrumentedLazyRegex(args=('foo',))
 
63
        pattern = InstrumentedLazyRegex(args=('foo',), kwargs={})
65
64
        actions.append(('created regex', 'foo'))
66
65
        # This match call should compile the regex and go through __getattr__
67
66
        pattern.match('foo')
72
71
        self.assertEqual([('created regex', 'foo'),
73
72
                          ('__getattr__', 'match'),
74
73
                          ('_real_re_compile', ('foo',), {}),
75
 
                         ], actions)
 
74
                          ], actions)
76
75
 
77
76
    def test_bad_pattern(self):
78
77
        """Ensure lazy regex handles bad patterns cleanly."""
79
78
        p = lazy_regex.lazy_compile('RE:[')
80
79
        # As p.match is lazy, we make it into a lambda so its handled
81
80
        # by assertRaises correctly.
82
 
        e = self.assertRaises(lazy_regex.InvalidPattern, lambda: p.match('foo'))
 
81
        e = self.assertRaises(lazy_regex.InvalidPattern,
 
82
                              lambda: p.match('foo'))
83
83
        # Expect either old or new form of error message
84
84
        self.assertContainsRe(e.msg, '^"RE:\\[" '
85
 
            '(unexpected end of regular expression'
86
 
            '|unterminated character set at position 3)$')
 
85
                              '(unexpected end of regular expression'
 
86
                              '|unterminated character set at position 3)$')
87
87
 
88
88
 
89
89
class TestLazyCompile(tests.TestCase):
134
134
        lazy_pattern = lazy_regex.lazy_compile('[,;]+')
135
135
        pickled = pickle.dumps(lazy_pattern)
136
136
        unpickled_lazy_pattern = pickle.loads(pickled)
137
 
        self.assertEqual(['x', 'y', 'z'],
138
 
            unpickled_lazy_pattern.split('x,y;z'))
139
 
 
140
 
 
141
 
class TestInstallLazyCompile(tests.TestCase):
142
 
    """Tests for lazy compiled regexps.
143
 
 
144
 
    Other tests, and breezy in general, count on the lazy regexp compiler
145
 
    being installed, and this is done by loading breezy.  So these tests
146
 
    assume it is installed, and leave it installed when they're done.
147
 
    """
148
 
 
149
 
    def test_install(self):
150
 
        # Don't count on it being present
151
 
        lazy_regex.install_lazy_compile()
152
 
        pattern = re.compile('foo')
153
 
        self.assertIsInstance(pattern, lazy_regex.LazyRegex)
154
 
 
155
 
    def test_reset(self):
156
 
        lazy_regex.reset_compile()
157
 
        self.addCleanup(lazy_regex.install_lazy_compile)
158
 
        pattern = re.compile('foo')
159
 
        self.assertFalse(isinstance(pattern, lazy_regex.LazyRegex),
160
 
            'lazy_regex.reset_compile() did not restore the original'
161
 
            ' compile() function %s' % (type(pattern),))
162
 
        # but the returned object should still support regex operations
163
 
        m = pattern.match('foo')
164
 
        self.assertEqual('foo', m.group())
 
137
        self.assertEqual(
 
138
            ['x', 'y', 'z'], unpickled_lazy_pattern.split('x,y;z'))