Coverage for /home/jenkins/workspace/NDS/Zserio/NDS_ZSERIO-linux-build/compiler/extensions/python/runtime/tests/test_enum.py: 100%

35 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-12-13 15:12 +0000

1import unittest 

2import warnings 

3 

4from zserio.enum import Enum, DeprecatedItem 

5 

6class EnumTest(unittest.TestCase): 

7 class TestEnum(Enum): 

8 ONE = 1, DeprecatedItem 

9 TWO = 2 

10 

11 def test_deprecation_getattribute(self): 

12 with warnings.catch_warnings(record=True) as caught_warnings: 

13 self.assertEqual(2, EnumTest.TestEnum.TWO.value) 

14 self.assertEqual(0, len(caught_warnings)) 

15 

16 with self.assertWarns(DeprecationWarning): 

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

18 

19 def test_deprecation_getitem(self): 

20 with warnings.catch_warnings(record=True) as caught_warnings: 

21 self.assertEqual(2, EnumTest.TestEnum["TWO"].value) 

22 self.assertEqual(0, len(caught_warnings)) 

23 

24 with self.assertWarns(DeprecationWarning): 

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

26 

27 def test_deprecation_call(self): 

28 with warnings.catch_warnings(record=True) as caught_warnings: 

29 self.assertEqual(EnumTest.TestEnum.TWO, EnumTest.TestEnum(2)) 

30 self.assertEqual(0, len(caught_warnings)) 

31 

32 with self.assertWarns(DeprecationWarning): 

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

34 

35 def test_invalid_argument(self): 

36 with self.assertRaises(ValueError): 

37 # pylint: disable=unused-variable 

38 class EnumInvalidDeprecatedItemObject(Enum): 

39 ONE = 1, DeprecatedItem() 

40 

41 with self.assertRaises(ValueError): 

42 # pylint: disable=unused-variable 

43 class EnumInvalidDeprecatedItemInt(Enum): 

44 ONE = 1, 2 

45 

46 with self.assertRaises(ValueError): 

47 # pylint: disable=unused-variable 

48 class EnumInvalidDeprecatedItemStr(Enum): 

49 ONE = 1, "deprecated"