Coverage for hdl_registers/parser/test/test_parser/test_parser_field.py: 100%
48 statements
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-19 20:51 +0000
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-19 20:51 +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
18def test_register_field_without_type_property_should_raise_exception(tmp_path):
19 toml_path = create_file(
20 file=tmp_path / "regs.toml",
21 contents="""
22[apa]
24mode = "r_w"
26hest.width = 4
27""",
28 )
30 with pytest.raises(ValueError) as exception_info:
31 from_toml(name="", toml_file=toml_path)
32 assert str(exception_info.value) == (
33 f'Error while parsing field "hest" in register "apa" in {toml_path}: '
34 'Missing required property "type".'
35 )
38def test_array_register_field_without_type_property_should_raise_exception(tmp_path):
39 toml_path = create_file(
40 file=tmp_path / "regs.toml",
41 contents="""
42[apa]
44type = "register_array"
45array_length = 2
47[apa.hest]
49mode = "r_w"
51zebra.width = 4
52""",
53 )
55 with pytest.raises(ValueError) as exception_info:
56 from_toml(name="", toml_file=toml_path)
57 assert str(exception_info.value) == (
58 f'Error while parsing field "zebra" in register "hest" within array "apa" in {toml_path}: '
59 'Missing required property "type".'
60 )
63def test_register_field_with_unknown_type_should_raise_exception(tmp_path):
64 toml_path = create_file(
65 file=tmp_path / "regs.toml",
66 contents="""
67[apa]
69mode = "r_w"
71hest.type = "bits"
72hest.width = 4
73""",
74 )
76 with pytest.raises(ValueError) as exception_info:
77 from_toml(name="", toml_file=toml_path)
78 assert str(exception_info.value) == (
79 f'Error while parsing field "hest" in register "apa" in {toml_path}: '
80 'Unknown field type "bits". Expected one of "bit", "bit_vector", "enumeration", "integer".'
81 )
84def test_array_register_field_with_unknown_type_should_raise_exception(tmp_path):
85 toml_path = create_file(
86 file=tmp_path / "regs.toml",
87 contents="""
88[apa]
90type = "register_array"
91array_length = 2
93[apa.hest]
95mode = "r_w"
97zebra.type = "bits"
98zebra.width = 4
99""",
100 )
102 with pytest.raises(ValueError) as exception_info:
103 from_toml(name="", toml_file=toml_path)
104 assert str(exception_info.value) == (
105 f'Error while parsing field "zebra" in register "hest" within array "apa" in {toml_path}: '
106 'Unknown field type "bits". Expected one of "bit", "bit_vector", "enumeration", "integer".'
107 )
110def test_unknown_bit_field_property_should_raise_exception(tmp_path):
111 toml_path = create_file(
112 file=tmp_path / "regs.toml",
113 contents="""
114[dummy_reg]
116mode = "w"
118dummy_bit.type = "bit"
119dummy_bit.description = "Stuff"
121dummy_bit.dummy_integer.max_value = 3
122""",
123 )
125 with pytest.raises(ValueError) as exception_info:
126 from_toml(name="", toml_file=toml_path)
127 assert str(exception_info.value) == (
128 f'Error while parsing field "dummy_bit" in register "dummy_reg" in {toml_path}: '
129 'Unknown property "dummy_integer".'
130 )
133def test_unknown_bit_vector_field_property_should_raise_exception(tmp_path):
134 toml_path = create_file(
135 file=tmp_path / "regs.toml",
136 contents="""
137[apa]
139type = "register_array"
140array_length = 2
142[apa.dummy_reg]
144mode = "w"
146[apa.dummy_reg.dummy_bit_vector]
148type = "bit_vector"
149description = "Stuff"
150width = 3
151height = 4
153""",
154 )
156 with pytest.raises(ValueError) as exception_info:
157 from_toml(name="", toml_file=toml_path)
158 assert str(exception_info.value) == (
159 f'Error while parsing field "dummy_bit_vector" in register "dummy_reg" in '
160 f'{toml_path}: Unknown property "height".'
161 )
164def test_bit_vector_field_without_width_should_raise_exception(tmp_path):
165 toml_path = create_file(
166 file=tmp_path / "regs.toml",
167 contents="""
168[test_reg]
169mode = "w"
171test_bit_vector.type = "bit_vector"
172""",
173 )
175 with pytest.raises(ValueError) as exception_info:
176 from_toml(name="", toml_file=toml_path)
177 assert str(exception_info.value) == (
178 f'Error while parsing field "test_bit_vector" in register "test_reg" in {toml_path}: '
179 'Missing required property "width".'
180 )
183def test_enumeration_field_without_elements_should_raise_exception(tmp_path):
184 toml_path = create_file(
185 file=tmp_path / "regs.toml",
186 contents="""
187[apa]
189type = "register_array"
190array_length = 2
192[apa.test_reg]
194mode = "w"
196test.type = "enumeration"
197""",
198 )
200 with pytest.raises(ValueError) as exception_info:
201 from_toml(name="", toml_file=toml_path)
202 assert str(exception_info.value) == (
203 f'Error while parsing field "test" in register "test_reg" in {toml_path}: '
204 'Missing required property "element".'
205 )
208def test_integer_field_without_max_value_should_raise_exception(tmp_path):
209 toml_path = create_file(
210 file=tmp_path / "regs.toml",
211 contents="""
212[test_reg]
213mode = "w"
215test_integer.type = "integer"
216test_integer.min_value = 3
217""",
218 )
220 with pytest.raises(ValueError) as exception_info:
221 from_toml(name="", toml_file=toml_path)
222 assert str(exception_info.value) == (
223 f'Error while parsing field "test_integer" in register "test_reg" in {toml_path}: '
224 'Missing required property "max_value".'
225 )