Coverage for hdl_registers/parser/test/test_parser/test_parser_constant.py: 100%
51 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.constant.bit_vector_constant import UnsignedVectorConstant
16from hdl_registers.constant.string_constant import StringConstant
17from hdl_registers.parser.toml import from_toml
20def test_constants_in_toml(tmp_path):
21 # Test all supported data types
22 toml_path = create_file(
23 file=tmp_path / "regs.toml",
24 contents="""
25[data_width]
27type = "constant"
28value = 0xf
29description = "the width"
31[apa]
33type = "constant"
34value = 3.14
36[hest]
38type = "constant"
39value = true
41[zebra]
43type = "constant"
44value = "foo"
46[base_address_hex]
48type = "constant"
49value = "0xFF01_2345"
50data_type = "unsigned"
52[base_address_bin]
54type = "constant"
55value = "0b1000_0011"
56data_type = "unsigned"
57""",
58 )
60 register_list = from_toml(name="", toml_file=toml_path)
61 assert len(register_list.constants) == 6
63 assert register_list.constants[0].name == "data_width"
64 assert register_list.constants[0].value == 15
65 assert register_list.constants[0].description == "the width"
67 assert register_list.constants[1].name == "apa"
68 assert register_list.constants[1].value == 3.14
69 assert register_list.constants[1].description == ""
71 assert register_list.constants[2].name == "hest"
72 assert register_list.constants[2].value is True
74 assert isinstance(register_list.constants[3], StringConstant)
75 assert register_list.constants[3].name == "zebra"
76 assert register_list.constants[3].value == "foo"
78 assert isinstance(register_list.constants[4], UnsignedVectorConstant)
79 assert register_list.constants[4].name == "base_address_hex"
80 assert register_list.constants[4].value == "FF01_2345"
82 assert isinstance(register_list.constants[5], UnsignedVectorConstant)
83 assert register_list.constants[5].name == "base_address_bin"
84 assert register_list.constants[5].value == "1000_0011"
87def test_constant_without_value_should_raise_exception(tmp_path):
88 toml_path = create_file(
89 file=tmp_path / "regs.toml",
90 contents="""
91[data_width]
93type = "constant"
94description = "the width"
95""",
96 )
97 with pytest.raises(ValueError) as exception_info:
98 from_toml(name="", toml_file=toml_path)
99 assert str(exception_info.value) == (
100 f'Error while parsing constant "data_width" in {toml_path}: '
101 'Missing required property "value".'
102 )
105def test_unknown_constant_property_should_raise_exception(tmp_path):
106 toml_path = create_file(
107 file=tmp_path / "regs.toml",
108 contents="""
109[data_width]
111type = "constant"
112value = 0xf
113default_value = 0xf
114""",
115 )
117 with pytest.raises(ValueError) as exception_info:
118 from_toml(name="", toml_file=toml_path)
119 assert str(exception_info.value) == (
120 f'Error while parsing constant "data_width" in {toml_path}: '
121 'Got unknown property "default_value".'
122 )
125def test_unknown_constant_sub_item_should_raise_exception(tmp_path):
126 toml_path = create_file(
127 file=tmp_path / "regs.toml",
128 contents="""
129[data_width]
131type = "constant"
132value = 0xf
134default_value.value = 0x3
135""",
136 )
138 with pytest.raises(ValueError) as exception_info:
139 from_toml(name="", toml_file=toml_path)
140 assert str(exception_info.value) == (
141 f'Error while parsing constant "data_width" in {toml_path}: '
142 'Got unknown property "default_value".'
143 )
146def test_data_type_on_non_string_constant_should_raise_exception(tmp_path):
147 toml_path = create_file(
148 file=tmp_path / "regs.toml",
149 contents="""
150[data_width]
152type = "constant"
153value = 0xf
154data_type = "unsigned"
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 constant "data_width" in {toml_path}: '
162 'May not set "data_type" for non-string constant.'
163 )
166def test_invalid_string_constant_data_type_should_raise_exception(tmp_path):
167 toml_path = create_file(
168 file=tmp_path / "regs.toml",
169 contents="""
170[data_width]
172type = "constant"
173value = "0xff"
174data_type = "signed"
175""",
176 )
178 with pytest.raises(ValueError) as exception_info:
179 from_toml(name="", toml_file=toml_path)
180 assert str(exception_info.value) == (
181 f'Error while parsing constant "data_width" in {toml_path}: Invalid data type "signed".'
182 )