/usr/lib/python2.7/dist-packages/test_characteristic.py is in python-characteristic 14.3.0-2.
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 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 | from __future__ import absolute_import, division, print_function
import linecache
import sys
import warnings
import pytest
from characteristic import (
Attribute,
NOTHING,
PY26,
_attrs_to_script,
_ensure_attributes,
attributes,
immutable,
with_cmp,
with_init,
with_repr,
)
PY2 = sys.version_info[0] == 2
warnings.simplefilter("always")
class TestAttribute(object):
def test_init_simple(self):
"""
Instantiating with just the name initializes properly.
"""
a = Attribute("foo")
assert "foo" == a.name
assert NOTHING is a.default_value
def test_init_default_factory(self):
"""
Instantiating with default_factory creates a proper descriptor for
_default.
"""
a = Attribute("foo", default_factory=list)
assert NOTHING is a.default_value
assert list() == a.default_factory()
def test_init_default_value(self):
"""
Instantiating with default_value initializes default properly.
"""
a = Attribute("foo", default_value="bar")
assert "bar" == a.default_value
def test_ambiguous_defaults(self):
"""
Instantiating with both default_value and default_factory raises
ValueError.
"""
with pytest.raises(ValueError):
Attribute(
"foo",
default_value="bar",
default_factory=lambda: 42
)
def test_missing_attr(self):
"""
Accessing inexistent attributes still raises an AttributeError.
"""
a = Attribute("foo")
with pytest.raises(AttributeError):
a.bar
def test_alias(self):
"""
If an attribute with a leading _ is defined, the initializer keyword
is stripped of it.
"""
a = Attribute("_private")
assert "private" == a._kw_name
def test_non_alias(self):
"""
The keyword name of a non-private
"""
a = Attribute("public")
assert "public" == a._kw_name
def test_dunder(self):
"""
Dunder gets all _ stripped.
"""
a = Attribute("__very_private")
assert "very_private" == a._kw_name
def test_init_aliaser_none(self):
"""
No aliasing if init_aliaser is None.
"""
a = Attribute("_private", init_aliaser=None)
assert a.name == a._kw_name
def test_init_aliaser(self):
"""
Any callable works for aliasing.
"""
a = Attribute("a", init_aliaser=lambda _: "foo")
assert "foo" == a._kw_name
def test_repr(self):
"""
repr returns the correct string.
"""
a = Attribute(
name="name",
exclude_from_cmp=True,
exclude_from_init=True,
exclude_from_repr=True,
exclude_from_immutable=True,
default_value=42,
instance_of=str,
init_aliaser=None
)
assert (
"<Attribute(name='name', exclude_from_cmp=True, "
"exclude_from_init=True, exclude_from_repr=True, "
"exclude_from_immutable=True, "
"default_value=42, default_factory=None, instance_of=<{0} 'str'>,"
" init_aliaser=None)>"
).format("type" if PY2 else "class") == repr(a)
def test_eq_different_types(self):
"""
Comparing Attribute with something else returns NotImplemented.
"""
assert NotImplemented == Attribute(name="name").__eq__(None)
def test_eq_equal(self):
"""
Equal Attributes are detected equal.
"""
kw = {
"name": "name",
"exclude_from_cmp": True,
"exclude_from_init": False,
"exclude_from_repr": True,
"exclude_from_immutable": False,
"default_value": 42,
"instance_of": int,
}
assert Attribute(**kw) == Attribute(**kw)
def test_eq_unequal(self):
"""
Equal Attributes are detected equal.
"""
kw = {
"name": "name",
"exclude_from_cmp": True,
"exclude_from_init": False,
"exclude_from_repr": True,
"exclude_from_immutable": False,
"default_value": 42,
"instance_of": int,
}
for arg in kw.keys():
kw_mutated = dict(**kw)
kw_mutated[arg] = "mutated"
assert Attribute(**kw) != Attribute(**kw_mutated)
@with_cmp(["a", "b"])
class CmpC(object):
def __init__(self, a, b):
self.a = a
self.b = b
class TestWithCmp(object):
def test_equal(self):
"""
Equal objects are detected as equal.
"""
assert CmpC(1, 2) == CmpC(1, 2)
assert not (CmpC(1, 2) != CmpC(1, 2))
def test_unequal_same_class(self):
"""
Unequal objects of correct type are detected as unequal.
"""
assert CmpC(1, 2) != CmpC(2, 1)
assert not (CmpC(1, 2) == CmpC(2, 1))
def test_unequal_different_class(self):
"""
Unequal objects of differnt type are detected even if their attributes
match.
"""
class NotCmpC(object):
a = 1
b = 2
assert CmpC(1, 2) != NotCmpC()
assert not (CmpC(1, 2) == NotCmpC())
@pytest.mark.parametrize(
"a,b", [
((1, 2), (2, 1)),
((1, 2), (1, 3)),
(("a", "b"), ("b", "a")),
]
)
def test_lt(self, a, b):
"""
__lt__ compares objects as tuples of attribute values.
"""
assert CmpC(*a) < CmpC(*b)
def test_lt_unordable(self):
"""
__lt__ returns NotImplemented if classes differ.
"""
assert NotImplemented == (CmpC(1, 2).__lt__(42))
@pytest.mark.parametrize(
"a,b", [
((1, 2), (2, 1)),
((1, 2), (1, 3)),
((1, 1), (1, 1)),
(("a", "b"), ("b", "a")),
(("a", "b"), ("a", "b")),
]
)
def test_le(self, a, b):
"""
__le__ compares objects as tuples of attribute values.
"""
assert CmpC(*a) <= CmpC(*b)
def test_le_unordable(self):
"""
__le__ returns NotImplemented if classes differ.
"""
assert NotImplemented == (CmpC(1, 2).__le__(42))
@pytest.mark.parametrize(
"a,b", [
((2, 1), (1, 2)),
((1, 3), (1, 2)),
(("b", "a"), ("a", "b")),
]
)
def test_gt(self, a, b):
"""
__gt__ compares objects as tuples of attribute values.
"""
assert CmpC(*a) > CmpC(*b)
def test_gt_unordable(self):
"""
__gt__ returns NotImplemented if classes differ.
"""
assert NotImplemented == (CmpC(1, 2).__gt__(42))
@pytest.mark.parametrize(
"a,b", [
((2, 1), (1, 2)),
((1, 3), (1, 2)),
((1, 1), (1, 1)),
(("b", "a"), ("a", "b")),
(("a", "b"), ("a", "b")),
]
)
def test_ge(self, a, b):
"""
__ge__ compares objects as tuples of attribute values.
"""
assert CmpC(*a) >= CmpC(*b)
def test_ge_unordable(self):
"""
__ge__ returns NotImplemented if classes differ.
"""
assert NotImplemented == (CmpC(1, 2).__ge__(42))
def test_hash(self):
"""
__hash__ returns different hashes for different values.
"""
assert hash(CmpC(1, 2)) != hash(CmpC(1, 1))
def test_Attribute_exclude_from_cmp(self):
"""
Ignores attribute if exclude_from_cmp=True.
"""
@with_cmp([Attribute("a", exclude_from_cmp=True), "b"])
class C(object):
def __init__(self, a, b):
self.a = a
self.b = b
assert C(42, 1) == C(23, 1)
@with_repr(["a", "b"])
class ReprC(object):
def __init__(self, a, b):
self.a = a
self.b = b
class TestReprAttrs(object):
def test_repr(self):
"""
Test repr returns a sensible value.
"""
assert "<ReprC(a=1, b=2)>" == repr(ReprC(1, 2))
def test_Attribute_exclude_from_repr(self):
"""
Ignores attribute if exclude_from_repr=True.
"""
@with_repr([Attribute("a", exclude_from_repr=True), "b"])
class C(object):
def __init__(self, a, b):
self.a = a
self.b = b
assert "<C(b=2)>" == repr(C(1, 2))
@with_init([Attribute("a"), Attribute("b")])
class InitC(object):
def __init__(self):
if self.a == self.b:
raise ValueError
class TestWithInit(object):
def test_sets_attributes(self):
"""
The attributes are initialized using the passed keywords.
"""
obj = InitC(a=1, b=2)
assert 1 == obj.a
assert 2 == obj.b
def test_custom_init(self):
"""
The class initializer is called too.
"""
with pytest.raises(ValueError):
InitC(a=1, b=1)
def test_passes_args(self):
"""
All positional parameters are passed to the original initializer.
"""
@with_init(["a"])
class InitWithArg(object):
def __init__(self, arg):
self.arg = arg
obj = InitWithArg(42, a=1)
assert 42 == obj.arg
assert 1 == obj.a
def test_passes_remaining_kw(self):
"""
Keyword arguments that aren't used for attributes are passed to the
original initializer.
"""
@with_init(["a"])
class InitWithKWArg(object):
def __init__(self, kw_arg=None):
self.kw_arg = kw_arg
obj = InitWithKWArg(a=1, kw_arg=42)
assert 42 == obj.kw_arg
assert 1 == obj.a
def test_does_not_pass_attrs(self):
"""
The attributes are removed from the keyword arguments before they are
passed to the original initializer.
"""
@with_init(["a"])
class InitWithKWArgs(object):
def __init__(self, **kw):
assert "a" not in kw
assert "b" in kw
InitWithKWArgs(a=1, b=42)
def test_defaults(self):
"""
If defaults are passed, they are used as fallback.
"""
@with_init(["a", "b"], defaults={"b": 2})
class InitWithDefaults(object):
pass
obj = InitWithDefaults(a=1)
assert 2 == obj.b
def test_missing_arg(self):
"""
Raises `ValueError` if a value isn't passed.
"""
with pytest.raises(ValueError) as e:
InitC(a=1)
assert "Missing keyword value for 'b'." == e.value.args[0]
def test_defaults_conflict(self):
"""
Raises `ValueError` if both defaults and an Attribute are passed.
"""
with pytest.raises(ValueError) as e:
@with_init([Attribute("a")], defaults={"a": 42})
class C(object):
pass
assert (
"Mixing of the 'defaults' keyword argument and passing instances "
"of Attribute for 'attrs' is prohibited. Please don't use "
"'defaults' anymore, it has been deprecated in 14.0."
== e.value.args[0]
)
def test_attribute(self):
"""
String attributes are converted to Attributes and thus work.
"""
@with_init(["a"])
class C(object):
pass
o = C(a=1)
assert 1 == o.a
def test_default_factory(self):
"""
The default factory is used for each instance of missing keyword
argument.
"""
@with_init([Attribute("a", default_factory=list)])
class C(object):
pass
o1 = C()
o2 = C()
assert o1.a is not o2.a
def test_underscores(self):
"""
with_init takes keyword aliasing into account.
"""
@with_init([Attribute("_a")])
class C(object):
pass
c = C(a=1)
assert 1 == c._a
def test_plain_no_alias(self):
"""
str-based attributes don't get aliased for backward-compatibility.
"""
@with_init(["_a"])
class C(object):
pass
c = C(_a=1)
assert 1 == c._a
def test_instance_of_fail(self):
"""
Raise `TypeError` if an Attribute with an `instance_of` is is attempted
to be set to a mismatched type.
"""
@with_init([Attribute("a", instance_of=int)])
class C(object):
pass
with pytest.raises(TypeError) as e:
C(a="not an int!")
assert (
"Attribute 'a' must be an instance of 'int'."
== e.value.args[0]
)
def test_instance_of_success(self):
"""
Setting an attribute to a value that doesn't conflict with an
`instance_of` declaration works.
"""
@with_init([Attribute("a", instance_of=int)])
class C(object):
pass
c = C(a=42)
assert 42 == c.a
def test_Attribute_exclude_from_init(self):
"""
Ignores attribute if exclude_from_init=True.
"""
@with_init([Attribute("a", exclude_from_init=True), "b"])
class C(object):
pass
C(b=1)
def test_deprecation_defaults(self):
"""
Emits a DeprecationWarning if `defaults` is used.
"""
with warnings.catch_warnings(record=True) as w:
@with_init(["a"], defaults={"a": 42})
class C(object):
pass
assert (
'`defaults` has been deprecated in 14.0, please use the '
'`Attribute` class instead.'
) == w[0].message.args[0]
assert issubclass(w[0].category, DeprecationWarning)
def test_linecache(self):
"""
The created init method is added to the linecache so PDB shows it
properly.
"""
attrs = [Attribute("a")]
@with_init(attrs)
class C(object):
pass
assert tuple == type(linecache.cache[C.__init__.__code__.co_filename])
def test_linecache_attrs_unique(self):
"""
If the attributes are the same, only one linecache entry is created.
Since the key within the cache is the filename, this effectively means
that the filenames must be equal if the attributes are equal.
"""
attrs = [Attribute("a")]
@with_init(attrs[:])
class C1(object):
pass
@with_init(attrs[:])
class C2(object):
pass
assert (
C1.__init__.__code__.co_filename
== C2.__init__.__code__.co_filename
)
def test_linecache_different_attrs(self):
"""
Different Attributes have different generated filenames.
"""
@with_init([Attribute("a")])
class C1(object):
pass
@with_init([Attribute("b")])
class C2(object):
pass
assert (
C1.__init__.__code__.co_filename
!= C2.__init__.__code__.co_filename
)
def test_no_attributes(self):
"""
Specifying no attributes doesn't raise an exception.
"""
@with_init([])
class C(object):
pass
C()
class TestAttributes(object):
def test_leaves_init_alone(self):
"""
If *apply_with_init* or *create_init* is `False`, leave __init__ alone.
"""
@attributes(["a"], apply_with_init=False)
class C(object):
pass
@attributes(["a"], create_init=False)
class CDeprecated(object):
pass
obj1 = C()
obj2 = CDeprecated()
with pytest.raises(AttributeError):
obj1.a
with pytest.raises(AttributeError):
obj2.a
def test_wraps_init(self):
"""
If *create_init* is `True`, build initializer.
"""
@attributes(["a", "b"], apply_with_init=True)
class C(object):
pass
obj = C(a=1, b=2)
assert 1 == obj.a
assert 2 == obj.b
def test_immutable(self):
"""
If *apply_immutable* is `True`, make class immutable.
"""
@attributes(["a"], apply_immutable=True)
class ImmuClass(object):
pass
obj = ImmuClass(a=42)
with pytest.raises(AttributeError):
obj.a = "23"
def test_apply_with_cmp(self):
"""
Don't add cmp methods if *apply_with_cmp* is `False`.
"""
@attributes(["a"], apply_with_cmp=False)
class C(object):
pass
obj = C(a=1)
if PY2:
assert None is getattr(obj, "__eq__", None)
else:
assert object.__eq__ == C.__eq__
def test_apply_with_repr(self):
"""
Don't add __repr__ if *apply_with_repr* is `False`.
"""
@attributes(["a"], apply_with_repr=False)
class C(object):
pass
assert repr(C(a=1)).startswith("<test_characteristic.")
def test_store_attributes(self):
"""
store_attributes is called on the class to store the attributes that
were passed in.
"""
attrs = [Attribute("a"), Attribute("b")]
@attributes(
attrs, store_attributes=lambda cls, a: setattr(cls, "foo", a)
)
class C(object):
pass
assert C.foo == attrs
def test_store_attributes_stores_Attributes(self):
"""
The attributes passed to store_attributes are always instances of
Attribute, even if they were simple strings when provided.
"""
@attributes(["a", "b"])
class C(object):
pass
assert C.characteristic_attributes == [Attribute("a"), Attribute("b")]
def test_store_attributes_defaults_to_characteristic_attributes(self):
"""
By default, store_attributes stores the attributes in
`characteristic_attributes` on the class.
"""
attrs = [Attribute("a")]
@attributes(attrs)
class C(object):
pass
assert C.characteristic_attributes == attrs
def test_private(self):
"""
Integration test for name mangling/aliasing.
"""
@attributes([Attribute("_a")])
class C(object):
pass
c = C(a=42)
assert 42 == c._a
def test_private_no_alias(self):
"""
Integration test for name mangling/aliasing.
"""
@attributes([Attribute("_a", init_aliaser=None)])
class C(object):
pass
c = C(_a=42)
assert 42 == c._a
def test_deprecation_create_init(self):
"""
Emits a DeprecationWarning if `create_init` is used.
"""
with warnings.catch_warnings(record=True) as w:
@attributes(["a"], create_init=False)
class C(object):
pass
assert (
'`create_init` has been deprecated in 14.0, please use '
'`apply_with_init`.'
) == w[0].message.args[0]
assert issubclass(w[0].category, DeprecationWarning)
def test_deprecation_defaults(self):
"""
Emits a DeprecationWarning if `defaults` is used.
"""
with warnings.catch_warnings(record=True) as w:
@attributes(["a"], defaults={"a": 42})
class C(object):
pass
assert (
'`defaults` has been deprecated in 14.0, please use the '
'`Attribute` class instead.'
) == w[0].message.args[0]
assert issubclass(w[0].category, DeprecationWarning)
def test_does_not_allow_extra_keyword_arguments(self):
"""
Keyword arguments other than the ones consumed are still TypeErrors.
"""
with pytest.raises(TypeError) as e:
@attributes(["a"], not_an_arg=12)
class C(object):
pass
assert e.value.args == (
"attributes() got an unexpected keyword argument 'not_an_arg'",
)
def test_no_attributes(self):
"""
Specifying no attributes doesn't raise an exception.
"""
@attributes([])
class C(object):
pass
C()
class TestEnsureAttributes(object):
def test_leaves_attribute_alone(self):
"""
List items that are an Attribute stay an Attribute.
"""
a = Attribute("a")
assert a is _ensure_attributes([a], {})[0]
def test_converts_rest(self):
"""
Any other item will be transformed into an Attribute.
"""
l = _ensure_attributes(["a"], {})
assert isinstance(l[0], Attribute)
assert "a" == l[0].name
def test_defaults(self):
"""
Legacy defaults are translated into default_value attributes.
"""
l = _ensure_attributes(["a"], {"a": 42})
assert 42 == l[0].default_value
def test_defaults_Attribute(self):
"""
Raises ValueError on defaults != {} and an Attribute within attrs.
"""
with pytest.raises(ValueError):
_ensure_attributes([Attribute("a")], defaults={"a": 42})
class TestImmutable(object):
def test_bare(self):
"""
In an immutable class, setting an definition-time attribute raises an
AttributeError.
"""
@immutable(["foo"])
class ImmuClass(object):
foo = "bar"
i = ImmuClass()
with pytest.raises(AttributeError):
i.foo = "not bar"
def test_Attribute(self):
"""
Mutation is caught if user passes an Attribute instance.
"""
@immutable([Attribute("foo")])
class ImmuClass(object):
def __init__(self):
self.foo = "bar"
i = ImmuClass()
with pytest.raises(AttributeError):
i.foo = "not bar"
def test_init(self):
"""
Changes within __init__ are allowed.
"""
@immutable(["foo"])
class ImmuClass(object):
def __init__(self):
self.foo = "bar"
i = ImmuClass()
assert "bar" == i.foo
def test_with_init(self):
"""
Changes in with_init's initializer are allowed.
"""
@immutable(["foo"])
@with_init(["foo"])
class ImmuClass(object):
pass
i = ImmuClass(foo="qux")
assert "qux" == i.foo
def test_Attribute_exclude_from_immutable(self):
"""
Ignores attribute if exclude_from_immutable=True.
"""
@immutable([Attribute("a", exclude_from_immutable=True), "b"])
class C(object):
def __init__(self, a, b):
self.a = a
self.b = b
c = C(1, 2)
c.a = 3
with pytest.raises(AttributeError):
c.b = 4
class TestAttrsToScript(object):
@pytest.mark.skipif(PY26, reason="Optimization works only on Python 2.7.")
def test_optimizes_simple(self):
"""
If no defaults and extra checks are passed, an optimized version is
used on Python 2.7+.
"""
attrs = [Attribute("a")]
script = _attrs_to_script(attrs)
assert "except KeyError as e:" in script
def test_nothing():
"""
``NOTHING`` has a sensible repr.
"""
assert "NOTHING" == repr(NOTHING)
def test_doc():
"""
The characteristic module has a docstring.
"""
import characteristic
assert characteristic.__doc__
|