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-07-18 11:41 +0000

1import unittest 

2import warnings 

3 

4from zserio.enum import Enum, DeprecatedItem 

5 

6 

7class EnumTest(unittest.TestCase): 

8 class TestEnum(Enum): 

9 ONE = 1, DeprecatedItem 

10 TWO = 2 

11 

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)) 

16 

17 with self.assertWarns(DeprecationWarning): 

18 self.assertEqual(1, EnumTest.TestEnum.ONE.value) 

19 

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)) 

24 

25 with self.assertWarns(DeprecationWarning): 

26 self.assertEqual(1, EnumTest.TestEnum["ONE"].value) 

27 

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)) 

32 

33 with self.assertWarns(DeprecationWarning): 

34 self.assertEqual(EnumTest.TestEnum.ONE, EnumTest.TestEnum(1)) 

35 

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)) 

42 

43 def test_invalid_argument(self): 

44 with self.assertRaises(ValueError): 

45 # pylint: disable=unused-variable 

46 class EnumInvalidDeprecatedItemObject(Enum): 

47 ONE = 1, DeprecatedItem() 

48 

49 with self.assertRaises(ValueError): 

50 # pylint: disable=unused-variable 

51 class EnumInvalidDeprecatedItemInt(Enum): 

52 ONE = 1, 2 

53 

54 with self.assertRaises(ValueError): 

55 # pylint: disable=unused-variable 

56 class EnumInvalidDeprecatedItemStr(Enum): 

57 ONE = 1, "deprecated"