forked from potfur/lcom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_reflection.py
122 lines (88 loc) · 3.7 KB
/
test_reflection.py
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
from pytest import raises
from src.reflection import ModuleReflection, ReflectionError
class ReflectionTestCase(object):
@classmethod
def setup_class(cls):
cls.module = ModuleReflection.from_file('./tests/fixtures.py')
class TestModuleReflection(ReflectionTestCase):
def test_filename_handles_slashes(self):
ref = ModuleReflection.from_file('./tests/fixtures.py')
assert ref.class_by_name('tests.fixtures.Reflection')
def test_filename_handles_backslashes(self):
ref = ModuleReflection.from_file('.\\tests\\fixtures.py')
assert ref.class_by_name('tests.fixtures.Reflection')
def test_lists_classes(self):
result = {cls.name() for cls in self.module.classes()}
assert result == {
'tests.fixtures.Zero',
'tests.fixtures.One',
'tests.fixtures.DeepOne',
'tests.fixtures.Two',
'tests.fixtures.Three',
'tests.fixtures.Loose',
'tests.fixtures.Reflection',
}
class TestClassReflection(ReflectionTestCase):
def setup_method(self):
self.ref = self.module.class_by_name('tests.fixtures.Reflection')
def test_list_variables(self):
result = {var for var in self.ref.vars()}
assert result == {
'CONST',
'__x',
'__y'
}
def test_list_class_methods(self):
result = {method.name() for method in self.ref.methods()}
assert result == {
'tests.fixtures.Reflection::decorated',
'tests.fixtures.Reflection::__init__',
'tests.fixtures.Reflection::get_x',
'tests.fixtures.Reflection::get_y',
'tests.fixtures.Reflection::loose',
'tests.fixtures.Reflection::methods',
'tests.fixtures.Reflection::vars',
'tests.fixtures.Reflection::consts',
}
def test_method_by_name(self):
expected = 'tests.fixtures.Reflection::get_x'
assert self.ref.method_by_name('get_x').name() == expected
def test_unknown_method_by_name(self):
with raises(ReflectionError) as e:
self.ref.method_by_name('foobar')
msg = 'Unknown method %s'
assert str(e.value) == msg % 'foobar'
class TestMethodReflection(ReflectionTestCase):
def setup_method(self):
self.ref = self.module.class_by_name('tests.fixtures.Reflection')
def test_list_used_methods(self):
ref = self.ref.method_by_name('methods')
assert set(ref.calls()) == {
'tests.fixtures.Reflection::get_x',
'tests.fixtures.Reflection::get_y'
}
def test_list_used_class_variables(self):
ref = self.ref.method_by_name('consts')
assert set(ref.vars()) == {'CONST'}
def test_list_used_instance_variables(self):
ref = self.ref.method_by_name('vars')
assert set(ref.vars()) == {'__x', '__y'}
def test_is_a_constructor(self):
ref = self.ref.method_by_name('__init__')
assert ref.is_constructor() is True
def test_is_not_a_constructor(self):
ref = self.ref.method_by_name('methods')
assert ref.is_constructor() is False
def test_is_loose_method(self):
ref = self.ref.method_by_name('loose')
assert ref.is_loose() is True
def test_is_not_a_loose_method(self):
ref = self.ref.method_by_name('methods')
assert ref.is_loose() is False
def test_has_decorator(self):
ref = self.ref.method_by_name('decorated')
assert ref.has_decorator('classmethod') is True
assert ref.has_decorator('foobar') is False
def test_has_not_decorator(self):
ref = self.ref.method_by_name('methods')
assert ref.has_decorator('foo') is False