-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathtest_flags.py
515 lines (377 loc) · 14.5 KB
/
test_flags.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
# SPDX-License-Identifier: MIT
from __future__ import annotations
import re
import pytest
from disnake import flags
class TestFlags(flags.BaseFlags):
"""A test class for flag testing."""
__test__ = False
@flags.flag_value
def one(self):
return 1 << 0
@flags.flag_value
def two(self):
return 1 << 1
@flags.flag_value
def four(self):
return 1 << 2
@flags.alias_flag_value
def three(self):
return 1 << 0 | 1 << 1
@flags.flag_value
def sixteen(self):
return 1 << 4
class OtherTestFlags(flags.BaseFlags):
"""Another test class for flag testing."""
@flags.flag_value
def other_one(self):
return 1 << 0
def test_all_flags_value() -> None:
assert flags.all_flags_value(TestFlags.VALID_FLAGS) == 0b10111
def test_flag_creation() -> None:
assert TestFlags.VALID_FLAGS == {
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"sixteen": 16,
}
assert TestFlags.DEFAULT_VALUE == 0
def test_flag_creation_inverted() -> None:
class InvertedFlags(flags.BaseFlags, inverted=True):
@flags.flag_value
def one(self):
return 1 << 0
@flags.flag_value
def two(self):
return 1 << 1
@flags.flag_value
def four(self):
return 1 << 2
@flags.alias_flag_value
def three(self):
return 1 << 0 | 1 << 1
@flags.flag_value
def sixteen(self):
return 1 << 4
assert InvertedFlags.VALID_FLAGS == {
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"sixteen": 16,
}
assert InvertedFlags.DEFAULT_VALUE == 0b10111
def test_flag_creation_empty() -> None:
with pytest.raises(RuntimeError, match="At least one flag must be defined"):
class InvertedFlags(flags.BaseFlags):
not_a_flag = 2
class TestFlagValue:
def test_flag_value_creation(self) -> None:
flag = flags.flag_value(lambda x: 1 << 2)
assert 1 << 2 == flag.flag
def test_flag_value_or(self) -> None:
ins = TestFlags.four | TestFlags.one
assert isinstance(ins, TestFlags)
assert ins.value == 5
assert (TestFlags.two | ins).value == 7
assert not ins.value & TestFlags.sixteen.flag
ins |= TestFlags.sixteen
assert ins.value == 21
ins = TestFlags()
assert ins.value == 0
assert (ins | TestFlags.one).value == 1
with pytest.raises(TypeError, match=re.escape("unsupported operand type(s) for |:")):
_ = TestFlags.four | 32 # type: ignore
with pytest.raises(TypeError, match=re.escape("unsupported operand type(s) for |:")):
_ = 32 | TestFlags.four # type: ignore
with pytest.raises(TypeError, match=re.escape("unsupported operand type(s) for |:")):
_ = TestFlags.four | OtherTestFlags.other_one # type: ignore
with pytest.raises(TypeError, match=re.escape("unsupported operand type(s) for |:")):
_ = TestFlags.four | OtherTestFlags(other_one=True) # type: ignore
def test_flag_value_invert(self) -> None:
ins = ~TestFlags.four
assert isinstance(ins, TestFlags)
assert ins.value == 23 - 4
def test_flag_value_xor(self) -> None:
ins = TestFlags(one=True, two=True)
ins ^= TestFlags.two
assert ins.value == 1
ins ^= TestFlags.three
assert ins.value == 2
assert (ins ^ TestFlags.four).value == 6
class TestBaseFlags:
def test__init__default_value(self) -> None:
ins = TestFlags()
assert ins.DEFAULT_VALUE is ins.value
def test__init__kwargs(self) -> None:
ins = TestFlags(one=True, two=False)
assert ins.one is True
assert ins.two is False
def test__init__invalid_kwargs(self) -> None:
with pytest.raises(TypeError, match="'h' is not a valid flag name."):
TestFlags(h=True)
def test_set_require_bool(self) -> None:
with pytest.raises(TypeError, match="Value to set for TestFlags must be a bool."):
TestFlags(one="h") # type: ignore
ins = TestFlags()
with pytest.raises(TypeError, match="Value to set for TestFlags must be a bool."):
ins.two = "h" # type: ignore
def test__eq__(self) -> None:
ins = TestFlags(one=True, two=True)
other = TestFlags(one=True, two=True)
assert ins is not other
assert ins == other
assert not ins != other
ins.two = False
assert not ins == other
assert ins != other
def test__eq__flag_value(self) -> None:
ins = TestFlags(one=True)
other = TestFlags(one=True, two=True)
assert ins == TestFlags.one
assert TestFlags.one == ins
assert not ins != TestFlags.one
assert ins != TestFlags.two
assert other != TestFlags.one
assert other != TestFlags.two
assert other == TestFlags.three
def test__and__(self) -> None:
ins = TestFlags(one=True, two=True)
other = TestFlags(one=True, two=True)
third = ins & other
assert third is not ins
assert third.value == 0b011
assert third == ins == other
ins.one = False
third = ins & other
assert third is not ins
assert third.value == 0b010
with pytest.raises(TypeError, match=re.escape("unsupported operand type(s) for &:")):
_ = ins & "44" # type: ignore
with pytest.raises(TypeError, match=re.escape("unsupported operand type(s) for &:")):
_ = "44" & ins # type: ignore
def test__iand__(self) -> None:
ins = TestFlags(one=True, two=True)
other = TestFlags(one=True, two=True)
third = ins
ins &= other
assert third is ins
assert ins.value == 0b011
other.two = False
other.four = True
ins &= other
assert third is ins
assert ins.value == 0b001
with pytest.raises(TypeError, match=re.escape("unsupported operand type(s) for &=:")):
ins &= 14 # type: ignore
def test__or__(self) -> None:
ins = TestFlags(one=True, two=False)
other = TestFlags(one=False, two=True)
third = ins | other
assert third is not ins
assert ins.value == 0b001
assert other.value == 0b010
assert third.value == 0b011
ins.one = False
third = ins | other
assert third.value == 0b010
assert third is not ins
ins.value = other.value
third = ins | other
assert third is not ins
assert third.value == 0b10
with pytest.raises(TypeError, match=re.escape("unsupported operand type(s) for |:")):
_ = ins | 28 # type: ignore
with pytest.raises(TypeError, match=re.escape("unsupported operand type(s) for |:")):
_ = 28 | ins # type: ignore
with pytest.raises(TypeError, match=re.escape("unsupported operand type(s) for |:")):
_ = ins | OtherTestFlags.other_one # type: ignore
def test__ior__(self) -> None:
ins = TestFlags(one=True, two=False)
other = TestFlags(one=False, two=True)
third = ins
ins |= other
assert ins is third
assert ins.value == 0b011
assert other.value == 0b010
other.four = True
ins |= other
assert ins.value == 0b111
with pytest.raises(TypeError, match=re.escape("unsupported operand type(s) for |=:")):
ins |= True # type: ignore
with pytest.raises(TypeError, match=re.escape("unsupported operand type(s) for |=:")):
ins |= OtherTestFlags.other_one # type: ignore
def test__xor__(self) -> None:
ins = TestFlags(one=True, two=False)
other = TestFlags(one=False, two=True)
third = ins ^ other
assert third.value == 0b011
assert third is not ins
other.one = True
third = ins ^ other
assert third.value == 0b010
with pytest.raises(TypeError, match=re.escape("unsupported operand type(s) for ^:")):
_ = ins ^ "h" # type: ignore
with pytest.raises(TypeError, match=re.escape("unsupported operand type(s) for ^:")):
_ = "h" ^ ins # type: ignore
with pytest.raises(TypeError, match=re.escape("unsupported operand type(s) for ^:")):
_ = ins ^ OtherTestFlags.other_one # type: ignore
def test__ixor__(self) -> None:
ins = TestFlags(one=True, two=False)
other = TestFlags(one=False, two=True)
third = ins
ins ^= other
assert ins is third
assert ins.value == 0b011
ins.two = False
other.one = True
ins ^= other
assert ins.value == 0b010
with pytest.raises(TypeError, match=re.escape("unsupported operand type(s) for ^=:")):
ins ^= "stability" # type: ignore
with pytest.raises(TypeError, match=re.escape("unsupported operand type(s) for ^=:")):
ins ^= OtherTestFlags.other_one # type: ignore
def test__le__(self) -> None:
ins = TestFlags(one=True, two=False)
other = TestFlags(one=False, two=True)
assert not ins <= other
other.one = True
assert ins <= other
with pytest.raises(
TypeError, match="'<=' not supported between instances of 'TestFlags' and 'int'"
):
_ = ins <= 4 # type: ignore
other.value = ins.value
assert ins <= other
def test__ge__(self) -> None:
ins = TestFlags(one=True, two=False)
other = TestFlags(one=False, two=True)
assert not ins >= other
ins.two = True
assert ins >= other
with pytest.raises(
TypeError, match="'>=' not supported between instances of 'TestFlags' and 'int'"
):
_ = ins >= 4 # type: ignore
other.value = ins.value
assert ins >= other
def test__lt__(self) -> None:
ins = TestFlags(one=True, two=False)
other = TestFlags(one=False, two=True)
assert not ins < other
other.one = True
assert ins < other
with pytest.raises(
TypeError, match="'<' not supported between instances of 'TestFlags' and 'int'"
):
_ = ins < 4 # type: ignore
other.value = ins.value
assert not ins < other
def test__gt__(self) -> None:
ins = TestFlags(one=True, two=False)
other = TestFlags(one=False, two=True)
assert not ins > other
ins.two = True
assert ins > other
with pytest.raises(
TypeError, match="'>' not supported between instances of 'TestFlags' and 'int'"
):
_ = ins > 4 # type: ignore
other.value = ins.value
assert not ins > other
def test__invert__(self) -> None:
ins = TestFlags(one=True)
assert ins.value == 0b0001
other = ~ins
# assert that invert does not modify anything in-place
assert ins.value == 0b0001
# the other `0` here is because invert does not invert values that are not defined
# assert other.value == goal
assert other.value == 0b10110
def test__hash__(self) -> None:
ins = TestFlags(one=True)
assert hash(ins) == hash(ins.value)
def test_iter(self) -> None:
ins = TestFlags(one=True, two=False)
assert len(list(iter(ins))) == 4
for flag, value in iter(ins):
assert flag in ins.VALID_FLAGS
assert getattr(ins, flag) == value
def test_from_value(self) -> None:
ins = TestFlags._from_value(0b101)
assert ins.value == 0b101
def test_set_and_get_flag(self) -> None:
ins = TestFlags()
assert ins.DEFAULT_VALUE == ins.value
ins.two = True
assert ins.two is True
assert ins.value == TestFlags.two.flag == 1 << 1
def test_alias_flag_value(self) -> None:
ins = TestFlags(three=True)
assert ins.value == 0b11
ins.three = False
assert ins.value == 0
def test_alias_priority(self) -> None:
ins = TestFlags(three=False, two=True, one=False)
assert ins.value == 0b10
ins = TestFlags(three=True, two=False, one=False)
assert ins.value == 0b00
ins = TestFlags(three=True, two=False)
assert ins.value == 0b01
ins = TestFlags(two=False, three=True)
assert ins.value == 0b11
ins = TestFlags(two=True, three=False)
assert ins.value == 0b00
class _ListFlags(flags.ListBaseFlags):
@flags.flag_value
def flag1(self):
return 1 << 0
@flags.flag_value
def flag2(self):
return 1 << 1
@flags.flag_value
def flag3(self):
return 1 << 2
class TestListBaseFlags:
@pytest.mark.parametrize(
("kwargs", "expected_value", "expected_values"),
[
({}, 0, []),
({"flag2": True}, 2, [1]),
({"flag3": True, "flag2": False, "flag1": True}, 5, [0, 2]),
],
)
def test_init(self, kwargs, expected_value, expected_values) -> None:
flags = _ListFlags(**kwargs)
assert flags.value == expected_value
assert flags.values == expected_values
def test_class_vars(self) -> None:
assert _ListFlags.DEFAULT_VALUE == 0
assert _ListFlags.VALID_FLAGS == {"flag1": 1, "flag2": 2, "flag3": 4}
def test_from_values(self) -> None:
flags = _ListFlags._from_values([0, 2, 3, 4, 5, 10])
assert flags.value == 1085
assert flags.values == [0, 2, 3, 4, 5, 10]
@pytest.mark.parametrize("value", [-10, -1, 100])
def test_from_values__invalid(self, value) -> None:
with pytest.raises(ValueError, match=re.escape("Flag values must be within [0, 64)")):
_ListFlags._from_values([0, 2, value, 3])
def test_update(self) -> None:
flags = _ListFlags(flag2=True)
flags.flag2 = False
flags.flag3 = True
assert flags.value == 4
assert flags.values == [2]
def test_operators(self) -> None:
a = _ListFlags(flag1=True, flag3=True)
b = _ListFlags(flag1=False, flag3=True)
assert a.values == [0, 2]
assert b.values == [2]
assert (~(a & b)).values == [0, 1]
class TestIntents:
def test_all_only_valid(self) -> None:
"""Test that Intents.all() doesn't include flags that aren't defined."""
intents = flags.Intents.all()
assert not (1 << 18 | 1 << 17) & intents.value
assert 1 << 20 & intents.value