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

58 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-20 20:51 +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://github.com/tsfpga/tsfpga 

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

8 

9# Standard libraries 

10from unittest.mock import MagicMock 

11 

12# Third party libraries 

13import pytest 

14 

15# First party libraries 

16from tsfpga.module_list import ModuleList 

17 

18 

19def test_module_list_indexing(): 

20 modules = ModuleList() 

21 

22 module_a = MagicMock() 

23 module_b = MagicMock() 

24 

25 modules.append(module_a) 

26 modules.append(module_b) 

27 

28 assert modules[0] is module_a 

29 assert modules[1] is module_b 

30 

31 

32@pytest.fixture 

33def module_list_test(): 

34 class ModuleListTest: 

35 def __init__(self): 

36 self.modules = ModuleList() 

37 

38 self.module_a = MagicMock() 

39 self.module_b = MagicMock() 

40 self.module_c = MagicMock() 

41 

42 self.module_a.name = "a" 

43 self.module_b.name = "b" 

44 self.module_c.name = "c" 

45 

46 self.modules.append(self.module_a) 

47 self.modules.append(self.module_b) 

48 self.modules.append(self.module_c) 

49 

50 return ModuleListTest() 

51 

52 

53# False positive for pytest fixtures 

54# pylint: disable=redefined-outer-name 

55 

56 

57def test_get(module_list_test): 

58 assert module_list_test.modules.get("a") == module_list_test.module_a 

59 assert module_list_test.modules.get("c") == module_list_test.module_c 

60 

61 with pytest.raises(ValueError) as exception_info: 

62 module_list_test.modules.get("d") 

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

64 

65 

66def test_iteration(module_list_test): 

67 expected = [module_list_test.module_a, module_list_test.module_b, module_list_test.module_c] 

68 

69 # pylint: disable=unnecessary-comprehension 

70 assert [module for module in module_list_test.modules] == expected 

71 assert list(module_list_test.modules) == expected 

72 

73 

74def test_can_cast_to_string_without_error(module_list_test): 

75 print(module_list_test.modules) 

76 

77 

78def test_concatenation(module_list_test): 

79 modules_2 = ModuleList() 

80 

81 module_d = MagicMock() 

82 module_d.name = "d" 

83 modules_2.append(module_d) 

84 

85 all_modules = module_list_test.modules + modules_2 

86 

87 assert len(all_modules) == 4 

88 assert list(all_modules) == [ 

89 module_list_test.module_a, 

90 module_list_test.module_b, 

91 module_list_test.module_c, 

92 module_d, 

93 ] 

94 

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

96 assert len(module_list_test.modules) == 3 

97 assert len(modules_2) == 1 

98 

99 assert (all_modules is not modules_2) and (all_modules is not module_list_test.modules) 

100 

101 # pylint: disable=protected-access 

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

103 all_modules._modules is not module_list_test.modules._modules 

104 ) 

105 

106 

107def test_concatenation_with_unknown_object_should_raise_interrupt(module_list_test): 

108 with pytest.raises(TypeError) as exception_info: 

109 # pylint: disable=pointless-statement 

110 module_list_test.modules + 3 

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

112 

113 

114def test_copy(module_list_test): 

115 modules_copy = module_list_test.modules.copy() 

116 

117 modules_copy.append(4) 

118 assert len(module_list_test.modules) == 3