24
24
from bzrlib.tests import TestCase, TestCaseWithTransport
27
# TODO: Make sure builtin exception class formats are consistent - e.g. should
28
# or shouldn't end with a full stop, etc.
27
31
class TestErrors(TestCaseWithTransport):
29
33
def test_no_repo(self):
53
57
repo.bzrdir.root_transport.base,
57
class PassThroughError(errors.BzrNewError):
58
"""Pass through %(foo)s and %(bar)s"""
60
def test_bzrnewerror_is_deprecated(self):
61
class DeprecatedError(errors.BzrNewError):
63
self.callDeprecated(['BzrNewError was deprecated in bzr 0.12; '
64
'please convert DeprecatedError to use BzrError instead'],
67
def test_bzrerror_from_literal_string(self):
68
# Some code constructs BzrError from a literal string, in which case
69
# no further formatting is done. (I'm not sure raising the base class
70
# is a great idea, but if the exception is not intended to be caught
71
# perhaps no more is needed.)
73
raise errors.BzrError('this is my errors; %d is not expanded')
74
except errors.BzrError, e:
75
self.assertEqual('this is my errors; %d is not expanded', str(e))
78
class PassThroughError(errors.BzrError):
80
_fmt = """Pass through %(foo)s and %(bar)s"""
60
82
def __init__(self, foo, bar):
61
errors.BzrNewError.__init__(self, foo=foo, bar=bar)
64
class ErrorWithBadFormat(errors.BzrNewError):
65
"""One format specifier: %(thing)s"""
83
errors.BzrError.__init__(self, foo=foo, bar=bar)
86
class ErrorWithBadFormat(errors.BzrError):
88
_fmt = """One format specifier: %(thing)s"""
91
class ErrorWithNoFormat(errors.BzrError):
92
"""This class has a docstring but no format string."""
68
95
class TestErrorFormatting(TestCase):
78
105
self.assertEqual('Pass through \xc2\xb5 and bar', s)
107
def test_missing_format_string(self):
108
# Code naively converted from BzrNewError might still try to
109
# use the docstring as the format string
110
e = ErrorWithNoFormat(param='randomvalue')
111
self.assertStartsWith(
112
str(e), 'Unprintable exception ErrorWithNoFormat')
80
114
def test_mismatched_format_args(self):
81
115
# Even though ErrorWithBadFormat's format string does not match the
82
116
# arguments we constructing it with, we can still stringify an instance
83
117
# of this exception. The resulting string will say its unprintable.
84
118
e = ErrorWithBadFormat(not_thing='x')
85
119
self.assertStartsWith(
86
str(e), 'Unprintable exception ErrorWithBadFormat(')
120
str(e), 'Unprintable exception ErrorWithBadFormat')
89
123
class TestSpecificErrors(TestCase):