Coverage for /home/runner/work/zserio/zserio/compiler/extensions/python/runtime/tests/test_enum.py: 100%
59 statements
« prev ^ index » next coverage.py v6.5.0, created at 2026-10-19 14:18 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2026-10-19 14:18 +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_lt(self):
44 class OtherEnum(Enum):
45 ONE = 1
46 TWO = 2
48 class ReversedEnum(Enum):
49 TWO = 2
50 ONE = 1
52 with warnings.catch_warnings():
53 warnings.simplefilter("ignore", DeprecationWarning)
54 one = EnumTest.TestEnum.ONE
55 self.assertTrue(one < EnumTest.TestEnum.TWO)
56 self.assertFalse(EnumTest.TestEnum.TWO < one)
57 self.assertFalse(one < one) # pylint: disable=comparison-with-itself
59 # ordered by values, not by the declaration order
60 self.assertTrue(ReversedEnum.ONE < ReversedEnum.TWO)
61 self.assertEqual([ReversedEnum.ONE, ReversedEnum.TWO], sorted(ReversedEnum))
63 with self.assertRaises(TypeError):
64 _ = OtherEnum.ONE < EnumTest.TestEnum.TWO
66 with self.assertRaises(TypeError):
67 _ = OtherEnum.ONE < 2
69 def test_invalid_argument(self):
70 with self.assertRaises(ValueError):
71 # pylint: disable=unused-variable
72 class EnumInvalidDeprecatedItemObject(Enum):
73 ONE = 1, DeprecatedItem()
75 with self.assertRaises(ValueError):
76 # pylint: disable=unused-variable
77 class EnumInvalidDeprecatedItemInt(Enum):
78 ONE = 1, 2
80 with self.assertRaises(ValueError):
81 # pylint: disable=unused-variable
82 class EnumInvalidDeprecatedItemStr(Enum):
83 ONE = 1, "deprecated"