Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

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

3# 

4# This file is part of the tsfpga project. 

5# https://tsfpga.com 

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

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

8 

9from unittest import TestCase 

10from unittest.mock import MagicMock 

11 

12import pytest 

13 

14from tsfpga.module_list import ModuleList 

15 

16 

17def test_module_list_indexing(): 

18 modules = ModuleList() 

19 

20 module_a = MagicMock() 

21 module_b = MagicMock() 

22 

23 modules.append(module_a) 

24 modules.append(module_b) 

25 

26 assert modules[0] is module_a 

27 assert modules[1] is module_b 

28 

29 

30class TestModuleList(TestCase): 

31 def setUp(self): 

32 self.modules = ModuleList() 

33 

34 self.module_a = MagicMock() 

35 self.module_b = MagicMock() 

36 self.module_c = MagicMock() 

37 

38 self.module_a.name = "a" 

39 self.module_b.name = "b" 

40 self.module_c.name = "c" 

41 

42 self.modules.append(self.module_a) 

43 self.modules.append(self.module_b) 

44 self.modules.append(self.module_c) 

45 

46 def test_get(self): 

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

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

49 

50 with pytest.raises(ValueError) as exception_info: 

51 self.modules.get("d") 

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

53 

54 def test_iteration(self): 

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

56 

57 # pylint: disable=unnecessary-comprehension 

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

59 assert list(self.modules) == expected 

60 

61 def test_can_cast_to_string_without_error(self): 

62 print(self.modules) 

63 

64 def test_concatenation(self): 

65 modules_2 = ModuleList() 

66 

67 module_d = MagicMock() 

68 module_d.name = "d" 

69 modules_2.append(module_d) 

70 

71 all_modules = self.modules + modules_2 

72 

73 assert len(all_modules) == 4 

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

75 

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

77 assert len(self.modules) == 3 

78 assert len(modules_2) == 1 

79 

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

81 

82 # pylint: disable=protected-access 

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

84 all_modules._modules is not self.modules._modules 

85 ) 

86 

87 def test_concatenation_with_unknown_object_should_raise_interrupt(self): 

88 with pytest.raises(TypeError) as exception_info: 

89 # pylint: disable=pointless-statement 

90 self.modules + 3 

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

92 

93 def test_copy(self): 

94 modules_copy = self.modules.copy() 

95 

96 modules_copy.append(4) 

97 assert len(self.modules) == 3