aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrian Harring <ferringb@gmail.com>2022-12-24 13:14:53 -0800
committerArthur Zamarin <arthurzam@gentoo.org>2022-12-25 19:49:11 +0200
commitd6a7c2e44b4f497357f8569d423104232a58f384 (patch)
tree625ac52169356714a9f5e69e11f2b6cc2d72355a /tests/test_obj.py
parentcompression: prefer gtar over tar if available (diff)
downloadsnakeoil-d6a7c2e44b4f497357f8569d423104232a58f384.tar.gz
snakeoil-d6a7c2e44b4f497357f8569d423104232a58f384.tar.bz2
snakeoil-d6a7c2e44b4f497357f8569d423104232a58f384.zip
Reformat w/ black 22.12.0 for consistency.
Signed-off-by: Brian Harring <ferringb@gmail.com> Signed-off-by: Arthur Zamarin <arthurzam@gentoo.org>
Diffstat (limited to 'tests/test_obj.py')
-rw-r--r--tests/test_obj.py85
1 files changed, 58 insertions, 27 deletions
diff --git a/tests/test_obj.py b/tests/test_obj.py
index 83f9f776..78083d8b 100644
--- a/tests/test_obj.py
+++ b/tests/test_obj.py
@@ -7,7 +7,6 @@ make_DIkls = obj.DelayedInstantiation_kls
class TestDelayedInstantiation:
-
def test_simple(self):
t = tuple([1, 2, 3])
o = make_DI(tuple, lambda: t)
@@ -19,19 +18,34 @@ class TestDelayedInstantiation:
assert t >= o
def test_descriptor_awareness(self):
- def assertKls(cls, ignores=(),
- default_ignores=("__new__", "__init__", "__init_subclass__",
- "__getattribute__", "__class__",
- "__getnewargs__", "__getstate__",
- "__doc__", "__class_getitem__")):
- required = set(x for x in dir(cls)
- if x.startswith("__") and x.endswith("__"))
+ def assertKls(
+ cls,
+ ignores=(),
+ default_ignores=(
+ "__new__",
+ "__init__",
+ "__init_subclass__",
+ "__getattribute__",
+ "__class__",
+ "__getnewargs__",
+ "__getstate__",
+ "__doc__",
+ "__class_getitem__",
+ ),
+ ):
+ required = set(
+ x for x in dir(cls) if x.startswith("__") and x.endswith("__")
+ )
missing = required.difference(obj.kls_descriptors)
missing.difference_update(obj.base_kls_descriptors)
missing.difference_update(default_ignores)
missing.difference_update(ignores)
- assert not missing, ("object %r potentially has unsupported special "
- "attributes: %s" % (cls, ', '.join(missing)))
+ assert (
+ not missing
+ ), "object %r potentially has unsupported special " "attributes: %s" % (
+ cls,
+ ", ".join(missing),
+ )
assertKls(object)
assertKls(1)
@@ -43,25 +57,38 @@ class TestDelayedInstantiation:
def test_BaseDelayedObject(self):
# assert that all methods/descriptors of object
# are covered via the base.
- o = set(dir(object)).difference(f"__{x}__" for x in (
- "class", "getattribute", "new", "init", "init_subclass", "getstate", "doc"))
+ o = set(dir(object)).difference(
+ f"__{x}__"
+ for x in (
+ "class",
+ "getattribute",
+ "new",
+ "init",
+ "init_subclass",
+ "getstate",
+ "doc",
+ )
+ )
diff = o.difference(obj.base_kls_descriptors)
- assert not diff, ("base delayed instantiation class should cover all of object, but "
- "%r was spotted" % (",".join(sorted(diff)),))
+ assert not diff, (
+ "base delayed instantiation class should cover all of object, but "
+ "%r was spotted" % (",".join(sorted(diff)),)
+ )
assert obj.DelayedInstantiation_kls(int, "1") + 2 == 3
-
def test_klass_choice_optimization(self):
"""ensure that BaseDelayedObject is used whenever possible"""
# note object is an odd one- it actually has a __doc__, thus
# it must always be a custom
o = make_DI(object, object)
- assert object.__getattribute__(o, '__class__') is not obj.BaseDelayedObject
+ assert object.__getattribute__(o, "__class__") is not obj.BaseDelayedObject
+
class foon:
pass
+
o = make_DI(foon, foon)
- cls = object.__getattribute__(o, '__class__')
+ cls = object.__getattribute__(o, "__class__")
assert cls is obj.BaseDelayedObject
# now ensure we always get the same kls back for derivatives
@@ -70,39 +97,43 @@ class TestDelayedInstantiation:
return True
o = make_DI(foon, foon)
- cls = object.__getattribute__(o, '__class__')
+ cls = object.__getattribute__(o, "__class__")
assert cls is not obj.BaseDelayedObject
o = make_DI(foon, foon)
- cls2 = object.__getattribute__(o, '__class__')
+ cls2 = object.__getattribute__(o, "__class__")
assert cls is cls2
def test__class__(self):
l = []
+
def f():
l.append(False)
return True
+
o = make_DI(bool, f)
assert isinstance(o, bool)
assert not l, "accessing __class__ shouldn't trigger instantiation"
def test__doc__(self):
l = []
+
def f():
l.append(True)
return foon()
+
class foon:
__doc__ = "monkey"
o = make_DI(foon, f)
- assert o.__doc__ == 'monkey'
+ assert o.__doc__ == "monkey"
assert not l, (
"in accessing __doc__, the instance was generated- "
"this is a class level attribute, thus shouldn't "
- "trigger instantiation")
+ "trigger instantiation"
+ )
class TestPopattr:
-
class Object:
pass
@@ -113,21 +144,21 @@ class TestPopattr:
def test_no_attrs(self):
# object without any attrs
with pytest.raises(AttributeError):
- obj.popattr(object(), 'nonexistent')
+ obj.popattr(object(), "nonexistent")
def test_nonexistent_attr(self):
# object with attr trying to get nonexistent attr
with pytest.raises(AttributeError):
- obj.popattr(self.o, 'nonexistent')
+ obj.popattr(self.o, "nonexistent")
def test_fallback(self):
# object with attr trying to get nonexistent attr using fallback
- value = obj.popattr(self.o, 'nonexistent', 2)
+ value = obj.popattr(self.o, "nonexistent", 2)
assert value == 2
def test_removed_attr(self):
- value = obj.popattr(self.o, 'test')
+ value = obj.popattr(self.o, "test")
assert value == 1
# verify that attr was removed from the object
with pytest.raises(AttributeError):
- obj.popattr(self.o, 'test')
+ obj.popattr(self.o, "test")