Coverage for hdl_registers/parser/test/test_parser/test_parser_register.py: 100%
62 statements
« prev ^ index » next coverage.py v7.6.8, created at 2024-12-01 20:50 +0000
« prev ^ index » next coverage.py v7.6.8, created at 2024-12-01 20:50 +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# --------------------------------------------------------------------------------------------------
10# Third party libraries
11import pytest
12from tsfpga.system_utils import create_file
14# First party libraries
15from hdl_registers.parser.toml import from_toml
16from hdl_registers.register_modes import REGISTER_MODES
19def test_register_can_be_specified_with_and_without_type(tmp_path):
20 toml_path = create_file(
21 file=tmp_path / "regs.toml",
22 contents="""
23[apa]
25mode = "w"
27[hest]
29type = "register"
30mode = "r"
31description = "zebra"
32""",
33 )
34 register_list = from_toml(name="", toml_file=toml_path)
36 assert register_list.get_register("apa").index == 0
37 assert register_list.get_register("apa").mode == REGISTER_MODES["w"]
38 assert register_list.get_register("apa").description == ""
39 assert register_list.get_register("hest").index == 1
40 assert register_list.get_register("hest").mode == REGISTER_MODES["r"]
41 assert register_list.get_register("hest").description == "zebra"
44def test_register_with_no_mode_property_should_raise_exception(tmp_path):
45 toml_path = create_file(
46 file=tmp_path / "regs.toml",
47 contents="""
48[apa]
50description = "w"
51""",
52 )
54 with pytest.raises(ValueError) as exception_info:
55 from_toml(name="", toml_file=toml_path)
56 assert (
57 str(exception_info.value)
58 == f'Error while parsing register "apa" in {toml_path}: Missing required property "mode".'
59 )
62def test_unknown_register_property_should_raise_exception(tmp_path):
63 toml_path = create_file(
64 file=tmp_path / "regs.toml",
65 contents="""
66[test_reg]
68mode = "w"
69dummy = 3
70""",
71 )
73 with pytest.raises(ValueError) as exception_info:
74 from_toml(name="", toml_file=toml_path)
75 assert str(exception_info.value) == (
76 f'Error while parsing register "test_reg" in {toml_path}: Got unknown property "dummy".'
77 )
80def test_array_register_can_be_specified_with_and_without_type(tmp_path):
81 toml_path = create_file(
82 file=tmp_path / "regs.toml",
83 contents="""
84[apa]
86type = "register_array"
87array_length = 2
89[apa.hest]
91mode = "r"
93[apa.zebra]
95type = "register"
96mode = "w"
97description = "stuff"
98""",
99 )
101 register_list = from_toml(name="", toml_file=toml_path)
102 assert register_list.get_register(register_name="hest", register_array_name="apa").index == 0
103 assert (
104 register_list.get_register(register_name="hest", register_array_name="apa").mode
105 == REGISTER_MODES["r"]
106 )
107 assert (
108 register_list.get_register(register_name="hest", register_array_name="apa").description
109 == ""
110 )
111 assert register_list.get_register(register_name="zebra", register_array_name="apa").index == 1
112 assert (
113 register_list.get_register(register_name="zebra", register_array_name="apa").mode
114 == REGISTER_MODES["w"]
115 )
116 assert (
117 register_list.get_register(register_name="zebra", register_array_name="apa").description
118 == "stuff"
119 )
122def test_array_register_with_bad_type_should_raise_exception(tmp_path):
123 toml_path = create_file(
124 file=tmp_path / "regs.toml",
125 contents="""
126[apa]
128type = "register_array"
129array_length = 2
131[apa.hest]
133type = "constant"
134""",
135 )
137 with pytest.raises(ValueError) as exception_info:
138 from_toml(name="", toml_file=toml_path)
139 assert str(exception_info.value) == (
140 f'Error while parsing register "hest" within array "apa" in {toml_path}: '
141 'Got unknown type "constant". Expected "register".'
142 )
145def test_array_register_with_no_mode_property_should_raise_exception(tmp_path):
146 toml_path = create_file(
147 file=tmp_path / "regs.toml",
148 contents="""
149[apa]
151type = "register_array"
152array_length = 2
154[apa.hest]
156description = "nothing"
157""",
158 )
160 with pytest.raises(ValueError) as exception_info:
161 from_toml(name="", toml_file=toml_path)
162 assert str(exception_info.value) == (
163 f'Error while parsing register "hest" within array "apa" in {toml_path}: '
164 'Missing required property "mode".'
165 )
168def test_unknown_array_register_property_should_raise_exception(tmp_path):
169 toml_path = create_file(
170 file=tmp_path / "regs.toml",
171 contents="""
172[test_array]
174type = "register_array"
175array_length = 2
177[test_array.hest]
179mode = "r"
180dummy = 3
181""",
182 )
184 with pytest.raises(ValueError) as exception_info:
185 from_toml(name="", toml_file=toml_path)
186 assert (
187 str(exception_info.value)
188 == f'Error while parsing register "hest" within array "test_array" in {toml_path}: '
189 'Got unknown property "dummy".'
190 )
193def test_plain_register_with_array_length_property_should_raise_exception(tmp_path):
194 toml_path = create_file(
195 file=tmp_path / "regs.toml",
196 contents="""
197[apa]
199mode = "r_w"
200array_length = 4
201""",
202 )
204 with pytest.raises(ValueError) as exception_info:
205 from_toml(name="", toml_file=toml_path)
206 assert str(exception_info.value) == (
207 f'Error while parsing register "apa" in {toml_path}: Got unknown property "array_length".'
208 )
211def test_unknown_register_mode_should_raise_exception(tmp_path):
212 toml_path = create_file(
213 file=tmp_path / "regs.toml",
214 contents="""
215[test_reg]
217mode = "rw"
218""",
219 )
221 with pytest.raises(ValueError) as exception_info:
222 from_toml(name="", toml_file=toml_path)
223 assert str(exception_info.value) == (
224 f'Error while parsing register "test_reg" in {toml_path}: '
225 'Got unknown mode "rw". Expected one of "r", "w", "r_w", "wpulse", "r_wpulse".'
226 )
229def test_unknown_array_register_mode_should_raise_exception(tmp_path):
230 toml_path = create_file(
231 file=tmp_path / "regs.toml",
232 contents="""
233[test_array]
235type = "register_array"
236array_length = 2
238[test_array.hest]
240mode = "r_pulse"
241""",
242 )
244 with pytest.raises(ValueError) as exception_info:
245 from_toml(name="", toml_file=toml_path)
246 assert (
247 str(exception_info.value) == f'Error while parsing register "hest" in {toml_path}: '
248 'Got unknown mode "r_pulse". Expected one of "r", "w", "r_w", "wpulse", "r_wpulse".'
249 )