|
1
|
|
|
from injectify.inspect_mate import ( |
|
2
|
|
|
is_attribute, is_property_method, is_regular_method, is_static_method, |
|
3
|
|
|
is_class_method, get_attributes, get_property_methods, get_regular_methods, |
|
4
|
|
|
get_static_methods, get_class_methods, get_all_attributes, get_all_methods, |
|
5
|
|
|
) |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
class Base: |
|
9
|
|
|
attribute = 'attribute' |
|
10
|
|
|
|
|
11
|
|
|
@property |
|
12
|
|
|
def property_method(self): |
|
13
|
|
|
return 'property_method' |
|
14
|
|
|
|
|
15
|
|
|
def regular_method(self): |
|
16
|
|
|
return 'regular_method' |
|
17
|
|
|
|
|
18
|
|
|
@staticmethod |
|
19
|
|
|
def static_method(): |
|
20
|
|
|
return 'static_method' |
|
21
|
|
|
|
|
22
|
|
|
@classmethod |
|
23
|
|
|
def class_method(cls): |
|
24
|
|
|
return 'class_method' |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
class MyClass(Base): |
|
28
|
|
|
pass |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
def test_is_attribute_property_method_regular_method_static_method_class_method(): |
|
32
|
|
|
assert is_attribute(MyClass, 'attribute', MyClass.attribute) |
|
33
|
|
|
assert is_property_method( |
|
34
|
|
|
MyClass, 'property_method', MyClass.property_method) |
|
35
|
|
|
assert is_regular_method( |
|
36
|
|
|
MyClass, 'regular_method', MyClass.regular_method) |
|
37
|
|
|
assert is_static_method( |
|
38
|
|
|
MyClass, 'static_method', MyClass.static_method) |
|
39
|
|
|
assert is_class_method(MyClass, 'class_method', MyClass.class_method) |
|
40
|
|
|
|
|
41
|
|
|
attr_list = [ |
|
42
|
|
|
(MyClass, 'attribute', MyClass.attribute), |
|
43
|
|
|
(MyClass, 'property_method', MyClass.property_method), |
|
44
|
|
|
(MyClass, 'regular_method', MyClass.regular_method), |
|
45
|
|
|
(MyClass, 'static_method', MyClass.static_method), |
|
46
|
|
|
(MyClass, 'class_method', MyClass.class_method), |
|
47
|
|
|
] |
|
48
|
|
|
|
|
49
|
|
|
checker_list = [ |
|
50
|
|
|
is_attribute, |
|
51
|
|
|
is_property_method, |
|
52
|
|
|
is_regular_method, |
|
53
|
|
|
is_static_method, |
|
54
|
|
|
is_class_method, |
|
55
|
|
|
] |
|
56
|
|
|
|
|
57
|
|
|
for i, pair in enumerate(attr_list): |
|
58
|
|
|
klass, attr, value = pair |
|
59
|
|
|
for j, checker in enumerate(checker_list): |
|
60
|
|
|
if i == j: |
|
61
|
|
|
assert checker(klass, attr, value) is True |
|
62
|
|
|
else: |
|
63
|
|
|
assert checker(klass, attr, value) is False |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
def test_getter(): |
|
67
|
|
|
def items_to_keys(items): |
|
68
|
|
|
return set([item[0] for item in items]) |
|
69
|
|
|
|
|
70
|
|
|
assert items_to_keys(get_attributes(MyClass)) == {'attribute'} |
|
71
|
|
|
assert items_to_keys( |
|
72
|
|
|
get_property_methods(MyClass)) == {'property_method'} |
|
73
|
|
|
assert items_to_keys( |
|
74
|
|
|
get_regular_methods(MyClass)) == {'regular_method'} |
|
75
|
|
|
assert items_to_keys( |
|
76
|
|
|
get_static_methods(MyClass)) == {'static_method'} |
|
77
|
|
|
assert items_to_keys( |
|
78
|
|
|
get_class_methods(MyClass)) == {'class_method'} |
|
79
|
|
|
assert items_to_keys( |
|
80
|
|
|
get_all_attributes(MyClass)) == {'attribute', 'property_method'} |
|
81
|
|
|
assert items_to_keys( |
|
82
|
|
|
get_all_methods(MyClass)) == {'regular_method', 'static_method', 'class_method'} |
|
83
|
|
|
|