Coverage for hdl_registers/parser/test/test_parser/test_parser_constant.py: 100%
51 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.constant.bit_vector_constant import UnsignedVectorConstant
14from hdl_registers.constant.string_constant import StringConstant
15from hdl_registers.parser.toml import from_toml
18def test_constants_in_toml(tmp_path):
19 # Test all supported data types
20 toml_path = create_file(
21 file=tmp_path / "regs.toml",
22 contents="""
23[data_width]
25type = "constant"
26value = 0xf
27description = "the width"
29[apa]
31type = "constant"
32value = 3.14
34[hest]
36type = "constant"
37value = true
39[zebra]
41type = "constant"
42value = "foo"
44[base_address_hex]
46type = "constant"
47value = "0xFF01_2345"
48data_type = "unsigned"
50[base_address_bin]
52type = "constant"
53value = "0b1000_0011"
54data_type = "unsigned"
55""",
56 )
58 register_list = from_toml(name="", toml_file=toml_path)
59 assert len(register_list.constants) == 6
61 assert register_list.constants[0].name == "data_width"
62 assert register_list.constants[0].value == 15
63 assert register_list.constants[0].description == "the width"
65 assert register_list.constants[1].name == "apa"
66 assert register_list.constants[1].value == 3.14
67 assert register_list.constants[1].description == ""
69 assert register_list.constants[2].name == "hest"
70 assert register_list.constants[2].value is True
72 assert isinstance(register_list.constants[3], StringConstant)
73 assert register_list.constants[3].name == "zebra"
74 assert register_list.constants[3].value == "foo"
76 assert isinstance(register_list.constants[4], UnsignedVectorConstant)
77 assert register_list.constants[4].name == "base_address_hex"
78 assert register_list.constants[4].value == "FF01_2345"
80 assert isinstance(register_list.constants[5], UnsignedVectorConstant)
81 assert register_list.constants[5].name == "base_address_bin"
82 assert register_list.constants[5].value == "1000_0011"
85def test_constant_without_value_should_raise_exception(tmp_path):
86 toml_path = create_file(
87 file=tmp_path / "regs.toml",
88 contents="""
89[data_width]
91type = "constant"
92description = "the width"
93""",
94 )
95 with pytest.raises(ValueError) as exception_info:
96 from_toml(name="", toml_file=toml_path)
97 assert str(exception_info.value) == (
98 f'Error while parsing constant "data_width" in {toml_path}: '
99 'Missing required property "value".'
100 )
103def test_unknown_constant_property_should_raise_exception(tmp_path):
104 toml_path = create_file(
105 file=tmp_path / "regs.toml",
106 contents="""
107[data_width]
109type = "constant"
110value = 0xf
111default_value = 0xf
112""",
113 )
115 with pytest.raises(ValueError) as exception_info:
116 from_toml(name="", toml_file=toml_path)
117 assert str(exception_info.value) == (
118 f'Error while parsing constant "data_width" in {toml_path}: '
119 'Got unknown property "default_value".'
120 )
123def test_unknown_constant_sub_item_should_raise_exception(tmp_path):
124 toml_path = create_file(
125 file=tmp_path / "regs.toml",
126 contents="""
127[data_width]
129type = "constant"
130value = 0xf
132default_value.value = 0x3
133""",
134 )
136 with pytest.raises(ValueError) as exception_info:
137 from_toml(name="", toml_file=toml_path)
138 assert str(exception_info.value) == (
139 f'Error while parsing constant "data_width" in {toml_path}: '
140 'Got unknown property "default_value".'
141 )
144def test_data_type_on_non_string_constant_should_raise_exception(tmp_path):
145 toml_path = create_file(
146 file=tmp_path / "regs.toml",
147 contents="""
148[data_width]
150type = "constant"
151value = 0xf
152data_type = "unsigned"
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 constant "data_width" in {toml_path}: '
160 'May not set "data_type" for non-string constant.'
161 )
164def test_invalid_string_constant_data_type_should_raise_exception(tmp_path):
165 toml_path = create_file(
166 file=tmp_path / "regs.toml",
167 contents="""
168[data_width]
170type = "constant"
171value = "0xff"
172data_type = "signed"
173""",
174 )
176 with pytest.raises(ValueError) as exception_info:
177 from_toml(name="", toml_file=toml_path)
178 assert str(exception_info.value) == (
179 f'Error while parsing constant "data_width" in {toml_path}: Invalid data type "signed".'
180 )