@@ -28,6 +28,9 @@ def load_tests(loader, tests, ignore):
28
28
))
29
29
return tests
30
30
31
+ MODULE = ('test.test_enum' , '__main__' )[__name__ == '__main__' ]
32
+ SHORT_MODULE = MODULE .split ('.' )[- 1 ]
33
+
31
34
# for pickle tests
32
35
try :
33
36
class Stooges (Enum ):
@@ -143,6 +146,23 @@ def __init__(self, fget=None, fset=None, fdel=None, doc=None):
143
146
def __get__ (self , instance , ownerclass ):
144
147
return self .fget (ownerclass )
145
148
149
+ # for global repr tests
150
+
151
+ @enum .global_enum
152
+ class HeadlightsK (IntFlag , boundary = enum .KEEP ):
153
+ OFF_K = 0
154
+ LOW_BEAM_K = auto ()
155
+ HIGH_BEAM_K = auto ()
156
+ FOG_K = auto ()
157
+
158
+
159
+ @enum .global_enum
160
+ class HeadlightsC (IntFlag , boundary = enum .CONFORM ):
161
+ OFF_C = 0
162
+ LOW_BEAM_C = auto ()
163
+ HIGH_BEAM_C = auto ()
164
+ FOG_C = auto ()
165
+
146
166
147
167
# tests
148
168
@@ -3224,6 +3244,34 @@ def test_repr(self):
3224
3244
self .assertEqual (repr (~ (Open .WO | Open .CE )), 'Open.RW' )
3225
3245
self .assertEqual (repr (Open (~ 4 )), '-5' )
3226
3246
3247
+ def test_global_repr_keep (self ):
3248
+ self .assertEqual (
3249
+ repr (HeadlightsK (0 )),
3250
+ '%s.OFF_K' % SHORT_MODULE ,
3251
+ )
3252
+ self .assertEqual (
3253
+ repr (HeadlightsK (2 ** 0 + 2 ** 2 + 2 ** 3 )),
3254
+ '%(m)s.LOW_BEAM_K|%(m)s.FOG_K|0x8' % {'m' : SHORT_MODULE },
3255
+ )
3256
+ self .assertEqual (
3257
+ repr (HeadlightsK (2 ** 3 )),
3258
+ '%(m)s.HeadlightsK(0x8)' % {'m' : SHORT_MODULE },
3259
+ )
3260
+
3261
+ def test_global_repr_conform1 (self ):
3262
+ self .assertEqual (
3263
+ repr (HeadlightsC (0 )),
3264
+ '%s.OFF_C' % SHORT_MODULE ,
3265
+ )
3266
+ self .assertEqual (
3267
+ repr (HeadlightsC (2 ** 0 + 2 ** 2 + 2 ** 3 )),
3268
+ '%(m)s.LOW_BEAM_C|%(m)s.FOG_C' % {'m' : SHORT_MODULE },
3269
+ )
3270
+ self .assertEqual (
3271
+ repr (HeadlightsC (2 ** 3 )),
3272
+ '%(m)s.OFF_C' % {'m' : SHORT_MODULE },
3273
+ )
3274
+
3227
3275
def test_format (self ):
3228
3276
Perm = self .Perm
3229
3277
self .assertEqual (format (Perm .R , '' ), '4' )
@@ -4085,7 +4133,7 @@ def setUp(self):
4085
4133
def test_convert_value_lookup_priority (self ):
4086
4134
test_type = enum .IntEnum ._convert_ (
4087
4135
'UnittestConvert' ,
4088
- ( 'test.test_enum' , '__main__' )[ __name__ == '__main__' ] ,
4136
+ MODULE ,
4089
4137
filter = lambda x : x .startswith ('CONVERT_TEST_' ))
4090
4138
# We don't want the reverse lookup value to vary when there are
4091
4139
# multiple possible names for a given value. It should always
@@ -4095,7 +4143,7 @@ def test_convert_value_lookup_priority(self):
4095
4143
def test_convert (self ):
4096
4144
test_type = enum .IntEnum ._convert_ (
4097
4145
'UnittestConvert' ,
4098
- ( 'test.test_enum' , '__main__' )[ __name__ == '__main__' ] ,
4146
+ MODULE ,
4099
4147
filter = lambda x : x .startswith ('CONVERT_TEST_' ))
4100
4148
# Ensure that test_type has all of the desired names and values.
4101
4149
self .assertEqual (test_type .CONVERT_TEST_NAME_F ,
@@ -4115,7 +4163,7 @@ def test_convert_warn(self):
4115
4163
with self .assertWarns (DeprecationWarning ):
4116
4164
enum .IntEnum ._convert (
4117
4165
'UnittestConvert' ,
4118
- ( 'test.test_enum' , '__main__' )[ __name__ == '__main__' ] ,
4166
+ MODULE ,
4119
4167
filter = lambda x : x .startswith ('CONVERT_TEST_' ))
4120
4168
4121
4169
@unittest .skipUnless (python_version >= (3 , 9 ),
@@ -4124,16 +4172,15 @@ def test_convert_raise(self):
4124
4172
with self .assertRaises (AttributeError ):
4125
4173
enum .IntEnum ._convert (
4126
4174
'UnittestConvert' ,
4127
- ( 'test.test_enum' , '__main__' )[ __name__ == '__main__' ] ,
4175
+ MODULE ,
4128
4176
filter = lambda x : x .startswith ('CONVERT_TEST_' ))
4129
4177
4130
4178
def test_convert_repr_and_str (self ):
4131
- module = ('test.test_enum' , '__main__' )[__name__ == '__main__' ]
4132
4179
test_type = enum .IntEnum ._convert_ (
4133
4180
'UnittestConvert' ,
4134
- module ,
4181
+ MODULE ,
4135
4182
filter = lambda x : x .startswith ('CONVERT_STRING_TEST_' ))
4136
- self .assertEqual (repr (test_type .CONVERT_STRING_TEST_NAME_A ), '%s.CONVERT_STRING_TEST_NAME_A' % module )
4183
+ self .assertEqual (repr (test_type .CONVERT_STRING_TEST_NAME_A ), '%s.CONVERT_STRING_TEST_NAME_A' % SHORT_MODULE )
4137
4184
self .assertEqual (str (test_type .CONVERT_STRING_TEST_NAME_A ), 'CONVERT_STRING_TEST_NAME_A' )
4138
4185
self .assertEqual (format (test_type .CONVERT_STRING_TEST_NAME_A ), '5' )
4139
4186
@@ -4151,7 +4198,7 @@ def setUp(self):
4151
4198
def test_convert (self ):
4152
4199
test_type = enum .StrEnum ._convert_ (
4153
4200
'UnittestConvert' ,
4154
- ( 'test.test_enum' , '__main__' )[ __name__ == '__main__' ] ,
4201
+ MODULE ,
4155
4202
filter = lambda x : x .startswith ('CONVERT_STR_' ))
4156
4203
# Ensure that test_type has all of the desired names and values.
4157
4204
self .assertEqual (test_type .CONVERT_STR_TEST_1 , 'hello' )
@@ -4162,12 +4209,11 @@ def test_convert(self):
4162
4209
[], msg = 'Names other than CONVERT_STR_* found.' )
4163
4210
4164
4211
def test_convert_repr_and_str (self ):
4165
- module = ('test.test_enum' , '__main__' )[__name__ == '__main__' ]
4166
4212
test_type = enum .StrEnum ._convert_ (
4167
4213
'UnittestConvert' ,
4168
- module ,
4214
+ MODULE ,
4169
4215
filter = lambda x : x .startswith ('CONVERT_STR_' ))
4170
- self .assertEqual (repr (test_type .CONVERT_STR_TEST_1 ), '%s.CONVERT_STR_TEST_1' % module )
4216
+ self .assertEqual (repr (test_type .CONVERT_STR_TEST_1 ), '%s.CONVERT_STR_TEST_1' % SHORT_MODULE )
4171
4217
self .assertEqual (str (test_type .CONVERT_STR_TEST_2 ), 'goodbye' )
4172
4218
self .assertEqual (format (test_type .CONVERT_STR_TEST_1 ), 'hello' )
4173
4219
0 commit comments