2009-01-03 03:35:36 -06:00
|
|
|
# Authors:
|
|
|
|
# Jason Gerard DeRose <jderose@redhat.com>
|
|
|
|
#
|
|
|
|
# Copyright (C) 2008 Red Hat
|
|
|
|
# see file 'COPYING' for use and warranty information
|
|
|
|
#
|
2010-12-09 06:59:11 -06:00
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
2009-01-03 03:35:36 -06:00
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2010-12-09 06:59:11 -06:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-01-03 03:35:36 -06:00
|
|
|
|
|
|
|
"""
|
2009-04-23 07:51:59 -05:00
|
|
|
Test the `ipalib.errors` module.
|
2009-01-03 03:35:36 -06:00
|
|
|
"""
|
|
|
|
|
2014-12-16 07:45:37 -06:00
|
|
|
# FIXME: Pylint errors
|
|
|
|
# pylint: disable=no-member
|
|
|
|
|
2009-01-03 03:35:36 -06:00
|
|
|
import re
|
|
|
|
import inspect
|
2015-04-24 07:39:48 -05:00
|
|
|
import pytest
|
2012-12-04 08:27:05 -06:00
|
|
|
|
2015-09-11 06:43:28 -05:00
|
|
|
import six
|
|
|
|
|
2013-05-21 06:40:27 -05:00
|
|
|
from ipatests.util import assert_equal, raises
|
2015-12-16 09:06:03 -06:00
|
|
|
from ipalib import errors
|
2009-01-04 04:52:08 -06:00
|
|
|
from ipalib.constants import TYPE_ERROR
|
2009-01-03 03:35:36 -06:00
|
|
|
|
2015-09-11 06:43:28 -05:00
|
|
|
if six.PY3:
|
|
|
|
unicode = str
|
|
|
|
|
2009-01-03 03:35:36 -06:00
|
|
|
|
2015-04-24 07:39:48 -05:00
|
|
|
pytestmark = pytest.mark.tier0
|
|
|
|
|
|
|
|
|
2009-01-03 16:35:54 -06:00
|
|
|
class PrivateExceptionTester(object):
|
|
|
|
_klass = None
|
|
|
|
__klass = None
|
|
|
|
|
|
|
|
def __get_klass(self):
|
|
|
|
if self.__klass is None:
|
|
|
|
self.__klass = self._klass
|
2015-08-24 05:40:33 -05:00
|
|
|
assert issubclass(self.__klass, Exception)
|
2009-04-23 07:51:59 -05:00
|
|
|
assert issubclass(self.__klass, errors.PrivateError)
|
|
|
|
assert not issubclass(self.__klass, errors.PublicError)
|
2009-01-03 16:35:54 -06:00
|
|
|
return self.__klass
|
|
|
|
klass = property(__get_klass)
|
|
|
|
|
|
|
|
def new(self, **kw):
|
Use Python3-compatible dict method names
Python 2 has keys()/values()/items(), which return lists,
iterkeys()/itervalues()/iteritems(), which return iterators,
and viewkeys()/viewvalues()/viewitems() which return views.
Python 3 has only keys()/values()/items(), which return views.
To get iterators, one can use iter() or a for loop/comprehension;
for lists there's the list() constructor.
When iterating through the entire dict, without modifying the dict,
the difference between Python 2's items() and iteritems() is
negligible, especially on small dicts (the main overhead is
extra memory, not CPU time). In the interest of simpler code,
this patch changes many instances of iteritems() to items(),
iterkeys() to keys() etc.
In other cases, helpers like six.itervalues are used.
Reviewed-By: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2015-08-11 06:51:14 -05:00
|
|
|
for (key, value) in kw.items():
|
2009-01-03 16:35:54 -06:00
|
|
|
assert not hasattr(self.klass, key), key
|
|
|
|
inst = self.klass(**kw)
|
2015-08-24 05:40:33 -05:00
|
|
|
assert isinstance(inst, Exception)
|
2009-04-23 07:51:59 -05:00
|
|
|
assert isinstance(inst, errors.PrivateError)
|
2009-01-03 16:35:54 -06:00
|
|
|
assert isinstance(inst, self.klass)
|
2009-04-23 07:51:59 -05:00
|
|
|
assert not isinstance(inst, errors.PublicError)
|
Use Python3-compatible dict method names
Python 2 has keys()/values()/items(), which return lists,
iterkeys()/itervalues()/iteritems(), which return iterators,
and viewkeys()/viewvalues()/viewitems() which return views.
Python 3 has only keys()/values()/items(), which return views.
To get iterators, one can use iter() or a for loop/comprehension;
for lists there's the list() constructor.
When iterating through the entire dict, without modifying the dict,
the difference between Python 2's items() and iteritems() is
negligible, especially on small dicts (the main overhead is
extra memory, not CPU time). In the interest of simpler code,
this patch changes many instances of iteritems() to items(),
iterkeys() to keys() etc.
In other cases, helpers like six.itervalues are used.
Reviewed-By: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2015-08-11 06:51:14 -05:00
|
|
|
for (key, value) in kw.items():
|
2009-01-03 16:35:54 -06:00
|
|
|
assert getattr(inst, key) is value
|
|
|
|
assert str(inst) == self.klass.format % kw
|
|
|
|
return inst
|
|
|
|
|
|
|
|
|
|
|
|
class test_PrivateError(PrivateExceptionTester):
|
|
|
|
"""
|
2009-04-23 07:51:59 -05:00
|
|
|
Test the `ipalib.errors.PrivateError` exception.
|
2009-01-03 16:35:54 -06:00
|
|
|
"""
|
2009-04-23 07:51:59 -05:00
|
|
|
_klass = errors.PrivateError
|
2009-01-03 16:35:54 -06:00
|
|
|
|
|
|
|
def test_init(self):
|
|
|
|
"""
|
2009-04-23 07:51:59 -05:00
|
|
|
Test the `ipalib.errors.PrivateError.__init__` method.
|
2009-01-03 16:35:54 -06:00
|
|
|
"""
|
|
|
|
inst = self.klass(key1='Value 1', key2='Value 2')
|
|
|
|
assert inst.key1 == 'Value 1'
|
|
|
|
assert inst.key2 == 'Value 2'
|
|
|
|
assert str(inst) == ''
|
|
|
|
|
|
|
|
# Test subclass and use of format:
|
|
|
|
class subclass(self.klass):
|
|
|
|
format = '%(true)r %(text)r %(number)r'
|
|
|
|
|
|
|
|
kw = dict(true=True, text='Hello!', number=18)
|
|
|
|
inst = subclass(**kw)
|
|
|
|
assert inst.true is True
|
|
|
|
assert inst.text is kw['text']
|
|
|
|
assert inst.number is kw['number']
|
|
|
|
assert str(inst) == subclass.format % kw
|
|
|
|
|
|
|
|
# Test via PrivateExceptionTester.new()
|
|
|
|
inst = self.new(**kw)
|
|
|
|
assert isinstance(inst, self.klass)
|
|
|
|
assert inst.true is True
|
|
|
|
assert inst.text is kw['text']
|
|
|
|
assert inst.number is kw['number']
|
|
|
|
|
|
|
|
|
|
|
|
class test_SubprocessError(PrivateExceptionTester):
|
|
|
|
"""
|
2009-04-23 07:51:59 -05:00
|
|
|
Test the `ipalib.errors.SubprocessError` exception.
|
2009-01-03 16:35:54 -06:00
|
|
|
"""
|
2009-01-03 18:27:53 -06:00
|
|
|
|
2009-04-23 07:51:59 -05:00
|
|
|
_klass = errors.SubprocessError
|
2009-01-03 16:35:54 -06:00
|
|
|
|
|
|
|
def test_init(self):
|
|
|
|
"""
|
2009-04-23 07:51:59 -05:00
|
|
|
Test the `ipalib.errors.SubprocessError.__init__` method.
|
2009-01-03 16:35:54 -06:00
|
|
|
"""
|
2016-12-01 07:49:14 -06:00
|
|
|
bin_false = '/bin/false'
|
|
|
|
inst = self.new(returncode=1, argv=(bin_false,))
|
2009-01-03 16:35:54 -06:00
|
|
|
assert inst.returncode == 1
|
2016-12-01 07:49:14 -06:00
|
|
|
assert inst.argv == (bin_false,)
|
|
|
|
assert str(inst) == "return code 1 from ('{}',)".format(bin_false)
|
2009-01-03 18:27:53 -06:00
|
|
|
|
|
|
|
|
|
|
|
class test_PluginSubclassError(PrivateExceptionTester):
|
|
|
|
"""
|
2009-04-23 07:51:59 -05:00
|
|
|
Test the `ipalib.errors.PluginSubclassError` exception.
|
2009-01-03 18:27:53 -06:00
|
|
|
"""
|
|
|
|
|
2009-04-23 07:51:59 -05:00
|
|
|
_klass = errors.PluginSubclassError
|
2009-01-03 18:27:53 -06:00
|
|
|
|
|
|
|
def test_init(self):
|
|
|
|
"""
|
2009-04-23 07:51:59 -05:00
|
|
|
Test the `ipalib.errors.PluginSubclassError.__init__` method.
|
2009-01-03 18:27:53 -06:00
|
|
|
"""
|
|
|
|
inst = self.new(plugin='bad', bases=('base1', 'base2'))
|
|
|
|
assert inst.plugin == 'bad'
|
|
|
|
assert inst.bases == ('base1', 'base2')
|
|
|
|
assert str(inst) == \
|
|
|
|
"'bad' not subclass of any base in ('base1', 'base2')"
|
|
|
|
|
|
|
|
|
|
|
|
class test_PluginDuplicateError(PrivateExceptionTester):
|
|
|
|
"""
|
2009-04-23 07:51:59 -05:00
|
|
|
Test the `ipalib.errors.PluginDuplicateError` exception.
|
2009-01-03 18:27:53 -06:00
|
|
|
"""
|
|
|
|
|
2009-04-23 07:51:59 -05:00
|
|
|
_klass = errors.PluginDuplicateError
|
2009-01-03 18:27:53 -06:00
|
|
|
|
|
|
|
def test_init(self):
|
|
|
|
"""
|
2009-04-23 07:51:59 -05:00
|
|
|
Test the `ipalib.errors.PluginDuplicateError.__init__` method.
|
2009-01-03 18:27:53 -06:00
|
|
|
"""
|
|
|
|
inst = self.new(plugin='my_plugin')
|
|
|
|
assert inst.plugin == 'my_plugin'
|
|
|
|
assert str(inst) == "'my_plugin' was already registered"
|
|
|
|
|
|
|
|
|
|
|
|
class test_PluginOverrideError(PrivateExceptionTester):
|
|
|
|
"""
|
2009-04-23 07:51:59 -05:00
|
|
|
Test the `ipalib.errors.PluginOverrideError` exception.
|
2009-01-03 18:27:53 -06:00
|
|
|
"""
|
|
|
|
|
2009-04-23 07:51:59 -05:00
|
|
|
_klass = errors.PluginOverrideError
|
2009-01-03 18:27:53 -06:00
|
|
|
|
|
|
|
def test_init(self):
|
|
|
|
"""
|
2009-04-23 07:51:59 -05:00
|
|
|
Test the `ipalib.errors.PluginOverrideError.__init__` method.
|
2009-01-03 18:27:53 -06:00
|
|
|
"""
|
|
|
|
inst = self.new(base='Base', name='cmd', plugin='my_cmd')
|
|
|
|
assert inst.base == 'Base'
|
|
|
|
assert inst.name == 'cmd'
|
|
|
|
assert inst.plugin == 'my_cmd'
|
|
|
|
assert str(inst) == "unexpected override of Base.cmd with 'my_cmd'"
|
|
|
|
|
|
|
|
|
|
|
|
class test_PluginMissingOverrideError(PrivateExceptionTester):
|
|
|
|
"""
|
2009-04-23 07:51:59 -05:00
|
|
|
Test the `ipalib.errors.PluginMissingOverrideError` exception.
|
2009-01-03 18:27:53 -06:00
|
|
|
"""
|
|
|
|
|
2009-04-23 07:51:59 -05:00
|
|
|
_klass = errors.PluginMissingOverrideError
|
2009-01-03 18:27:53 -06:00
|
|
|
|
|
|
|
def test_init(self):
|
|
|
|
"""
|
2009-04-23 07:51:59 -05:00
|
|
|
Test the `ipalib.errors.PluginMissingOverrideError.__init__` method.
|
2009-01-03 18:27:53 -06:00
|
|
|
"""
|
|
|
|
inst = self.new(base='Base', name='cmd', plugin='my_cmd')
|
|
|
|
assert inst.base == 'Base'
|
|
|
|
assert inst.name == 'cmd'
|
|
|
|
assert inst.plugin == 'my_cmd'
|
|
|
|
assert str(inst) == "Base.cmd not registered, cannot override with 'my_cmd'"
|
2009-01-03 16:35:54 -06:00
|
|
|
|
2009-01-04 04:52:08 -06:00
|
|
|
|
|
|
|
##############################################################################
|
|
|
|
# Unit tests for public errors:
|
|
|
|
|
|
|
|
class PublicExceptionTester(object):
|
|
|
|
_klass = None
|
|
|
|
__klass = None
|
|
|
|
|
|
|
|
def __get_klass(self):
|
|
|
|
if self.__klass is None:
|
|
|
|
self.__klass = self._klass
|
2015-08-24 05:40:33 -05:00
|
|
|
assert issubclass(self.__klass, Exception)
|
2009-04-23 07:51:59 -05:00
|
|
|
assert issubclass(self.__klass, errors.PublicError)
|
|
|
|
assert not issubclass(self.__klass, errors.PrivateError)
|
2009-01-06 14:33:22 -06:00
|
|
|
assert type(self.__klass.errno) is int
|
|
|
|
assert 900 <= self.__klass.errno <= 5999
|
2009-01-04 04:52:08 -06:00
|
|
|
return self.__klass
|
|
|
|
klass = property(__get_klass)
|
|
|
|
|
2009-01-08 01:07:18 -06:00
|
|
|
def new(self, format=None, message=None, **kw):
|
2009-01-04 04:52:08 -06:00
|
|
|
# Test that TypeError is raised if message isn't unicode:
|
2015-09-17 11:46:14 -05:00
|
|
|
e = raises(TypeError, self.klass, message=b'The message')
|
|
|
|
assert str(e) == TYPE_ERROR % ('message', unicode, b'The message', bytes)
|
2009-01-04 04:52:08 -06:00
|
|
|
|
|
|
|
# Test the instance:
|
Use Python3-compatible dict method names
Python 2 has keys()/values()/items(), which return lists,
iterkeys()/itervalues()/iteritems(), which return iterators,
and viewkeys()/viewvalues()/viewitems() which return views.
Python 3 has only keys()/values()/items(), which return views.
To get iterators, one can use iter() or a for loop/comprehension;
for lists there's the list() constructor.
When iterating through the entire dict, without modifying the dict,
the difference between Python 2's items() and iteritems() is
negligible, especially on small dicts (the main overhead is
extra memory, not CPU time). In the interest of simpler code,
this patch changes many instances of iteritems() to items(),
iterkeys() to keys() etc.
In other cases, helpers like six.itervalues are used.
Reviewed-By: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2015-08-11 06:51:14 -05:00
|
|
|
for (key, value) in kw.items():
|
2009-01-04 04:52:08 -06:00
|
|
|
assert not hasattr(self.klass, key), key
|
2009-01-08 01:07:18 -06:00
|
|
|
inst = self.klass(format=format, message=message, **kw)
|
2012-12-04 08:27:05 -06:00
|
|
|
for required_class in self.required_classes:
|
|
|
|
assert isinstance(inst, required_class)
|
2009-01-04 04:52:08 -06:00
|
|
|
assert isinstance(inst, self.klass)
|
2009-04-23 07:51:59 -05:00
|
|
|
assert not isinstance(inst, errors.PrivateError)
|
Use Python3-compatible dict method names
Python 2 has keys()/values()/items(), which return lists,
iterkeys()/itervalues()/iteritems(), which return iterators,
and viewkeys()/viewvalues()/viewitems() which return views.
Python 3 has only keys()/values()/items(), which return views.
To get iterators, one can use iter() or a for loop/comprehension;
for lists there's the list() constructor.
When iterating through the entire dict, without modifying the dict,
the difference between Python 2's items() and iteritems() is
negligible, especially on small dicts (the main overhead is
extra memory, not CPU time). In the interest of simpler code,
this patch changes many instances of iteritems() to items(),
iterkeys() to keys() etc.
In other cases, helpers like six.itervalues are used.
Reviewed-By: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2015-08-11 06:51:14 -05:00
|
|
|
for (key, value) in kw.items():
|
2009-01-04 04:52:08 -06:00
|
|
|
assert getattr(inst, key) is value
|
|
|
|
return inst
|
|
|
|
|
|
|
|
|
|
|
|
class test_PublicError(PublicExceptionTester):
|
|
|
|
"""
|
2009-04-23 07:51:59 -05:00
|
|
|
Test the `ipalib.errors.PublicError` exception.
|
2009-01-04 04:52:08 -06:00
|
|
|
"""
|
2009-04-23 07:51:59 -05:00
|
|
|
_klass = errors.PublicError
|
2015-08-24 05:40:33 -05:00
|
|
|
required_classes = Exception, errors.PublicError
|
2009-01-04 04:52:08 -06:00
|
|
|
|
|
|
|
def test_init(self):
|
2009-01-08 01:07:18 -06:00
|
|
|
message = u'The translated, interpolated message'
|
|
|
|
format = 'key=%(key1)r and key2=%(key2)r'
|
2016-06-20 03:29:26 -05:00
|
|
|
val1 = u'Value 1'
|
|
|
|
val2 = u'Value 2'
|
2009-01-08 01:07:18 -06:00
|
|
|
kw = dict(key1=val1, key2=val2)
|
|
|
|
|
|
|
|
# Test with format=str, message=None
|
|
|
|
inst = self.klass(format, **kw)
|
|
|
|
assert inst.format is format
|
2017-10-03 05:36:21 -05:00
|
|
|
assert_equal(str(inst), format % kw)
|
2009-01-08 01:07:18 -06:00
|
|
|
assert inst.forwarded is False
|
|
|
|
assert inst.key1 is val1
|
|
|
|
assert inst.key2 is val2
|
|
|
|
|
|
|
|
# Test with format=None, message=unicode
|
|
|
|
inst = self.klass(message=message, **kw)
|
|
|
|
assert inst.format is None
|
2017-10-03 05:36:21 -05:00
|
|
|
assert str(inst) == message
|
2009-01-08 01:07:18 -06:00
|
|
|
assert inst.strerror is message
|
|
|
|
assert inst.forwarded is True
|
|
|
|
assert inst.key1 is val1
|
|
|
|
assert inst.key2 is val2
|
|
|
|
|
2015-09-17 11:46:14 -05:00
|
|
|
# Test with format=None, message=bytes
|
|
|
|
e = raises(TypeError, self.klass, message=b'the message', **kw)
|
|
|
|
assert str(e) == TYPE_ERROR % ('message', unicode, b'the message', bytes)
|
2009-01-08 01:07:18 -06:00
|
|
|
|
|
|
|
# Test with format=None, message=None
|
|
|
|
e = raises(ValueError, self.klass, **kw)
|
2012-12-04 08:27:05 -06:00
|
|
|
assert (str(e) == '%s.format is None yet format=None, message=None' %
|
|
|
|
self.klass.__name__)
|
2009-01-08 01:07:18 -06:00
|
|
|
|
|
|
|
|
|
|
|
######################################
|
|
|
|
# Test via PublicExceptionTester.new()
|
2009-01-04 04:52:08 -06:00
|
|
|
|
2009-01-08 01:07:18 -06:00
|
|
|
# Test with format=str, message=None
|
|
|
|
inst = self.new(format, **kw)
|
|
|
|
assert isinstance(inst, self.klass)
|
|
|
|
assert inst.format is format
|
2017-10-03 05:36:21 -05:00
|
|
|
assert_equal(str(inst), format % kw)
|
2009-01-08 01:07:18 -06:00
|
|
|
assert inst.forwarded is False
|
|
|
|
assert inst.key1 is val1
|
|
|
|
assert inst.key2 is val2
|
|
|
|
|
|
|
|
# Test with format=None, message=unicode
|
|
|
|
inst = self.new(message=message, **kw)
|
|
|
|
assert isinstance(inst, self.klass)
|
|
|
|
assert inst.format is None
|
2017-10-03 05:36:21 -05:00
|
|
|
assert str(inst) == message
|
2009-01-08 01:07:18 -06:00
|
|
|
assert inst.strerror is message
|
|
|
|
assert inst.forwarded is True
|
|
|
|
assert inst.key1 is val1
|
|
|
|
assert inst.key2 is val2
|
|
|
|
|
|
|
|
|
|
|
|
##################
|
|
|
|
# Test a subclass:
|
2009-01-04 04:52:08 -06:00
|
|
|
class subclass(self.klass):
|
2009-01-08 01:07:18 -06:00
|
|
|
format = '%(true)r %(text)r %(number)r'
|
2009-01-04 04:52:08 -06:00
|
|
|
|
2016-06-20 03:29:26 -05:00
|
|
|
kw = dict(true=True, text=u'Hello!', number=18)
|
2009-01-08 01:07:18 -06:00
|
|
|
|
|
|
|
# Test with format=str, message=None
|
|
|
|
e = raises(ValueError, subclass, format, **kw)
|
|
|
|
assert str(e) == 'non-generic %r needs format=None; got format=%r' % (
|
|
|
|
'subclass', format)
|
|
|
|
|
|
|
|
# Test with format=None, message=None:
|
2009-01-04 04:52:08 -06:00
|
|
|
inst = subclass(**kw)
|
2009-01-08 01:07:18 -06:00
|
|
|
assert inst.format is subclass.format
|
2017-10-03 05:36:21 -05:00
|
|
|
assert_equal(str(inst), subclass.format % kw)
|
2009-01-08 01:07:18 -06:00
|
|
|
assert inst.forwarded is False
|
2009-01-04 04:52:08 -06:00
|
|
|
assert inst.true is True
|
|
|
|
assert inst.text is kw['text']
|
|
|
|
assert inst.number is kw['number']
|
|
|
|
|
2009-01-08 01:07:18 -06:00
|
|
|
# Test with format=None, message=unicode:
|
|
|
|
inst = subclass(message=message, **kw)
|
|
|
|
assert inst.format is subclass.format
|
2017-10-03 05:36:21 -05:00
|
|
|
assert str(inst) == message
|
2009-01-08 01:07:18 -06:00
|
|
|
assert inst.strerror is message
|
|
|
|
assert inst.forwarded is True
|
2009-01-04 04:52:08 -06:00
|
|
|
assert inst.true is True
|
|
|
|
assert inst.text is kw['text']
|
|
|
|
assert inst.number is kw['number']
|
|
|
|
|
2012-10-12 04:13:59 -05:00
|
|
|
# Test with instructions:
|
|
|
|
# first build up "instructions", then get error and search for
|
|
|
|
# lines of instructions appended to the end of the strerror
|
|
|
|
# despite the parameter 'instructions' not existing in the format
|
|
|
|
instructions = u"The quick brown fox jumps over the lazy dog".split()
|
|
|
|
# this expression checks if each word of instructions
|
|
|
|
# exists in a string as a separate line, with right order
|
|
|
|
regexp = re.compile('(?ims).*' +
|
2015-08-12 05:25:30 -05:00
|
|
|
''.join('(%s).*' % (x) for x in instructions) +
|
2012-10-12 04:13:59 -05:00
|
|
|
'$')
|
|
|
|
inst = subclass(instructions=instructions, **kw)
|
|
|
|
assert inst.format is subclass.format
|
2016-06-20 03:29:26 -05:00
|
|
|
assert_equal(inst.instructions, unicode(instructions))
|
2012-10-12 04:13:59 -05:00
|
|
|
inst_match = regexp.match(inst.strerror).groups()
|
|
|
|
assert_equal(list(inst_match),list(instructions))
|
|
|
|
|
2009-01-04 04:52:08 -06:00
|
|
|
|
2012-12-04 08:27:05 -06:00
|
|
|
class BaseMessagesTest(object):
|
|
|
|
"""Generic test for all of a module's errors or messages
|
2009-01-03 03:35:36 -06:00
|
|
|
"""
|
2012-12-04 08:27:05 -06:00
|
|
|
def test_public_messages(self):
|
|
|
|
i = 0
|
|
|
|
for klass in self.message_list:
|
|
|
|
for required_class in self.required_classes:
|
|
|
|
assert issubclass(klass, required_class)
|
|
|
|
assert type(klass.errno) is int
|
|
|
|
assert klass.errno in self.errno_range
|
|
|
|
doc = inspect.getdoc(klass)
|
|
|
|
assert doc is not None, 'need class docstring for %s' % klass.__name__
|
|
|
|
m = re.match(r'^\*{2}(\d+)\*{2} ', doc)
|
|
|
|
assert m is not None, "need '**ERRNO**' in %s docstring" % klass.__name__
|
|
|
|
errno = int(m.group(1))
|
|
|
|
assert errno == klass.errno, (
|
|
|
|
'docstring=%r but errno=%r in %s' % (errno, klass.errno, klass.__name__)
|
|
|
|
)
|
|
|
|
self.extratest(klass)
|
|
|
|
|
|
|
|
# Test format
|
|
|
|
if klass.format is not None:
|
|
|
|
assert klass.format is self.texts[i]
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
def extratest(self, cls):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class test_PublicErrors(object):
|
|
|
|
message_list = errors.public_errors
|
2015-08-12 08:23:56 -05:00
|
|
|
errno_range = list(range(900, 5999))
|
2015-08-24 05:40:33 -05:00
|
|
|
required_classes = (Exception, errors.PublicError)
|
2012-12-04 08:27:05 -06:00
|
|
|
texts = errors._texts
|
|
|
|
|
|
|
|
def extratest(self, cls):
|
|
|
|
assert not issubclass(cls, errors.PrivateError)
|