|
1
|
|
|
# This Python file uses the following encoding: utf-8 |
|
2
|
|
|
|
|
3
|
|
|
from unittest import TestCase |
|
4
|
|
|
|
|
5
|
|
|
from galactic.type.category import * |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
class ImpreciseCategoryTest(TestCase): |
|
9
|
|
|
def test_imprecise_category(self): |
|
10
|
|
|
Null = imprecise_category('Null') |
|
11
|
|
|
self.assertEqual( |
|
12
|
|
|
str(Null), |
|
13
|
|
|
"test_category.Null", |
|
14
|
|
|
"The string representation of the class is not correct" |
|
15
|
|
|
) |
|
16
|
|
|
Color = imprecise_category('Color', ['R', 'G', 'B']) |
|
17
|
|
|
self.assertEqual( |
|
18
|
|
|
str(Color), |
|
19
|
|
|
"test_category.Color", |
|
20
|
|
|
"The string representation of the class is not correct" |
|
21
|
|
|
) |
|
22
|
|
|
Color = imprecise_category('Color', ['R', 'G', 'B'], module='mymodule') |
|
23
|
|
|
self.assertEqual( |
|
24
|
|
|
str(Color), |
|
25
|
|
|
"mymodule.Color", |
|
26
|
|
|
"The string representation of the class is not correct" |
|
27
|
|
|
) |
|
28
|
|
|
|
|
29
|
|
|
def test___new__(self): |
|
30
|
|
|
Color = imprecise_category('Color', ['R', 'G', 'B']) |
|
31
|
|
|
self.assertEqual( |
|
32
|
|
|
str(Color()), |
|
33
|
|
|
"{}", |
|
34
|
|
|
"The string representation of an empty category is not correct" |
|
35
|
|
|
) |
|
36
|
|
|
self.assertEqual( |
|
37
|
|
|
str(Color(['R', 'G'])), |
|
38
|
|
|
"{'R', 'G'}", |
|
39
|
|
|
"The string representation of a non-empty category is not correct" |
|
40
|
|
|
) |
|
41
|
|
|
self.assertFalse( |
|
42
|
|
|
Color(['R', 'G']) is Color(['R', 'G']), |
|
43
|
|
|
"The instances must not be identical" |
|
44
|
|
|
) |
|
45
|
|
|
Color = imprecise_category('Color', ['R', 'G', 'B'], cache=True) |
|
46
|
|
|
self.assertTrue( |
|
47
|
|
|
Color(['R', 'G']) is Color(['R', 'G']), |
|
48
|
|
|
"The instances must be identical" |
|
49
|
|
|
) |
|
50
|
|
|
|
|
51
|
|
|
def test___contains__(self): |
|
52
|
|
|
Color = imprecise_category('Color', ['R', 'G', 'B']) |
|
53
|
|
|
color = Color(['R', 'G']) |
|
54
|
|
|
self.assertTrue( |
|
55
|
|
|
'G' in color, |
|
56
|
|
|
"G belongs to the color" |
|
57
|
|
|
) |
|
58
|
|
|
self.assertFalse( |
|
59
|
|
|
'B' in color, |
|
60
|
|
|
"B does not belong to the color" |
|
61
|
|
|
) |
|
62
|
|
|
with self.assertRaises(ValueError): |
|
63
|
|
|
_ = 'X' in color |
|
64
|
|
|
|
|
65
|
|
|
def test___len__(self): |
|
66
|
|
|
Color = imprecise_category('Color', ['R', 'G', 'B']) |
|
67
|
|
|
color = Color(['R', 'G']) |
|
68
|
|
|
self.assertEqual( |
|
69
|
|
|
len(color), |
|
70
|
|
|
2, |
|
71
|
|
|
"The length of the color must be 2" |
|
72
|
|
|
) |
|
73
|
|
|
|
|
74
|
|
|
def test___iter__(self): |
|
75
|
|
|
Color = imprecise_category('Color', ['R', 'G', 'B']) |
|
76
|
|
|
color = Color(['R', 'G']) |
|
77
|
|
|
self.assertEqual( |
|
78
|
|
|
[item for item in color], |
|
79
|
|
|
['R', 'G'], |
|
80
|
|
|
"The iteration over the color must be ['R', 'G']" |
|
81
|
|
|
) |
|
82
|
|
|
|
|
83
|
|
View Code Duplication |
def test___le__(self): |
|
|
|
|
|
|
84
|
|
|
Color = imprecise_category('Color', ['R', 'G', 'B']) |
|
85
|
|
|
color1 = Color(['R']) |
|
86
|
|
|
color2 = Color(['R', 'G']) |
|
87
|
|
|
color3 = Color(['R', 'B']) |
|
88
|
|
|
self.assertTrue( |
|
89
|
|
|
color1 <= color2, |
|
90
|
|
|
"['R'] is a subset of ['R', 'G']" |
|
91
|
|
|
) |
|
92
|
|
|
self.assertTrue( |
|
93
|
|
|
color1 <= color1, |
|
94
|
|
|
"['R'] is a subset of ['R']" |
|
95
|
|
|
) |
|
96
|
|
|
self.assertFalse( |
|
97
|
|
|
color2 <= color3, |
|
98
|
|
|
"['R', 'G'] is not a subset of ['R', 'B']" |
|
99
|
|
|
) |
|
100
|
|
|
with self.assertRaises(TypeError): |
|
101
|
|
|
_ = color1 <= 'dummy' |
|
102
|
|
|
|
|
103
|
|
View Code Duplication |
def test___lt__(self): |
|
|
|
|
|
|
104
|
|
|
Color = imprecise_category('Color', ['R', 'G', 'B']) |
|
105
|
|
|
color1 = Color(['R']) |
|
106
|
|
|
color2 = Color(['R', 'G']) |
|
107
|
|
|
color3 = Color(['R', 'B']) |
|
108
|
|
|
self.assertTrue( |
|
109
|
|
|
color1 < color2, |
|
110
|
|
|
"['R'] is a strict subset of ['R', 'G']" |
|
111
|
|
|
) |
|
112
|
|
|
self.assertFalse( |
|
113
|
|
|
color1 < color1, |
|
114
|
|
|
"['R'] is not a strict subset of ['R']" |
|
115
|
|
|
) |
|
116
|
|
|
self.assertFalse( |
|
117
|
|
|
color2 < color3, |
|
118
|
|
|
"['R', 'G'] is not a strict subset of ['R', 'B']" |
|
119
|
|
|
) |
|
120
|
|
|
with self.assertRaises(TypeError): |
|
121
|
|
|
_ = color1 < 'dummy' |
|
122
|
|
|
|
|
123
|
|
|
def test___eq__(self): |
|
124
|
|
|
Color = imprecise_category('Color', ['R', 'G', 'B']) |
|
125
|
|
|
color1 = Color(['R']) |
|
126
|
|
|
color2 = Color(['R', 'G']) |
|
127
|
|
|
color3 = Color(['R', 'B']) |
|
128
|
|
|
self.assertFalse( |
|
129
|
|
|
color1 == color2, |
|
130
|
|
|
"['R'] is not equal to ['R', 'G']" |
|
131
|
|
|
) |
|
132
|
|
|
self.assertTrue( |
|
133
|
|
|
color1 == color1, |
|
134
|
|
|
"['R'] is equal to ['R']" |
|
135
|
|
|
) |
|
136
|
|
|
with self.assertRaises(TypeError): |
|
137
|
|
|
_ = color1 == 'dummy' |
|
138
|
|
|
|
|
139
|
|
View Code Duplication |
def test___gt__(self): |
|
|
|
|
|
|
140
|
|
|
Color = imprecise_category('Color', ['R', 'G', 'B']) |
|
141
|
|
|
color1 = Color(['R']) |
|
142
|
|
|
color2 = Color(['R', 'G']) |
|
143
|
|
|
color3 = Color(['R', 'B']) |
|
144
|
|
|
self.assertTrue( |
|
145
|
|
|
color2 > color1, |
|
146
|
|
|
"['R', 'G'] is a strict superset of ['R']" |
|
147
|
|
|
) |
|
148
|
|
|
self.assertFalse( |
|
149
|
|
|
color1 > color1, |
|
150
|
|
|
"['R'] is not a strict superset of ['R']" |
|
151
|
|
|
) |
|
152
|
|
|
self.assertFalse( |
|
153
|
|
|
color2 > color3, |
|
154
|
|
|
"['R', 'G'] is not a strict superset of ['R', 'B']" |
|
155
|
|
|
) |
|
156
|
|
|
with self.assertRaises(TypeError): |
|
157
|
|
|
_ = color1 > 'dummy' |
|
158
|
|
|
|
|
159
|
|
|
def test___and__(self): |
|
160
|
|
|
Color = imprecise_category('Color', ['R', 'G', 'B']) |
|
161
|
|
|
color1 = Color(['R']) |
|
162
|
|
|
color2 = Color(['R', 'G']) |
|
163
|
|
|
color3 = Color(['R', 'B']) |
|
164
|
|
|
self.assertEqual( |
|
165
|
|
|
color1, |
|
166
|
|
|
color2 & color3, |
|
167
|
|
|
"['R', 'G'] & ['R', 'B'] = ['R']" |
|
168
|
|
|
) |
|
169
|
|
|
with self.assertRaises(TypeError): |
|
170
|
|
|
_ = color1 & 'dummy' |
|
171
|
|
|
|
|
172
|
|
|
def test___or__(self): |
|
173
|
|
|
Color = imprecise_category('Color', ['R', 'G', 'B']) |
|
174
|
|
|
color1 = Color(['R', 'G', 'B']) |
|
175
|
|
|
color2 = Color(['R', 'G']) |
|
176
|
|
|
color3 = Color(['R', 'B']) |
|
177
|
|
|
self.assertEqual( |
|
178
|
|
|
color1, |
|
179
|
|
|
color2 | color3, |
|
180
|
|
|
"['R', 'G'] | ['R', 'B'] = ['R', 'G', 'B']" |
|
181
|
|
|
) |
|
182
|
|
|
with self.assertRaises(TypeError): |
|
183
|
|
|
_ = color1 | 'dummy' |
|
184
|
|
|
|
|
185
|
|
|
def test___sub__(self): |
|
186
|
|
|
Color = imprecise_category('Color', ['R', 'G', 'B']) |
|
187
|
|
|
color1 = Color(['G']) |
|
188
|
|
|
color2 = Color(['R', 'G']) |
|
189
|
|
|
color3 = Color(['R', 'B']) |
|
190
|
|
|
self.assertEqual( |
|
191
|
|
|
color1, |
|
192
|
|
|
color2 - color3, |
|
193
|
|
|
"['R', 'G'] - ['R', 'B'] = ['B']" |
|
194
|
|
|
) |
|
195
|
|
|
with self.assertRaises(TypeError): |
|
196
|
|
|
_ = color1 - 'dummy' |
|
197
|
|
|
|
|
198
|
|
|
def test___xor__(self): |
|
199
|
|
|
Color = imprecise_category('Color', ['R', 'G', 'B']) |
|
200
|
|
|
color1 = Color(['G', 'B']) |
|
201
|
|
|
color2 = Color(['R', 'G']) |
|
202
|
|
|
color3 = Color(['R', 'B']) |
|
203
|
|
|
self.assertEqual( |
|
204
|
|
|
color1, |
|
205
|
|
|
color2 ^ color3, |
|
206
|
|
|
"['R', 'G'] - ['R', 'B'] = ['G', 'B']" |
|
207
|
|
|
) |
|
208
|
|
|
with self.assertRaises(TypeError): |
|
209
|
|
|
_ = color1 ^ 'dummy' |
|
210
|
|
|
|
|
211
|
|
View Code Duplication |
def test_isdisjoint(self): |
|
|
|
|
|
|
212
|
|
|
Color = imprecise_category('Color', ['R', 'G', 'B']) |
|
213
|
|
|
color1 = Color(['B']) |
|
214
|
|
|
color2 = Color(['R', 'G']) |
|
215
|
|
|
color3 = Color(['R', 'B']) |
|
216
|
|
|
self.assertTrue( |
|
217
|
|
|
color1.isdisjoint(color2), |
|
218
|
|
|
"bool(['B'] & ['R', 'G']) = True" |
|
219
|
|
|
) |
|
220
|
|
|
self.assertFalse( |
|
221
|
|
|
color1.isdisjoint(color3), |
|
222
|
|
|
"bool(['B'] & ['R', 'B']) = False" |
|
223
|
|
|
) |
|
224
|
|
|
self.assertTrue( |
|
225
|
|
|
color1.isdisjoint(['R', 'G']), |
|
226
|
|
|
"bool(['B'] & ['R', 'G']) = True" |
|
227
|
|
|
) |
|
228
|
|
|
with self.assertRaises(TypeError): |
|
229
|
|
|
_ = color1.isdisjoint(125) |
|
230
|
|
|
|
|
231
|
|
View Code Duplication |
def test_issubset(self): |
|
|
|
|
|
|
232
|
|
|
Color = imprecise_category('Color', ['R', 'G', 'B']) |
|
233
|
|
|
color1 = Color(['B']) |
|
234
|
|
|
color2 = Color(['R', 'G']) |
|
235
|
|
|
color3 = Color(['R', 'B']) |
|
236
|
|
|
self.assertFalse( |
|
237
|
|
|
color1.issubset(color2), |
|
238
|
|
|
"['B'] is not a subset of ['R', 'G']" |
|
239
|
|
|
) |
|
240
|
|
|
self.assertTrue( |
|
241
|
|
|
color1.issubset(color3), |
|
242
|
|
|
"['B'] is a subset of ['R', 'B']" |
|
243
|
|
|
) |
|
244
|
|
|
self.assertTrue( |
|
245
|
|
|
color1.issubset(['B', 'G']), |
|
246
|
|
|
"['B'] is a subset of ['B', 'G']" |
|
247
|
|
|
) |
|
248
|
|
|
with self.assertRaises(TypeError): |
|
249
|
|
|
_ = color1.issubset(125) |
|
250
|
|
|
|
|
251
|
|
View Code Duplication |
def test_issuperset(self): |
|
|
|
|
|
|
252
|
|
|
Color = imprecise_category('Color', ['R', 'G', 'B']) |
|
253
|
|
|
color1 = Color(['B']) |
|
254
|
|
|
color2 = Color(['R', 'G']) |
|
255
|
|
|
color3 = Color(['R', 'B']) |
|
256
|
|
|
self.assertFalse( |
|
257
|
|
|
color1.issuperset(color2), |
|
258
|
|
|
"['B'] is not a superset of ['R', 'G']" |
|
259
|
|
|
) |
|
260
|
|
|
self.assertTrue( |
|
261
|
|
|
color3.issuperset(color1), |
|
262
|
|
|
"['R', 'B'] is a superset of ['B']" |
|
263
|
|
|
) |
|
264
|
|
|
self.assertTrue( |
|
265
|
|
|
color3.issuperset(['R']), |
|
266
|
|
|
"['R', 'B'] is a superset of ['R']" |
|
267
|
|
|
) |
|
268
|
|
|
with self.assertRaises(TypeError): |
|
269
|
|
|
_ = color1.issuperset(125) |
|
270
|
|
|
|
|
271
|
|
|
def test_union(self): |
|
272
|
|
|
Color = imprecise_category('Color', ['R', 'G', 'B']) |
|
273
|
|
|
color1 = Color(['B', 'R']) |
|
274
|
|
|
color2 = Color() |
|
275
|
|
|
color3 = Color(['B']) |
|
276
|
|
|
self.assertEqual( |
|
277
|
|
|
color3.union(color2, ['R']), |
|
278
|
|
|
color1, |
|
279
|
|
|
"['B'] | [] | ['R'] = ['B', 'R']" |
|
280
|
|
|
) |
|
281
|
|
|
|
|
282
|
|
|
def test_intersection(self): |
|
283
|
|
|
Color = imprecise_category('Color', ['R', 'G', 'B']) |
|
284
|
|
|
color1 = Color(['B']) |
|
285
|
|
|
color2 = Color(['B', 'R']) |
|
286
|
|
|
color3 = Color(['R', 'G', 'B']) |
|
287
|
|
|
self.assertEqual( |
|
288
|
|
|
color3.intersection(color2, ['B']), |
|
289
|
|
|
color1, |
|
290
|
|
|
"['R', 'G', 'B'] & ['B', 'R'] & ['B'] = ['B']" |
|
291
|
|
|
) |
|
292
|
|
|
|
|
293
|
|
|
def test_difference(self): |
|
294
|
|
|
Color = imprecise_category('Color', ['R', 'G', 'B']) |
|
295
|
|
|
color1 = Color(['G']) |
|
296
|
|
|
color2 = Color(['B', 'R']) |
|
297
|
|
|
color3 = Color(['R', 'G', 'B']) |
|
298
|
|
|
self.assertEqual( |
|
299
|
|
|
color3.difference(color2, ['B']), |
|
300
|
|
|
color1, |
|
301
|
|
|
"['R', 'G', 'B'] - ['B', 'R'] - ['B'] = ['G']" |
|
302
|
|
|
) |
|
303
|
|
|
|
|
304
|
|
|
def test_symmetric_difference(self): |
|
305
|
|
|
Color = imprecise_category('Color', ['R', 'G', 'B']) |
|
306
|
|
|
color1 = Color(['B', 'G']) |
|
307
|
|
|
color2 = Color(['B', 'R']) |
|
308
|
|
|
color3 = Color(['R', 'G']) |
|
309
|
|
|
self.assertEqual( |
|
310
|
|
|
color3.symmetric_difference(color2), |
|
311
|
|
|
color1, |
|
312
|
|
|
"['R', 'G'] ^ ['B', 'R'] = ['B', 'G']" |
|
313
|
|
|
) |
|
314
|
|
|
self.assertEqual( |
|
315
|
|
|
color3.symmetric_difference(['B', 'G']), |
|
316
|
|
|
Color(['R', 'B']), |
|
317
|
|
|
"['R', 'G'] ^ ['B', 'G'] = ['B', 'R]" |
|
318
|
|
|
) |
|
319
|
|
|
with self.assertRaises(TypeError): |
|
320
|
|
|
_ = color1.symmetric_difference(125) |
|
321
|
|
|
|
|
322
|
|
|
def test___repr__(self): |
|
323
|
|
|
Color = imprecise_category('Color', ['R', 'G', 'B']) |
|
324
|
|
|
color = Color(['B', 'G']) |
|
325
|
|
|
self.assertEqual( |
|
326
|
|
|
repr(color), |
|
327
|
|
|
"Color({'G', 'B'})", |
|
328
|
|
|
"repr(Color(['B', 'G'])) = Color({'G', 'B'})" |
|
329
|
|
|
) |
|
330
|
|
|
|
|
331
|
|
|
class CategoryTest(TestCase): |
|
332
|
|
|
def test_category(self): |
|
333
|
|
|
Color = category('Color', ['R', 'G', 'B']) |
|
334
|
|
|
self.assertEqual( |
|
335
|
|
|
str(Color), |
|
336
|
|
|
"<class 'test_category.Color'>", |
|
337
|
|
|
"The string representation of the class is not correct" |
|
338
|
|
|
) |
|
339
|
|
|
Color = category('Color', ['R', 'G', 'B'], module='mymodule') |
|
340
|
|
|
self.assertEqual( |
|
341
|
|
|
str(Color), |
|
342
|
|
|
"<class 'mymodule.Color'>", |
|
343
|
|
|
"The string representation of the class is not correct" |
|
344
|
|
|
) |
|
345
|
|
|
with self.assertRaises(ValueError): |
|
346
|
|
|
_ = category('Color') |
|
347
|
|
|
|
|
348
|
|
|
def test___new__(self): |
|
349
|
|
|
Color = category('Color', ['R', 'G', 'B']) |
|
350
|
|
|
self.assertEqual( |
|
351
|
|
|
str(Color()), |
|
352
|
|
|
"R", |
|
353
|
|
|
"The string representation of the instance is not correct" |
|
354
|
|
|
) |
|
355
|
|
|
self.assertEqual( |
|
356
|
|
|
str(Color('B')), |
|
357
|
|
|
"B", |
|
358
|
|
|
"The string representation of the instance is not correct" |
|
359
|
|
|
) |
|
360
|
|
|
with self.assertRaises(ValueError): |
|
361
|
|
|
_ = Color('Y') |
|
362
|
|
|
|
|
363
|
|
|
def test___repr__(self): |
|
364
|
|
|
Color = category('Color', ['R', 'G', 'B']) |
|
365
|
|
|
self.assertEqual( |
|
366
|
|
|
repr(Color()), |
|
367
|
|
|
"Color('R')", |
|
368
|
|
|
"The representation of the instance is not correct" |
|
369
|
|
|
) |
|
370
|
|
|
|