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 

17class TestModuleList(TestCase): 

18 def setUp(self): 

19 self.modules = ModuleList() 

20 

21 self.module_a = MagicMock() 

22 self.module_b = MagicMock() 

23 self.module_c = MagicMock() 

24 

25 self.module_a.name = "a" 

26 self.module_b.name = "b" 

27 self.module_c.name = "c" 

28 

29 self.modules.append(self.module_a) 

30 self.modules.append(self.module_b) 

31 self.modules.append(self.module_c) 

32 

33 def test_get(self): 

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

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

36 

37 with pytest.raises(ValueError) as exception_info: 

38 self.modules.get("d") 

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

40 

41 def test_iteration(self): 

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

43 

44 # pylint: disable=unnecessary-comprehension 

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

46 assert list(self.modules) == expected 

47 

48 def test_can_cast_to_string_without_error(self): 

49 print(self.modules) 

50 

51 def test_concatenation(self): 

52 modules_2 = ModuleList() 

53 

54 module_d = MagicMock() 

55 module_d.name = "d" 

56 modules_2.append(module_d) 

57 

58 all_modules = self.modules + modules_2 

59 

60 assert len(all_modules) == 4 

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

62 

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

64 assert len(self.modules) == 3 

65 assert len(modules_2) == 1 

66 

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

68 

69 # pylint: disable=protected-access 

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

71 all_modules._modules is not self.modules._modules 

72 ) 

73 

74 def test_concatenation_with_unknown_object_should_raise_interrupt(self): 

75 with pytest.raises(TypeError) as exception_info: 

76 # pylint: disable=pointless-statement 

77 self.modules + 3 

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

79 

80 def test_copy(self): 

81 modules_copy = self.modules.copy() 

82 

83 modules_copy.append(4) 

84 assert len(self.modules) == 3