/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-05-06 11:48:54 UTC
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@jelmer.uk-20180506114854-h4qd9ojaqy8wxjsd
Move .mailmap to root.

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', args, kwargs))
 
51
        self._actions.append(('_real_re_compile',
 
52
                                               args, kwargs))
52
53
        return super(InstrumentedLazyRegex, self)._real_re_compile(
53
54
            *args, **kwargs)
54
55
 
60
61
        actions = []
61
62
        InstrumentedLazyRegex.use_actions(actions)
62
63
 
63
 
        pattern = InstrumentedLazyRegex(args=('foo',), kwargs={})
 
64
        pattern = InstrumentedLazyRegex(args=('foo',))
64
65
        actions.append(('created regex', 'foo'))
65
66
        # This match call should compile the regex and go through __getattr__
66
67
        pattern.match('foo')
71
72
        self.assertEqual([('created regex', 'foo'),
72
73
                          ('__getattr__', 'match'),
73
74
                          ('_real_re_compile', ('foo',), {}),
74
 
                          ], actions)
 
75
                         ], actions)
75
76
 
76
77
    def test_bad_pattern(self):
77
78
        """Ensure lazy regex handles bad patterns cleanly."""
78
79
        p = lazy_regex.lazy_compile('RE:[')
79
80
        # As p.match is lazy, we make it into a lambda so its handled
80
81
        # by assertRaises correctly.
81
 
        e = self.assertRaises(lazy_regex.InvalidPattern,
82
 
                              lambda: p.match('foo'))
 
82
        e = self.assertRaises(lazy_regex.InvalidPattern, 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(
138
 
            ['x', 'y', 'z'], unpickled_lazy_pattern.split('x,y;z'))
 
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())