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
|
# pylint: disable=missing-docstring, too-few-public-methods
"""
Test the trait-type ``UseEnum``.
"""
from __future__ import annotations
import enum
import unittest
from traitlets import CaselessStrEnum, Enum, FuzzyEnum, HasTraits, TraitError, UseEnum
# -----------------------------------------------------------------------------
# TEST SUPPORT:
# -----------------------------------------------------------------------------
class Color(enum.Enum):
red = 1
green = 2
blue = 3
yellow = 4
class OtherColor(enum.Enum):
red = 0
green = 1
class CSColor(enum.Enum):
red = 1
Green = 2
BLUE = 3
YeLLoW = 4
color_choices = "red Green BLUE YeLLoW".split()
# -----------------------------------------------------------------------------
# TESTSUITE:
# -----------------------------------------------------------------------------
class TestUseEnum(unittest.TestCase):
# pylint: disable=invalid-name
class Example(HasTraits):
color = UseEnum(Color, help="Color enum")
def test_assign_enum_value(self):
example = self.Example()
example.color = Color.green
self.assertEqual(example.color, Color.green)
def test_assign_all_enum_values(self):
# pylint: disable=no-member
enum_values = list(Color.__members__.values())
for value in enum_values:
self.assertIsInstance(value, Color)
example = self.Example()
example.color = value
self.assertEqual(example.color, value)
self.assertIsInstance(value, Color)
def test_assign_enum_value__with_other_enum_raises_error(self):
example = self.Example()
with self.assertRaises(TraitError):
example.color = OtherColor.green
def test_assign_enum_name_1(self):
# -- CONVERT: string => Enum value (item)
example = self.Example()
example.color = "red"
self.assertEqual(example.color, Color.red)
def test_assign_enum_value_name(self):
# -- CONVERT: string => Enum value (item)
# pylint: disable=no-member
enum_names = [enum_val.name for enum_val in Color.__members__.values()]
for value in enum_names:
self.assertIsInstance(value, str)
example = self.Example()
enum_value = Color.__members__.get(value)
example.color = value
self.assertIs(example.color, enum_value)
self.assertEqual(example.color.name, value) # type:ignore
def test_assign_scoped_enum_value_name(self):
# -- CONVERT: string => Enum value (item)
scoped_names = ["Color.red", "Color.green", "Color.blue", "Color.yellow"]
for value in scoped_names:
example = self.Example()
example.color = value
self.assertIsInstance(example.color, Color)
self.assertEqual(str(example.color), value)
def test_assign_bad_enum_value_name__raises_error(self):
# -- CONVERT: string => Enum value (item)
bad_enum_names = ["UNKNOWN_COLOR", "RED", "Green", "blue2"]
for value in bad_enum_names:
example = self.Example()
with self.assertRaises(TraitError):
example.color = value
def test_assign_enum_value_number_1(self):
# -- CONVERT: number => Enum value (item)
example = self.Example()
example.color = 1 # == Color.red.value
example.color = Color.red.value
self.assertEqual(example.color, Color.red)
def test_assign_enum_value_number(self):
# -- CONVERT: number => Enum value (item)
# pylint: disable=no-member
enum_numbers = [enum_val.value for enum_val in Color.__members__.values()]
for value in enum_numbers:
self.assertIsInstance(value, int)
example = self.Example()
example.color = value
self.assertIsInstance(example.color, Color)
self.assertEqual(example.color.value, value) # type:ignore
def test_assign_bad_enum_value_number__raises_error(self):
# -- CONVERT: number => Enum value (item)
bad_numbers = [-1, 0, 5]
for value in bad_numbers:
self.assertIsInstance(value, int)
assert UseEnum(Color).select_by_number(value, None) is None
example = self.Example()
with self.assertRaises(TraitError):
example.color = value
def test_ctor_without_default_value(self):
# -- IMPLICIT: default_value = Color.red (first enum-value)
class Example2(HasTraits):
color = UseEnum(Color)
example = Example2()
self.assertEqual(example.color, Color.red)
def test_ctor_with_default_value_as_enum_value(self):
# -- CONVERT: number => Enum value (item)
class Example2(HasTraits):
color = UseEnum(Color, default_value=Color.green)
example = Example2()
self.assertEqual(example.color, Color.green)
def test_ctor_with_default_value_none_and_not_allow_none(self):
# -- IMPLICIT: default_value = Color.red (first enum-value)
class Example2(HasTraits):
color1 = UseEnum(Color, default_value=None, allow_none=False)
color2 = UseEnum(Color, default_value=None)
example = Example2()
self.assertEqual(example.color1, Color.red)
self.assertEqual(example.color2, Color.red)
def test_ctor_with_default_value_none_and_allow_none(self):
class Example2(HasTraits):
color1 = UseEnum(Color, default_value=None, allow_none=True)
color2 = UseEnum(Color, allow_none=True)
example = Example2()
self.assertIs(example.color1, None)
self.assertIs(example.color2, None)
def test_assign_none_without_allow_none_resets_to_default_value(self):
class Example2(HasTraits):
color1 = UseEnum(Color, allow_none=False)
color2 = UseEnum(Color)
example = Example2()
example.color1 = None
example.color2 = None
self.assertIs(example.color1, Color.red)
self.assertIs(example.color2, Color.red)
def test_assign_none_to_enum_or_none(self):
class Example2(HasTraits):
color = UseEnum(Color, allow_none=True)
example = Example2()
example.color = None
self.assertIs(example.color, None)
def test_assign_bad_value_with_to_enum_or_none(self):
class Example2(HasTraits):
color = UseEnum(Color, allow_none=True)
example = Example2()
with self.assertRaises(TraitError):
example.color = "BAD_VALUE"
def test_info(self):
choices = color_choices
class Example(HasTraits):
enum1 = Enum(choices, allow_none=False)
enum2 = CaselessStrEnum(choices, allow_none=False)
enum3 = FuzzyEnum(choices, allow_none=False)
enum4 = UseEnum(CSColor, allow_none=False)
for i in range(1, 5):
attr = "enum%s" % i
enum = getattr(Example, attr)
enum.allow_none = True
info = enum.info()
self.assertEqual(len(info.split(", ")), len(choices), info.split(", "))
self.assertIn("or None", info)
info = enum.info_rst()
self.assertEqual(len(info.split("|")), len(choices), info.split("|"))
self.assertIn("or `None`", info)
# Check no single `\` exists.
self.assertNotRegex(info, r"\b\\\b")
enum.allow_none = False
info = enum.info()
self.assertEqual(len(info.split(", ")), len(choices), info.split(", "))
self.assertNotIn("None", info)
info = enum.info_rst()
self.assertEqual(len(info.split("|")), len(choices), info.split("|"))
self.assertNotIn("None", info)
# Check no single `\` exists.
self.assertNotRegex(info, r"\b\\\b")
# -----------------------------------------------------------------------------
# TESTSUITE:
# -----------------------------------------------------------------------------
class TestFuzzyEnum(unittest.TestCase):
# Check mostly `validate()`, Ctor must be checked on generic `Enum`
# or `CaselessStrEnum`.
def test_search_all_prefixes__overwrite(self):
class FuzzyExample(HasTraits):
color = FuzzyEnum(color_choices, help="Color enum")
example = FuzzyExample()
for color in color_choices:
for wlen in range(1, len(color)):
value = color[:wlen]
example.color = value
self.assertEqual(example.color, color)
example.color = value.upper()
self.assertEqual(example.color, color)
example.color = value.lower()
self.assertEqual(example.color, color)
def test_search_all_prefixes__ctor(self):
class FuzzyExample(HasTraits):
color = FuzzyEnum(color_choices, help="Color enum")
for color in color_choices:
for wlen in range(1, len(color)):
value = color[:wlen]
example = FuzzyExample()
example.color = value
self.assertEqual(example.color, color)
example = FuzzyExample()
example.color = value.upper()
self.assertEqual(example.color, color)
example = FuzzyExample()
example.color = value.lower()
self.assertEqual(example.color, color)
def test_search_substrings__overwrite(self):
class FuzzyExample(HasTraits):
color = FuzzyEnum(color_choices, help="Color enum", substring_matching=True)
example = FuzzyExample()
for color in color_choices:
for wlen in range(2):
value = color[wlen:]
example.color = value
self.assertEqual(example.color, color)
example.color = value.upper()
self.assertEqual(example.color, color)
example.color = value.lower()
self.assertEqual(example.color, color)
def test_search_substrings__ctor(self):
class FuzzyExample(HasTraits):
color = FuzzyEnum(color_choices, help="Color enum", substring_matching=True)
color = color_choices[-1] # 'YeLLoW'
for end in (-1, len(color)):
for start in range(1, len(color) - 2):
value = color[start:end]
example = FuzzyExample()
example.color = value
self.assertEqual(example.color, color)
example = FuzzyExample()
example.color = value.upper()
self.assertEqual(example.color, color)
def test_assign_other_raises(self):
def new_trait_class(case_sensitive, substring_matching):
class Example(HasTraits):
color = FuzzyEnum(
color_choices,
case_sensitive=case_sensitive,
substring_matching=substring_matching,
)
return Example
example = new_trait_class(case_sensitive=False, substring_matching=False)()
with self.assertRaises(TraitError):
example.color = ""
with self.assertRaises(TraitError):
example.color = "BAD COLOR"
with self.assertRaises(TraitError):
example.color = "ed"
example = new_trait_class(case_sensitive=True, substring_matching=False)()
with self.assertRaises(TraitError):
example.color = ""
with self.assertRaises(TraitError):
example.color = "Red" # not 'red'
example = new_trait_class(case_sensitive=True, substring_matching=True)()
with self.assertRaises(TraitError):
example.color = ""
with self.assertRaises(TraitError):
example.color = "BAD COLOR"
with self.assertRaises(TraitError):
example.color = "green" # not 'Green'
with self.assertRaises(TraitError):
example.color = "lue" # not (b)'LUE'
with self.assertRaises(TraitError):
example.color = "lUE" # not (b)'LUE'
example = new_trait_class(case_sensitive=False, substring_matching=True)()
with self.assertRaises(TraitError):
example.color = ""
with self.assertRaises(TraitError):
example.color = "BAD COLOR"
def test_ctor_with_default_value(self):
def new_trait_class(default_value, case_sensitive, substring_matching):
class Example(HasTraits):
color = FuzzyEnum(
color_choices,
default_value=default_value,
case_sensitive=case_sensitive,
substring_matching=substring_matching,
)
return Example
for color in color_choices:
example = new_trait_class(color, False, False)()
self.assertEqual(example.color, color)
example = new_trait_class(color.upper(), False, False)()
self.assertEqual(example.color, color)
color = color_choices[-1] # 'YeLLoW'
example = new_trait_class(color, True, False)()
self.assertEqual(example.color, color)
# FIXME: default value not validated!
# with self.assertRaises(TraitError):
# example = new_trait_class(color.lower(), True, False)
|