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
|
|
|
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
|
|
|
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
|
|
|
self.assertFalse( |
128
|
|
|
color1 == color2, |
129
|
|
|
"['R'] is not equal to ['R', 'G']" |
130
|
|
|
) |
131
|
|
|
self.assertTrue( |
132
|
|
|
color1 == color1, |
133
|
|
|
"['R'] is equal to ['R']" |
134
|
|
|
) |
135
|
|
|
with self.assertRaises(TypeError): |
136
|
|
|
_ = color1 == 'dummy' |
137
|
|
|
|
138
|
|
|
def test___gt__(self): |
139
|
|
|
Color = imprecise_category('Color', ['R', 'G', 'B']) |
140
|
|
|
color1 = Color(['R']) |
141
|
|
|
color2 = Color(['R', 'G']) |
142
|
|
|
color3 = Color(['R', 'B']) |
143
|
|
|
self.assertTrue( |
144
|
|
|
color2 > color1, |
145
|
|
|
"['R', 'G'] is a strict superset of ['R']" |
146
|
|
|
) |
147
|
|
|
self.assertFalse( |
148
|
|
|
color1 > color1, |
149
|
|
|
"['R'] is not a strict superset of ['R']" |
150
|
|
|
) |
151
|
|
|
self.assertFalse( |
152
|
|
|
color2 > color3, |
153
|
|
|
"['R', 'G'] is not a strict superset of ['R', 'B']" |
154
|
|
|
) |
155
|
|
|
with self.assertRaises(TypeError): |
156
|
|
|
_ = color1 > 'dummy' |
157
|
|
|
|
158
|
|
|
def test___and__(self): |
159
|
|
|
Color = imprecise_category('Color', ['R', 'G', 'B']) |
160
|
|
|
color1 = Color(['R']) |
161
|
|
|
color2 = Color(['R', 'G']) |
162
|
|
|
color3 = Color(['R', 'B']) |
163
|
|
|
self.assertEqual( |
164
|
|
|
color1, |
165
|
|
|
color2 & color3, |
166
|
|
|
"['R', 'G'] & ['R', 'B'] = ['R']" |
167
|
|
|
) |
168
|
|
|
with self.assertRaises(TypeError): |
169
|
|
|
_ = color1 & 'dummy' |
170
|
|
|
|
171
|
|
|
def test___or__(self): |
172
|
|
|
Color = imprecise_category('Color', ['R', 'G', 'B']) |
173
|
|
|
color1 = Color(['R', 'G', 'B']) |
174
|
|
|
color2 = Color(['R', 'G']) |
175
|
|
|
color3 = Color(['R', 'B']) |
176
|
|
|
self.assertEqual( |
177
|
|
|
color1, |
178
|
|
|
color2 | color3, |
179
|
|
|
"['R', 'G'] | ['R', 'B'] = ['R', 'G', 'B']" |
180
|
|
|
) |
181
|
|
|
with self.assertRaises(TypeError): |
182
|
|
|
_ = color1 | 'dummy' |
183
|
|
|
|
184
|
|
|
def test___sub__(self): |
185
|
|
|
Color = imprecise_category('Color', ['R', 'G', 'B']) |
186
|
|
|
color1 = Color(['G']) |
187
|
|
|
color2 = Color(['R', 'G']) |
188
|
|
|
color3 = Color(['R', 'B']) |
189
|
|
|
self.assertEqual( |
190
|
|
|
color1, |
191
|
|
|
color2 - color3, |
192
|
|
|
"['R', 'G'] - ['R', 'B'] = ['B']" |
193
|
|
|
) |
194
|
|
|
with self.assertRaises(TypeError): |
195
|
|
|
_ = color1 - 'dummy' |
196
|
|
|
|
197
|
|
|
def test___xor__(self): |
198
|
|
|
Color = imprecise_category('Color', ['R', 'G', 'B']) |
199
|
|
|
color1 = Color(['G', 'B']) |
200
|
|
|
color2 = Color(['R', 'G']) |
201
|
|
|
color3 = Color(['R', 'B']) |
202
|
|
|
self.assertEqual( |
203
|
|
|
color1, |
204
|
|
|
color2 ^ color3, |
205
|
|
|
"['R', 'G'] - ['R', 'B'] = ['G', 'B']" |
206
|
|
|
) |
207
|
|
|
with self.assertRaises(TypeError): |
208
|
|
|
_ = color1 ^ 'dummy' |
209
|
|
|
|
210
|
|
|
def test_isdisjoint(self): |
211
|
|
View Code Duplication |
Color = imprecise_category('Color', ['R', 'G', 'B']) |
|
|
|
|
212
|
|
|
color1 = Color(['B']) |
213
|
|
|
color2 = Color(['R', 'G']) |
214
|
|
|
color3 = Color(['R', 'B']) |
215
|
|
|
self.assertTrue( |
216
|
|
|
color1.isdisjoint(color2), |
217
|
|
|
"bool(['B'] & ['R', 'G']) = True" |
218
|
|
|
) |
219
|
|
|
self.assertFalse( |
220
|
|
|
color1.isdisjoint(color3), |
221
|
|
|
"bool(['B'] & ['R', 'B']) = False" |
222
|
|
|
) |
223
|
|
|
self.assertTrue( |
224
|
|
|
color1.isdisjoint(['R', 'G']), |
225
|
|
|
"bool(['B'] & ['R', 'G']) = True" |
226
|
|
|
) |
227
|
|
|
with self.assertRaises(TypeError): |
228
|
|
|
_ = color1.isdisjoint(125) |
229
|
|
|
|
230
|
|
|
def test_issubset(self): |
231
|
|
View Code Duplication |
Color = imprecise_category('Color', ['R', 'G', 'B']) |
|
|
|
|
232
|
|
|
color1 = Color(['B']) |
233
|
|
|
color2 = Color(['R', 'G']) |
234
|
|
|
color3 = Color(['R', 'B']) |
235
|
|
|
self.assertFalse( |
236
|
|
|
color1.issubset(color2), |
237
|
|
|
"['B'] is not a subset of ['R', 'G']" |
238
|
|
|
) |
239
|
|
|
self.assertTrue( |
240
|
|
|
color1.issubset(color3), |
241
|
|
|
"['B'] is a subset of ['R', 'B']" |
242
|
|
|
) |
243
|
|
|
self.assertTrue( |
244
|
|
|
color1.issubset(['B', 'G']), |
245
|
|
|
"['B'] is a subset of ['B', 'G']" |
246
|
|
|
) |
247
|
|
|
with self.assertRaises(TypeError): |
248
|
|
|
_ = color1.issubset(125) |
249
|
|
|
|
250
|
|
|
def test_issuperset(self): |
251
|
|
View Code Duplication |
Color = imprecise_category('Color', ['R', 'G', 'B']) |
|
|
|
|
252
|
|
|
color1 = Color(['B']) |
253
|
|
|
color2 = Color(['R', 'G']) |
254
|
|
|
color3 = Color(['R', 'B']) |
255
|
|
|
self.assertFalse( |
256
|
|
|
color1.issuperset(color2), |
257
|
|
|
"['B'] is not a superset of ['R', 'G']" |
258
|
|
|
) |
259
|
|
|
self.assertTrue( |
260
|
|
|
color3.issuperset(color1), |
261
|
|
|
"['R', 'B'] is a superset of ['B']" |
262
|
|
|
) |
263
|
|
|
self.assertTrue( |
264
|
|
|
color3.issuperset(['R']), |
265
|
|
|
"['R', 'B'] is a superset of ['R']" |
266
|
|
|
) |
267
|
|
|
with self.assertRaises(TypeError): |
268
|
|
|
_ = color1.issuperset(125) |
269
|
|
|
|
270
|
|
|
def test_union(self): |
271
|
|
|
Color = imprecise_category('Color', ['R', 'G', 'B']) |
272
|
|
|
color1 = Color(['B', 'R']) |
273
|
|
|
color2 = Color() |
274
|
|
|
color3 = Color(['B']) |
275
|
|
|
self.assertEqual( |
276
|
|
|
color3.union(color2, ['R']), |
277
|
|
|
color1, |
278
|
|
|
"['B'] | [] | ['R'] = ['B', 'R']" |
279
|
|
|
) |
280
|
|
|
|
281
|
|
|
def test_intersection(self): |
282
|
|
|
Color = imprecise_category('Color', ['R', 'G', 'B']) |
283
|
|
|
color1 = Color(['B']) |
284
|
|
|
color2 = Color(['B', 'R']) |
285
|
|
|
color3 = Color(['R', 'G', 'B']) |
286
|
|
|
self.assertEqual( |
287
|
|
|
color3.intersection(color2, ['B']), |
288
|
|
|
color1, |
289
|
|
|
"['R', 'G', 'B'] & ['B', 'R'] & ['B'] = ['B']" |
290
|
|
|
) |
291
|
|
|
|
292
|
|
|
def test_difference(self): |
293
|
|
|
Color = imprecise_category('Color', ['R', 'G', 'B']) |
294
|
|
|
color1 = Color(['G']) |
295
|
|
|
color2 = Color(['B', 'R']) |
296
|
|
|
color3 = Color(['R', 'G', 'B']) |
297
|
|
|
self.assertEqual( |
298
|
|
|
color3.difference(color2, ['B']), |
299
|
|
|
color1, |
300
|
|
|
"['R', 'G', 'B'] - ['B', 'R'] - ['B'] = ['G']" |
301
|
|
|
) |
302
|
|
|
|
303
|
|
|
def test_symmetric_difference(self): |
304
|
|
|
Color = imprecise_category('Color', ['R', 'G', 'B']) |
305
|
|
|
color1 = Color(['B', 'G']) |
306
|
|
|
color2 = Color(['B', 'R']) |
307
|
|
|
color3 = Color(['R', 'G']) |
308
|
|
|
self.assertEqual( |
309
|
|
|
color3.symmetric_difference(color2), |
310
|
|
|
color1, |
311
|
|
|
"['R', 'G'] ^ ['B', 'R'] = ['B', 'G']" |
312
|
|
|
) |
313
|
|
|
self.assertEqual( |
314
|
|
|
color3.symmetric_difference(['B', 'G']), |
315
|
|
|
Color(['R', 'B']), |
316
|
|
|
"['R', 'G'] ^ ['B', 'G'] = ['B', 'R]" |
317
|
|
|
) |
318
|
|
|
with self.assertRaises(TypeError): |
319
|
|
|
_ = color1.symmetric_difference(125) |
320
|
|
|
|
321
|
|
|
def test___repr__(self): |
322
|
|
|
Color = imprecise_category('Color', ['R', 'G', 'B']) |
323
|
|
|
color = Color(['B', 'G']) |
324
|
|
|
self.assertEqual( |
325
|
|
|
repr(color), |
326
|
|
|
"Color({'G', 'B'})", |
327
|
|
|
"repr(Color(['B', 'G'])) = Color({'G', 'B'})" |
328
|
|
|
) |
329
|
|
|
|
330
|
|
|
class CategoryTest(TestCase): |
331
|
|
|
def test_category(self): |
332
|
|
|
Color = category('Color', ['R', 'G', 'B']) |
333
|
|
|
self.assertEqual( |
334
|
|
|
str(Color), |
335
|
|
|
"<class 'test_category.Color'>", |
336
|
|
|
"The string representation of the class is not correct" |
337
|
|
|
) |
338
|
|
|
Color = category('Color', ['R', 'G', 'B'], module='mymodule') |
339
|
|
|
self.assertEqual( |
340
|
|
|
str(Color), |
341
|
|
|
"<class 'mymodule.Color'>", |
342
|
|
|
"The string representation of the class is not correct" |
343
|
|
|
) |
344
|
|
|
with self.assertRaises(ValueError): |
345
|
|
|
_ = category('Color') |
346
|
|
|
|
347
|
|
|
def test___new__(self): |
348
|
|
|
Color = category('Color', ['R', 'G', 'B']) |
349
|
|
|
self.assertEqual( |
350
|
|
|
str(Color()), |
351
|
|
|
"R", |
352
|
|
|
"The string representation of the instance is not correct" |
353
|
|
|
) |
354
|
|
|
self.assertEqual( |
355
|
|
|
str(Color('B')), |
356
|
|
|
"B", |
357
|
|
|
"The string representation of the instance is not correct" |
358
|
|
|
) |
359
|
|
|
with self.assertRaises(ValueError): |
360
|
|
|
_ = Color('Y') |
361
|
|
|
|
362
|
|
|
def test___repr__(self): |
363
|
|
|
Color = category('Color', ['R', 'G', 'B']) |
364
|
|
|
self.assertEqual( |
365
|
|
|
repr(Color()), |
366
|
|
|
"Color('R')", |
367
|
|
|
"The representation of the instance is not correct" |
368
|
|
|
) |
369
|
|
|
|