~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_errors.py

Merge bzr.dev (via NO_SMART_VFS branch).

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
from bzrlib.tests import TestCase, TestCaseWithTransport
25
25
 
26
26
 
 
27
# TODO: Make sure builtin exception class formats are consistent - e.g. should
 
28
# or shouldn't end with a full stop, etc.
 
29
 
 
30
 
27
31
class TestErrors(TestCaseWithTransport):
28
32
 
29
33
    def test_disabled_method(self):
38
42
            "read without data loss.",
39
43
            str(error))
40
44
 
 
45
    def test_install_failed(self):
 
46
        error = errors.InstallFailed(['rev-one'])
 
47
        self.assertEqual("Could not install revisions:\nrev-one", str(error))
 
48
        error = errors.InstallFailed(['rev-one', 'rev-two'])
 
49
        self.assertEqual("Could not install revisions:\nrev-one, rev-two",
 
50
                         str(error))
 
51
        error = errors.InstallFailed([None])
 
52
        self.assertEqual("Could not install revisions:\nNone", str(error))
 
53
 
41
54
    def test_medium_not_connected(self):
42
55
        error = errors.MediumNotConnected("a medium")
43
56
        self.assertEqualDiff(
89
102
                             repo.bzrdir.root_transport.base,
90
103
                             str(error))
91
104
 
 
105
    def test_bzrnewerror_is_deprecated(self):
 
106
        class DeprecatedError(errors.BzrNewError):
 
107
            pass
 
108
        self.callDeprecated(['BzrNewError was deprecated in bzr 0.13; '
 
109
             'please convert DeprecatedError to use BzrError instead'],
 
110
            DeprecatedError)
 
111
 
 
112
    def test_bzrerror_from_literal_string(self):
 
113
        # Some code constructs BzrError from a literal string, in which case
 
114
        # no further formatting is done.  (I'm not sure raising the base class
 
115
        # is a great idea, but if the exception is not intended to be caught
 
116
        # perhaps no more is needed.)
 
117
        try:
 
118
            raise errors.BzrError('this is my errors; %d is not expanded')
 
119
        except errors.BzrError, e:
 
120
            self.assertEqual('this is my errors; %d is not expanded', str(e))
 
121
 
92
122
    def test_reading_completed(self):
93
123
        error = errors.ReadingCompleted("a request")
94
124
        self.assertEqualDiff("The MediumRequest 'a request' has already had "
110
140
            " no data may be read.",
111
141
            str(error))
112
142
 
113
 
 
114
 
class PassThroughError(errors.BzrNewError):
115
 
    """Pass through %(foo)s and %(bar)s"""
116
 
 
117
 
    def __init__(self, foo, bar):
118
 
        errors.BzrNewError.__init__(self, foo=foo, bar=bar)
119
 
 
120
 
 
121
 
class ErrorWithBadFormat(errors.BzrNewError):
122
 
    """One format specifier: %(thing)s"""
123
 
 
124
 
 
125
 
class TestErrorFormatting(TestCase):
126
 
    
127
 
    def test_always_str(self):
128
 
        e = PassThroughError(u'\xb5', 'bar')
129
 
        self.assertIsInstance(e.__str__(), str)
130
 
        # In Python str(foo) *must* return a real byte string
131
 
        # not a Unicode string. The following line would raise a
132
 
        # Unicode error, because it tries to call str() on the string
133
 
        # returned from e.__str__(), and it has non ascii characters
134
 
        s = str(e)
135
 
        self.assertEqual('Pass through \xc2\xb5 and bar', s)
136
 
 
137
 
    def test_mismatched_format_args(self):
138
 
        # Even though ErrorWithBadFormat's format string does not match the
139
 
        # arguments we constructing it with, we can still stringify an instance
140
 
        # of this exception. The resulting string will say its unprintable.
141
 
        e = ErrorWithBadFormat(not_thing='x')
142
 
        self.assertStartsWith(
143
 
            str(e), 'Unprintable exception ErrorWithBadFormat(')
144
 
 
145
 
 
146
 
class TestSpecificErrors(TestCase):
147
 
    
148
143
    def test_transport_not_possible(self):
149
144
        e = errors.TransportNotPossible('readonly', 'original error')
150
145
        self.assertEqual('Transport operation not possible:'
194
189
            orig_error='my_error')
195
190
 
196
191
 
 
192
 
 
193
class PassThroughError(errors.BzrError):
 
194
    
 
195
    _fmt = """Pass through %(foo)s and %(bar)s"""
 
196
 
 
197
    def __init__(self, foo, bar):
 
198
        errors.BzrError.__init__(self, foo=foo, bar=bar)
 
199
 
 
200
 
 
201
class ErrorWithBadFormat(errors.BzrError):
 
202
 
 
203
    _fmt = """One format specifier: %(thing)s"""
 
204
 
 
205
 
 
206
class ErrorWithNoFormat(errors.BzrError):
 
207
    """This class has a docstring but no format string."""
 
208
 
 
209
 
 
210
class TestErrorFormatting(TestCase):
 
211
    
 
212
    def test_always_str(self):
 
213
        e = PassThroughError(u'\xb5', 'bar')
 
214
        self.assertIsInstance(e.__str__(), str)
 
215
        # In Python str(foo) *must* return a real byte string
 
216
        # not a Unicode string. The following line would raise a
 
217
        # Unicode error, because it tries to call str() on the string
 
218
        # returned from e.__str__(), and it has non ascii characters
 
219
        s = str(e)
 
220
        self.assertEqual('Pass through \xc2\xb5 and bar', s)
 
221
 
 
222
    def test_missing_format_string(self):
 
223
        e = ErrorWithNoFormat(param='randomvalue')
 
224
        s = self.callDeprecated(
 
225
                ['ErrorWithNoFormat uses its docstring as a format, it should use _fmt instead'],
 
226
                lambda x: str(x), e)
 
227
        ## s = str(e)
 
228
        self.assertEqual(s, 
 
229
                "This class has a docstring but no format string.")
 
230
 
 
231
    def test_mismatched_format_args(self):
 
232
        # Even though ErrorWithBadFormat's format string does not match the
 
233
        # arguments we constructing it with, we can still stringify an instance
 
234
        # of this exception. The resulting string will say its unprintable.
 
235
        e = ErrorWithBadFormat(not_thing='x')
 
236
        self.assertStartsWith(
 
237
            str(e), 'Unprintable exception ErrorWithBadFormat')
 
238