Coverage for tsfpga/test/unit/test_module_list.py: 100%

56 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-11-29 20:01 +0000

1# -------------------------------------------------------------------------------------------------- 

2# Copyright (c) Lukas Vik. All rights reserved. 

3# 

4# This file is part of the tsfpga project, a project platform for modern FPGA development. 

5# https://tsfpga.com 

6# https://gitlab.com/tsfpga/tsfpga 

7# -------------------------------------------------------------------------------------------------- 

8 

9# Standard libraries 

10from unittest import TestCase 

11from unittest.mock import MagicMock 

12 

13# Third party libraries 

14import pytest 

15 

16# First party libraries 

17from tsfpga.module_list import ModuleList 

18 

19 

20def test_module_list_indexing(): 

21 modules = ModuleList() 

22 

23 module_a = MagicMock() 

24 module_b = MagicMock() 

25 

26 modules.append(module_a) 

27 modules.append(module_b) 

28 

29 assert modules[0] is module_a 

30 assert modules[1] is module_b 

31 

32 

33class TestModuleList(TestCase): 

34 def setUp(self): 

35 self.modules = ModuleList() 

36 

37 self.module_a = MagicMock() 

38 self.module_b = MagicMock() 

39 self.module_c = MagicMock() 

40 

41 self.module_a.name = "a" 

42 self.module_b.name = "b" 

43 self.module_c.name = "c" 

44 

45 self.modules.append(self.module_a) 

46 self.modules.append(self.module_b) 

47 self.modules.append(self.module_c) 

48 

49 def test_get(self): 

50 assert self.modules.get("a") == self.module_a 

51 assert self.modules.get("c") == self.module_c 

52 

53 with pytest.raises(ValueError) as exception_info: 

54 self.modules.get("d") 

55 assert str(exception_info.value) == 'No module "d" available' 

56 

57 def test_iteration(self): 

58 expected = [self.module_a, self.module_b, self.module_c] 

59 

60 # pylint: disable=unnecessary-comprehension 

61 assert [module for module in self.modules] == expected 

62 assert list(self.modules) == expected 

63 

64 def test_can_cast_to_string_without_error(self): 

65 print(self.modules) 

66 

67 def test_concatenation(self): 

68 modules_2 = ModuleList() 

69 

70 module_d = MagicMock() 

71 module_d.name = "d" 

72 modules_2.append(module_d) 

73 

74 all_modules = self.modules + modules_2 

75 

76 assert len(all_modules) == 4 

77 assert list(all_modules) == [self.module_a, self.module_b, self.module_c, module_d] 

78 

79 # The addition should result in a new object. Should not affect the existing objects. 

80 assert len(self.modules) == 3 

81 assert len(modules_2) == 1 

82 

83 assert (all_modules is not modules_2) and (all_modules is not self.modules) 

84 

85 # pylint: disable=protected-access 

86 assert (all_modules._modules is not modules_2._modules) and ( 

87 all_modules._modules is not self.modules._modules 

88 ) 

89 

90 def test_concatenation_with_unknown_object_should_raise_interrupt(self): 

91 with pytest.raises(TypeError) as exception_info: 

92 # pylint: disable=pointless-statement 

93 self.modules + 3 

94 assert str(exception_info.value) == "Can only concatenate with another ModuleList" 

95 

96 def test_copy(self): 

97 modules_copy = self.modules.copy() 

98 

99 modules_copy.append(4) 

100 assert len(self.modules) == 3