Coverage for hdl_registers/parser/test/test_parser/test_parser_register.py: 100%
62 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-03-12 11:11 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-03-12 11:11 +0000
1# --------------------------------------------------------------------------------------------------
2# Copyright (c) Lukas Vik. All rights reserved.
3#
4# This file is part of the hdl-registers project, an HDL register generator fast enough to run
5# in real time.
6# https://hdl-registers.com
7# https://github.com/hdl-registers/hdl-registers
8# --------------------------------------------------------------------------------------------------
10import pytest
11from tsfpga.system_utils import create_file
13from hdl_registers.parser.toml import from_toml
14from hdl_registers.register_modes import REGISTER_MODES
17def test_register_can_be_specified_with_and_without_type(tmp_path):
18 toml_path = create_file(
19 file=tmp_path / "regs.toml",
20 contents="""
21[apa]
23mode = "w"
25[hest]
27type = "register"
28mode = "r"
29description = "zebra"
30""",
31 )
32 register_list = from_toml(name="", toml_file=toml_path)
34 assert register_list.get_register("apa").index == 0
35 assert register_list.get_register("apa").mode == REGISTER_MODES["w"]
36 assert register_list.get_register("apa").description == ""
37 assert register_list.get_register("hest").index == 1
38 assert register_list.get_register("hest").mode == REGISTER_MODES["r"]
39 assert register_list.get_register("hest").description == "zebra"
42def test_register_with_no_mode_property_should_raise_exception(tmp_path):
43 toml_path = create_file(
44 file=tmp_path / "regs.toml",
45 contents="""
46[apa]
48description = "w"
49""",
50 )
52 with pytest.raises(ValueError) as exception_info:
53 from_toml(name="", toml_file=toml_path)
54 assert (
55 str(exception_info.value)
56 == f'Error while parsing register "apa" in {toml_path}: Missing required property "mode".'
57 )
60def test_unknown_register_property_should_raise_exception(tmp_path):
61 toml_path = create_file(
62 file=tmp_path / "regs.toml",
63 contents="""
64[test_reg]
66mode = "w"
67dummy = 3
68""",
69 )
71 with pytest.raises(ValueError) as exception_info:
72 from_toml(name="", toml_file=toml_path)
73 assert str(exception_info.value) == (
74 f'Error while parsing register "test_reg" in {toml_path}: Got unknown property "dummy".'
75 )
78def test_array_register_can_be_specified_with_and_without_type(tmp_path):
79 toml_path = create_file(
80 file=tmp_path / "regs.toml",
81 contents="""
82[apa]
84type = "register_array"
85array_length = 2
87[apa.hest]
89mode = "r"
91[apa.zebra]
93type = "register"
94mode = "w"
95description = "stuff"
96""",
97 )
99 register_list = from_toml(name="", toml_file=toml_path)
100 assert register_list.get_register(register_name="hest", register_array_name="apa").index == 0
101 assert (
102 register_list.get_register(register_name="hest", register_array_name="apa").mode
103 == REGISTER_MODES["r"]
104 )
105 assert (
106 register_list.get_register(register_name="hest", register_array_name="apa").description
107 == ""
108 )
109 assert register_list.get_register(register_name="zebra", register_array_name="apa").index == 1
110 assert (
111 register_list.get_register(register_name="zebra", register_array_name="apa").mode
112 == REGISTER_MODES["w"]
113 )
114 assert (
115 register_list.get_register(register_name="zebra", register_array_name="apa").description
116 == "stuff"
117 )
120def test_array_register_with_bad_type_should_raise_exception(tmp_path):
121 toml_path = create_file(
122 file=tmp_path / "regs.toml",
123 contents="""
124[apa]
126type = "register_array"
127array_length = 2
129[apa.hest]
131type = "constant"
132""",
133 )
135 with pytest.raises(ValueError) as exception_info:
136 from_toml(name="", toml_file=toml_path)
137 assert str(exception_info.value) == (
138 f'Error while parsing register "hest" within array "apa" in {toml_path}: '
139 'Got unknown type "constant". Expected "register".'
140 )
143def test_array_register_with_no_mode_property_should_raise_exception(tmp_path):
144 toml_path = create_file(
145 file=tmp_path / "regs.toml",
146 contents="""
147[apa]
149type = "register_array"
150array_length = 2
152[apa.hest]
154description = "nothing"
155""",
156 )
158 with pytest.raises(ValueError) as exception_info:
159 from_toml(name="", toml_file=toml_path)
160 assert str(exception_info.value) == (
161 f'Error while parsing register "hest" within array "apa" in {toml_path}: '
162 'Missing required property "mode".'
163 )
166def test_unknown_array_register_property_should_raise_exception(tmp_path):
167 toml_path = create_file(
168 file=tmp_path / "regs.toml",
169 contents="""
170[test_array]
172type = "register_array"
173array_length = 2
175[test_array.hest]
177mode = "r"
178dummy = 3
179""",
180 )
182 with pytest.raises(ValueError) as exception_info:
183 from_toml(name="", toml_file=toml_path)
184 assert (
185 str(exception_info.value)
186 == f'Error while parsing register "hest" within array "test_array" in {toml_path}: '
187 'Got unknown property "dummy".'
188 )
191def test_plain_register_with_array_length_property_should_raise_exception(tmp_path):
192 toml_path = create_file(
193 file=tmp_path / "regs.toml",
194 contents="""
195[apa]
197mode = "r_w"
198array_length = 4
199""",
200 )
202 with pytest.raises(ValueError) as exception_info:
203 from_toml(name="", toml_file=toml_path)
204 assert str(exception_info.value) == (
205 f'Error while parsing register "apa" in {toml_path}: Got unknown property "array_length".'
206 )
209def test_unknown_register_mode_should_raise_exception(tmp_path):
210 toml_path = create_file(
211 file=tmp_path / "regs.toml",
212 contents="""
213[test_reg]
215mode = "rw"
216""",
217 )
219 with pytest.raises(ValueError) as exception_info:
220 from_toml(name="", toml_file=toml_path)
221 assert str(exception_info.value) == (
222 f'Error while parsing register "test_reg" in {toml_path}: '
223 'Got unknown mode "rw". Expected one of "r", "w", "r_w", "wpulse", "r_wpulse".'
224 )
227def test_unknown_array_register_mode_should_raise_exception(tmp_path):
228 toml_path = create_file(
229 file=tmp_path / "regs.toml",
230 contents="""
231[test_array]
233type = "register_array"
234array_length = 2
236[test_array.hest]
238mode = "r_pulse"
239""",
240 )
242 with pytest.raises(ValueError) as exception_info:
243 from_toml(name="", toml_file=toml_path)
244 assert (
245 str(exception_info.value) == f'Error while parsing register "hest" in {toml_path}: '
246 'Got unknown mode "r_pulse". Expected one of "r", "w", "r_w", "wpulse", "r_wpulse".'
247 )