| 1 |
"""Unit testing for get_defining_class in cmd2/utils.py module.""" |
| 2 |
|
| 3 |
import functools |
| 4 |
|
| 5 |
import cmd2.utils as cu |
| 6 |
|
| 7 |
|
| 8 |
class ParentClass: |
| 9 |
def func_with_overrides(self) -> None: |
| 10 |
pass |
| 11 |
|
| 12 |
def parent_only_func(self, param1, param2) -> None: |
| 13 |
pass |
| 14 |
|
| 15 |
|
| 16 |
class ChildClass(ParentClass): |
| 17 |
def func_with_overrides(self) -> None: |
| 18 |
super().func_with_overrides() |
| 19 |
|
| 20 |
def child_function(self) -> None: |
| 21 |
pass |
| 22 |
|
| 23 |
def lambda1() -> int: |
| 24 |
return 1 |
| 25 |
|
| 26 |
def lambda2() -> int: |
| 27 |
return 2 |
| 28 |
|
| 29 |
@classmethod |
| 30 |
def class_method(cls) -> None: |
| 31 |
pass |
| 32 |
|
| 33 |
@staticmethod |
| 34 |
def static_meth() -> None: |
| 35 |
pass |
| 36 |
|
| 37 |
|
| 38 |
def func_not_in_class() -> None: |
| 39 |
pass |
| 40 |
|
| 41 |
|
| 42 |
def test_get_defining_class() -> None: |
| 43 |
parent_instance = ParentClass() |
| 44 |
child_instance = ChildClass() |
| 45 |
|
| 46 |
|
| 47 |
assert cu.get_defining_class(ParentClass.func_with_overrides) is ParentClass |
| 48 |
assert cu.get_defining_class(ParentClass.parent_only_func) is ParentClass |
| 49 |
assert cu.get_defining_class(ChildClass.func_with_overrides) is ChildClass |
| 50 |
assert cu.get_defining_class(ChildClass.parent_only_func) is ParentClass |
| 51 |
assert cu.get_defining_class(ChildClass.child_function) is ChildClass |
| 52 |
assert cu.get_defining_class(ChildClass.class_method) is ChildClass |
| 53 |
assert cu.get_defining_class(ChildClass.static_meth) is ChildClass |
| 54 |
|
| 55 |
|
| 56 |
assert cu.get_defining_class(parent_instance.func_with_overrides) is ParentClass |
| 57 |
assert cu.get_defining_class(parent_instance.parent_only_func) is ParentClass |
| 58 |
assert cu.get_defining_class(child_instance.func_with_overrides) is ChildClass |
| 59 |
assert cu.get_defining_class(child_instance.parent_only_func) is ParentClass |
| 60 |
assert cu.get_defining_class(child_instance.child_function) is ChildClass |
| 61 |
assert cu.get_defining_class(child_instance.class_method) is ChildClass |
| 62 |
assert cu.get_defining_class(child_instance.static_meth) is ChildClass |
| 63 |
|
| 64 |
|
| 65 |
assert cu.get_defining_class(func_not_in_class) is None |
| 66 |
|
| 67 |
|
| 68 |
assert cu.get_defining_class(ChildClass.lambda1) is ChildClass |
| 69 |
assert cu.get_defining_class(ChildClass.lambda2) is ChildClass |
| 70 |
assert cu.get_defining_class(ChildClass().lambda1) is ChildClass |
| 71 |
assert cu.get_defining_class(ChildClass().lambda2) is ChildClass |
| 72 |
|
| 73 |
|
| 74 |
partial_unbound = functools.partial(ParentClass.parent_only_func, 1) |
| 75 |
nested_partial_unbound = functools.partial(partial_unbound, 2) |
| 76 |
assert cu.get_defining_class(partial_unbound) is ParentClass |
| 77 |
assert cu.get_defining_class(nested_partial_unbound) is ParentClass |
| 78 |
|
| 79 |
partial_bound = functools.partial(parent_instance.parent_only_func, 1) |
| 80 |
nested_partial_bound = functools.partial(partial_bound, 2) |
| 81 |
assert cu.get_defining_class(partial_bound) is ParentClass |
| 82 |
assert cu.get_defining_class(nested_partial_bound) is ParentClass |