Coverage for hdl_registers/parser/test/test_parser/test_parser_field.py: 100%
48 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
16def test_register_field_without_type_property_should_raise_exception(tmp_path):
17 toml_path = create_file(
18 file=tmp_path / "regs.toml",
19 contents="""
20[apa]
22mode = "r_w"
24hest.width = 4
25""",
26 )
28 with pytest.raises(ValueError) as exception_info:
29 from_toml(name="", toml_file=toml_path)
30 assert str(exception_info.value) == (
31 f'Error while parsing field "hest" in register "apa" in {toml_path}: '
32 'Missing required property "type".'
33 )
36def test_array_register_field_without_type_property_should_raise_exception(tmp_path):
37 toml_path = create_file(
38 file=tmp_path / "regs.toml",
39 contents="""
40[apa]
42type = "register_array"
43array_length = 2
45[apa.hest]
47mode = "r_w"
49zebra.width = 4
50""",
51 )
53 with pytest.raises(ValueError) as exception_info:
54 from_toml(name="", toml_file=toml_path)
55 assert str(exception_info.value) == (
56 f'Error while parsing field "zebra" in register "hest" within array "apa" in {toml_path}: '
57 'Missing required property "type".'
58 )
61def test_register_field_with_unknown_type_should_raise_exception(tmp_path):
62 toml_path = create_file(
63 file=tmp_path / "regs.toml",
64 contents="""
65[apa]
67mode = "r_w"
69hest.type = "bits"
70hest.width = 4
71""",
72 )
74 with pytest.raises(ValueError) as exception_info:
75 from_toml(name="", toml_file=toml_path)
76 assert str(exception_info.value) == (
77 f'Error while parsing field "hest" in register "apa" in {toml_path}: '
78 'Unknown field type "bits". Expected one of "bit", "bit_vector", "enumeration", "integer".'
79 )
82def test_array_register_field_with_unknown_type_should_raise_exception(tmp_path):
83 toml_path = create_file(
84 file=tmp_path / "regs.toml",
85 contents="""
86[apa]
88type = "register_array"
89array_length = 2
91[apa.hest]
93mode = "r_w"
95zebra.type = "bits"
96zebra.width = 4
97""",
98 )
100 with pytest.raises(ValueError) as exception_info:
101 from_toml(name="", toml_file=toml_path)
102 assert str(exception_info.value) == (
103 f'Error while parsing field "zebra" in register "hest" within array "apa" in {toml_path}: '
104 'Unknown field type "bits". Expected one of "bit", "bit_vector", "enumeration", "integer".'
105 )
108def test_unknown_bit_field_property_should_raise_exception(tmp_path):
109 toml_path = create_file(
110 file=tmp_path / "regs.toml",
111 contents="""
112[dummy_reg]
114mode = "w"
116dummy_bit.type = "bit"
117dummy_bit.description = "Stuff"
119dummy_bit.dummy_integer.max_value = 3
120""",
121 )
123 with pytest.raises(ValueError) as exception_info:
124 from_toml(name="", toml_file=toml_path)
125 assert str(exception_info.value) == (
126 f'Error while parsing field "dummy_bit" in register "dummy_reg" in {toml_path}: '
127 'Unknown property "dummy_integer".'
128 )
131def test_unknown_bit_vector_field_property_should_raise_exception(tmp_path):
132 toml_path = create_file(
133 file=tmp_path / "regs.toml",
134 contents="""
135[apa]
137type = "register_array"
138array_length = 2
140[apa.dummy_reg]
142mode = "w"
144[apa.dummy_reg.dummy_bit_vector]
146type = "bit_vector"
147description = "Stuff"
148width = 3
149height = 4
151""",
152 )
154 with pytest.raises(ValueError) as exception_info:
155 from_toml(name="", toml_file=toml_path)
156 assert str(exception_info.value) == (
157 f'Error while parsing field "dummy_bit_vector" in register "dummy_reg" in '
158 f'{toml_path}: Unknown property "height".'
159 )
162def test_bit_vector_field_without_width_should_raise_exception(tmp_path):
163 toml_path = create_file(
164 file=tmp_path / "regs.toml",
165 contents="""
166[test_reg]
167mode = "w"
169test_bit_vector.type = "bit_vector"
170""",
171 )
173 with pytest.raises(ValueError) as exception_info:
174 from_toml(name="", toml_file=toml_path)
175 assert str(exception_info.value) == (
176 f'Error while parsing field "test_bit_vector" in register "test_reg" in {toml_path}: '
177 'Missing required property "width".'
178 )
181def test_enumeration_field_without_elements_should_raise_exception(tmp_path):
182 toml_path = create_file(
183 file=tmp_path / "regs.toml",
184 contents="""
185[apa]
187type = "register_array"
188array_length = 2
190[apa.test_reg]
192mode = "w"
194test.type = "enumeration"
195""",
196 )
198 with pytest.raises(ValueError) as exception_info:
199 from_toml(name="", toml_file=toml_path)
200 assert str(exception_info.value) == (
201 f'Error while parsing field "test" in register "test_reg" in {toml_path}: '
202 'Missing required property "element".'
203 )
206def test_integer_field_without_max_value_should_raise_exception(tmp_path):
207 toml_path = create_file(
208 file=tmp_path / "regs.toml",
209 contents="""
210[test_reg]
211mode = "w"
213test_integer.type = "integer"
214test_integer.min_value = 3
215""",
216 )
218 with pytest.raises(ValueError) as exception_info:
219 from_toml(name="", toml_file=toml_path)
220 assert str(exception_info.value) == (
221 f'Error while parsing field "test_integer" in register "test_reg" in {toml_path}: '
222 'Missing required property "max_value".'
223 )