/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-23 22:06:41 UTC
  • mfrom: (6738 trunk)
  • mto: This revision was merged to the branch mainline in revision 6739.
  • Revision ID: jelmer@jelmer.uk-20170723220641-69eczax9bmv8d6kk
Merge trunk, address review comments.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
    )
27
27
 
28
28
 
29
 
class TestErrors(tests.TestCase):
 
29
class ErrorTests(tests.TestCase):
30
30
 
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'))
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)$')
 
82
        e = self.assertRaises(lazy_regex.InvalidPattern, lambda: p.match('foo'))
 
83
        self.assertEqual(e.msg, '"RE:[" unexpected end of regular expression')
87
84
 
88
85
 
89
86
class TestLazyCompile(tests.TestCase):
124
121
        self.assertEqual('fooo', pattern.search('fooo').group())
125
122
 
126
123
    def test_split(self):
127
 
        pattern = lazy_regex.lazy_compile('[,;]+')
 
124
        pattern = lazy_regex.lazy_compile('[,;]*')
128
125
        self.assertEqual(['x', 'y', 'z'], pattern.split('x,y;z'))
129
126
 
130
127
    def test_pickle(self):
131
128
        # When pickling, just compile the regex.
132
129
        # Sphinx, which we use for documentation, pickles
133
130
        # some compiled regexes.
134
 
        lazy_pattern = lazy_regex.lazy_compile('[,;]+')
 
131
        lazy_pattern = lazy_regex.lazy_compile('[,;]*')
135
132
        pickled = pickle.dumps(lazy_pattern)
136
133
        unpickled_lazy_pattern = pickle.loads(pickled)
137
 
        self.assertEqual(
138
 
            ['x', 'y', 'z'], unpickled_lazy_pattern.split('x,y;z'))
 
134
        self.assertEqual(['x', 'y', 'z'],
 
135
            unpickled_lazy_pattern.split('x,y;z'))
 
136
 
 
137
 
 
138
class TestInstallLazyCompile(tests.TestCase):
 
139
    """Tests for lazy compiled regexps.
 
140
 
 
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
    """
 
145
 
 
146
    def test_install(self):
 
147
        # Don't count on it being present
 
148
        lazy_regex.install_lazy_compile()
 
149
        pattern = re.compile('foo')
 
150
        self.assertIsInstance(pattern, lazy_regex.LazyRegex)
 
151
 
 
152
    def test_reset(self):
 
153
        lazy_regex.reset_compile()
 
154
        self.addCleanup(lazy_regex.install_lazy_compile)
 
155
        pattern = re.compile('foo')
 
156
        self.assertFalse(isinstance(pattern, lazy_regex.LazyRegex),
 
157
            'lazy_regex.reset_compile() did not restore the original'
 
158
            ' compile() function %s' % (type(pattern),))
 
159
        # but the returned object should still support regex operations
 
160
        m = pattern.match('foo')
 
161
        self.assertEqual('foo', m.group())