This repository has been archived by the owner. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_role.py
160 lines (112 loc) · 3.75 KB
/
test_role.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
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
import unittest
import pyrrole
class TestRole(unittest.TestCase):
def test_role_class(self):
class FallFromTree(metaclass=pyrrole.Role):
a = 1
def fall(self, tree='tree'):
print(f'Fall from {tree}')
class Fruit:
pass
@FallFromTree
class Apple(Fruit):
pass
apple = Apple()
self.assertIsInstance(apple, Fruit)
self.assertIn('FallFromTree', apple.__roles__)
self.assertEqual(apple.a, 1)
apple.fall()
apple.fall('apple tree')
self.assertTrue(pyrrole.has_role(apple, FallFromTree))
def test_roles_decorator(self):
class FallFromTree(metaclass=pyrrole.Role):
def fall(self, tree='tree'):
print(f'Fall from {tree}')
class Deciduous(metaclass=pyrrole.Role):
pass
class Fruit:
pass
@pyrrole.apply_roles(FallFromTree, Deciduous)
class Apple(Fruit):
pass
apple = Apple()
self.assertIsInstance(apple, Fruit)
self.assertTrue(pyrrole.has_role(apple, FallFromTree))
self.assertTrue(pyrrole.has_role(apple, Deciduous))
def test_role_decorator(self):
@pyrrole.role
class FallFromTree:
def __init__(self, cls):
self.fruits = list()
self.fruits.append(cls)
def fall(self, tree='tree'):
print(f'Fall from {tree}')
def __str__(self):
return "Fall from tree."
class Fruit:
pass
@FallFromTree
class Apple(Fruit):
pass
apple = Apple()
self.assertIsInstance(FallFromTree, pyrrole.pyrrole.Role)
self.assertIsInstance(apple, Fruit)
self.assertIn('FallFromTree', apple.__roles__)
self.assertTrue(pyrrole.pyrrole.has_role(apple, FallFromTree))
def test_role_method_decorator(self):
@pyrrole.role
class FallFromTree:
def __init__(self, cls):
self.fruits = list()
self.fruits.append(cls)
def fall(self, tree='tree'):
print(f'Fall from {tree}')
@pyrrole.role_method
def __str__(self):
return "Fall from tree string."
class Fruit:
pass
@FallFromTree
class Apple(Fruit):
pass
apple = Apple()
print(apple)
def test_role_name_conflict(self):
class FallFromTree(metaclass=pyrrole.Role):
def fall(self, tree='tree'):
print(f'Fall from {tree}')
class Fruit:
pass
@FallFromTree
@pyrrole.rename_role_methods(fall='fall_apple')
class Apple(Fruit):
def fall(self):
print('Apple fell')
apple = Apple()
apple.fall_apple()
self.assertIsInstance(apple, Fruit)
self.assertTrue(pyrrole.has_role(apple, FallFromTree))
@pyrrole.role
class Deciduous:
def __init__(self, cls):
self.tree = cls
def fall(self, tree='tree'):
print(f'Fall from {tree}')
class Fruit:
pass
@Deciduous
@pyrrole.rename_role_methods(fall='fall_apple')
@FallFromTree
@pyrrole.rename_role_methods(fall='fall_pear')
class Pear(Fruit):
def fall(self):
print('Pear fell')
pear = Pear()
pear.fall_pear()
pear.fall_apple()
pear.fall()
self.assertIsInstance(pear, Fruit)
self.assertTrue(pyrrole.has_role(pear, Deciduous))
self.assertTrue(pyrrole.has_role(pear, FallFromTree))
if __name__ == '__main__':
unittest.main()