/usr/lib/python3/dist-packages/pyutilib/enum/tests/test_enum.py is in python3-pyutilib 5.3.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 | #! /usr/bin/env python
# -*- coding: utf-8 -*-
# test/test_enum.py
# Part of enum, a package providing enumerated types for Python.
#
# Copyright © 2007–2009 Ben Finney <ben+python@benfinney.id.au>
# This is free software; you may copy, modify and/or distribute this work
# under the terms of the GNU General Public License, version 2 or later
# or, at your option, the terms of the Python license.
""" Unit test for ‘enum’ module.
"""
import unittest
import pyutilib.enum as enum
class Mock_Enum(object):
""" Mock object for Enum testing. """
def __init__(self, *keys):
""" Set up a new instance. """
pass
def setup_enum_value_fixtures(testcase):
""" Set up fixtures for test cases using ‘EnumValue’. """
testcase.bad_keys = [
None, 0, 1, (), Mock_Enum()
]
testcase.other_values = [
None, 0, 1, (), Mock_Enum(), "bogus-str",
enum.EnumValue(Mock_Enum(), 0, 'bogus-enum'),
]
testcase.planets = [
('mercury', "Mercury"),
('venus', "Venus"),
('earth', "Earth"),
('mars', "Mars"),
('jupiter', "Jupiter"),
('saturn', "Saturn"),
('uranus', "Uranus"),
('neptune', "Neptune"),
]
planet_keys = [key for (key, name) in testcase.planets]
colour_keys = [
'red', 'green', 'blue',
'yellow', 'orange', 'purple',
'white', 'black',
]
simple_keys = ['spam', 'eggs', 'beans']
testcase.SimpleEnum = testcase.enum_factory_class(*simple_keys)
Colour = testcase.enum_factory_class(*colour_keys)
Planet = testcase.enum_factory_class(*planet_keys)
testcase.valid_values = {
Colour: dict(
keys = colour_keys,
),
Planet: dict(
keys = planet_keys,
),
}
for enumtype, params in testcase.valid_values.items():
params['enumtype'] = enumtype
values = {}
for i, key in enumerate(params['keys']):
values[key] = enum.EnumValue(enumtype, i, key)
params['values'] = values
class XTest_Module(object):
""" Test case for the module. """
def setUp(self):
""" Set up test fixtures. """
from sys import modules
self.module = modules['enum']
def test_author_name_is_string(self):
""" Module should have __author_name__ string. """
mod_author_name = self.module.__author_name__
self.assertTrue(isinstance(mod_author_name, basestring))
def test_author_email_is_string(self):
""" Module should have __author_email__ string. """
mod_author_email = self.module.__author_email__
self.assertTrue(isinstance(mod_author_email, basestring))
def test_author_is_string(self):
""" Module should have __author__ string. """
mod_author = self.module.__author__
self.assertTrue(isinstance(mod_author, basestring))
def test_author_contains_name(self):
""" Module __author__ string should contain author name. """
mod_author = self.module.__author__
mod_author_name = self.module.__author_name__
self.assertTrue(mod_author.startswith(mod_author_name))
def test_author_contains_email(self):
""" Module __author__ string should contain author email. """
mod_author = self.module.__author__
mod_author_email = self.module.__author_email__
self.assertTrue(mod_author.endswith("<%(mod_author_email)s>" % vars()))
def test_date_is_string(self):
""" Module should have __date__ string. """
mod_date = self.module.__date__
self.assertTrue(isinstance(mod_date, basestring))
def test_copyright_is_string(self):
""" Module should have __copyright__ string. """
mod_copyright = self.module.__copyright__
self.assertTrue(isinstance(mod_copyright, basestring))
def test_copyright_contains_name(self):
""" Module __copyright__ string should contain author name. """
mod_copyright = self.module.__copyright__
mod_author_name = self.module.__author_name__
self.assertTrue(mod_copyright.endswith(mod_author_name))
def test_copyright_contains_begin_year(self):
""" Module __copyright__ string should contain beginning year. """
mod_copyright = self.module.__copyright__
year_begin = self.module._copyright_year_begin
self.assertTrue(year_begin in mod_copyright)
def test_copyright_contains_latest_year(self):
""" Module __copyright__ string should contain latest year.. """
mod_copyright = self.module.__copyright__
year_latest = self.module.__date__.split('-')[0]
self.assertTrue(year_latest in mod_copyright)
def test_license_is_string(self):
""" Module should have __license__ string. """
mod_license = self.module.__license__
self.assertTrue(isinstance(mod_license, basestring))
def test_url_is_string(self):
""" Module should have __url__ string. """
mod_url = self.module.__url__
self.assertTrue(isinstance(mod_url, basestring))
def test_version_is_string(self):
""" Module should have __version__ string. """
mod_version = self.module.__version__
self.assertTrue(isinstance(mod_version, basestring))
class Test_EnumException(unittest.TestCase):
""" Test case for the Enum exception classes. """
def setUp(self):
""" Set up test fixtures. """
self.valid_exceptions = {
enum.EnumEmptyError: dict(
min_args = 0,
types = (enum.EnumException, AssertionError),
),
enum.EnumBadKeyError: dict(
min_args = 1,
types = (enum.EnumException, TypeError),
),
enum.EnumBadIndexError: dict(
min_args = 2,
types = (enum.EnumException, AssertionError),
),
enum.EnumBadTypeError: dict(
min_args = 2,
types = (enum.EnumException, TypeError),
),
enum.EnumImmutableError: dict(
min_args = 1,
types = (enum.EnumException, TypeError),
),
}
for exc_type, params in self.valid_exceptions.items():
args = (None,) * params['min_args']
instance = exc_type(*args)
self.valid_exceptions[exc_type]['instance'] = instance
def test_EnumException_abstract(self):
""" The module exception base class should be abstract. """
self.assertRaises(
NotImplementedError, enum.EnumException)
def test_exception_instance(self):
""" Exception instance should be created. """
for exc_type, params in self.valid_exceptions.items():
instance = params['instance']
self.assertTrue(instance)
def test_exception_types(self):
""" Exception instances should match expected types. """
for exc_type, params in self.valid_exceptions.items():
instance = params['instance']
for match_type in params['types']:
self.assertTrue(
isinstance(instance, match_type),
msg=(
"instance: %(instance)r, match_type: %(match_type)s"
) % vars())
class Test_EnumValue(unittest.TestCase):
""" Test case for the EnumValue class. """
enum_factory_class = Mock_Enum
def setUp(self):
""" Set up the test fixtures. """
setup_enum_value_fixtures(self)
def test_instantiate(self):
""" Creating an EnumValue instance should succeed. """
for enumtype, params in self.valid_values.items():
for key, instance in params['values'].items():
self.assertTrue(instance)
def test_enumtype_equal(self):
""" EnumValue should export its enum type. """
for enumtype, params in self.valid_values.items():
for key, instance in params['values'].items():
self.assertEqual(enumtype, instance.enumtype)
def test_key_equal(self):
""" EnumValue should export its string key. """
for enumtype, params in self.valid_values.items():
for key, instance in params['values'].items():
self.assertEqual(key, instance.key)
def test_str_key(self):
""" String value for EnumValue should be its key string. """
for enumtype, params in self.valid_values.items():
for key, instance in params['values'].items():
self.assertEqual(key, str(instance))
def test_index_equal(self):
""" EnumValue should export its sequence index. """
for enumtype, params in self.valid_values.items():
for i, key in enumerate(params['keys']):
instance = params['values'][key]
self.assertEqual(i, instance.index)
def test_repr(self):
""" Representation of EnumValue should be meaningful. """
for enumtype, params in self.valid_values.items():
for i, key in enumerate(params['keys']):
instance = params['values'][key]
repr_str = repr(instance)
self.assertTrue(repr_str.startswith('EnumValue('))
self.assertTrue(repr_str.count(repr(enumtype)))
self.assertTrue(repr_str.count(repr(i)))
self.assertTrue(repr_str.count(repr(key)))
self.assertTrue(repr_str.endswith(')'))
def test_hash_equal(self):
""" Each EnumValue instance should have same hash as its value. """
for enumtype, params in self.valid_values.items():
for i, key in enumerate(params['keys']):
instance = params['values'][key]
self.assertEqual(hash(i), hash(instance))
def test_hash_unequal(self):
""" Different EnumValue instances should have different hashes. """
for enumtype, params in self.valid_values.items():
for i, key in enumerate(params['keys']):
instance = params['values'][key]
for j, other_key in enumerate(params['keys']):
if i == j:
continue
other_instance = params['values'][other_key]
self.assertNotEqual(
hash(instance), hash(other_instance))
def Xtest_comparison_method_has_matching_name(self):
""" Comparison method should have matching name for attribute. """
for compare_func in compare_functions:
func_name = compare_func.__name__
expect_name = func_name
compare_method = getattr(enum.EnumValue, func_name)
self.assertEqual(
expect_name, compare_method.__name__)
def Xtest_comparison_method_has_docstring(self):
""" Comparison method should have docstring. """
for compare_func in compare_functions:
func_name = compare_func.__name__
compare_method = getattr(enum.EnumValue, func_name)
self.assertTrue(
isinstance(compare_method.__doc__, basestring))
def test_compare_equal(self):
""" An EnumValue should compare equal to its value. """
for enumtype, params in self.valid_values.items():
for i, key in enumerate(params['keys']):
instance = params['values'][key]
self.assertEqual(
instance, enum.EnumValue(enumtype, i, key))
def test_compare_unequal(self):
""" An EnumValue should compare different to other values. """
for enumtype, params in self.valid_values.items():
for i, key in enumerate(params['keys']):
instance = params['values'][key]
self.assertNotEqual(
instance,
enum.EnumValue(enumtype, None, None))
def XXtest_compare_sequence(self):
""" EnumValue instances should compare as their sequence order. """
for enumtype, params in self.valid_values.items():
for i, left_key in enumerate(params['keys']):
for j, right_key in enumerate(params['keys']):
for compare_func in compare_functions:
self.assertEqual(
compare_func(i, j),
compare_func(params['values'][left_key],
enum.EnumValue(enumtype, j, right_key))
)
def Xtest_compare_different_enum(self):
""" An EnumValue should not implement comparison to other enums. """
for enumtype, params in self.valid_values.items():
for i, key in enumerate(params['keys']):
for compare_func in compare_functions:
instance = params['values'][key]
test_value = enum.EnumValue(self.SimpleEnum, i, key)
compare_method = getattr(instance, compare_func.__name__)
compare_result = compare_method(test_value)
self.assertEqual(NotImplemented, compare_result)
def Xtest_compare_non_enum(self):
""" An EnumValue should not implement comparison to other types. """
test_value = enum.EnumValue(self.SimpleEnum, 0, 'test')
for other_value in self.other_values:
for compare_func in compare_functions:
compare_method = getattr(test_value, compare_func.__name__)
compare_result = compare_method(other_value)
self.assertEqual(NotImplemented, compare_result)
def Xtest_compare_equality_different_enum(self):
""" An EnumValue should compare inequal to values of other enums. """
for enumtype, params in self.valid_values.items():
for i, key in enumerate(params['keys']):
instance = params['values'][key]
test_value = enum.EnumValue(self.SimpleEnum, i, key)
self.assertNotEqual(test_value, instance)
def Xtest_compare_equality_non_enum(self):
""" An EnumValue should compare inequal to any other value. """
test_value = enum.EnumValue(self.SimpleEnum, 0, 'test')
for other_value in self.other_values:
self.assertNotEqual(test_value, other_value)
def test_sequence_other_values(self):
""" An EnumValue should compare sequentially to other values. """
test_value = enum.EnumValue(self.SimpleEnum, 0, 'test')
test_list = list(self.other_values)
test_list.append(test_value)
test_list.sort()
self.assertTrue(test_value in test_list)
def test_value_key(self):
""" An EnumValue should have the specified key. """
for enumtype, params in self.valid_values.items():
for key, instance in params['values'].items():
self.assertEqual(key, instance.key)
def test_value_enumtype(self):
""" An EnumValue should have its associated enumtype. """
for enumtype, params in self.valid_values.items():
for key, instance in params['values'].items():
self.assertEqual(enumtype, instance.enumtype)
class Test_Enum(unittest.TestCase):
""" Test case for the Enum class. """
enum_factory_class = enum.Enum
def setUp(self):
""" Set up the test fixtures. """
setup_enum_value_fixtures(self)
def test_empty_enum(self):
""" Enum constructor should refuse empty keys sequence. """
self.assertRaises(
enum.EnumEmptyError,
enum.Enum)
def test_bad_key(self):
""" Enum constructor should refuse non-string keys. """
for key in self.bad_keys:
args = ("valid", key, "valid")
self.assertRaises(
enum.EnumBadKeyError,
enum.Enum, *args)
def test_bad_index(self):
self.assertRaises(enum.EnumBadIndexError,
enum.Enum, (enum.EnumValue(None, 1, 'a')))
def test_bad_type(self):
self.assertRaises(enum.EnumBadTypeError,
enum.Enum, *(enum.EnumValue(None, 0, 'a'),
enum.EnumValue(1, 1, 'b')))
def test_value_attributes(self):
""" Enumeration should have attributes for each value. """
for enumtype, params in self.valid_values.items():
for i, key in enumerate(params['keys']):
instance = getattr(enumtype, key)
test_value = enum.EnumValue(enumtype, i, key)
self.assertEqual(test_value, instance)
def test_length(self):
""" Enumeration should have length of its value set. """
for enumtype, params in self.valid_values.items():
self.assertEqual(len(params['values']), len(enumtype))
def test_value_items(self):
""" Enumeration should have items for each value. """
for enumtype, params in self.valid_values.items():
for i, key in enumerate(params['keys']):
value = enumtype[i]
test_value = enum.EnumValue(enumtype, i, key)
self.assertEqual(test_value, value)
def test_iterable(self):
""" Enumeration class should iterate over its values. """
for enumtype, params in self.valid_values.items():
for i, value in enumerate(enumtype):
key = params['keys'][i]
test_value = params['values'][key]
self.assertEqual(value, test_value)
def test_iterate_sequence(self):
""" Enumeration iteration should match specified sequence. """
for enumtype, params in self.valid_values.items():
values_dict = params['values']
values_seq = [values_dict[key] for key in params['keys']]
enum_seq = [val for val in enumtype]
self.assertEqual(values_seq, enum_seq)
self.assertNotEqual(values_seq.reverse(), enum_seq)
def XXtest_membership_bogus(self):
""" Enumeration should not contain bogus values. """
for enumtype, params in self.valid_values.items():
for value in self.other_values:
print(value in self.other_values, value)
self.assertFalse(value in enumtype)
def test_membership_value(self):
""" Enumeration should contain explicit value. """
for enumtype, params in self.valid_values.items():
for i, key in enumerate(params['keys']):
value = params['values'][key]
self.assertTrue(value in enumtype)
def test_membership_key(self):
""" Enumeration should contain key string. """
for enumtype, params in self.valid_values.items():
for key in params['keys']:
self.assertTrue(key in enumtype)
def test_add_attribute(self):
""" Enumeration should refuse attribute addition. """
for enumtype, params in self.valid_values.items():
self.assertRaises(
enum.EnumImmutableError,
setattr, enumtype, 'bogus', "bogus")
def test_modify_attribute(self):
""" Enumeration should refuse attribute modification. """
for enumtype, params in self.valid_values.items():
for key in params['keys']:
self.assertRaises(
enum.EnumImmutableError,
setattr, enumtype, key, "bogus")
def test_delete_attribute(self):
""" Enumeration should refuse attribute deletion. """
for enumtype, params in self.valid_values.items():
for key in params['keys']:
self.assertRaises(
enum.EnumImmutableError,
delattr, enumtype, key)
def test_add_item(self):
""" Enumeration should refuse item addition. """
for enumtype, params in self.valid_values.items():
index = len(params['keys'])
self.assertRaises(
enum.EnumImmutableError,
enumtype.__setitem__, index, "bogus")
def test_modify_item(self):
""" Enumeration should refuse item modification. """
for enumtype, params in self.valid_values.items():
for i, key in enumerate(params['keys']):
self.assertRaises(
enum.EnumImmutableError,
enumtype.__setitem__, i, "bogus")
def test_delete_item(self):
""" Enumeration should refuse item deletion. """
for enumtype, params in self.valid_values.items():
for i, key in enumerate(params['keys']):
self.assertRaises(
enum.EnumImmutableError,
enumtype.__delitem__, i)
def suite():
""" Create the test suite for this module. """
from sys import modules
loader = unittest.TestLoader()
suite = loader.loadTestsFromModule(modules[__name__])
return suite
def __main__(argv=None):
""" Mainline function for this module. """
import sys as _sys
if not argv:
argv = _sys.argv
exitcode = None
try:
unittest.main(argv=argv, defaultTest='suite')
except SystemExit as exc:
exitcode = exc.code
return exitcode
if __name__ == '__main__':
import sys
exitcode = __main__(sys.argv)
sys.exit(exitcode)
# Local variables:
# mode: python
# End:
# vim: filetype=python fileencoding=utf-8 :
|