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

  • Committer: Robert Collins
  • Date: 2010-05-06 23:41:35 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506234135-yivbzczw1sejxnxc
Lock methods on ``Tree``, ``Branch`` and ``Repository`` are now
expected to return an object which can be used to unlock them. This reduces
duplicate code when using cleanups. The previous 'tokens's returned by
``Branch.lock_write`` and ``Repository.lock_write`` are now attributes
on the result of the lock_write. ``repository.RepositoryWriteLockResult``
and ``branch.BranchWriteLockResult`` document this. (Robert Collins)

``log._get_info_for_log_files`` now takes an add_cleanup callable.
(Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2011 Canonical Ltd
 
1
# Copyright (C) 2006 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
16
16
 
17
17
"""Test that lazy regexes are not compiled right away"""
18
18
 
19
 
import pickle
20
19
import re
21
20
 
22
 
from .. import errors
23
 
from .. import (
 
21
from bzrlib import (
24
22
    lazy_regex,
25
23
    tests,
26
24
    )
27
25
 
28
26
 
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
27
class InstrumentedLazyRegex(lazy_regex.LazyRegex):
38
28
    """Keep track of actions on the lazy regex"""
39
29
 
48
38
        return super(InstrumentedLazyRegex, self).__getattr__(attr)
49
39
 
50
40
    def _real_re_compile(self, *args, **kwargs):
51
 
        self._actions.append(('_real_re_compile', args, kwargs))
52
 
        return super(InstrumentedLazyRegex, self)._real_re_compile(
53
 
            *args, **kwargs)
 
41
        self._actions.append(('_real_re_compile',
 
42
                                               args, kwargs))
 
43
        return super(InstrumentedLazyRegex, self)._real_re_compile(*args, **kwargs)
54
44
 
55
45
 
56
46
class TestLazyRegex(tests.TestCase):
60
50
        actions = []
61
51
        InstrumentedLazyRegex.use_actions(actions)
62
52
 
63
 
        pattern = InstrumentedLazyRegex(args=('foo',), kwargs={})
 
53
        pattern = InstrumentedLazyRegex(args=('foo',))
64
54
        actions.append(('created regex', 'foo'))
65
55
        # This match call should compile the regex and go through __getattr__
66
56
        pattern.match('foo')
71
61
        self.assertEqual([('created regex', 'foo'),
72
62
                          ('__getattr__', 'match'),
73
63
                          ('_real_re_compile', ('foo',), {}),
74
 
                          ], actions)
75
 
 
76
 
    def test_bad_pattern(self):
77
 
        """Ensure lazy regex handles bad patterns cleanly."""
78
 
        p = lazy_regex.lazy_compile('RE:[')
79
 
        # As p.match is lazy, we make it into a lambda so its handled
80
 
        # 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)$')
 
64
                         ], actions)
87
65
 
88
66
 
89
67
class TestLazyCompile(tests.TestCase):
124
102
        self.assertEqual('fooo', pattern.search('fooo').group())
125
103
 
126
104
    def test_split(self):
127
 
        pattern = lazy_regex.lazy_compile('[,;]+')
 
105
        pattern = lazy_regex.lazy_compile('[,;]*')
128
106
        self.assertEqual(['x', 'y', 'z'], pattern.split('x,y;z'))
129
107
 
130
 
    def test_pickle(self):
131
 
        # When pickling, just compile the regex.
132
 
        # Sphinx, which we use for documentation, pickles
133
 
        # some compiled regexes.
134
 
        lazy_pattern = lazy_regex.lazy_compile('[,;]+')
135
 
        pickled = pickle.dumps(lazy_pattern)
136
 
        unpickled_lazy_pattern = pickle.loads(pickled)
137
 
        self.assertEqual(
138
 
            ['x', 'y', 'z'], unpickled_lazy_pattern.split('x,y;z'))
 
108
 
 
109
class TestInstallLazyCompile(tests.TestCase):
 
110
 
 
111
    def setUp(self):
 
112
        super(TestInstallLazyCompile, self).setUp()
 
113
        self.addCleanup(lazy_regex.reset_compile)
 
114
 
 
115
    def test_install(self):
 
116
        lazy_regex.install_lazy_compile()
 
117
        pattern = re.compile('foo')
 
118
        self.assertIsInstance(pattern, lazy_regex.LazyRegex)
 
119
 
 
120
    def test_reset(self):
 
121
        lazy_regex.install_lazy_compile()
 
122
        lazy_regex.reset_compile()
 
123
        pattern = re.compile('foo')
 
124
        self.failIf(isinstance(pattern, lazy_regex.LazyRegex),
 
125
                    'lazy_regex.reset_compile() did not restore the original'
 
126
                    ' compile() function %s' % (type(pattern),))
 
127
        # but the returned object should still support regex operations
 
128
        m = pattern.match('foo')
 
129
        self.assertEqual('foo', m.group())