Coverage for tsfpga/test/test_module_list.py: 100%
58 statements
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-21 20:51 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-21 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# --------------------------------------------------------------------------------------------------
9# Standard libraries
10from unittest.mock import MagicMock
12# Third party libraries
13import pytest
15# First party libraries
16from tsfpga.module_list import ModuleList
19def test_module_list_indexing():
20 modules = ModuleList()
22 module_a = MagicMock()
23 module_b = MagicMock()
25 modules.append(module_a)
26 modules.append(module_b)
28 assert modules[0] is module_a
29 assert modules[1] is module_b
32@pytest.fixture
33def module_list_test():
34 class ModuleListTest:
35 def __init__(self):
36 self.modules = ModuleList()
38 self.module_a = MagicMock()
39 self.module_b = MagicMock()
40 self.module_c = MagicMock()
42 self.module_a.name = "a"
43 self.module_b.name = "b"
44 self.module_c.name = "c"
46 self.modules.append(self.module_a)
47 self.modules.append(self.module_b)
48 self.modules.append(self.module_c)
50 return ModuleListTest()
53# False positive for pytest fixtures
54# pylint: disable=redefined-outer-name
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
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'
66def test_iteration(module_list_test):
67 expected = [module_list_test.module_a, module_list_test.module_b, module_list_test.module_c]
69 # pylint: disable=unnecessary-comprehension
70 assert [module for module in module_list_test.modules] == expected
71 assert list(module_list_test.modules) == expected
74def test_can_cast_to_string_without_error(module_list_test):
75 print(module_list_test.modules)
78def test_concatenation(module_list_test):
79 modules_2 = ModuleList()
81 module_d = MagicMock()
82 module_d.name = "d"
83 modules_2.append(module_d)
85 all_modules = module_list_test.modules + modules_2
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 ]
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
99 assert (all_modules is not modules_2) and (all_modules is not module_list_test.modules)
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 )
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"
114def test_copy(module_list_test):
115 modules_copy = module_list_test.modules.copy()
117 modules_copy.append(4)
118 assert len(module_list_test.modules) == 3