]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-46262: [Enum] test error path in `Flag._missing_` (GH-30408)
authorNikita Sobolev <mail@sobolevn.me>
Wed, 5 Jan 2022 00:11:06 +0000 (03:11 +0300)
committerGitHub <noreply@github.com>
Wed, 5 Jan 2022 00:11:06 +0000 (16:11 -0800)
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>
Lib/test/test_enum.py
Misc/NEWS.d/next/Tests/2022-01-05-01-38-45.bpo-46262.MhiLWP.rst [new file with mode: 0644]

index eecb9fd4835c4099dc7e1820ed35e0b2731fa2ed..51a31e5ebf8076b889269238e18d35ed1a91a890 100644 (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):
 
diff --git a/Misc/NEWS.d/next/Tests/2022-01-05-01-38-45.bpo-46262.MhiLWP.rst b/Misc/NEWS.d/next/Tests/2022-01-05-01-38-45.bpo-46262.MhiLWP.rst
new file mode 100644 (file)
index 0000000..456d135
--- /dev/null
@@ -0,0 +1 @@
+Cover ``ValueError`` path in tests for :meth:`enum.Flag._missing_`.