bpo-46262: [Enum] test error path in Flag._missing_ (GH-30408)

add tests that exercise the `_missing_` error path for `Flag` and `IntFlag`

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
This commit is contained in:
Nikita Sobolev 2022-01-05 03:11:06 +03:00 committed by GitHub
parent 31e43cbe5f
commit 91bc6f9615
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 0 deletions

View file

@ -3414,6 +3414,19 @@ class TestFlag(unittest.TestCase):
self.assertFalse(NeverEnum.__dict__.get('_test1', False))
self.assertFalse(NeverEnum.__dict__.get('_test2', False))
def test_default_missing(self):
with self.assertRaisesRegex(
ValueError,
"'RED' is not a valid TestFlag.Color",
) as ctx:
self.Color('RED')
self.assertIs(ctx.exception.__context__, None)
P = Flag('P', 'X Y')
with self.assertRaisesRegex(ValueError, "'X' is not a valid P") as ctx:
P('X')
self.assertIs(ctx.exception.__context__, None)
class TestIntFlag(unittest.TestCase):
"""Tests of the IntFlags."""
@ -3975,6 +3988,19 @@ class TestIntFlag(unittest.TestCase):
'at least one thread failed while creating composite members')
self.assertEqual(256, len(seen), 'too many composite members created')
def test_default_missing(self):
with self.assertRaisesRegex(
ValueError,
"'RED' is not a valid TestIntFlag.Color",
) as ctx:
self.Color('RED')
self.assertIs(ctx.exception.__context__, None)
P = IntFlag('P', 'X Y')
with self.assertRaisesRegex(ValueError, "'X' is not a valid P") as ctx:
P('X')
self.assertIs(ctx.exception.__context__, None)
class TestEmptyAndNonLatinStrings(unittest.TestCase):