Coverage for /home/runner/work/zserio/zserio/compiler/extensions/python/runtime/tests/test_enum.py: 100%
35 statements
« prev ^ index » next coverage.py v6.5.0, created at 2024-04-30 09:38 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2024-04-30 09:38 +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_invalid_argument(self):
37 with self.assertRaises(ValueError):
38 # pylint: disable=unused-variable
39 class EnumInvalidDeprecatedItemObject(Enum):
40 ONE = 1, DeprecatedItem()
42 with self.assertRaises(ValueError):
43 # pylint: disable=unused-variable
44 class EnumInvalidDeprecatedItemInt(Enum):
45 ONE = 1, 2
47 with self.assertRaises(ValueError):
48 # pylint: disable=unused-variable
49 class EnumInvalidDeprecatedItemStr(Enum):
50 ONE = 1, "deprecated"