-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtests.py
More file actions
191 lines (140 loc) · 3.75 KB
/
tests.py
File metadata and controls
191 lines (140 loc) · 3.75 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
from flyweight2.classes import Object as Obj, List, Dict, Set
class Object(Obj):
y = 0
def __init__(self, x):
self.x = x
def update(self, value):
self.x = value
@classmethod
def update_cls_attr_by_classmethod(cls, value):
cls.y = value
@staticmethod
def update_cls_attr_by_staticmethod(value):
Object.y = value
def update_cls_attr_by_instancemethod(self, value):
Object.y = value
obj_pool = getattr(Object, '_pool')
class TestObjectCreation(object):
def test_not_create_object(self):
assert len(obj_pool) == 0
def test_create_object_1(self):
a = Object(1)
assert a.__class__ == Object
assert type(a) != Object
assert isinstance(a, object)
def test_create_object_2(self):
a = Object(1)
assert a.x == 1
assert len(obj_pool) == 1
def test_create_object_3(self):
a = Object(1)
b = Object(1)
assert a.x == 1
assert b.x == 1
assert len(obj_pool) == 1
def test_create_object_4(self):
a = Object(1)
b = Object(2)
assert a.x == 1
assert b.x == 2
assert len(obj_pool) == 2
class TestObjectUpdateByDirectlyAssign(object):
def test_update_1(self):
a = Object(1)
b = Object(1)
a.x = 1
assert a.x == 1
assert b.x == 1
assert len(obj_pool) == 1
def test_update_2(self):
a = Object(1)
b = Object(1)
a.x = 2
assert a.x == 2
assert b.x == 1
assert len(obj_pool) == 2
def test_update_3(self):
a = Object(1)
b = Object(1)
c = Object(2)
a.x = 2
assert a.x == 2
assert b.x == 1
assert c.x == 2
assert len(obj_pool) == 2
def test_update_class_attribute(self):
a = Object(1)
b = Object(1)
a.y = 1
assert a.y == 1
assert b.y == 0
assert len(obj_pool) == 2
class TestObjectUpdateByOtherWay(object):
def test_update_by_method(self):
a = Object(1)
a.update(2)
assert a.x == 2
assert len(obj_pool) == 1
def test_update_cls_attr_by_instancemethod(self):
a = Object(1)
b = Object(1)
a.update_cls_attr_by_instancemethod(1)
assert a.y == 1
assert b.y == 1
assert len(obj_pool) == 1
def test_update_class_attribute_by_staticmethod(self):
a = Object(1)
b = Object(1)
a.update_cls_attr_by_staticmethod(1)
assert a.y == 1
assert b.y == 1
assert len(obj_pool) == 1
def test_update_class_attribute_by_classmethod(self):
a = Object(1)
b = Object(1)
a.update_cls_attr_by_classmethod(1)
assert a.y == 1
assert b.y == 1
assert len(obj_pool) == 1
def test_list():
list_pool = getattr(List, '_pool')
a1 = List()
a2 = List()
a3 = List([1])
assert a1 == []
assert a2 == []
assert a3[0] == 1
assert len(list_pool) == 2
a2.append(1)
assert a1 == []
assert a2[0] == 1
assert a3[0] == 1
assert len(list_pool) == 2
def test_dict():
dict_pool = getattr(Dict, '_pool')
a1 = Dict()
a2 = Dict()
a3 = Dict({1: 2})
assert a1 == {}
assert a2 == {}
assert a3[1] == 2
assert len(dict_pool) == 2
a2.update({1: 2})
assert a1 == {}
assert a2[1] == 2
assert a3[1] == 2
assert len(dict_pool) == 2
def test_set():
set_pool = getattr(Set, '_pool')
a1 = Set()
a2 = Set()
a3 = Set({1})
assert a1 == set()
assert a2 == set()
assert a3 == {1}
assert len(set_pool) == 2
a2.add(1)
assert a1 == set()
assert a2 == {1}
assert a3 == {1}
assert len(set_pool) == 2