|
1
|
|
|
import importlib |
|
2
|
|
|
import typing |
|
3
|
|
|
|
|
4
|
|
|
import pytest |
|
5
|
|
|
|
|
6
|
|
|
from . import enum_options |
|
7
|
|
|
|
|
8
|
|
|
T = typing.TypeVar('T') |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
@pytest.fixture(scope='session', params=['current', 'future']) |
|
12
|
|
|
def enum_module(request): |
|
13
|
|
|
return importlib.import_module( |
|
14
|
|
|
f'.enum_with_{request.param}', __name__.rpartition('.')[0]) |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
def test_generic_subclass_succeeds(enum): |
|
18
|
|
|
|
|
19
|
|
|
@enum.enum |
|
20
|
|
|
class TestClass(typing.Generic[T]): |
|
21
|
|
|
Variant: enum.Ctor[()] |
|
22
|
|
|
|
|
23
|
|
|
assert TestClass.Variant() |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
def test_ctor_usable_as_empty(enum): |
|
27
|
|
|
assert enum.Ctor is enum.Ctor[()] |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
def test_ctor_converts_to_tuple(enum): |
|
31
|
|
|
assert enum.Ctor[(list,)] is enum.Ctor[list] |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
def test_ctor_controls_subclass_creation(enum): |
|
35
|
|
|
with pytest.raises(TypeError): |
|
36
|
|
|
class CantMake(enum.Ctor): |
|
37
|
|
|
pass |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
def test_ctor_cant_index_twice(enum): |
|
41
|
|
|
with pytest.raises(TypeError): |
|
42
|
|
|
assert not enum.Ctor[list][list] |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
def test_enum_class(enum_module): |
|
46
|
|
|
for enum_class in enum_module.TEST_CLASSES: |
|
47
|
|
|
assert enum_class |
|
48
|
|
|
|
|
49
|
|
|
annotations_left = typing.get_type_hints( |
|
50
|
|
|
enum_class.Left.__new__, vars(enum_module)) |
|
51
|
|
|
expected_annotations_left = {'return': enum_class} |
|
52
|
|
|
for (index, typ) in enumerate(enum_class.left_type): |
|
53
|
|
|
expected_annotations_left[f'_{index}'] = typ |
|
54
|
|
|
assert annotations_left == expected_annotations_left |
|
55
|
|
|
|
|
56
|
|
|
annotations_right = typing.get_type_hints( |
|
57
|
|
|
enum_class.Right.__new__, vars(enum_module)) |
|
58
|
|
|
expected_annotations_right = {'return': enum_class} |
|
59
|
|
|
for (index, typ) in enumerate(enum_class.right_type): |
|
60
|
|
|
expected_annotations_right[f'_{index}'] = typ |
|
61
|
|
|
assert annotations_right == expected_annotations_right |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
def test_valid_eq(option_class): |
|
65
|
|
|
if option_class.eq: |
|
66
|
|
|
assert option_class.Left(1) == option_class.Left(1) |
|
67
|
|
|
assert option_class.Right('abc') == option_class.Right('abc') |
|
68
|
|
|
assert option_class.Left(1) != option_class.Right('abc') |
|
69
|
|
|
# This next one is invalid type-wise. |
|
70
|
|
|
assert option_class.Left(1) != option_class.Right(1) |
|
71
|
|
|
assert option_class.Left(1) != option_class.Left(2) |
|
72
|
|
|
assert hash(option_class.Left(1)) |
|
73
|
|
|
else: |
|
74
|
|
|
instance = option_class.Left(1) |
|
75
|
|
|
assert instance |
|
76
|
|
|
# The base class should compare by object identity instead. |
|
77
|
|
|
assert instance == instance |
|
78
|
|
|
assert option_class.Left(1) != option_class.Left(1) |
|
79
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
def test_cant_hash(): |
|
82
|
|
|
with pytest.raises(TypeError): |
|
83
|
|
|
assert not hash(enum_options.CustomEq.Left(1)) |
|
84
|
|
|
assert ( |
|
85
|
|
|
enum_options.CustomEq.Left(1) != enum_options.CustomEq.Left(1)) |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
def test_str(option_class): |
|
89
|
|
|
assert str(option_class.Left(1)) == repr(option_class.Left(1)) |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
def test_cant_init_superclass(option_class): |
|
93
|
|
|
with pytest.raises(TypeError): |
|
94
|
|
|
assert not option_class(()) |
|
95
|
|
|
|