Coverage for hdl_registers/constant/test/test_string_constant.py: 100%
23 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# --------------------------------------------------------------------------------------------------
10from copy import copy
12import pytest
14from hdl_registers.constant.string_constant import StringConstant
17def test_constant():
18 for value in ["", "hello"]:
19 constant = StringConstant(name="apa", value=value, description=f"desc {value}")
21 assert constant.name == "apa"
22 assert constant.value == value
23 assert constant.description == f"desc {value}"
26def test_invalid_data_type():
27 with pytest.raises(TypeError) as exception_info:
28 StringConstant(name="apa", value=3.5)
29 assert (
30 str(exception_info.value)
31 == 'Constant "apa" has invalid data type "<class \'float\'>". Value: "3.5".'
32 )
35def test_repr():
36 data = StringConstant(name="apa", value="hello")
38 # Check that repr is an actual representation, not just "X object at 0xABCDEF"
39 assert "apa" in repr(data)
40 assert repr(data) == repr(copy(data))
42 # Different name
43 other = StringConstant(name="hest", value="hello")
44 assert repr(data) != repr(other)
46 # Different value
47 other = StringConstant(name="apa", value="bye")
48 assert repr(data) != repr(other)
50 # Different description
51 data = StringConstant(name="apa", value="hello", description="X")
52 assert repr(data) != repr(other)