Coverage for /home/runner/work/zserio/zserio/compiler/extensions/python/runtime/tests/test_enum.py: 100%
40 statements
« prev ^ index » next coverage.py v6.5.0, created at 2024-12-05 10:43 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2024-12-05 10:43 +0000
1import unittest
2import warnings
4from zserio.enum import Enum, DeprecatedItem
7class EnumTest(unittest.TestCase):
8 class TestEnum(Enum):
9 ONE = 1, DeprecatedItem
10 TWO = 2
12 def test_deprecation_getattribute(self):
13 with warnings.catch_warnings(record=True) as caught_warnings:
14 self.assertEqual(2, EnumTest.TestEnum.TWO.value)
15 self.assertEqual(0, len(caught_warnings))
17 with self.assertWarns(DeprecationWarning):
18 self.assertEqual(1, EnumTest.TestEnum.ONE.value)
20 def test_deprecation_getitem(self):
21 with warnings.catch_warnings(record=True) as caught_warnings:
22 self.assertEqual(2, EnumTest.TestEnum["TWO"].value)
23 self.assertEqual(0, len(caught_warnings))
25 with self.assertWarns(DeprecationWarning):
26 self.assertEqual(1, EnumTest.TestEnum["ONE"].value)
28 def test_deprecation_call(self):
29 with warnings.catch_warnings(record=True) as caught_warnings:
30 self.assertEqual(EnumTest.TestEnum.TWO, EnumTest.TestEnum(2))
31 self.assertEqual(0, len(caught_warnings))
33 with self.assertWarns(DeprecationWarning):
34 self.assertEqual(EnumTest.TestEnum.ONE, EnumTest.TestEnum(1))
36 def test_functional_syntax(self):
37 # pylint: disable=invalid-name
38 TestEnumColor = Enum("TestEnumColor", ["BLUE", "RED", "GREEN"])
39 self.assertEqual(TestEnumColor.BLUE, TestEnumColor(1))
40 self.assertEqual(TestEnumColor.RED, TestEnumColor(2))
41 self.assertEqual(TestEnumColor.GREEN, TestEnumColor(3))
43 def test_invalid_argument(self):
44 with self.assertRaises(ValueError):
45 # pylint: disable=unused-variable
46 class EnumInvalidDeprecatedItemObject(Enum):
47 ONE = 1, DeprecatedItem()
49 with self.assertRaises(ValueError):
50 # pylint: disable=unused-variable
51 class EnumInvalidDeprecatedItemInt(Enum):
52 ONE = 1, 2
54 with self.assertRaises(ValueError):
55 # pylint: disable=unused-variable
56 class EnumInvalidDeprecatedItemStr(Enum):
57 ONE = 1, "deprecated"