/
test.py
125 lines (102 loc) · 4.28 KB
/
test.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
from mapped_enum import enum_map
from enum import Enum
import unittest
class TestMapping(unittest.TestCase):
def setUp(self):
@enum_map(['color', 'sound'])
class Animal(Enum):
CHOCOLATE_LAB = 'brown', 'woof'
TABBY_CAT = 'orange', 'meow'
LION = 'yellow', 'roar'
self.Animal = Animal
def test_to(self):
self.assertEqual(self.Animal.CHOCOLATE_LAB.to_color(), 'brown')
self.assertEqual(self.Animal.TABBY_CAT.to_sound(), 'meow')
def test_from(self):
self.assertEqual(self.Animal.from_color('yellow'), self.Animal.LION)
self.assertEqual(self.Animal.from_sound('woof'), self.Animal.CHOCOLATE_LAB)
def test_non_enum(self):
with self.assertRaisesRegex(ValueError, 'not a descendant of Enum$'):
# noinspection PyUnusedLocal
@enum_map(['bar'])
class Foo:
pass
def test_invalid_keynames(self):
with self.assertRaisesRegex(ValueError, 'results in invalid identifiers$'):
# noinspection PyUnusedLocal
@enum_map(['!bar'])
class Foo:
pass
def test_alternate_prefix(self):
@enum_map(['direction'], to_prefix='as_', from_prefix='with_')
class Cardinal(Enum):
NORTH = 'up'
WEST = 'left'
self.assertEqual(Cardinal.NORTH.as_direction(), 'up')
self.assertEqual(Cardinal.with_direction('left'), Cardinal.WEST)
def test_wrong_alternate_prefix(self):
with self.assertRaisesRegex(ValueError, '^invalid prefix ".*" for `to` method$'):
# noinspection PyUnusedLocal
@enum_map(['name'], to_prefix='1to_')
class Foo(Enum):
pass
def test_no_keys(self):
with self.assertRaisesRegex(ValueError, '^keys specifier cannot be empty$'):
# noinspection PyUnusedLocal
@enum_map('')
class Foo(Enum):
pass
with self.assertRaisesRegex(ValueError, '^at least one key must be specified'):
# noinspection PyUnusedLocal
@enum_map([])
class Foo(Enum):
pass
def test_wrong_mapping_count(self):
with self.assertRaisesRegex(AttributeError, '^.* has the wrong number of map values'):
# noinspection PyUnusedLocal
@enum_map(['speed', 'color', 'noise'])
class Car(Enum):
RACECAR = 'fast', 'red', 'loud'
# Missing `noise` mapping
HYBRID = 'slow', 'blue'
with self.assertRaisesRegex(AttributeError, '^.* has the wrong number of map values'):
# noinspection PyUnusedLocal
@enum_map(['speed'])
class Car(Enum):
RACECAR = 'fast'
# Has extra mapping
HYBRID = 'slow', 'blue'
def test_override(self):
with self.assertRaisesRegex(ValueError, 'already exists$'):
# noinspection PyUnusedLocal
@enum_map(['first_month', 'color'])
class Season(Enum):
SPRING = 'march', 'green'
SUMMER = 'july', 'yellow'
FALL = 'september', 'orange'
WINTER = 'december', 'blue'
def to_color(self):
# This method being defined should cause an exception.
pass
@enum_map(['first_month', 'color'], allow_override=True)
class Season(Enum):
SPRING = 'march', 'green'
SUMMER = 'july', 'yellow'
FALL = 'september', 'orange'
WINTER = 'december', 'blue'
def to_color(self):
# Override Summer's color to be red instead.
if self == self.SUMMER:
return 'red'
return self.value[1]
self.assertEqual(Season.WINTER.to_color(), 'blue')
self.assertEqual(Season.SUMMER.to_color(), 'red')
def test_multiple_froms(self):
@enum_map(['ground', 'water'], multiple_from=True)
class Environment(Enum):
DESERT = 'sand', 'none'
BEACH = 'sand', 'lots'
PLAINS = 'grass', 'some'
self.assertEqual(Environment.from_ground('sand'), [Environment.DESERT, Environment.BEACH])
if __name__ == '__main__':
unittest.main()